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