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

Last change on this file since 26837 was 26837, checked in by rvelices, 10 years ago

admin rating improvements

  • use web service for deletion
  • use datatables for rating by user
  • use fontello icons
File size: 8.8 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  global $conf;
143
144  if ($conf['show_version'] or is_admin())
145  {
146    return PHPWG_VERSION;
147  }
148  else
149  {
150    return new PwgError(403, 'Forbidden');
151  }
152}
153
154/**
155 * API method
156 * Returns general informations about the installation
157 * @param mixed[] $params
158 */
159function ws_getInfos($params, &$service)
160{
161  $infos['version'] = PHPWG_VERSION;
162
163  $query = 'SELECT COUNT(*) FROM '.IMAGES_TABLE.';';
164  list($infos['nb_elements']) = pwg_db_fetch_row(pwg_query($query));
165
166  $query = 'SELECT COUNT(*) FROM '.CATEGORIES_TABLE.';';
167  list($infos['nb_categories']) = pwg_db_fetch_row(pwg_query($query));
168
169  $query = 'SELECT COUNT(*) FROM '.CATEGORIES_TABLE.' WHERE dir IS NULL;';
170  list($infos['nb_virtual']) = pwg_db_fetch_row(pwg_query($query));
171
172  $query = 'SELECT COUNT(*) FROM '.CATEGORIES_TABLE.' WHERE dir IS NOT NULL;';
173  list($infos['nb_physical']) = pwg_db_fetch_row(pwg_query($query));
174
175  $query = 'SELECT COUNT(*) FROM '.IMAGE_CATEGORY_TABLE.';';
176  list($infos['nb_image_category']) = pwg_db_fetch_row(pwg_query($query));
177
178  $query = 'SELECT COUNT(*) FROM '.TAGS_TABLE.';';
179  list($infos['nb_tags']) = pwg_db_fetch_row(pwg_query($query));
180
181  $query = 'SELECT COUNT(*) FROM '.IMAGE_TAG_TABLE.';';
182  list($infos['nb_image_tag']) = pwg_db_fetch_row(pwg_query($query));
183
184  $query = 'SELECT COUNT(*) FROM '.USERS_TABLE.';';
185  list($infos['nb_users']) = pwg_db_fetch_row(pwg_query($query));
186
187  $query = 'SELECT COUNT(*) FROM '.GROUPS_TABLE.';';
188  list($infos['nb_groups']) = pwg_db_fetch_row(pwg_query($query));
189
190  $query = 'SELECT COUNT(*) FROM '.COMMENTS_TABLE.';';
191  list($infos['nb_comments']) = pwg_db_fetch_row(pwg_query($query));
192
193  // first element
194  if ($infos['nb_elements'] > 0)
195  {
196    $query = 'SELECT MIN(date_available) FROM '.IMAGES_TABLE.';';
197    list($infos['first_date']) = pwg_db_fetch_row(pwg_query($query));
198  }
199
200  // unvalidated comments
201  if ($infos['nb_comments'] > 0)
202  {
203    $query = 'SELECT COUNT(*) FROM '.COMMENTS_TABLE.' WHERE validated=\'false\';';
204    list($infos['nb_unvalidated_comments']) = pwg_db_fetch_row(pwg_query($query));
205  }
206
207  foreach ($infos as $name => $value)
208  {
209    $output[] = array(
210      'name' => $name,
211      'value' => $value,
212    );
213  }
214
215  return array('infos' => new PwgNamedArray($output, 'item'));
216}
217
218/**
219 * API method
220 * Adds images to the caddie
221 * @param mixed[] $params
222 *    @option int[] image_id
223 */
224function ws_caddie_add($params, &$service)
225{
226  global $user;
227
228  $query = '
229SELECT id
230  FROM '. IMAGES_TABLE .'
231      LEFT JOIN '. CADDIE_TABLE .'
232      ON id=element_id AND user_id='. $user['id'] .'
233  WHERE id IN ('. implode(',',$params['image_id']) .')
234    AND element_id IS NULL
235;';
236  $result = array_from_query($query, 'id');
237
238  $datas = array();
239  foreach ($result as $id)
240  {
241    $datas[] = array(
242      'element_id' => $id,
243      'user_id' => $user['id'],
244      );
245  }
246  if (count($datas))
247  {
248    mass_inserts(
249      CADDIE_TABLE,
250      array('element_id','user_id'),
251      $datas
252      );
253  }
254  return count($datas);
255}
256
257/**
258 * API method
259 * Deletes rates of an user
260 * @param mixed[] $params
261 *    @option int user_id
262 *    @option string anonymous_id (optional)
263 */
264function ws_rates_delete($params, &$service)
265{
266  $query = '
267DELETE FROM '. RATE_TABLE .'
268  WHERE user_id='. $params['user_id'];
269
270  if (!empty($params['anonymous_id']))
271  {
272    $query .= ' AND anonymous_id=\''.$params['anonymous_id'].'\'';
273  }
274  if (!empty($params['image_id']))
275  {
276    $query .= ' AND element_id='.$params['image_id'];
277  }
278
279  $changes = pwg_db_changes(pwg_query($query));
280  if ($changes)
281  {
282    include_once(PHPWG_ROOT_PATH.'include/functions_rate.inc.php');
283    update_rating_score();
284  }
285  return $changes;
286}
287
288/**
289 * API method
290 * Performs a login
291 * @param mixed[] $params
292 *    @option string username
293 *    @option string password
294 */
295function ws_session_login($params, &$service)
296{
297  if (try_log_user($params['username'], $params['password'], false))
298  {
299    return true;
300  }
301  return new PwgError(999, 'Invalid username/password');
302}
303
304
305/**
306 * API method
307 * Performs a logout
308 * @param mixed[] $params
309 */
310function ws_session_logout($params, &$service)
311{
312  if (!is_a_guest())
313  {
314    logout_user();
315  }
316  return true;
317}
318
319/**
320 * API method
321 * Returns info about the current user
322 * @param mixed[] $params
323 */
324function ws_session_getStatus($params, &$service)
325{
326  global $user;
327
328  $res['username'] = is_a_guest() ? 'guest' : stripslashes($user['username']);
329  foreach ( array('status', 'theme', 'language') as $k )
330  {
331    $res[$k] = $user[$k];
332  }
333  $res['pwg_token'] = get_pwg_token();
334  $res['charset'] = get_pwg_charset();
335
336  list($dbnow) = pwg_db_fetch_row(pwg_query('SELECT NOW();'));
337  $res['current_datetime'] = $dbnow;
338
339  return $res;
340}
341
342?>
Note: See TracBrowser for help on using the repository browser.