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 | /**** IMPLEMENTATION OF WEB SERVICE METHODS ***********************************/ |
---|
25 | |
---|
26 | /** |
---|
27 | * Event handler for method invocation security check. Should return a PwgError |
---|
28 | * if the preconditions are not satifsied for method invocation. |
---|
29 | */ |
---|
30 | function ws_isInvokeAllowed($res, $methodName, $params) |
---|
31 | { |
---|
32 | global $conf; |
---|
33 | |
---|
34 | if ( strpos($methodName,'reflection.')===0 ) |
---|
35 | { // OK for reflection |
---|
36 | return $res; |
---|
37 | } |
---|
38 | |
---|
39 | if ( !is_autorize_status(ACCESS_GUEST) and |
---|
40 | strpos($methodName,'pwg.session.')!==0 ) |
---|
41 | { |
---|
42 | return new PwgError(401, 'Access denied'); |
---|
43 | } |
---|
44 | |
---|
45 | return $res; |
---|
46 | } |
---|
47 | |
---|
48 | /** |
---|
49 | * returns a "standard" (for our web service) array of sql where clauses that |
---|
50 | * filters the images (images table only) |
---|
51 | */ |
---|
52 | function ws_std_image_sql_filter( $params, $tbl_name='' ) |
---|
53 | { |
---|
54 | $clauses = array(); |
---|
55 | if ( is_numeric($params['f_min_rate']) ) |
---|
56 | { |
---|
57 | $clauses[] = $tbl_name.'rating_score>='.$params['f_min_rate']; |
---|
58 | } |
---|
59 | if ( is_numeric($params['f_max_rate']) ) |
---|
60 | { |
---|
61 | $clauses[] = $tbl_name.'rating_score<='.$params['f_max_rate']; |
---|
62 | } |
---|
63 | if ( is_numeric($params['f_min_hit']) ) |
---|
64 | { |
---|
65 | $clauses[] = $tbl_name.'hit>='.$params['f_min_hit']; |
---|
66 | } |
---|
67 | if ( is_numeric($params['f_max_hit']) ) |
---|
68 | { |
---|
69 | $clauses[] = $tbl_name.'hit<='.$params['f_max_hit']; |
---|
70 | } |
---|
71 | if ( isset($params['f_min_date_available']) ) |
---|
72 | { |
---|
73 | $clauses[] = $tbl_name."date_available>='".$params['f_min_date_available']."'"; |
---|
74 | } |
---|
75 | if ( isset($params['f_max_date_available']) ) |
---|
76 | { |
---|
77 | $clauses[] = $tbl_name."date_available<'".$params['f_max_date_available']."'"; |
---|
78 | } |
---|
79 | if ( isset($params['f_min_date_created']) ) |
---|
80 | { |
---|
81 | $clauses[] = $tbl_name."date_creation>='".$params['f_min_date_created']."'"; |
---|
82 | } |
---|
83 | if ( isset($params['f_max_date_created']) ) |
---|
84 | { |
---|
85 | $clauses[] = $tbl_name."date_creation<'".$params['f_max_date_created']."'"; |
---|
86 | } |
---|
87 | if ( is_numeric($params['f_min_ratio']) ) |
---|
88 | { |
---|
89 | $clauses[] = $tbl_name.'width/'.$tbl_name.'height>='.$params['f_min_ratio']; |
---|
90 | } |
---|
91 | if ( is_numeric($params['f_max_ratio']) ) |
---|
92 | { |
---|
93 | $clauses[] = $tbl_name.'width/'.$tbl_name.'height<='.$params['f_max_ratio']; |
---|
94 | } |
---|
95 | if (is_numeric($params['f_max_level']) ) |
---|
96 | { |
---|
97 | $clauses[] = $tbl_name.'level <= '.$params['f_max_level']; |
---|
98 | } |
---|
99 | return $clauses; |
---|
100 | } |
---|
101 | |
---|
102 | /** |
---|
103 | * returns a "standard" (for our web service) ORDER BY sql clause for images |
---|
104 | */ |
---|
105 | function ws_std_image_sql_order( $params, $tbl_name='' ) |
---|
106 | { |
---|
107 | $ret = ''; |
---|
108 | if ( empty($params['order']) ) |
---|
109 | { |
---|
110 | return $ret; |
---|
111 | } |
---|
112 | $matches = array(); |
---|
113 | preg_match_all('/([a-z_]+) *(?:(asc|desc)(?:ending)?)? *(?:, *|$)/i', |
---|
114 | $params['order'], $matches); |
---|
115 | for ($i=0; $i<count($matches[1]); $i++) |
---|
116 | { |
---|
117 | switch ($matches[1][$i]) |
---|
118 | { |
---|
119 | case 'date_created': |
---|
120 | $matches[1][$i] = 'date_creation'; break; |
---|
121 | case 'date_posted': |
---|
122 | $matches[1][$i] = 'date_available'; break; |
---|
123 | case 'rand': case 'random': |
---|
124 | $matches[1][$i] = DB_RANDOM_FUNCTION.'()'; break; |
---|
125 | } |
---|
126 | $sortable_fields = array('id', 'file', 'name', 'hit', 'rating_score', |
---|
127 | 'date_creation', 'date_available', DB_RANDOM_FUNCTION.'()' ); |
---|
128 | if ( in_array($matches[1][$i], $sortable_fields) ) |
---|
129 | { |
---|
130 | if (!empty($ret)) |
---|
131 | $ret .= ', '; |
---|
132 | if ($matches[1][$i] != DB_RANDOM_FUNCTION.'()' ) |
---|
133 | { |
---|
134 | $ret .= $tbl_name; |
---|
135 | } |
---|
136 | $ret .= $matches[1][$i]; |
---|
137 | $ret .= ' '.$matches[2][$i]; |
---|
138 | } |
---|
139 | } |
---|
140 | return $ret; |
---|
141 | } |
---|
142 | |
---|
143 | /** |
---|
144 | * returns an array map of urls (thumb/element) for image_row - to be returned |
---|
145 | * in a standard way by different web service methods |
---|
146 | */ |
---|
147 | function ws_std_get_urls($image_row) |
---|
148 | { |
---|
149 | $ret = array(); |
---|
150 | |
---|
151 | $ret['page_url'] = make_picture_url( array( |
---|
152 | 'image_id' => $image_row['id'], |
---|
153 | 'image_file' => $image_row['file'], |
---|
154 | ) |
---|
155 | ); |
---|
156 | |
---|
157 | $src_image = new SrcImage($image_row); |
---|
158 | |
---|
159 | if ( $src_image->is_original() ) |
---|
160 | {// we have a photo |
---|
161 | global $user; |
---|
162 | if ($user['enabled_high']) |
---|
163 | { |
---|
164 | $ret['element_url'] = $src_image->get_url(); |
---|
165 | } |
---|
166 | } |
---|
167 | else |
---|
168 | { |
---|
169 | $ret['element_url'] = get_element_url($image_row); |
---|
170 | } |
---|
171 | |
---|
172 | $derivatives = DerivativeImage::get_all($src_image); |
---|
173 | $derivatives_arr = array(); |
---|
174 | foreach($derivatives as $type=>$derivative) |
---|
175 | { |
---|
176 | $size = $derivative->get_size(); |
---|
177 | $size != null or $size=array(null,null); |
---|
178 | $derivatives_arr[$type] = array('url' => $derivative->get_url(), 'width'=>$size[0], 'height'=>$size[1] ); |
---|
179 | } |
---|
180 | $ret['derivatives'] = $derivatives_arr;; |
---|
181 | return $ret; |
---|
182 | } |
---|
183 | |
---|
184 | /** |
---|
185 | * returns an array of image attributes that are to be encoded as xml attributes |
---|
186 | * instead of xml elements |
---|
187 | */ |
---|
188 | function ws_std_get_image_xml_attributes() |
---|
189 | { |
---|
190 | return array( |
---|
191 | 'id','element_url', 'page_url', 'file','width','height','hit','date_available','date_creation' |
---|
192 | ); |
---|
193 | } |
---|
194 | |
---|
195 | function ws_std_get_category_xml_attributes() |
---|
196 | { |
---|
197 | return array( |
---|
198 | 'id', 'url', 'nb_images', 'total_nb_images', 'nb_categories', 'date_last', 'max_date_last', |
---|
199 | ); |
---|
200 | } |
---|
201 | |
---|
202 | function ws_std_get_tag_xml_attributes() |
---|
203 | { |
---|
204 | return array( |
---|
205 | 'id', 'name', 'url_name', 'counter', 'url', 'page_url', |
---|
206 | ); |
---|
207 | } |
---|
208 | |
---|
209 | function ws_getMissingDerivatives($params, $service) |
---|
210 | { |
---|
211 | if (!is_admin()) |
---|
212 | { |
---|
213 | return new PwgError(403, 'Forbidden'); |
---|
214 | } |
---|
215 | |
---|
216 | if ( empty($params['types']) ) |
---|
217 | { |
---|
218 | $types = array_keys(ImageStdParams::get_defined_type_map()); |
---|
219 | } |
---|
220 | else |
---|
221 | { |
---|
222 | $types = array_intersect(array_keys(ImageStdParams::get_defined_type_map()), $params['types']); |
---|
223 | if (count($types)==0) |
---|
224 | { |
---|
225 | return new PwgError(WS_ERR_INVALID_PARAM, "Invalid types"); |
---|
226 | } |
---|
227 | } |
---|
228 | |
---|
229 | if ( ($max_urls = intval($params['max_urls'])) <= 0) |
---|
230 | { |
---|
231 | return new PwgError(WS_ERR_INVALID_PARAM, "Invalid max_urls"); |
---|
232 | } |
---|
233 | |
---|
234 | list($max_id, $image_count) = pwg_db_fetch_row( pwg_query('SELECT MAX(id)+1, COUNT(*) FROM '.IMAGES_TABLE) ); |
---|
235 | |
---|
236 | if (0 == $image_count) |
---|
237 | { |
---|
238 | return array(); |
---|
239 | } |
---|
240 | |
---|
241 | $start_id = intval($params['prev_page']); |
---|
242 | if ($start_id<=0) |
---|
243 | { |
---|
244 | $start_id = $max_id; |
---|
245 | } |
---|
246 | |
---|
247 | $uid = '&b='.time(); |
---|
248 | global $conf; |
---|
249 | $conf['question_mark_in_urls'] = $conf['php_extension_in_urls'] = true; |
---|
250 | $conf['derivative_url_style']=2; //script |
---|
251 | |
---|
252 | $qlimit = min(5000, ceil(max($image_count/500, $max_urls/count($types)))); |
---|
253 | $where_clauses = ws_std_image_sql_filter( $params, '' ); |
---|
254 | $where_clauses[] = 'id<start_id'; |
---|
255 | if ( !empty($params['ids']) ) |
---|
256 | { |
---|
257 | $where_clauses[] = 'id IN ('.implode(',',$params['ids']).')'; |
---|
258 | } |
---|
259 | |
---|
260 | $query_model = 'SELECT id, path, representative_ext, width,height,rotation |
---|
261 | FROM '.IMAGES_TABLE.' |
---|
262 | WHERE '.implode(' AND ', $where_clauses).' |
---|
263 | ORDER BY id DESC |
---|
264 | LIMIT '.$qlimit; |
---|
265 | |
---|
266 | $urls=array(); |
---|
267 | do |
---|
268 | { |
---|
269 | $result = pwg_query( str_replace('start_id', $start_id, $query_model)); |
---|
270 | $is_last = pwg_db_num_rows($result) < $qlimit; |
---|
271 | while ($row=pwg_db_fetch_assoc($result)) |
---|
272 | { |
---|
273 | $start_id = $row['id']; |
---|
274 | $src_image = new SrcImage($row); |
---|
275 | if ($src_image->is_mimetype()) |
---|
276 | continue; |
---|
277 | foreach($types as $type) |
---|
278 | { |
---|
279 | $derivative = new DerivativeImage($type, $src_image); |
---|
280 | if ($type != $derivative->get_type()) |
---|
281 | continue; |
---|
282 | if (@filemtime($derivative->get_path())===false) |
---|
283 | { |
---|
284 | $urls[] = $derivative->get_url().$uid; |
---|
285 | } |
---|
286 | } |
---|
287 | if (count($urls)>=$max_urls && !$is_last) |
---|
288 | break; |
---|
289 | } |
---|
290 | if ($is_last) |
---|
291 | { |
---|
292 | $start_id = 0; |
---|
293 | } |
---|
294 | }while (count($urls)<$max_urls && $start_id); |
---|
295 | |
---|
296 | $ret = array(); |
---|
297 | if ($start_id) |
---|
298 | { |
---|
299 | $ret['next_page']=$start_id; |
---|
300 | } |
---|
301 | $ret['urls']=$urls; |
---|
302 | return $ret; |
---|
303 | } |
---|
304 | |
---|
305 | /** |
---|
306 | * returns PWG version (web service method) |
---|
307 | */ |
---|
308 | function ws_getVersion($params, $service) |
---|
309 | { |
---|
310 | global $conf; |
---|
311 | if ($conf['show_version'] or is_admin() ) |
---|
312 | return PHPWG_VERSION; |
---|
313 | else |
---|
314 | return new PwgError(403, 'Forbidden'); |
---|
315 | } |
---|
316 | |
---|
317 | /** |
---|
318 | * returns general informations (web service method) |
---|
319 | */ |
---|
320 | function ws_getInfos($params, $service) |
---|
321 | { |
---|
322 | if (!is_admin()) |
---|
323 | { |
---|
324 | return new PwgError(403, 'Forbidden'); |
---|
325 | } |
---|
326 | |
---|
327 | $infos['version'] = PHPWG_VERSION; |
---|
328 | |
---|
329 | $query = 'SELECT COUNT(*) FROM '.IMAGES_TABLE.';'; |
---|
330 | list($infos['nb_elements']) = pwg_db_fetch_row(pwg_query($query)); |
---|
331 | |
---|
332 | $query = 'SELECT COUNT(*) FROM '.CATEGORIES_TABLE.';'; |
---|
333 | list($infos['nb_categories']) = pwg_db_fetch_row(pwg_query($query)); |
---|
334 | |
---|
335 | $query = 'SELECT COUNT(*) FROM '.CATEGORIES_TABLE.' WHERE dir IS NULL;'; |
---|
336 | list($infos['nb_virtual']) = pwg_db_fetch_row(pwg_query($query)); |
---|
337 | |
---|
338 | $query = 'SELECT COUNT(*) FROM '.CATEGORIES_TABLE.' WHERE dir IS NOT NULL;'; |
---|
339 | list($infos['nb_physical']) = pwg_db_fetch_row(pwg_query($query)); |
---|
340 | |
---|
341 | $query = 'SELECT COUNT(*) FROM '.IMAGE_CATEGORY_TABLE.';'; |
---|
342 | list($infos['nb_image_category']) = pwg_db_fetch_row(pwg_query($query)); |
---|
343 | |
---|
344 | $query = 'SELECT COUNT(*) FROM '.TAGS_TABLE.';'; |
---|
345 | list($infos['nb_tags']) = pwg_db_fetch_row(pwg_query($query)); |
---|
346 | |
---|
347 | $query = 'SELECT COUNT(*) FROM '.IMAGE_TAG_TABLE.';'; |
---|
348 | list($infos['nb_image_tag']) = pwg_db_fetch_row(pwg_query($query)); |
---|
349 | |
---|
350 | $query = 'SELECT COUNT(*) FROM '.USERS_TABLE.';'; |
---|
351 | list($infos['nb_users']) = pwg_db_fetch_row(pwg_query($query)); |
---|
352 | |
---|
353 | $query = 'SELECT COUNT(*) FROM '.GROUPS_TABLE.';'; |
---|
354 | list($infos['nb_groups']) = pwg_db_fetch_row(pwg_query($query)); |
---|
355 | |
---|
356 | $query = 'SELECT COUNT(*) FROM '.COMMENTS_TABLE.';'; |
---|
357 | list($infos['nb_comments']) = pwg_db_fetch_row(pwg_query($query)); |
---|
358 | |
---|
359 | // first element |
---|
360 | if ($infos['nb_elements'] > 0) |
---|
361 | { |
---|
362 | $query = 'SELECT MIN(date_available) FROM '.IMAGES_TABLE.';'; |
---|
363 | list($infos['first_date']) = pwg_db_fetch_row(pwg_query($query)); |
---|
364 | } |
---|
365 | |
---|
366 | // unvalidated comments |
---|
367 | if ($infos['nb_comments'] > 0) |
---|
368 | { |
---|
369 | $query = 'SELECT COUNT(*) FROM '.COMMENTS_TABLE.' WHERE validated=\'false\';'; |
---|
370 | list($infos['nb_unvalidated_comments']) = pwg_db_fetch_row(pwg_query($query)); |
---|
371 | } |
---|
372 | |
---|
373 | foreach ($infos as $name => $value) |
---|
374 | { |
---|
375 | $output[] = array( |
---|
376 | 'name' => $name, |
---|
377 | 'value' => $value, |
---|
378 | ); |
---|
379 | } |
---|
380 | |
---|
381 | return array('infos' => new PwgNamedArray($output, 'item')); |
---|
382 | } |
---|
383 | |
---|
384 | function ws_caddie_add($params, $service) |
---|
385 | { |
---|
386 | if (!is_admin()) |
---|
387 | { |
---|
388 | return new PwgError(401, 'Access denied'); |
---|
389 | } |
---|
390 | $params['image_id'] = array_map( 'intval',$params['image_id'] ); |
---|
391 | if ( empty($params['image_id']) ) |
---|
392 | { |
---|
393 | return new PwgError(WS_ERR_INVALID_PARAM, "Invalid image_id"); |
---|
394 | } |
---|
395 | global $user; |
---|
396 | $query = ' |
---|
397 | SELECT id |
---|
398 | FROM '.IMAGES_TABLE.' LEFT JOIN '.CADDIE_TABLE.' ON id=element_id AND user_id='.$user['id'].' |
---|
399 | WHERE id IN ('.implode(',',$params['image_id']).') |
---|
400 | AND element_id IS NULL'; |
---|
401 | $datas = array(); |
---|
402 | foreach ( array_from_query($query, 'id') as $id ) |
---|
403 | { |
---|
404 | $datas[] = array('element_id'=>$id, 'user_id'=>$user['id']); |
---|
405 | } |
---|
406 | if (count($datas)) |
---|
407 | { |
---|
408 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
---|
409 | mass_inserts(CADDIE_TABLE, array('element_id','user_id'), $datas); |
---|
410 | } |
---|
411 | return count($datas); |
---|
412 | } |
---|
413 | |
---|
414 | /** |
---|
415 | * returns images per category (web service method) |
---|
416 | */ |
---|
417 | function ws_categories_getImages($params, $service) |
---|
418 | { |
---|
419 | global $user, $conf; |
---|
420 | |
---|
421 | $images = array(); |
---|
422 | |
---|
423 | //------------------------------------------------- get the related categories |
---|
424 | $where_clauses = array(); |
---|
425 | foreach($params['cat_id'] as $cat_id) |
---|
426 | { |
---|
427 | $cat_id = (int)$cat_id; |
---|
428 | if ($cat_id<=0) |
---|
429 | continue; |
---|
430 | if ($params['recursive']) |
---|
431 | { |
---|
432 | $where_clauses[] = 'uppercats '.DB_REGEX_OPERATOR.' \'(^|,)'.$cat_id.'(,|$)\''; |
---|
433 | } |
---|
434 | else |
---|
435 | { |
---|
436 | $where_clauses[] = 'id='.$cat_id; |
---|
437 | } |
---|
438 | } |
---|
439 | if (!empty($where_clauses)) |
---|
440 | { |
---|
441 | $where_clauses = array( '('. |
---|
442 | implode(' |
---|
443 | OR ', $where_clauses) . ')' |
---|
444 | ); |
---|
445 | } |
---|
446 | $where_clauses[] = get_sql_condition_FandF( |
---|
447 | array('forbidden_categories' => 'id'), |
---|
448 | NULL, true |
---|
449 | ); |
---|
450 | |
---|
451 | $query = ' |
---|
452 | SELECT id, name, permalink, image_order |
---|
453 | FROM '.CATEGORIES_TABLE.' |
---|
454 | WHERE '. implode(' |
---|
455 | AND ', $where_clauses); |
---|
456 | $result = pwg_query($query); |
---|
457 | $cats = array(); |
---|
458 | while ($row = pwg_db_fetch_assoc($result)) |
---|
459 | { |
---|
460 | $row['id'] = (int)$row['id']; |
---|
461 | $cats[ $row['id'] ] = $row; |
---|
462 | } |
---|
463 | |
---|
464 | //-------------------------------------------------------- get the images |
---|
465 | if ( !empty($cats) ) |
---|
466 | { |
---|
467 | $where_clauses = ws_std_image_sql_filter( $params, 'i.' ); |
---|
468 | $where_clauses[] = 'category_id IN (' |
---|
469 | .implode(',', array_keys($cats) ) |
---|
470 | .')'; |
---|
471 | $where_clauses[] = get_sql_condition_FandF( array( |
---|
472 | 'visible_images' => 'i.id' |
---|
473 | ), null, true |
---|
474 | ); |
---|
475 | |
---|
476 | $order_by = ws_std_image_sql_order($params, 'i.'); |
---|
477 | if ( empty($order_by) |
---|
478 | and count($params['cat_id'])==1 |
---|
479 | and isset($cats[ $params['cat_id'][0] ]['image_order']) |
---|
480 | ) |
---|
481 | { |
---|
482 | $order_by = $cats[ $params['cat_id'][0] ]['image_order']; |
---|
483 | } |
---|
484 | $order_by = empty($order_by) ? $conf['order_by'] : 'ORDER BY '.$order_by; |
---|
485 | |
---|
486 | $params['per_page'] = (int)$params['per_page']; |
---|
487 | $params['page'] = (int)$params['page']; |
---|
488 | |
---|
489 | $query = ' |
---|
490 | SELECT i.*, GROUP_CONCAT(category_id) AS cat_ids |
---|
491 | FROM '.IMAGES_TABLE.' i |
---|
492 | INNER JOIN '.IMAGE_CATEGORY_TABLE.' ON i.id=image_id |
---|
493 | WHERE '. implode(' |
---|
494 | AND ', $where_clauses).' |
---|
495 | GROUP BY i.id |
---|
496 | '.$order_by.' |
---|
497 | LIMIT '.$params['per_page'].' OFFSET '.($params['per_page']*$params['page']); |
---|
498 | |
---|
499 | $result = pwg_query($query); |
---|
500 | while ($row = pwg_db_fetch_assoc($result)) |
---|
501 | { |
---|
502 | $image = array(); |
---|
503 | foreach ( array('id', 'width', 'height', 'hit') as $k ) |
---|
504 | { |
---|
505 | if (isset($row[$k])) |
---|
506 | { |
---|
507 | $image[$k] = (int)$row[$k]; |
---|
508 | } |
---|
509 | } |
---|
510 | foreach ( array('file', 'name', 'comment', 'date_creation', 'date_available') as $k ) |
---|
511 | { |
---|
512 | $image[$k] = $row[$k]; |
---|
513 | } |
---|
514 | $image = array_merge( $image, ws_std_get_urls($row) ); |
---|
515 | |
---|
516 | $image_cats = array(); |
---|
517 | foreach ( explode(',', $row['cat_ids']) as $cat_id ) |
---|
518 | { |
---|
519 | $url = make_index_url( |
---|
520 | array( |
---|
521 | 'category' => $cats[$cat_id], |
---|
522 | ) |
---|
523 | ); |
---|
524 | $page_url = make_picture_url( |
---|
525 | array( |
---|
526 | 'category' => $cats[$cat_id], |
---|
527 | 'image_id' => $row['id'], |
---|
528 | 'image_file' => $row['file'], |
---|
529 | ) |
---|
530 | ); |
---|
531 | $image_cats[] = array ( |
---|
532 | 'id' => (int)$cat_id, |
---|
533 | 'url' => $url, |
---|
534 | 'page_url' => $page_url, |
---|
535 | ); |
---|
536 | } |
---|
537 | |
---|
538 | $image['categories'] = new PwgNamedArray( |
---|
539 | $image_cats,'category', array('id','url','page_url') |
---|
540 | ); |
---|
541 | $images[] = $image; |
---|
542 | } |
---|
543 | } |
---|
544 | |
---|
545 | return array ( |
---|
546 | 'paging' => new PwgNamedStruct( |
---|
547 | array( |
---|
548 | 'page' => $params['page'], |
---|
549 | 'per_page' => $params['per_page'], |
---|
550 | 'count' => count($images) |
---|
551 | ) ), |
---|
552 | 'images' => new PwgNamedArray($images, 'image', ws_std_get_image_xml_attributes() ) |
---|
553 | ); |
---|
554 | } |
---|
555 | |
---|
556 | |
---|
557 | /** |
---|
558 | * create a tree from a flat list of categories, no recursivity for high speed |
---|
559 | */ |
---|
560 | function categories_flatlist_to_tree($categories) |
---|
561 | { |
---|
562 | $tree = array(); |
---|
563 | $key_of_cat = array(); |
---|
564 | |
---|
565 | foreach ($categories as $key => &$node) |
---|
566 | { |
---|
567 | $key_of_cat[$node['id']] = $key; |
---|
568 | |
---|
569 | if (!isset($node['id_uppercat'])) |
---|
570 | { |
---|
571 | $tree[] = &$node; |
---|
572 | } |
---|
573 | else |
---|
574 | { |
---|
575 | if (!isset($categories[ $key_of_cat[ $node['id_uppercat'] ] ]['sub_categories'])) |
---|
576 | { |
---|
577 | $categories[ $key_of_cat[ $node['id_uppercat'] ] ]['sub_categories'] = new PwgNamedArray(array(), 'category', ws_std_get_category_xml_attributes()); |
---|
578 | } |
---|
579 | |
---|
580 | $categories[ $key_of_cat[ $node['id_uppercat'] ] ]['sub_categories']->_content[] = &$node; |
---|
581 | } |
---|
582 | } |
---|
583 | |
---|
584 | return $tree; |
---|
585 | } |
---|
586 | |
---|
587 | /** |
---|
588 | * returns a list of categories (web service method) |
---|
589 | */ |
---|
590 | function ws_categories_getList($params, $service) |
---|
591 | { |
---|
592 | global $user,$conf; |
---|
593 | |
---|
594 | $where = array('1=1'); |
---|
595 | $join_type = 'INNER'; |
---|
596 | $join_user = $user['id']; |
---|
597 | |
---|
598 | if (!$params['recursive']) |
---|
599 | { |
---|
600 | if ($params['cat_id']>0) |
---|
601 | $where[] = '(id_uppercat='.(int)($params['cat_id']).' |
---|
602 | OR id='.(int)($params['cat_id']).')'; |
---|
603 | else |
---|
604 | $where[] = 'id_uppercat IS NULL'; |
---|
605 | } |
---|
606 | elseif ($params['cat_id']>0) |
---|
607 | { |
---|
608 | $where[] = 'uppercats '.DB_REGEX_OPERATOR.' \'(^|,)'. |
---|
609 | (int)($params['cat_id']) |
---|
610 | .'(,|$)\''; |
---|
611 | } |
---|
612 | |
---|
613 | if ($params['public']) |
---|
614 | { |
---|
615 | $where[] = 'status = "public"'; |
---|
616 | $where[] = 'visible = "true"'; |
---|
617 | |
---|
618 | $join_user = $conf['guest_id']; |
---|
619 | } |
---|
620 | elseif (is_admin()) |
---|
621 | { |
---|
622 | // in this very specific case, we don't want to hide empty |
---|
623 | // categories. Function calculate_permissions will only return |
---|
624 | // categories that are either locked or private and not permitted |
---|
625 | // |
---|
626 | // calculate_permissions does not consider empty categories as forbidden |
---|
627 | $forbidden_categories = calculate_permissions($user['id'], $user['status']); |
---|
628 | $where[]= 'id NOT IN ('.$forbidden_categories.')'; |
---|
629 | $join_type = 'LEFT'; |
---|
630 | } |
---|
631 | |
---|
632 | $query = ' |
---|
633 | SELECT id, name, permalink, uppercats, global_rank, id_uppercat, |
---|
634 | comment, |
---|
635 | nb_images, count_images AS total_nb_images, |
---|
636 | representative_picture_id, user_representative_picture_id, count_images, count_categories, |
---|
637 | date_last, max_date_last, count_categories AS nb_categories |
---|
638 | FROM '.CATEGORIES_TABLE.' |
---|
639 | '.$join_type.' JOIN '.USER_CACHE_CATEGORIES_TABLE.' ON id=cat_id AND user_id='.$join_user.' |
---|
640 | WHERE '. implode(' |
---|
641 | AND ', $where); |
---|
642 | |
---|
643 | $result = pwg_query($query); |
---|
644 | |
---|
645 | // management of the album thumbnail -- starts here |
---|
646 | $image_ids = array(); |
---|
647 | $categories = array(); |
---|
648 | $user_representative_updates_for = array(); |
---|
649 | // management of the album thumbnail -- stops here |
---|
650 | |
---|
651 | $cats = array(); |
---|
652 | while ($row = pwg_db_fetch_assoc($result)) |
---|
653 | { |
---|
654 | $row['url'] = make_index_url( |
---|
655 | array( |
---|
656 | 'category' => $row |
---|
657 | ) |
---|
658 | ); |
---|
659 | foreach( array('id','nb_images','total_nb_images','nb_categories') as $key) |
---|
660 | { |
---|
661 | $row[$key] = (int)$row[$key]; |
---|
662 | } |
---|
663 | |
---|
664 | if ($params['fullname']) |
---|
665 | { |
---|
666 | $row['name'] = strip_tags(get_cat_display_name_cache($row['uppercats'], null, false)); |
---|
667 | } |
---|
668 | else |
---|
669 | { |
---|
670 | $row['name'] = strip_tags( |
---|
671 | trigger_event( |
---|
672 | 'render_category_name', |
---|
673 | $row['name'], |
---|
674 | 'ws_categories_getList' |
---|
675 | ) |
---|
676 | ); |
---|
677 | } |
---|
678 | |
---|
679 | $row['comment'] = strip_tags( |
---|
680 | trigger_event( |
---|
681 | 'render_category_description', |
---|
682 | $row['comment'], |
---|
683 | 'ws_categories_getList' |
---|
684 | ) |
---|
685 | ); |
---|
686 | |
---|
687 | // management of the album thumbnail -- starts here |
---|
688 | // |
---|
689 | // on branch 2.3, the algorithm is duplicated from |
---|
690 | // include/category_cats, but we should use a common code for Piwigo 2.4 |
---|
691 | // |
---|
692 | // warning : if the API method is called with $params['public'], the |
---|
693 | // album thumbnail may be not accurate. The thumbnail can be viewed by |
---|
694 | // the connected user, but maybe not by the guest. Changing the |
---|
695 | // filtering method would be too complicated for now. We will simply |
---|
696 | // avoid to persist the user_representative_picture_id in the database |
---|
697 | // if $params['public'] |
---|
698 | if (!empty($row['user_representative_picture_id'])) |
---|
699 | { |
---|
700 | $image_id = $row['user_representative_picture_id']; |
---|
701 | } |
---|
702 | elseif (!empty($row['representative_picture_id'])) |
---|
703 | { // if a representative picture is set, it has priority |
---|
704 | $image_id = $row['representative_picture_id']; |
---|
705 | } |
---|
706 | elseif ($conf['allow_random_representative']) |
---|
707 | { |
---|
708 | // searching a random representant among elements in sub-categories |
---|
709 | $image_id = get_random_image_in_category($row); |
---|
710 | } |
---|
711 | else |
---|
712 | { // searching a random representant among representant of sub-categories |
---|
713 | if ($row['count_categories']>0 and $row['count_images']>0) |
---|
714 | { |
---|
715 | $query = ' |
---|
716 | SELECT representative_picture_id |
---|
717 | FROM '.CATEGORIES_TABLE.' INNER JOIN '.USER_CACHE_CATEGORIES_TABLE.' |
---|
718 | ON id = cat_id and user_id = '.$user['id'].' |
---|
719 | WHERE uppercats LIKE \''.$row['uppercats'].',%\' |
---|
720 | AND representative_picture_id IS NOT NULL' |
---|
721 | .get_sql_condition_FandF |
---|
722 | ( |
---|
723 | array |
---|
724 | ( |
---|
725 | 'visible_categories' => 'id', |
---|
726 | ), |
---|
727 | "\n AND" |
---|
728 | ).' |
---|
729 | ORDER BY '.DB_RANDOM_FUNCTION.'() |
---|
730 | LIMIT 1 |
---|
731 | ;'; |
---|
732 | $subresult = pwg_query($query); |
---|
733 | if (pwg_db_num_rows($subresult) > 0) |
---|
734 | { |
---|
735 | list($image_id) = pwg_db_fetch_row($subresult); |
---|
736 | } |
---|
737 | } |
---|
738 | } |
---|
739 | |
---|
740 | if (isset($image_id)) |
---|
741 | { |
---|
742 | if ($conf['representative_cache_on_subcats'] and $row['user_representative_picture_id'] != $image_id) |
---|
743 | { |
---|
744 | $user_representative_updates_for[ $row['id'] ] = $image_id; |
---|
745 | } |
---|
746 | |
---|
747 | $row['representative_picture_id'] = $image_id; |
---|
748 | $image_ids[] = $image_id; |
---|
749 | $categories[] = $row; |
---|
750 | } |
---|
751 | unset($image_id); |
---|
752 | // management of the album thumbnail -- stops here |
---|
753 | |
---|
754 | |
---|
755 | $cats[] = $row; |
---|
756 | } |
---|
757 | usort($cats, 'global_rank_compare'); |
---|
758 | |
---|
759 | // management of the album thumbnail -- starts here |
---|
760 | if (count($categories) > 0) |
---|
761 | { |
---|
762 | $thumbnail_src_of = array(); |
---|
763 | $new_image_ids = array(); |
---|
764 | |
---|
765 | $query = ' |
---|
766 | SELECT id, path, representative_ext, level |
---|
767 | FROM '.IMAGES_TABLE.' |
---|
768 | WHERE id IN ('.implode(',', $image_ids).') |
---|
769 | ;'; |
---|
770 | $result = pwg_query($query); |
---|
771 | while ($row = pwg_db_fetch_assoc($result)) |
---|
772 | { |
---|
773 | if ($row['level'] <= $user['level']) |
---|
774 | { |
---|
775 | $thumbnail_src_of[$row['id']] = DerivativeImage::thumb_url($row); |
---|
776 | } |
---|
777 | else |
---|
778 | { |
---|
779 | // problem: we must not display the thumbnail of a photo which has a |
---|
780 | // higher privacy level than user privacy level |
---|
781 | // |
---|
782 | // * what is the represented category? |
---|
783 | // * find a random photo matching user permissions |
---|
784 | // * register it at user_representative_picture_id |
---|
785 | // * set it as the representative_picture_id for the category |
---|
786 | |
---|
787 | foreach ($categories as &$category) |
---|
788 | { |
---|
789 | if ($row['id'] == $category['representative_picture_id']) |
---|
790 | { |
---|
791 | // searching a random representant among elements in sub-categories |
---|
792 | $image_id = get_random_image_in_category($category); |
---|
793 | |
---|
794 | if (isset($image_id) and !in_array($image_id, $image_ids)) |
---|
795 | { |
---|
796 | $new_image_ids[] = $image_id; |
---|
797 | } |
---|
798 | |
---|
799 | if ($conf['representative_cache_on_level']) |
---|
800 | { |
---|
801 | $user_representative_updates_for[ $category['id'] ] = $image_id; |
---|
802 | } |
---|
803 | |
---|
804 | $category['representative_picture_id'] = $image_id; |
---|
805 | } |
---|
806 | } |
---|
807 | unset($category); |
---|
808 | } |
---|
809 | } |
---|
810 | |
---|
811 | if (count($new_image_ids) > 0) |
---|
812 | { |
---|
813 | $query = ' |
---|
814 | SELECT id, path, representative_ext |
---|
815 | FROM '.IMAGES_TABLE.' |
---|
816 | WHERE id IN ('.implode(',', $new_image_ids).') |
---|
817 | ;'; |
---|
818 | $result = pwg_query($query); |
---|
819 | while ($row = pwg_db_fetch_assoc($result)) |
---|
820 | { |
---|
821 | $thumbnail_src_of[$row['id']] = DerivativeImage::thumb_url($row); |
---|
822 | } |
---|
823 | } |
---|
824 | } |
---|
825 | |
---|
826 | // compared to code in include/category_cats, we only persist the new |
---|
827 | // user_representative if we have used $user['id'] and not the guest id, |
---|
828 | // or else the real guest may see thumbnail that he should not |
---|
829 | if (!$params['public'] and count($user_representative_updates_for)) |
---|
830 | { |
---|
831 | $updates = array(); |
---|
832 | |
---|
833 | foreach ($user_representative_updates_for as $cat_id => $image_id) |
---|
834 | { |
---|
835 | $updates[] = array( |
---|
836 | 'user_id' => $user['id'], |
---|
837 | 'cat_id' => $cat_id, |
---|
838 | 'user_representative_picture_id' => $image_id, |
---|
839 | ); |
---|
840 | } |
---|
841 | |
---|
842 | mass_updates( |
---|
843 | USER_CACHE_CATEGORIES_TABLE, |
---|
844 | array( |
---|
845 | 'primary' => array('user_id', 'cat_id'), |
---|
846 | 'update' => array('user_representative_picture_id') |
---|
847 | ), |
---|
848 | $updates |
---|
849 | ); |
---|
850 | } |
---|
851 | |
---|
852 | foreach ($cats as &$cat) |
---|
853 | { |
---|
854 | foreach ($categories as $category) |
---|
855 | { |
---|
856 | if ($category['id'] == $cat['id'] and isset($category['representative_picture_id'])) |
---|
857 | { |
---|
858 | $cat['tn_url'] = $thumbnail_src_of[$category['representative_picture_id']]; |
---|
859 | } |
---|
860 | } |
---|
861 | // we don't want them in the output |
---|
862 | unset($cat['user_representative_picture_id']); |
---|
863 | unset($cat['count_images']); |
---|
864 | unset($cat['count_categories']); |
---|
865 | } |
---|
866 | unset($cat); |
---|
867 | // management of the album thumbnail -- stops here |
---|
868 | |
---|
869 | if ($params['tree_output']) |
---|
870 | { |
---|
871 | $cats = categories_flatlist_to_tree($cats); |
---|
872 | } |
---|
873 | |
---|
874 | return array( |
---|
875 | 'categories' => new PwgNamedArray($cats, 'category', ws_std_get_category_xml_attributes()) |
---|
876 | ); |
---|
877 | } |
---|
878 | |
---|
879 | /** |
---|
880 | * returns the list of categories as you can see them in administration (web |
---|
881 | * service method). |
---|
882 | * |
---|
883 | * Only admin can run this method and permissions are not taken into |
---|
884 | * account. |
---|
885 | */ |
---|
886 | function ws_categories_getAdminList($params, $service) |
---|
887 | { |
---|
888 | if (!is_admin()) |
---|
889 | { |
---|
890 | return new PwgError(401, 'Access denied'); |
---|
891 | } |
---|
892 | |
---|
893 | $query = ' |
---|
894 | SELECT |
---|
895 | category_id, |
---|
896 | COUNT(*) AS counter |
---|
897 | FROM '.IMAGE_CATEGORY_TABLE.' |
---|
898 | GROUP BY category_id |
---|
899 | ;'; |
---|
900 | $nb_images_of = simple_hash_from_query($query, 'category_id', 'counter'); |
---|
901 | |
---|
902 | $query = ' |
---|
903 | SELECT |
---|
904 | id, |
---|
905 | name, |
---|
906 | comment, |
---|
907 | uppercats, |
---|
908 | global_rank |
---|
909 | FROM '.CATEGORIES_TABLE.' |
---|
910 | ;'; |
---|
911 | $result = pwg_query($query); |
---|
912 | $cats = array(); |
---|
913 | |
---|
914 | while ($row = pwg_db_fetch_assoc($result)) |
---|
915 | { |
---|
916 | $id = $row['id']; |
---|
917 | $row['nb_images'] = isset($nb_images_of[$id]) ? $nb_images_of[$id] : 0; |
---|
918 | $row['name'] = strip_tags( |
---|
919 | trigger_event( |
---|
920 | 'render_category_name', |
---|
921 | $row['name'], |
---|
922 | 'ws_categories_getAdminList' |
---|
923 | ) |
---|
924 | ); |
---|
925 | $row['comment'] = strip_tags( |
---|
926 | trigger_event( |
---|
927 | 'render_category_description', |
---|
928 | $row['comment'], |
---|
929 | 'ws_categories_getAdminList' |
---|
930 | ) |
---|
931 | ); |
---|
932 | $cats[] = $row; |
---|
933 | } |
---|
934 | |
---|
935 | usort($cats, 'global_rank_compare'); |
---|
936 | return array( |
---|
937 | 'categories' => new PwgNamedArray( |
---|
938 | $cats, |
---|
939 | 'category', |
---|
940 | array( |
---|
941 | 'id', |
---|
942 | 'nb_images', |
---|
943 | 'name', |
---|
944 | 'uppercats', |
---|
945 | 'global_rank', |
---|
946 | ) |
---|
947 | ) |
---|
948 | ); |
---|
949 | } |
---|
950 | |
---|
951 | /** |
---|
952 | * returns detailed information for an element (web service method) |
---|
953 | */ |
---|
954 | function ws_images_addComment($params, $service) |
---|
955 | { |
---|
956 | if (!$service->isPost()) |
---|
957 | { |
---|
958 | return new PwgError(405, "This method requires HTTP POST"); |
---|
959 | } |
---|
960 | $params['image_id'] = (int)$params['image_id']; |
---|
961 | $query = ' |
---|
962 | SELECT DISTINCT image_id |
---|
963 | FROM '.IMAGE_CATEGORY_TABLE.' INNER JOIN '.CATEGORIES_TABLE.' ON category_id=id |
---|
964 | WHERE commentable="true" |
---|
965 | AND image_id='.$params['image_id']. |
---|
966 | get_sql_condition_FandF( |
---|
967 | array( |
---|
968 | 'forbidden_categories' => 'id', |
---|
969 | 'visible_categories' => 'id', |
---|
970 | 'visible_images' => 'image_id' |
---|
971 | ), |
---|
972 | ' AND' |
---|
973 | ); |
---|
974 | if ( !pwg_db_num_rows( pwg_query( $query ) ) ) |
---|
975 | { |
---|
976 | return new PwgError(WS_ERR_INVALID_PARAM, "Invalid image_id"); |
---|
977 | } |
---|
978 | |
---|
979 | $comm = array( |
---|
980 | 'author' => trim( $params['author'] ), |
---|
981 | 'content' => trim( $params['content'] ), |
---|
982 | 'image_id' => $params['image_id'], |
---|
983 | ); |
---|
984 | |
---|
985 | include_once(PHPWG_ROOT_PATH.'include/functions_comment.inc.php'); |
---|
986 | |
---|
987 | $comment_action = insert_user_comment( |
---|
988 | $comm, $params['key'], $infos |
---|
989 | ); |
---|
990 | |
---|
991 | switch ($comment_action) |
---|
992 | { |
---|
993 | case 'reject': |
---|
994 | $infos[] = l10n('Your comment has NOT been registered because it did not pass the validation rules'); |
---|
995 | return new PwgError(403, implode("; ", $infos) ); |
---|
996 | case 'validate': |
---|
997 | case 'moderate': |
---|
998 | $ret = array( |
---|
999 | 'id' => $comm['id'], |
---|
1000 | 'validation' => $comment_action=='validate', |
---|
1001 | ); |
---|
1002 | return array( 'comment' => new PwgNamedStruct($ret) ); |
---|
1003 | default: |
---|
1004 | return new PwgError(500, "Unknown comment action ".$comment_action ); |
---|
1005 | } |
---|
1006 | } |
---|
1007 | |
---|
1008 | /** |
---|
1009 | * returns detailed information for an element (web service method) |
---|
1010 | */ |
---|
1011 | function ws_images_getInfo($params, $service) |
---|
1012 | { |
---|
1013 | global $user, $conf; |
---|
1014 | $params['image_id'] = (int)$params['image_id']; |
---|
1015 | if ( $params['image_id']<=0 ) |
---|
1016 | { |
---|
1017 | return new PwgError(WS_ERR_INVALID_PARAM, "Invalid image_id"); |
---|
1018 | } |
---|
1019 | |
---|
1020 | $query=' |
---|
1021 | SELECT * FROM '.IMAGES_TABLE.' |
---|
1022 | WHERE id='.$params['image_id']. |
---|
1023 | get_sql_condition_FandF( |
---|
1024 | array('visible_images' => 'id'), |
---|
1025 | ' AND' |
---|
1026 | ).' |
---|
1027 | LIMIT 1'; |
---|
1028 | |
---|
1029 | $image_row = pwg_db_fetch_assoc(pwg_query($query)); |
---|
1030 | if ($image_row==null) |
---|
1031 | { |
---|
1032 | return new PwgError(404, "image_id not found"); |
---|
1033 | } |
---|
1034 | $image_row = array_merge( $image_row, ws_std_get_urls($image_row) ); |
---|
1035 | |
---|
1036 | //-------------------------------------------------------- related categories |
---|
1037 | $query = ' |
---|
1038 | SELECT id, name, permalink, uppercats, global_rank, commentable |
---|
1039 | FROM '.IMAGE_CATEGORY_TABLE.' |
---|
1040 | INNER JOIN '.CATEGORIES_TABLE.' ON category_id = id |
---|
1041 | WHERE image_id = '.$image_row['id']. |
---|
1042 | get_sql_condition_FandF( |
---|
1043 | array( 'forbidden_categories' => 'category_id' ), |
---|
1044 | ' AND' |
---|
1045 | ).' |
---|
1046 | ;'; |
---|
1047 | $result = pwg_query($query); |
---|
1048 | $is_commentable = false; |
---|
1049 | $related_categories = array(); |
---|
1050 | while ($row = pwg_db_fetch_assoc($result)) |
---|
1051 | { |
---|
1052 | if ($row['commentable']=='true') |
---|
1053 | { |
---|
1054 | $is_commentable = true; |
---|
1055 | } |
---|
1056 | unset($row['commentable']); |
---|
1057 | $row['url'] = make_index_url( |
---|
1058 | array( |
---|
1059 | 'category' => $row |
---|
1060 | ) |
---|
1061 | ); |
---|
1062 | |
---|
1063 | $row['page_url'] = make_picture_url( |
---|
1064 | array( |
---|
1065 | 'image_id' => $image_row['id'], |
---|
1066 | 'image_file' => $image_row['file'], |
---|
1067 | 'category' => $row |
---|
1068 | ) |
---|
1069 | ); |
---|
1070 | $row['id']=(int)$row['id']; |
---|
1071 | $related_categories[] = $row; |
---|
1072 | } |
---|
1073 | usort($related_categories, 'global_rank_compare'); |
---|
1074 | if ( empty($related_categories) ) |
---|
1075 | { |
---|
1076 | return new PwgError(401, 'Access denied'); |
---|
1077 | } |
---|
1078 | |
---|
1079 | //-------------------------------------------------------------- related tags |
---|
1080 | $related_tags = get_common_tags( array($image_row['id']), -1 ); |
---|
1081 | foreach( $related_tags as $i=>$tag) |
---|
1082 | { |
---|
1083 | $tag['url'] = make_index_url( |
---|
1084 | array( |
---|
1085 | 'tags' => array($tag) |
---|
1086 | ) |
---|
1087 | ); |
---|
1088 | $tag['page_url'] = make_picture_url( |
---|
1089 | array( |
---|
1090 | 'image_id' => $image_row['id'], |
---|
1091 | 'image_file' => $image_row['file'], |
---|
1092 | 'tags' => array($tag), |
---|
1093 | ) |
---|
1094 | ); |
---|
1095 | unset($tag['counter']); |
---|
1096 | $tag['id']=(int)$tag['id']; |
---|
1097 | $related_tags[$i]=$tag; |
---|
1098 | } |
---|
1099 | //------------------------------------------------------------- related rates |
---|
1100 | $rating = array('score'=>$image_row['rating_score'], 'count'=>0, 'average'=>null); |
---|
1101 | if (isset($rating['score'])) |
---|
1102 | { |
---|
1103 | $query = ' |
---|
1104 | SELECT COUNT(rate) AS count |
---|
1105 | , ROUND(AVG(rate),2) AS average |
---|
1106 | FROM '.RATE_TABLE.' |
---|
1107 | WHERE element_id = '.$image_row['id'].' |
---|
1108 | ;'; |
---|
1109 | $row = pwg_db_fetch_assoc(pwg_query($query)); |
---|
1110 | $rating['score'] = (float)$rating['score']; |
---|
1111 | $rating['average'] = (float)$row['average']; |
---|
1112 | $rating['count'] = (int)$row['count']; |
---|
1113 | } |
---|
1114 | |
---|
1115 | //---------------------------------------------------------- related comments |
---|
1116 | $related_comments = array(); |
---|
1117 | |
---|
1118 | $where_comments = 'image_id = '.$image_row['id']; |
---|
1119 | if ( !is_admin() ) |
---|
1120 | { |
---|
1121 | $where_comments .= ' |
---|
1122 | AND validated="true"'; |
---|
1123 | } |
---|
1124 | |
---|
1125 | $query = ' |
---|
1126 | SELECT COUNT(id) AS nb_comments |
---|
1127 | FROM '.COMMENTS_TABLE.' |
---|
1128 | WHERE '.$where_comments; |
---|
1129 | list($nb_comments) = array_from_query($query, 'nb_comments'); |
---|
1130 | $nb_comments = (int)$nb_comments; |
---|
1131 | |
---|
1132 | if ( $nb_comments>0 and $params['comments_per_page']>0 ) |
---|
1133 | { |
---|
1134 | $query = ' |
---|
1135 | SELECT id, date, author, content |
---|
1136 | FROM '.COMMENTS_TABLE.' |
---|
1137 | WHERE '.$where_comments.' |
---|
1138 | ORDER BY date |
---|
1139 | LIMIT '.(int)$params['comments_per_page']. |
---|
1140 | ' OFFSET '.(int)($params['comments_per_page']*$params['comments_page']); |
---|
1141 | |
---|
1142 | $result = pwg_query($query); |
---|
1143 | while ($row = pwg_db_fetch_assoc($result)) |
---|
1144 | { |
---|
1145 | $row['id']=(int)$row['id']; |
---|
1146 | $related_comments[] = $row; |
---|
1147 | } |
---|
1148 | } |
---|
1149 | |
---|
1150 | $comment_post_data = null; |
---|
1151 | if ($is_commentable and |
---|
1152 | (!is_a_guest() |
---|
1153 | or (is_a_guest() and $conf['comments_forall'] ) |
---|
1154 | ) |
---|
1155 | ) |
---|
1156 | { |
---|
1157 | $comment_post_data['author'] = stripslashes($user['username']); |
---|
1158 | $comment_post_data['key'] = get_ephemeral_key(2, $params['image_id']); |
---|
1159 | } |
---|
1160 | |
---|
1161 | $ret = $image_row; |
---|
1162 | foreach ( array('id','width','height','hit','filesize') as $k ) |
---|
1163 | { |
---|
1164 | if (isset($ret[$k])) |
---|
1165 | { |
---|
1166 | $ret[$k] = (int)$ret[$k]; |
---|
1167 | } |
---|
1168 | } |
---|
1169 | foreach ( array('path', 'storage_category_id') as $k ) |
---|
1170 | { |
---|
1171 | unset($ret[$k]); |
---|
1172 | } |
---|
1173 | |
---|
1174 | $ret['rates'] = array( WS_XML_ATTRIBUTES => $rating ); |
---|
1175 | $ret['categories'] = new PwgNamedArray($related_categories, 'category', array('id','url', 'page_url') ); |
---|
1176 | $ret['tags'] = new PwgNamedArray($related_tags, 'tag', ws_std_get_tag_xml_attributes() ); |
---|
1177 | if ( isset($comment_post_data) ) |
---|
1178 | { |
---|
1179 | $ret['comment_post'] = array( WS_XML_ATTRIBUTES => $comment_post_data ); |
---|
1180 | } |
---|
1181 | $ret['comments_paging'] = new PwgNamedStruct( array( |
---|
1182 | 'page' => $params['comments_page'], |
---|
1183 | 'per_page' => $params['comments_per_page'], |
---|
1184 | 'count' => count($related_comments), |
---|
1185 | 'total_count' => $nb_comments, |
---|
1186 | ) ); |
---|
1187 | |
---|
1188 | $ret['comments'] = new PwgNamedArray($related_comments, 'comment', array('id','date') ); |
---|
1189 | |
---|
1190 | if ($service->_responseFormat != 'rest') |
---|
1191 | return $ret; // for backward compatibility only |
---|
1192 | else |
---|
1193 | return array( 'image' => new PwgNamedStruct($ret, null, array('name','comment') ) ); |
---|
1194 | } |
---|
1195 | |
---|
1196 | |
---|
1197 | /** |
---|
1198 | * rates the image_id in the parameter |
---|
1199 | */ |
---|
1200 | function ws_images_Rate($params, $service) |
---|
1201 | { |
---|
1202 | $image_id = (int)$params['image_id']; |
---|
1203 | $query = ' |
---|
1204 | SELECT DISTINCT id FROM '.IMAGES_TABLE.' |
---|
1205 | INNER JOIN '.IMAGE_CATEGORY_TABLE.' ON id=image_id |
---|
1206 | WHERE id='.$image_id |
---|
1207 | .get_sql_condition_FandF( |
---|
1208 | array( |
---|
1209 | 'forbidden_categories' => 'category_id', |
---|
1210 | 'forbidden_images' => 'id', |
---|
1211 | ), |
---|
1212 | ' AND' |
---|
1213 | ).' |
---|
1214 | LIMIT 1'; |
---|
1215 | if ( pwg_db_num_rows( pwg_query($query) )==0 ) |
---|
1216 | { |
---|
1217 | return new PwgError(404, "Invalid image_id or access denied" ); |
---|
1218 | } |
---|
1219 | $rate = (int)$params['rate']; |
---|
1220 | include_once(PHPWG_ROOT_PATH.'include/functions_rate.inc.php'); |
---|
1221 | $res = rate_picture( $image_id, $rate ); |
---|
1222 | if ($res==false) |
---|
1223 | { |
---|
1224 | global $conf; |
---|
1225 | return new PwgError( 403, "Forbidden or rate not in ". implode(',',$conf['rate_items'])); |
---|
1226 | } |
---|
1227 | return $res; |
---|
1228 | } |
---|
1229 | |
---|
1230 | |
---|
1231 | /** |
---|
1232 | * returns a list of elements corresponding to a query search |
---|
1233 | */ |
---|
1234 | function ws_images_search($params, $service) |
---|
1235 | { |
---|
1236 | global $page; |
---|
1237 | $images = array(); |
---|
1238 | include_once( PHPWG_ROOT_PATH .'include/functions_search.inc.php' ); |
---|
1239 | |
---|
1240 | $where_clauses = ws_std_image_sql_filter( $params, 'i.' ); |
---|
1241 | $order_by = ws_std_image_sql_order($params, 'i.'); |
---|
1242 | |
---|
1243 | $super_order_by = false; |
---|
1244 | if ( !empty($order_by) ) |
---|
1245 | { |
---|
1246 | global $conf; |
---|
1247 | $conf['order_by'] = 'ORDER BY '.$order_by; |
---|
1248 | $super_order_by=true; // quick_search_result might be faster |
---|
1249 | } |
---|
1250 | |
---|
1251 | $search_result = get_quick_search_results($params['query'], |
---|
1252 | $super_order_by, |
---|
1253 | implode(' AND ', $where_clauses) |
---|
1254 | ); |
---|
1255 | |
---|
1256 | $params['per_page'] = (int)$params['per_page']; |
---|
1257 | $params['page'] = (int)$params['page']; |
---|
1258 | |
---|
1259 | $image_ids = array_slice( |
---|
1260 | $search_result['items'], |
---|
1261 | $params['page']*$params['per_page'], |
---|
1262 | $params['per_page'] |
---|
1263 | ); |
---|
1264 | |
---|
1265 | if ( count($image_ids) ) |
---|
1266 | { |
---|
1267 | $query = ' |
---|
1268 | SELECT * FROM '.IMAGES_TABLE.' |
---|
1269 | WHERE id IN ('.implode(',', $image_ids).')'; |
---|
1270 | |
---|
1271 | $image_ids = array_flip($image_ids); |
---|
1272 | $result = pwg_query($query); |
---|
1273 | while ($row = pwg_db_fetch_assoc($result)) |
---|
1274 | { |
---|
1275 | $image = array(); |
---|
1276 | foreach ( array('id', 'width', 'height', 'hit') as $k ) |
---|
1277 | { |
---|
1278 | if (isset($row[$k])) |
---|
1279 | { |
---|
1280 | $image[$k] = (int)$row[$k]; |
---|
1281 | } |
---|
1282 | } |
---|
1283 | foreach ( array('file', 'name', 'comment', 'date_creation', 'date_available') as $k ) |
---|
1284 | { |
---|
1285 | $image[$k] = $row[$k]; |
---|
1286 | } |
---|
1287 | $image = array_merge( $image, ws_std_get_urls($row) ); |
---|
1288 | $images[$image_ids[$image['id']]] = $image; |
---|
1289 | } |
---|
1290 | ksort($images, SORT_NUMERIC); |
---|
1291 | $images = array_values($images); |
---|
1292 | } |
---|
1293 | |
---|
1294 | return array ( |
---|
1295 | 'paging' => new PwgNamedStruct( |
---|
1296 | array( |
---|
1297 | 'page' => $params['page'], |
---|
1298 | 'per_page' => $params['per_page'], |
---|
1299 | 'count' => count($images), |
---|
1300 | 'total_count' => count($search_result['items']), |
---|
1301 | ) ), |
---|
1302 | 'images' => new PwgNamedArray($images, 'image', |
---|
1303 | ws_std_get_image_xml_attributes() ) |
---|
1304 | ); |
---|
1305 | } |
---|
1306 | |
---|
1307 | function ws_images_setPrivacyLevel($params, $service) |
---|
1308 | { |
---|
1309 | if (!is_admin()) |
---|
1310 | { |
---|
1311 | return new PwgError(401, 'Access denied'); |
---|
1312 | } |
---|
1313 | if (!$service->isPost()) |
---|
1314 | { |
---|
1315 | return new PwgError(405, "This method requires HTTP POST"); |
---|
1316 | } |
---|
1317 | $params['image_id'] = array_map( 'intval',$params['image_id'] ); |
---|
1318 | if ( empty($params['image_id']) ) |
---|
1319 | { |
---|
1320 | return new PwgError(WS_ERR_INVALID_PARAM, "Invalid image_id"); |
---|
1321 | } |
---|
1322 | global $conf; |
---|
1323 | if ( !in_array( (int)$params['level'], $conf['available_permission_levels']) ) |
---|
1324 | { |
---|
1325 | return new PwgError(WS_ERR_INVALID_PARAM, "Invalid level"); |
---|
1326 | } |
---|
1327 | |
---|
1328 | $query = ' |
---|
1329 | UPDATE '.IMAGES_TABLE.' |
---|
1330 | SET level='.(int)$params['level'].' |
---|
1331 | WHERE id IN ('.implode(',',$params['image_id']).')'; |
---|
1332 | $result = pwg_query($query); |
---|
1333 | $affected_rows = pwg_db_changes($result); |
---|
1334 | if ($affected_rows) |
---|
1335 | { |
---|
1336 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
---|
1337 | invalidate_user_cache(); |
---|
1338 | } |
---|
1339 | return $affected_rows; |
---|
1340 | } |
---|
1341 | |
---|
1342 | function ws_images_setRank($params, $service) |
---|
1343 | { |
---|
1344 | if (!is_admin()) |
---|
1345 | { |
---|
1346 | return new PwgError(401, 'Access denied'); |
---|
1347 | } |
---|
1348 | |
---|
1349 | if (!$service->isPost()) |
---|
1350 | { |
---|
1351 | return new PwgError(405, "This method requires HTTP POST"); |
---|
1352 | } |
---|
1353 | |
---|
1354 | // is the image_id valid? |
---|
1355 | $params['image_id'] = (int)$params['image_id']; |
---|
1356 | if ($params['image_id'] <= 0) |
---|
1357 | { |
---|
1358 | return new PwgError(WS_ERR_INVALID_PARAM, "Invalid image_id"); |
---|
1359 | } |
---|
1360 | |
---|
1361 | // is the category valid? |
---|
1362 | $params['category_id'] = (int)$params['category_id']; |
---|
1363 | if ($params['category_id'] <= 0) |
---|
1364 | { |
---|
1365 | return new PwgError(WS_ERR_INVALID_PARAM, "Invalid category_id"); |
---|
1366 | } |
---|
1367 | |
---|
1368 | // is the rank valid? |
---|
1369 | $params['rank'] = (int)$params['rank']; |
---|
1370 | if ($params['rank'] <= 0) |
---|
1371 | { |
---|
1372 | return new PwgError(WS_ERR_INVALID_PARAM, "Invalid rank"); |
---|
1373 | } |
---|
1374 | |
---|
1375 | // does the image really exist? |
---|
1376 | $query=' |
---|
1377 | SELECT |
---|
1378 | * |
---|
1379 | FROM '.IMAGES_TABLE.' |
---|
1380 | WHERE id = '.$params['image_id'].' |
---|
1381 | ;'; |
---|
1382 | |
---|
1383 | $image_row = pwg_db_fetch_assoc(pwg_query($query)); |
---|
1384 | if ($image_row == null) |
---|
1385 | { |
---|
1386 | return new PwgError(404, "image_id not found"); |
---|
1387 | } |
---|
1388 | |
---|
1389 | // is the image associated to this category? |
---|
1390 | $query = ' |
---|
1391 | SELECT |
---|
1392 | image_id, |
---|
1393 | category_id, |
---|
1394 | rank |
---|
1395 | FROM '.IMAGE_CATEGORY_TABLE.' |
---|
1396 | WHERE image_id = '.$params['image_id'].' |
---|
1397 | AND category_id = '.$params['category_id'].' |
---|
1398 | ;'; |
---|
1399 | $category_row = pwg_db_fetch_assoc(pwg_query($query)); |
---|
1400 | if ($category_row == null) |
---|
1401 | { |
---|
1402 | return new PwgError(404, "This image is not associated to this category"); |
---|
1403 | } |
---|
1404 | |
---|
1405 | // what is the current higher rank for this category? |
---|
1406 | $query = ' |
---|
1407 | SELECT |
---|
1408 | MAX(rank) AS max_rank |
---|
1409 | FROM '.IMAGE_CATEGORY_TABLE.' |
---|
1410 | WHERE category_id = '.$params['category_id'].' |
---|
1411 | ;'; |
---|
1412 | $result = pwg_query($query); |
---|
1413 | $row = pwg_db_fetch_assoc($result); |
---|
1414 | |
---|
1415 | if (is_numeric($row['max_rank'])) |
---|
1416 | { |
---|
1417 | if ($params['rank'] > $row['max_rank']) |
---|
1418 | { |
---|
1419 | $params['rank'] = $row['max_rank'] + 1; |
---|
1420 | } |
---|
1421 | } |
---|
1422 | else |
---|
1423 | { |
---|
1424 | $params['rank'] = 1; |
---|
1425 | } |
---|
1426 | |
---|
1427 | // update rank for all other photos in the same category |
---|
1428 | $query = ' |
---|
1429 | UPDATE '.IMAGE_CATEGORY_TABLE.' |
---|
1430 | SET rank = rank + 1 |
---|
1431 | WHERE category_id = '.$params['category_id'].' |
---|
1432 | AND rank IS NOT NULL |
---|
1433 | AND rank >= '.$params['rank'].' |
---|
1434 | ;'; |
---|
1435 | pwg_query($query); |
---|
1436 | |
---|
1437 | // set the new rank for the photo |
---|
1438 | $query = ' |
---|
1439 | UPDATE '.IMAGE_CATEGORY_TABLE.' |
---|
1440 | SET rank = '.$params['rank'].' |
---|
1441 | WHERE image_id = '.$params['image_id'].' |
---|
1442 | AND category_id = '.$params['category_id'].' |
---|
1443 | ;'; |
---|
1444 | pwg_query($query); |
---|
1445 | |
---|
1446 | // return data for client |
---|
1447 | return array( |
---|
1448 | 'image_id' => $params['image_id'], |
---|
1449 | 'category_id' => $params['category_id'], |
---|
1450 | 'rank' => $params['rank'], |
---|
1451 | ); |
---|
1452 | } |
---|
1453 | |
---|
1454 | function ws_images_add_chunk($params, $service) |
---|
1455 | { |
---|
1456 | global $conf; |
---|
1457 | |
---|
1458 | // data |
---|
1459 | // original_sum |
---|
1460 | // type {thumb, file, high} |
---|
1461 | // position |
---|
1462 | |
---|
1463 | if (!is_admin()) |
---|
1464 | { |
---|
1465 | return new PwgError(401, 'Access denied'); |
---|
1466 | } |
---|
1467 | |
---|
1468 | if (!$service->isPost()) |
---|
1469 | { |
---|
1470 | return new PwgError(405, "This method requires HTTP POST"); |
---|
1471 | } |
---|
1472 | |
---|
1473 | foreach ($params as $param_key => $param_value) { |
---|
1474 | if ('data' == $param_key) { |
---|
1475 | continue; |
---|
1476 | } |
---|
1477 | |
---|
1478 | ws_logfile( |
---|
1479 | sprintf( |
---|
1480 | '[ws_images_add_chunk] input param "%s" : "%s"', |
---|
1481 | $param_key, |
---|
1482 | is_null($param_value) ? 'NULL' : $param_value |
---|
1483 | ) |
---|
1484 | ); |
---|
1485 | } |
---|
1486 | |
---|
1487 | $upload_dir = $conf['upload_dir'].'/buffer'; |
---|
1488 | |
---|
1489 | // create the upload directory tree if not exists |
---|
1490 | if (!mkgetdir($upload_dir, MKGETDIR_DEFAULT&~MKGETDIR_DIE_ON_ERROR)) |
---|
1491 | { |
---|
1492 | return new PwgError(500, 'error during buffer directory creation'); |
---|
1493 | } |
---|
1494 | |
---|
1495 | $filename = sprintf( |
---|
1496 | '%s-%s-%05u.block', |
---|
1497 | $params['original_sum'], |
---|
1498 | $params['type'], |
---|
1499 | $params['position'] |
---|
1500 | ); |
---|
1501 | |
---|
1502 | ws_logfile('[ws_images_add_chunk] data length : '.strlen($params['data'])); |
---|
1503 | |
---|
1504 | $bytes_written = file_put_contents( |
---|
1505 | $upload_dir.'/'.$filename, |
---|
1506 | base64_decode($params['data']) |
---|
1507 | ); |
---|
1508 | |
---|
1509 | if (false === $bytes_written) { |
---|
1510 | return new PwgError( |
---|
1511 | 500, |
---|
1512 | 'an error has occured while writting chunk '.$params['position'].' for '.$params['type'] |
---|
1513 | ); |
---|
1514 | } |
---|
1515 | } |
---|
1516 | |
---|
1517 | function merge_chunks($output_filepath, $original_sum, $type) |
---|
1518 | { |
---|
1519 | global $conf; |
---|
1520 | |
---|
1521 | ws_logfile('[merge_chunks] input parameter $output_filepath : '.$output_filepath); |
---|
1522 | |
---|
1523 | if (is_file($output_filepath)) |
---|
1524 | { |
---|
1525 | unlink($output_filepath); |
---|
1526 | |
---|
1527 | if (is_file($output_filepath)) |
---|
1528 | { |
---|
1529 | return new PwgError(500, '[merge_chunks] error while trying to remove existing '.$output_filepath); |
---|
1530 | } |
---|
1531 | } |
---|
1532 | |
---|
1533 | $upload_dir = $conf['upload_dir'].'/buffer'; |
---|
1534 | $pattern = '/'.$original_sum.'-'.$type.'/'; |
---|
1535 | $chunks = array(); |
---|
1536 | |
---|
1537 | if ($handle = opendir($upload_dir)) |
---|
1538 | { |
---|
1539 | while (false !== ($file = readdir($handle))) |
---|
1540 | { |
---|
1541 | if (preg_match($pattern, $file)) |
---|
1542 | { |
---|
1543 | ws_logfile($file); |
---|
1544 | $chunks[] = $upload_dir.'/'.$file; |
---|
1545 | } |
---|
1546 | } |
---|
1547 | closedir($handle); |
---|
1548 | } |
---|
1549 | |
---|
1550 | sort($chunks); |
---|
1551 | |
---|
1552 | if (function_exists('memory_get_usage')) { |
---|
1553 | ws_logfile('[merge_chunks] memory_get_usage before loading chunks: '.memory_get_usage()); |
---|
1554 | } |
---|
1555 | |
---|
1556 | $i = 0; |
---|
1557 | |
---|
1558 | foreach ($chunks as $chunk) |
---|
1559 | { |
---|
1560 | $string = file_get_contents($chunk); |
---|
1561 | |
---|
1562 | if (function_exists('memory_get_usage')) { |
---|
1563 | ws_logfile('[merge_chunks] memory_get_usage on chunk '.++$i.': '.memory_get_usage()); |
---|
1564 | } |
---|
1565 | |
---|
1566 | if (!file_put_contents($output_filepath, $string, FILE_APPEND)) |
---|
1567 | { |
---|
1568 | return new PwgError(500, '[merge_chunks] error while writting chunks for '.$output_filepath); |
---|
1569 | } |
---|
1570 | |
---|
1571 | unlink($chunk); |
---|
1572 | } |
---|
1573 | |
---|
1574 | if (function_exists('memory_get_usage')) { |
---|
1575 | ws_logfile('[merge_chunks] memory_get_usage after loading chunks: '.memory_get_usage()); |
---|
1576 | } |
---|
1577 | } |
---|
1578 | |
---|
1579 | /** |
---|
1580 | * Function introduced for Piwigo 2.4 and the new "multiple size" |
---|
1581 | * (derivatives) feature. As we only need the biggest sent photo as |
---|
1582 | * "original", we remove chunks for smaller sizes. We can't make it earlier |
---|
1583 | * in ws_images_add_chunk because at this moment we don't know which $type |
---|
1584 | * will be the biggest (we could remove the thumb, but let's use the same |
---|
1585 | * algorithm) |
---|
1586 | */ |
---|
1587 | function remove_chunks($original_sum, $type) |
---|
1588 | { |
---|
1589 | global $conf; |
---|
1590 | |
---|
1591 | $upload_dir = $conf['upload_dir'].'/buffer'; |
---|
1592 | $pattern = '/'.$original_sum.'-'.$type.'/'; |
---|
1593 | $chunks = array(); |
---|
1594 | |
---|
1595 | if ($handle = opendir($upload_dir)) |
---|
1596 | { |
---|
1597 | while (false !== ($file = readdir($handle))) |
---|
1598 | { |
---|
1599 | if (preg_match($pattern, $file)) |
---|
1600 | { |
---|
1601 | $chunks[] = $upload_dir.'/'.$file; |
---|
1602 | } |
---|
1603 | } |
---|
1604 | closedir($handle); |
---|
1605 | } |
---|
1606 | |
---|
1607 | foreach ($chunks as $chunk) |
---|
1608 | { |
---|
1609 | unlink($chunk); |
---|
1610 | } |
---|
1611 | } |
---|
1612 | |
---|
1613 | function ws_images_addFile($params, $service) |
---|
1614 | { |
---|
1615 | ws_logfile(__FUNCTION__.', input : '.var_export($params, true)); |
---|
1616 | // image_id |
---|
1617 | // type {thumb, file, high} |
---|
1618 | // sum -> not used currently (Piwigo 2.4) |
---|
1619 | |
---|
1620 | global $conf; |
---|
1621 | if (!is_admin()) |
---|
1622 | { |
---|
1623 | return new PwgError(401, 'Access denied'); |
---|
1624 | } |
---|
1625 | |
---|
1626 | $params['image_id'] = (int)$params['image_id']; |
---|
1627 | if ($params['image_id'] <= 0) |
---|
1628 | { |
---|
1629 | return new PwgError(WS_ERR_INVALID_PARAM, "Invalid image_id"); |
---|
1630 | } |
---|
1631 | |
---|
1632 | // |
---|
1633 | // what is the path and other infos about the photo? |
---|
1634 | // |
---|
1635 | $query = ' |
---|
1636 | SELECT |
---|
1637 | path, |
---|
1638 | file, |
---|
1639 | md5sum, |
---|
1640 | width, |
---|
1641 | height, |
---|
1642 | filesize |
---|
1643 | FROM '.IMAGES_TABLE.' |
---|
1644 | WHERE id = '.$params['image_id'].' |
---|
1645 | ;'; |
---|
1646 | $image = pwg_db_fetch_assoc(pwg_query($query)); |
---|
1647 | |
---|
1648 | if ($image == null) |
---|
1649 | { |
---|
1650 | return new PwgError(404, "image_id not found"); |
---|
1651 | } |
---|
1652 | |
---|
1653 | // since Piwigo 2.4 and derivatives, we do not take the imported "thumb" |
---|
1654 | // into account |
---|
1655 | if ('thumb' == $params['type']) |
---|
1656 | { |
---|
1657 | remove_chunks($image['md5sum'], $type); |
---|
1658 | return true; |
---|
1659 | } |
---|
1660 | |
---|
1661 | // since Piwigo 2.4 and derivatives, we only care about the "original" |
---|
1662 | $original_type = 'file'; |
---|
1663 | if ('high' == $params['type']) |
---|
1664 | { |
---|
1665 | $original_type = 'high'; |
---|
1666 | } |
---|
1667 | |
---|
1668 | $file_path = $conf['upload_dir'].'/buffer/'.$image['md5sum'].'-original'; |
---|
1669 | |
---|
1670 | merge_chunks($file_path, $image['md5sum'], $original_type); |
---|
1671 | chmod($file_path, 0644); |
---|
1672 | |
---|
1673 | include_once(PHPWG_ROOT_PATH.'admin/include/functions_upload.inc.php'); |
---|
1674 | |
---|
1675 | // if we receive the "file", we only update the original if the "file" is |
---|
1676 | // bigger than current original |
---|
1677 | if ('file' == $params['type']) |
---|
1678 | { |
---|
1679 | $do_update = false; |
---|
1680 | |
---|
1681 | $infos = pwg_image_infos($file_path); |
---|
1682 | |
---|
1683 | foreach (array('width', 'height', 'filesize') as $image_info) |
---|
1684 | { |
---|
1685 | if ($infos[$image_info] > $image[$image_info]) |
---|
1686 | { |
---|
1687 | $do_update = true; |
---|
1688 | } |
---|
1689 | } |
---|
1690 | |
---|
1691 | if (!$do_update) |
---|
1692 | { |
---|
1693 | unlink($file_path); |
---|
1694 | return true; |
---|
1695 | } |
---|
1696 | } |
---|
1697 | |
---|
1698 | $image_id = add_uploaded_file( |
---|
1699 | $file_path, |
---|
1700 | $image['file'], |
---|
1701 | null, |
---|
1702 | null, |
---|
1703 | $params['image_id'], |
---|
1704 | $image['md5sum'] // we force the md5sum to remain the same |
---|
1705 | ); |
---|
1706 | } |
---|
1707 | |
---|
1708 | function ws_images_add($params, $service) |
---|
1709 | { |
---|
1710 | global $conf, $user; |
---|
1711 | if (!is_admin()) |
---|
1712 | { |
---|
1713 | return new PwgError(401, 'Access denied'); |
---|
1714 | } |
---|
1715 | |
---|
1716 | foreach ($params as $param_key => $param_value) { |
---|
1717 | ws_logfile( |
---|
1718 | sprintf( |
---|
1719 | '[pwg.images.add] input param "%s" : "%s"', |
---|
1720 | $param_key, |
---|
1721 | is_null($param_value) ? 'NULL' : $param_value |
---|
1722 | ) |
---|
1723 | ); |
---|
1724 | } |
---|
1725 | |
---|
1726 | $params['image_id'] = (int)$params['image_id']; |
---|
1727 | if ($params['image_id'] > 0) |
---|
1728 | { |
---|
1729 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
---|
1730 | |
---|
1731 | $query=' |
---|
1732 | SELECT * |
---|
1733 | FROM '.IMAGES_TABLE.' |
---|
1734 | WHERE id = '.$params['image_id'].' |
---|
1735 | ;'; |
---|
1736 | |
---|
1737 | $image_row = pwg_db_fetch_assoc(pwg_query($query)); |
---|
1738 | if ($image_row == null) |
---|
1739 | { |
---|
1740 | return new PwgError(404, "image_id not found"); |
---|
1741 | } |
---|
1742 | } |
---|
1743 | |
---|
1744 | // does the image already exists ? |
---|
1745 | if ($params['check_uniqueness']) |
---|
1746 | { |
---|
1747 | if ('md5sum' == $conf['uniqueness_mode']) |
---|
1748 | { |
---|
1749 | $where_clause = "md5sum = '".$params['original_sum']."'"; |
---|
1750 | } |
---|
1751 | if ('filename' == $conf['uniqueness_mode']) |
---|
1752 | { |
---|
1753 | $where_clause = "file = '".$params['original_filename']."'"; |
---|
1754 | } |
---|
1755 | |
---|
1756 | $query = ' |
---|
1757 | SELECT |
---|
1758 | COUNT(*) AS counter |
---|
1759 | FROM '.IMAGES_TABLE.' |
---|
1760 | WHERE '.$where_clause.' |
---|
1761 | ;'; |
---|
1762 | list($counter) = pwg_db_fetch_row(pwg_query($query)); |
---|
1763 | if ($counter != 0) { |
---|
1764 | return new PwgError(500, 'file already exists'); |
---|
1765 | } |
---|
1766 | } |
---|
1767 | |
---|
1768 | // due to the new feature "derivatives" (multiple sizes) introduced for |
---|
1769 | // Piwigo 2.4, we only take the biggest photos sent on |
---|
1770 | // pwg.images.addChunk. If "high" is available we use it as "original" |
---|
1771 | // else we use "file". |
---|
1772 | remove_chunks($params['original_sum'], 'thumb'); |
---|
1773 | |
---|
1774 | if (isset($params['high_sum'])) |
---|
1775 | { |
---|
1776 | $original_type = 'high'; |
---|
1777 | remove_chunks($params['original_sum'], 'file'); |
---|
1778 | } |
---|
1779 | else |
---|
1780 | { |
---|
1781 | $original_type = 'file'; |
---|
1782 | } |
---|
1783 | |
---|
1784 | $file_path = $conf['upload_dir'].'/buffer/'.$params['original_sum'].'-original'; |
---|
1785 | |
---|
1786 | merge_chunks($file_path, $params['original_sum'], $original_type); |
---|
1787 | chmod($file_path, 0644); |
---|
1788 | |
---|
1789 | include_once(PHPWG_ROOT_PATH.'admin/include/functions_upload.inc.php'); |
---|
1790 | |
---|
1791 | $image_id = add_uploaded_file( |
---|
1792 | $file_path, |
---|
1793 | $params['original_filename'], |
---|
1794 | null, // categories |
---|
1795 | isset($params['level']) ? $params['level'] : null, |
---|
1796 | $params['image_id'] > 0 ? $params['image_id'] : null, |
---|
1797 | $params['original_sum'] |
---|
1798 | ); |
---|
1799 | |
---|
1800 | $info_columns = array( |
---|
1801 | 'name', |
---|
1802 | 'author', |
---|
1803 | 'comment', |
---|
1804 | 'date_creation', |
---|
1805 | ); |
---|
1806 | |
---|
1807 | $update = array(); |
---|
1808 | |
---|
1809 | foreach ($info_columns as $key) |
---|
1810 | { |
---|
1811 | if (isset($params[$key])) |
---|
1812 | { |
---|
1813 | $update[$key] = $params[$key]; |
---|
1814 | } |
---|
1815 | } |
---|
1816 | |
---|
1817 | if (count(array_keys($update)) > 0) |
---|
1818 | { |
---|
1819 | single_update( |
---|
1820 | IMAGES_TABLE, |
---|
1821 | $update, |
---|
1822 | array('id' => $image_id) |
---|
1823 | ); |
---|
1824 | } |
---|
1825 | |
---|
1826 | $url_params = array('image_id' => $image_id); |
---|
1827 | |
---|
1828 | // let's add links between the image and the categories |
---|
1829 | if (isset($params['categories'])) |
---|
1830 | { |
---|
1831 | ws_add_image_category_relations($image_id, $params['categories']); |
---|
1832 | |
---|
1833 | if (preg_match('/^\d+/', $params['categories'], $matches)) { |
---|
1834 | $category_id = $matches[0]; |
---|
1835 | |
---|
1836 | $query = ' |
---|
1837 | SELECT id, name, permalink |
---|
1838 | FROM '.CATEGORIES_TABLE.' |
---|
1839 | WHERE id = '.$category_id.' |
---|
1840 | ;'; |
---|
1841 | $result = pwg_query($query); |
---|
1842 | $category = pwg_db_fetch_assoc($result); |
---|
1843 | |
---|
1844 | $url_params['section'] = 'categories'; |
---|
1845 | $url_params['category'] = $category; |
---|
1846 | } |
---|
1847 | } |
---|
1848 | |
---|
1849 | // and now, let's create tag associations |
---|
1850 | if (isset($params['tag_ids']) and !empty($params['tag_ids'])) |
---|
1851 | { |
---|
1852 | set_tags( |
---|
1853 | explode(',', $params['tag_ids']), |
---|
1854 | $image_id |
---|
1855 | ); |
---|
1856 | } |
---|
1857 | |
---|
1858 | invalidate_user_cache(); |
---|
1859 | |
---|
1860 | return array( |
---|
1861 | 'image_id' => $image_id, |
---|
1862 | 'url' => make_picture_url($url_params), |
---|
1863 | ); |
---|
1864 | } |
---|
1865 | |
---|
1866 | function ws_images_addSimple($params, $service) |
---|
1867 | { |
---|
1868 | global $conf; |
---|
1869 | if (!is_admin()) |
---|
1870 | { |
---|
1871 | return new PwgError(401, 'Access denied'); |
---|
1872 | } |
---|
1873 | |
---|
1874 | if (!$service->isPost()) |
---|
1875 | { |
---|
1876 | return new PwgError(405, "This method requires HTTP POST"); |
---|
1877 | } |
---|
1878 | |
---|
1879 | if (!isset($_FILES['image'])) |
---|
1880 | { |
---|
1881 | return new PwgError(405, "The image (file) parameter is missing"); |
---|
1882 | } |
---|
1883 | |
---|
1884 | $params['image_id'] = (int)$params['image_id']; |
---|
1885 | if ($params['image_id'] > 0) |
---|
1886 | { |
---|
1887 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
---|
1888 | |
---|
1889 | $query=' |
---|
1890 | SELECT * |
---|
1891 | FROM '.IMAGES_TABLE.' |
---|
1892 | WHERE id = '.$params['image_id'].' |
---|
1893 | ;'; |
---|
1894 | |
---|
1895 | $image_row = pwg_db_fetch_assoc(pwg_query($query)); |
---|
1896 | if ($image_row == null) |
---|
1897 | { |
---|
1898 | return new PwgError(404, "image_id not found"); |
---|
1899 | } |
---|
1900 | } |
---|
1901 | |
---|
1902 | // category |
---|
1903 | $params['category'] = (int)$params['category']; |
---|
1904 | if ($params['category'] <= 0 and $params['image_id'] <= 0) |
---|
1905 | { |
---|
1906 | return new PwgError(WS_ERR_INVALID_PARAM, "Invalid category_id"); |
---|
1907 | } |
---|
1908 | |
---|
1909 | include_once(PHPWG_ROOT_PATH.'admin/include/functions_upload.inc.php'); |
---|
1910 | |
---|
1911 | $image_id = add_uploaded_file( |
---|
1912 | $_FILES['image']['tmp_name'], |
---|
1913 | $_FILES['image']['name'], |
---|
1914 | $params['category'] > 0 ? array($params['category']) : null, |
---|
1915 | 8, |
---|
1916 | $params['image_id'] > 0 ? $params['image_id'] : null |
---|
1917 | ); |
---|
1918 | |
---|
1919 | $info_columns = array( |
---|
1920 | 'name', |
---|
1921 | 'author', |
---|
1922 | 'comment', |
---|
1923 | 'level', |
---|
1924 | 'date_creation', |
---|
1925 | ); |
---|
1926 | |
---|
1927 | foreach ($info_columns as $key) |
---|
1928 | { |
---|
1929 | if (isset($params[$key])) |
---|
1930 | { |
---|
1931 | $update[$key] = $params[$key]; |
---|
1932 | } |
---|
1933 | } |
---|
1934 | |
---|
1935 | if (count(array_keys($update)) > 0) |
---|
1936 | { |
---|
1937 | $update['id'] = $image_id; |
---|
1938 | |
---|
1939 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
---|
1940 | mass_updates( |
---|
1941 | IMAGES_TABLE, |
---|
1942 | array( |
---|
1943 | 'primary' => array('id'), |
---|
1944 | 'update' => array_diff(array_keys($update), array('id')) |
---|
1945 | ), |
---|
1946 | array($update) |
---|
1947 | ); |
---|
1948 | } |
---|
1949 | |
---|
1950 | |
---|
1951 | if (isset($params['tags']) and !empty($params['tags'])) |
---|
1952 | { |
---|
1953 | $tag_ids = array(); |
---|
1954 | if (is_array($params['tags'])) |
---|
1955 | { |
---|
1956 | foreach ($params['tags'] as $tag_name) |
---|
1957 | { |
---|
1958 | $tag_ids[] = tag_id_from_tag_name($tag_name); |
---|
1959 | } |
---|
1960 | } |
---|
1961 | else |
---|
1962 | { |
---|
1963 | $tag_names = preg_split('~(?<!\\\),~', $params['tags']); |
---|
1964 | foreach ($tag_names as $tag_name) |
---|
1965 | { |
---|
1966 | $tag_ids[] = tag_id_from_tag_name(preg_replace('#\\\\*,#', ',', $tag_name)); |
---|
1967 | } |
---|
1968 | } |
---|
1969 | |
---|
1970 | add_tags($tag_ids, array($image_id)); |
---|
1971 | } |
---|
1972 | |
---|
1973 | $url_params = array('image_id' => $image_id); |
---|
1974 | |
---|
1975 | if ($params['category'] > 0) |
---|
1976 | { |
---|
1977 | $query = ' |
---|
1978 | SELECT id, name, permalink |
---|
1979 | FROM '.CATEGORIES_TABLE.' |
---|
1980 | WHERE id = '.$params['category'].' |
---|
1981 | ;'; |
---|
1982 | $result = pwg_query($query); |
---|
1983 | $category = pwg_db_fetch_assoc($result); |
---|
1984 | |
---|
1985 | $url_params['section'] = 'categories'; |
---|
1986 | $url_params['category'] = $category; |
---|
1987 | } |
---|
1988 | |
---|
1989 | // update metadata from the uploaded file (exif/iptc), even if the sync |
---|
1990 | // was already performed by add_uploaded_file(). |
---|
1991 | |
---|
1992 | require_once(PHPWG_ROOT_PATH.'admin/include/functions_metadata.php'); |
---|
1993 | sync_metadata(array($image_id)); |
---|
1994 | |
---|
1995 | return array( |
---|
1996 | 'image_id' => $image_id, |
---|
1997 | 'url' => make_picture_url($url_params), |
---|
1998 | ); |
---|
1999 | } |
---|
2000 | |
---|
2001 | function ws_rates_delete($params, $service) |
---|
2002 | { |
---|
2003 | global $conf; |
---|
2004 | |
---|
2005 | if (!$service->isPost()) |
---|
2006 | { |
---|
2007 | return new PwgError(405, 'This method requires HTTP POST'); |
---|
2008 | } |
---|
2009 | |
---|
2010 | if (!is_admin()) |
---|
2011 | { |
---|
2012 | return new PwgError(401, 'Access denied'); |
---|
2013 | } |
---|
2014 | |
---|
2015 | $user_id = (int)$params['user_id']; |
---|
2016 | if ($user_id<=0) |
---|
2017 | { |
---|
2018 | return new PwgError(WS_ERR_INVALID_PARAM, 'Invalid user_id'); |
---|
2019 | } |
---|
2020 | |
---|
2021 | $query = ' |
---|
2022 | DELETE FROM '.RATE_TABLE.' |
---|
2023 | WHERE user_id='.$user_id; |
---|
2024 | |
---|
2025 | if (!empty($params['anonymous_id'])) |
---|
2026 | { |
---|
2027 | $query .= ' AND anonymous_id=\''.$params['anonymous_id'].'\''; |
---|
2028 | } |
---|
2029 | |
---|
2030 | $changes = pwg_db_changes(pwg_query($query)); |
---|
2031 | if ($changes) |
---|
2032 | { |
---|
2033 | include_once(PHPWG_ROOT_PATH.'include/functions_rate.inc.php'); |
---|
2034 | update_rating_score(); |
---|
2035 | } |
---|
2036 | return $changes; |
---|
2037 | } |
---|
2038 | |
---|
2039 | |
---|
2040 | /** |
---|
2041 | * perform a login (web service method) |
---|
2042 | */ |
---|
2043 | function ws_session_login($params, $service) |
---|
2044 | { |
---|
2045 | global $conf; |
---|
2046 | |
---|
2047 | if (!$service->isPost()) |
---|
2048 | { |
---|
2049 | return new PwgError(405, "This method requires HTTP POST"); |
---|
2050 | } |
---|
2051 | if (try_log_user($params['username'], $params['password'],false)) |
---|
2052 | { |
---|
2053 | return true; |
---|
2054 | } |
---|
2055 | return new PwgError(999, 'Invalid username/password'); |
---|
2056 | } |
---|
2057 | |
---|
2058 | |
---|
2059 | /** |
---|
2060 | * performs a logout (web service method) |
---|
2061 | */ |
---|
2062 | function ws_session_logout($params, $service) |
---|
2063 | { |
---|
2064 | if (!is_a_guest()) |
---|
2065 | { |
---|
2066 | logout_user(); |
---|
2067 | } |
---|
2068 | return true; |
---|
2069 | } |
---|
2070 | |
---|
2071 | function ws_session_getStatus($params, $service) |
---|
2072 | { |
---|
2073 | global $user; |
---|
2074 | $res = array(); |
---|
2075 | $res['username'] = is_a_guest() ? 'guest' : stripslashes($user['username']); |
---|
2076 | foreach ( array('status', 'theme', 'language') as $k ) |
---|
2077 | { |
---|
2078 | $res[$k] = $user[$k]; |
---|
2079 | } |
---|
2080 | $res['pwg_token'] = get_pwg_token(); |
---|
2081 | $res['charset'] = get_pwg_charset(); |
---|
2082 | |
---|
2083 | list($dbnow) = pwg_db_fetch_row(pwg_query('SELECT NOW();')); |
---|
2084 | $res['current_datetime'] = $dbnow; |
---|
2085 | |
---|
2086 | return $res; |
---|
2087 | } |
---|
2088 | |
---|
2089 | |
---|
2090 | /** |
---|
2091 | * returns a list of tags (web service method) |
---|
2092 | */ |
---|
2093 | function ws_tags_getList($params, $service) |
---|
2094 | { |
---|
2095 | $tags = get_available_tags(); |
---|
2096 | if ($params['sort_by_counter']) |
---|
2097 | { |
---|
2098 | usort($tags, create_function('$a,$b', 'return -$a["counter"]+$b["counter"];') ); |
---|
2099 | } |
---|
2100 | else |
---|
2101 | { |
---|
2102 | usort($tags, 'tag_alpha_compare'); |
---|
2103 | } |
---|
2104 | for ($i=0; $i<count($tags); $i++) |
---|
2105 | { |
---|
2106 | $tags[$i]['id'] = (int)$tags[$i]['id']; |
---|
2107 | $tags[$i]['counter'] = (int)$tags[$i]['counter']; |
---|
2108 | $tags[$i]['url'] = make_index_url( |
---|
2109 | array( |
---|
2110 | 'section'=>'tags', |
---|
2111 | 'tags'=>array($tags[$i]) |
---|
2112 | ) |
---|
2113 | ); |
---|
2114 | } |
---|
2115 | return array('tags' => new PwgNamedArray($tags, 'tag', ws_std_get_tag_xml_attributes()) ); |
---|
2116 | } |
---|
2117 | |
---|
2118 | /** |
---|
2119 | * returns the list of tags as you can see them in administration (web |
---|
2120 | * service method). |
---|
2121 | * |
---|
2122 | * Only admin can run this method and permissions are not taken into |
---|
2123 | * account. |
---|
2124 | */ |
---|
2125 | function ws_tags_getAdminList($params, $service) |
---|
2126 | { |
---|
2127 | if (!is_admin()) |
---|
2128 | { |
---|
2129 | return new PwgError(401, 'Access denied'); |
---|
2130 | } |
---|
2131 | |
---|
2132 | $tags = get_all_tags(); |
---|
2133 | return array( |
---|
2134 | 'tags' => new PwgNamedArray( |
---|
2135 | $tags, |
---|
2136 | 'tag', |
---|
2137 | ws_std_get_tag_xml_attributes() |
---|
2138 | ) |
---|
2139 | ); |
---|
2140 | } |
---|
2141 | |
---|
2142 | /** |
---|
2143 | * returns a list of images for tags (web service method) |
---|
2144 | */ |
---|
2145 | function ws_tags_getImages($params, $service) |
---|
2146 | { |
---|
2147 | global $conf; |
---|
2148 | |
---|
2149 | // first build all the tag_ids we are interested in |
---|
2150 | $params['tag_id'] = array_map( 'intval',$params['tag_id'] ); |
---|
2151 | $tags = find_tags($params['tag_id'], $params['tag_url_name'], $params['tag_name']); |
---|
2152 | $tags_by_id = array(); |
---|
2153 | foreach( $tags as $tag ) |
---|
2154 | { |
---|
2155 | $tags['id'] = (int)$tag['id']; |
---|
2156 | $tags_by_id[ $tag['id'] ] = $tag; |
---|
2157 | } |
---|
2158 | unset($tags); |
---|
2159 | $tag_ids = array_keys($tags_by_id); |
---|
2160 | |
---|
2161 | |
---|
2162 | $where_clauses = ws_std_image_sql_filter($params); |
---|
2163 | if (!empty($where_clauses)) |
---|
2164 | { |
---|
2165 | $where_clauses = implode( ' AND ', $where_clauses); |
---|
2166 | } |
---|
2167 | $image_ids = get_image_ids_for_tags( |
---|
2168 | $tag_ids, |
---|
2169 | $params['tag_mode_and'] ? 'AND' : 'OR', |
---|
2170 | $where_clauses, |
---|
2171 | ws_std_image_sql_order($params) ); |
---|
2172 | |
---|
2173 | $count_set = count($image_ids); |
---|
2174 | $params['per_page'] = (int)$params['per_page']; |
---|
2175 | $params['page'] = (int)$params['page']; |
---|
2176 | $image_ids = array_slice($image_ids, $params['per_page']*$params['page'], $params['per_page'] ); |
---|
2177 | |
---|
2178 | $image_tag_map = array(); |
---|
2179 | if ( !empty($image_ids) and !$params['tag_mode_and'] ) |
---|
2180 | { // build list of image ids with associated tags per image |
---|
2181 | $query = ' |
---|
2182 | SELECT image_id, GROUP_CONCAT(tag_id) AS tag_ids |
---|
2183 | FROM '.IMAGE_TAG_TABLE.' |
---|
2184 | WHERE tag_id IN ('.implode(',',$tag_ids).') AND image_id IN ('.implode(',',$image_ids).') |
---|
2185 | GROUP BY image_id'; |
---|
2186 | $result = pwg_query($query); |
---|
2187 | while ( $row=pwg_db_fetch_assoc($result) ) |
---|
2188 | { |
---|
2189 | $row['image_id'] = (int)$row['image_id']; |
---|
2190 | $image_ids[] = $row['image_id']; |
---|
2191 | $image_tag_map[ $row['image_id'] ] = explode(',', $row['tag_ids']); |
---|
2192 | } |
---|
2193 | } |
---|
2194 | |
---|
2195 | $images = array(); |
---|
2196 | if (!empty($image_ids)) |
---|
2197 | { |
---|
2198 | $rank_of = array_flip($image_ids); |
---|
2199 | $result = pwg_query(' |
---|
2200 | SELECT * FROM '.IMAGES_TABLE.' |
---|
2201 | WHERE id IN ('.implode(',',$image_ids).')'); |
---|
2202 | while ($row = pwg_db_fetch_assoc($result)) |
---|
2203 | { |
---|
2204 | $image = array(); |
---|
2205 | $image['rank'] = $rank_of[ $row['id'] ]; |
---|
2206 | foreach ( array('id', 'width', 'height', 'hit') as $k ) |
---|
2207 | { |
---|
2208 | if (isset($row[$k])) |
---|
2209 | { |
---|
2210 | $image[$k] = (int)$row[$k]; |
---|
2211 | } |
---|
2212 | } |
---|
2213 | foreach ( array('file', 'name', 'comment', 'date_creation', 'date_available') as $k ) |
---|
2214 | { |
---|
2215 | $image[$k] = $row[$k]; |
---|
2216 | } |
---|
2217 | $image = array_merge( $image, ws_std_get_urls($row) ); |
---|
2218 | |
---|
2219 | $image_tag_ids = ($params['tag_mode_and']) ? $tag_ids : $image_tag_map[$image['id']]; |
---|
2220 | $image_tags = array(); |
---|
2221 | foreach ($image_tag_ids as $tag_id) |
---|
2222 | { |
---|
2223 | $url = make_index_url( |
---|
2224 | array( |
---|
2225 | 'section'=>'tags', |
---|
2226 | 'tags'=> array($tags_by_id[$tag_id]) |
---|
2227 | ) |
---|
2228 | ); |
---|
2229 | $page_url = make_picture_url( |
---|
2230 | array( |
---|
2231 | 'section'=>'tags', |
---|
2232 | 'tags'=> array($tags_by_id[$tag_id]), |
---|
2233 | 'image_id' => $row['id'], |
---|
2234 | 'image_file' => $row['file'], |
---|
2235 | ) |
---|
2236 | ); |
---|
2237 | $image_tags[] = array( |
---|
2238 | 'id' => (int)$tag_id, |
---|
2239 | 'url' => $url, |
---|
2240 | 'page_url' => $page_url, |
---|
2241 | ); |
---|
2242 | } |
---|
2243 | $image['tags'] = new PwgNamedArray($image_tags, 'tag', ws_std_get_tag_xml_attributes() ); |
---|
2244 | $images[] = $image; |
---|
2245 | } |
---|
2246 | usort($images, 'rank_compare'); |
---|
2247 | unset($rank_of); |
---|
2248 | } |
---|
2249 | |
---|
2250 | return array( |
---|
2251 | 'paging' => new PwgNamedStruct( |
---|
2252 | array( |
---|
2253 | 'page' => $params['page'], |
---|
2254 | 'per_page' => $params['per_page'], |
---|
2255 | 'count' => count($images), |
---|
2256 | 'total_count' => $count_set, |
---|
2257 | ) ), |
---|
2258 | 'images' => new PwgNamedArray($images, 'image', |
---|
2259 | ws_std_get_image_xml_attributes() ) |
---|
2260 | ); |
---|
2261 | } |
---|
2262 | |
---|
2263 | function ws_categories_add($params, $service) |
---|
2264 | { |
---|
2265 | if (!is_admin()) |
---|
2266 | { |
---|
2267 | return new PwgError(401, 'Access denied'); |
---|
2268 | } |
---|
2269 | |
---|
2270 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
---|
2271 | |
---|
2272 | $options = array(); |
---|
2273 | if (!empty($params['status']) and in_array($params['status'], array('private','public'))) |
---|
2274 | { |
---|
2275 | $options['status'] = $params['status']; |
---|
2276 | } |
---|
2277 | |
---|
2278 | if (!empty($params['visible']) and in_array($params['visible'], array('true','false'))) |
---|
2279 | { |
---|
2280 | $options['visible'] = get_boolean($params['visible']); |
---|
2281 | } |
---|
2282 | |
---|
2283 | if (!empty($params['commentable']) and in_array($params['commentable'], array('true','false')) ) |
---|
2284 | { |
---|
2285 | $options['commentable'] = get_boolean($params['commentable']); |
---|
2286 | } |
---|
2287 | |
---|
2288 | if (!empty($params['comment'])) |
---|
2289 | { |
---|
2290 | $options['comment'] = $params['comment']; |
---|
2291 | } |
---|
2292 | |
---|
2293 | |
---|
2294 | $creation_output = create_virtual_category( |
---|
2295 | $params['name'], |
---|
2296 | $params['parent'], |
---|
2297 | $options |
---|
2298 | ); |
---|
2299 | |
---|
2300 | if (isset($creation_output['error'])) |
---|
2301 | { |
---|
2302 | return new PwgError(500, $creation_output['error']); |
---|
2303 | } |
---|
2304 | |
---|
2305 | invalidate_user_cache(); |
---|
2306 | |
---|
2307 | return $creation_output; |
---|
2308 | } |
---|
2309 | |
---|
2310 | function ws_tags_add($params, $service) |
---|
2311 | { |
---|
2312 | if (!is_admin()) |
---|
2313 | { |
---|
2314 | return new PwgError(401, 'Access denied'); |
---|
2315 | } |
---|
2316 | |
---|
2317 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
---|
2318 | |
---|
2319 | $creation_output = create_tag($params['name']); |
---|
2320 | |
---|
2321 | if (isset($creation_output['error'])) |
---|
2322 | { |
---|
2323 | return new PwgError(500, $creation_output['error']); |
---|
2324 | } |
---|
2325 | |
---|
2326 | return $creation_output; |
---|
2327 | } |
---|
2328 | |
---|
2329 | function ws_images_exist($params, $service) |
---|
2330 | { |
---|
2331 | ws_logfile(__FUNCTION__.' '.var_export($params, true)); |
---|
2332 | |
---|
2333 | global $conf; |
---|
2334 | |
---|
2335 | if (!is_admin()) |
---|
2336 | { |
---|
2337 | return new PwgError(401, 'Access denied'); |
---|
2338 | } |
---|
2339 | |
---|
2340 | $split_pattern = '/[\s,;\|]/'; |
---|
2341 | |
---|
2342 | if ('md5sum' == $conf['uniqueness_mode']) |
---|
2343 | { |
---|
2344 | // search among photos the list of photos already added, based on md5sum |
---|
2345 | // list |
---|
2346 | $md5sums = preg_split( |
---|
2347 | $split_pattern, |
---|
2348 | $params['md5sum_list'], |
---|
2349 | -1, |
---|
2350 | PREG_SPLIT_NO_EMPTY |
---|
2351 | ); |
---|
2352 | |
---|
2353 | $query = ' |
---|
2354 | SELECT |
---|
2355 | id, |
---|
2356 | md5sum |
---|
2357 | FROM '.IMAGES_TABLE.' |
---|
2358 | WHERE md5sum IN (\''.implode("','", $md5sums).'\') |
---|
2359 | ;'; |
---|
2360 | $id_of_md5 = simple_hash_from_query($query, 'md5sum', 'id'); |
---|
2361 | |
---|
2362 | $result = array(); |
---|
2363 | |
---|
2364 | foreach ($md5sums as $md5sum) |
---|
2365 | { |
---|
2366 | $result[$md5sum] = null; |
---|
2367 | if (isset($id_of_md5[$md5sum])) |
---|
2368 | { |
---|
2369 | $result[$md5sum] = $id_of_md5[$md5sum]; |
---|
2370 | } |
---|
2371 | } |
---|
2372 | } |
---|
2373 | |
---|
2374 | if ('filename' == $conf['uniqueness_mode']) |
---|
2375 | { |
---|
2376 | // search among photos the list of photos already added, based on |
---|
2377 | // filename list |
---|
2378 | $filenames = preg_split( |
---|
2379 | $split_pattern, |
---|
2380 | $params['filename_list'], |
---|
2381 | -1, |
---|
2382 | PREG_SPLIT_NO_EMPTY |
---|
2383 | ); |
---|
2384 | |
---|
2385 | $query = ' |
---|
2386 | SELECT |
---|
2387 | id, |
---|
2388 | file |
---|
2389 | FROM '.IMAGES_TABLE.' |
---|
2390 | WHERE file IN (\''.implode("','", $filenames).'\') |
---|
2391 | ;'; |
---|
2392 | $id_of_filename = simple_hash_from_query($query, 'file', 'id'); |
---|
2393 | |
---|
2394 | $result = array(); |
---|
2395 | |
---|
2396 | foreach ($filenames as $filename) |
---|
2397 | { |
---|
2398 | $result[$filename] = null; |
---|
2399 | if (isset($id_of_filename[$filename])) |
---|
2400 | { |
---|
2401 | $result[$filename] = $id_of_filename[$filename]; |
---|
2402 | } |
---|
2403 | } |
---|
2404 | } |
---|
2405 | |
---|
2406 | return $result; |
---|
2407 | } |
---|
2408 | |
---|
2409 | function ws_images_checkFiles($params, $service) |
---|
2410 | { |
---|
2411 | ws_logfile(__FUNCTION__.', input : '.var_export($params, true)); |
---|
2412 | |
---|
2413 | if (!is_admin()) |
---|
2414 | { |
---|
2415 | return new PwgError(401, 'Access denied'); |
---|
2416 | } |
---|
2417 | |
---|
2418 | // input parameters |
---|
2419 | // |
---|
2420 | // image_id |
---|
2421 | // thumbnail_sum |
---|
2422 | // file_sum |
---|
2423 | // high_sum |
---|
2424 | |
---|
2425 | $params['image_id'] = (int)$params['image_id']; |
---|
2426 | if ($params['image_id'] <= 0) |
---|
2427 | { |
---|
2428 | return new PwgError(WS_ERR_INVALID_PARAM, "Invalid image_id"); |
---|
2429 | } |
---|
2430 | |
---|
2431 | $query = ' |
---|
2432 | SELECT |
---|
2433 | path |
---|
2434 | FROM '.IMAGES_TABLE.' |
---|
2435 | WHERE id = '.$params['image_id'].' |
---|
2436 | ;'; |
---|
2437 | $result = pwg_query($query); |
---|
2438 | if (pwg_db_num_rows($result) == 0) |
---|
2439 | { |
---|
2440 | return new PwgError(404, "image_id not found"); |
---|
2441 | } |
---|
2442 | list($path) = pwg_db_fetch_row($result); |
---|
2443 | |
---|
2444 | $ret = array(); |
---|
2445 | |
---|
2446 | if (isset($params['thumbnail_sum'])) |
---|
2447 | { |
---|
2448 | // We always say the thumbnail is equal to create no reaction on the |
---|
2449 | // other side. Since Piwigo 2.4 and derivatives, the thumbnails and web |
---|
2450 | // sizes are always generated by Piwigo |
---|
2451 | $ret['thumbnail'] = 'equals'; |
---|
2452 | } |
---|
2453 | |
---|
2454 | if (isset($params['high_sum'])) |
---|
2455 | { |
---|
2456 | $ret['file'] = 'equals'; |
---|
2457 | $compare_type = 'high'; |
---|
2458 | } |
---|
2459 | elseif (isset($params['file_sum'])) |
---|
2460 | { |
---|
2461 | $compare_type = 'file'; |
---|
2462 | } |
---|
2463 | |
---|
2464 | if (isset($compare_type)) |
---|
2465 | { |
---|
2466 | ws_logfile(__FUNCTION__.', md5_file($path) = '.md5_file($path)); |
---|
2467 | if (md5_file($path) != $params[$compare_type.'_sum']) |
---|
2468 | { |
---|
2469 | $ret[$compare_type] = 'differs'; |
---|
2470 | } |
---|
2471 | else |
---|
2472 | { |
---|
2473 | $ret[$compare_type] = 'equals'; |
---|
2474 | } |
---|
2475 | } |
---|
2476 | |
---|
2477 | ws_logfile(__FUNCTION__.', output : '.var_export($ret, true)); |
---|
2478 | |
---|
2479 | return $ret; |
---|
2480 | } |
---|
2481 | |
---|
2482 | function ws_images_setInfo($params, $service) |
---|
2483 | { |
---|
2484 | global $conf; |
---|
2485 | if (!is_admin()) |
---|
2486 | { |
---|
2487 | return new PwgError(401, 'Access denied'); |
---|
2488 | } |
---|
2489 | |
---|
2490 | if (!$service->isPost()) |
---|
2491 | { |
---|
2492 | return new PwgError(405, "This method requires HTTP POST"); |
---|
2493 | } |
---|
2494 | |
---|
2495 | $params['image_id'] = (int)$params['image_id']; |
---|
2496 | if ($params['image_id'] <= 0) |
---|
2497 | { |
---|
2498 | return new PwgError(WS_ERR_INVALID_PARAM, "Invalid image_id"); |
---|
2499 | } |
---|
2500 | |
---|
2501 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
---|
2502 | |
---|
2503 | $query=' |
---|
2504 | SELECT * |
---|
2505 | FROM '.IMAGES_TABLE.' |
---|
2506 | WHERE id = '.$params['image_id'].' |
---|
2507 | ;'; |
---|
2508 | |
---|
2509 | $image_row = pwg_db_fetch_assoc(pwg_query($query)); |
---|
2510 | if ($image_row == null) |
---|
2511 | { |
---|
2512 | return new PwgError(404, "image_id not found"); |
---|
2513 | } |
---|
2514 | |
---|
2515 | // database registration |
---|
2516 | $update = array(); |
---|
2517 | |
---|
2518 | $info_columns = array( |
---|
2519 | 'name', |
---|
2520 | 'author', |
---|
2521 | 'comment', |
---|
2522 | 'level', |
---|
2523 | 'date_creation', |
---|
2524 | ); |
---|
2525 | |
---|
2526 | foreach ($info_columns as $key) |
---|
2527 | { |
---|
2528 | if (isset($params[$key])) |
---|
2529 | { |
---|
2530 | if ('fill_if_empty' == $params['single_value_mode']) |
---|
2531 | { |
---|
2532 | if (empty($image_row[$key])) |
---|
2533 | { |
---|
2534 | $update[$key] = $params[$key]; |
---|
2535 | } |
---|
2536 | } |
---|
2537 | elseif ('replace' == $params['single_value_mode']) |
---|
2538 | { |
---|
2539 | $update[$key] = $params[$key]; |
---|
2540 | } |
---|
2541 | else |
---|
2542 | { |
---|
2543 | return new PwgError( |
---|
2544 | 500, |
---|
2545 | '[ws_images_setInfo]' |
---|
2546 | .' invalid parameter single_value_mode "'.$params['single_value_mode'].'"' |
---|
2547 | .', possible values are {fill_if_empty, replace}.' |
---|
2548 | ); |
---|
2549 | } |
---|
2550 | } |
---|
2551 | } |
---|
2552 | |
---|
2553 | if (isset($params['file'])) |
---|
2554 | { |
---|
2555 | if (!empty($image_row['storage_category_id'])) |
---|
2556 | { |
---|
2557 | return new PwgError(500, '[ws_images_setInfo] updating "file" is forbidden on photos added by synchronization'); |
---|
2558 | } |
---|
2559 | |
---|
2560 | $update['file'] = $params['file']; |
---|
2561 | } |
---|
2562 | |
---|
2563 | if (count(array_keys($update)) > 0) |
---|
2564 | { |
---|
2565 | $update['id'] = $params['image_id']; |
---|
2566 | |
---|
2567 | mass_updates( |
---|
2568 | IMAGES_TABLE, |
---|
2569 | array( |
---|
2570 | 'primary' => array('id'), |
---|
2571 | 'update' => array_diff(array_keys($update), array('id')) |
---|
2572 | ), |
---|
2573 | array($update) |
---|
2574 | ); |
---|
2575 | } |
---|
2576 | |
---|
2577 | if (isset($params['categories'])) |
---|
2578 | { |
---|
2579 | ws_add_image_category_relations( |
---|
2580 | $params['image_id'], |
---|
2581 | $params['categories'], |
---|
2582 | ('replace' == $params['multiple_value_mode'] ? true : false) |
---|
2583 | ); |
---|
2584 | } |
---|
2585 | |
---|
2586 | // and now, let's create tag associations |
---|
2587 | if (isset($params['tag_ids'])) |
---|
2588 | { |
---|
2589 | $tag_ids = array(); |
---|
2590 | |
---|
2591 | foreach (explode(',', $params['tag_ids']) as $candidate) |
---|
2592 | { |
---|
2593 | $candidate = trim($candidate); |
---|
2594 | |
---|
2595 | if (preg_match(PATTERN_ID, $candidate)) |
---|
2596 | { |
---|
2597 | $tag_ids[] = $candidate; |
---|
2598 | } |
---|
2599 | } |
---|
2600 | |
---|
2601 | if ('replace' == $params['multiple_value_mode']) |
---|
2602 | { |
---|
2603 | set_tags( |
---|
2604 | $tag_ids, |
---|
2605 | $params['image_id'] |
---|
2606 | ); |
---|
2607 | } |
---|
2608 | elseif ('append' == $params['multiple_value_mode']) |
---|
2609 | { |
---|
2610 | add_tags( |
---|
2611 | $tag_ids, |
---|
2612 | array($params['image_id']) |
---|
2613 | ); |
---|
2614 | } |
---|
2615 | else |
---|
2616 | { |
---|
2617 | return new PwgError( |
---|
2618 | 500, |
---|
2619 | '[ws_images_setInfo]' |
---|
2620 | .' invalid parameter multiple_value_mode "'.$params['multiple_value_mode'].'"' |
---|
2621 | .', possible values are {replace, append}.' |
---|
2622 | ); |
---|
2623 | } |
---|
2624 | } |
---|
2625 | |
---|
2626 | invalidate_user_cache(); |
---|
2627 | } |
---|
2628 | |
---|
2629 | function ws_images_delete($params, $service) |
---|
2630 | { |
---|
2631 | global $conf; |
---|
2632 | if (!is_admin()) |
---|
2633 | { |
---|
2634 | return new PwgError(401, 'Access denied'); |
---|
2635 | } |
---|
2636 | |
---|
2637 | if (!$service->isPost()) |
---|
2638 | { |
---|
2639 | return new PwgError(405, "This method requires HTTP POST"); |
---|
2640 | } |
---|
2641 | |
---|
2642 | if (empty($params['pwg_token']) or get_pwg_token() != $params['pwg_token']) |
---|
2643 | { |
---|
2644 | return new PwgError(403, 'Invalid security token'); |
---|
2645 | } |
---|
2646 | |
---|
2647 | $params['image_id'] = preg_split( |
---|
2648 | '/[\s,;\|]/', |
---|
2649 | $params['image_id'], |
---|
2650 | -1, |
---|
2651 | PREG_SPLIT_NO_EMPTY |
---|
2652 | ); |
---|
2653 | $params['image_id'] = array_map('intval', $params['image_id']); |
---|
2654 | |
---|
2655 | $image_ids = array(); |
---|
2656 | foreach ($params['image_id'] as $image_id) |
---|
2657 | { |
---|
2658 | if ($image_id > 0) |
---|
2659 | { |
---|
2660 | $image_ids[] = $image_id; |
---|
2661 | } |
---|
2662 | } |
---|
2663 | |
---|
2664 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
---|
2665 | delete_elements($image_ids, true); |
---|
2666 | invalidate_user_cache(); |
---|
2667 | } |
---|
2668 | |
---|
2669 | function ws_add_image_category_relations($image_id, $categories_string, $replace_mode=false) |
---|
2670 | { |
---|
2671 | // let's add links between the image and the categories |
---|
2672 | // |
---|
2673 | // $params['categories'] should look like 123,12;456,auto;789 which means: |
---|
2674 | // |
---|
2675 | // 1. associate with category 123 on rank 12 |
---|
2676 | // 2. associate with category 456 on automatic rank |
---|
2677 | // 3. associate with category 789 on automatic rank |
---|
2678 | $cat_ids = array(); |
---|
2679 | $rank_on_category = array(); |
---|
2680 | $search_current_ranks = false; |
---|
2681 | |
---|
2682 | $tokens = explode(';', $categories_string); |
---|
2683 | foreach ($tokens as $token) |
---|
2684 | { |
---|
2685 | @list($cat_id, $rank) = explode(',', $token); |
---|
2686 | |
---|
2687 | if (!preg_match('/^\d+$/', $cat_id)) |
---|
2688 | { |
---|
2689 | continue; |
---|
2690 | } |
---|
2691 | |
---|
2692 | $cat_ids[] = $cat_id; |
---|
2693 | |
---|
2694 | if (!isset($rank)) |
---|
2695 | { |
---|
2696 | $rank = 'auto'; |
---|
2697 | } |
---|
2698 | $rank_on_category[$cat_id] = $rank; |
---|
2699 | |
---|
2700 | if ($rank == 'auto') |
---|
2701 | { |
---|
2702 | $search_current_ranks = true; |
---|
2703 | } |
---|
2704 | } |
---|
2705 | |
---|
2706 | $cat_ids = array_unique($cat_ids); |
---|
2707 | |
---|
2708 | if (count($cat_ids) == 0) |
---|
2709 | { |
---|
2710 | return new PwgError( |
---|
2711 | 500, |
---|
2712 | '[ws_add_image_category_relations] there is no category defined in "'.$categories_string.'"' |
---|
2713 | ); |
---|
2714 | } |
---|
2715 | |
---|
2716 | $query = ' |
---|
2717 | SELECT |
---|
2718 | id |
---|
2719 | FROM '.CATEGORIES_TABLE.' |
---|
2720 | WHERE id IN ('.implode(',', $cat_ids).') |
---|
2721 | ;'; |
---|
2722 | $db_cat_ids = array_from_query($query, 'id'); |
---|
2723 | |
---|
2724 | $unknown_cat_ids = array_diff($cat_ids, $db_cat_ids); |
---|
2725 | if (count($unknown_cat_ids) != 0) |
---|
2726 | { |
---|
2727 | return new PwgError( |
---|
2728 | 500, |
---|
2729 | '[ws_add_image_category_relations] the following categories are unknown: '.implode(', ', $unknown_cat_ids) |
---|
2730 | ); |
---|
2731 | } |
---|
2732 | |
---|
2733 | $to_update_cat_ids = array(); |
---|
2734 | |
---|
2735 | // in case of replace mode, we first check the existing associations |
---|
2736 | $query = ' |
---|
2737 | SELECT |
---|
2738 | category_id |
---|
2739 | FROM '.IMAGE_CATEGORY_TABLE.' |
---|
2740 | WHERE image_id = '.$image_id.' |
---|
2741 | ;'; |
---|
2742 | $existing_cat_ids = array_from_query($query, 'category_id'); |
---|
2743 | |
---|
2744 | if ($replace_mode) |
---|
2745 | { |
---|
2746 | $to_remove_cat_ids = array_diff($existing_cat_ids, $cat_ids); |
---|
2747 | if (count($to_remove_cat_ids) > 0) |
---|
2748 | { |
---|
2749 | $query = ' |
---|
2750 | DELETE |
---|
2751 | FROM '.IMAGE_CATEGORY_TABLE.' |
---|
2752 | WHERE image_id = '.$image_id.' |
---|
2753 | AND category_id IN ('.implode(', ', $to_remove_cat_ids).') |
---|
2754 | ;'; |
---|
2755 | pwg_query($query); |
---|
2756 | update_category($to_remove_cat_ids); |
---|
2757 | } |
---|
2758 | } |
---|
2759 | |
---|
2760 | $new_cat_ids = array_diff($cat_ids, $existing_cat_ids); |
---|
2761 | if (count($new_cat_ids) == 0) |
---|
2762 | { |
---|
2763 | return true; |
---|
2764 | } |
---|
2765 | |
---|
2766 | if ($search_current_ranks) |
---|
2767 | { |
---|
2768 | $query = ' |
---|
2769 | SELECT |
---|
2770 | category_id, |
---|
2771 | MAX(rank) AS max_rank |
---|
2772 | FROM '.IMAGE_CATEGORY_TABLE.' |
---|
2773 | WHERE rank IS NOT NULL |
---|
2774 | AND category_id IN ('.implode(',', $new_cat_ids).') |
---|
2775 | GROUP BY category_id |
---|
2776 | ;'; |
---|
2777 | $current_rank_of = simple_hash_from_query( |
---|
2778 | $query, |
---|
2779 | 'category_id', |
---|
2780 | 'max_rank' |
---|
2781 | ); |
---|
2782 | |
---|
2783 | foreach ($new_cat_ids as $cat_id) |
---|
2784 | { |
---|
2785 | if (!isset($current_rank_of[$cat_id])) |
---|
2786 | { |
---|
2787 | $current_rank_of[$cat_id] = 0; |
---|
2788 | } |
---|
2789 | |
---|
2790 | if ('auto' == $rank_on_category[$cat_id]) |
---|
2791 | { |
---|
2792 | $rank_on_category[$cat_id] = $current_rank_of[$cat_id] + 1; |
---|
2793 | } |
---|
2794 | } |
---|
2795 | } |
---|
2796 | |
---|
2797 | $inserts = array(); |
---|
2798 | |
---|
2799 | foreach ($new_cat_ids as $cat_id) |
---|
2800 | { |
---|
2801 | $inserts[] = array( |
---|
2802 | 'image_id' => $image_id, |
---|
2803 | 'category_id' => $cat_id, |
---|
2804 | 'rank' => $rank_on_category[$cat_id], |
---|
2805 | ); |
---|
2806 | } |
---|
2807 | |
---|
2808 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
---|
2809 | mass_inserts( |
---|
2810 | IMAGE_CATEGORY_TABLE, |
---|
2811 | array_keys($inserts[0]), |
---|
2812 | $inserts |
---|
2813 | ); |
---|
2814 | |
---|
2815 | update_category($new_cat_ids); |
---|
2816 | } |
---|
2817 | |
---|
2818 | function ws_categories_setInfo($params, $service) |
---|
2819 | { |
---|
2820 | global $conf; |
---|
2821 | if (!is_admin()) |
---|
2822 | { |
---|
2823 | return new PwgError(401, 'Access denied'); |
---|
2824 | } |
---|
2825 | |
---|
2826 | if (!$service->isPost()) |
---|
2827 | { |
---|
2828 | return new PwgError(405, "This method requires HTTP POST"); |
---|
2829 | } |
---|
2830 | |
---|
2831 | // category_id |
---|
2832 | // name |
---|
2833 | // comment |
---|
2834 | |
---|
2835 | $params['category_id'] = (int)$params['category_id']; |
---|
2836 | if ($params['category_id'] <= 0) |
---|
2837 | { |
---|
2838 | return new PwgError(WS_ERR_INVALID_PARAM, "Invalid category_id"); |
---|
2839 | } |
---|
2840 | |
---|
2841 | // database registration |
---|
2842 | $update = array( |
---|
2843 | 'id' => $params['category_id'], |
---|
2844 | ); |
---|
2845 | |
---|
2846 | $info_columns = array( |
---|
2847 | 'name', |
---|
2848 | 'comment', |
---|
2849 | ); |
---|
2850 | |
---|
2851 | $perform_update = false; |
---|
2852 | foreach ($info_columns as $key) |
---|
2853 | { |
---|
2854 | if (isset($params[$key])) |
---|
2855 | { |
---|
2856 | $perform_update = true; |
---|
2857 | $update[$key] = $params[$key]; |
---|
2858 | } |
---|
2859 | } |
---|
2860 | |
---|
2861 | if ($perform_update) |
---|
2862 | { |
---|
2863 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
---|
2864 | mass_updates( |
---|
2865 | CATEGORIES_TABLE, |
---|
2866 | array( |
---|
2867 | 'primary' => array('id'), |
---|
2868 | 'update' => array_diff(array_keys($update), array('id')) |
---|
2869 | ), |
---|
2870 | array($update) |
---|
2871 | ); |
---|
2872 | } |
---|
2873 | |
---|
2874 | } |
---|
2875 | |
---|
2876 | function ws_categories_setRepresentative($params, $service) |
---|
2877 | { |
---|
2878 | global $conf; |
---|
2879 | |
---|
2880 | if (!is_admin()) |
---|
2881 | { |
---|
2882 | return new PwgError(401, 'Access denied'); |
---|
2883 | } |
---|
2884 | |
---|
2885 | if (!$service->isPost()) |
---|
2886 | { |
---|
2887 | return new PwgError(405, "This method requires HTTP POST"); |
---|
2888 | } |
---|
2889 | |
---|
2890 | // category_id |
---|
2891 | // image_id |
---|
2892 | |
---|
2893 | $params['category_id'] = (int)$params['category_id']; |
---|
2894 | if ($params['category_id'] <= 0) |
---|
2895 | { |
---|
2896 | return new PwgError(WS_ERR_INVALID_PARAM, "Invalid category_id"); |
---|
2897 | } |
---|
2898 | |
---|
2899 | // does the category really exist? |
---|
2900 | $query=' |
---|
2901 | SELECT |
---|
2902 | * |
---|
2903 | FROM '.CATEGORIES_TABLE.' |
---|
2904 | WHERE id = '.$params['category_id'].' |
---|
2905 | ;'; |
---|
2906 | $row = pwg_db_fetch_assoc(pwg_query($query)); |
---|
2907 | if ($row == null) |
---|
2908 | { |
---|
2909 | return new PwgError(404, "category_id not found"); |
---|
2910 | } |
---|
2911 | |
---|
2912 | $params['image_id'] = (int)$params['image_id']; |
---|
2913 | if ($params['image_id'] <= 0) |
---|
2914 | { |
---|
2915 | return new PwgError(WS_ERR_INVALID_PARAM, "Invalid image_id"); |
---|
2916 | } |
---|
2917 | |
---|
2918 | // does the image really exist? |
---|
2919 | $query=' |
---|
2920 | SELECT |
---|
2921 | * |
---|
2922 | FROM '.IMAGES_TABLE.' |
---|
2923 | WHERE id = '.$params['image_id'].' |
---|
2924 | ;'; |
---|
2925 | |
---|
2926 | $row = pwg_db_fetch_assoc(pwg_query($query)); |
---|
2927 | if ($row == null) |
---|
2928 | { |
---|
2929 | return new PwgError(404, "image_id not found"); |
---|
2930 | } |
---|
2931 | |
---|
2932 | // apply change |
---|
2933 | $query = ' |
---|
2934 | UPDATE '.CATEGORIES_TABLE.' |
---|
2935 | SET representative_picture_id = '.$params['image_id'].' |
---|
2936 | WHERE id = '.$params['category_id'].' |
---|
2937 | ;'; |
---|
2938 | pwg_query($query); |
---|
2939 | |
---|
2940 | $query = ' |
---|
2941 | UPDATE '.USER_CACHE_CATEGORIES_TABLE.' |
---|
2942 | SET user_representative_picture_id = NULL |
---|
2943 | WHERE cat_id = '.$params['category_id'].' |
---|
2944 | ;'; |
---|
2945 | pwg_query($query); |
---|
2946 | } |
---|
2947 | |
---|
2948 | function ws_categories_delete($params, $service) |
---|
2949 | { |
---|
2950 | global $conf; |
---|
2951 | if (!is_admin()) |
---|
2952 | { |
---|
2953 | return new PwgError(401, 'Access denied'); |
---|
2954 | } |
---|
2955 | |
---|
2956 | if (!$service->isPost()) |
---|
2957 | { |
---|
2958 | return new PwgError(405, "This method requires HTTP POST"); |
---|
2959 | } |
---|
2960 | |
---|
2961 | if (empty($params['pwg_token']) or get_pwg_token() != $params['pwg_token']) |
---|
2962 | { |
---|
2963 | return new PwgError(403, 'Invalid security token'); |
---|
2964 | } |
---|
2965 | |
---|
2966 | $modes = array('no_delete', 'delete_orphans', 'force_delete'); |
---|
2967 | if (!in_array($params['photo_deletion_mode'], $modes)) |
---|
2968 | { |
---|
2969 | return new PwgError( |
---|
2970 | 500, |
---|
2971 | '[ws_categories_delete]' |
---|
2972 | .' invalid parameter photo_deletion_mode "'.$params['photo_deletion_mode'].'"' |
---|
2973 | .', possible values are {'.implode(', ', $modes).'}.' |
---|
2974 | ); |
---|
2975 | } |
---|
2976 | |
---|
2977 | $params['category_id'] = preg_split( |
---|
2978 | '/[\s,;\|]/', |
---|
2979 | $params['category_id'], |
---|
2980 | -1, |
---|
2981 | PREG_SPLIT_NO_EMPTY |
---|
2982 | ); |
---|
2983 | $params['category_id'] = array_map('intval', $params['category_id']); |
---|
2984 | |
---|
2985 | $category_ids = array(); |
---|
2986 | foreach ($params['category_id'] as $category_id) |
---|
2987 | { |
---|
2988 | if ($category_id > 0) |
---|
2989 | { |
---|
2990 | $category_ids[] = $category_id; |
---|
2991 | } |
---|
2992 | } |
---|
2993 | |
---|
2994 | if (count($category_ids) == 0) |
---|
2995 | { |
---|
2996 | return; |
---|
2997 | } |
---|
2998 | |
---|
2999 | $query = ' |
---|
3000 | SELECT id |
---|
3001 | FROM '.CATEGORIES_TABLE.' |
---|
3002 | WHERE id IN ('.implode(',', $category_ids).') |
---|
3003 | ;'; |
---|
3004 | $category_ids = array_from_query($query, 'id'); |
---|
3005 | |
---|
3006 | if (count($category_ids) == 0) |
---|
3007 | { |
---|
3008 | return; |
---|
3009 | } |
---|
3010 | |
---|
3011 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
---|
3012 | delete_categories($category_ids, $params['photo_deletion_mode']); |
---|
3013 | update_global_rank(); |
---|
3014 | } |
---|
3015 | |
---|
3016 | function ws_categories_move($params, $service) |
---|
3017 | { |
---|
3018 | global $conf, $page; |
---|
3019 | |
---|
3020 | if (!is_admin()) |
---|
3021 | { |
---|
3022 | return new PwgError(401, 'Access denied'); |
---|
3023 | } |
---|
3024 | |
---|
3025 | if (!$service->isPost()) |
---|
3026 | { |
---|
3027 | return new PwgError(405, "This method requires HTTP POST"); |
---|
3028 | } |
---|
3029 | |
---|
3030 | if (empty($params['pwg_token']) or get_pwg_token() != $params['pwg_token']) |
---|
3031 | { |
---|
3032 | return new PwgError(403, 'Invalid security token'); |
---|
3033 | } |
---|
3034 | |
---|
3035 | $params['category_id'] = preg_split( |
---|
3036 | '/[\s,;\|]/', |
---|
3037 | $params['category_id'], |
---|
3038 | -1, |
---|
3039 | PREG_SPLIT_NO_EMPTY |
---|
3040 | ); |
---|
3041 | $params['category_id'] = array_map('intval', $params['category_id']); |
---|
3042 | |
---|
3043 | $category_ids = array(); |
---|
3044 | foreach ($params['category_id'] as $category_id) |
---|
3045 | { |
---|
3046 | if ($category_id > 0) |
---|
3047 | { |
---|
3048 | $category_ids[] = $category_id; |
---|
3049 | } |
---|
3050 | } |
---|
3051 | |
---|
3052 | if (count($category_ids) == 0) |
---|
3053 | { |
---|
3054 | return new PwgError(403, 'Invalid category_id input parameter, no category to move'); |
---|
3055 | } |
---|
3056 | |
---|
3057 | // we can't move physical categories |
---|
3058 | $categories_in_db = array(); |
---|
3059 | |
---|
3060 | $query = ' |
---|
3061 | SELECT |
---|
3062 | id, |
---|
3063 | name, |
---|
3064 | dir |
---|
3065 | FROM '.CATEGORIES_TABLE.' |
---|
3066 | WHERE id IN ('.implode(',', $category_ids).') |
---|
3067 | ;'; |
---|
3068 | $result = pwg_query($query); |
---|
3069 | while ($row = pwg_db_fetch_assoc($result)) |
---|
3070 | { |
---|
3071 | $categories_in_db[$row['id']] = $row; |
---|
3072 | // we break on error at first physical category detected |
---|
3073 | if (!empty($row['dir'])) |
---|
3074 | { |
---|
3075 | $row['name'] = strip_tags( |
---|
3076 | trigger_event( |
---|
3077 | 'render_category_name', |
---|
3078 | $row['name'], |
---|
3079 | 'ws_categories_move' |
---|
3080 | ) |
---|
3081 | ); |
---|
3082 | |
---|
3083 | return new PwgError( |
---|
3084 | 403, |
---|
3085 | sprintf( |
---|
3086 | 'Category %s (%u) is not a virtual category, you cannot move it', |
---|
3087 | $row['name'], |
---|
3088 | $row['id'] |
---|
3089 | ) |
---|
3090 | ); |
---|
3091 | } |
---|
3092 | } |
---|
3093 | |
---|
3094 | if (count($categories_in_db) != count($category_ids)) |
---|
3095 | { |
---|
3096 | $unknown_category_ids = array_diff($category_ids, array_keys($categories_in_db)); |
---|
3097 | |
---|
3098 | return new PwgError( |
---|
3099 | 403, |
---|
3100 | sprintf( |
---|
3101 | 'Category %u does not exist', |
---|
3102 | $unknown_category_ids[0] |
---|
3103 | ) |
---|
3104 | ); |
---|
3105 | } |
---|
3106 | |
---|
3107 | // does this parent exists? This check should be made in the |
---|
3108 | // move_categories function, not here |
---|
3109 | // |
---|
3110 | // 0 as parent means "move categories at gallery root" |
---|
3111 | if (!is_numeric($params['parent'])) |
---|
3112 | { |
---|
3113 | return new PwgError(403, 'Invalid parent input parameter'); |
---|
3114 | } |
---|
3115 | |
---|
3116 | if (0 != $params['parent']) { |
---|
3117 | $params['parent'] = intval($params['parent']); |
---|
3118 | $subcat_ids = get_subcat_ids(array($params['parent'])); |
---|
3119 | if (count($subcat_ids) == 0) |
---|
3120 | { |
---|
3121 | return new PwgError(403, 'Unknown parent category id'); |
---|
3122 | } |
---|
3123 | } |
---|
3124 | |
---|
3125 | $page['infos'] = array(); |
---|
3126 | $page['errors'] = array(); |
---|
3127 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
---|
3128 | move_categories($category_ids, $params['parent']); |
---|
3129 | invalidate_user_cache(); |
---|
3130 | |
---|
3131 | if (count($page['errors']) != 0) |
---|
3132 | { |
---|
3133 | return new PwgError(403, implode('; ', $page['errors'])); |
---|
3134 | } |
---|
3135 | } |
---|
3136 | |
---|
3137 | function ws_logfile($string) |
---|
3138 | { |
---|
3139 | global $conf; |
---|
3140 | |
---|
3141 | if (!$conf['ws_enable_log']) { |
---|
3142 | return true; |
---|
3143 | } |
---|
3144 | |
---|
3145 | file_put_contents( |
---|
3146 | $conf['ws_log_filepath'], |
---|
3147 | '['.date('c').'] '.$string."\n", |
---|
3148 | FILE_APPEND |
---|
3149 | ); |
---|
3150 | } |
---|
3151 | |
---|
3152 | function ws_images_checkUpload($params, $service) |
---|
3153 | { |
---|
3154 | global $conf; |
---|
3155 | |
---|
3156 | if (!is_admin()) |
---|
3157 | { |
---|
3158 | return new PwgError(401, 'Access denied'); |
---|
3159 | } |
---|
3160 | |
---|
3161 | include_once(PHPWG_ROOT_PATH.'admin/include/functions_upload.inc.php'); |
---|
3162 | $ret['message'] = ready_for_upload_message(); |
---|
3163 | $ret['ready_for_upload'] = true; |
---|
3164 | |
---|
3165 | if (!empty($ret['message'])) |
---|
3166 | { |
---|
3167 | $ret['ready_for_upload'] = false; |
---|
3168 | } |
---|
3169 | |
---|
3170 | return $ret; |
---|
3171 | } |
---|
3172 | |
---|
3173 | function ws_plugins_getList($params, $service) |
---|
3174 | { |
---|
3175 | global $conf; |
---|
3176 | |
---|
3177 | if (!is_admin()) |
---|
3178 | { |
---|
3179 | return new PwgError(401, 'Access denied'); |
---|
3180 | } |
---|
3181 | |
---|
3182 | include_once(PHPWG_ROOT_PATH.'admin/include/plugins.class.php'); |
---|
3183 | $plugins = new plugins(); |
---|
3184 | $plugins->sort_fs_plugins('name'); |
---|
3185 | $plugin_list = array(); |
---|
3186 | |
---|
3187 | foreach($plugins->fs_plugins as $plugin_id => $fs_plugin) |
---|
3188 | { |
---|
3189 | if (isset($plugins->db_plugins_by_id[$plugin_id])) |
---|
3190 | { |
---|
3191 | $state = $plugins->db_plugins_by_id[$plugin_id]['state']; |
---|
3192 | } |
---|
3193 | else |
---|
3194 | { |
---|
3195 | $state = 'uninstalled'; |
---|
3196 | } |
---|
3197 | |
---|
3198 | $plugin_list[] = |
---|
3199 | array( |
---|
3200 | 'id' => $plugin_id, |
---|
3201 | 'name' => $fs_plugin['name'], |
---|
3202 | 'version' => $fs_plugin['version'], |
---|
3203 | 'state' => $state, |
---|
3204 | 'description' => $fs_plugin['description'], |
---|
3205 | ); |
---|
3206 | } |
---|
3207 | |
---|
3208 | return $plugin_list; |
---|
3209 | } |
---|
3210 | |
---|
3211 | function ws_plugins_performAction($params, &$service) |
---|
3212 | { |
---|
3213 | global $template; |
---|
3214 | |
---|
3215 | if (!is_admin()) |
---|
3216 | { |
---|
3217 | return new PwgError(401, 'Access denied'); |
---|
3218 | } |
---|
3219 | |
---|
3220 | if (empty($params['pwg_token']) or get_pwg_token() != $params['pwg_token']) |
---|
3221 | { |
---|
3222 | return new PwgError(403, 'Invalid security token'); |
---|
3223 | } |
---|
3224 | |
---|
3225 | define('IN_ADMIN', true); |
---|
3226 | include_once(PHPWG_ROOT_PATH.'admin/include/plugins.class.php'); |
---|
3227 | $plugins = new plugins(); |
---|
3228 | $errors = $plugins->perform_action($params['action'], $params['plugin']); |
---|
3229 | |
---|
3230 | |
---|
3231 | if (!empty($errors)) |
---|
3232 | { |
---|
3233 | return new PwgError(500, $errors); |
---|
3234 | } |
---|
3235 | else |
---|
3236 | { |
---|
3237 | if (in_array($params['action'], array('activate', 'deactivate'))) |
---|
3238 | { |
---|
3239 | $template->delete_compiled_templates(); |
---|
3240 | } |
---|
3241 | return true; |
---|
3242 | } |
---|
3243 | } |
---|
3244 | |
---|
3245 | function ws_themes_performAction($params, $service) |
---|
3246 | { |
---|
3247 | global $template; |
---|
3248 | |
---|
3249 | if (!is_admin()) |
---|
3250 | { |
---|
3251 | return new PwgError(401, 'Access denied'); |
---|
3252 | } |
---|
3253 | |
---|
3254 | if (empty($params['pwg_token']) or get_pwg_token() != $params['pwg_token']) |
---|
3255 | { |
---|
3256 | return new PwgError(403, 'Invalid security token'); |
---|
3257 | } |
---|
3258 | |
---|
3259 | define('IN_ADMIN', true); |
---|
3260 | include_once(PHPWG_ROOT_PATH.'admin/include/themes.class.php'); |
---|
3261 | $themes = new themes(); |
---|
3262 | $errors = $themes->perform_action($params['action'], $params['theme']); |
---|
3263 | |
---|
3264 | if (!empty($errors)) |
---|
3265 | { |
---|
3266 | return new PwgError(500, $errors); |
---|
3267 | } |
---|
3268 | else |
---|
3269 | { |
---|
3270 | if (in_array($params['action'], array('activate', 'deactivate'))) |
---|
3271 | { |
---|
3272 | $template->delete_compiled_templates(); |
---|
3273 | } |
---|
3274 | return true; |
---|
3275 | } |
---|
3276 | } |
---|
3277 | |
---|
3278 | function ws_extensions_update($params, $service) |
---|
3279 | { |
---|
3280 | if (!is_webmaster()) |
---|
3281 | { |
---|
3282 | return new PwgError(401, l10n('Webmaster status is required.')); |
---|
3283 | } |
---|
3284 | |
---|
3285 | if (empty($params['pwg_token']) or get_pwg_token() != $params['pwg_token']) |
---|
3286 | { |
---|
3287 | return new PwgError(403, 'Invalid security token'); |
---|
3288 | } |
---|
3289 | |
---|
3290 | if (empty($params['type']) or !in_array($params['type'], array('plugins', 'themes', 'languages'))) |
---|
3291 | { |
---|
3292 | return new PwgError(403, "invalid extension type"); |
---|
3293 | } |
---|
3294 | |
---|
3295 | if (empty($params['id']) or empty($params['revision'])) |
---|
3296 | { |
---|
3297 | return new PwgError(null, 'Wrong parameters'); |
---|
3298 | } |
---|
3299 | |
---|
3300 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
---|
3301 | include_once(PHPWG_ROOT_PATH.'admin/include/'.$params['type'].'.class.php'); |
---|
3302 | |
---|
3303 | $type = $params['type']; |
---|
3304 | $extension_id = $params['id']; |
---|
3305 | $revision = $params['revision']; |
---|
3306 | |
---|
3307 | $extension = new $type(); |
---|
3308 | |
---|
3309 | if ($type == 'plugins') |
---|
3310 | { |
---|
3311 | if (isset($extension->db_plugins_by_id[$extension_id]) and $extension->db_plugins_by_id[$extension_id]['state'] == 'active') |
---|
3312 | { |
---|
3313 | $extension->perform_action('deactivate', $extension_id); |
---|
3314 | |
---|
3315 | redirect(PHPWG_ROOT_PATH |
---|
3316 | . 'ws.php' |
---|
3317 | . '?method=pwg.extensions.update' |
---|
3318 | . '&type=plugins' |
---|
3319 | . '&id=' . $extension_id |
---|
3320 | . '&revision=' . $revision |
---|
3321 | . '&reactivate=true' |
---|
3322 | . '&pwg_token=' . get_pwg_token() |
---|
3323 | . '&format=json' |
---|
3324 | ); |
---|
3325 | } |
---|
3326 | |
---|
3327 | $upgrade_status = $extension->extract_plugin_files('upgrade', $revision, $extension_id); |
---|
3328 | $extension_name = $extension->fs_plugins[$extension_id]['name']; |
---|
3329 | |
---|
3330 | if (isset($params['reactivate'])) |
---|
3331 | { |
---|
3332 | $extension->perform_action('activate', $extension_id); |
---|
3333 | } |
---|
3334 | } |
---|
3335 | elseif ($type == 'themes') |
---|
3336 | { |
---|
3337 | $upgrade_status = $extension->extract_theme_files('upgrade', $revision, $extension_id); |
---|
3338 | $extension_name = $extension->fs_themes[$extension_id]['name']; |
---|
3339 | } |
---|
3340 | elseif ($type == 'languages') |
---|
3341 | { |
---|
3342 | $upgrade_status = $extension->extract_language_files('upgrade', $revision, $extension_id); |
---|
3343 | $extension_name = $extension->fs_languages[$extension_id]['name']; |
---|
3344 | } |
---|
3345 | |
---|
3346 | global $template; |
---|
3347 | $template->delete_compiled_templates(); |
---|
3348 | |
---|
3349 | switch ($upgrade_status) |
---|
3350 | { |
---|
3351 | case 'ok': |
---|
3352 | return l10n('%s has been successfully updated.', $extension_name); |
---|
3353 | |
---|
3354 | case 'temp_path_error': |
---|
3355 | return new PwgError(null, l10n('Can\'t create temporary file.')); |
---|
3356 | |
---|
3357 | case 'dl_archive_error': |
---|
3358 | return new PwgError(null, l10n('Can\'t download archive.')); |
---|
3359 | |
---|
3360 | case 'archive_error': |
---|
3361 | return new PwgError(null, l10n('Can\'t read or extract archive.')); |
---|
3362 | |
---|
3363 | default: |
---|
3364 | return new PwgError(null, l10n('An error occured during extraction (%s).', $upgrade_status)); |
---|
3365 | } |
---|
3366 | } |
---|
3367 | |
---|
3368 | function ws_extensions_ignoreupdate($params, $service) |
---|
3369 | { |
---|
3370 | global $conf; |
---|
3371 | |
---|
3372 | define('IN_ADMIN', true); |
---|
3373 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
---|
3374 | |
---|
3375 | if (!is_webmaster()) |
---|
3376 | { |
---|
3377 | return new PwgError(401, 'Access denied'); |
---|
3378 | } |
---|
3379 | |
---|
3380 | if (empty($params['pwg_token']) or get_pwg_token() != $params['pwg_token']) |
---|
3381 | { |
---|
3382 | return new PwgError(403, 'Invalid security token'); |
---|
3383 | } |
---|
3384 | |
---|
3385 | $conf['updates_ignored'] = unserialize($conf['updates_ignored']); |
---|
3386 | |
---|
3387 | // Reset ignored extension |
---|
3388 | if ($params['reset']) |
---|
3389 | { |
---|
3390 | if (!empty($params['type']) and isset($conf['updates_ignored'][$params['type']])) |
---|
3391 | { |
---|
3392 | $conf['updates_ignored'][$params['type']] = array(); |
---|
3393 | } |
---|
3394 | else |
---|
3395 | { |
---|
3396 | $conf['updates_ignored'] = array( |
---|
3397 | 'plugins'=>array(), |
---|
3398 | 'themes'=>array(), |
---|
3399 | 'languages'=>array() |
---|
3400 | ); |
---|
3401 | } |
---|
3402 | conf_update_param('updates_ignored', pwg_db_real_escape_string(serialize($conf['updates_ignored']))); |
---|
3403 | unset($_SESSION['extensions_need_update']); |
---|
3404 | return true; |
---|
3405 | } |
---|
3406 | |
---|
3407 | if (empty($params['id']) or empty($params['type']) or !in_array($params['type'], array('plugins', 'themes', 'languages'))) |
---|
3408 | { |
---|
3409 | return new PwgError(403, 'Invalid parameters'); |
---|
3410 | } |
---|
3411 | |
---|
3412 | // Add or remove extension from ignore list |
---|
3413 | if (!in_array($params['id'], $conf['updates_ignored'][$params['type']])) |
---|
3414 | { |
---|
3415 | $conf['updates_ignored'][ $params['type'] ][] = $params['id']; |
---|
3416 | } |
---|
3417 | conf_update_param('updates_ignored', pwg_db_real_escape_string(serialize($conf['updates_ignored']))); |
---|
3418 | unset($_SESSION['extensions_need_update']); |
---|
3419 | return true; |
---|
3420 | } |
---|
3421 | |
---|
3422 | function ws_extensions_checkupdates($params, $service) |
---|
3423 | { |
---|
3424 | global $conf; |
---|
3425 | |
---|
3426 | define('IN_ADMIN', true); |
---|
3427 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
---|
3428 | include_once(PHPWG_ROOT_PATH.'admin/include/updates.class.php'); |
---|
3429 | $update = new updates(); |
---|
3430 | |
---|
3431 | if (!is_admin()) |
---|
3432 | { |
---|
3433 | return new PwgError(401, 'Access denied'); |
---|
3434 | } |
---|
3435 | |
---|
3436 | $result = array(); |
---|
3437 | |
---|
3438 | if (!isset($_SESSION['need_update'])) |
---|
3439 | $update->check_piwigo_upgrade(); |
---|
3440 | |
---|
3441 | $result['piwigo_need_update'] = $_SESSION['need_update']; |
---|
3442 | |
---|
3443 | $conf['updates_ignored'] = unserialize($conf['updates_ignored']); |
---|
3444 | |
---|
3445 | if (!isset($_SESSION['extensions_need_update'])) |
---|
3446 | $update->check_extensions(); |
---|
3447 | else |
---|
3448 | $update->check_updated_extensions(); |
---|
3449 | |
---|
3450 | if (!is_array($_SESSION['extensions_need_update'])) |
---|
3451 | $result['ext_need_update'] = null; |
---|
3452 | else |
---|
3453 | $result['ext_need_update'] = !empty($_SESSION['extensions_need_update']); |
---|
3454 | |
---|
3455 | return $result; |
---|
3456 | } |
---|
3457 | ?> |
---|