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

Last change on this file since 25757 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
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based photo gallery                                    |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2013 Piwigo Team                  http://piwigo.org |
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// +-----------------------------------------------------------------------+
23
24/**
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{
30  global $conf;
31
32  if ( strpos($methodName,'reflection.')===0 )
33  { // OK for reflection
34    return $res;
35  }
36
37  if ( !is_autorize_status(ACCESS_GUEST) and
38      strpos($methodName,'pwg.session.')!==0 )
39  {
40    return new PwgError(401, 'Access denied');
41  }
42
43  return $res;
44}
45
46/**
47 * returns a "standard" (for our web service) array of sql where clauses that
48 * filters the images (images table only)
49 */
50function ws_std_image_sql_filter( $params, $tbl_name='' )
51{
52  $clauses = array();
53  if ( is_numeric($params['f_min_rate']) )
54  {
55    $clauses[] = $tbl_name.'rating_score>='.$params['f_min_rate'];
56  }
57  if ( is_numeric($params['f_max_rate']) )
58  {
59    $clauses[] = $tbl_name.'rating_score<='.$params['f_max_rate'];
60  }
61  if ( is_numeric($params['f_min_hit']) )
62  {
63    $clauses[] = $tbl_name.'hit>='.$params['f_min_hit'];
64  }
65  if ( is_numeric($params['f_max_hit']) )
66  {
67    $clauses[] = $tbl_name.'hit<='.$params['f_max_hit'];
68  }
69  if ( isset($params['f_min_date_available']) )
70  {
71    $clauses[] = $tbl_name."date_available>='".$params['f_min_date_available']."'";
72  }
73  if ( isset($params['f_max_date_available']) )
74  {
75    $clauses[] = $tbl_name."date_available<'".$params['f_max_date_available']."'";
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  {
87    $clauses[] = $tbl_name.'width/'.$tbl_name.'height>='.$params['f_min_ratio'];
88  }
89  if ( is_numeric($params['f_max_ratio']) )
90  {
91    $clauses[] = $tbl_name.'width/'.$tbl_name.'height<='.$params['f_max_ratio'];
92  }
93  if (is_numeric($params['f_max_level']) )
94  {
95    $clauses[] = $tbl_name.'level <= '.$params['f_max_level'];
96  }
97  return $clauses;
98}
99
100/**
101 * returns a "standard" (for our web service) ORDER BY sql clause for images
102 */
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':
122        $matches[1][$i] = DB_RANDOM_FUNCTION.'()'; break;
123    }
124    $sortable_fields = array('id', 'file', 'name', 'hit', 'rating_score',
125      'date_creation', 'date_available', DB_RANDOM_FUNCTION.'()' );
126    if ( in_array($matches[1][$i], $sortable_fields) )
127    {
128      if (!empty($ret))
129        $ret .= ', ';
130      if ($matches[1][$i] != DB_RANDOM_FUNCTION.'()' )
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
144 */
145function ws_std_get_urls($image_row)
146{
147  $ret = array();
148
149  $ret['page_url'] = make_picture_url( array(
150            'image_id' => $image_row['id'],
151            'image_file' => $image_row['file'],
152          )
153        );
154
155  $src_image = new SrcImage($image_row);
156
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
166  {
167    $ret['element_url'] = get_element_url($image_row);
168  }
169
170  $derivatives = DerivativeImage::get_all($src_image);
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;;
179  return $ret;
180}
181
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(
189    'id','element_url', 'page_url', 'file','width','height','hit','date_available','date_creation'
190    );
191}
192
193function ws_std_get_category_xml_attributes()
194{
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
207/**
208 * Writes info to the log file
209 */
210function ws_logfile($string)
211{
212  global $conf;
213
214  if (!$conf['ws_enable_log'])
215  {
216    return true;
217  }
218
219  file_put_contents(
220    $conf['ws_log_filepath'],
221    '['.date('c').'] '.$string."\n",
222    FILE_APPEND
223    );
224}
225
226/**
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    {
240      $tree[] = &$node;
241    }
242    else
243    {
244      if (!isset($categories[ $key_of_cat[ $node['id_uppercat'] ] ]['sub_categories']))
245      {
246        $categories[ $key_of_cat[ $node['id_uppercat'] ] ]['sub_categories'] =
247          new PwgNamedArray(array(), 'category', ws_std_get_category_xml_attributes());
248      }
249
250      $categories[ $key_of_cat[ $node['id_uppercat'] ] ]['sub_categories']->_content[] = &$node;
251    }
252  }
253
254  return $tree;
255}
256
257?>
Note: See TracBrowser for help on using the repository browser.