source: branches/2.3/include/ws_functions.inc.php @ 12539

Last change on this file since 12539 was 12539, checked in by plg, 12 years ago

feature 2352: a bug had been introduced by r12537, the piwigo_user_cache_categories.count_categories
was missing (the list of albums was not refreshing correctly on the upload form)

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