source: trunk/admin/include/functions.php @ 12763

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

merge r12762 from branch 2.3 to trunk

bug 2536 fixed: a new virtual album is now added on top of the list of other
albums in the parent album (instead of bottom).

  • Property svn:eol-style set to LF
File size: 51.2 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
24include(PHPWG_ROOT_PATH.'admin/include/functions_metadata.php');
25
26// The function delete_site deletes a site and call the function
27// delete_categories for each primary category of the site
28function delete_site( $id )
29{
30  // destruction of the categories of the site
31  $query = '
32SELECT id
33  FROM '.CATEGORIES_TABLE.'
34  WHERE site_id = '.$id.'
35;';
36  $result = pwg_query($query);
37  $category_ids = array();
38  while ($row = pwg_db_fetch_assoc($result))
39  {
40    array_push($category_ids, $row['id']);
41  }
42  delete_categories($category_ids);
43
44  // destruction of the site
45  $query = '
46DELETE FROM '.SITES_TABLE.'
47  WHERE id = '.$id.'
48;';
49  pwg_query($query);
50}
51
52
53// The function delete_categories deletes the categories identified by the
54// (numeric) key of the array $ids. It also deletes (in the database) :
55//    - all the elements physically linked to the category (delete_elements, see further)
56//    - all the links between elements and this category
57//    - all the restrictions linked to the category
58// The function works recursively.
59//
60// the $photo_deletion_mode is for photos virtually linked to the categorty
61//   * no_delete : delete no photo, may create orphans
62//   * delete_orphans : delete photos that are no longer linked to any category
63//   * force_delete : delete photos even if they are linked to another category
64function delete_categories($ids, $photo_deletion_mode='no_delete')
65{
66  if (count($ids) == 0)
67  {
68    return;
69  }
70
71  // add sub-category ids to the given ids : if a category is deleted, all
72  // sub-categories must be so
73  $ids = get_subcat_ids($ids);
74
75  // destruction of all photos physically linked to the category
76  $query = '
77SELECT id
78  FROM '.IMAGES_TABLE.'
79  WHERE storage_category_id IN (
80'.wordwrap(implode(', ', $ids), 80, "\n").')
81;';
82  $result = pwg_query($query);
83  $element_ids = array();
84  while ($row = pwg_db_fetch_assoc($result))
85  {
86    array_push($element_ids, $row['id']);
87  }
88  delete_elements($element_ids);
89
90  // now, should we delete photos that are virtually linked to the category?
91  if ('delete_orphans' == $photo_deletion_mode or 'force_delete' == $photo_deletion_mode)
92  {
93    $query = '
94SELECT
95    DISTINCT(image_id)
96  FROM '.IMAGE_CATEGORY_TABLE.'
97  WHERE category_id IN ('.implode(',', $ids).')
98;';
99    $image_ids_linked = array_from_query($query, 'image_id');
100
101    if (count($image_ids_linked) > 0)
102    {
103      if ('delete_orphans' == $photo_deletion_mode)
104      {
105        $query = '
106SELECT
107    DISTINCT(image_id)
108  FROM '.IMAGE_CATEGORY_TABLE.'
109  WHERE image_id IN ('.implode(',', $image_ids_linked).')
110    AND category_id NOT IN ('.implode(',', $ids).')
111;';
112        $image_ids_not_orphans = array_from_query($query, 'image_id');
113        $image_ids_to_delete = array_diff($image_ids_linked, $image_ids_not_orphans);
114      }
115
116      if ('force_delete' == $photo_deletion_mode)
117      {
118        $image_ids_to_delete = $image_ids_linked;
119      }
120
121      delete_elements($image_ids_to_delete, true);
122    }
123  }
124
125  // destruction of the links between images and this category
126  $query = '
127DELETE FROM '.IMAGE_CATEGORY_TABLE.'
128  WHERE category_id IN (
129'.wordwrap(implode(', ', $ids), 80, "\n").')
130;';
131  pwg_query($query);
132
133  // destruction of the access linked to the category
134  $query = '
135DELETE FROM '.USER_ACCESS_TABLE.'
136  WHERE cat_id IN (
137'.wordwrap(implode(', ', $ids), 80, "\n").')
138;';
139  pwg_query($query);
140
141  $query = '
142DELETE FROM '.GROUP_ACCESS_TABLE.'
143  WHERE cat_id IN (
144'.wordwrap(implode(', ', $ids), 80, "\n").')
145;';
146  pwg_query($query);
147
148  // destruction of the category
149  $query = '
150DELETE FROM '.CATEGORIES_TABLE.'
151  WHERE id IN (
152'.wordwrap(implode(', ', $ids), 80, "\n").')
153;';
154  pwg_query($query);
155
156  $query='
157DELETE FROM '.OLD_PERMALINKS_TABLE.'
158  WHERE cat_id IN ('.implode(',',$ids).')';
159  pwg_query($query);
160
161  $query='
162DELETE FROM '.USER_CACHE_CATEGORIES_TABLE.'
163  WHERE cat_id IN ('.implode(',',$ids).')';
164  pwg_query($query);
165
166  trigger_action('delete_categories', $ids);
167}
168
169// Deletes all files (on disk) related to given image ids
170// @return image ids where files are deleted successfully
171function delete_element_files($ids)
172{
173  if (count($ids) == 0)
174  {
175    return 0;
176  }
177
178  include_once(PHPWG_ROOT_PATH.'include/functions_picture.inc.php');
179 
180  $new_ids = array();
181
182  $query = '
183SELECT
184    id,
185    path,
186    tn_ext,
187    has_high,
188    representative_ext
189  FROM '.IMAGES_TABLE.'
190  WHERE id IN ('.implode(',', $ids).')
191;';
192  $result = pwg_query($query);
193  while ($row = pwg_db_fetch_assoc($result))
194  {
195    if (url_is_remote($row['path']))
196    {
197      continue;
198    }
199   
200    $files = array();
201    $files[] = get_element_path($row);
202
203    if (!empty($row['tn_ext']))
204    {
205      $files[] = get_thumbnail_path($row);
206    }
207   
208    if (!empty($row['has_high']) and get_boolean($row['has_high']))
209    {
210      $files[] = get_high_path($row);
211    }
212   
213    if (!empty($row['representative_ext']))
214    {
215      $pi = pathinfo($row['path']);
216      $file_wo_ext = get_filename_wo_extension($pi['basename']);
217      $files[] = PHPWG_ROOT_PATH.$pi['dirname'].'/pwg_representative/'.$file_wo_ext.'.'.$row['representative_ext'];
218    }
219
220    $ok = true;
221    foreach ($files as $path)
222    {
223      if (is_file($path) and !unlink($path))
224      {
225        $ok = false;
226        trigger_error('"'.$path.'" cannot be removed', E_USER_WARNING);
227        break;
228      }
229    }
230   
231    if ($ok)
232    {
233      $new_ids[] += $row['id'];
234    }
235    else
236    {
237      break;
238    }
239  }
240  return $new_ids;
241}
242
243// The function delete_elements deletes the elements identified by the
244// (numeric) values of the array $ids. It also deletes (in the database) :
245//    - all the comments related to elements
246//    - all the links between categories and elements
247//    - all the favorites associated to elements
248// @return number of deleted elements
249function delete_elements($ids, $physical_deletion=false)
250{
251  if (count($ids) == 0)
252  {
253    return 0;
254  }
255  trigger_action('begin_delete_elements', $ids);
256
257  if ($physical_deletion)
258  {
259    $ids = delete_element_files($ids);
260    if (count($ids)==0)
261    {
262      return 0;
263    }
264  }
265
266  // destruction of the comments on the image
267  $query = '
268DELETE FROM '.COMMENTS_TABLE.'
269  WHERE image_id IN (
270'.wordwrap(implode(', ', $ids), 80, "\n").')
271;';
272  pwg_query($query);
273
274  // destruction of the links between images and this category
275  $query = '
276DELETE FROM '.IMAGE_CATEGORY_TABLE.'
277  WHERE image_id IN (
278'.wordwrap(implode(', ', $ids), 80, "\n").')
279;';
280  pwg_query($query);
281
282  // destruction of the links between images and tags
283  $query = '
284DELETE FROM '.IMAGE_TAG_TABLE.'
285  WHERE image_id IN (
286'.wordwrap(implode(', ', $ids), 80, "\n").')
287;';
288  pwg_query($query);
289
290  // destruction of the favorites associated with the picture
291  $query = '
292DELETE FROM '.FAVORITES_TABLE.'
293  WHERE image_id IN (
294'.wordwrap(implode(', ', $ids), 80, "\n").')
295;';
296  pwg_query($query);
297
298  // destruction of the rates associated to this element
299  $query = '
300DELETE FROM '.RATE_TABLE.'
301  WHERE element_id IN (
302'.wordwrap(implode(', ', $ids), 80, "\n").')
303;';
304  pwg_query($query);
305
306  // destruction of the rates associated to this element
307  $query = '
308DELETE FROM '.CADDIE_TABLE.'
309  WHERE element_id IN (
310'.wordwrap(implode(', ', $ids), 80, "\n").')
311;';
312  pwg_query($query);
313
314  // destruction of the image
315  $query = '
316DELETE FROM '.IMAGES_TABLE.'
317  WHERE id IN (
318'.wordwrap(implode(', ', $ids), 80, "\n").')
319;';
320  pwg_query($query);
321
322  // are the photo used as category representant?
323  $query = '
324SELECT
325    id
326  FROM '.CATEGORIES_TABLE.'
327  WHERE representative_picture_id IN (
328'.wordwrap(implode(', ', $ids), 80, "\n").')
329;';
330  $category_ids = array_from_query($query, 'id');
331  if (count($category_ids) > 0)
332  {
333    update_category($category_ids);
334  }
335
336  trigger_action('delete_elements', $ids);
337  return count($ids);
338}
339
340// The delete_user function delete a user identified by the $user_id
341// It also deletes :
342//     - all the access linked to this user
343//     - all the links to any group
344//     - all the favorites linked to this user
345//     - calculated permissions linked to the user
346//     - all datas about notifications for the user
347function delete_user($user_id)
348{
349  global $conf;
350  $tables = array(
351    // destruction of the access linked to the user
352    USER_ACCESS_TABLE,
353    // destruction of data notification by mail for this user
354    USER_MAIL_NOTIFICATION_TABLE,
355    // destruction of data RSS notification for this user
356    USER_FEED_TABLE,
357    // deletion of calculated permissions linked to the user
358    USER_CACHE_TABLE,
359    // deletion of computed cache data linked to the user
360    USER_CACHE_CATEGORIES_TABLE,
361    // destruction of the group links for this user
362    USER_GROUP_TABLE,
363    // destruction of the favorites associated with the user
364    FAVORITES_TABLE,
365    // destruction of the caddie associated with the user
366    CADDIE_TABLE,
367    // deletion of piwigo specific informations
368    USER_INFOS_TABLE,
369    );
370
371  foreach ($tables as $table)
372  {
373    $query = '
374DELETE FROM '.$table.'
375  WHERE user_id = '.$user_id.'
376;';
377    pwg_query($query);
378  }
379
380  // destruction of the user
381  $query = '
382DELETE FROM '.SESSIONS_TABLE.'
383  WHERE data LIKE \'pwg_uid|i:'.(int)$user_id.';%\'
384;';
385  pwg_query($query);
386
387  // destruction of the user
388  $query = '
389DELETE FROM '.USERS_TABLE.'
390  WHERE '.$conf['user_fields']['id'].' = '.$user_id.'
391;';
392  pwg_query($query);
393
394  trigger_action('delete_user', $user_id);
395}
396
397/**
398 * Deletes all tags linked to no photo
399 */
400function delete_orphan_tags()
401{
402  $orphan_tags = get_orphan_tags();
403 
404  if (count($orphan_tags) > 0)
405  {
406    $orphan_tag_ids = array();
407    foreach ($orphan_tags as $tag)
408    {
409      array_push($orphan_tag_ids, $tag['id']);
410    }
411
412    $query = '
413DELETE
414  FROM '.TAGS_TABLE.'
415  WHERE id IN ('.implode(',', $orphan_tag_ids).')
416;';
417    pwg_query($query);
418  }
419}
420
421/**
422 * Get all tags (id + name) linked to no photo
423 */
424function get_orphan_tags()
425{
426  $orphan_tags = array();
427 
428  $query = '
429SELECT
430    id,
431    name
432  FROM '.TAGS_TABLE.'
433    LEFT JOIN '.IMAGE_TAG_TABLE.' ON id = tag_id
434  WHERE tag_id IS NULL
435;';
436  $result = pwg_query($query);
437  while ($row = pwg_db_fetch_assoc($result))
438  {
439    array_push($orphan_tags, $row);
440  }
441
442  return $orphan_tags;
443}
444
445/**
446 * Verifies that the representative picture really exists in the db and
447 * picks up a random represantive if possible and based on config.
448 *
449 * @param mixed category id
450 * @returns void
451 */
452function update_category($ids = 'all')
453{
454  global $conf;
455
456  if ($ids=='all')
457  {
458    $where_cats = '1=1';
459  }
460  elseif ( !is_array($ids) )
461  {
462    $where_cats = '%s='.$ids;
463  }
464  else
465  {
466    if (count($ids) == 0)
467    {
468      return false;
469    }
470    $where_cats = '%s IN('.wordwrap(implode(', ', $ids), 120, "\n").')';
471  }
472
473  // find all categories where the setted representative is not possible :
474  // the picture does not exist
475  $query = '
476SELECT DISTINCT c.id
477  FROM '.CATEGORIES_TABLE.' AS c LEFT JOIN '.IMAGES_TABLE.' AS i
478    ON c.representative_picture_id = i.id
479  WHERE representative_picture_id IS NOT NULL
480    AND '.sprintf($where_cats, 'c.id').'
481    AND i.id IS NULL
482;';
483  $wrong_representant = array_from_query($query, 'id');
484
485  if (count($wrong_representant) > 0)
486  {
487    $query = '
488UPDATE '.CATEGORIES_TABLE.'
489  SET representative_picture_id = NULL
490  WHERE id IN ('.wordwrap(implode(', ', $wrong_representant), 120, "\n").')
491;';
492    pwg_query($query);
493  }
494
495  if (!$conf['allow_random_representative'])
496  {
497    // If the random representant is not allowed, we need to find
498    // categories with elements and with no representant. Those categories
499    // must be added to the list of categories to set to a random
500    // representant.
501    $query = '
502SELECT DISTINCT id
503  FROM '.CATEGORIES_TABLE.' INNER JOIN '.IMAGE_CATEGORY_TABLE.'
504    ON id = category_id
505  WHERE representative_picture_id IS NULL
506    AND '.sprintf($where_cats, 'category_id').'
507;';
508    $to_rand = array_from_query($query, 'id');
509    if (count($to_rand) > 0)
510    {
511      set_random_representant($to_rand);
512    }
513  }
514}
515
516/**
517 * returns an array containing sub-directories which can be a category,
518 * recursive by default
519 *
520 * directories nammed "thumbnail", "pwg_high" or "pwg_representative" are
521 * omitted
522 *
523 * @param string $basedir
524 * @return array
525 */
526function get_fs_directories($path, $recursive = true)
527{
528  $dirs = array();
529
530  if (is_dir($path))
531  {
532    if ($contents = opendir($path))
533    {
534      while (($node = readdir($contents)) !== false)
535      {
536        if ($node != '.'
537            and $node != '..'
538            and $node != '.svn'
539            and $node != 'thumbnail'
540            and $node != 'pwg_high'
541            and $node != 'pwg_representative'
542            and is_dir($path.'/'.$node))
543        {
544          array_push($dirs, $path.'/'.$node);
545          if ($recursive)
546          {
547            $dirs = array_merge($dirs, get_fs_directories($path.'/'.$node));
548          }
549        }
550      }
551      closedir($contents);
552    }
553  }
554
555  return $dirs;
556}
557
558/**
559 * order categories (update categories.rank and global_rank database fields)
560 * so that rank field are consecutive integers starting at 1 for each child
561 * @return void
562 */
563function update_global_rank()
564{
565  $query = '
566SELECT id, id_uppercat, uppercats, rank, global_rank
567  FROM '.CATEGORIES_TABLE.'
568  ORDER BY id_uppercat,rank,name';
569
570  $cat_map = array();
571
572  $current_rank = 0;
573  $current_uppercat = '';
574
575  $result = pwg_query($query);
576  while ($row = pwg_db_fetch_assoc($result))
577  {
578    if ($row['id_uppercat'] != $current_uppercat)
579    {
580      $current_rank = 0;
581      $current_uppercat = $row['id_uppercat'];
582    }
583    ++$current_rank;
584    $cat =
585      array(
586        'rank' =>        $current_rank,
587        'rank_changed' =>$current_rank!=$row['rank'],
588        'global_rank' => $row['global_rank'],
589        'uppercats' =>   $row['uppercats'],
590        );
591    $cat_map[ $row['id'] ] = $cat;
592  }
593
594  $datas = array();
595
596  foreach( $cat_map as $id=>$cat )
597  {
598    $new_global_rank = preg_replace(
599          '/(\d+)/e',
600          "\$cat_map['$1']['rank']",
601          str_replace(',', '.', $cat['uppercats'] )
602          );
603    if ( $cat['rank_changed']
604      or $new_global_rank!=$cat['global_rank']
605      )
606    {
607      $datas[] = array(
608          'id' => $id,
609          'rank' => $cat['rank'],
610          'global_rank' => $new_global_rank,
611        );
612    }
613  }
614
615  mass_updates(
616    CATEGORIES_TABLE,
617    array(
618      'primary' => array('id'),
619      'update'  => array('rank', 'global_rank')
620      ),
621    $datas
622    );
623  return count($datas);
624}
625
626/**
627 * change the visible property on a set of categories
628 *
629 * @param array categories
630 * @param string value
631 * @return void
632 */
633function set_cat_visible($categories, $value)
634{
635  if (!in_array($value, array('true', 'false')))
636  {
637    trigger_error("set_cat_visible invalid param $value", E_USER_WARNING);
638    return false;
639  }
640
641  // unlocking a category => all its parent categories become unlocked
642  if ($value == 'true')
643  {
644    $uppercats = get_uppercat_ids($categories);
645    $query = '
646UPDATE '.CATEGORIES_TABLE.'
647  SET visible = \'true\'
648  WHERE id IN ('.implode(',', $uppercats).')';
649    pwg_query($query);
650  }
651  // locking a category   => all its child categories become locked
652  if ($value == 'false')
653  {
654    $subcats = get_subcat_ids($categories);
655    $query = '
656UPDATE '.CATEGORIES_TABLE.'
657  SET visible = \'false\'
658  WHERE id IN ('.implode(',', $subcats).')';
659    pwg_query($query);
660  }
661}
662
663/**
664 * change the status property on a set of categories : private or public
665 *
666 * @param array categories
667 * @param string value
668 * @return void
669 */
670function set_cat_status($categories, $value)
671{
672  if (!in_array($value, array('public', 'private')))
673  {
674    trigger_error("set_cat_status invalid param $value", E_USER_WARNING);
675    return false;
676  }
677
678  // make public a category => all its parent categories become public
679  if ($value == 'public')
680  {
681    $uppercats = get_uppercat_ids($categories);
682    $query = '
683UPDATE '.CATEGORIES_TABLE.'
684  SET status = \'public\'
685  WHERE id IN ('.implode(',', $uppercats).')
686;';
687    pwg_query($query);
688  }
689  // make a category private => all its child categories become private
690  if ($value == 'private')
691  {
692    $subcats = get_subcat_ids($categories);
693    $query = '
694UPDATE '.CATEGORIES_TABLE.'
695  SET status = \'private\'
696  WHERE id IN ('.implode(',', $subcats).')';
697    pwg_query($query);
698  }
699}
700
701/**
702 * returns all uppercats category ids of the given category ids
703 *
704 * @param array cat_ids
705 * @return array
706 */
707function get_uppercat_ids($cat_ids)
708{
709  if (!is_array($cat_ids) or count($cat_ids) < 1)
710  {
711    return array();
712  }
713
714  $uppercats = array();
715
716  $query = '
717SELECT uppercats
718  FROM '.CATEGORIES_TABLE.'
719  WHERE id IN ('.implode(',', $cat_ids).')
720;';
721  $result = pwg_query($query);
722  while ($row = pwg_db_fetch_assoc($result))
723  {
724    $uppercats = array_merge($uppercats,
725                             explode(',', $row['uppercats']));
726  }
727  $uppercats = array_unique($uppercats);
728
729  return $uppercats;
730}
731
732/**
733 * set a new random representant to the categories
734 *
735 * @param array categories
736 */
737function set_random_representant($categories)
738{
739  $datas = array();
740  foreach ($categories as $category_id)
741  {
742    $query = '
743SELECT image_id
744  FROM '.IMAGE_CATEGORY_TABLE.'
745  WHERE category_id = '.$category_id.'
746  ORDER BY '.DB_RANDOM_FUNCTION.'()
747  LIMIT 1
748;';
749    list($representative) = pwg_db_fetch_row(pwg_query($query));
750
751    array_push(
752      $datas,
753      array(
754        'id' => $category_id,
755        'representative_picture_id' => $representative,
756        )
757      );
758  }
759
760  mass_updates(
761    CATEGORIES_TABLE,
762    array(
763      'primary' => array('id'),
764      'update' => array('representative_picture_id')
765      ),
766    $datas
767    );
768}
769
770/**
771 * returns the fulldir for each given category id
772 *
773 * @param array cat_ids
774 * @return array
775 */
776function get_fulldirs($cat_ids)
777{
778  if (count($cat_ids) == 0)
779  {
780    return array();
781  }
782
783  // caching directories of existing categories
784  $query = '
785SELECT id, dir
786  FROM '.CATEGORIES_TABLE.'
787  WHERE dir IS NOT NULL
788;';
789  $cat_dirs = simple_hash_from_query($query, 'id', 'dir');
790
791  // caching galleries_url
792  $query = '
793SELECT id, galleries_url
794  FROM '.SITES_TABLE.'
795;';
796  $galleries_url = simple_hash_from_query($query, 'id', 'galleries_url');
797
798  // categories : id, site_id, uppercats
799  $categories = array();
800
801  $query = '
802SELECT id, uppercats, site_id
803  FROM '.CATEGORIES_TABLE.'
804  WHERE dir IS NOT NULL
805    AND id IN (
806'.wordwrap(implode(', ', $cat_ids), 80, "\n").')
807;';
808  $result = pwg_query($query);
809  while ($row = pwg_db_fetch_assoc($result))
810  {
811    array_push($categories, $row);
812  }
813
814  // filling $cat_fulldirs
815  $cat_fulldirs = array();
816  foreach ($categories as $category)
817  {
818    $uppercats = str_replace(',', '/', $category['uppercats']);
819    $cat_fulldirs[$category['id']] = $galleries_url[$category['site_id']];
820    $cat_fulldirs[$category['id']].= preg_replace('/(\d+)/e',
821                                                  "\$cat_dirs['$1']",
822                                                  $uppercats);
823  }
824
825  return $cat_fulldirs;
826}
827
828/**
829 * returns an array with all file system files according to
830 * $conf['file_ext']
831 *
832 * @param string $path
833 * @param bool recursive
834 * @return array
835 */
836function get_fs($path, $recursive = true)
837{
838  global $conf;
839
840  // because isset is faster than in_array...
841  if (!isset($conf['flip_picture_ext']))
842  {
843    $conf['flip_picture_ext'] = array_flip($conf['picture_ext']);
844  }
845  if (!isset($conf['flip_file_ext']))
846  {
847    $conf['flip_file_ext'] = array_flip($conf['file_ext']);
848  }
849
850  $fs['elements'] = array();
851  $fs['thumbnails'] = array();
852  $fs['representatives'] = array();
853  $subdirs = array();
854
855  if (is_dir($path))
856  {
857    if ($contents = opendir($path))
858    {
859      while (($node = readdir($contents)) !== false)
860      {
861        if ($node == '.' or $node == '..') continue;
862
863        if (is_file($path.'/'.$node))
864        {
865          $extension = get_extension($node);
866
867//          if (in_array($extension, $conf['picture_ext']))
868          if (isset($conf['flip_picture_ext'][$extension]))
869          {
870            if (basename($path) == 'thumbnail')
871            {
872              array_push($fs['thumbnails'], $path.'/'.$node);
873            }
874            else if (basename($path) == 'pwg_representative')
875            {
876              array_push($fs['representatives'], $path.'/'.$node);
877            }
878            else
879            {
880              array_push($fs['elements'], $path.'/'.$node);
881            }
882          }
883//          else if (in_array($extension, $conf['file_ext']))
884          else if (isset($conf['flip_file_ext'][$extension]))
885          {
886            array_push($fs['elements'], $path.'/'.$node);
887          }
888        }
889        else if (is_dir($path.'/'.$node) and $node != 'pwg_high' and $recursive)
890        {
891          array_push($subdirs, $node);
892        }
893      }
894    }
895    closedir($contents);
896
897    foreach ($subdirs as $subdir)
898    {
899      $tmp_fs = get_fs($path.'/'.$subdir);
900
901      $fs['elements']        = array_merge($fs['elements'],
902                                           $tmp_fs['elements']);
903
904      $fs['thumbnails']      = array_merge($fs['thumbnails'],
905                                           $tmp_fs['thumbnails']);
906
907      $fs['representatives'] = array_merge($fs['representatives'],
908                                           $tmp_fs['representatives']);
909    }
910  }
911  return $fs;
912}
913
914/**
915 * synchronize base users list and related users list
916 *
917 * compares and synchronizes base users table (USERS_TABLE) with its child
918 * tables (USER_INFOS_TABLE, USER_ACCESS, USER_CACHE, USER_GROUP) : each
919 * base user must be present in child tables, users in child tables not
920 * present in base table must be deleted.
921 *
922 * @return void
923 */
924function sync_users()
925{
926  global $conf;
927
928  $query = '
929SELECT '.$conf['user_fields']['id'].' AS id
930  FROM '.USERS_TABLE.'
931;';
932  $base_users = array_from_query($query, 'id');
933
934  $query = '
935SELECT user_id
936  FROM '.USER_INFOS_TABLE.'
937;';
938  $infos_users = array_from_query($query, 'user_id');
939
940  // users present in $base_users and not in $infos_users must be added
941  $to_create = array_diff($base_users, $infos_users);
942
943  if (count($to_create) > 0)
944  {
945    create_user_infos($to_create);
946  }
947
948  // users present in user related tables must be present in the base user
949  // table
950  $tables = array(
951    USER_MAIL_NOTIFICATION_TABLE,
952    USER_FEED_TABLE,
953    USER_INFOS_TABLE,
954    USER_ACCESS_TABLE,
955    USER_CACHE_TABLE,
956    USER_CACHE_CATEGORIES_TABLE,
957    USER_GROUP_TABLE
958    );
959
960  foreach ($tables as $table)
961  {
962    $query = '
963SELECT DISTINCT user_id
964  FROM '.$table.'
965;';
966    $to_delete = array_diff(
967      array_from_query($query, 'user_id'),
968      $base_users
969      );
970
971    if (count($to_delete) > 0)
972    {
973      $query = '
974DELETE
975  FROM '.$table.'
976  WHERE user_id in ('.implode(',', $to_delete).')
977;';
978      pwg_query($query);
979    }
980  }
981}
982
983/**
984 * updates categories.uppercats field based on categories.id +
985 * categories.id_uppercat
986 *
987 * @return void
988 */
989function update_uppercats()
990{
991  $query = '
992SELECT id, id_uppercat, uppercats
993  FROM '.CATEGORIES_TABLE.'
994;';
995  $cat_map = hash_from_query($query, 'id');
996
997  $datas = array();
998  foreach ($cat_map as $id => $cat)
999  {
1000    $upper_list = array();
1001
1002    $uppercat = $id;
1003    while ($uppercat)
1004    {
1005      array_push($upper_list, $uppercat);
1006      $uppercat = $cat_map[$uppercat]['id_uppercat'];
1007    }
1008
1009    $new_uppercats = implode(',', array_reverse($upper_list));
1010    if ($new_uppercats != $cat['uppercats'])
1011    {
1012      array_push(
1013        $datas,
1014        array(
1015          'id' => $id,
1016          'uppercats' => $new_uppercats
1017          )
1018        );
1019    }
1020  }
1021  $fields = array('primary' => array('id'), 'update' => array('uppercats'));
1022  mass_updates(CATEGORIES_TABLE, $fields, $datas);
1023}
1024
1025/**
1026 * update images.path field
1027 *
1028 * @return void
1029 */
1030function update_path()
1031{
1032  $query = '
1033SELECT DISTINCT(storage_category_id)
1034  FROM '.IMAGES_TABLE.'
1035  WHERE storage_category_id IS NOT NULL
1036;';
1037  $cat_ids = array_from_query($query, 'storage_category_id');
1038  $fulldirs = get_fulldirs($cat_ids);
1039
1040  foreach ($cat_ids as $cat_id)
1041  {
1042    $query = '
1043UPDATE '.IMAGES_TABLE.'
1044  SET path = '.pwg_db_concat(array("'".$fulldirs[$cat_id]."/'",'file')).'
1045  WHERE storage_category_id = '.$cat_id.'
1046;';
1047    pwg_query($query);
1048  }
1049}
1050
1051/**
1052 * change the parent category of the given categories. The categories are
1053 * supposed virtual.
1054 *
1055 * @param array category identifiers
1056 * @param int parent category identifier
1057 * @return void
1058 */
1059function move_categories($category_ids, $new_parent = -1)
1060{
1061  global $page;
1062
1063  if (count($category_ids) == 0)
1064  {
1065    return;
1066  }
1067
1068  $new_parent = $new_parent < 1 ? 'NULL' : $new_parent;
1069
1070  $categories = array();
1071
1072  $query = '
1073SELECT id, id_uppercat, status, uppercats
1074  FROM '.CATEGORIES_TABLE.'
1075  WHERE id IN ('.implode(',', $category_ids).')
1076;';
1077  $result = pwg_query($query);
1078  while ($row = pwg_db_fetch_assoc($result))
1079  {
1080    $categories[$row['id']] =
1081      array(
1082        'parent' => empty($row['id_uppercat']) ? 'NULL' : $row['id_uppercat'],
1083        'status' => $row['status'],
1084        'uppercats' => $row['uppercats']
1085        );
1086  }
1087
1088  // is the movement possible? The movement is impossible if you try to move
1089  // a category in a sub-category or itself
1090  if ('NULL' != $new_parent)
1091  {
1092    $query = '
1093SELECT uppercats
1094  FROM '.CATEGORIES_TABLE.'
1095  WHERE id = '.$new_parent.'
1096;';
1097    list($new_parent_uppercats) = pwg_db_fetch_row(pwg_query($query));
1098
1099    foreach ($categories as $category)
1100    {
1101      // technically, you can't move a category with uppercats 12,125,13,14
1102      // into a new parent category with uppercats 12,125,13,14,24
1103      if (preg_match('/^'.$category['uppercats'].'(,|$)/', $new_parent_uppercats))
1104      {
1105        array_push(
1106          $page['errors'],
1107          l10n('You cannot move an album in its own sub album')
1108          );
1109        return;
1110      }
1111    }
1112  }
1113
1114  $tables =
1115    array(
1116      USER_ACCESS_TABLE => 'user_id',
1117      GROUP_ACCESS_TABLE => 'group_id'
1118      );
1119
1120  $query = '
1121UPDATE '.CATEGORIES_TABLE.'
1122  SET id_uppercat = '.$new_parent.'
1123  WHERE id IN ('.implode(',', $category_ids).')
1124;';
1125  pwg_query($query);
1126
1127  update_uppercats();
1128  update_global_rank();
1129
1130  // status and related permissions management
1131  if ('NULL' == $new_parent)
1132  {
1133    $parent_status = 'public';
1134  }
1135  else
1136  {
1137    $query = '
1138SELECT status
1139  FROM '.CATEGORIES_TABLE.'
1140  WHERE id = '.$new_parent.'
1141;';
1142    list($parent_status) = pwg_db_fetch_row(pwg_query($query));
1143  }
1144
1145  if ('private' == $parent_status)
1146  {
1147    foreach ($categories as $cat_id => $category)
1148    {
1149      switch ($category['status'])
1150      {
1151        case 'public' :
1152        {
1153          set_cat_status(array($cat_id), 'private');
1154          break;
1155        }
1156        case 'private' :
1157        {
1158          $subcats = get_subcat_ids(array($cat_id));
1159
1160          foreach ($tables as $table => $field)
1161          {
1162            $query = '
1163SELECT '.$field.'
1164  FROM '.$table.'
1165  WHERE cat_id = '.$cat_id.'
1166;';
1167            $category_access = array_from_query($query, $field);
1168
1169            $query = '
1170SELECT '.$field.'
1171  FROM '.$table.'
1172  WHERE cat_id = '.$new_parent.'
1173;';
1174            $parent_access = array_from_query($query, $field);
1175
1176            $to_delete = array_diff($parent_access, $category_access);
1177
1178            if (count($to_delete) > 0)
1179            {
1180              $query = '
1181DELETE FROM '.$table.'
1182  WHERE '.$field.' IN ('.implode(',', $to_delete).')
1183    AND cat_id IN ('.implode(',', $subcats).')
1184;';
1185              pwg_query($query);
1186            }
1187          }
1188          break;
1189        }
1190      }
1191    }
1192  }
1193
1194  array_push(
1195    $page['infos'],
1196    l10n_dec(
1197      '%d album moved', '%d albums moved',
1198      count($categories)
1199      )
1200    );
1201}
1202
1203/**
1204 * create a virtual category
1205 *
1206 * @param string category name
1207 * @param int parent category id
1208 * @return array with ('info' and 'id') or ('error') key
1209 */
1210function create_virtual_category($category_name, $parent_id=null)
1211{
1212  global $conf, $user;
1213
1214  // is the given category name only containing blank spaces ?
1215  if (preg_match('/^\s*$/', $category_name))
1216  {
1217    return array('error' => l10n('The name of an album must not be empty'));
1218  }
1219
1220  $parent_id = !empty($parent_id) ? $parent_id : 'NULL';
1221
1222  $insert = array(
1223    'name' => $category_name,
1224    'rank' => 0,
1225    'commentable' => boolean_to_string($conf['newcat_default_commentable']),
1226    );
1227
1228  if ($parent_id != 'NULL')
1229  {
1230    $query = '
1231SELECT id, uppercats, global_rank, visible, status
1232  FROM '.CATEGORIES_TABLE.'
1233  WHERE id = '.$parent_id.'
1234;';
1235    $parent = pwg_db_fetch_assoc(pwg_query($query));
1236
1237    $insert['id_uppercat'] = $parent['id'];
1238    $insert['global_rank'] = $parent['global_rank'].'.'.$insert['rank'];
1239
1240    // at creation, must a category be visible or not ? Warning : if the
1241    // parent category is invisible, the category is automatically create
1242    // invisible. (invisible = locked)
1243    if ('false' == $parent['visible'])
1244    {
1245      $insert['visible'] = 'false';
1246    }
1247    else
1248    {
1249      $insert['visible'] = boolean_to_string($conf['newcat_default_visible']);
1250    }
1251
1252    // at creation, must a category be public or private ? Warning : if the
1253    // parent category is private, the category is automatically create
1254    // private.
1255    if ('private' == $parent['status'])
1256    {
1257      $insert['status'] = 'private';
1258    }
1259    else
1260    {
1261      $insert['status'] = $conf['newcat_default_status'];
1262    }
1263  }
1264  else
1265  {
1266    $insert['visible'] = boolean_to_string($conf['newcat_default_visible']);
1267    $insert['status'] = $conf['newcat_default_status'];
1268    $insert['global_rank'] = $insert['rank'];
1269  }
1270
1271  // we have then to add the virtual category
1272  mass_inserts(
1273    CATEGORIES_TABLE,
1274    array(
1275      'site_id', 'name', 'id_uppercat', 'rank', 'commentable',
1276      'visible', 'status', 'global_rank',
1277      ),
1278    array($insert)
1279    );
1280
1281  $inserted_id = pwg_db_insert_id(CATEGORIES_TABLE);
1282
1283  $query = '
1284UPDATE
1285  '.CATEGORIES_TABLE.'
1286  SET uppercats = \''.
1287    (isset($parent) ? $parent{'uppercats'}.',' : '').
1288    $inserted_id.
1289    '\'
1290  WHERE id = '.$inserted_id.'
1291;';
1292  pwg_query($query);
1293
1294  update_global_rank();
1295
1296  if ('private' == $insert['status'])
1297  {
1298    add_permission_on_category($inserted_id, array_unique(array_merge(get_admins(), array($user['id']))));
1299  }
1300
1301  return array(
1302    'info' => l10n('Virtual album added'),
1303    'id'   => $inserted_id,
1304    );
1305}
1306
1307/**
1308 * Set tags to an image. Warning: given tags are all tags associated to the
1309 * image, not additionnal tags.
1310 *
1311 * @param array tag ids
1312 * @param int image id
1313 * @return void
1314 */
1315function set_tags($tags, $image_id)
1316{
1317  $query = '
1318DELETE
1319  FROM '.IMAGE_TAG_TABLE.'
1320  WHERE image_id = '.$image_id.'
1321;';
1322  pwg_query($query);
1323
1324  if (count($tags) > 0)
1325  {
1326    $inserts = array();
1327    foreach ($tags as $tag_id)
1328    {
1329      array_push(
1330        $inserts,
1331        array(
1332          'tag_id' => $tag_id,
1333          'image_id' => $image_id
1334          )
1335        );
1336    }
1337    mass_inserts(
1338      IMAGE_TAG_TABLE,
1339      array_keys($inserts[0]),
1340      $inserts
1341      );
1342  }
1343}
1344
1345/**
1346 * Add new tags to a set of images.
1347 *
1348 * @param array tag ids
1349 * @param array image ids
1350 * @return void
1351 */
1352function add_tags($tags, $images)
1353{
1354  if (count($tags) == 0 or count($images) == 0)
1355  {
1356    return;
1357  }
1358
1359  // we can't insert twice the same {image_id,tag_id} so we must first
1360  // delete lines we'll insert later
1361  $query = '
1362DELETE
1363  FROM '.IMAGE_TAG_TABLE.'
1364  WHERE image_id IN ('.implode(',', $images).')
1365    AND tag_id IN ('.implode(',', $tags).')
1366;';
1367  pwg_query($query);
1368
1369  $inserts = array();
1370  foreach ($images as $image_id)
1371  {
1372    foreach ($tags as $tag_id)
1373    {
1374      array_push(
1375        $inserts,
1376        array(
1377          'image_id' => $image_id,
1378          'tag_id' => $tag_id,
1379          )
1380        );
1381    }
1382  }
1383  mass_inserts(
1384    IMAGE_TAG_TABLE,
1385    array_keys($inserts[0]),
1386    $inserts
1387    );
1388}
1389
1390/**
1391 *
1392 */
1393function delete_tags($tag_ids)
1394{
1395  if (is_numeric($tag_ids))
1396  {
1397    $tag_ids = array($tag_ids);
1398  }
1399
1400  if (!is_array($tag_ids))
1401  {
1402    return false;
1403  }
1404 
1405  $query = '
1406DELETE
1407  FROM '.IMAGE_TAG_TABLE.'
1408  WHERE tag_id IN ('.implode(',', $tag_ids).')
1409;';
1410  pwg_query($query);
1411 
1412  $query = '
1413DELETE
1414  FROM '.TAGS_TABLE.'
1415  WHERE id IN ('.implode(',', $tag_ids).')
1416;';
1417  pwg_query($query);
1418}
1419
1420function tag_id_from_tag_name($tag_name)
1421{
1422  global $page;
1423
1424  $tag_name = trim($tag_name);
1425  if (isset($page['tag_id_from_tag_name_cache'][$tag_name]))
1426  {
1427    return $page['tag_id_from_tag_name_cache'][$tag_name];
1428  }
1429
1430  // does the tag already exists?
1431  $query = '
1432SELECT id
1433  FROM '.TAGS_TABLE.'
1434  WHERE name = \''.$tag_name.'\'
1435;';
1436  $existing_tags = array_from_query($query, 'id');
1437
1438  if (count($existing_tags) == 0)
1439  {
1440    mass_inserts(
1441      TAGS_TABLE,
1442      array('name', 'url_name'),
1443      array(
1444        array(
1445          'name' => $tag_name,
1446          'url_name' => trigger_event('render_tag_url', $tag_name),
1447          )
1448        )
1449      );
1450
1451    $page['tag_id_from_tag_name_cache'][$tag_name] = pwg_db_insert_id(TAGS_TABLE);
1452  }
1453  else
1454  {
1455    $page['tag_id_from_tag_name_cache'][$tag_name] = $existing_tags[0];
1456  }
1457
1458  return $page['tag_id_from_tag_name_cache'][$tag_name];
1459}
1460
1461function set_tags_of($tags_of)
1462{
1463  if (count($tags_of) > 0)
1464  {
1465    $query = '
1466DELETE
1467  FROM '.IMAGE_TAG_TABLE.'
1468  WHERE image_id IN ('.implode(',', array_keys($tags_of)).')
1469;';
1470    pwg_query($query);
1471
1472    $inserts = array();
1473
1474    foreach ($tags_of as $image_id => $tag_ids)
1475    {
1476      foreach ($tag_ids as $tag_id)
1477      {
1478        array_push(
1479          $inserts,
1480          array(
1481            'image_id' => $image_id,
1482            'tag_id' => $tag_id,
1483            )
1484          );
1485      }
1486    }
1487
1488    mass_inserts(
1489      IMAGE_TAG_TABLE,
1490      array_keys($inserts[0]),
1491      $inserts
1492      );
1493  }
1494}
1495
1496/**
1497 * Associate a list of images to a list of categories.
1498 *
1499 * The function will not duplicate links
1500 *
1501 * @param array images
1502 * @param array categories
1503 * @return void
1504 */
1505function associate_images_to_categories($images, $categories)
1506{
1507  if (count($images) == 0
1508      or count($categories) == 0)
1509  {
1510    return false;
1511  }
1512
1513  $query = '
1514DELETE
1515  FROM '.IMAGE_CATEGORY_TABLE.'
1516  WHERE image_id IN ('.implode(',', $images).')
1517    AND category_id IN ('.implode(',', $categories).')
1518;';
1519  pwg_query($query);
1520
1521  $inserts = array();
1522  foreach ($categories as $category_id)
1523  {
1524    foreach ($images as $image_id)
1525    {
1526      array_push(
1527        $inserts,
1528        array(
1529          'image_id' => $image_id,
1530          'category_id' => $category_id,
1531          )
1532        );
1533    }
1534  }
1535
1536  mass_inserts(
1537    IMAGE_CATEGORY_TABLE,
1538    array_keys($inserts[0]),
1539    $inserts
1540    );
1541
1542  update_category($categories);
1543}
1544
1545/**
1546 * Associate images associated to a list of source categories to a list of
1547 * destination categories.
1548 *
1549 * @param array sources
1550 * @param array destinations
1551 * @return void
1552 */
1553function associate_categories_to_categories($sources, $destinations)
1554{
1555  if (count($sources) == 0)
1556  {
1557    return false;
1558  }
1559
1560  $query = '
1561SELECT image_id
1562  FROM '.IMAGE_CATEGORY_TABLE.'
1563  WHERE category_id IN ('.implode(',', $sources).')
1564;';
1565  $images = array_from_query($query, 'image_id');
1566
1567  associate_images_to_categories($images, $destinations);
1568}
1569
1570/**
1571 * Refer main Piwigo URLs (currently PHPWG_DOMAIN domain)
1572 *
1573 * @param void
1574 * @return array like $conf['links']
1575 */
1576function pwg_URL()
1577{
1578  $urls = array(
1579    'HOME'       => 'http://'.PHPWG_DOMAIN,
1580    'WIKI'       => 'http://'.PHPWG_DOMAIN.'/doc',
1581    'DEMO'       => 'http://'.PHPWG_DOMAIN.'/demo',
1582    'FORUM'      => 'http://'.PHPWG_DOMAIN.'/forum',
1583    'BUGS'       => 'http://'.PHPWG_DOMAIN.'/bugs',
1584    'EXTENSIONS' => 'http://'.PHPWG_DOMAIN.'/ext',
1585    );
1586  return $urls;
1587}
1588
1589/**
1590 * Invalidates cahed data (permissions and category counts) for all users.
1591 */
1592function invalidate_user_cache($full = true)
1593{
1594  if ($full)
1595  {
1596    $query = '
1597TRUNCATE TABLE '.USER_CACHE_CATEGORIES_TABLE.';';
1598    pwg_query($query);
1599    $query = '
1600TRUNCATE TABLE '.USER_CACHE_TABLE.';';
1601    pwg_query($query);
1602  }
1603  else
1604  {
1605    $query = '
1606UPDATE '.USER_CACHE_TABLE.'
1607  SET need_update = \'true\';';
1608    pwg_query($query);
1609  }
1610  trigger_action('invalidate_user_cache', $full);
1611}
1612
1613/**
1614 * adds the caracter set to a create table sql query.
1615 * all CREATE TABLE queries must call this function
1616 * @param string query - the sql query
1617 */
1618function create_table_add_character_set($query)
1619{
1620  defined('DB_CHARSET') or fatal_error('create_table_add_character_set DB_CHARSET undefined');
1621  if ('DB_CHARSET'!='')
1622  {
1623    if ( version_compare(mysql_get_server_info(), '4.1.0', '<') )
1624    {
1625      return $query;
1626    }
1627    $charset_collate = " DEFAULT CHARACTER SET ".DB_CHARSET;
1628    if (DB_COLLATE!='')
1629    {
1630      $charset_collate .= " COLLATE ".DB_COLLATE;
1631    }
1632    if ( is_array($query) )
1633    {
1634      foreach( $query as $id=>$q)
1635      {
1636        $q=trim($q);
1637        $q=trim($q, ';');
1638        if (preg_match('/^CREATE\s+TABLE/i',$q))
1639        {
1640          $q.=$charset_collate;
1641        }
1642        $q .= ';';
1643        $query[$id] = $q;
1644      }
1645    }
1646    else
1647    {
1648      $query=trim($query);
1649      $query=trim($query, ';');
1650      if (preg_match('/^CREATE\s+TABLE/i',$query))
1651      {
1652        $query.=$charset_collate;
1653      }
1654      $query .= ';';
1655    }
1656  }
1657  return $query;
1658}
1659
1660/**
1661 * Returns array use on template with html_options method
1662 * @param Min and Max access to use
1663 * @return array of user access level
1664 */
1665function get_user_access_level_html_options($MinLevelAccess = ACCESS_FREE, $MaxLevelAccess = ACCESS_CLOSED)
1666{
1667  $tpl_options = array();
1668  for ($level = $MinLevelAccess; $level <= $MaxLevelAccess; $level++)
1669  {
1670    $tpl_options[$level] = l10n(sprintf('ACCESS_%d', $level));
1671  }
1672  return $tpl_options;
1673}
1674
1675/**
1676 * returns a list of templates currently available in template-extension
1677 * Each .tpl file is extracted from template-extension.
1678 * @return array
1679 */
1680function get_extents($start='')
1681{
1682  if ($start == '') { $start = './template-extension'; }
1683  $dir = opendir($start);
1684  $extents = array();
1685
1686  while (($file = readdir($dir)) !== false)
1687  {
1688    if ( $file == '.' or $file == '..' or $file == '.svn') continue;
1689    $path = $start . '/' . $file;
1690    if (is_dir($path))
1691    {
1692      $extents = array_merge($extents, get_extents($path));
1693    }
1694    elseif ( !is_link($path) and file_exists($path)
1695            and get_extension($path) == 'tpl' )
1696    {
1697      $extents[] = substr($path, 21);
1698    }
1699  }
1700  return $extents;
1701}
1702
1703function create_tag($tag_name)
1704{
1705  // does the tag already exists?
1706  $query = '
1707SELECT id
1708  FROM '.TAGS_TABLE.'
1709  WHERE name = \''.$tag_name.'\'
1710;';
1711  $existing_tags = array_from_query($query, 'id');
1712
1713  if (count($existing_tags) == 0)
1714  {
1715    mass_inserts(
1716      TAGS_TABLE,
1717      array('name', 'url_name'),
1718      array(
1719        array(
1720          'name' => $tag_name,
1721          'url_name' => trigger_event('render_tag_url', $tag_name),
1722          )
1723        )
1724      );
1725
1726    $inserted_id = pwg_db_insert_id(TAGS_TABLE);
1727
1728    return array(
1729      'info' => sprintf(
1730        l10n('Tag "%s" was added'),
1731        stripslashes($tag_name)
1732        ),
1733      'id' => $inserted_id,
1734      );
1735  }
1736  else
1737  {
1738    return array(
1739      'error' => sprintf(
1740        l10n('Tag "%s" already exists'),
1741        stripslashes($tag_name)
1742        )
1743      );
1744  }
1745}
1746
1747/**
1748 * Is the category accessible to the (Admin) user ?
1749 *
1750 * Note : if the user is not authorized to see this category, category jump
1751 * will be replaced by admin cat_modify page
1752 *
1753 * @param int category id to verify
1754 * @return bool
1755 */
1756function cat_admin_access($category_id)
1757{
1758  global $user;
1759
1760  // $filter['visible_categories'] and $filter['visible_images']
1761  // are not used because it's not necessary (filter <> restriction)
1762  if (in_array($category_id, explode(',', $user['forbidden_categories'])))
1763  {
1764    return false;
1765  }
1766  return true;
1767}
1768
1769/**
1770 * Retrieve data from external URL
1771 *
1772 * @param string $src: URL
1773 * @param global $dest: can be a file ressource or string
1774 * @return bool
1775 */
1776function fetchRemote($src, &$dest, $get_data=array(), $post_data=array(), $user_agent='Piwigo', $step=0)
1777{
1778  // Try to retrieve data from local file?
1779  if (!url_is_remote($src))
1780  {
1781    $content = @file_get_contents($src);
1782    if ($content !== false)
1783    {
1784      is_resource($dest) ? @fwrite($dest, $content) : $dest = $content;
1785      return true;
1786    }
1787    else
1788    {
1789      return false;
1790    }
1791  }
1792
1793  // After 3 redirections, return false
1794  if ($step > 3) return false;
1795
1796  // Initialization
1797  $method  = empty($post_data) ? 'GET' : 'POST';
1798  $request = empty($post_data) ? '' : http_build_query($post_data, '', '&');
1799  if (!empty($get_data))
1800  {
1801    $src .= strpos($src, '?') === false ? '?' : '&';
1802    $src .= http_build_query($get_data, '', '&');
1803  }
1804
1805  // Initialize $dest
1806  is_resource($dest) or $dest = '';
1807
1808  // Try curl to read remote file
1809  if (function_exists('curl_init'))
1810  {
1811    $ch = @curl_init();
1812    @curl_setopt($ch, CURLOPT_URL, $src);
1813    @curl_setopt($ch, CURLOPT_HEADER, 1);
1814    @curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
1815    @curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
1816    if ($method == 'POST')
1817    {
1818      @curl_setopt($ch, CURLOPT_POST, 1);
1819      @curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
1820    }
1821    $content = @curl_exec($ch);
1822    $header_length = @curl_getinfo($ch, CURLINFO_HEADER_SIZE);
1823    $status = @curl_getinfo($ch, CURLINFO_HTTP_CODE);
1824    @curl_close($ch);
1825    if ($content !== false and $status >= 200 and $status < 400)
1826    {
1827      if (preg_match('/Location:\s+?(.+)/', substr($content, 0, $header_length), $m))
1828      {
1829        return fetchRemote($m[1], $dest, array(), array(), $user_agent, $step+1);
1830      }
1831      $content = substr($content, $header_length);
1832      is_resource($dest) ? @fwrite($dest, $content) : $dest = $content;
1833      return true;
1834    }
1835  }
1836
1837  // Try file_get_contents to read remote file
1838  if (ini_get('allow_url_fopen'))
1839  {
1840    $opts = array(
1841      'http' => array(
1842        'method' => $method,
1843        'user_agent' => $user_agent,
1844      )
1845    );
1846    if ($method == 'POST')
1847    {
1848      $opts['http']['content'] = $request;
1849    }
1850    $context = @stream_context_create($opts);
1851    $content = @file_get_contents($src, false, $context);
1852    if ($content !== false)
1853    {
1854      is_resource($dest) ? @fwrite($dest, $content) : $dest = $content;
1855      return true;
1856    }
1857  }
1858
1859  // Try fsockopen to read remote file
1860  $src = parse_url($src);
1861  $host = $src['host'];
1862  $path = isset($src['path']) ? $src['path'] : '/';
1863  $path .= isset($src['query']) ? '?'.$src['query'] : '';
1864
1865  if (($s = @fsockopen($host,80,$errno,$errstr,5)) === false)
1866  {
1867    return false;
1868  }
1869
1870  $http_request  = $method." ".$path." HTTP/1.0\r\n";
1871  $http_request .= "Host: ".$host."\r\n";
1872  if ($method == 'POST')
1873  {
1874    $http_request .= "Content-Type: application/x-www-form-urlencoded;\r\n";
1875    $http_request .= "Content-Length: ".strlen($request)."\r\n";
1876  }
1877  $http_request .= "User-Agent: ".$user_agent."\r\n";
1878  $http_request .= "Accept: */*\r\n";
1879  $http_request .= "\r\n";
1880  $http_request .= $request;
1881
1882  fwrite($s, $http_request);
1883
1884  $i = 0;
1885  $in_content = false;
1886  while (!feof($s))
1887  {
1888    $line = fgets($s);
1889
1890    if (rtrim($line,"\r\n") == '' && !$in_content)
1891    {
1892      $in_content = true;
1893      $i++;
1894      continue;
1895    }
1896    if ($i == 0)
1897    {
1898      if (!preg_match('/HTTP\/(\\d\\.\\d)\\s*(\\d+)\\s*(.*)/',rtrim($line,"\r\n"), $m))
1899      {
1900        fclose($s);
1901        return false;
1902      }
1903      $status = (integer) $m[2];
1904      if ($status < 200 || $status >= 400)
1905      {
1906        fclose($s);
1907        return false;
1908      }
1909    }
1910    if (!$in_content)
1911    {
1912      if (preg_match('/Location:\s+?(.+)$/',rtrim($line,"\r\n"),$m))
1913      {
1914        fclose($s);
1915        return fetchRemote(trim($m[1]),$dest,array(),array(),$user_agent,$step+1);
1916      }
1917      $i++;
1918      continue;
1919    }
1920    is_resource($dest) ? @fwrite($dest, $line) : $dest .= $line;
1921    $i++;
1922  }
1923  fclose($s);
1924  return true;
1925}
1926
1927
1928/**
1929 * returns the groupname corresponding to the given group identifier if
1930 * exists
1931 *
1932 * @param int group_id
1933 * @return mixed
1934 */
1935function get_groupname($group_id)
1936{
1937  $query = '
1938SELECT name
1939  FROM '.GROUPS_TABLE.'
1940  WHERE id = '.intval($group_id).'
1941;';
1942  $result = pwg_query($query);
1943  if (pwg_db_num_rows($result) > 0)
1944  {
1945    list($groupname) = pwg_db_fetch_row($result);
1946  }
1947  else
1948  {
1949    return false;
1950  }
1951
1952  return $groupname;
1953}
1954
1955/**
1956 * returns the username corresponding to the given user identifier if exists
1957 *
1958 * @param int user_id
1959 * @return mixed
1960 */
1961function get_username($user_id)
1962{
1963  global $conf;
1964
1965  $query = '
1966SELECT '.$conf['user_fields']['username'].'
1967  FROM '.USERS_TABLE.'
1968  WHERE '.$conf['user_fields']['id'].' = '.intval($user_id).'
1969;';
1970  $result = pwg_query($query);
1971  if (pwg_db_num_rows($result) > 0)
1972  {
1973    list($username) = pwg_db_fetch_row($result);
1974  }
1975  else
1976  {
1977    return false;
1978  }
1979
1980  return stripslashes($username);
1981}
1982
1983function get_newsletter_subscribe_base_url($language) {
1984  $subscribe_domain = 'piwigo.org';
1985
1986  $domain_of = array(
1987    'fr_FR' => 'fr.piwigo.org',
1988    'it_IT' => 'it.piwigo.org',
1989    'de_DE' => 'de.piwigo.org',
1990    'es_ES' => 'es.piwigo.org',
1991    'zh_CN' => 'cn.piwigo.org',
1992    'pl_PL' => 'pl.piwigo.org',
1993    'hu_HU' => 'hu.piwigo.org',
1994    'ru_RU' => 'ru.piwigo.org',
1995        'nl_NL' => 'nl.piwigo.org',
1996    );
1997
1998  if (isset($domain_of[$language])) {
1999    $subscribe_domain = $domain_of[$language];
2000  }
2001
2002  return 'http://'.$subscribe_domain.'/announcement/subscribe/';
2003}
2004
2005/**
2006 * Accordion menus need to know which section to open by default when
2007 * loading the page
2008 */
2009function get_active_menu($menu_page)
2010{
2011  global $page;
2012
2013  if (isset($page['active_menu']))
2014  {
2015    return $page['active_menu'];
2016  }
2017
2018  switch ($menu_page)
2019  {
2020    case 'photos_add':
2021    case 'rating':
2022    case 'tags':
2023    case 'picture_modify':
2024    case 'batch_manager':
2025      return 0;
2026
2027    case 'cat_list':
2028    case 'cat_modify':
2029    case 'cat_move':
2030    case 'cat_options':
2031    case 'cat_perm':
2032    case 'permalinks':
2033      return 1;
2034
2035    case 'user_list':
2036    case 'user_perm':
2037    case 'group_list':
2038    case 'group_perm':
2039    case 'notification_by_mail':
2040      return 2;
2041
2042    case 'plugins':
2043    case 'plugin':
2044      return 3;
2045
2046    case 'site_manager':
2047    case 'site_update':
2048    case 'stats':
2049    case 'history':
2050    case 'maintenance':
2051    case 'thumbnail':
2052    case 'comments':
2053    case 'updates':
2054      return 4;
2055
2056    case 'configuration':
2057    case 'extend_for_templates':
2058    case 'menubar':
2059    case 'themes':
2060    case 'theme':
2061    case 'languages':
2062      return 5;
2063  }
2064  return 0;
2065}
2066
2067function get_taglist($query, $only_user_language=true)
2068{
2069  $result = pwg_query($query);
2070 
2071  $taglist = array();
2072  while ($row = pwg_db_fetch_assoc($result))
2073  {
2074    if (!$only_user_language and preg_match_all('#\[lang=(.*?)\](.*?)\[/lang\]#is', $row['name'], $matches))
2075    {
2076      foreach ($matches[2] as $tag_name)
2077      {
2078        array_push(
2079          $taglist,
2080          array(
2081            'name' => trigger_event('render_tag_name', $tag_name),
2082            'id' => '~~'.$row['id'].'~~',
2083            )
2084          );
2085      }
2086
2087      $row['name'] = preg_replace('#\[lang=(.*?)\](.*?)\[/lang\]#is', null, $row['name']);
2088    }
2089   
2090    if (strlen($row['name']) > 0)
2091    {
2092      array_push(
2093        $taglist,
2094        array(
2095          'name' => trigger_event('render_tag_name', $row['name']),
2096          'id' => '~~'.$row['id'].'~~',
2097          )
2098        );
2099    }
2100  }
2101 
2102  $cmp = create_function('$a,$b', 'return strcasecmp($a["name"], $b["name"]);');
2103  usort($taglist, $cmp);
2104
2105  return $taglist;
2106}
2107
2108function get_tag_ids($raw_tags, $allow_create=true)
2109{
2110  // In $raw_tags we receive something like array('~~6~~', '~~59~~', 'New
2111  // tag', 'Another new tag') The ~~34~~ means that it is an existing
2112  // tag. I've added the surrounding ~~ to permit creation of tags like "10"
2113  // or "1234" (numeric characters only)
2114
2115  $tag_ids = array();
2116  $raw_tags = explode(',',$raw_tags);
2117
2118  foreach ($raw_tags as $raw_tag)
2119  {
2120    if (preg_match('/^~~(\d+)~~$/', $raw_tag, $matches))
2121    {
2122      array_push($tag_ids, $matches[1]);
2123    }
2124    elseif ($allow_create)
2125    {
2126      // we have to create a new tag
2127      $tag_ids[] = tag_id_from_tag_name($raw_tag);
2128    }
2129  }
2130
2131  return $tag_ids;
2132}
2133
2134/** returns the argument_ids array with new sequenced keys based on related
2135 * names. Sequence is not case sensitive.
2136 * Warning: By definition, this function breaks original keys
2137 */
2138function order_by_name($element_ids,$name)
2139{
2140  $ordered_element_ids = array();
2141  foreach ($element_ids as $k_id => $element_id)
2142  {
2143    $key = strtolower($name[$element_id]) .'-'. $name[$element_id] .'-'. $k_id;
2144    $ordered_element_ids[$key] = $element_id;
2145  }
2146  ksort($ordered_element_ids);
2147  return $ordered_element_ids;
2148}
2149
2150function add_permission_on_category($category_ids, $user_ids)
2151{
2152  // array-ify categories and users
2153  if (!is_array($category_ids))
2154  {
2155    $category_ids = array($category_ids);
2156  }
2157
2158  if (!is_array($user_ids))
2159  {
2160    $user_ids = array($user_ids);
2161  }
2162
2163  // check for emptiness
2164  if (count($category_ids) == 0 or count($user_ids) == 0)
2165  {
2166    return;
2167  }
2168 
2169  // make sure categories are private and select uppercats or subcats
2170  $cat_ids = (isset($_POST['apply_on_sub'])) ? implode(',', get_subcat_ids($category_ids)).",".implode(',', get_uppercat_ids($category_ids)) : implode(',', get_uppercat_ids($category_ids));
2171  $query = '
2172SELECT
2173    id
2174  FROM '.CATEGORIES_TABLE.'
2175  WHERE id IN ('.$cat_ids.')
2176    AND status = \'private\'
2177;';
2178  $private_cats = array_from_query($query, 'id');
2179
2180  if (count($private_cats) == 0)
2181  {
2182    return;
2183  }
2184 
2185  // We must not reinsert already existing lines in user_access table
2186  $granteds = array();
2187  foreach ($private_cats as $cat_id)
2188  {
2189    $granteds[$cat_id] = array();
2190  }
2191 
2192  $query = '
2193SELECT
2194    user_id,
2195    cat_id
2196  FROM '.USER_ACCESS_TABLE.'
2197  WHERE cat_id IN ('.implode(',', $private_cats).')
2198    AND user_id IN ('.implode(',', $user_ids).')
2199;';
2200  $result = pwg_query($query);
2201  while ($row = pwg_db_fetch_assoc($result))
2202  {
2203    array_push($granteds[$row['cat_id']], $row['user_id']);
2204  }
2205
2206  $inserts = array();
2207 
2208  foreach ($private_cats as $cat_id)
2209  {
2210    $grant_to_users = array_diff($user_ids, $granteds[$cat_id]);
2211   
2212    foreach ($grant_to_users as $user_id)
2213    {
2214      array_push(
2215        $inserts,
2216        array(
2217          'user_id' => $user_id,
2218          'cat_id' => $cat_id
2219          )
2220        );
2221    }
2222  }
2223
2224  if (count($inserts) > 0)
2225  {
2226    mass_inserts(USER_ACCESS_TABLE, array_keys($inserts[0]), $inserts);
2227  }
2228}
2229
2230
2231function get_admins($include_webmaster=true)
2232{
2233  $status_list = array('admin');
2234
2235  if ($include_webmaster)
2236  {
2237    $status_list[] = 'webmaster';
2238  }
2239 
2240  $query = '
2241SELECT
2242    user_id
2243  FROM '.USER_INFOS_TABLE.'
2244  WHERE status in (\''.implode("','", $status_list).'\')
2245;';
2246
2247  return array_from_query($query, 'user_id');
2248}
2249?>
Note: See TracBrowser for help on using the repository browser.