source: trunk/include/ws_functions/pwg.php @ 29017

Last change on this file since 29017 was 27371, checked in by rvelices, 11 years ago

web service always return pwg version (allow in the future mobile apps to maintain compatibility)

File size: 8.7 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based photo gallery                                    |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2014 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 * API method
26 * Returns a list of missing derivatives (not generated yet)
27 * @param mixed[] $params
28 *    @option string types (optional)
29 *    @option int[] ids
30 *    @option int max_urls
31 *    @option int prev_page (optional)
32 */
33function ws_getMissingDerivatives($params, &$service)
34{
35  global $conf;
36
37  if (empty($params['types']))
38  {
39    $types = array_keys(ImageStdParams::get_defined_type_map());
40  }
41  else
42  {
43    $types = array_intersect(array_keys(ImageStdParams::get_defined_type_map()), $params['types']);
44    if (count($types)==0)
45    {
46      return new PwgError(WS_ERR_INVALID_PARAM, "Invalid types");
47    }
48  }
49
50  $max_urls = $params['max_urls'];
51  $query = 'SELECT MAX(id)+1, COUNT(*) FROM '. IMAGES_TABLE .';';
52  list($max_id, $image_count) = pwg_db_fetch_row(pwg_query($query));
53
54  if (0 == $image_count)
55  {
56    return array();
57  }
58
59  $start_id = $params['prev_page'];
60  if ($start_id<=0)
61  {
62    $start_id = $max_id;
63  }
64
65  $uid = '&b='.time();
66
67  $conf['question_mark_in_urls'] = $conf['php_extension_in_urls'] = true;
68  $conf['derivative_url_style'] = 2; //script
69
70  $qlimit = min(5000, ceil(max($image_count/500, $max_urls/count($types))));
71  $where_clauses = ws_std_image_sql_filter( $params, '' );
72  $where_clauses[] = 'id<start_id';
73
74  if (!empty($params['ids']))
75  {
76    $where_clauses[] = 'id IN ('.implode(',',$params['ids']).')';
77  }
78
79  $query_model = '
80SELECT id, path, representative_ext, width, height, rotation
81  FROM '. IMAGES_TABLE .'
82  WHERE '. implode(' AND ', $where_clauses) .'
83  ORDER BY id DESC
84  LIMIT '. $qlimit .'
85;';
86
87  $urls = array();
88  do
89  {
90    $result = pwg_query(str_replace('start_id', $start_id, $query_model));
91    $is_last = pwg_db_num_rows($result) < $qlimit;
92
93    while ($row=pwg_db_fetch_assoc($result))
94    {
95      $start_id = $row['id'];
96      $src_image = new SrcImage($row);
97      if ($src_image->is_mimetype())
98      {
99        continue;
100      }
101
102      foreach($types as $type)
103      {
104        $derivative = new DerivativeImage($type, $src_image);
105        if ($type != $derivative->get_type())
106        {
107          continue;
108        }
109        if (@filemtime($derivative->get_path())===false)
110        {
111          $urls[] = $derivative->get_url().$uid;
112        }
113      }
114
115      if (count($urls)>=$max_urls and !$is_last)
116      {
117        break;
118      }
119    }
120    if ($is_last)
121    {
122      $start_id = 0;
123    }
124  } while (count($urls)<$max_urls and $start_id);
125
126  $ret = array();
127  if ($start_id)
128  {
129    $ret['next_page'] = $start_id;
130  }
131  $ret['urls'] = $urls;
132  return $ret;
133}
134
135/**
136 * API method
137 * Returns Piwigo version
138 * @param mixed[] $params
139 */
140function ws_getVersion($params, &$service)
141{
142  return PHPWG_VERSION;
143}
144
145/**
146 * API method
147 * Returns general informations about the installation
148 * @param mixed[] $params
149 */
150function ws_getInfos($params, &$service)
151{
152  $infos['version'] = PHPWG_VERSION;
153
154  $query = 'SELECT COUNT(*) FROM '.IMAGES_TABLE.';';
155  list($infos['nb_elements']) = pwg_db_fetch_row(pwg_query($query));
156
157  $query = 'SELECT COUNT(*) FROM '.CATEGORIES_TABLE.';';
158  list($infos['nb_categories']) = pwg_db_fetch_row(pwg_query($query));
159
160  $query = 'SELECT COUNT(*) FROM '.CATEGORIES_TABLE.' WHERE dir IS NULL;';
161  list($infos['nb_virtual']) = pwg_db_fetch_row(pwg_query($query));
162
163  $query = 'SELECT COUNT(*) FROM '.CATEGORIES_TABLE.' WHERE dir IS NOT NULL;';
164  list($infos['nb_physical']) = pwg_db_fetch_row(pwg_query($query));
165
166  $query = 'SELECT COUNT(*) FROM '.IMAGE_CATEGORY_TABLE.';';
167  list($infos['nb_image_category']) = pwg_db_fetch_row(pwg_query($query));
168
169  $query = 'SELECT COUNT(*) FROM '.TAGS_TABLE.';';
170  list($infos['nb_tags']) = pwg_db_fetch_row(pwg_query($query));
171
172  $query = 'SELECT COUNT(*) FROM '.IMAGE_TAG_TABLE.';';
173  list($infos['nb_image_tag']) = pwg_db_fetch_row(pwg_query($query));
174
175  $query = 'SELECT COUNT(*) FROM '.USERS_TABLE.';';
176  list($infos['nb_users']) = pwg_db_fetch_row(pwg_query($query));
177
178  $query = 'SELECT COUNT(*) FROM '.GROUPS_TABLE.';';
179  list($infos['nb_groups']) = pwg_db_fetch_row(pwg_query($query));
180
181  $query = 'SELECT COUNT(*) FROM '.COMMENTS_TABLE.';';
182  list($infos['nb_comments']) = pwg_db_fetch_row(pwg_query($query));
183
184  // first element
185  if ($infos['nb_elements'] > 0)
186  {
187    $query = 'SELECT MIN(date_available) FROM '.IMAGES_TABLE.';';
188    list($infos['first_date']) = pwg_db_fetch_row(pwg_query($query));
189  }
190
191  // unvalidated comments
192  if ($infos['nb_comments'] > 0)
193  {
194    $query = 'SELECT COUNT(*) FROM '.COMMENTS_TABLE.' WHERE validated=\'false\';';
195    list($infos['nb_unvalidated_comments']) = pwg_db_fetch_row(pwg_query($query));
196  }
197
198  foreach ($infos as $name => $value)
199  {
200    $output[] = array(
201      'name' => $name,
202      'value' => $value,
203    );
204  }
205
206  return array('infos' => new PwgNamedArray($output, 'item'));
207}
208
209/**
210 * API method
211 * Adds images to the caddie
212 * @param mixed[] $params
213 *    @option int[] image_id
214 */
215function ws_caddie_add($params, &$service)
216{
217  global $user;
218
219  $query = '
220SELECT id
221  FROM '. IMAGES_TABLE .'
222      LEFT JOIN '. CADDIE_TABLE .'
223      ON id=element_id AND user_id='. $user['id'] .'
224  WHERE id IN ('. implode(',',$params['image_id']) .')
225    AND element_id IS NULL
226;';
227  $result = array_from_query($query, 'id');
228
229  $datas = array();
230  foreach ($result as $id)
231  {
232    $datas[] = array(
233      'element_id' => $id,
234      'user_id' => $user['id'],
235      );
236  }
237  if (count($datas))
238  {
239    mass_inserts(
240      CADDIE_TABLE,
241      array('element_id','user_id'),
242      $datas
243      );
244  }
245  return count($datas);
246}
247
248/**
249 * API method
250 * Deletes rates of an user
251 * @param mixed[] $params
252 *    @option int user_id
253 *    @option string anonymous_id (optional)
254 */
255function ws_rates_delete($params, &$service)
256{
257  $query = '
258DELETE FROM '. RATE_TABLE .'
259  WHERE user_id='. $params['user_id'];
260
261  if (!empty($params['anonymous_id']))
262  {
263    $query .= ' AND anonymous_id=\''.$params['anonymous_id'].'\'';
264  }
265  if (!empty($params['image_id']))
266  {
267    $query .= ' AND element_id='.$params['image_id'];
268  }
269
270  $changes = pwg_db_changes(pwg_query($query));
271  if ($changes)
272  {
273    include_once(PHPWG_ROOT_PATH.'include/functions_rate.inc.php');
274    update_rating_score();
275  }
276  return $changes;
277}
278
279/**
280 * API method
281 * Performs a login
282 * @param mixed[] $params
283 *    @option string username
284 *    @option string password
285 */
286function ws_session_login($params, &$service)
287{
288  if (try_log_user($params['username'], $params['password'], false))
289  {
290    return true;
291  }
292  return new PwgError(999, 'Invalid username/password');
293}
294
295
296/**
297 * API method
298 * Performs a logout
299 * @param mixed[] $params
300 */
301function ws_session_logout($params, &$service)
302{
303  if (!is_a_guest())
304  {
305    logout_user();
306  }
307  return true;
308}
309
310/**
311 * API method
312 * Returns info about the current user
313 * @param mixed[] $params
314 */
315function ws_session_getStatus($params, &$service)
316{
317  global $user;
318
319  $res['username'] = is_a_guest() ? 'guest' : stripslashes($user['username']);
320  foreach ( array('status', 'theme', 'language') as $k )
321  {
322    $res[$k] = $user[$k];
323  }
324  $res['pwg_token'] = get_pwg_token();
325  $res['charset'] = get_pwg_charset();
326
327  list($dbnow) = pwg_db_fetch_row(pwg_query('SELECT NOW();'));
328  $res['current_datetime'] = $dbnow;
329  $res['version'] = PHPWG_VERSION;
330  return $res;
331}
332
333?>
Note: See TracBrowser for help on using the repository browser.