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

Last change on this file since 362 was 362, checked in by z0rglub, 20 years ago

header global refactoring

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 30.2 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// |                             functions.php                             |
4// +-----------------------------------------------------------------------+
5// | application   : PhpWebGallery <http://phpwebgallery.net>              |
6// | branch        : BSF (Best So Far)                                     |
7// +-----------------------------------------------------------------------+
8// | file          : $RCSfile$
9// | last update   : $Date: 2004-02-11 23:20:38 +0000 (Wed, 11 Feb 2004) $
10// | last modifier : $Author: z0rglub $
11// | revision      : $Revision: 362 $
12// +-----------------------------------------------------------------------+
13// | This program is free software; you can redistribute it and/or modify  |
14// | it under the terms of the GNU General Public License as published by  |
15// | the Free Software Foundation                                          |
16// |                                                                       |
17// | This program is distributed in the hope that it will be useful, but   |
18// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
19// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
20// | General Public License for more details.                              |
21// |                                                                       |
22// | You should have received a copy of the GNU General Public License     |
23// | along with this program; if not, write to the Free Software           |
24// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
25// | USA.                                                                  |
26// +-----------------------------------------------------------------------+
27
28$tab_ext_create_TN = array ( 'jpg', 'png', 'JPG', 'PNG' );
29
30// is_image returns true if the given $filename (including the path) is a
31// picture according to its format and its extension.
32// As GD library can only generate pictures from jpeg and png files, if you
33// ask if the filename is an image for thumbnail creation (second parameter
34// set to true), the only authorized formats are jpeg and png.
35function is_image( $filename, $create_thumbnail = false )
36{
37  global $conf, $tab_ext_create_TN;
38
39  if ( is_file( $filename ) )
40  {
41    $size = getimagesize( $filename );
42    // $size[2] == 1 means GIF
43    // $size[2] == 2 means JPG
44    // $size[2] == 3 means PNG
45    if ( !$create_thumbnail )
46    {
47      if ( in_array( get_extension( $filename ), $conf['picture_ext'] )
48           and ( $size[2] == 1 or $size[2] == 2 or $size[2] == 3 ) )
49      {
50        return true;
51      }
52    }
53    else
54    {
55      if ( in_array( get_extension( $filename ), $tab_ext_create_TN )
56           and ( $size[2] == 2 or $size[2] == 3 ) )
57      {
58        return true;
59      }
60    }
61  }
62  return false;
63}
64
65/**
66 * returns an array with all picture files according to $conf['picture_ext']
67 *
68 * @param string $dir
69 * @return array
70 */
71function get_picture_files( $dir )
72{
73  global $conf;
74
75  $pictures = array();
76  if ( $opendir = opendir( $dir ) )
77  {
78    while ( $file = readdir( $opendir ) )
79    {
80      if ( in_array( get_extension( $file ), $conf['picture_ext'] ) )
81      {
82        array_push( $pictures, $file );
83      }
84    }
85  }
86  return $pictures;
87}
88
89/**
90 * returns an array with all thumbnails according to $conf['picture_ext']
91 * and $conf['prefix_thumbnail']
92 *
93 * @param string $dir
94 * @return array
95 */
96function get_thumb_files( $dir )
97{
98  global $conf;
99
100  $prefix_length = strlen( $conf['prefix_thumbnail'] );
101 
102  $thumbnails = array();
103  if ( $opendir = @opendir( $dir ) )
104  {
105    while ( $file = readdir( $opendir ) )
106    {
107      if ( in_array( get_extension( $file ), $conf['picture_ext'] )
108           and substr($file,0,$prefix_length) == $conf['prefix_thumbnail'] )
109      {
110        array_push( $thumbnails, $file );
111      }
112    }
113  }
114  return $thumbnails;
115}
116
117function TN_exists( $dir, $file )
118{
119  global $conf;
120
121  $filename = get_filename_wo_extension( $file );
122  foreach ( $conf['picture_ext'] as $ext ) {
123    $test = $dir.'/thumbnail/'.$conf['prefix_thumbnail'].$filename.'.'.$ext;
124    if ( is_file ( $test ) )
125    {
126      return $ext;
127    }
128  }
129  return false;
130}
131       
132
133// The function delete_site deletes a site
134// and call the function delete_category for each primary category of the site
135function delete_site( $id )
136{
137  // destruction of the categories of the site
138  $query = 'SELECT id';
139  $query.= ' FROM '.PREFIX_TABLE.'categories';
140  $query.= ' WHERE site_id = '.$id;
141  $query.= ';';
142  $result = mysql_query( $query );
143  while ( $row = mysql_fetch_array( $result ) )
144  {
145    delete_category( $row['id'] );
146  }
147               
148  // destruction of the site
149  $query = 'DELETE FROM '.PREFIX_TABLE.'sites';
150  $query.= ' WHERE id = '.$id;
151  $query.= ';';
152  mysql_query( $query );
153}
154       
155
156// The function delete_category deletes the category identified by the $id
157// It also deletes (in the database) :
158//    - all the images of the images (thanks to delete_image, see further)
159//    - all the links between images and this category
160//    - all the restrictions linked to the category
161// The function works recursively.
162function delete_category( $id )
163{
164  // destruction of all the related images
165  $query = 'SELECT id';
166  $query.= ' FROM '.PREFIX_TABLE.'images';
167  $query.= ' WHERE storage_category_id = '.$id;
168  $query.= ';';
169  $result = mysql_query( $query );
170  while ( $row = mysql_fetch_array( $result ) )
171  {
172    delete_image( $row['id'] );
173  }
174
175  // destruction of the links between images and this category
176  $query = 'DELETE FROM '.PREFIX_TABLE.'image_category';
177  $query.= ' WHERE category_id = '.$id;
178  $query.= ';';
179  mysql_query( $query );
180
181  // destruction of the access linked to the category
182  $query = 'DELETE FROM '.PREFIX_TABLE.'user_access';
183  $query.= ' WHERE cat_id = '.$id;
184  $query.= ';';
185  mysql_query( $query );
186  $query = 'DELETE FROM '.PREFIX_TABLE.'group_access';
187  $query.= ' WHERE cat_id = '.$id;
188  $query.= ';';
189  mysql_query( $query );
190
191  // destruction of the sub-categories
192  $query = 'SELECT id';
193  $query.= ' FROM '.PREFIX_TABLE.'categories';
194  $query.= ' WHERE id_uppercat = '.$id;
195  $query.= ';';
196  $result = mysql_query( $query );
197  while( $row = mysql_fetch_array( $result ) )
198  {
199    delete_category( $row['id'] );
200  }
201
202  // destruction of the category
203  $query = 'DELETE FROM '.PREFIX_TABLE.'categories';
204  $query.= ' WHERE id = '.$id;
205  $query.= ';';
206  mysql_query( $query );
207}
208       
209
210// The function delete_image deletes the image identified by the $id
211// It also deletes (in the database) :
212//    - all the comments related to the image
213//    - all the links between categories and this image
214//    - all the favorites associated to the image
215function delete_image( $id )
216{
217  global $count_deleted;
218               
219  // destruction of the comments on the image
220  $query = 'DELETE FROM '.PREFIX_TABLE.'comments';
221  $query.= ' WHERE image_id = '.$id;
222  $query.= ';';
223  mysql_query( $query );
224
225  // destruction of the links between images and this category
226  $query = 'DELETE FROM '.PREFIX_TABLE.'image_category';
227  $query.= ' WHERE image_id = '.$id;
228  $query.= ';';
229  mysql_query( $query );
230
231  // destruction of the favorites associated with the picture
232  $query = 'DELETE FROM '.PREFIX_TABLE.'favorites';
233  $query.= ' WHERE image_id = '.$id;
234  $query.= ';';
235  mysql_query( $query );
236               
237  // destruction of the image
238  $query = 'DELETE FROM '.PREFIX_TABLE.'images';
239  $query.= ' WHERE id = '.$id;
240  $query.= ';';
241  mysql_query( $query );
242  $count_deleted++;
243}
244
245// The delete_user function delete a user identified by the $user_id
246// It also deletes :
247//     - all the access linked to this user
248//     - all the links to any group
249//     - all the favorites linked to this user
250//     - all sessions linked to this user
251//     - all categories informations linked to this user
252function delete_user( $user_id )
253{
254  // destruction of the access linked to the user
255  $query = 'DELETE FROM '.PREFIX_TABLE.'user_access';
256  $query.= ' WHERE user_id = '.$user_id;
257  $query.= ';';
258  mysql_query( $query );
259
260  // destruction of the group links for this user
261  $query = 'DELETE FROM '.PREFIX_TABLE.'user_group';
262  $query.= ' WHERE user_id = '.$user_id;
263  $query.= ';';
264  mysql_query( $query );
265
266  // destruction of the favorites associated with the user
267  $query = 'DELETE FROM '.PREFIX_TABLE.'favorites';
268  $query.= ' WHERE user_id = '.$user_id;
269  $query.= ';';
270  mysql_query( $query );
271
272  // destruction of the sessions linked with the user
273  $query = 'DELETE FROM '.PREFIX_TABLE.'sessions';
274  $query.= ' WHERE user_id = '.$user_id;
275  $query.= ';';
276  mysql_query( $query );
277
278  // destruction of the categories informations linked with the user
279  $query = 'DELETE FROM '.PREFIX_TABLE.'user_category';
280  $query.= ' WHERE user_id = '.$user_id;
281  $query.= ';';
282  mysql_query( $query );
283
284  // destruction of the user
285  $query = 'DELETE FROM '.PREFIX_TABLE.'users';
286  $query.= ' WHERE id = '.$user_id;
287  $query.= ';';
288  mysql_query( $query );
289}
290
291// delete_group deletes a group identified by its $group_id.
292// It also deletes :
293//     - all the access linked to this group
294//     - all the links between this group and any user
295function delete_group( $group_id )
296{
297  // destruction of the access linked to the group
298  $query = 'DELETE FROM '.PREFIX_TABLE.'group_access';
299  $query.= ' WHERE group_id = '.$group_id;
300  $query.= ';';
301  mysql_query( $query );
302
303  // synchronize all users linked to the group
304  synchronize_group( $group_id );
305
306  // destruction of the users links for this group
307  $query = 'DELETE FROM '.PREFIX_TABLE.'user_group';
308  $query.= ' WHERE group_id = '.$group_id;
309  $query.= ';';
310  mysql_query( $query );
311
312  // destruction of the group
313  $query = 'DELETE FROM '.PREFIX_TABLE.'groups';
314  $query.= ' WHERE id = '.$group_id;
315  $query.= ';';
316  mysql_query( $query );
317}
318
319// The check_favorites function deletes all the favorites of a user if he is
320// not allowed to see them (the category or an upper category is restricted
321// or invisible)
322function check_favorites( $user_id )
323{
324  $query = 'SELECT status,forbidden_categories';
325  $query.= ' FROM '.PREFIX_TABLE.'users';
326  $query.= ' WHERE id = '.$user_id;
327  $query.= ';';
328  $row = mysql_fetch_array( mysql_query( $query ) );
329  $status = $row['status'];
330  // retrieving all the restricted categories for this user
331  if ( isset( $row['forbidden_categories'] ) )
332    $restricted_cat = explode( ',', $row['forbidden_categories'] );
333  else
334    $restricted_cat = array();
335  // retrieving all the favorites for this user and comparing their
336  // categories to the restricted categories
337  $query = 'SELECT image_id';
338  $query.= ' FROM '.PREFIX_TABLE.'favorites';
339  $query.= ' WHERE user_id = '.$user_id;
340  $query.= ';';
341  $result = mysql_query ( $query );
342  while ( $row = mysql_fetch_array( $result ) )
343  {
344    // for each picture, we have to check all the categories it belongs
345    // to. Indeed if a picture belongs to category_1 and category_2 and that
346    // category_2 is not restricted to the user, he can have the picture as
347    // favorite.
348    $query = 'SELECT DISTINCT(category_id) as category_id';
349    $query.= ' FROM '.PREFIX_TABLE.'image_category';
350    $query.= ' WHERE image_id = '.$row['image_id'];
351    $query.= ';';
352    $picture_result = mysql_query( $query );
353    $picture_cat = array();
354    while ( $picture_row = mysql_fetch_array( $picture_result ) )
355    {
356      array_push( $picture_cat, $picture_row['category_id'] );
357    }
358    if ( count( array_diff( $picture_cat, $restricted_cat ) ) == 0 )
359    {
360      $query = 'DELETE FROM '.PREFIX_TABLE.'favorites';
361      $query.= ' WHERE image_id = '.$row['image_id'];
362      $query.= ' AND user_id = '.$user_id;
363      $query.= ';';
364      mysql_query( $query );
365    }
366  }
367}
368
369// update_category updates calculated informations about a category :
370// date_last and nb_images. It also verifies that the representative picture
371// is really linked to the category.
372function update_category( $id = 'all' )
373{
374  if ( $id == 'all' )
375  {
376    $query = 'SELECT id';
377    $query.= ' FROM '.PREFIX_TABLE.'categories';
378    $query.= ';';
379    $result = mysql_query( $query );
380    while ( $row = mysql_fetch_array( $result ) )
381    {
382      // recursive call
383      update_category( $row['id'] );
384    }
385  }
386  else if ( is_numeric( $id ) )
387  {
388    // updating the number of pictures
389    $query = 'SELECT COUNT(*) as nb_images';
390    $query.= ' FROM '.PREFIX_TABLE.'image_category';
391    $query.= ' WHERE category_id = '.$id;
392    $query.= ';';
393    list( $nb_images ) = mysql_fetch_array( mysql_query( $query ) );
394    // updating the date_last
395    $query = 'SELECT MAX(date_available) AS date_available';
396    $query.= ' FROM '.PREFIX_TABLE.'images';
397    $query.= ' INNER JOIN '.PREFIX_TABLE.'image_category ON id = image_id';
398    $query.= ' WHERE category_id = '.$id;
399    $query.= ';';
400    list( $date_available ) = mysql_fetch_array( mysql_query( $query ) );
401   
402    $query = 'UPDATE '.PREFIX_TABLE.'categories';
403    $query.= " SET date_last = '".$date_available."'";
404    $query.= ', nb_images = '.$nb_images;
405    $query.= ' WHERE id = '.$id;
406    $query.= ';';
407    mysql_query( $query );
408
409    // updating the representative_picture_id : if the representative
410    // picture of the category is not any more linked to the category, we
411    // have to set representative_picture_id to NULL
412    $query = 'SELECT representative_picture_id';
413    $query.= ' FROM '.PREFIX_TABLE.'categories';
414    $query.= ' WHERE id = '.$id;
415    $row = mysql_fetch_array( mysql_query( $query ) );
416    // if the category has no representative picture (ie
417    // representative_picture_id == NULL) we don't update anything
418    if ( isset( $row['representative_picture_id'] ) )
419    {
420      $query = 'SELECT image_id';
421      $query.= ' FROM '.PREFIX_TABLE.'image_category';
422      $query.= ' WHERE category_id = '.$id;
423      $query.= ' AND image_id = '.$row['representative_picture_id'];
424      $query.= ';';
425      $result = mysql_query( $query );
426      if ( mysql_num_rows( $result ) == 0 )
427      {
428        $query = 'UPDATE '.PREFIX_TABLE.'categories';
429        $query.= ' SET representative_picture_id = NULL';
430        $query.= ' WHERE id = '.$id;
431        $query.= ';';
432        mysql_query( $query );
433      }
434    }
435  }
436}
437
438function check_date_format( $date )
439{
440  // date arrives at this format : DD/MM/YYYY
441  @list($day,$month,$year) = explode( '/', $date );
442  return @checkdate( $month, $day, $year );
443}
444
445function date_convert( $date )
446{
447  // date arrives at this format : DD/MM/YYYY
448  // It must be transformed in YYYY-MM-DD
449  list($day,$month,$year) = explode( '/', $date );
450  return $year.'-'.$month.'-'.$day;
451}
452
453function date_convert_back( $date )
454{
455  // date arrives at this format : YYYY-MM-DD
456  // It must be transformed in DD/MM/YYYY
457  if ( $date != '' )
458  {
459    list($year,$month,$day) = explode( '-', $date );
460    return $day.'/'.$month.'/'.$year;
461  }
462  else
463  {
464    return '';
465  }
466}
467
468// get_keywords returns an array with relevant keywords found in the string
469// given in argument. Keywords must be separated by comma in this string.
470// keywords must :
471//   - be longer or equal to 3 characters
472//   - not contain ', " or blank characters
473//   - unique in the string ("test,test" -> "test")
474function get_keywords( $keywords_string )
475{
476  $keywords = array();
477
478  $candidates = explode( ',', $keywords_string );
479  foreach ( $candidates as $candidate ) {
480    if ( strlen($candidate) >= 3 and !preg_match( '/(\'|"|\s)/', $candidate ) )
481      array_push( $keywords, $candidate );
482  }
483
484  return array_unique( $keywords );
485}
486
487function display_categories( $categories, $indent,
488                             $selected = -1, $forbidden = -1 )
489{
490  global $vtp,$sub;
491
492  foreach ( $categories as $category ) {
493    if ( $category['id'] != $forbidden )
494    {
495      $vtp->addSession( $sub, 'associate_cat' );
496      $vtp->setVar( $sub, 'associate_cat.value',   $category['id'] );
497      $content = $indent.'- '.$category['name'];
498      $vtp->setVar( $sub, 'associate_cat.content', $content );
499      if ( $category['id'] == $selected )
500        $vtp->setVar( $sub, 'associate_cat.selected', ' selected="selected"' );
501      $vtp->closeSession( $sub, 'associate_cat' );
502      display_categories( $category['subcats'], $indent.str_repeat('&nbsp;',3),
503                          $selected, $forbidden );
504    }
505  }
506}
507
508/**
509 * Complete plain structure of the gallery
510 *
511 * Returns the plain structure (one level array) of the gallery. In the
512 * returned array, each element is an array with jeys 'id' and
513 * 'id_uppercat'. The function also fills the array $page['subcats'] which
514 * associate (category_id => array of sub-categories id).
515 *
516 * @param bool $use_name
517 * @return array
518 */
519function get_plain_structure( $use_name = false )
520{
521  global $page;
522
523  $plain_structure = array();
524
525  $query = 'SELECT id,id_uppercat';
526  if ( $use_name ) $query.= ',name';
527  $query.= ' FROM '.PREFIX_TABLE.'categories';
528  $query.= ' ORDER BY id_uppercat ASC, rank ASC';
529  $query.= ';';
530
531  $subcats = array();
532  $id_uppercat = 'NULL';
533
534  $result = mysql_query( $query );
535  while ( $row = mysql_fetch_array( $result ) )
536  {
537    $plain_structure[$row['id']]['id'] = $row['id'];
538    if ( !isset( $row['id_uppercat'] ) ) $row['id_uppercat'] = 'NULL';
539    $plain_structure[$row['id']]['id_uppercat'] = $row['id_uppercat'];
540    if ( $use_name ) $plain_structure[$row['id']]['name'] = $row['name'];
541    // subcats list
542    if ( $row['id_uppercat'] != $id_uppercat )
543    {
544      $page['subcats'][$id_uppercat] = $subcats;
545
546      $subcats = array();
547      $id_uppercat = $row['id_uppercat'];
548    }
549    array_push( $subcats, $row['id'] );
550  }
551  mysql_free_result( $result );
552 
553  $page['subcats'][$id_uppercat] = $subcats;
554
555  return $plain_structure;
556}
557
558/**
559 * get N levels array representing structure under the given category
560 *
561 * create_structure returns the N levels array representing structure under
562 * the given gategory id. It also updates the
563 * $page['plain_structure'][id]['all_subcats_id'] and
564 * $page['plain_structure'][id]['direct_subcats_ids'] for each sub category.
565 *
566 * @param int $id_uppercat
567 * @return array
568 */
569function create_structure( $id_uppercat )
570{
571  global $page;
572
573  $structure = array();
574  $ids = get_subcats_ids( $id_uppercat );
575  foreach ( $ids as $id ) {
576    $category = $page['plain_structure'][$id];
577
578    $category['subcats'] = create_structure( $id );
579
580    $page['plain_structure'][$id]['all_subcats_ids'] =
581      get_all_subcats_ids( $id );
582
583    $page['plain_structure'][$id]['direct_subcats_ids'] =
584      get_subcats_ids( $id );
585
586    array_push( $structure, $category );
587  }
588  return $structure;
589}
590
591/**
592 * returns direct sub-categories ids
593 *
594 * Returns an array containing all the direct sub-categories ids of the
595 * given category. It uses the $page['subcats'] global array.
596 *
597 * @param int $id_uppercat
598 * @return array
599 */
600function get_subcats_ids( $id_uppercat )
601{
602  global $page;
603
604  if ( $id_uppercat == '' ) $id_uppercat = 'NULL';
605
606  if ( isset( $page['subcats'][$id_uppercat] ) )
607    return $page['subcats'][$id_uppercat];
608  else
609    return array();
610}
611
612/**
613 * returns all sub-categories ids, not only direct ones
614 *
615 * Returns an array containing all the sub-categories ids of the given
616 * category, not only direct ones. This function is recursive.
617 *
618 * @param int $category_id
619 * @return array
620 */
621function get_all_subcats_ids( $category_id )
622{
623  $ids = array();
624 
625  $subcats = get_subcats_ids( $category_id );
626  $ids = array_merge( $ids, $subcats );
627  foreach ( $subcats as $subcat ) {
628    // recursive call
629    $sub_subcats = get_all_subcats_ids( $subcat );
630    $ids = array_merge( $ids, $sub_subcats );
631  }
632  return array_unique( $ids );
633}
634
635/**
636 * prepares the query to update the table user_category
637 *
638 * Prepares the query (global variable $values) to update table
639 * user_category : for a couple (user,category) the number of sub-categories
640 * and the last date of the category (all sub-categories taken into
641 * account). It also calls function update_uppercats for each category. The
642 * function is recursive.
643 *
644 * @param array $categories
645 * @return void
646 */
647function update_user_category( $categories )
648{
649  global $page,$user_restrictions,$value_num,$values;
650
651  foreach ( $categories as $category ) {
652    // recursive call
653    update_user_category( $category['subcats'] );
654    // 1. update the table user_category
655    foreach ( $user_restrictions as $user_id => $restrictions ) {
656      // if the category is forbidden to this user, go to next user
657      if ( in_array( $category['id'], $restrictions ) ) continue;
658
659      // how many sub_categories for this user ?
660      $user_subcats = array_diff(
661        $page['plain_structure'][$category['id']]['direct_subcats_ids'],
662        $restrictions );
663      $user_nb_subcats = count( array_unique( $user_subcats ) );
664      // last date of the category
665      $user_all_subcats = array_unique( array_diff(
666        $page['plain_structure'][$category['id']]['all_subcats_ids'],
667        $restrictions ) );
668           
669      $query = 'SELECT MAX(date_last) AS last_date';
670      $query.= ' FROM '.PREFIX_TABLE.'categories';
671      $query.= ' WHERE id IN ('.$category['id'];
672      if ( count( $user_all_subcats ) > 0 )
673        $query.= ','.implode( ',', $user_all_subcats );
674      $query.= ')';
675      $query.= ';';
676      $row = mysql_fetch_array( mysql_query( $query ) );
677
678      // insert a new line in database
679      if ( $value_num++ > 0 ) $values.= ', ';
680      else                    $values.= ' ';
681      $values.= '('.$user_id.",".$category['id'];
682      if ( isset( $row['last_date'] ) ) $values.= ",'".$row['last_date']."'";
683      else                              $values.= ',NULL';
684      $values.= ','.$user_nb_subcats.')';
685    }
686    update_uppercats( $category['id'] );
687  }
688}
689
690/**
691 * updates the column categories.uppercats
692 *
693 * @param int $category_id
694 * @return void
695 */
696function update_uppercats( $category_id )
697{
698  global $page;
699
700  $final_id = $category_id;
701  $uppercats = array();
702
703  array_push( $uppercats, $category_id );
704  $uppercat = $page['plain_structure'][$category_id]['id_uppercat'];
705
706  while ( $uppercat != 'NULL' )
707  {
708    array_push( $uppercats, $uppercat );
709    $category_id = $page['plain_structure'][$category_id]['id_uppercat'];
710    $uppercat = $page['plain_structure'][$category_id]['id_uppercat'];
711  }
712
713  $string_uppercats = implode( ',', array_reverse( $uppercats ) );
714  $query = 'UPDATE '.PREFIX_TABLE.'categories';
715  $query.= ' SET uppercats = '."'".$string_uppercats."'";
716  $query.= ' WHERE id = '.$final_id;
717  $query.= ';';
718  mysql_query( $query );
719}
720
721/**
722 * returns an array with the ids of the restricted categories for the user
723 *
724 * Returns an array with the ids of the restricted categories for the
725 * user. If the $check_invisible parameter is set to true, invisible
726 * categorie are added to the restricted one in the array.
727 *
728 * @param int $user_id
729 * @param string $user_status
730 * @param bool $check_invisible
731 * @param bool $use_groups
732 * @return array
733 */
734function get_user_restrictions( $user_id, $user_status,
735                                $check_invisible, $use_groups = true )
736{
737  // 1. retrieving ids of private categories
738  $query = 'SELECT id';
739  $query.= ' FROM '.PREFIX_TABLE.'categories';
740  $query.= " WHERE status = 'private'";
741  $query.= ';';
742  $result = mysql_query( $query );
743  $privates = array();
744  while ( $row = mysql_fetch_array( $result ) )
745  {
746    array_push( $privates, $row['id'] );
747  }
748  // 2. retrieving all authorized categories for the user
749  $authorized = array();
750  // 2.1. retrieving authorized categories thanks to personnal user
751  //      authorization
752  $query = 'SELECT cat_id';
753  $query.= ' FROM '.PREFIX_TABLE.'user_access';
754  $query.= ' WHERE user_id = '.$user_id;
755  $query.= ';';
756  $result = mysql_query( $query );
757  while ( $row = mysql_fetch_array( $result ) )
758  {
759    array_push( $authorized, $row['cat_id'] );
760  }
761  // 2.2. retrieving authorized categories thanks to group authorization to
762  //      which the user is a member
763  if ( $use_groups )
764  {
765    $query = 'SELECT ga.cat_id';
766    $query.= ' FROM '.PREFIX_TABLE.'user_group as ug';
767    $query.= ', '.PREFIX_TABLE.'group_access as ga';
768    $query.= ' WHERE ug.group_id = ga.group_id';
769    $query.= ' AND ug.user_id = '.$user_id;
770    $query.= ';';
771    $result = mysql_query( $query );
772    while ( $row = mysql_fetch_array( $result ) )
773    {
774      array_push( $authorized, $row['cat_id'] );
775    }
776    $authorized = array_unique( $authorized );
777  }
778
779  $forbidden = array();
780  foreach ( $privates as $private ) {
781    if ( !in_array( $private, $authorized ) )
782    {
783      array_push( $forbidden, $private );
784    }
785  }
786
787  if ( $check_invisible )
788  {
789    // 3. adding to the restricted categories, the invisible ones
790    if ( $user_status != 'admin' )
791    {
792      $query = 'SELECT id';
793      $query.= ' FROM '.PREFIX_TABLE.'categories';
794      $query.= " WHERE visible = 'false';";
795      $result = mysql_query( $query );
796      while ( $row = mysql_fetch_array( $result ) )
797      {
798        array_push( $forbidden, $row['id'] );
799      }
800    }
801  }
802  return array_unique( $forbidden );
803}
804
805/**
806 * finalizes operation for user_category table update
807 *
808 * This function is called by synchronization_*. It creates the
809 * $page['plain_structure'] and $page['structure'], get the SQL query to
810 * update user_category, clean user_category, and finally update the
811 * table. The users updates depends on the global array $user_restrictions.
812 *
813 * @return void
814 */
815function synchronize()
816{
817  global $user_restrictions,$page,$values;
818
819  update_user_category( $page['structure'] );
820
821  // cleaning user_category table for users to update
822  foreach( $user_restrictions as $user_id => $restrictions ) {
823    $query = 'DELETE';
824    $query.= ' FROM '.PREFIX_TABLE.'user_category';
825    $query.= ' WHERE user_id = '.$user_id;
826    $query.= ';';
827    mysql_query( $query );
828  }
829
830  $query = 'INSERT INTO '.PREFIX_TABLE.'user_category';
831  $query.= ' (user_id,category_id,date_last,nb_sub_categories) VALUES ';
832  $query.= $values;
833  $query.= ';';
834  mysql_query( $query );
835}
836
837/**
838 * synchronizes all users calculated informations
839 *
840 * fills global array $user_restrictions with all users and related
841 * restrictions before calling synchronize.
842 *
843 * @return void
844 */
845function synchronize_all_users()
846{
847  global $user_restrictions,$page;
848
849  $page['plain_structure'] = get_plain_structure();
850  $page['structure']       = create_structure( '' );
851 
852  $user_restrictions = array();
853 
854  $query = 'SELECT id';
855  $query.= ' FROM '.PREFIX_TABLE.'users';
856  $query.= ';';
857  $result = mysql_query( $query );
858  while ( $row = mysql_fetch_array( $result ) )
859  {
860    $user_restrictions[$row['id']] = update_user_restrictions( $row['id'] );
861  }
862  synchronize();
863}
864
865/**
866 * synchronizes 1 user calculated informations
867 *
868 * fills global array $user_restrictions with the user id and its related
869 * restrictions before calling synchronize.
870 *
871 * @param int $user_id
872 * @return void
873 */
874function synchronize_user( $user_id )
875{
876  global $user_restrictions,$page;
877
878  $page['plain_structure'] = get_plain_structure();
879  $page['structure']       = create_structure( '' );
880 
881  $user_restrictions = array();
882  $user_restrictions[$user_id] = update_user_restrictions( $user_id );
883  synchronize();
884}
885
886/**
887 * synchronizes all users (belonging to the group) calculated informations
888 *
889 * fills global array $user_restrictions with all users and related
890 * restrictions before calling synchronize.
891 *
892 * @return void
893 */
894function synchronize_group( $group_id )
895{
896  global $user_restrictions,$page;
897
898  $page['plain_structure'] = get_plain_structure();
899  $page['structure']       = create_structure( '' );
900 
901  $user_restrictions = array();
902 
903  $query = 'SELECT id';
904  $query.= ' FROM '.PREFIX_TABLE.'users';
905  $query.= ', '.PREFIX_TABLE.'user_group';
906  $query.= ' WHERE group_id = '.$group_id;
907  $query.= ' AND id = user_id';
908  $query.= ';';
909  $result = mysql_query( $query );
910  while ( $row = mysql_fetch_array( $result ) )
911  {
912    $user_restrictions[$row['id']] = update_user_restrictions( $row['id'] );
913  }
914  synchronize();
915}
916
917/**
918 * updates the calculated data users.forbidden_categories, it includes
919 * sub-categories of the direct forbidden categories
920 *
921 * @param nt $user_id
922 * @return array
923 */
924function update_user_restrictions( $user_id )
925{
926  $restrictions = get_user_all_restrictions( $user_id );
927
928  // update the users.forbidden_categories in database
929  $query = 'UPDATE '.PREFIX_TABLE.'users';
930  $query.= ' SET forbidden_categories = ';
931  if ( count( $restrictions ) > 0 )
932    $query.= "'".implode( ',', $restrictions )."'";
933  else
934    $query.= 'NULL';
935  $query .= ' WHERE id = '.$user_id;
936  $query.= ';';
937  mysql_query( $query );
938
939  return $restrictions;
940}
941
942/**
943 * returns all the restricted categories ids including sub-categories
944 *
945 * @param int $user_id
946 * @return array
947 */
948function get_user_all_restrictions( $user_id )
949{
950  global $page;
951 
952  $query = 'SELECT status';
953  $query.= ' FROM '.PREFIX_TABLE.'users';
954  $query.= ' WHERE id = '.$user_id;
955  $query.= ';';
956  $row = mysql_fetch_array( mysql_query( $query ) );
957 
958  $base_restrictions=get_user_restrictions($user_id,$row['status'],true,true);
959
960  $restrictions = $base_restrictions;
961  foreach ( $base_restrictions as $category_id ) {
962    echo $category_id.' is forbidden to user '.$user_id.'<br />';
963    $restrictions =
964      array_merge( $restrictions,
965                   $page['plain_structure'][$category_id]['all_subcats_ids'] );
966  }
967
968  return array_unique( $restrictions );
969}
970
971// The function is_user_allowed returns :
972//      - 0 : if the category is allowed with this $restrictions array
973//      - 1 : if this category is not allowed
974//      - 2 : if an uppercat category is not allowed
975// Note : the restrictions array must represent ONLY direct forbidden
976// categories, not all forbidden categories
977function is_user_allowed( $category_id, $restrictions )
978{
979  if ( in_array( $category_id, $restrictions ) ) return 1;
980
981  $query = 'SELECT uppercats';
982  $query.= ' FROM '.PREFIX_TABLE.'categories';
983  $query.= ' WHERE id = '.$category_id;
984  $query.= ';';
985  $row = mysql_fetch_array( mysql_query( $query ) );
986  $uppercats = explode( ',', $row['uppercats'] );
987  foreach ( $uppercats as $category_id ) {
988    if ( in_array( $category_id, $restrictions ) ) return 2;
989  }
990
991  // no restriction found : the user is allowed to access this category
992  return 0;
993}
994
995/**
996 * returns an array containing sub-directories which can be a category
997 *
998 * directories nammed "thumbnail" are omitted
999 *
1000 * @param string $basedir
1001 * @return array
1002 */
1003function get_category_directories( $basedir )
1004{
1005  $sub_dirs = array();
1006
1007  if ( $opendir = opendir( $basedir ) )
1008  {
1009    while ( $file = readdir( $opendir ) )
1010    {
1011      if ( $file != '.' and $file != '..'
1012           and is_dir( $basedir.'/'.$file )
1013           and $file != 'thumbnail' )
1014      {
1015        array_push( $sub_dirs, $file );
1016      }
1017    }
1018  }
1019  return $sub_dirs;
1020}
1021?>
Note: See TracBrowser for help on using the repository browser.