source: trunk/include/ws_functions/pwg.images.php @ 31102

Last change on this file since 31102 was 31102, checked in by mistic100, 9 years ago

feature 3221 Add Logger class

File size: 41.3 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based photo gallery                                    |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2014 Piwigo Team                  http://piwigo.org |
6// | Copyright(C) 2003-2008 PhpWebGallery Team    http://phpwebgallery.net |
7// | Copyright(C) 2002-2003 Pierrick LE GALL   http://le-gall.net/pierrick |
8// +-----------------------------------------------------------------------+
9// | This program is free software; you can redistribute it and/or modify  |
10// | it under the terms of the GNU General Public License as published by  |
11// | the Free Software Foundation                                          |
12// |                                                                       |
13// | This program is distributed in the hope that it will be useful, but   |
14// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
15// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
16// | General Public License for more details.                              |
17// |                                                                       |
18// | You should have received a copy of the GNU General Public License     |
19// | along with this program; if not, write to the Free Software           |
20// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
21// | USA.                                                                  |
22// +-----------------------------------------------------------------------+
23
24// +-----------------------------------------------------------------------+
25// | UTILITIES                                                             |
26// +-----------------------------------------------------------------------+
27
28/**
29 * Sets associations of an image
30 * @param int $image_id
31 * @param string $categories_string - "cat_id[,rank];cat_id[,rank]"
32 * @param bool $replace_mode - removes old associations
33 */
34function ws_add_image_category_relations($image_id, $categories_string, $replace_mode=false)
35{
36  // let's add links between the image and the categories
37  //
38  // $params['categories'] should look like 123,12;456,auto;789 which means:
39  //
40  // 1. associate with category 123 on rank 12
41  // 2. associate with category 456 on automatic rank
42  // 3. associate with category 789 on automatic rank
43  $cat_ids = array();
44  $rank_on_category = array();
45  $search_current_ranks = false;
46
47  $tokens = explode(';', $categories_string);
48  foreach ($tokens as $token)
49  {
50    @list($cat_id, $rank) = explode(',', $token);
51
52    if (!preg_match('/^\d+$/', $cat_id))
53    {
54      continue;
55    }
56
57    $cat_ids[] = $cat_id;
58
59    if (!isset($rank))
60    {
61      $rank = 'auto';
62    }
63    $rank_on_category[$cat_id] = $rank;
64
65    if ($rank == 'auto')
66    {
67      $search_current_ranks = true;
68    }
69  }
70
71  $cat_ids = array_unique($cat_ids);
72
73  if (count($cat_ids) == 0)
74  {
75    return new PwgError(500,
76      '[ws_add_image_category_relations] there is no category defined in "'.$categories_string.'"'
77      );
78  }
79
80  $query = '
81SELECT id
82  FROM '.CATEGORIES_TABLE.'
83  WHERE id IN ('.implode(',', $cat_ids).')
84;';
85  $db_cat_ids = query2array($query, null, 'id');
86
87  $unknown_cat_ids = array_diff($cat_ids, $db_cat_ids);
88  if (count($unknown_cat_ids) != 0)
89  {
90    return new PwgError(500,
91      '[ws_add_image_category_relations] the following categories are unknown: '.implode(', ', $unknown_cat_ids)
92      );
93  }
94
95  $to_update_cat_ids = array();
96
97  // in case of replace mode, we first check the existing associations
98  $query = '
99SELECT category_id
100  FROM '.IMAGE_CATEGORY_TABLE.'
101  WHERE image_id = '.$image_id.'
102;';
103  $existing_cat_ids = query2array($query, null, 'category_id');
104
105  if ($replace_mode)
106  {
107    $to_remove_cat_ids = array_diff($existing_cat_ids, $cat_ids);
108    if (count($to_remove_cat_ids) > 0)
109    {
110      $query = '
111DELETE
112  FROM '.IMAGE_CATEGORY_TABLE.'
113  WHERE image_id = '.$image_id.'
114    AND category_id IN ('.implode(', ', $to_remove_cat_ids).')
115;';
116      pwg_query($query);
117      update_category($to_remove_cat_ids);
118    }
119  }
120
121  $new_cat_ids = array_diff($cat_ids, $existing_cat_ids);
122  if (count($new_cat_ids) == 0)
123  {
124    return true;
125  }
126
127  if ($search_current_ranks)
128  {
129    $query = '
130SELECT category_id, MAX(rank) AS max_rank
131  FROM '.IMAGE_CATEGORY_TABLE.'
132  WHERE rank IS NOT NULL
133    AND category_id IN ('.implode(',', $new_cat_ids).')
134  GROUP BY category_id
135;';
136    $current_rank_of = query2array(
137      $query,
138      'category_id',
139      'max_rank'
140      );
141
142    foreach ($new_cat_ids as $cat_id)
143    {
144      if (!isset($current_rank_of[$cat_id]))
145      {
146        $current_rank_of[$cat_id] = 0;
147      }
148
149      if ('auto' == $rank_on_category[$cat_id])
150      {
151        $rank_on_category[$cat_id] = $current_rank_of[$cat_id] + 1;
152      }
153    }
154  }
155
156  $inserts = array();
157
158  foreach ($new_cat_ids as $cat_id)
159  {
160    $inserts[] = array(
161      'image_id' => $image_id,
162      'category_id' => $cat_id,
163      'rank' => $rank_on_category[$cat_id],
164      );
165  }
166
167  mass_inserts(
168    IMAGE_CATEGORY_TABLE,
169    array_keys($inserts[0]),
170    $inserts
171    );
172
173  include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
174  update_category($new_cat_ids);
175}
176
177/**
178 * Merge chunks added by pwg.images.addChunk
179 * @param string $output_filepath
180 * @param string $original_sum
181 * @param string $type
182 */
183function merge_chunks($output_filepath, $original_sum, $type)
184{
185  global $conf, $logger;
186
187  $logger->debug('[merge_chunks] input parameter $output_filepath : '.$output_filepath, 'WS');
188
189  if (is_file($output_filepath))
190  {
191    unlink($output_filepath);
192
193    if (is_file($output_filepath))
194    {
195      return new PwgError(500, '[merge_chunks] error while trying to remove existing '.$output_filepath);
196    }
197  }
198
199  $upload_dir = $conf['upload_dir'].'/buffer';
200  $pattern = '/'.$original_sum.'-'.$type.'/';
201  $chunks = array();
202
203  if ($handle = opendir($upload_dir))
204  {
205    while (false !== ($file = readdir($handle)))
206    {
207      if (preg_match($pattern, $file))
208      {
209        $logger->debug($file, 'WS');
210        $chunks[] = $upload_dir.'/'.$file;
211      }
212    }
213    closedir($handle);
214  }
215
216  sort($chunks);
217
218  if (function_exists('memory_get_usage')) {
219    $logger->debug('[merge_chunks] memory_get_usage before loading chunks: '.memory_get_usage(), 'WS');
220  }
221
222  $i = 0;
223
224  foreach ($chunks as $chunk)
225  {
226    $string = file_get_contents($chunk);
227
228    if (function_exists('memory_get_usage')) {
229      $logger->debug('[merge_chunks] memory_get_usage on chunk '.++$i.': '.memory_get_usage(), 'WS');
230    }
231
232    if (!file_put_contents($output_filepath, $string, FILE_APPEND))
233    {
234      return new PwgError(500, '[merge_chunks] error while writting chunks for '.$output_filepath);
235    }
236
237    unlink($chunk);
238  }
239
240  if (function_exists('memory_get_usage')) {
241    $logger->debug('[merge_chunks] memory_get_usage after loading chunks: '.memory_get_usage(), 'WS');
242  }
243}
244
245/**
246 * Deletes chunks added with pwg.images.addChunk
247 * @param string $original_sum
248 * @param string $type
249 *
250 * Function introduced for Piwigo 2.4 and the new "multiple size"
251 * (derivatives) feature. As we only need the biggest sent photo as
252 * "original", we remove chunks for smaller sizes. We can't make it earlier
253 * in ws_images_add_chunk because at this moment we don't know which $type
254 * will be the biggest (we could remove the thumb, but let's use the same
255 * algorithm)
256 */
257function remove_chunks($original_sum, $type)
258{
259  global $conf;
260
261  $upload_dir = $conf['upload_dir'].'/buffer';
262  $pattern = '/'.$original_sum.'-'.$type.'/';
263  $chunks = array();
264
265  if ($handle = opendir($upload_dir))
266  {
267    while (false !== ($file = readdir($handle)))
268    {
269      if (preg_match($pattern, $file))
270      {
271        $chunks[] = $upload_dir.'/'.$file;
272      }
273    }
274    closedir($handle);
275  }
276
277  foreach ($chunks as $chunk)
278  {
279    unlink($chunk);
280  }
281}
282
283
284// +-----------------------------------------------------------------------+
285// | METHODS                                                               |
286// +-----------------------------------------------------------------------+
287
288/**
289 * API method
290 * Adds a comment to an image
291 * @param mixed[] $params
292 *    @option int image_id
293 *    @option string author
294 *    @option string content
295 *    @option string key
296 */
297function ws_images_addComment($params, $service)
298{
299  $query = '
300SELECT DISTINCT image_id
301  FROM '. IMAGE_CATEGORY_TABLE .'
302      INNER JOIN '.CATEGORIES_TABLE.' ON category_id=id
303  WHERE commentable="true"
304    AND image_id='.$params['image_id'].
305    get_sql_condition_FandF(
306      array(
307        'forbidden_categories' => 'id',
308        'visible_categories' => 'id',
309        'visible_images' => 'image_id'
310        ),
311      ' AND'
312      ).'
313;';
314
315  if (!pwg_db_num_rows(pwg_query($query)))
316  {
317    return new PwgError(WS_ERR_INVALID_PARAM, 'Invalid image_id');
318  }
319
320  $comm = array(
321    'author' => trim($params['author']),
322    'content' => trim($params['content']),
323    'image_id' => $params['image_id'],
324   );
325
326  include_once(PHPWG_ROOT_PATH.'include/functions_comment.inc.php');
327
328  $comment_action = insert_user_comment($comm, $params['key'], $infos);
329
330  switch ($comment_action)
331  {
332    case 'reject':
333      $infos[] = l10n('Your comment has NOT been registered because it did not pass the validation rules');
334      return new PwgError(403, implode("; ", $infos) );
335
336    case 'validate':
337    case 'moderate':
338      $ret = array(
339        'id' => $comm['id'],
340        'validation' => $comment_action=='validate',
341        );
342      return array('comment' => new PwgNamedStruct($ret));
343
344    default:
345      return new PwgError(500, "Unknown comment action ".$comment_action );
346  }
347}
348
349/**
350 * API method
351 * Returns detailed information for an element
352 * @param mixed[] $params
353 *    @option int image_id
354 *    @option int comments_page
355 *    @option int comments_per_page
356 */
357function ws_images_getInfo($params, $service)
358{
359  global $user, $conf;
360
361  $query='
362SELECT *
363  FROM '. IMAGES_TABLE .'
364  WHERE id='. $params['image_id'] .
365    get_sql_condition_FandF(
366      array('visible_images' => 'id'),
367      ' AND'
368      ).'
369LIMIT 1
370;';
371  $result = pwg_query($query);
372
373  if (pwg_db_num_rows($result) == 0)
374  {
375    return new PwgError(404, 'image_id not found');
376  }
377
378  $image_row = pwg_db_fetch_assoc($result);
379  $image_row = array_merge($image_row, ws_std_get_urls($image_row));
380
381  //-------------------------------------------------------- related categories
382  $query = '
383SELECT id, name, permalink, uppercats, global_rank, commentable
384  FROM '. IMAGE_CATEGORY_TABLE .'
385    INNER JOIN '. CATEGORIES_TABLE .' ON category_id = id
386  WHERE image_id = '. $image_row['id'] .
387    get_sql_condition_FandF(
388      array('forbidden_categories' => 'category_id'),
389      ' AND'
390      ).'
391;';
392  $result = pwg_query($query);
393
394  $is_commentable = false;
395  $related_categories = array();
396  while ($row = pwg_db_fetch_assoc($result))
397  {
398    if ($row['commentable']=='true')
399    {
400      $is_commentable = true;
401    }
402    unset($row['commentable']);
403
404    $row['url'] = make_index_url(
405      array(
406        'category' => $row
407        )
408      );
409
410    $row['page_url'] = make_picture_url(
411      array(
412        'image_id' => $image_row['id'],
413        'image_file' => $image_row['file'],
414        'category' => $row
415        )
416      );
417
418    $row['id']=(int)$row['id'];
419    $related_categories[] = $row;
420  }
421  usort($related_categories, 'global_rank_compare');
422
423  if (empty($related_categories))
424  {
425    return new PwgError(401, 'Access denied');
426  }
427
428  //-------------------------------------------------------------- related tags
429  $related_tags = get_common_tags(array($image_row['id']), -1);
430  foreach ($related_tags as $i=>$tag)
431  {
432    $tag['url'] = make_index_url(
433      array(
434        'tags' => array($tag)
435        )
436      );
437    $tag['page_url'] = make_picture_url(
438      array(
439        'image_id' => $image_row['id'],
440        'image_file' => $image_row['file'],
441        'tags' => array($tag),
442        )
443      );
444
445    unset($tag['counter']);
446    $tag['id'] = (int)$tag['id'];
447    $related_tags[$i] = $tag;
448  }
449
450  //------------------------------------------------------------- related rates
451        $rating = array(
452    'score' => $image_row['rating_score'],
453    'count' => 0,
454    'average' => null,
455    );
456        if (isset($rating['score']))
457        {
458                $query = '
459SELECT COUNT(rate) AS count, ROUND(AVG(rate),2) AS average
460  FROM '. RATE_TABLE .'
461  WHERE element_id = '. $image_row['id'] .'
462;';
463                $row = pwg_db_fetch_assoc(pwg_query($query));
464
465                $rating['score'] = (float)$rating['score'];
466                $rating['average'] = (float)$row['average'];
467                $rating['count'] = (int)$row['count'];
468        }
469
470  //---------------------------------------------------------- related comments
471  $related_comments = array();
472
473  $where_comments = 'image_id = '.$image_row['id'];
474  if (!is_admin())
475  {
476    $where_comments .= ' AND validated="true"';
477  }
478
479  $query = '
480SELECT COUNT(id) AS nb_comments
481  FROM '. COMMENTS_TABLE .'
482  WHERE '. $where_comments .'
483;';
484  list($nb_comments) = query2array($query, null, 'nb_comments');
485  $nb_comments = (int)$nb_comments;
486
487  if ($nb_comments>0 and $params['comments_per_page']>0)
488  {
489    $query = '
490SELECT id, date, author, content
491  FROM '. COMMENTS_TABLE .'
492  WHERE '. $where_comments .'
493  ORDER BY date
494  LIMIT '. (int)$params['comments_per_page'] .'
495  OFFSET '. (int)($params['comments_per_page']*$params['comments_page']) .'
496;';
497    $result = pwg_query($query);
498
499    while ($row = pwg_db_fetch_assoc($result))
500    {
501      $row['id'] = (int)$row['id'];
502      $related_comments[] = $row;
503    }
504  }
505
506  $comment_post_data = null;
507  if ($is_commentable and
508      (!is_a_guest()
509        or (is_a_guest() and $conf['comments_forall'] )
510      )
511    )
512  {
513    $comment_post_data['author'] = stripslashes($user['username']);
514    $comment_post_data['key'] = get_ephemeral_key(2, $params['image_id']);
515  }
516
517  $ret = $image_row;
518  foreach (array('id','width','height','hit','filesize') as $k)
519  {
520    if (isset($ret[$k]))
521    {
522      $ret[$k] = (int)$ret[$k];
523    }
524  }
525  foreach (array('path', 'storage_category_id') as $k)
526  {
527    unset($ret[$k]);
528  }
529
530  $ret['rates'] = array(
531    WS_XML_ATTRIBUTES => $rating
532    );
533  $ret['categories'] = new PwgNamedArray(
534    $related_categories,
535    'category',
536    array('id','url', 'page_url')
537    );
538  $ret['tags'] = new PwgNamedArray(
539    $related_tags,
540    'tag',
541    ws_std_get_tag_xml_attributes()
542    );
543  if (isset($comment_post_data))
544  {
545    $ret['comment_post'] = array(
546      WS_XML_ATTRIBUTES => $comment_post_data
547      );
548  }
549  $ret['comments_paging'] = new PwgNamedStruct(
550    array(
551      'page' => $params['comments_page'],
552      'per_page' => $params['comments_per_page'],
553      'count' => count($related_comments),
554      'total_count' => $nb_comments,
555      )
556    );
557  $ret['comments'] = new PwgNamedArray(
558    $related_comments,
559    'comment',
560    array('id','date')
561    );
562
563  if ($service->_responseFormat != 'rest')
564  {
565    return $ret; // for backward compatibility only
566  }
567  else
568  {
569    return array(
570      'image' => new PwgNamedStruct($ret, null, array('name','comment'))
571      );
572  }
573}
574
575/**
576 * API method
577 * Rates an image
578 * @param mixed[] $params
579 *    @option int image_id
580 *    @option float rate
581 */
582function ws_images_rate($params, $service)
583{
584  $query = '
585SELECT DISTINCT id
586  FROM '. IMAGES_TABLE .'
587    INNER JOIN '. IMAGE_CATEGORY_TABLE .' ON id=image_id
588  WHERE id='. $params['image_id']
589    .get_sql_condition_FandF(
590      array(
591        'forbidden_categories' => 'category_id',
592        'forbidden_images' => 'id',
593        ),
594      '    AND'
595      ).'
596  LIMIT 1
597;';
598  if (pwg_db_num_rows(pwg_query($query))==0)
599  {
600    return new PwgError(404, 'Invalid image_id or access denied');
601  }
602
603  include_once(PHPWG_ROOT_PATH.'include/functions_rate.inc.php');
604  $res = rate_picture($params['image_id'], (int)$params['rate']);
605
606  if ($res==false)
607  {
608    global $conf;
609    return new PwgError(403, 'Forbidden or rate not in '. implode(',', $conf['rate_items']));
610  }
611  return $res;
612}
613
614/**
615 * API method
616 * Returns a list of elements corresponding to a query search
617 * @param mixed[] $params
618 *    @option string query
619 *    @option int per_page
620 *    @option int page
621 *    @option string order (optional)
622 */
623function ws_images_search($params, $service)
624{
625  include_once(PHPWG_ROOT_PATH .'include/functions_search.inc.php');
626
627  $images = array();
628  $where_clauses = ws_std_image_sql_filter($params, 'i.');
629  $order_by = ws_std_image_sql_order($params, 'i.');
630
631  $super_order_by = false;
632  if (!empty($order_by))
633  {
634    global $conf;
635    $conf['order_by'] = 'ORDER BY '.$order_by;
636    $super_order_by = true; // quick_search_result might be faster
637  }
638
639  $search_result = get_quick_search_results(
640    $params['query'],
641    array(
642      'super_order_by' => $super_order_by,
643      'images_where' => implode(' AND ', $where_clauses)
644    )
645    );
646
647  $image_ids = array_slice(
648    $search_result['items'],
649    $params['page']*$params['per_page'],
650    $params['per_page']
651    );
652
653  if (count($image_ids))
654  {
655    $query = '
656SELECT *
657  FROM '. IMAGES_TABLE .'
658  WHERE id IN ('. implode(',', $image_ids) .')
659;';
660    $result = pwg_query($query);
661    $image_ids = array_flip($image_ids);
662
663    while ($row = pwg_db_fetch_assoc($result))
664    {
665      $image = array();
666      foreach (array('id', 'width', 'height', 'hit') as $k)
667      {
668        if (isset($row[$k]))
669        {
670          $image[$k] = (int)$row[$k];
671        }
672      }
673      foreach (array('file', 'name', 'comment', 'date_creation', 'date_available') as $k)
674      {
675        $image[$k] = $row[$k];
676      }
677
678      $image = array_merge($image, ws_std_get_urls($row));
679      $images[ $image_ids[ $image['id'] ] ] = $image;
680    }
681    ksort($images, SORT_NUMERIC);
682    $images = array_values($images);
683  }
684
685  return array (
686    'paging' => new PwgNamedStruct(
687      array(
688        'page' => $params['page'],
689        'per_page' => $params['per_page'],
690        'count' => count($images),
691        'total_count' => count($search_result['items']),
692        )
693      ),
694    'images' => new PwgNamedArray(
695      $images,
696      'image',
697      ws_std_get_image_xml_attributes()
698      )
699    );
700}
701
702/**
703 * API method
704 * Sets the level of an image
705 * @param mixed[] $params
706 *    @option int image_id
707 *    @option int level
708 */
709function ws_images_setPrivacyLevel($params, $service)
710{
711  global $conf;
712
713  if (!in_array($params['level'], $conf['available_permission_levels']))
714  {
715    return new PwgError(WS_ERR_INVALID_PARAM, 'Invalid level');
716  }
717
718  $query = '
719UPDATE '. IMAGES_TABLE .'
720  SET level='. (int)$params['level'] .'
721  WHERE id IN ('. implode(',',$params['image_id']) .')
722;';
723  $result = pwg_query($query);
724
725  $affected_rows = pwg_db_changes($result);
726  if ($affected_rows)
727  {
728    include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
729    invalidate_user_cache();
730  }
731  return $affected_rows;
732}
733
734/**
735 * API method
736 * Sets the rank of an image in a category
737 * @param mixed[] $params
738 *    @option int image_id
739 *    @option int category_id
740 *    @option int rank
741 */
742function ws_images_setRank($params, $service)
743{
744  // does the image really exist?
745  $query = '
746SELECT COUNT(*)
747  FROM '. IMAGES_TABLE .'
748  WHERE id = '. $params['image_id'] .'
749;';
750  list($count) = pwg_db_fetch_row(pwg_query($query));
751  if ($count == 0)
752  {
753    return new PwgError(404, 'image_id not found');
754  }
755
756  // is the image associated to this category?
757  $query = '
758SELECT COUNT(*)
759  FROM '. IMAGE_CATEGORY_TABLE .'
760  WHERE image_id = '. $params['image_id'] .'
761    AND category_id = '. $params['category_id'] .'
762;';
763  list($count) = pwg_db_fetch_row(pwg_query($query));
764  if ($count == 0)
765  {
766    return new PwgError(404, 'This image is not associated to this category');
767  }
768
769  // what is the current higher rank for this category?
770  $query = '
771SELECT MAX(rank) AS max_rank
772  FROM '. IMAGE_CATEGORY_TABLE .'
773  WHERE category_id = '. $params['category_id'] .'
774;';
775  $row = pwg_db_fetch_assoc(pwg_query($query));
776
777  if (is_numeric($row['max_rank']))
778  {
779    if ($params['rank'] > $row['max_rank'])
780    {
781      $params['rank'] = $row['max_rank'] + 1;
782    }
783  }
784  else
785  {
786    $params['rank'] = 1;
787  }
788
789  // update rank for all other photos in the same category
790  $query = '
791UPDATE '. IMAGE_CATEGORY_TABLE .'
792  SET rank = rank + 1
793  WHERE category_id = '. $params['category_id'] .'
794    AND rank IS NOT NULL
795    AND rank >= '. $params['rank'] .'
796;';
797  pwg_query($query);
798
799  // set the new rank for the photo
800  $query = '
801UPDATE '. IMAGE_CATEGORY_TABLE .'
802  SET rank = '. $params['rank'] .'
803  WHERE image_id = '. $params['image_id'] .'
804    AND category_id = '. $params['category_id'] .'
805;';
806  pwg_query($query);
807
808  // return data for client
809  return array(
810    'image_id' => $params['image_id'],
811    'category_id' => $params['category_id'],
812    'rank' => $params['rank'],
813    );
814}
815
816/**
817 * API method
818 * Adds a file chunk
819 * @param mixed[] $params
820 *    @option string data
821 *    @option string original_sum
822 *    @option string type = 'file'
823 *    @option int position
824 */
825function ws_images_add_chunk($params, $service)
826{
827  global $conf, $logger;
828
829  foreach ($params as $param_key => $param_value)
830  {
831    if ('data' == $param_key)
832    {
833      continue;
834    }
835
836    $logger->debug(sprintf(
837      '[ws_images_add_chunk] input param "%s" : "%s"',
838      $param_key,
839      is_null($param_value) ? 'NULL' : $param_value
840      ), 'WS');
841  }
842
843  $upload_dir = $conf['upload_dir'].'/buffer';
844
845  // create the upload directory tree if not exists
846  if (!mkgetdir($upload_dir, MKGETDIR_DEFAULT&~MKGETDIR_DIE_ON_ERROR))
847  {
848    return new PwgError(500, 'error during buffer directory creation');
849  }
850
851  $filename = sprintf(
852    '%s-%s-%05u.block',
853    $params['original_sum'],
854    $params['type'],
855    $params['position']
856    );
857
858  $logger->debug('[ws_images_add_chunk] data length : '.strlen($params['data']), 'WS');
859
860  $bytes_written = file_put_contents(
861    $upload_dir.'/'.$filename,
862    base64_decode($params['data'])
863    );
864
865  if (false === $bytes_written)
866  {
867    return new PwgError(500,
868      'an error has occured while writting chunk '.$params['position'].' for '.$params['type']
869      );
870  }
871}
872
873/**
874 * API method
875 * Adds a file
876 * @param mixed[] $params
877 *    @option int image_id
878 *    @option string type = 'file'
879 *    @option string sum
880 */
881function ws_images_addFile($params, $service)
882{
883  global $conf, $logger;
884
885  $logger->debug(__FUNCTION__, 'WS', $params);
886
887  // what is the path and other infos about the photo?
888  $query = '
889SELECT
890    path, file, md5sum,
891    width, height, filesize
892  FROM '. IMAGES_TABLE .'
893  WHERE id = '. $params['image_id'] .'
894;';
895  $result = pwg_query($query);
896
897  if (pwg_db_num_rows($result) == 0)
898  {
899    return new PwgError(404, "image_id not found");
900  }
901
902  $image = pwg_db_fetch_assoc($result);
903
904  // since Piwigo 2.4 and derivatives, we do not take the imported "thumb" into account
905  if ('thumb' == $params['type'])
906  {
907    remove_chunks($image['md5sum'], $type);
908    return true;
909  }
910
911  // since Piwigo 2.4 and derivatives, we only care about the "original"
912  $original_type = 'file';
913  if ('high' == $params['type'])
914  {
915    $original_type = 'high';
916  }
917
918  $file_path = $conf['upload_dir'].'/buffer/'.$image['md5sum'].'-original';
919
920  merge_chunks($file_path, $image['md5sum'], $original_type);
921  chmod($file_path, 0644);
922
923  include_once(PHPWG_ROOT_PATH.'admin/include/functions_upload.inc.php');
924
925  // if we receive the "file", we only update the original if the "file" is
926  // bigger than current original
927  if ('file' == $params['type'])
928  {
929    $do_update = false;
930
931    $infos = pwg_image_infos($file_path);
932
933    foreach (array('width', 'height', 'filesize') as $image_info)
934    {
935      if ($infos[$image_info] > $image[$image_info])
936      {
937        $do_update = true;
938      }
939    }
940
941    if (!$do_update)
942    {
943      unlink($file_path);
944      return true;
945    }
946  }
947
948  $image_id = add_uploaded_file(
949    $file_path,
950    $image['file'],
951    null,
952    null,
953    $params['image_id'],
954    $image['md5sum'] // we force the md5sum to remain the same
955    );
956}
957
958/**
959 * API method
960 * Adds an image
961 * @param mixed[] $params
962 *    @option string original_sum
963 *    @option string original_filename (optional)
964 *    @option string name (optional)
965 *    @option string author (optional)
966 *    @option string date_creation (optional)
967 *    @option string comment (optional)
968 *    @option string categories (optional) - "cat_id[,rank];cat_id[,rank]"
969 *    @option string tags_ids (optional) - "tag_id,tag_id"
970 *    @option int level
971 *    @option bool check_uniqueness
972 *    @option int image_id (optional)
973 */
974function ws_images_add($params, $service)
975{
976  global $conf, $user, $logger;
977
978  foreach ($params as $param_key => $param_value)
979  {
980    $logger->debug(sprintf(
981      '[pwg.images.add] input param "%s" : "%s"',
982      $param_key,
983      is_null($param_value) ? 'NULL' : $param_value
984      ), 'WS');
985  }
986
987  if ($params['image_id'] > 0)
988  {
989    $query = '
990SELECT COUNT(*)
991  FROM '. IMAGES_TABLE .'
992  WHERE id = '. $params['image_id'] .'
993;';
994    list($count) = pwg_db_fetch_row(pwg_query($query));
995    if ($count == 0)
996    {
997      return new PwgError(404, 'image_id not found');
998    }
999  }
1000
1001  // does the image already exists ?
1002  if ($params['check_uniqueness'])
1003  {
1004    if ('md5sum' == $conf['uniqueness_mode'])
1005    {
1006      $where_clause = "md5sum = '".$params['original_sum']."'";
1007    }
1008    if ('filename' == $conf['uniqueness_mode'])
1009    {
1010      $where_clause = "file = '".$params['original_filename']."'";
1011    }
1012
1013    $query = '
1014SELECT COUNT(*)
1015  FROM '. IMAGES_TABLE .'
1016  WHERE '. $where_clause .'
1017;';
1018    list($counter) = pwg_db_fetch_row(pwg_query($query));
1019    if ($counter != 0)
1020    {
1021      return new PwgError(500, 'file already exists');
1022    }
1023  }
1024
1025  // due to the new feature "derivatives" (multiple sizes) introduced for
1026  // Piwigo 2.4, we only take the biggest photos sent on
1027  // pwg.images.addChunk. If "high" is available we use it as "original"
1028  // else we use "file".
1029  remove_chunks($params['original_sum'], 'thumb');
1030
1031  if (isset($params['high_sum']))
1032  {
1033    $original_type = 'high';
1034    remove_chunks($params['original_sum'], 'file');
1035  }
1036  else
1037  {
1038    $original_type = 'file';
1039  }
1040
1041  $file_path = $conf['upload_dir'].'/buffer/'.$params['original_sum'].'-original';
1042
1043  merge_chunks($file_path, $params['original_sum'], $original_type);
1044  chmod($file_path, 0644);
1045
1046  include_once(PHPWG_ROOT_PATH.'admin/include/functions_upload.inc.php');
1047
1048  $image_id = add_uploaded_file(
1049    $file_path,
1050    $params['original_filename'],
1051    null, // categories
1052    isset($params['level']) ? $params['level'] : null,
1053    $params['image_id'] > 0 ? $params['image_id'] : null,
1054    $params['original_sum']
1055    );
1056
1057  $info_columns = array(
1058    'name',
1059    'author',
1060    'comment',
1061    'date_creation',
1062    );
1063
1064  $update = array();
1065  foreach ($info_columns as $key)
1066  {
1067    if (isset($params[$key]))
1068    {
1069      $update[$key] = $params[$key];
1070    }
1071  }
1072
1073  if (count(array_keys($update)) > 0)
1074  {
1075    single_update(
1076      IMAGES_TABLE,
1077      $update,
1078      array('id' => $image_id)
1079      );
1080  }
1081
1082  $url_params = array('image_id' => $image_id);
1083
1084  // let's add links between the image and the categories
1085  if (isset($params['categories']))
1086  {
1087    ws_add_image_category_relations($image_id, $params['categories']);
1088
1089    if (preg_match('/^\d+/', $params['categories'], $matches))
1090    {
1091      $category_id = $matches[0];
1092
1093      $query = '
1094SELECT id, name, permalink
1095  FROM '. CATEGORIES_TABLE .'
1096  WHERE id = '. $category_id .'
1097;';
1098      $result = pwg_query($query);
1099      $category = pwg_db_fetch_assoc($result);
1100
1101      $url_params['section'] = 'categories';
1102      $url_params['category'] = $category;
1103    }
1104  }
1105
1106  // and now, let's create tag associations
1107  if (isset($params['tag_ids']) and !empty($params['tag_ids']))
1108  {
1109    set_tags(
1110      explode(',', $params['tag_ids']),
1111      $image_id
1112      );
1113  }
1114
1115  invalidate_user_cache();
1116
1117  return array(
1118    'image_id' => $image_id,
1119    'url' => make_picture_url($url_params),
1120    );
1121}
1122
1123/**
1124 * API method
1125 * Adds a image (simple way)
1126 * @param mixed[] $params
1127 *    @option int[] category
1128 *    @option string name (optional)
1129 *    @option string author (optional)
1130 *    @option string comment (optional)
1131 *    @option int level
1132 *    @option string|string[] tags
1133 *    @option int image_id (optional)
1134 */
1135function ws_images_addSimple($params, $service)
1136{
1137  global $conf;
1138
1139  if (!isset($_FILES['image']))
1140  {
1141    return new PwgError(405, 'The image (file) is missing');
1142  }
1143
1144  if ($params['image_id'] > 0)
1145  {
1146    $query='
1147SELECT COUNT(*)
1148  FROM '. IMAGES_TABLE .'
1149  WHERE id = '. $params['image_id'] .'
1150;';
1151    list($count) = pwg_db_fetch_row(pwg_query($query));
1152    if ($count == 0)
1153    {
1154      return new PwgError(404, 'image_id not found');
1155    }
1156  }
1157
1158  include_once(PHPWG_ROOT_PATH.'admin/include/functions_upload.inc.php');
1159
1160  $image_id = add_uploaded_file(
1161    $_FILES['image']['tmp_name'],
1162    $_FILES['image']['name'],
1163    $params['category'],
1164    8,
1165    $params['image_id'] > 0 ? $params['image_id'] : null
1166    );
1167
1168  $info_columns = array(
1169    'name',
1170    'author',
1171    'comment',
1172    'level',
1173    'date_creation',
1174    );
1175
1176  $update = array();
1177  foreach ($info_columns as $key)
1178  {
1179    if (isset($params[$key]))
1180    {
1181      $update[$key] = $params[$key];
1182    }
1183  }
1184
1185  single_update(
1186    IMAGES_TABLE,
1187    $update,
1188    array('id' => $image_id)
1189    );
1190
1191  if (isset($params['tags']) and !empty($params['tags']))
1192  {
1193    include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
1194
1195    $tag_ids = array();
1196    if (is_array($params['tags']))
1197    {
1198      foreach ($params['tags'] as $tag_name)
1199      {
1200        $tag_ids[] = tag_id_from_tag_name($tag_name);
1201      }
1202    }
1203    else
1204    {
1205      $tag_names = preg_split('~(?<!\\\),~', $params['tags']);
1206      foreach ($tag_names as $tag_name)
1207      {
1208        $tag_ids[] = tag_id_from_tag_name(preg_replace('#\\\\*,#', ',', $tag_name));
1209      }
1210    }
1211
1212    add_tags($tag_ids, array($image_id));
1213  }
1214
1215  $url_params = array('image_id' => $image_id);
1216
1217  if (!empty($params['category']))
1218  {
1219    $query = '
1220SELECT id, name, permalink
1221  FROM '. CATEGORIES_TABLE .'
1222  WHERE id = '. $params['category'][0] .'
1223;';
1224    $result = pwg_query($query);
1225    $category = pwg_db_fetch_assoc($result);
1226
1227    $url_params['section'] = 'categories';
1228    $url_params['category'] = $category;
1229  }
1230
1231  // update metadata from the uploaded file (exif/iptc), even if the sync
1232  // was already performed by add_uploaded_file().
1233  require_once(PHPWG_ROOT_PATH.'admin/include/functions_metadata.php');
1234  sync_metadata(array($image_id));
1235
1236  return array(
1237    'image_id' => $image_id,
1238    'url' => make_picture_url($url_params),
1239    );
1240}
1241
1242/**
1243 * API method
1244 * Adds a image (simple way)
1245 * @param mixed[] $params
1246 *    @option int[] category
1247 *    @option string name (optional)
1248 *    @option string author (optional)
1249 *    @option string comment (optional)
1250 *    @option int level
1251 *    @option string|string[] tags
1252 *    @option int image_id (optional)
1253 */
1254function ws_images_upload($params, $service)
1255{
1256  global $conf;
1257
1258  if (get_pwg_token() != $params['pwg_token'])
1259  {
1260    return new PwgError(403, 'Invalid security token');
1261  }
1262
1263  // usleep(100000);
1264
1265  // if (!isset($_FILES['image']))
1266  // {
1267  //   return new PwgError(405, 'The image (file) is missing');
1268  // }
1269
1270  // file_put_contents('/tmp/plupload.log', "[".date('c')."] ".__FUNCTION__."\n\n", FILE_APPEND);
1271  // file_put_contents('/tmp/plupload.log', '$_FILES = '.var_export($_FILES, true)."\n", FILE_APPEND);
1272  // file_put_contents('/tmp/plupload.log', '$_POST = '.var_export($_POST, true)."\n", FILE_APPEND);
1273
1274  $upload_dir = $conf['upload_dir'].'/buffer';
1275
1276  // create the upload directory tree if not exists
1277  if (!mkgetdir($upload_dir, MKGETDIR_DEFAULT&~MKGETDIR_DIE_ON_ERROR))
1278  {
1279    return new PwgError(500, 'error during buffer directory creation');
1280  }
1281
1282  // Get a file name
1283  if (isset($_REQUEST["name"]))
1284  {
1285    $fileName = $_REQUEST["name"];
1286  }
1287  elseif (!empty($_FILES))
1288  {
1289    $fileName = $_FILES["file"]["name"];
1290  }
1291  else
1292  {
1293    $fileName = uniqid("file_");
1294  }
1295
1296  $filePath = $upload_dir.DIRECTORY_SEPARATOR.$fileName;
1297
1298  // Chunking might be enabled
1299  $chunk = isset($_REQUEST["chunk"]) ? intval($_REQUEST["chunk"]) : 0;
1300  $chunks = isset($_REQUEST["chunks"]) ? intval($_REQUEST["chunks"]) : 0;
1301
1302  // file_put_contents('/tmp/plupload.log', "[".date('c')."] ".__FUNCTION__.', '.$fileName.' '.($chunk+1).'/'.$chunks."\n", FILE_APPEND);
1303
1304  // Open temp file
1305  if (!$out = @fopen("{$filePath}.part", $chunks ? "ab" : "wb"))
1306  {
1307    die('{"jsonrpc" : "2.0", "error" : {"code": 102, "message": "Failed to open output stream."}, "id" : "id"}');
1308  }
1309
1310  if (!empty($_FILES))
1311  {
1312    if ($_FILES["file"]["error"] || !is_uploaded_file($_FILES["file"]["tmp_name"]))
1313    {
1314      die('{"jsonrpc" : "2.0", "error" : {"code": 103, "message": "Failed to move uploaded file."}, "id" : "id"}');
1315    }
1316
1317    // Read binary input stream and append it to temp file
1318    if (!$in = @fopen($_FILES["file"]["tmp_name"], "rb"))
1319    {
1320      die('{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "Failed to open input stream."}, "id" : "id"}');
1321    }
1322  }
1323  else
1324  {
1325    if (!$in = @fopen("php://input", "rb"))
1326    {
1327      die('{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "Failed to open input stream."}, "id" : "id"}');
1328    }
1329  }
1330
1331  while ($buff = fread($in, 4096))
1332  {
1333    fwrite($out, $buff);
1334  }
1335
1336  @fclose($out);
1337  @fclose($in);
1338
1339  // Check if file has been uploaded
1340  if (!$chunks || $chunk == $chunks - 1)
1341  {
1342    // Strip the temp .part suffix off
1343    rename("{$filePath}.part", $filePath);
1344
1345    include_once(PHPWG_ROOT_PATH.'admin/include/functions_upload.inc.php');
1346
1347    $image_id = add_uploaded_file(
1348      $filePath,
1349      stripslashes($params['name']), // function add_uploaded_file will secure before insert
1350      $params['category'],
1351      $params['level'],
1352      null // image_id = not provided, this is a new photo
1353      );
1354
1355    $query = '
1356SELECT
1357    id,
1358    name,
1359    representative_ext,
1360    path
1361  FROM '.IMAGES_TABLE.'
1362  WHERE id = '.$image_id.'
1363;';
1364    $image_infos = pwg_db_fetch_assoc(pwg_query($query));
1365
1366    $query = '
1367SELECT
1368    COUNT(*) AS nb_photos
1369  FROM '.IMAGE_CATEGORY_TABLE.'
1370  WHERE category_id = '.$params['category'][0].'
1371;';
1372    $category_infos = pwg_db_fetch_assoc(pwg_query($query));
1373
1374    $category_name = get_cat_display_name_from_id($params['category'][0], null);
1375
1376    return array(
1377      'image_id' => $image_id,
1378      'src' => DerivativeImage::thumb_url($image_infos),
1379      'name' => $image_infos['name'],
1380      'category' => array(
1381        'id' => $params['category'][0],
1382        'nb_photos' => $category_infos['nb_photos'],
1383        'label' => $category_name,
1384        )
1385      );
1386  }
1387}
1388
1389/**
1390 * API method
1391 * Check if an image exists by it's name or md5 sum
1392 * @param mixed[] $params
1393 *    @option string md5sum_list (optional)
1394 *    @option string filename_list (optional)
1395 */
1396function ws_images_exist($params, $service)
1397{
1398  global $conf, $logger;
1399
1400  $logger->debug(__FUNCTION__, 'WS', $params);
1401
1402  $split_pattern = '/[\s,;\|]/';
1403  $result = array();
1404
1405  if ('md5sum' == $conf['uniqueness_mode'])
1406  {
1407    // search among photos the list of photos already added, based on md5sum list
1408    $md5sums = preg_split(
1409      $split_pattern,
1410      $params['md5sum_list'],
1411      -1,
1412      PREG_SPLIT_NO_EMPTY
1413    );
1414
1415    $query = '
1416SELECT id, md5sum
1417  FROM '. IMAGES_TABLE .'
1418  WHERE md5sum IN (\''. implode("','", $md5sums) .'\')
1419;';
1420    $id_of_md5 = query2array($query, 'md5sum', 'id');
1421
1422    foreach ($md5sums as $md5sum)
1423    {
1424      $result[$md5sum] = null;
1425      if (isset($id_of_md5[$md5sum]))
1426      {
1427        $result[$md5sum] = $id_of_md5[$md5sum];
1428      }
1429    }
1430  }
1431  elseif ('filename' == $conf['uniqueness_mode'])
1432  {
1433    // search among photos the list of photos already added, based on
1434    // filename list
1435    $filenames = preg_split(
1436      $split_pattern,
1437      $params['filename_list'],
1438      -1,
1439      PREG_SPLIT_NO_EMPTY
1440    );
1441
1442    $query = '
1443SELECT id, file
1444  FROM '.IMAGES_TABLE.'
1445  WHERE file IN (\''. implode("','", $filenames) .'\')
1446;';
1447    $id_of_filename = query2array($query, 'file', 'id');
1448
1449    foreach ($filenames as $filename)
1450    {
1451      $result[$filename] = null;
1452      if (isset($id_of_filename[$filename]))
1453      {
1454        $result[$filename] = $id_of_filename[$filename];
1455      }
1456    }
1457  }
1458
1459  return $result;
1460}
1461
1462/**
1463 * API method
1464 * Check is file has been update
1465 * @param mixed[] $params
1466 *    @option int image_id
1467 *    @option string file_sum
1468 */
1469function ws_images_checkFiles($params, $service)
1470{
1471  global $logger;
1472
1473  $logger->debug(__FUNCTION__, 'WS', $params);
1474
1475  $query = '
1476SELECT path
1477  FROM '. IMAGES_TABLE .'
1478  WHERE id = '. $params['image_id'] .'
1479;';
1480  $result = pwg_query($query);
1481
1482  if (pwg_db_num_rows($result) == 0)
1483  {
1484    return new PwgError(404, 'image_id not found');
1485  }
1486
1487  list($path) = pwg_db_fetch_row($result);
1488
1489  $ret = array();
1490
1491  if (isset($params['thumbnail_sum']))
1492  {
1493    // We always say the thumbnail is equal to create no reaction on the
1494    // other side. Since Piwigo 2.4 and derivatives, the thumbnails and web
1495    // sizes are always generated by Piwigo
1496    $ret['thumbnail'] = 'equals';
1497  }
1498
1499  if (isset($params['high_sum']))
1500  {
1501    $ret['file'] = 'equals';
1502    $compare_type = 'high';
1503  }
1504  elseif (isset($params['file_sum']))
1505  {
1506    $compare_type = 'file';
1507  }
1508
1509  if (isset($compare_type))
1510  {
1511    $logger->debug(__FUNCTION__.', md5_file($path) = '.md5_file($path), 'WS');
1512    if (md5_file($path) != $params[$compare_type.'_sum'])
1513    {
1514      $ret[$compare_type] = 'differs';
1515    }
1516    else
1517    {
1518      $ret[$compare_type] = 'equals';
1519    }
1520  }
1521
1522  $logger->debug(__FUNCTION__, 'WS', $ret);
1523
1524  return $ret;
1525}
1526
1527/**
1528 * API method
1529 * Sets details of an image
1530 * @param mixed[] $params
1531 *    @option int image_id
1532 *    @option string file (optional)
1533 *    @option string name (optional)
1534 *    @option string author (optional)
1535 *    @option string date_creation (optional)
1536 *    @option string comment (optional)
1537 *    @option string categories (optional) - "cat_id[,rank];cat_id[,rank]"
1538 *    @option string tags_ids (optional) - "tag_id,tag_id"
1539 *    @option int level (optional)
1540 *    @option string single_value_mode
1541 *    @option string multiple_value_mode
1542 */
1543function ws_images_setInfo($params, $service)
1544{
1545  include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
1546
1547  $query='
1548SELECT *
1549  FROM '. IMAGES_TABLE .'
1550  WHERE id = '. $params['image_id'] .'
1551;';
1552  $result = pwg_query($query);
1553
1554  if (pwg_db_num_rows($result) == 0)
1555  {
1556    return new PwgError(404, 'image_id not found');
1557  }
1558
1559  $image_row = pwg_db_fetch_assoc($result);
1560
1561  // database registration
1562  $update = array();
1563
1564  $info_columns = array(
1565    'name',
1566    'author',
1567    'comment',
1568    'level',
1569    'date_creation',
1570    );
1571
1572  foreach ($info_columns as $key)
1573  {
1574    if (isset($params[$key]))
1575    {
1576      if ('fill_if_empty' == $params['single_value_mode'])
1577      {
1578        if (empty($image_row[$key]))
1579        {
1580          $update[$key] = $params[$key];
1581        }
1582      }
1583      elseif ('replace' == $params['single_value_mode'])
1584      {
1585        $update[$key] = $params[$key];
1586      }
1587      else
1588      {
1589        return new PwgError(500,
1590          '[ws_images_setInfo]'
1591          .' invalid parameter single_value_mode "'.$params['single_value_mode'].'"'
1592          .', possible values are {fill_if_empty, replace}.'
1593          );
1594      }
1595    }
1596  }
1597
1598  if (isset($params['file']))
1599  {
1600    if (!empty($image_row['storage_category_id']))
1601    {
1602      return new PwgError(500,
1603        '[ws_images_setInfo] updating "file" is forbidden on photos added by synchronization'
1604        );
1605    }
1606
1607    $update['file'] = $params['file'];
1608  }
1609
1610  if (count(array_keys($update)) > 0)
1611  {
1612    $update['id'] = $params['image_id'];
1613
1614    single_update(
1615      IMAGES_TABLE,
1616      $update,
1617      array('id' => $update['id'])
1618      );
1619  }
1620
1621  if (isset($params['categories']))
1622  {
1623    ws_add_image_category_relations(
1624      $params['image_id'],
1625      $params['categories'],
1626      ('replace' == $params['multiple_value_mode'] ? true : false)
1627      );
1628  }
1629
1630  // and now, let's create tag associations
1631  if (isset($params['tag_ids']))
1632  {
1633    $tag_ids = array();
1634
1635    foreach (explode(',', $params['tag_ids']) as $candidate)
1636    {
1637      $candidate = trim($candidate);
1638
1639      if (preg_match(PATTERN_ID, $candidate))
1640      {
1641        $tag_ids[] = $candidate;
1642      }
1643    }
1644
1645    if ('replace' == $params['multiple_value_mode'])
1646    {
1647      set_tags(
1648        $tag_ids,
1649        $params['image_id']
1650        );
1651    }
1652    elseif ('append' == $params['multiple_value_mode'])
1653    {
1654      add_tags(
1655        $tag_ids,
1656        array($params['image_id'])
1657        );
1658    }
1659    else
1660    {
1661      return new PwgError(500,
1662        '[ws_images_setInfo]'
1663        .' invalid parameter multiple_value_mode "'.$params['multiple_value_mode'].'"'
1664        .', possible values are {replace, append}.'
1665        );
1666    }
1667  }
1668
1669  invalidate_user_cache();
1670}
1671
1672/**
1673 * API method
1674 * Deletes an image
1675 * @param mixed[] $params
1676 *    @option int|int[] image_id
1677 *    @option string pwg_token
1678 */
1679function ws_images_delete($params, $service)
1680{
1681  if (get_pwg_token() != $params['pwg_token'])
1682  {
1683    return new PwgError(403, 'Invalid security token');
1684  }
1685
1686  if (!is_array($params['image_id']))
1687  {
1688    $params['image_id'] = preg_split(
1689      '/[\s,;\|]/',
1690      $params['image_id'],
1691      -1,
1692      PREG_SPLIT_NO_EMPTY
1693      );
1694  }
1695  $params['image_id'] = array_map('intval', $params['image_id']);
1696
1697  $image_ids = array();
1698  foreach ($params['image_id'] as $image_id)
1699  {
1700    if ($image_id > 0)
1701    {
1702      $image_ids[] = $image_id;
1703    }
1704  }
1705
1706  include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
1707  delete_elements($image_ids, true);
1708  invalidate_user_cache();
1709}
1710
1711/**
1712 * API method
1713 * Checks if Piwigo is ready for upload
1714 * @param mixed[] $params
1715 */
1716function ws_images_checkUpload($params, $service)
1717{
1718  include_once(PHPWG_ROOT_PATH.'admin/include/functions_upload.inc.php');
1719
1720  $ret['message'] = ready_for_upload_message();
1721  $ret['ready_for_upload'] = true;
1722  if (!empty($ret['message']))
1723  {
1724    $ret['ready_for_upload'] = false;
1725  }
1726
1727  return $ret;
1728}
1729
1730?>
Note: See TracBrowser for help on using the repository browser.