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.'average_rate>'.$params['f_min_rate']; |
---|
58 | } |
---|
59 | if ( is_numeric($params['f_max_rate']) ) |
---|
60 | { |
---|
61 | $clauses[] = $tbl_name.'average_rate<='.$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', 'average_rate', |
---|
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 | date_last, max_date_last, count_categories AS nb_categories |
---|
494 | FROM '.CATEGORIES_TABLE.' |
---|
495 | '.$join_type.' JOIN '.USER_CACHE_CATEGORIES_TABLE.' ON id=cat_id AND user_id='.$join_user.' |
---|
496 | WHERE '. implode(' |
---|
497 | AND ', $where); |
---|
498 | |
---|
499 | $result = pwg_query($query); |
---|
500 | |
---|
501 | $cats = array(); |
---|
502 | while ($row = pwg_db_fetch_assoc($result)) |
---|
503 | { |
---|
504 | $row['url'] = make_index_url( |
---|
505 | array( |
---|
506 | 'category' => $row |
---|
507 | ) |
---|
508 | ); |
---|
509 | foreach( array('id','nb_images','total_nb_images','nb_categories') as $key) |
---|
510 | { |
---|
511 | $row[$key] = (int)$row[$key]; |
---|
512 | } |
---|
513 | |
---|
514 | $row['name'] = strip_tags( |
---|
515 | trigger_event( |
---|
516 | 'render_category_name', |
---|
517 | $row['name'], |
---|
518 | 'ws_categories_getList' |
---|
519 | ) |
---|
520 | ); |
---|
521 | |
---|
522 | $row['comment'] = strip_tags( |
---|
523 | trigger_event( |
---|
524 | 'render_category_description', |
---|
525 | $row['comment'], |
---|
526 | 'ws_categories_getList' |
---|
527 | ) |
---|
528 | ); |
---|
529 | |
---|
530 | array_push($cats, $row); |
---|
531 | } |
---|
532 | usort($cats, 'global_rank_compare'); |
---|
533 | |
---|
534 | if ($params['tree_output']) |
---|
535 | { |
---|
536 | return categories_flatlist_to_tree($cats); |
---|
537 | } |
---|
538 | else |
---|
539 | { |
---|
540 | return array( |
---|
541 | 'categories' => new PwgNamedArray( |
---|
542 | $cats, |
---|
543 | 'category', |
---|
544 | array( |
---|
545 | 'id', |
---|
546 | 'url', |
---|
547 | 'nb_images', |
---|
548 | 'total_nb_images', |
---|
549 | 'nb_categories', |
---|
550 | 'date_last', |
---|
551 | 'max_date_last', |
---|
552 | ) |
---|
553 | ) |
---|
554 | ); |
---|
555 | } |
---|
556 | } |
---|
557 | |
---|
558 | /** |
---|
559 | * returns the list of categories as you can see them in administration (web |
---|
560 | * service method). |
---|
561 | * |
---|
562 | * Only admin can run this method and permissions are not taken into |
---|
563 | * account. |
---|
564 | */ |
---|
565 | function ws_categories_getAdminList($params, &$service) |
---|
566 | { |
---|
567 | if (!is_admin()) |
---|
568 | { |
---|
569 | return new PwgError(401, 'Access denied'); |
---|
570 | } |
---|
571 | |
---|
572 | $query = ' |
---|
573 | SELECT |
---|
574 | category_id, |
---|
575 | COUNT(*) AS counter |
---|
576 | FROM '.IMAGE_CATEGORY_TABLE.' |
---|
577 | GROUP BY category_id |
---|
578 | ;'; |
---|
579 | $nb_images_of = simple_hash_from_query($query, 'category_id', 'counter'); |
---|
580 | |
---|
581 | $query = ' |
---|
582 | SELECT |
---|
583 | id, |
---|
584 | name, |
---|
585 | comment, |
---|
586 | uppercats, |
---|
587 | global_rank |
---|
588 | FROM '.CATEGORIES_TABLE.' |
---|
589 | ;'; |
---|
590 | $result = pwg_query($query); |
---|
591 | $cats = array(); |
---|
592 | |
---|
593 | while ($row = pwg_db_fetch_assoc($result)) |
---|
594 | { |
---|
595 | $id = $row['id']; |
---|
596 | $row['nb_images'] = isset($nb_images_of[$id]) ? $nb_images_of[$id] : 0; |
---|
597 | $row['name'] = strip_tags( |
---|
598 | trigger_event( |
---|
599 | 'render_category_name', |
---|
600 | $row['name'], |
---|
601 | 'ws_categories_getAdminList' |
---|
602 | ) |
---|
603 | ); |
---|
604 | $row['comment'] = strip_tags( |
---|
605 | trigger_event( |
---|
606 | 'render_category_description', |
---|
607 | $row['comment'], |
---|
608 | 'ws_categories_getAdminList' |
---|
609 | ) |
---|
610 | ); |
---|
611 | array_push($cats, $row); |
---|
612 | } |
---|
613 | |
---|
614 | usort($cats, 'global_rank_compare'); |
---|
615 | return array( |
---|
616 | 'categories' => new PwgNamedArray( |
---|
617 | $cats, |
---|
618 | 'category', |
---|
619 | array( |
---|
620 | 'id', |
---|
621 | 'nb_images', |
---|
622 | 'name', |
---|
623 | 'uppercats', |
---|
624 | 'global_rank', |
---|
625 | ) |
---|
626 | ) |
---|
627 | ); |
---|
628 | } |
---|
629 | |
---|
630 | /** |
---|
631 | * returns detailed information for an element (web service method) |
---|
632 | */ |
---|
633 | function ws_images_addComment($params, &$service) |
---|
634 | { |
---|
635 | if (!$service->isPost()) |
---|
636 | { |
---|
637 | return new PwgError(405, "This method requires HTTP POST"); |
---|
638 | } |
---|
639 | $params['image_id'] = (int)$params['image_id']; |
---|
640 | $query = ' |
---|
641 | SELECT DISTINCT image_id |
---|
642 | FROM '.IMAGE_CATEGORY_TABLE.' INNER JOIN '.CATEGORIES_TABLE.' ON category_id=id |
---|
643 | WHERE commentable="true" |
---|
644 | AND image_id='.$params['image_id']. |
---|
645 | get_sql_condition_FandF( |
---|
646 | array( |
---|
647 | 'forbidden_categories' => 'id', |
---|
648 | 'visible_categories' => 'id', |
---|
649 | 'visible_images' => 'image_id' |
---|
650 | ), |
---|
651 | ' AND' |
---|
652 | ); |
---|
653 | if ( !pwg_db_num_rows( pwg_query( $query ) ) ) |
---|
654 | { |
---|
655 | return new PwgError(WS_ERR_INVALID_PARAM, "Invalid image_id"); |
---|
656 | } |
---|
657 | |
---|
658 | $comm = array( |
---|
659 | 'author' => trim( $params['author'] ), |
---|
660 | 'content' => trim( $params['content'] ), |
---|
661 | 'image_id' => $params['image_id'], |
---|
662 | ); |
---|
663 | |
---|
664 | include_once(PHPWG_ROOT_PATH.'include/functions_comment.inc.php'); |
---|
665 | |
---|
666 | $comment_action = insert_user_comment( |
---|
667 | $comm, $params['key'], $infos |
---|
668 | ); |
---|
669 | |
---|
670 | switch ($comment_action) |
---|
671 | { |
---|
672 | case 'reject': |
---|
673 | array_push($infos, l10n('Your comment has NOT been registered because it did not pass the validation rules') ); |
---|
674 | return new PwgError(403, implode("; ", $infos) ); |
---|
675 | case 'validate': |
---|
676 | case 'moderate': |
---|
677 | $ret = array( |
---|
678 | 'id' => $comm['id'], |
---|
679 | 'validation' => $comment_action=='validate', |
---|
680 | ); |
---|
681 | return new PwgNamedStruct( |
---|
682 | 'comment', |
---|
683 | $ret, |
---|
684 | null, array() |
---|
685 | ); |
---|
686 | default: |
---|
687 | return new PwgError(500, "Unknown comment action ".$comment_action ); |
---|
688 | } |
---|
689 | } |
---|
690 | |
---|
691 | /** |
---|
692 | * returns detailed information for an element (web service method) |
---|
693 | */ |
---|
694 | function ws_images_getInfo($params, &$service) |
---|
695 | { |
---|
696 | @include_once(PHPWG_ROOT_PATH.'include/functions_picture.inc.php'); |
---|
697 | global $user, $conf; |
---|
698 | $params['image_id'] = (int)$params['image_id']; |
---|
699 | if ( $params['image_id']<=0 ) |
---|
700 | { |
---|
701 | return new PwgError(WS_ERR_INVALID_PARAM, "Invalid image_id"); |
---|
702 | } |
---|
703 | |
---|
704 | $query=' |
---|
705 | SELECT * FROM '.IMAGES_TABLE.' |
---|
706 | WHERE id='.$params['image_id']. |
---|
707 | get_sql_condition_FandF( |
---|
708 | array('visible_images' => 'id'), |
---|
709 | ' AND' |
---|
710 | ).' |
---|
711 | LIMIT 1'; |
---|
712 | |
---|
713 | $image_row = pwg_db_fetch_assoc(pwg_query($query)); |
---|
714 | if ($image_row==null) |
---|
715 | { |
---|
716 | return new PwgError(404, "image_id not found"); |
---|
717 | } |
---|
718 | $image_row = array_merge( $image_row, ws_std_get_urls($image_row) ); |
---|
719 | |
---|
720 | //-------------------------------------------------------- related categories |
---|
721 | $query = ' |
---|
722 | SELECT id, name, permalink, uppercats, global_rank, commentable |
---|
723 | FROM '.IMAGE_CATEGORY_TABLE.' |
---|
724 | INNER JOIN '.CATEGORIES_TABLE.' ON category_id = id |
---|
725 | WHERE image_id = '.$image_row['id']. |
---|
726 | get_sql_condition_FandF( |
---|
727 | array( 'forbidden_categories' => 'category_id' ), |
---|
728 | ' AND' |
---|
729 | ).' |
---|
730 | ;'; |
---|
731 | $result = pwg_query($query); |
---|
732 | $is_commentable = false; |
---|
733 | $related_categories = array(); |
---|
734 | while ($row = pwg_db_fetch_assoc($result)) |
---|
735 | { |
---|
736 | if ($row['commentable']=='true') |
---|
737 | { |
---|
738 | $is_commentable = true; |
---|
739 | } |
---|
740 | unset($row['commentable']); |
---|
741 | $row['url'] = make_index_url( |
---|
742 | array( |
---|
743 | 'category' => $row |
---|
744 | ) |
---|
745 | ); |
---|
746 | |
---|
747 | $row['page_url'] = make_picture_url( |
---|
748 | array( |
---|
749 | 'image_id' => $image_row['id'], |
---|
750 | 'image_file' => $image_row['file'], |
---|
751 | 'category' => $row |
---|
752 | ) |
---|
753 | ); |
---|
754 | $row['id']=(int)$row['id']; |
---|
755 | array_push($related_categories, $row); |
---|
756 | } |
---|
757 | usort($related_categories, 'global_rank_compare'); |
---|
758 | if ( empty($related_categories) ) |
---|
759 | { |
---|
760 | return new PwgError(401, 'Access denied'); |
---|
761 | } |
---|
762 | |
---|
763 | //-------------------------------------------------------------- related tags |
---|
764 | $related_tags = get_common_tags( array($image_row['id']), -1 ); |
---|
765 | foreach( $related_tags as $i=>$tag) |
---|
766 | { |
---|
767 | $tag['url'] = make_index_url( |
---|
768 | array( |
---|
769 | 'tags' => array($tag) |
---|
770 | ) |
---|
771 | ); |
---|
772 | $tag['page_url'] = make_picture_url( |
---|
773 | array( |
---|
774 | 'image_id' => $image_row['id'], |
---|
775 | 'image_file' => $image_row['file'], |
---|
776 | 'tags' => array($tag), |
---|
777 | ) |
---|
778 | ); |
---|
779 | unset($tag['counter']); |
---|
780 | $tag['id']=(int)$tag['id']; |
---|
781 | $related_tags[$i]=$tag; |
---|
782 | } |
---|
783 | //------------------------------------------------------------- related rates |
---|
784 | $query = ' |
---|
785 | SELECT COUNT(rate) AS count |
---|
786 | , ROUND(AVG(rate),2) AS average |
---|
787 | FROM '.RATE_TABLE.' |
---|
788 | WHERE element_id = '.$image_row['id'].' |
---|
789 | ;'; |
---|
790 | $rating = pwg_db_fetch_assoc(pwg_query($query)); |
---|
791 | $rating['count'] = (int)$rating['count']; |
---|
792 | |
---|
793 | //---------------------------------------------------------- related comments |
---|
794 | $related_comments = array(); |
---|
795 | |
---|
796 | $where_comments = 'image_id = '.$image_row['id']; |
---|
797 | if ( !is_admin() ) |
---|
798 | { |
---|
799 | $where_comments .= ' |
---|
800 | AND validated="true"'; |
---|
801 | } |
---|
802 | |
---|
803 | $query = ' |
---|
804 | SELECT COUNT(id) AS nb_comments |
---|
805 | FROM '.COMMENTS_TABLE.' |
---|
806 | WHERE '.$where_comments; |
---|
807 | list($nb_comments) = array_from_query($query, 'nb_comments'); |
---|
808 | $nb_comments = (int)$nb_comments; |
---|
809 | |
---|
810 | if ( $nb_comments>0 and $params['comments_per_page']>0 ) |
---|
811 | { |
---|
812 | $query = ' |
---|
813 | SELECT id, date, author, content |
---|
814 | FROM '.COMMENTS_TABLE.' |
---|
815 | WHERE '.$where_comments.' |
---|
816 | ORDER BY date |
---|
817 | LIMIT '.(int)$params['comments_per_page']. |
---|
818 | ' OFFSET '.(int)($params['comments_per_page']*$params['comments_page']); |
---|
819 | |
---|
820 | $result = pwg_query($query); |
---|
821 | while ($row = pwg_db_fetch_assoc($result)) |
---|
822 | { |
---|
823 | $row['id']=(int)$row['id']; |
---|
824 | array_push($related_comments, $row); |
---|
825 | } |
---|
826 | } |
---|
827 | |
---|
828 | $comment_post_data = null; |
---|
829 | if ($is_commentable and |
---|
830 | (!is_a_guest() |
---|
831 | or (is_a_guest() and $conf['comments_forall'] ) |
---|
832 | ) |
---|
833 | ) |
---|
834 | { |
---|
835 | $comment_post_data['author'] = stripslashes($user['username']); |
---|
836 | $comment_post_data['key'] = get_ephemeral_key(2, $params['image_id']); |
---|
837 | } |
---|
838 | |
---|
839 | $ret = $image_row; |
---|
840 | foreach ( array('id','width','height','hit','filesize') as $k ) |
---|
841 | { |
---|
842 | if (isset($ret[$k])) |
---|
843 | { |
---|
844 | $ret[$k] = (int)$ret[$k]; |
---|
845 | } |
---|
846 | } |
---|
847 | foreach ( array('path', 'storage_category_id') as $k ) |
---|
848 | { |
---|
849 | unset($ret[$k]); |
---|
850 | } |
---|
851 | |
---|
852 | $ret['rates'] = array( WS_XML_ATTRIBUTES => $rating ); |
---|
853 | $ret['categories'] = new PwgNamedArray($related_categories, 'category', array('id','url', 'page_url') ); |
---|
854 | $ret['tags'] = new PwgNamedArray($related_tags, 'tag', array('id','url_name','url','name','page_url') ); |
---|
855 | if ( isset($comment_post_data) ) |
---|
856 | { |
---|
857 | $ret['comment_post'] = array( WS_XML_ATTRIBUTES => $comment_post_data ); |
---|
858 | } |
---|
859 | $ret['comments'] = array( |
---|
860 | WS_XML_ATTRIBUTES => |
---|
861 | array( |
---|
862 | 'page' => $params['comments_page'], |
---|
863 | 'per_page' => $params['comments_per_page'], |
---|
864 | 'count' => count($related_comments), |
---|
865 | 'nb_comments' => $nb_comments, |
---|
866 | ), |
---|
867 | WS_XML_CONTENT => new PwgNamedArray($related_comments, 'comment', array('id','date') ) |
---|
868 | ); |
---|
869 | |
---|
870 | return new PwgNamedStruct('image',$ret, null, array('name','comment') ); |
---|
871 | } |
---|
872 | |
---|
873 | |
---|
874 | /** |
---|
875 | * rates the image_id in the parameter |
---|
876 | */ |
---|
877 | function ws_images_Rate($params, &$service) |
---|
878 | { |
---|
879 | $image_id = (int)$params['image_id']; |
---|
880 | $query = ' |
---|
881 | SELECT DISTINCT id FROM '.IMAGES_TABLE.' |
---|
882 | INNER JOIN '.IMAGE_CATEGORY_TABLE.' ON id=image_id |
---|
883 | WHERE id='.$image_id |
---|
884 | .get_sql_condition_FandF( |
---|
885 | array( |
---|
886 | 'forbidden_categories' => 'category_id', |
---|
887 | 'forbidden_images' => 'id', |
---|
888 | ), |
---|
889 | ' AND' |
---|
890 | ).' |
---|
891 | LIMIT 1'; |
---|
892 | if ( pwg_db_num_rows( pwg_query($query) )==0 ) |
---|
893 | { |
---|
894 | return new PwgError(404, "Invalid image_id or access denied" ); |
---|
895 | } |
---|
896 | $rate = (int)$params['rate']; |
---|
897 | include_once(PHPWG_ROOT_PATH.'include/functions_rate.inc.php'); |
---|
898 | $res = rate_picture( $image_id, $rate ); |
---|
899 | if ($res==false) |
---|
900 | { |
---|
901 | global $conf; |
---|
902 | return new PwgError( 403, "Forbidden or rate not in ". implode(',',$conf['rate_items'])); |
---|
903 | } |
---|
904 | return $res; |
---|
905 | } |
---|
906 | |
---|
907 | |
---|
908 | /** |
---|
909 | * returns a list of elements corresponding to a query search |
---|
910 | */ |
---|
911 | function ws_images_search($params, &$service) |
---|
912 | { |
---|
913 | global $page; |
---|
914 | $images = array(); |
---|
915 | include_once( PHPWG_ROOT_PATH .'include/functions_search.inc.php' ); |
---|
916 | include_once(PHPWG_ROOT_PATH.'include/functions_picture.inc.php'); |
---|
917 | |
---|
918 | $where_clauses = ws_std_image_sql_filter( $params, 'i.' ); |
---|
919 | $order_by = ws_std_image_sql_order($params, 'i.'); |
---|
920 | |
---|
921 | $super_order_by = false; |
---|
922 | if ( !empty($order_by) ) |
---|
923 | { |
---|
924 | global $conf; |
---|
925 | $conf['order_by'] = 'ORDER BY '.$order_by; |
---|
926 | $super_order_by=true; // quick_search_result might be faster |
---|
927 | } |
---|
928 | |
---|
929 | $search_result = get_quick_search_results($params['query'], |
---|
930 | $super_order_by, |
---|
931 | implode(',', $where_clauses) |
---|
932 | ); |
---|
933 | |
---|
934 | $image_ids = array_slice( |
---|
935 | $search_result['items'], |
---|
936 | $params['page']*$params['per_page'], |
---|
937 | $params['per_page'] |
---|
938 | ); |
---|
939 | |
---|
940 | if ( count($image_ids) ) |
---|
941 | { |
---|
942 | $query = ' |
---|
943 | SELECT * FROM '.IMAGES_TABLE.' |
---|
944 | WHERE id IN ('.implode(',', $image_ids).')'; |
---|
945 | |
---|
946 | $image_ids = array_flip($image_ids); |
---|
947 | $result = pwg_query($query); |
---|
948 | while ($row = pwg_db_fetch_assoc($result)) |
---|
949 | { |
---|
950 | $image = array(); |
---|
951 | foreach ( array('id', 'width', 'height', 'hit') as $k ) |
---|
952 | { |
---|
953 | if (isset($row[$k])) |
---|
954 | { |
---|
955 | $image[$k] = (int)$row[$k]; |
---|
956 | } |
---|
957 | } |
---|
958 | foreach ( array('file', 'name', 'comment', 'date_creation', 'date_available') as $k ) |
---|
959 | { |
---|
960 | $image[$k] = $row[$k]; |
---|
961 | } |
---|
962 | $image = array_merge( $image, ws_std_get_urls($row) ); |
---|
963 | $images[$image_ids[$image['id']]] = $image; |
---|
964 | } |
---|
965 | ksort($images, SORT_NUMERIC); |
---|
966 | $images = array_values($images); |
---|
967 | } |
---|
968 | |
---|
969 | |
---|
970 | return array( 'images' => |
---|
971 | array ( |
---|
972 | WS_XML_ATTRIBUTES => |
---|
973 | array( |
---|
974 | 'page' => $params['page'], |
---|
975 | 'per_page' => $params['per_page'], |
---|
976 | 'count' => count($images) |
---|
977 | ), |
---|
978 | WS_XML_CONTENT => new PwgNamedArray($images, 'image', |
---|
979 | ws_std_get_image_xml_attributes() ) |
---|
980 | ) |
---|
981 | ); |
---|
982 | } |
---|
983 | |
---|
984 | function ws_images_setPrivacyLevel($params, &$service) |
---|
985 | { |
---|
986 | if (!is_admin()) |
---|
987 | { |
---|
988 | return new PwgError(401, 'Access denied'); |
---|
989 | } |
---|
990 | if (!$service->isPost()) |
---|
991 | { |
---|
992 | return new PwgError(405, "This method requires HTTP POST"); |
---|
993 | } |
---|
994 | $params['image_id'] = array_map( 'intval',$params['image_id'] ); |
---|
995 | if ( empty($params['image_id']) ) |
---|
996 | { |
---|
997 | return new PwgError(WS_ERR_INVALID_PARAM, "Invalid image_id"); |
---|
998 | } |
---|
999 | global $conf; |
---|
1000 | if ( !in_array( (int)$params['level'], $conf['available_permission_levels']) ) |
---|
1001 | { |
---|
1002 | return new PwgError(WS_ERR_INVALID_PARAM, "Invalid level"); |
---|
1003 | } |
---|
1004 | |
---|
1005 | $query = ' |
---|
1006 | UPDATE '.IMAGES_TABLE.' |
---|
1007 | SET level='.(int)$params['level'].' |
---|
1008 | WHERE id IN ('.implode(',',$params['image_id']).')'; |
---|
1009 | $result = pwg_query($query); |
---|
1010 | $affected_rows = pwg_db_changes($result); |
---|
1011 | if ($affected_rows) |
---|
1012 | { |
---|
1013 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
---|
1014 | invalidate_user_cache(); |
---|
1015 | } |
---|
1016 | return $affected_rows; |
---|
1017 | } |
---|
1018 | |
---|
1019 | function ws_images_setRank($params, &$service) |
---|
1020 | { |
---|
1021 | if (!is_admin()) |
---|
1022 | { |
---|
1023 | return new PwgError(401, 'Access denied'); |
---|
1024 | } |
---|
1025 | |
---|
1026 | if (!$service->isPost()) |
---|
1027 | { |
---|
1028 | return new PwgError(405, "This method requires HTTP POST"); |
---|
1029 | } |
---|
1030 | |
---|
1031 | // is the image_id valid? |
---|
1032 | $params['image_id'] = (int)$params['image_id']; |
---|
1033 | if ($params['image_id'] <= 0) |
---|
1034 | { |
---|
1035 | return new PwgError(WS_ERR_INVALID_PARAM, "Invalid image_id"); |
---|
1036 | } |
---|
1037 | |
---|
1038 | // is the category valid? |
---|
1039 | $params['category_id'] = (int)$params['category_id']; |
---|
1040 | if ($params['category_id'] <= 0) |
---|
1041 | { |
---|
1042 | return new PwgError(WS_ERR_INVALID_PARAM, "Invalid category_id"); |
---|
1043 | } |
---|
1044 | |
---|
1045 | // is the rank valid? |
---|
1046 | $params['rank'] = (int)$params['rank']; |
---|
1047 | if ($params['rank'] <= 0) |
---|
1048 | { |
---|
1049 | return new PwgError(WS_ERR_INVALID_PARAM, "Invalid rank"); |
---|
1050 | } |
---|
1051 | |
---|
1052 | // does the image really exist? |
---|
1053 | $query=' |
---|
1054 | SELECT |
---|
1055 | * |
---|
1056 | FROM '.IMAGES_TABLE.' |
---|
1057 | WHERE id = '.$params['image_id'].' |
---|
1058 | ;'; |
---|
1059 | |
---|
1060 | $image_row = pwg_db_fetch_assoc(pwg_query($query)); |
---|
1061 | if ($image_row == null) |
---|
1062 | { |
---|
1063 | return new PwgError(404, "image_id not found"); |
---|
1064 | } |
---|
1065 | |
---|
1066 | // is the image associated to this category? |
---|
1067 | $query = ' |
---|
1068 | SELECT |
---|
1069 | image_id, |
---|
1070 | category_id, |
---|
1071 | rank |
---|
1072 | FROM '.IMAGE_CATEGORY_TABLE.' |
---|
1073 | WHERE image_id = '.$params['image_id'].' |
---|
1074 | AND category_id = '.$params['category_id'].' |
---|
1075 | ;'; |
---|
1076 | $category_row = pwg_db_fetch_assoc(pwg_query($query)); |
---|
1077 | if ($category_row == null) |
---|
1078 | { |
---|
1079 | return new PwgError(404, "This image is not associated to this category"); |
---|
1080 | } |
---|
1081 | |
---|
1082 | // what is the current higher rank for this category? |
---|
1083 | $query = ' |
---|
1084 | SELECT |
---|
1085 | MAX(rank) AS max_rank |
---|
1086 | FROM '.IMAGE_CATEGORY_TABLE.' |
---|
1087 | WHERE category_id = '.$params['category_id'].' |
---|
1088 | ;'; |
---|
1089 | $result = pwg_query($query); |
---|
1090 | $row = pwg_db_fetch_assoc($result); |
---|
1091 | |
---|
1092 | if (is_numeric($row['max_rank'])) |
---|
1093 | { |
---|
1094 | if ($params['rank'] > $row['max_rank']) |
---|
1095 | { |
---|
1096 | $params['rank'] = $row['max_rank'] + 1; |
---|
1097 | } |
---|
1098 | } |
---|
1099 | else |
---|
1100 | { |
---|
1101 | $params['rank'] = 1; |
---|
1102 | } |
---|
1103 | |
---|
1104 | // update rank for all other photos in the same category |
---|
1105 | $query = ' |
---|
1106 | UPDATE '.IMAGE_CATEGORY_TABLE.' |
---|
1107 | SET rank = rank + 1 |
---|
1108 | WHERE category_id = '.$params['category_id'].' |
---|
1109 | AND rank IS NOT NULL |
---|
1110 | AND rank >= '.$params['rank'].' |
---|
1111 | ;'; |
---|
1112 | pwg_query($query); |
---|
1113 | |
---|
1114 | // set the new rank for the photo |
---|
1115 | $query = ' |
---|
1116 | UPDATE '.IMAGE_CATEGORY_TABLE.' |
---|
1117 | SET rank = '.$params['rank'].' |
---|
1118 | WHERE image_id = '.$params['image_id'].' |
---|
1119 | AND category_id = '.$params['category_id'].' |
---|
1120 | ;'; |
---|
1121 | pwg_query($query); |
---|
1122 | |
---|
1123 | // return data for client |
---|
1124 | return array( |
---|
1125 | 'image_id' => $params['image_id'], |
---|
1126 | 'category_id' => $params['category_id'], |
---|
1127 | 'rank' => $params['rank'], |
---|
1128 | ); |
---|
1129 | } |
---|
1130 | |
---|
1131 | function ws_images_add_chunk($params, &$service) |
---|
1132 | { |
---|
1133 | global $conf; |
---|
1134 | |
---|
1135 | ws_logfile('[ws_images_add_chunk] welcome'); |
---|
1136 | // data |
---|
1137 | // original_sum |
---|
1138 | // type {thumb, file, high} |
---|
1139 | // position |
---|
1140 | |
---|
1141 | if (!is_admin()) |
---|
1142 | { |
---|
1143 | return new PwgError(401, 'Access denied'); |
---|
1144 | } |
---|
1145 | |
---|
1146 | if (!$service->isPost()) |
---|
1147 | { |
---|
1148 | return new PwgError(405, "This method requires HTTP POST"); |
---|
1149 | } |
---|
1150 | |
---|
1151 | foreach ($params as $param_key => $param_value) { |
---|
1152 | if ('data' == $param_key) { |
---|
1153 | continue; |
---|
1154 | } |
---|
1155 | |
---|
1156 | ws_logfile( |
---|
1157 | sprintf( |
---|
1158 | '[ws_images_add_chunk] input param "%s" : "%s"', |
---|
1159 | $param_key, |
---|
1160 | is_null($param_value) ? 'NULL' : $param_value |
---|
1161 | ) |
---|
1162 | ); |
---|
1163 | } |
---|
1164 | |
---|
1165 | $upload_dir = $conf['upload_dir'].'/buffer'; |
---|
1166 | |
---|
1167 | // create the upload directory tree if not exists |
---|
1168 | if (!is_dir($upload_dir)) { |
---|
1169 | umask(0000); |
---|
1170 | $recursive = true; |
---|
1171 | if (!@mkdir($upload_dir, 0777, $recursive)) |
---|
1172 | { |
---|
1173 | return new PwgError(500, 'error during buffer directory creation'); |
---|
1174 | } |
---|
1175 | } |
---|
1176 | |
---|
1177 | if (!is_writable($upload_dir)) |
---|
1178 | { |
---|
1179 | // last chance to make the directory writable |
---|
1180 | @chmod($upload_dir, 0777); |
---|
1181 | |
---|
1182 | if (!is_writable($upload_dir)) |
---|
1183 | { |
---|
1184 | return new PwgError(500, 'buffer directory has no write access'); |
---|
1185 | } |
---|
1186 | } |
---|
1187 | |
---|
1188 | secure_directory($upload_dir); |
---|
1189 | |
---|
1190 | $filename = sprintf( |
---|
1191 | '%s-%s-%05u.block', |
---|
1192 | $params['original_sum'], |
---|
1193 | $params['type'], |
---|
1194 | $params['position'] |
---|
1195 | ); |
---|
1196 | |
---|
1197 | ws_logfile('[ws_images_add_chunk] data length : '.strlen($params['data'])); |
---|
1198 | |
---|
1199 | $bytes_written = file_put_contents( |
---|
1200 | $upload_dir.'/'.$filename, |
---|
1201 | base64_decode($params['data']) |
---|
1202 | ); |
---|
1203 | |
---|
1204 | if (false === $bytes_written) { |
---|
1205 | return new PwgError( |
---|
1206 | 500, |
---|
1207 | 'an error has occured while writting chunk '.$params['position'].' for '.$params['type'] |
---|
1208 | ); |
---|
1209 | } |
---|
1210 | } |
---|
1211 | |
---|
1212 | function merge_chunks($output_filepath, $original_sum, $type) |
---|
1213 | { |
---|
1214 | global $conf; |
---|
1215 | |
---|
1216 | ws_logfile('[merge_chunks] input parameter $output_filepath : '.$output_filepath); |
---|
1217 | |
---|
1218 | if (is_file($output_filepath)) |
---|
1219 | { |
---|
1220 | unlink($output_filepath); |
---|
1221 | |
---|
1222 | if (is_file($output_filepath)) |
---|
1223 | { |
---|
1224 | new PwgError(500, '[merge_chunks] error while trying to remove existing '.$output_filepath); |
---|
1225 | exit(); |
---|
1226 | } |
---|
1227 | } |
---|
1228 | |
---|
1229 | $upload_dir = $conf['upload_dir'].'/buffer'; |
---|
1230 | $pattern = '/'.$original_sum.'-'.$type.'/'; |
---|
1231 | $chunks = array(); |
---|
1232 | |
---|
1233 | if ($handle = opendir($upload_dir)) |
---|
1234 | { |
---|
1235 | while (false !== ($file = readdir($handle))) |
---|
1236 | { |
---|
1237 | if (preg_match($pattern, $file)) |
---|
1238 | { |
---|
1239 | ws_logfile($file); |
---|
1240 | array_push($chunks, $upload_dir.'/'.$file); |
---|
1241 | } |
---|
1242 | } |
---|
1243 | closedir($handle); |
---|
1244 | } |
---|
1245 | |
---|
1246 | sort($chunks); |
---|
1247 | |
---|
1248 | if (function_exists('memory_get_usage')) { |
---|
1249 | ws_logfile('[merge_chunks] memory_get_usage before loading chunks: '.memory_get_usage()); |
---|
1250 | } |
---|
1251 | |
---|
1252 | $i = 0; |
---|
1253 | |
---|
1254 | foreach ($chunks as $chunk) |
---|
1255 | { |
---|
1256 | $string = file_get_contents($chunk); |
---|
1257 | |
---|
1258 | if (function_exists('memory_get_usage')) { |
---|
1259 | ws_logfile('[merge_chunks] memory_get_usage on chunk '.++$i.': '.memory_get_usage()); |
---|
1260 | } |
---|
1261 | |
---|
1262 | if (!file_put_contents($output_filepath, $string, FILE_APPEND)) |
---|
1263 | { |
---|
1264 | new PwgError(500, '[merge_chunks] error while writting chunks for '.$output_filepath); |
---|
1265 | exit(); |
---|
1266 | } |
---|
1267 | |
---|
1268 | unlink($chunk); |
---|
1269 | } |
---|
1270 | |
---|
1271 | if (function_exists('memory_get_usage')) { |
---|
1272 | ws_logfile('[merge_chunks] memory_get_usage after loading chunks: '.memory_get_usage()); |
---|
1273 | } |
---|
1274 | } |
---|
1275 | |
---|
1276 | /* |
---|
1277 | * The $file_path must be the path of the basic "web sized" photo |
---|
1278 | * The $type value will automatically modify the $file_path to the corresponding file |
---|
1279 | */ |
---|
1280 | function add_file($file_path, $type, $original_sum, $file_sum) |
---|
1281 | { |
---|
1282 | include_once(PHPWG_ROOT_PATH.'admin/include/functions_upload.inc.php'); |
---|
1283 | |
---|
1284 | $file_path = file_path_for_type($file_path, $type); |
---|
1285 | |
---|
1286 | $upload_dir = dirname($file_path); |
---|
1287 | if (substr(PHP_OS, 0, 3) == 'WIN') |
---|
1288 | { |
---|
1289 | $upload_dir = str_replace('/', DIRECTORY_SEPARATOR, $upload_dir); |
---|
1290 | } |
---|
1291 | |
---|
1292 | ws_logfile('[add_file] file_path : '.$file_path); |
---|
1293 | ws_logfile('[add_file] upload_dir : '.$upload_dir); |
---|
1294 | |
---|
1295 | if (!is_dir($upload_dir)) { |
---|
1296 | umask(0000); |
---|
1297 | $recursive = true; |
---|
1298 | if (!@mkdir($upload_dir, 0777, $recursive)) |
---|
1299 | { |
---|
1300 | new PwgError(500, '[add_file] error during '.$type.' directory creation'); |
---|
1301 | exit(); |
---|
1302 | } |
---|
1303 | } |
---|
1304 | |
---|
1305 | if (!is_writable($upload_dir)) |
---|
1306 | { |
---|
1307 | // last chance to make the directory writable |
---|
1308 | @chmod($upload_dir, 0777); |
---|
1309 | |
---|
1310 | if (!is_writable($upload_dir)) |
---|
1311 | { |
---|
1312 | new PwgError(500, '[add_file] '.$type.' directory has no write access'); |
---|
1313 | exit(); |
---|
1314 | } |
---|
1315 | } |
---|
1316 | |
---|
1317 | secure_directory($upload_dir); |
---|
1318 | |
---|
1319 | // merge the thumbnail |
---|
1320 | merge_chunks($file_path, $original_sum, $type); |
---|
1321 | chmod($file_path, 0644); |
---|
1322 | |
---|
1323 | // check dumped thumbnail md5 |
---|
1324 | $dumped_md5 = md5_file($file_path); |
---|
1325 | if ($dumped_md5 != $file_sum) { |
---|
1326 | new PwgError(500, '[add_file] '.$type.' transfer failed'); |
---|
1327 | exit(); |
---|
1328 | } |
---|
1329 | |
---|
1330 | list($width, $height) = getimagesize($file_path); |
---|
1331 | $filesize = floor(filesize($file_path)/1024); |
---|
1332 | |
---|
1333 | return array( |
---|
1334 | 'width' => $width, |
---|
1335 | 'height' => $height, |
---|
1336 | 'filesize' => $filesize, |
---|
1337 | ); |
---|
1338 | } |
---|
1339 | |
---|
1340 | function ws_images_addFile($params, &$service) |
---|
1341 | { |
---|
1342 | // image_id |
---|
1343 | // type {thumb, file, high} |
---|
1344 | // sum |
---|
1345 | |
---|
1346 | global $conf; |
---|
1347 | if (!is_admin()) |
---|
1348 | { |
---|
1349 | return new PwgError(401, 'Access denied'); |
---|
1350 | } |
---|
1351 | |
---|
1352 | $params['image_id'] = (int)$params['image_id']; |
---|
1353 | if ($params['image_id'] <= 0) |
---|
1354 | { |
---|
1355 | return new PwgError(WS_ERR_INVALID_PARAM, "Invalid image_id"); |
---|
1356 | } |
---|
1357 | |
---|
1358 | // |
---|
1359 | // what is the path? |
---|
1360 | // |
---|
1361 | $query = ' |
---|
1362 | SELECT |
---|
1363 | path, |
---|
1364 | md5sum |
---|
1365 | FROM '.IMAGES_TABLE.' |
---|
1366 | WHERE id = '.$params['image_id'].' |
---|
1367 | ;'; |
---|
1368 | list($file_path, $original_sum) = pwg_db_fetch_row(pwg_query($query)); |
---|
1369 | |
---|
1370 | // TODO only files added with web API can be updated with web API |
---|
1371 | |
---|
1372 | // |
---|
1373 | // makes sure directories are there and call the merge_chunks |
---|
1374 | // |
---|
1375 | $infos = add_file($file_path, $params['type'], $original_sum, $params['sum']); |
---|
1376 | |
---|
1377 | // |
---|
1378 | // update basic metadata from file |
---|
1379 | // |
---|
1380 | $update = array(); |
---|
1381 | |
---|
1382 | if ('high' == $params['type']) |
---|
1383 | { |
---|
1384 | $update['high_filesize'] = $infos['filesize']; |
---|
1385 | $update['high_width'] = $infos['width']; |
---|
1386 | $update['high_height'] = $infos['height']; |
---|
1387 | $update['has_high'] = 'true'; |
---|
1388 | } |
---|
1389 | |
---|
1390 | if ('file' == $params['type']) |
---|
1391 | { |
---|
1392 | $update['filesize'] = $infos['filesize']; |
---|
1393 | $update['width'] = $infos['width']; |
---|
1394 | $update['height'] = $infos['height']; |
---|
1395 | } |
---|
1396 | |
---|
1397 | // we may have nothing to update at database level, for example with a |
---|
1398 | // thumbnail update |
---|
1399 | if (count($update) > 0) |
---|
1400 | { |
---|
1401 | $update['id'] = $params['image_id']; |
---|
1402 | |
---|
1403 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
---|
1404 | mass_updates( |
---|
1405 | IMAGES_TABLE, |
---|
1406 | array( |
---|
1407 | 'primary' => array('id'), |
---|
1408 | 'update' => array_diff(array_keys($update), array('id')) |
---|
1409 | ), |
---|
1410 | array($update) |
---|
1411 | ); |
---|
1412 | } |
---|
1413 | } |
---|
1414 | |
---|
1415 | function ws_images_add($params, &$service) |
---|
1416 | { |
---|
1417 | global $conf, $user; |
---|
1418 | if (!is_admin()) |
---|
1419 | { |
---|
1420 | return new PwgError(401, 'Access denied'); |
---|
1421 | } |
---|
1422 | |
---|
1423 | foreach ($params as $param_key => $param_value) { |
---|
1424 | ws_logfile( |
---|
1425 | sprintf( |
---|
1426 | '[pwg.images.add] input param "%s" : "%s"', |
---|
1427 | $param_key, |
---|
1428 | is_null($param_value) ? 'NULL' : $param_value |
---|
1429 | ) |
---|
1430 | ); |
---|
1431 | } |
---|
1432 | |
---|
1433 | // does the image already exists ? |
---|
1434 | if ('md5sum' == $conf['uniqueness_mode']) |
---|
1435 | { |
---|
1436 | $where_clause = "md5sum = '".$params['original_sum']."'"; |
---|
1437 | } |
---|
1438 | if ('filename' == $conf['uniqueness_mode']) |
---|
1439 | { |
---|
1440 | $where_clause = "file = '".$params['original_filename']."'"; |
---|
1441 | } |
---|
1442 | |
---|
1443 | $query = ' |
---|
1444 | SELECT |
---|
1445 | COUNT(*) AS counter |
---|
1446 | FROM '.IMAGES_TABLE.' |
---|
1447 | WHERE '.$where_clause.' |
---|
1448 | ;'; |
---|
1449 | list($counter) = pwg_db_fetch_row(pwg_query($query)); |
---|
1450 | if ($counter != 0) { |
---|
1451 | return new PwgError(500, 'file already exists'); |
---|
1452 | } |
---|
1453 | |
---|
1454 | // current date |
---|
1455 | list($dbnow) = pwg_db_fetch_row(pwg_query('SELECT NOW();')); |
---|
1456 | list($year, $month, $day) = preg_split('/[^\d]/', $dbnow, 4); |
---|
1457 | |
---|
1458 | // upload directory hierarchy |
---|
1459 | $upload_dir = sprintf( |
---|
1460 | $conf['upload_dir'].'/%s/%s/%s', |
---|
1461 | $year, |
---|
1462 | $month, |
---|
1463 | $day |
---|
1464 | ); |
---|
1465 | |
---|
1466 | // compute file path |
---|
1467 | $date_string = preg_replace('/[^\d]/', '', $dbnow); |
---|
1468 | $random_string = substr($params['file_sum'], 0, 8); |
---|
1469 | $filename_wo_ext = $date_string.'-'.$random_string; |
---|
1470 | $file_path = $upload_dir.'/'.$filename_wo_ext.'.jpg'; |
---|
1471 | |
---|
1472 | // add files |
---|
1473 | $file_infos = add_file($file_path, 'file', $params['original_sum'], $params['file_sum']); |
---|
1474 | $thumb_infos = add_file($file_path, 'thumb', $params['original_sum'], $params['thumbnail_sum']); |
---|
1475 | |
---|
1476 | if (isset($params['high_sum'])) |
---|
1477 | { |
---|
1478 | $high_infos = add_file($file_path, 'high', $params['original_sum'], $params['high_sum']); |
---|
1479 | } |
---|
1480 | |
---|
1481 | // database registration |
---|
1482 | $insert = array( |
---|
1483 | 'file' => !empty($params['original_filename']) ? $params['original_filename'] : $filename_wo_ext.'.jpg', |
---|
1484 | 'date_available' => $dbnow, |
---|
1485 | 'tn_ext' => 'jpg', |
---|
1486 | 'name' => $params['name'], |
---|
1487 | 'path' => $file_path, |
---|
1488 | 'filesize' => $file_infos['filesize'], |
---|
1489 | 'width' => $file_infos['width'], |
---|
1490 | 'height' => $file_infos['height'], |
---|
1491 | 'md5sum' => $params['original_sum'], |
---|
1492 | 'added_by' => $user['id'], |
---|
1493 | ); |
---|
1494 | |
---|
1495 | $info_columns = array( |
---|
1496 | 'name', |
---|
1497 | 'author', |
---|
1498 | 'comment', |
---|
1499 | 'level', |
---|
1500 | 'date_creation', |
---|
1501 | ); |
---|
1502 | |
---|
1503 | foreach ($info_columns as $key) |
---|
1504 | { |
---|
1505 | if (isset($params[$key])) |
---|
1506 | { |
---|
1507 | $insert[$key] = $params[$key]; |
---|
1508 | } |
---|
1509 | } |
---|
1510 | |
---|
1511 | if (isset($params['high_sum'])) |
---|
1512 | { |
---|
1513 | $insert['has_high'] = 'true'; |
---|
1514 | $insert['high_filesize'] = $high_infos['filesize']; |
---|
1515 | $insert['high_width'] = $high_infos['width']; |
---|
1516 | $insert['high_height'] = $high_infos['height']; |
---|
1517 | } |
---|
1518 | |
---|
1519 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
---|
1520 | mass_inserts( |
---|
1521 | IMAGES_TABLE, |
---|
1522 | array_keys($insert), |
---|
1523 | array($insert) |
---|
1524 | ); |
---|
1525 | |
---|
1526 | $image_id = pwg_db_insert_id(IMAGES_TABLE); |
---|
1527 | |
---|
1528 | // let's add links between the image and the categories |
---|
1529 | if (isset($params['categories'])) |
---|
1530 | { |
---|
1531 | ws_add_image_category_relations($image_id, $params['categories']); |
---|
1532 | } |
---|
1533 | |
---|
1534 | // and now, let's create tag associations |
---|
1535 | if (isset($params['tag_ids']) and !empty($params['tag_ids'])) |
---|
1536 | { |
---|
1537 | set_tags( |
---|
1538 | explode(',', $params['tag_ids']), |
---|
1539 | $image_id |
---|
1540 | ); |
---|
1541 | } |
---|
1542 | |
---|
1543 | // update metadata from the uploaded file (exif/iptc) |
---|
1544 | require_once(PHPWG_ROOT_PATH.'admin/include/functions_metadata.php'); |
---|
1545 | update_metadata(array($image_id=>$file_path)); |
---|
1546 | |
---|
1547 | invalidate_user_cache(); |
---|
1548 | } |
---|
1549 | |
---|
1550 | function ws_images_addSimple($params, &$service) |
---|
1551 | { |
---|
1552 | global $conf; |
---|
1553 | if (!is_admin()) |
---|
1554 | { |
---|
1555 | return new PwgError(401, 'Access denied'); |
---|
1556 | } |
---|
1557 | |
---|
1558 | if (!$service->isPost()) |
---|
1559 | { |
---|
1560 | return new PwgError(405, "This method requires HTTP POST"); |
---|
1561 | } |
---|
1562 | |
---|
1563 | if (!isset($_FILES['image'])) |
---|
1564 | { |
---|
1565 | return new PwgError(405, "The image (file) parameter is missing"); |
---|
1566 | } |
---|
1567 | |
---|
1568 | $params['image_id'] = (int)$params['image_id']; |
---|
1569 | if ($params['image_id'] > 0) |
---|
1570 | { |
---|
1571 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
---|
1572 | |
---|
1573 | $query=' |
---|
1574 | SELECT * |
---|
1575 | FROM '.IMAGES_TABLE.' |
---|
1576 | WHERE id = '.$params['image_id'].' |
---|
1577 | ;'; |
---|
1578 | |
---|
1579 | $image_row = pwg_db_fetch_assoc(pwg_query($query)); |
---|
1580 | if ($image_row == null) |
---|
1581 | { |
---|
1582 | return new PwgError(404, "image_id not found"); |
---|
1583 | } |
---|
1584 | } |
---|
1585 | |
---|
1586 | // category |
---|
1587 | $params['category'] = (int)$params['category']; |
---|
1588 | if ($params['category'] <= 0 and $params['image_id'] <= 0) |
---|
1589 | { |
---|
1590 | return new PwgError(WS_ERR_INVALID_PARAM, "Invalid category_id"); |
---|
1591 | } |
---|
1592 | |
---|
1593 | include_once(PHPWG_ROOT_PATH.'admin/include/functions_upload.inc.php'); |
---|
1594 | |
---|
1595 | $image_id = add_uploaded_file( |
---|
1596 | $_FILES['image']['tmp_name'], |
---|
1597 | $_FILES['image']['name'], |
---|
1598 | $params['category'] > 0 ? array($params['category']) : null, |
---|
1599 | 8, |
---|
1600 | $params['image_id'] > 0 ? $params['image_id'] : null |
---|
1601 | ); |
---|
1602 | |
---|
1603 | $info_columns = array( |
---|
1604 | 'name', |
---|
1605 | 'author', |
---|
1606 | 'comment', |
---|
1607 | 'level', |
---|
1608 | 'date_creation', |
---|
1609 | ); |
---|
1610 | |
---|
1611 | foreach ($info_columns as $key) |
---|
1612 | { |
---|
1613 | if (isset($params[$key])) |
---|
1614 | { |
---|
1615 | $update[$key] = $params[$key]; |
---|
1616 | } |
---|
1617 | } |
---|
1618 | |
---|
1619 | if (count(array_keys($update)) > 0) |
---|
1620 | { |
---|
1621 | $update['id'] = $image_id; |
---|
1622 | |
---|
1623 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
---|
1624 | mass_updates( |
---|
1625 | IMAGES_TABLE, |
---|
1626 | array( |
---|
1627 | 'primary' => array('id'), |
---|
1628 | 'update' => array_diff(array_keys($update), array('id')) |
---|
1629 | ), |
---|
1630 | array($update) |
---|
1631 | ); |
---|
1632 | } |
---|
1633 | |
---|
1634 | |
---|
1635 | if (isset($params['tags']) and !empty($params['tags'])) |
---|
1636 | { |
---|
1637 | $tag_ids = array(); |
---|
1638 | $tag_names = explode(',', $params['tags']); |
---|
1639 | foreach ($tag_names as $tag_name) |
---|
1640 | { |
---|
1641 | $tag_id = tag_id_from_tag_name($tag_name); |
---|
1642 | array_push($tag_ids, $tag_id); |
---|
1643 | } |
---|
1644 | |
---|
1645 | add_tags($tag_ids, array($image_id)); |
---|
1646 | } |
---|
1647 | |
---|
1648 | $url_params = array('image_id' => $image_id); |
---|
1649 | |
---|
1650 | if ($params['category'] > 0) |
---|
1651 | { |
---|
1652 | $query = ' |
---|
1653 | SELECT id, name, permalink |
---|
1654 | FROM '.CATEGORIES_TABLE.' |
---|
1655 | WHERE id = '.$params['category'].' |
---|
1656 | ;'; |
---|
1657 | $result = pwg_query($query); |
---|
1658 | $category = pwg_db_fetch_assoc($result); |
---|
1659 | |
---|
1660 | $url_params['section'] = 'categories'; |
---|
1661 | $url_params['category'] = $category; |
---|
1662 | } |
---|
1663 | |
---|
1664 | // update metadata from the uploaded file (exif/iptc), even if the sync |
---|
1665 | // was already performed by add_uploaded_file(). |
---|
1666 | $query = ' |
---|
1667 | SELECT |
---|
1668 | path |
---|
1669 | FROM '.IMAGES_TABLE.' |
---|
1670 | WHERE id = '.$image_id.' |
---|
1671 | ;'; |
---|
1672 | list($file_path) = pwg_db_fetch_row(pwg_query($query)); |
---|
1673 | |
---|
1674 | require_once(PHPWG_ROOT_PATH.'admin/include/functions_metadata.php'); |
---|
1675 | update_metadata(array($image_id=>$file_path)); |
---|
1676 | |
---|
1677 | return array( |
---|
1678 | 'image_id' => $image_id, |
---|
1679 | 'url' => make_picture_url($url_params), |
---|
1680 | ); |
---|
1681 | } |
---|
1682 | |
---|
1683 | /** |
---|
1684 | * perform a login (web service method) |
---|
1685 | */ |
---|
1686 | function ws_session_login($params, &$service) |
---|
1687 | { |
---|
1688 | global $conf; |
---|
1689 | |
---|
1690 | if (!$service->isPost()) |
---|
1691 | { |
---|
1692 | return new PwgError(405, "This method requires HTTP POST"); |
---|
1693 | } |
---|
1694 | if (try_log_user($params['username'], $params['password'],false)) |
---|
1695 | { |
---|
1696 | return true; |
---|
1697 | } |
---|
1698 | return new PwgError(999, 'Invalid username/password'); |
---|
1699 | } |
---|
1700 | |
---|
1701 | |
---|
1702 | /** |
---|
1703 | * performs a logout (web service method) |
---|
1704 | */ |
---|
1705 | function ws_session_logout($params, &$service) |
---|
1706 | { |
---|
1707 | if (!is_a_guest()) |
---|
1708 | { |
---|
1709 | logout_user(); |
---|
1710 | } |
---|
1711 | return true; |
---|
1712 | } |
---|
1713 | |
---|
1714 | function ws_session_getStatus($params, &$service) |
---|
1715 | { |
---|
1716 | global $user; |
---|
1717 | $res = array(); |
---|
1718 | $res['username'] = is_a_guest() ? 'guest' : stripslashes($user['username']); |
---|
1719 | foreach ( array('status', 'theme', 'language') as $k ) |
---|
1720 | { |
---|
1721 | $res[$k] = $user[$k]; |
---|
1722 | } |
---|
1723 | $res['pwg_token'] = get_pwg_token(); |
---|
1724 | $res['charset'] = get_pwg_charset(); |
---|
1725 | return $res; |
---|
1726 | } |
---|
1727 | |
---|
1728 | |
---|
1729 | /** |
---|
1730 | * returns a list of tags (web service method) |
---|
1731 | */ |
---|
1732 | function ws_tags_getList($params, &$service) |
---|
1733 | { |
---|
1734 | $tags = get_available_tags(); |
---|
1735 | if ($params['sort_by_counter']) |
---|
1736 | { |
---|
1737 | usort($tags, create_function('$a,$b', 'return -$a["counter"]+$b["counter"];') ); |
---|
1738 | } |
---|
1739 | else |
---|
1740 | { |
---|
1741 | usort($tags, 'tag_alpha_compare'); |
---|
1742 | } |
---|
1743 | for ($i=0; $i<count($tags); $i++) |
---|
1744 | { |
---|
1745 | $tags[$i]['id'] = (int)$tags[$i]['id']; |
---|
1746 | $tags[$i]['counter'] = (int)$tags[$i]['counter']; |
---|
1747 | $tags[$i]['url'] = make_index_url( |
---|
1748 | array( |
---|
1749 | 'section'=>'tags', |
---|
1750 | 'tags'=>array($tags[$i]) |
---|
1751 | ) |
---|
1752 | ); |
---|
1753 | } |
---|
1754 | return array('tags' => new PwgNamedArray($tags, 'tag', array('id','url_name','url', 'name', 'counter' )) ); |
---|
1755 | } |
---|
1756 | |
---|
1757 | /** |
---|
1758 | * returns the list of tags as you can see them in administration (web |
---|
1759 | * service method). |
---|
1760 | * |
---|
1761 | * Only admin can run this method and permissions are not taken into |
---|
1762 | * account. |
---|
1763 | */ |
---|
1764 | function ws_tags_getAdminList($params, &$service) |
---|
1765 | { |
---|
1766 | if (!is_admin()) |
---|
1767 | { |
---|
1768 | return new PwgError(401, 'Access denied'); |
---|
1769 | } |
---|
1770 | |
---|
1771 | $tags = get_all_tags(); |
---|
1772 | return array( |
---|
1773 | 'tags' => new PwgNamedArray( |
---|
1774 | $tags, |
---|
1775 | 'tag', |
---|
1776 | array( |
---|
1777 | 'name', |
---|
1778 | 'id', |
---|
1779 | 'url_name', |
---|
1780 | ) |
---|
1781 | ) |
---|
1782 | ); |
---|
1783 | } |
---|
1784 | |
---|
1785 | /** |
---|
1786 | * returns a list of images for tags (web service method) |
---|
1787 | */ |
---|
1788 | function ws_tags_getImages($params, &$service) |
---|
1789 | { |
---|
1790 | @include_once(PHPWG_ROOT_PATH.'include/functions_picture.inc.php'); |
---|
1791 | global $conf; |
---|
1792 | |
---|
1793 | // first build all the tag_ids we are interested in |
---|
1794 | $params['tag_id'] = array_map( 'intval',$params['tag_id'] ); |
---|
1795 | $tags = find_tags($params['tag_id'], $params['tag_url_name'], $params['tag_name']); |
---|
1796 | $tags_by_id = array(); |
---|
1797 | foreach( $tags as $tag ) |
---|
1798 | { |
---|
1799 | $tags['id'] = (int)$tag['id']; |
---|
1800 | $tags_by_id[ $tag['id'] ] = $tag; |
---|
1801 | } |
---|
1802 | unset($tags); |
---|
1803 | $tag_ids = array_keys($tags_by_id); |
---|
1804 | |
---|
1805 | |
---|
1806 | $where_clauses = ws_std_image_sql_filter($params); |
---|
1807 | if (!empty($where_clauses)) |
---|
1808 | { |
---|
1809 | $where_clauses = implode( ' AND ', $where_clauses); |
---|
1810 | } |
---|
1811 | $image_ids = get_image_ids_for_tags( |
---|
1812 | $tag_ids, |
---|
1813 | $params['tag_mode_and'] ? 'AND' : 'OR', |
---|
1814 | $where_clauses, |
---|
1815 | ws_std_image_sql_order($params) ); |
---|
1816 | |
---|
1817 | |
---|
1818 | $image_ids = array_slice($image_ids, (int)($params['per_page']*$params['page']), (int)$params['per_page'] ); |
---|
1819 | |
---|
1820 | $image_tag_map = array(); |
---|
1821 | if ( !empty($image_ids) and !$params['tag_mode_and'] ) |
---|
1822 | { // build list of image ids with associated tags per image |
---|
1823 | $query = ' |
---|
1824 | SELECT image_id, GROUP_CONCAT(tag_id) AS tag_ids |
---|
1825 | FROM '.IMAGE_TAG_TABLE.' |
---|
1826 | WHERE tag_id IN ('.implode(',',$tag_ids).') AND image_id IN ('.implode(',',$image_ids).') |
---|
1827 | GROUP BY image_id'; |
---|
1828 | $result = pwg_query($query); |
---|
1829 | while ( $row=pwg_db_fetch_assoc($result) ) |
---|
1830 | { |
---|
1831 | $row['image_id'] = (int)$row['image_id']; |
---|
1832 | array_push( $image_ids, $row['image_id'] ); |
---|
1833 | $image_tag_map[ $row['image_id'] ] = explode(',', $row['tag_ids']); |
---|
1834 | } |
---|
1835 | } |
---|
1836 | |
---|
1837 | $images = array(); |
---|
1838 | if (!empty($image_ids)) |
---|
1839 | { |
---|
1840 | $rank_of = array_flip($image_ids); |
---|
1841 | $result = pwg_query(' |
---|
1842 | SELECT * FROM '.IMAGES_TABLE.' |
---|
1843 | WHERE id IN ('.implode(',',$image_ids).')'); |
---|
1844 | while ($row = pwg_db_fetch_assoc($result)) |
---|
1845 | { |
---|
1846 | $image = array(); |
---|
1847 | $image['rank'] = $rank_of[ $row['id'] ]; |
---|
1848 | foreach ( array('id', 'width', 'height', 'hit') as $k ) |
---|
1849 | { |
---|
1850 | if (isset($row[$k])) |
---|
1851 | { |
---|
1852 | $image[$k] = (int)$row[$k]; |
---|
1853 | } |
---|
1854 | } |
---|
1855 | foreach ( array('file', 'name', 'comment', 'date_creation', 'date_available') as $k ) |
---|
1856 | { |
---|
1857 | $image[$k] = $row[$k]; |
---|
1858 | } |
---|
1859 | $image = array_merge( $image, ws_std_get_urls($row) ); |
---|
1860 | |
---|
1861 | $image_tag_ids = ($params['tag_mode_and']) ? $tag_ids : $image_tag_map[$image['id']]; |
---|
1862 | $image_tags = array(); |
---|
1863 | foreach ($image_tag_ids as $tag_id) |
---|
1864 | { |
---|
1865 | $url = make_index_url( |
---|
1866 | array( |
---|
1867 | 'section'=>'tags', |
---|
1868 | 'tags'=> array($tags_by_id[$tag_id]) |
---|
1869 | ) |
---|
1870 | ); |
---|
1871 | $page_url = make_picture_url( |
---|
1872 | array( |
---|
1873 | 'section'=>'tags', |
---|
1874 | 'tags'=> array($tags_by_id[$tag_id]), |
---|
1875 | 'image_id' => $row['id'], |
---|
1876 | 'image_file' => $row['file'], |
---|
1877 | ) |
---|
1878 | ); |
---|
1879 | array_push($image_tags, array( |
---|
1880 | 'id' => (int)$tag_id, |
---|
1881 | 'url' => $url, |
---|
1882 | 'page_url' => $page_url, |
---|
1883 | ) |
---|
1884 | ); |
---|
1885 | } |
---|
1886 | $image['tags'] = new PwgNamedArray($image_tags, 'tag', |
---|
1887 | array('id','url_name','url','page_url') |
---|
1888 | ); |
---|
1889 | array_push($images, $image); |
---|
1890 | } |
---|
1891 | usort($images, 'rank_compare'); |
---|
1892 | unset($rank_of); |
---|
1893 | } |
---|
1894 | |
---|
1895 | return array( 'images' => |
---|
1896 | array ( |
---|
1897 | WS_XML_ATTRIBUTES => |
---|
1898 | array( |
---|
1899 | 'page' => $params['page'], |
---|
1900 | 'per_page' => $params['per_page'], |
---|
1901 | 'count' => count($images) |
---|
1902 | ), |
---|
1903 | WS_XML_CONTENT => new PwgNamedArray($images, 'image', |
---|
1904 | ws_std_get_image_xml_attributes() ) |
---|
1905 | ) |
---|
1906 | ); |
---|
1907 | } |
---|
1908 | |
---|
1909 | function ws_categories_add($params, &$service) |
---|
1910 | { |
---|
1911 | if (!is_admin()) |
---|
1912 | { |
---|
1913 | return new PwgError(401, 'Access denied'); |
---|
1914 | } |
---|
1915 | |
---|
1916 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
---|
1917 | |
---|
1918 | $creation_output = create_virtual_category( |
---|
1919 | $params['name'], |
---|
1920 | $params['parent'] |
---|
1921 | ); |
---|
1922 | |
---|
1923 | if (isset($creation_output['error'])) |
---|
1924 | { |
---|
1925 | return new PwgError(500, $creation_output['error']); |
---|
1926 | } |
---|
1927 | |
---|
1928 | invalidate_user_cache(); |
---|
1929 | |
---|
1930 | return $creation_output; |
---|
1931 | } |
---|
1932 | |
---|
1933 | function ws_tags_add($params, &$service) |
---|
1934 | { |
---|
1935 | if (!is_admin()) |
---|
1936 | { |
---|
1937 | return new PwgError(401, 'Access denied'); |
---|
1938 | } |
---|
1939 | |
---|
1940 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
---|
1941 | |
---|
1942 | $creation_output = create_tag($params['name']); |
---|
1943 | |
---|
1944 | if (isset($creation_output['error'])) |
---|
1945 | { |
---|
1946 | return new PwgError(500, $creation_output['error']); |
---|
1947 | } |
---|
1948 | |
---|
1949 | return $creation_output; |
---|
1950 | } |
---|
1951 | |
---|
1952 | function ws_images_exist($params, &$service) |
---|
1953 | { |
---|
1954 | global $conf; |
---|
1955 | |
---|
1956 | if (!is_admin()) |
---|
1957 | { |
---|
1958 | return new PwgError(401, 'Access denied'); |
---|
1959 | } |
---|
1960 | |
---|
1961 | $split_pattern = '/[\s,;\|]/'; |
---|
1962 | |
---|
1963 | if ('md5sum' == $conf['uniqueness_mode']) |
---|
1964 | { |
---|
1965 | // search among photos the list of photos already added, based on md5sum |
---|
1966 | // list |
---|
1967 | $md5sums = preg_split( |
---|
1968 | $split_pattern, |
---|
1969 | $params['md5sum_list'], |
---|
1970 | -1, |
---|
1971 | PREG_SPLIT_NO_EMPTY |
---|
1972 | ); |
---|
1973 | |
---|
1974 | $query = ' |
---|
1975 | SELECT |
---|
1976 | id, |
---|
1977 | md5sum |
---|
1978 | FROM '.IMAGES_TABLE.' |
---|
1979 | WHERE md5sum IN (\''.implode("','", $md5sums).'\') |
---|
1980 | ;'; |
---|
1981 | $id_of_md5 = simple_hash_from_query($query, 'md5sum', 'id'); |
---|
1982 | |
---|
1983 | $result = array(); |
---|
1984 | |
---|
1985 | foreach ($md5sums as $md5sum) |
---|
1986 | { |
---|
1987 | $result[$md5sum] = null; |
---|
1988 | if (isset($id_of_md5[$md5sum])) |
---|
1989 | { |
---|
1990 | $result[$md5sum] = $id_of_md5[$md5sum]; |
---|
1991 | } |
---|
1992 | } |
---|
1993 | } |
---|
1994 | |
---|
1995 | if ('filename' == $conf['uniqueness_mode']) |
---|
1996 | { |
---|
1997 | // search among photos the list of photos already added, based on |
---|
1998 | // filename list |
---|
1999 | $filenames = preg_split( |
---|
2000 | $split_pattern, |
---|
2001 | $params['filename_list'], |
---|
2002 | -1, |
---|
2003 | PREG_SPLIT_NO_EMPTY |
---|
2004 | ); |
---|
2005 | |
---|
2006 | $query = ' |
---|
2007 | SELECT |
---|
2008 | id, |
---|
2009 | file |
---|
2010 | FROM '.IMAGES_TABLE.' |
---|
2011 | WHERE file IN (\''.implode("','", $filenames).'\') |
---|
2012 | ;'; |
---|
2013 | $id_of_filename = simple_hash_from_query($query, 'file', 'id'); |
---|
2014 | |
---|
2015 | $result = array(); |
---|
2016 | |
---|
2017 | foreach ($filenames as $filename) |
---|
2018 | { |
---|
2019 | $result[$filename] = null; |
---|
2020 | if (isset($id_of_filename[$filename])) |
---|
2021 | { |
---|
2022 | $result[$filename] = $id_of_filename[$filename]; |
---|
2023 | } |
---|
2024 | } |
---|
2025 | } |
---|
2026 | |
---|
2027 | return $result; |
---|
2028 | } |
---|
2029 | |
---|
2030 | function ws_images_checkFiles($params, &$service) |
---|
2031 | { |
---|
2032 | if (!is_admin()) |
---|
2033 | { |
---|
2034 | return new PwgError(401, 'Access denied'); |
---|
2035 | } |
---|
2036 | |
---|
2037 | // input parameters |
---|
2038 | // |
---|
2039 | // image_id |
---|
2040 | // thumbnail_sum |
---|
2041 | // file_sum |
---|
2042 | // high_sum |
---|
2043 | |
---|
2044 | $params['image_id'] = (int)$params['image_id']; |
---|
2045 | if ($params['image_id'] <= 0) |
---|
2046 | { |
---|
2047 | return new PwgError(WS_ERR_INVALID_PARAM, "Invalid image_id"); |
---|
2048 | } |
---|
2049 | |
---|
2050 | $query = ' |
---|
2051 | SELECT |
---|
2052 | path |
---|
2053 | FROM '.IMAGES_TABLE.' |
---|
2054 | WHERE id = '.$params['image_id'].' |
---|
2055 | ;'; |
---|
2056 | $result = pwg_query($query); |
---|
2057 | if (pwg_db_num_rows($result) == 0) { |
---|
2058 | return new PwgError(404, "image_id not found"); |
---|
2059 | } |
---|
2060 | list($path) = pwg_db_fetch_row($result); |
---|
2061 | |
---|
2062 | $ret = array(); |
---|
2063 | |
---|
2064 | foreach (array('thumb', 'file', 'high') as $type) { |
---|
2065 | $param_name = $type; |
---|
2066 | if ('thumb' == $type) { |
---|
2067 | $param_name = 'thumbnail'; |
---|
2068 | } |
---|
2069 | |
---|
2070 | if (isset($params[$param_name.'_sum'])) { |
---|
2071 | include_once(PHPWG_ROOT_PATH.'admin/include/functions_upload.inc.php'); |
---|
2072 | $type_path = file_path_for_type($path, $type); |
---|
2073 | if (!is_file($type_path)) { |
---|
2074 | $ret[$param_name] = 'missing'; |
---|
2075 | } |
---|
2076 | else { |
---|
2077 | if (md5_file($type_path) != $params[$param_name.'_sum']) { |
---|
2078 | $ret[$param_name] = 'differs'; |
---|
2079 | } |
---|
2080 | else { |
---|
2081 | $ret[$param_name] = 'equals'; |
---|
2082 | } |
---|
2083 | } |
---|
2084 | } |
---|
2085 | } |
---|
2086 | |
---|
2087 | return $ret; |
---|
2088 | } |
---|
2089 | |
---|
2090 | function ws_images_setInfo($params, &$service) |
---|
2091 | { |
---|
2092 | global $conf; |
---|
2093 | if (!is_admin()) |
---|
2094 | { |
---|
2095 | return new PwgError(401, 'Access denied'); |
---|
2096 | } |
---|
2097 | |
---|
2098 | if (!$service->isPost()) |
---|
2099 | { |
---|
2100 | return new PwgError(405, "This method requires HTTP POST"); |
---|
2101 | } |
---|
2102 | |
---|
2103 | $params['image_id'] = (int)$params['image_id']; |
---|
2104 | if ($params['image_id'] <= 0) |
---|
2105 | { |
---|
2106 | return new PwgError(WS_ERR_INVALID_PARAM, "Invalid image_id"); |
---|
2107 | } |
---|
2108 | |
---|
2109 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
---|
2110 | |
---|
2111 | $query=' |
---|
2112 | SELECT * |
---|
2113 | FROM '.IMAGES_TABLE.' |
---|
2114 | WHERE id = '.$params['image_id'].' |
---|
2115 | ;'; |
---|
2116 | |
---|
2117 | $image_row = pwg_db_fetch_assoc(pwg_query($query)); |
---|
2118 | if ($image_row == null) |
---|
2119 | { |
---|
2120 | return new PwgError(404, "image_id not found"); |
---|
2121 | } |
---|
2122 | |
---|
2123 | // database registration |
---|
2124 | $update = array(); |
---|
2125 | |
---|
2126 | $info_columns = array( |
---|
2127 | 'name', |
---|
2128 | 'author', |
---|
2129 | 'comment', |
---|
2130 | 'level', |
---|
2131 | 'date_creation', |
---|
2132 | ); |
---|
2133 | |
---|
2134 | foreach ($info_columns as $key) |
---|
2135 | { |
---|
2136 | if (isset($params[$key])) |
---|
2137 | { |
---|
2138 | if ('fill_if_empty' == $params['single_value_mode']) |
---|
2139 | { |
---|
2140 | if (empty($image_row[$key])) |
---|
2141 | { |
---|
2142 | $update[$key] = $params[$key]; |
---|
2143 | } |
---|
2144 | } |
---|
2145 | elseif ('replace' == $params['single_value_mode']) |
---|
2146 | { |
---|
2147 | $update[$key] = $params[$key]; |
---|
2148 | } |
---|
2149 | else |
---|
2150 | { |
---|
2151 | new PwgError( |
---|
2152 | 500, |
---|
2153 | '[ws_images_setInfo]' |
---|
2154 | .' invalid parameter single_value_mode "'.$params['single_value_mode'].'"' |
---|
2155 | .', possible values are {fill_if_empty, replace}.' |
---|
2156 | ); |
---|
2157 | exit(); |
---|
2158 | } |
---|
2159 | } |
---|
2160 | } |
---|
2161 | |
---|
2162 | if (count(array_keys($update)) > 0) |
---|
2163 | { |
---|
2164 | $update['id'] = $params['image_id']; |
---|
2165 | |
---|
2166 | mass_updates( |
---|
2167 | IMAGES_TABLE, |
---|
2168 | array( |
---|
2169 | 'primary' => array('id'), |
---|
2170 | 'update' => array_diff(array_keys($update), array('id')) |
---|
2171 | ), |
---|
2172 | array($update) |
---|
2173 | ); |
---|
2174 | } |
---|
2175 | |
---|
2176 | if (isset($params['categories'])) |
---|
2177 | { |
---|
2178 | ws_add_image_category_relations( |
---|
2179 | $params['image_id'], |
---|
2180 | $params['categories'], |
---|
2181 | ('replace' == $params['multiple_value_mode'] ? true : false) |
---|
2182 | ); |
---|
2183 | } |
---|
2184 | |
---|
2185 | // and now, let's create tag associations |
---|
2186 | if (isset($params['tag_ids'])) |
---|
2187 | { |
---|
2188 | $tag_ids = explode(',', $params['tag_ids']); |
---|
2189 | |
---|
2190 | if ('replace' == $params['multiple_value_mode']) |
---|
2191 | { |
---|
2192 | set_tags( |
---|
2193 | $tag_ids, |
---|
2194 | $params['image_id'] |
---|
2195 | ); |
---|
2196 | } |
---|
2197 | elseif ('append' == $params['multiple_value_mode']) |
---|
2198 | { |
---|
2199 | add_tags( |
---|
2200 | $tag_ids, |
---|
2201 | array($params['image_id']) |
---|
2202 | ); |
---|
2203 | } |
---|
2204 | else |
---|
2205 | { |
---|
2206 | new PwgError( |
---|
2207 | 500, |
---|
2208 | '[ws_images_setInfo]' |
---|
2209 | .' invalid parameter multiple_value_mode "'.$params['multiple_value_mode'].'"' |
---|
2210 | .', possible values are {replace, append}.' |
---|
2211 | ); |
---|
2212 | exit(); |
---|
2213 | } |
---|
2214 | } |
---|
2215 | |
---|
2216 | invalidate_user_cache(); |
---|
2217 | } |
---|
2218 | |
---|
2219 | function ws_images_delete($params, &$service) |
---|
2220 | { |
---|
2221 | global $conf; |
---|
2222 | if (!is_admin()) |
---|
2223 | { |
---|
2224 | return new PwgError(401, 'Access denied'); |
---|
2225 | } |
---|
2226 | |
---|
2227 | if (!$service->isPost()) |
---|
2228 | { |
---|
2229 | return new PwgError(405, "This method requires HTTP POST"); |
---|
2230 | } |
---|
2231 | |
---|
2232 | if (empty($params['pwg_token']) or get_pwg_token() != $params['pwg_token']) |
---|
2233 | { |
---|
2234 | return new PwgError(403, 'Invalid security token'); |
---|
2235 | } |
---|
2236 | |
---|
2237 | $params['image_id'] = preg_split( |
---|
2238 | '/[\s,;\|]/', |
---|
2239 | $params['image_id'], |
---|
2240 | -1, |
---|
2241 | PREG_SPLIT_NO_EMPTY |
---|
2242 | ); |
---|
2243 | $params['image_id'] = array_map('intval', $params['image_id']); |
---|
2244 | |
---|
2245 | $image_ids = array(); |
---|
2246 | foreach ($params['image_id'] as $image_id) |
---|
2247 | { |
---|
2248 | if ($image_id > 0) |
---|
2249 | { |
---|
2250 | array_push($image_ids, $image_id); |
---|
2251 | } |
---|
2252 | } |
---|
2253 | |
---|
2254 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
---|
2255 | delete_elements($image_ids, true); |
---|
2256 | } |
---|
2257 | |
---|
2258 | function ws_add_image_category_relations($image_id, $categories_string, $replace_mode=false) |
---|
2259 | { |
---|
2260 | // let's add links between the image and the categories |
---|
2261 | // |
---|
2262 | // $params['categories'] should look like 123,12;456,auto;789 which means: |
---|
2263 | // |
---|
2264 | // 1. associate with category 123 on rank 12 |
---|
2265 | // 2. associate with category 456 on automatic rank |
---|
2266 | // 3. associate with category 789 on automatic rank |
---|
2267 | $cat_ids = array(); |
---|
2268 | $rank_on_category = array(); |
---|
2269 | $search_current_ranks = false; |
---|
2270 | |
---|
2271 | $tokens = explode(';', $categories_string); |
---|
2272 | foreach ($tokens as $token) |
---|
2273 | { |
---|
2274 | @list($cat_id, $rank) = explode(',', $token); |
---|
2275 | |
---|
2276 | if (!preg_match('/^\d+$/', $cat_id)) |
---|
2277 | { |
---|
2278 | continue; |
---|
2279 | } |
---|
2280 | |
---|
2281 | array_push($cat_ids, $cat_id); |
---|
2282 | |
---|
2283 | if (!isset($rank)) |
---|
2284 | { |
---|
2285 | $rank = 'auto'; |
---|
2286 | } |
---|
2287 | $rank_on_category[$cat_id] = $rank; |
---|
2288 | |
---|
2289 | if ($rank == 'auto') |
---|
2290 | { |
---|
2291 | $search_current_ranks = true; |
---|
2292 | } |
---|
2293 | } |
---|
2294 | |
---|
2295 | $cat_ids = array_unique($cat_ids); |
---|
2296 | |
---|
2297 | if (count($cat_ids) == 0) |
---|
2298 | { |
---|
2299 | new PwgError( |
---|
2300 | 500, |
---|
2301 | '[ws_add_image_category_relations] there is no category defined in "'.$categories_string.'"' |
---|
2302 | ); |
---|
2303 | exit(); |
---|
2304 | } |
---|
2305 | |
---|
2306 | $query = ' |
---|
2307 | SELECT |
---|
2308 | id |
---|
2309 | FROM '.CATEGORIES_TABLE.' |
---|
2310 | WHERE id IN ('.implode(',', $cat_ids).') |
---|
2311 | ;'; |
---|
2312 | $db_cat_ids = array_from_query($query, 'id'); |
---|
2313 | |
---|
2314 | $unknown_cat_ids = array_diff($cat_ids, $db_cat_ids); |
---|
2315 | if (count($unknown_cat_ids) != 0) |
---|
2316 | { |
---|
2317 | new PwgError( |
---|
2318 | 500, |
---|
2319 | '[ws_add_image_category_relations] the following categories are unknown: '.implode(', ', $unknown_cat_ids) |
---|
2320 | ); |
---|
2321 | exit(); |
---|
2322 | } |
---|
2323 | |
---|
2324 | $to_update_cat_ids = array(); |
---|
2325 | |
---|
2326 | // in case of replace mode, we first check the existing associations |
---|
2327 | $query = ' |
---|
2328 | SELECT |
---|
2329 | category_id |
---|
2330 | FROM '.IMAGE_CATEGORY_TABLE.' |
---|
2331 | WHERE image_id = '.$image_id.' |
---|
2332 | ;'; |
---|
2333 | $existing_cat_ids = array_from_query($query, 'category_id'); |
---|
2334 | |
---|
2335 | if ($replace_mode) |
---|
2336 | { |
---|
2337 | $to_remove_cat_ids = array_diff($existing_cat_ids, $cat_ids); |
---|
2338 | if (count($to_remove_cat_ids) > 0) |
---|
2339 | { |
---|
2340 | $query = ' |
---|
2341 | DELETE |
---|
2342 | FROM '.IMAGE_CATEGORY_TABLE.' |
---|
2343 | WHERE image_id = '.$image_id.' |
---|
2344 | AND category_id IN ('.implode(', ', $to_remove_cat_ids).') |
---|
2345 | ;'; |
---|
2346 | pwg_query($query); |
---|
2347 | update_category($to_remove_cat_ids); |
---|
2348 | } |
---|
2349 | } |
---|
2350 | |
---|
2351 | $new_cat_ids = array_diff($cat_ids, $existing_cat_ids); |
---|
2352 | if (count($new_cat_ids) == 0) |
---|
2353 | { |
---|
2354 | return true; |
---|
2355 | } |
---|
2356 | |
---|
2357 | if ($search_current_ranks) |
---|
2358 | { |
---|
2359 | $query = ' |
---|
2360 | SELECT |
---|
2361 | category_id, |
---|
2362 | MAX(rank) AS max_rank |
---|
2363 | FROM '.IMAGE_CATEGORY_TABLE.' |
---|
2364 | WHERE rank IS NOT NULL |
---|
2365 | AND category_id IN ('.implode(',', $new_cat_ids).') |
---|
2366 | GROUP BY category_id |
---|
2367 | ;'; |
---|
2368 | $current_rank_of = simple_hash_from_query( |
---|
2369 | $query, |
---|
2370 | 'category_id', |
---|
2371 | 'max_rank' |
---|
2372 | ); |
---|
2373 | |
---|
2374 | foreach ($new_cat_ids as $cat_id) |
---|
2375 | { |
---|
2376 | if (!isset($current_rank_of[$cat_id])) |
---|
2377 | { |
---|
2378 | $current_rank_of[$cat_id] = 0; |
---|
2379 | } |
---|
2380 | |
---|
2381 | if ('auto' == $rank_on_category[$cat_id]) |
---|
2382 | { |
---|
2383 | $rank_on_category[$cat_id] = $current_rank_of[$cat_id] + 1; |
---|
2384 | } |
---|
2385 | } |
---|
2386 | } |
---|
2387 | |
---|
2388 | $inserts = array(); |
---|
2389 | |
---|
2390 | foreach ($new_cat_ids as $cat_id) |
---|
2391 | { |
---|
2392 | array_push( |
---|
2393 | $inserts, |
---|
2394 | array( |
---|
2395 | 'image_id' => $image_id, |
---|
2396 | 'category_id' => $cat_id, |
---|
2397 | 'rank' => $rank_on_category[$cat_id], |
---|
2398 | ) |
---|
2399 | ); |
---|
2400 | } |
---|
2401 | |
---|
2402 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
---|
2403 | mass_inserts( |
---|
2404 | IMAGE_CATEGORY_TABLE, |
---|
2405 | array_keys($inserts[0]), |
---|
2406 | $inserts |
---|
2407 | ); |
---|
2408 | |
---|
2409 | update_category($new_cat_ids); |
---|
2410 | } |
---|
2411 | |
---|
2412 | function ws_categories_setInfo($params, &$service) |
---|
2413 | { |
---|
2414 | global $conf; |
---|
2415 | if (!is_admin()) |
---|
2416 | { |
---|
2417 | return new PwgError(401, 'Access denied'); |
---|
2418 | } |
---|
2419 | |
---|
2420 | if (!$service->isPost()) |
---|
2421 | { |
---|
2422 | return new PwgError(405, "This method requires HTTP POST"); |
---|
2423 | } |
---|
2424 | |
---|
2425 | // category_id |
---|
2426 | // name |
---|
2427 | // comment |
---|
2428 | |
---|
2429 | $params['category_id'] = (int)$params['category_id']; |
---|
2430 | if ($params['category_id'] <= 0) |
---|
2431 | { |
---|
2432 | return new PwgError(WS_ERR_INVALID_PARAM, "Invalid category_id"); |
---|
2433 | } |
---|
2434 | |
---|
2435 | // database registration |
---|
2436 | $update = array( |
---|
2437 | 'id' => $params['category_id'], |
---|
2438 | ); |
---|
2439 | |
---|
2440 | $info_columns = array( |
---|
2441 | 'name', |
---|
2442 | 'comment', |
---|
2443 | ); |
---|
2444 | |
---|
2445 | $perform_update = false; |
---|
2446 | foreach ($info_columns as $key) |
---|
2447 | { |
---|
2448 | if (isset($params[$key])) |
---|
2449 | { |
---|
2450 | $perform_update = true; |
---|
2451 | $update[$key] = $params[$key]; |
---|
2452 | } |
---|
2453 | } |
---|
2454 | |
---|
2455 | if ($perform_update) |
---|
2456 | { |
---|
2457 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
---|
2458 | mass_updates( |
---|
2459 | CATEGORIES_TABLE, |
---|
2460 | array( |
---|
2461 | 'primary' => array('id'), |
---|
2462 | 'update' => array_diff(array_keys($update), array('id')) |
---|
2463 | ), |
---|
2464 | array($update) |
---|
2465 | ); |
---|
2466 | } |
---|
2467 | |
---|
2468 | } |
---|
2469 | |
---|
2470 | function ws_categories_delete($params, &$service) |
---|
2471 | { |
---|
2472 | global $conf; |
---|
2473 | if (!is_admin()) |
---|
2474 | { |
---|
2475 | return new PwgError(401, 'Access denied'); |
---|
2476 | } |
---|
2477 | |
---|
2478 | if (!$service->isPost()) |
---|
2479 | { |
---|
2480 | return new PwgError(405, "This method requires HTTP POST"); |
---|
2481 | } |
---|
2482 | |
---|
2483 | if (empty($params['pwg_token']) or get_pwg_token() != $params['pwg_token']) |
---|
2484 | { |
---|
2485 | return new PwgError(403, 'Invalid security token'); |
---|
2486 | } |
---|
2487 | |
---|
2488 | $modes = array('no_delete', 'delete_orphans', 'force_delete'); |
---|
2489 | if (!in_array($params['photo_deletion_mode'], $modes)) |
---|
2490 | { |
---|
2491 | return new PwgError( |
---|
2492 | 500, |
---|
2493 | '[ws_categories_delete]' |
---|
2494 | .' invalid parameter photo_deletion_mode "'.$params['photo_deletion_mode'].'"' |
---|
2495 | .', possible values are {'.implode(', ', $modes).'}.' |
---|
2496 | ); |
---|
2497 | } |
---|
2498 | |
---|
2499 | $params['category_id'] = preg_split( |
---|
2500 | '/[\s,;\|]/', |
---|
2501 | $params['category_id'], |
---|
2502 | -1, |
---|
2503 | PREG_SPLIT_NO_EMPTY |
---|
2504 | ); |
---|
2505 | $params['category_id'] = array_map('intval', $params['category_id']); |
---|
2506 | |
---|
2507 | $category_ids = array(); |
---|
2508 | foreach ($params['category_id'] as $category_id) |
---|
2509 | { |
---|
2510 | if ($category_id > 0) |
---|
2511 | { |
---|
2512 | array_push($category_ids, $category_id); |
---|
2513 | } |
---|
2514 | } |
---|
2515 | |
---|
2516 | if (count($category_ids) == 0) |
---|
2517 | { |
---|
2518 | return; |
---|
2519 | } |
---|
2520 | |
---|
2521 | $query = ' |
---|
2522 | SELECT id |
---|
2523 | FROM '.CATEGORIES_TABLE.' |
---|
2524 | WHERE id IN ('.implode(',', $category_ids).') |
---|
2525 | ;'; |
---|
2526 | $category_ids = array_from_query($query, 'id'); |
---|
2527 | |
---|
2528 | if (count($category_ids) == 0) |
---|
2529 | { |
---|
2530 | return; |
---|
2531 | } |
---|
2532 | |
---|
2533 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
---|
2534 | delete_categories($category_ids, $params['photo_deletion_mode']); |
---|
2535 | update_global_rank(); |
---|
2536 | } |
---|
2537 | |
---|
2538 | function ws_categories_move($params, &$service) |
---|
2539 | { |
---|
2540 | global $conf, $page; |
---|
2541 | |
---|
2542 | if (!is_admin()) |
---|
2543 | { |
---|
2544 | return new PwgError(401, 'Access denied'); |
---|
2545 | } |
---|
2546 | |
---|
2547 | if (!$service->isPost()) |
---|
2548 | { |
---|
2549 | return new PwgError(405, "This method requires HTTP POST"); |
---|
2550 | } |
---|
2551 | |
---|
2552 | if (empty($params['pwg_token']) or get_pwg_token() != $params['pwg_token']) |
---|
2553 | { |
---|
2554 | return new PwgError(403, 'Invalid security token'); |
---|
2555 | } |
---|
2556 | |
---|
2557 | $params['category_id'] = preg_split( |
---|
2558 | '/[\s,;\|]/', |
---|
2559 | $params['category_id'], |
---|
2560 | -1, |
---|
2561 | PREG_SPLIT_NO_EMPTY |
---|
2562 | ); |
---|
2563 | $params['category_id'] = array_map('intval', $params['category_id']); |
---|
2564 | |
---|
2565 | $category_ids = array(); |
---|
2566 | foreach ($params['category_id'] as $category_id) |
---|
2567 | { |
---|
2568 | if ($category_id > 0) |
---|
2569 | { |
---|
2570 | array_push($category_ids, $category_id); |
---|
2571 | } |
---|
2572 | } |
---|
2573 | |
---|
2574 | if (count($category_ids) == 0) |
---|
2575 | { |
---|
2576 | return new PwgError(403, 'Invalid category_id input parameter, no category to move'); |
---|
2577 | } |
---|
2578 | |
---|
2579 | // we can't move physical categories |
---|
2580 | $categories_in_db = array(); |
---|
2581 | |
---|
2582 | $query = ' |
---|
2583 | SELECT |
---|
2584 | id, |
---|
2585 | name, |
---|
2586 | dir |
---|
2587 | FROM '.CATEGORIES_TABLE.' |
---|
2588 | WHERE id IN ('.implode(',', $category_ids).') |
---|
2589 | ;'; |
---|
2590 | $result = pwg_query($query); |
---|
2591 | while ($row = pwg_db_fetch_assoc($result)) |
---|
2592 | { |
---|
2593 | $categories_in_db[$row['id']] = $row; |
---|
2594 | // we break on error at first physical category detected |
---|
2595 | if (!empty($row['dir'])) |
---|
2596 | { |
---|
2597 | $row['name'] = strip_tags( |
---|
2598 | trigger_event( |
---|
2599 | 'render_category_name', |
---|
2600 | $row['name'], |
---|
2601 | 'ws_categories_move' |
---|
2602 | ) |
---|
2603 | ); |
---|
2604 | |
---|
2605 | return new PwgError( |
---|
2606 | 403, |
---|
2607 | sprintf( |
---|
2608 | 'Category %s (%u) is not a virtual category, you cannot move it', |
---|
2609 | $row['name'], |
---|
2610 | $row['id'] |
---|
2611 | ) |
---|
2612 | ); |
---|
2613 | } |
---|
2614 | } |
---|
2615 | |
---|
2616 | if (count($categories_in_db) != count($category_ids)) |
---|
2617 | { |
---|
2618 | $unknown_category_ids = array_diff($category_ids, array_keys($categories_in_db)); |
---|
2619 | |
---|
2620 | return new PwgError( |
---|
2621 | 403, |
---|
2622 | sprintf( |
---|
2623 | 'Category %u does not exist', |
---|
2624 | $unknown_category_ids[0] |
---|
2625 | ) |
---|
2626 | ); |
---|
2627 | } |
---|
2628 | |
---|
2629 | // does this parent exists? This check should be made in the |
---|
2630 | // move_categories function, not here |
---|
2631 | // |
---|
2632 | // 0 as parent means "move categories at gallery root" |
---|
2633 | if (!is_numeric($params['parent'])) |
---|
2634 | { |
---|
2635 | return new PwgError(403, 'Invalid parent input parameter'); |
---|
2636 | } |
---|
2637 | |
---|
2638 | if (0 != $params['parent']) { |
---|
2639 | $params['parent'] = intval($params['parent']); |
---|
2640 | $subcat_ids = get_subcat_ids(array($params['parent'])); |
---|
2641 | if (count($subcat_ids) == 0) |
---|
2642 | { |
---|
2643 | return new PwgError(403, 'Unknown parent category id'); |
---|
2644 | } |
---|
2645 | } |
---|
2646 | |
---|
2647 | $page['infos'] = array(); |
---|
2648 | $page['errors'] = array(); |
---|
2649 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
---|
2650 | move_categories($category_ids, $params['parent']); |
---|
2651 | invalidate_user_cache(); |
---|
2652 | |
---|
2653 | if (count($page['errors']) != 0) |
---|
2654 | { |
---|
2655 | return new PwgError(403, implode('; ', $page['errors'])); |
---|
2656 | } |
---|
2657 | } |
---|
2658 | |
---|
2659 | function ws_logfile($string) |
---|
2660 | { |
---|
2661 | global $conf; |
---|
2662 | |
---|
2663 | if (!$conf['ws_enable_log']) { |
---|
2664 | return true; |
---|
2665 | } |
---|
2666 | |
---|
2667 | file_put_contents( |
---|
2668 | $conf['ws_log_filepath'], |
---|
2669 | '['.date('c').'] '.$string."\n", |
---|
2670 | FILE_APPEND |
---|
2671 | ); |
---|
2672 | } |
---|
2673 | |
---|
2674 | function ws_images_checkUpload($params, &$service) |
---|
2675 | { |
---|
2676 | global $conf; |
---|
2677 | |
---|
2678 | if (!is_admin()) |
---|
2679 | { |
---|
2680 | return new PwgError(401, 'Access denied'); |
---|
2681 | } |
---|
2682 | |
---|
2683 | include_once(PHPWG_ROOT_PATH.'admin/include/functions_upload.inc.php'); |
---|
2684 | $ret['message'] = ready_for_upload_message(); |
---|
2685 | $ret['ready_for_upload'] = true; |
---|
2686 | |
---|
2687 | if (!empty($ret['message'])) |
---|
2688 | { |
---|
2689 | $ret['ready_for_upload'] = false; |
---|
2690 | } |
---|
2691 | |
---|
2692 | return $ret; |
---|
2693 | } |
---|
2694 | |
---|
2695 | function ws_plugins_getList($params, &$service) |
---|
2696 | { |
---|
2697 | global $conf; |
---|
2698 | |
---|
2699 | if (!is_admin()) |
---|
2700 | { |
---|
2701 | return new PwgError(401, 'Access denied'); |
---|
2702 | } |
---|
2703 | |
---|
2704 | include_once(PHPWG_ROOT_PATH.'admin/include/plugins.class.php'); |
---|
2705 | $plugins = new plugins(); |
---|
2706 | $plugins->sort_fs_plugins('name'); |
---|
2707 | $plugin_list = array(); |
---|
2708 | |
---|
2709 | foreach($plugins->fs_plugins as $plugin_id => $fs_plugin) |
---|
2710 | { |
---|
2711 | if (isset($plugins->db_plugins_by_id[$plugin_id])) |
---|
2712 | { |
---|
2713 | $state = $plugins->db_plugins_by_id[$plugin_id]['state']; |
---|
2714 | } |
---|
2715 | else |
---|
2716 | { |
---|
2717 | $state = 'uninstalled'; |
---|
2718 | } |
---|
2719 | |
---|
2720 | array_push( |
---|
2721 | $plugin_list, |
---|
2722 | array( |
---|
2723 | 'id' => $plugin_id, |
---|
2724 | 'name' => $fs_plugin['name'], |
---|
2725 | 'version' => $fs_plugin['version'], |
---|
2726 | 'state' => $state, |
---|
2727 | 'description' => $fs_plugin['description'], |
---|
2728 | ) |
---|
2729 | ); |
---|
2730 | } |
---|
2731 | |
---|
2732 | return $plugin_list; |
---|
2733 | } |
---|
2734 | |
---|
2735 | function ws_plugins_performAction($params, &$service) |
---|
2736 | { |
---|
2737 | global $template; |
---|
2738 | |
---|
2739 | if (!is_admin()) |
---|
2740 | { |
---|
2741 | return new PwgError(401, 'Access denied'); |
---|
2742 | } |
---|
2743 | |
---|
2744 | if (empty($params['pwg_token']) or get_pwg_token() != $params['pwg_token']) |
---|
2745 | { |
---|
2746 | return new PwgError(403, 'Invalid security token'); |
---|
2747 | } |
---|
2748 | |
---|
2749 | define('IN_ADMIN', true); |
---|
2750 | include_once(PHPWG_ROOT_PATH.'admin/include/plugins.class.php'); |
---|
2751 | $plugins = new plugins(); |
---|
2752 | $errors = $plugins->perform_action($params['action'], $params['plugin']); |
---|
2753 | |
---|
2754 | |
---|
2755 | if (!empty($errors)) |
---|
2756 | { |
---|
2757 | return new PwgError(500, $errors); |
---|
2758 | } |
---|
2759 | else |
---|
2760 | { |
---|
2761 | if (in_array($params['action'], array('activate', 'deactivate'))) |
---|
2762 | { |
---|
2763 | $template->delete_compiled_templates(); |
---|
2764 | } |
---|
2765 | return true; |
---|
2766 | } |
---|
2767 | } |
---|
2768 | |
---|
2769 | function ws_themes_performAction($params, &$service) |
---|
2770 | { |
---|
2771 | global $template; |
---|
2772 | |
---|
2773 | if (!is_admin()) |
---|
2774 | { |
---|
2775 | return new PwgError(401, 'Access denied'); |
---|
2776 | } |
---|
2777 | |
---|
2778 | if (empty($params['pwg_token']) or get_pwg_token() != $params['pwg_token']) |
---|
2779 | { |
---|
2780 | return new PwgError(403, 'Invalid security token'); |
---|
2781 | } |
---|
2782 | |
---|
2783 | define('IN_ADMIN', true); |
---|
2784 | include_once(PHPWG_ROOT_PATH.'admin/include/themes.class.php'); |
---|
2785 | $themes = new themes(); |
---|
2786 | $errors = $themes->perform_action($params['action'], $params['theme']); |
---|
2787 | |
---|
2788 | if (!empty($errors)) |
---|
2789 | { |
---|
2790 | return new PwgError(500, $errors); |
---|
2791 | } |
---|
2792 | else |
---|
2793 | { |
---|
2794 | if (in_array($params['action'], array('activate', 'deactivate'))) |
---|
2795 | { |
---|
2796 | $template->delete_compiled_templates(); |
---|
2797 | } |
---|
2798 | return true; |
---|
2799 | } |
---|
2800 | } |
---|
2801 | |
---|
2802 | function ws_images_resizethumbnail($params, &$service) |
---|
2803 | { |
---|
2804 | if (!is_admin()) |
---|
2805 | { |
---|
2806 | return new PwgError(401, 'Access denied'); |
---|
2807 | } |
---|
2808 | |
---|
2809 | if (empty($params['image_id']) and empty($params['image_path'])) |
---|
2810 | { |
---|
2811 | return new PwgError(403, "image_id or image_path is missing"); |
---|
2812 | } |
---|
2813 | |
---|
2814 | include_once(PHPWG_ROOT_PATH.'admin/include/functions_upload.inc.php'); |
---|
2815 | include_once(PHPWG_ROOT_PATH.'admin/include/image.class.php'); |
---|
2816 | |
---|
2817 | if (!empty($params['image_id'])) |
---|
2818 | { |
---|
2819 | $query=' |
---|
2820 | SELECT id, path, tn_ext, has_high |
---|
2821 | FROM '.IMAGES_TABLE.' |
---|
2822 | WHERE id = '.(int)$params['image_id'].' |
---|
2823 | ;'; |
---|
2824 | $image = pwg_db_fetch_assoc(pwg_query($query)); |
---|
2825 | |
---|
2826 | if ($image == null) |
---|
2827 | { |
---|
2828 | return new PwgError(403, "image_id not found"); |
---|
2829 | } |
---|
2830 | |
---|
2831 | $image_path = $image['path']; |
---|
2832 | $thumb_path = get_thumbnail_path($image); |
---|
2833 | } |
---|
2834 | else |
---|
2835 | { |
---|
2836 | $image_path = $params['image_path']; |
---|
2837 | $thumb_path = file_path_for_type($image_path, 'thumb'); |
---|
2838 | } |
---|
2839 | |
---|
2840 | if (!file_exists($image_path) or !is_valid_image_extension(get_extension($image_path))) |
---|
2841 | { |
---|
2842 | return new PwgError(403, "image can't be resized"); |
---|
2843 | } |
---|
2844 | |
---|
2845 | $result = false; |
---|
2846 | prepare_directory(dirname($thumb_path)); |
---|
2847 | $img = new pwg_image($image_path, $params['library']); |
---|
2848 | |
---|
2849 | $result = $img->pwg_resize( |
---|
2850 | $thumb_path, |
---|
2851 | $params['maxwidth'], |
---|
2852 | $params['maxheight'], |
---|
2853 | $params['quality'], |
---|
2854 | false, // automatic rotation is not needed for thumbnails. |
---|
2855 | true, // strip metadata |
---|
2856 | get_boolean($params['crop']), |
---|
2857 | get_boolean($params['follow_orientation']) |
---|
2858 | ); |
---|
2859 | |
---|
2860 | $img->destroy(); |
---|
2861 | return $result; |
---|
2862 | } |
---|
2863 | |
---|
2864 | function ws_images_resizewebsize($params, &$service) |
---|
2865 | { |
---|
2866 | if (!is_admin()) |
---|
2867 | { |
---|
2868 | return new PwgError(401, 'Access denied'); |
---|
2869 | } |
---|
2870 | |
---|
2871 | include_once(PHPWG_ROOT_PATH.'include/functions_picture.inc.php'); |
---|
2872 | include_once(PHPWG_ROOT_PATH.'admin/include/functions_upload.inc.php'); |
---|
2873 | include_once(PHPWG_ROOT_PATH.'admin/include/image.class.php'); |
---|
2874 | |
---|
2875 | $query=' |
---|
2876 | SELECT id, path, tn_ext, has_high |
---|
2877 | FROM '.IMAGES_TABLE.' |
---|
2878 | WHERE id = '.(int)$params['image_id'].' |
---|
2879 | ;'; |
---|
2880 | $image = pwg_db_fetch_assoc(pwg_query($query)); |
---|
2881 | |
---|
2882 | if ($image == null) |
---|
2883 | { |
---|
2884 | return new PwgError(403, "image_id not found"); |
---|
2885 | } |
---|
2886 | |
---|
2887 | $image_path = $image['path']; |
---|
2888 | $hd_path = get_high_path($image); |
---|
2889 | |
---|
2890 | if (empty($image['has_high']) or !file_exists($hd_path) or !is_valid_image_extension(get_extension($image_path))) |
---|
2891 | { |
---|
2892 | return new PwgError(403, "image can't be resized"); |
---|
2893 | } |
---|
2894 | |
---|
2895 | $result = false; |
---|
2896 | $img = new pwg_image($hd_path, $params['library']); |
---|
2897 | |
---|
2898 | $result = $img->pwg_resize( |
---|
2899 | $image_path, |
---|
2900 | $params['maxwidth'], |
---|
2901 | $params['maxheight'], |
---|
2902 | $params['quality'], |
---|
2903 | $params['automatic_rotation'], |
---|
2904 | false // strip metadata |
---|
2905 | ); |
---|
2906 | |
---|
2907 | $img->destroy(); |
---|
2908 | |
---|
2909 | global $conf; |
---|
2910 | $conf['use_exif'] = false; |
---|
2911 | $conf['use_iptc'] = false; |
---|
2912 | update_metadata(array($image['id'] => $image['path'])); |
---|
2913 | |
---|
2914 | return $result; |
---|
2915 | } |
---|
2916 | |
---|
2917 | function ws_extensions_update($params, &$service) |
---|
2918 | { |
---|
2919 | if (!is_webmaster()) |
---|
2920 | { |
---|
2921 | return new PwgError(401, l10n('Webmaster status is required.')); |
---|
2922 | } |
---|
2923 | |
---|
2924 | if (empty($params['pwg_token']) or get_pwg_token() != $params['pwg_token']) |
---|
2925 | { |
---|
2926 | return new PwgError(403, 'Invalid security token'); |
---|
2927 | } |
---|
2928 | |
---|
2929 | if (empty($params['type']) or !in_array($params['type'], array('plugins', 'themes', 'languages'))) |
---|
2930 | { |
---|
2931 | return new PwgError(403, "invalid extension type"); |
---|
2932 | } |
---|
2933 | |
---|
2934 | if (empty($params['id']) or empty($params['revision'])) |
---|
2935 | { |
---|
2936 | return new PwgError(null, 'Wrong parameters'); |
---|
2937 | } |
---|
2938 | |
---|
2939 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
---|
2940 | include_once(PHPWG_ROOT_PATH.'admin/include/'.$params['type'].'.class.php'); |
---|
2941 | |
---|
2942 | $type = $params['type']; |
---|
2943 | $extension_id = $params['id']; |
---|
2944 | $revision = $params['revision']; |
---|
2945 | |
---|
2946 | $extension = new $type(); |
---|
2947 | |
---|
2948 | if ($type == 'plugins') |
---|
2949 | { |
---|
2950 | if (isset($extension->db_plugins_by_id[$extension_id]) and $extension->db_plugins_by_id[$extension_id]['state'] == 'active') |
---|
2951 | { |
---|
2952 | $extension->perform_action('deactivate', $extension_id); |
---|
2953 | |
---|
2954 | redirect(PHPWG_ROOT_PATH |
---|
2955 | . 'ws.php' |
---|
2956 | . '?method=pwg.extensions.update' |
---|
2957 | . '&type=plugins' |
---|
2958 | . '&id=' . $extension_id |
---|
2959 | . '&revision=' . $revision |
---|
2960 | . '&reactivate=true' |
---|
2961 | . '&pwg_token=' . get_pwg_token() |
---|
2962 | . '&format=json' |
---|
2963 | ); |
---|
2964 | } |
---|
2965 | |
---|
2966 | $upgrade_status = $extension->extract_plugin_files('upgrade', $revision, $extension_id); |
---|
2967 | $extension_name = $extension->fs_plugins[$extension_id]['name']; |
---|
2968 | |
---|
2969 | if (isset($params['reactivate'])) |
---|
2970 | { |
---|
2971 | $extension->perform_action('activate', $extension_id); |
---|
2972 | } |
---|
2973 | } |
---|
2974 | elseif ($type == 'themes') |
---|
2975 | { |
---|
2976 | $upgrade_status = $extension->extract_theme_files('upgrade', $revision, $extension_id); |
---|
2977 | $extension_name = $extension->fs_themes[$extension_id]['name']; |
---|
2978 | } |
---|
2979 | elseif ($type == 'languages') |
---|
2980 | { |
---|
2981 | $upgrade_status = $extension->extract_language_files('upgrade', $revision, $extension_id); |
---|
2982 | $extension_name = $extension->fs_languages[$extension_id]['name']; |
---|
2983 | } |
---|
2984 | |
---|
2985 | global $template; |
---|
2986 | $template->delete_compiled_templates(); |
---|
2987 | |
---|
2988 | switch ($upgrade_status) |
---|
2989 | { |
---|
2990 | case 'ok': |
---|
2991 | return sprintf(l10n('%s has been successfully updated.'), $extension_name); |
---|
2992 | |
---|
2993 | case 'temp_path_error': |
---|
2994 | return new PwgError(null, l10n('Can\'t create temporary file.')); |
---|
2995 | |
---|
2996 | case 'dl_archive_error': |
---|
2997 | return new PwgError(null, l10n('Can\'t download archive.')); |
---|
2998 | |
---|
2999 | case 'archive_error': |
---|
3000 | return new PwgError(null, l10n('Can\'t read or extract archive.')); |
---|
3001 | |
---|
3002 | default: |
---|
3003 | return new PwgError(null, sprintf(l10n('An error occured during extraction (%s).'), $upgrade_status)); |
---|
3004 | } |
---|
3005 | } |
---|
3006 | |
---|
3007 | function ws_extensions_ignoreupdate($params, &$service) |
---|
3008 | { |
---|
3009 | global $conf; |
---|
3010 | |
---|
3011 | define('IN_ADMIN', true); |
---|
3012 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
---|
3013 | |
---|
3014 | if (!is_webmaster()) |
---|
3015 | { |
---|
3016 | return new PwgError(401, 'Access denied'); |
---|
3017 | } |
---|
3018 | |
---|
3019 | if (empty($params['pwg_token']) or get_pwg_token() != $params['pwg_token']) |
---|
3020 | { |
---|
3021 | return new PwgError(403, 'Invalid security token'); |
---|
3022 | } |
---|
3023 | |
---|
3024 | $conf['updates_ignored'] = unserialize($conf['updates_ignored']); |
---|
3025 | |
---|
3026 | // Reset ignored extension |
---|
3027 | if ($params['reset']) |
---|
3028 | { |
---|
3029 | if (!empty($params['type']) and isset($conf['updates_ignored'][$params['type']])) |
---|
3030 | { |
---|
3031 | $conf['updates_ignored'][$params['type']] = array(); |
---|
3032 | } |
---|
3033 | else |
---|
3034 | { |
---|
3035 | $conf['updates_ignored'] = array( |
---|
3036 | 'plugins'=>array(), |
---|
3037 | 'themes'=>array(), |
---|
3038 | 'languages'=>array() |
---|
3039 | ); |
---|
3040 | } |
---|
3041 | conf_update_param('updates_ignored', pwg_db_real_escape_string(serialize($conf['updates_ignored']))); |
---|
3042 | unset($_SESSION['extensions_need_update']); |
---|
3043 | return true; |
---|
3044 | } |
---|
3045 | |
---|
3046 | if (empty($params['id']) or empty($params['type']) or !in_array($params['type'], array('plugins', 'themes', 'languages'))) |
---|
3047 | { |
---|
3048 | return new PwgError(403, 'Invalid parameters'); |
---|
3049 | } |
---|
3050 | |
---|
3051 | // Add or remove extension from ignore list |
---|
3052 | if (!in_array($params['id'], $conf['updates_ignored'][$params['type']])) |
---|
3053 | { |
---|
3054 | array_push($conf['updates_ignored'][$params['type']], $params['id']); |
---|
3055 | } |
---|
3056 | conf_update_param('updates_ignored', pwg_db_real_escape_string(serialize($conf['updates_ignored']))); |
---|
3057 | unset($_SESSION['extensions_need_update']); |
---|
3058 | return true; |
---|
3059 | } |
---|
3060 | |
---|
3061 | function ws_extensions_checkupdates($params, &$service) |
---|
3062 | { |
---|
3063 | global $conf; |
---|
3064 | |
---|
3065 | define('IN_ADMIN', true); |
---|
3066 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
---|
3067 | include_once(PHPWG_ROOT_PATH.'admin/include/updates.class.php'); |
---|
3068 | $update = new updates(); |
---|
3069 | |
---|
3070 | if (!is_admin()) |
---|
3071 | { |
---|
3072 | return new PwgError(401, 'Access denied'); |
---|
3073 | } |
---|
3074 | |
---|
3075 | $result = array(); |
---|
3076 | |
---|
3077 | if (!isset($_SESSION['need_update'])) |
---|
3078 | $update->check_piwigo_upgrade(); |
---|
3079 | |
---|
3080 | $result['piwigo_need_update'] = $_SESSION['need_update']; |
---|
3081 | |
---|
3082 | $conf['updates_ignored'] = unserialize($conf['updates_ignored']); |
---|
3083 | |
---|
3084 | if (!isset($_SESSION['extensions_need_update'])) |
---|
3085 | $update->check_extensions(); |
---|
3086 | else |
---|
3087 | $update->check_updated_extensions(); |
---|
3088 | |
---|
3089 | if (!is_array($_SESSION['extensions_need_update'])) |
---|
3090 | $result['ext_need_update'] = null; |
---|
3091 | else |
---|
3092 | $result['ext_need_update'] = !empty($_SESSION['extensions_need_update']); |
---|
3093 | |
---|
3094 | return $result; |
---|
3095 | } |
---|
3096 | ?> |
---|