source: trunk/include/ws_functions.inc.php @ 25394

Last change on this file since 25394 was 25281, checked in by mistic100, 11 years ago

splits ws_functions.inc.php in 8 files + comments + code cleaning

  • Property svn:eol-style set to LF
File size: 7.3 KB
RevLine 
[1698]1<?php
2// +-----------------------------------------------------------------------+
[8728]3// | Piwigo - a PHP based photo gallery                                    |
[2297]4// +-----------------------------------------------------------------------+
[19703]5// | Copyright(C) 2008-2013 Piwigo Team                  http://piwigo.org |
[2297]6// | Copyright(C) 2003-2008 PhpWebGallery Team    http://phpwebgallery.net |
7// | Copyright(C) 2002-2003 Pierrick LE GALL   http://le-gall.net/pierrick |
8// +-----------------------------------------------------------------------+
9// | This program is free software; you can redistribute it and/or modify  |
10// | it under the terms of the GNU General Public License as published by  |
11// | the Free Software Foundation                                          |
12// |                                                                       |
13// | This program is distributed in the hope that it will be useful, but   |
14// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
15// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
16// | General Public License for more details.                              |
17// |                                                                       |
18// | You should have received a copy of the GNU General Public License     |
19// | along with this program; if not, write to the Free Software           |
20// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
21// | USA.                                                                  |
22// +-----------------------------------------------------------------------+
[1698]23
24/**
[1768]25 * Event handler for method invocation security check. Should return a PwgError
26 * if the preconditions are not satifsied for method invocation.
27 */
28function ws_isInvokeAllowed($res, $methodName, $params)
29{
[2572]30  global $conf;
[2119]31
[1849]32  if ( strpos($methodName,'reflection.')===0 )
33  { // OK for reflection
34    return $res;
35  }
[2119]36
[1849]37  if ( !is_autorize_status(ACCESS_GUEST) and
38      strpos($methodName,'pwg.session.')!==0 )
[1768]39  {
[1849]40    return new PwgError(401, 'Access denied');
41  }
[2119]42
[1768]43  return $res;
44}
45
46/**
[1698]47 * returns a "standard" (for our web service) array of sql where clauses that
[1711]48 * filters the images (images table only)
49 */
[1698]50function ws_std_image_sql_filter( $params, $tbl_name='' )
51{
52  $clauses = array();
53  if ( is_numeric($params['f_min_rate']) )
54  {
[21799]55    $clauses[] = $tbl_name.'rating_score>='.$params['f_min_rate'];
[1698]56  }
57  if ( is_numeric($params['f_max_rate']) )
58  {
[11893]59    $clauses[] = $tbl_name.'rating_score<='.$params['f_max_rate'];
[1698]60  }
61  if ( is_numeric($params['f_min_hit']) )
62  {
[21799]63    $clauses[] = $tbl_name.'hit>='.$params['f_min_hit'];
[1698]64  }
65  if ( is_numeric($params['f_max_hit']) )
66  {
67    $clauses[] = $tbl_name.'hit<='.$params['f_max_hit'];
68  }
[9576]69  if ( isset($params['f_min_date_available']) )
[1698]70  {
[9576]71    $clauses[] = $tbl_name."date_available>='".$params['f_min_date_available']."'";
[1698]72  }
[9576]73  if ( isset($params['f_max_date_available']) )
[1698]74  {
[9576]75    $clauses[] = $tbl_name."date_available<'".$params['f_max_date_available']."'";
[1698]76  }
77  if ( isset($params['f_min_date_created']) )
78  {
79    $clauses[] = $tbl_name."date_creation>='".$params['f_min_date_created']."'";
80  }
81  if ( isset($params['f_max_date_created']) )
82  {
83    $clauses[] = $tbl_name."date_creation<'".$params['f_max_date_created']."'";
84  }
85  if ( is_numeric($params['f_min_ratio']) )
86  {
[21799]87    $clauses[] = $tbl_name.'width/'.$tbl_name.'height>='.$params['f_min_ratio'];
[1698]88  }
89  if ( is_numeric($params['f_max_ratio']) )
90  {
91    $clauses[] = $tbl_name.'width/'.$tbl_name.'height<='.$params['f_max_ratio'];
92  }
[12865]93  if (is_numeric($params['f_max_level']) )
[1698]94  {
[12865]95    $clauses[] = $tbl_name.'level <= '.$params['f_max_level'];
[1698]96  }
97  return $clauses;
98}
99
100/**
101 * returns a "standard" (for our web service) ORDER BY sql clause for images
[1711]102 */
[1698]103function ws_std_image_sql_order( $params, $tbl_name='' )
104{
105  $ret = '';
106  if ( empty($params['order']) )
107  {
108    return $ret;
109  }
110  $matches = array();
111  preg_match_all('/([a-z_]+) *(?:(asc|desc)(?:ending)?)? *(?:, *|$)/i',
112    $params['order'], $matches);
113  for ($i=0; $i<count($matches[1]); $i++)
114  {
115    switch ($matches[1][$i])
116    {
117      case 'date_created':
118        $matches[1][$i] = 'date_creation'; break;
119      case 'date_posted':
120        $matches[1][$i] = 'date_available'; break;
121      case 'rand': case 'random':
[4367]122        $matches[1][$i] = DB_RANDOM_FUNCTION.'()'; break;
[1698]123    }
[11893]124    $sortable_fields = array('id', 'file', 'name', 'hit', 'rating_score',
[4367]125      'date_creation', 'date_available', DB_RANDOM_FUNCTION.'()' );
[1698]126    if ( in_array($matches[1][$i], $sortable_fields) )
127    {
128      if (!empty($ret))
129        $ret .= ', ';
[4367]130      if ($matches[1][$i] != DB_RANDOM_FUNCTION.'()' )
[1698]131      {
132        $ret .= $tbl_name;
133      }
134      $ret .= $matches[1][$i];
135      $ret .= ' '.$matches[2][$i];
136    }
137  }
138  return $ret;
139}
140
141/**
142 * returns an array map of urls (thumb/element) for image_row - to be returned
143 * in a standard way by different web service methods
[1711]144 */
[1698]145function ws_std_get_urls($image_row)
146{
[12796]147  $ret = array();
[12865]148
[18732]149  $ret['page_url'] = make_picture_url( array(
150            'image_id' => $image_row['id'],
151            'image_file' => $image_row['file'],
152          )
153        );
154
[12855]155  $src_image = new SrcImage($image_row);
156
[17469]157  if ( $src_image->is_original() )
158  {// we have a photo
159    global $user;
160    if ($user['enabled_high'])
161    {
162      $ret['element_url'] = $src_image->get_url();
163    }
164  }
165  else
[1698]166  {
[12796]167    $ret['element_url'] = get_element_url($image_row);
[1698]168  }
[12865]169
[12855]170  $derivatives = DerivativeImage::get_all($src_image);
[12796]171  $derivatives_arr = array();
172  foreach($derivatives as $type=>$derivative)
173  {
174    $size = $derivative->get_size();
175    $size != null or $size=array(null,null);
176    $derivatives_arr[$type] = array('url' => $derivative->get_url(), 'width'=>$size[0], 'height'=>$size[1] );
177  }
178  $ret['derivatives'] = $derivatives_arr;;
[1698]179  return $ret;
180}
181
[1845]182/**
183 * returns an array of image attributes that are to be encoded as xml attributes
184 * instead of xml elements
185 */
186function ws_std_get_image_xml_attributes()
187{
188  return array(
[18732]189    'id','element_url', 'page_url', 'file','width','height','hit','date_available','date_creation'
[1845]190    );
191}
[1698]192
[22729]193function ws_std_get_category_xml_attributes()
[12865]194{
[22729]195  return array(
196    'id', 'url', 'nb_images', 'total_nb_images', 'nb_categories', 'date_last', 'max_date_last',
197    );
198}
199
200function ws_std_get_tag_xml_attributes()
201{
202  return array(
203    'id', 'name', 'url_name', 'counter', 'url', 'page_url',
204    );
205}
206
[1781]207/**
[25281]208 * Writes info to the log file
[1781]209 */
[25281]210function ws_logfile($string)
[1698]211{
[1852]212  global $conf;
[1698]213
[25281]214  if (!$conf['ws_enable_log'])
[10017]215  {
[25281]216    return true;
[10017]217  }
218
[25281]219  file_put_contents(
220    $conf['ws_log_filepath'],
221    '['.date('c').'] '.$string."\n",
222    FILE_APPEND
[10017]223    );
224}
225
[1698]226/**
[13074]227 * create a tree from a flat list of categories, no recursivity for high speed
228 */
229function categories_flatlist_to_tree($categories)
230{
231  $tree = array();
232  $key_of_cat = array();
233
234  foreach ($categories as $key => &$node)
235  {
236    $key_of_cat[$node['id']] = $key;
237
238    if (!isset($node['id_uppercat']))
239    {
[22729]240      $tree[] = &$node;
[13074]241    }
242    else
243    {
244      if (!isset($categories[ $key_of_cat[ $node['id_uppercat'] ] ]['sub_categories']))
245      {
[25281]246        $categories[ $key_of_cat[ $node['id_uppercat'] ] ]['sub_categories'] =
247          new PwgNamedArray(array(), 'category', ws_std_get_category_xml_attributes());
[13074]248      }
249
[22729]250      $categories[ $key_of_cat[ $node['id_uppercat'] ] ]['sub_categories']->_content[] = &$node;
[13074]251    }
252  }
253
254  return $tree;
255}
256
[25117]257?>
Note: See TracBrowser for help on using the repository browser.