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

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

* empty log message *

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.9 KB
Line 
1<?php
2/***************************************************************************
3 *                               functions.php                             *
4 *                            -------------------                          *
5 *   application          : PhpWebGallery 1.3                              *
6 *   author               : Pierrick LE GALL <pierrick@z0rglub.com>        *
7 *                                                                         *
8 ***************************************************************************
9
10 ***************************************************************************
11 *                                                                         *
12 *   This program is free software; you can redistribute it and/or modify  *
13 *   it under the terms of the GNU General Public License as published by  *
14 *   the Free Software Foundation;                                         *
15 *                                                                         *
16 ***************************************************************************/
17
18$tab_ext_create_TN = array ( 'jpg', 'png', 'JPG', 'PNG' );
19
20function is_image( $filename, $create_thumbnail = false )
21{
22  global $conf, $tab_ext_create_TN;
23
24  if ( is_file ( $filename ) )
25  {
26    $size = getimagesize( $filename );
27    // $size[2] == 1 means GIF
28    // $size[2] == 2 means JPG
29    // $size[2] == 3 means PNG
30    if ( !$create_thumbnail )
31    {
32      if ( in_array( get_extension( $filename ), $conf['picture_ext'] )
33           and ( $size[2] == 1 or $size[2] == 2 or $size[2] == 3 ) )
34      {
35        return true;
36      }
37    }
38    else
39    {
40      if ( in_array( get_extension( $filename ), $tab_ext_create_TN )
41           and ( $size[2] == 2 or $size[2] == 3 ) )
42      {
43        return true;
44      }
45    }
46  }
47  return false;
48}
49       
50function TN_exists( $dir, $file )
51{
52  global $conf;
53
54  $filename = get_filename_wo_extension( $file );
55  foreach ( $conf['picture_ext'] as $ext ) {
56    $test = $dir.'/thumbnail/'.$conf['prefix_thumbnail'].$filename.'.'.$ext;
57    if ( is_file ( $test ) )
58    {
59      return $ext;
60    }
61  }
62  return false;
63}       
64       
65// The function delete_site deletes a site
66// and call the function delete_category for each primary category of the site
67function delete_site( $id )
68{
69  // destruction of the categories of the site
70  $query = 'SELECT id';
71  $query.= ' FROM '.PREFIX_TABLE.'categories';
72  $query.= ' WHERE site_id = '.$id;
73  $query.= ';';
74  $result = mysql_query( $query );
75  while ( $row = mysql_fetch_array( $result ) )
76  {
77    delete_category( $row['id'] );
78  }
79               
80  // destruction of the site
81  $query = 'DELETE FROM '.PREFIX_TABLE.'sites';
82  $query.= ' WHERE id = '.$id;
83  $query.= ';';
84  mysql_query( $query );
85}
86       
87// The function delete_category deletes the category identified by the $id
88// It also deletes (in the database) :
89//    - all the images of the images (thanks to delete_image, see further)
90//    - all the restrictions linked to the category
91// The function works recursively.
92function delete_category( $id )
93{
94  // destruction of all the related images
95  $query = 'SELECT id';
96  $query.= ' FROM '.PREFIX_TABLE.'images';
97  $query.= ' WHERE cat_id = '.$id;
98  $query.= ';';
99  $result = mysql_query( $query );
100  while ( $row = mysql_fetch_array( $result ) )
101  {
102    delete_image( $row['id'] );
103  }
104
105  // destruction of the access linked to the category
106  $query = 'DELETE FROM '.PREFIX_TABLE.'user_access';
107  $query.= ' WHERE cat_id = '.$id;
108  $query.= ';';
109  mysql_query( $query );
110  $query = 'DELETE FROM '.PREFIX_TABLE.'group_access';
111  $query.= ' WHERE cat_id = '.$id;
112  $query.= ';';
113  mysql_query( $query );
114
115  // destruction of the sub-categories
116  $query = 'SELECT id';
117  $query.= ' FROM '.PREFIX_TABLE.'categories';
118  $query.= ' WHERE id_uppercat = '.$id;
119  $query.= ';';
120  $result = mysql_query( $query );
121  while( $row = mysql_fetch_array( $result ) )
122  {
123    delete_category( $row['id'] );
124  }
125
126  // destruction of the category
127  $query = 'DELETE FROM '.PREFIX_TABLE.'categories';
128  $query.= ' WHERE id = '.$id;
129  $query.= ';';
130  mysql_query( $query );
131}
132       
133// The function delete_image deletes the image identified by the $id
134// It also deletes (in the database) :
135//    - all the comments related to the image
136//    - all the favorites associated to the image
137function delete_image( $id )
138{
139  global $count_deleted;
140               
141  // destruction of the comments on the image
142  $query = 'DELETE FROM '.PREFIX_TABLE.'comments';
143  $query.= ' WHERE image_id = '.$id;
144  $query.= ';';
145  mysql_query( $query );
146               
147  // destruction of the favorites associated with the picture
148  $query = 'DELETE FROM '.PREFIX_TABLE.'favorites';
149  $query.= ' WHERE image_id = '.$id;
150  $query.= ';';
151  mysql_query( $query );
152               
153  // destruction of the image
154  $query = 'DELETE FROM '.PREFIX_TABLE.'images';
155  $query.= ' WHERE id = '.$id;
156  $query.= ';';
157  mysql_query( $query );
158  $count_deleted++;
159}
160       
161// The delete_user function delete a user identified by the $user_id
162// It also deletes :
163//     - all the access linked to this user
164//     - all the links to any group
165//     - all the favorites linked to this user
166//     - all sessions linked to this user
167function delete_user( $user_id )
168{
169  // destruction of the access linked to the user
170  $query = 'DELETE FROM '.PREFIX_TABLE.'user_access';
171  $query.= ' WHERE user_id = '.$user_id;
172  $query.= ';';
173  mysql_query( $query );
174
175  // destruction of the group links for this user
176  $query = 'DELETE FROM '.PREFIX_TABLE.'user_group';
177  $query.= ' WHERE user_id = '.$user_id;
178  $query.= ';';
179  mysql_query( $query );
180
181  // destruction of the favorites associated with the user
182  $query = 'DELETE FROM '.PREFIX_TABLE.'favorites';
183  $query.= ' WHERE user_id = '.$user_id;
184  $query.= ';';
185  mysql_query( $query );
186
187  // destruction of the sessions linked with the user
188  $query = 'DELETE FROM '.PREFIX_TABLE.'sessions';
189  $query.= ' WHERE user_id = '.$user_id;
190  $query.= ';';
191  mysql_query( $query );
192               
193  // destruction of the user
194  $query = 'DELETE FROM '.PREFIX_TABLE.'users';
195  $query.= ' WHERE id = '.$user_id;
196  $query.= ';';
197  mysql_query( $query );
198}
199
200// delete_group deletes a group identified by its $group_id.
201// It also deletes :
202//     - all the access linked to this group
203//     - all the links between this group and any user
204function delete_group( $group_id )
205{
206  // destruction of the access linked to the group
207  $query = 'DELETE FROM '.PREFIX_TABLE.'group_access';
208  $query.= ' WHERE group_id = '.$group_id;
209  $query.= ';';
210  mysql_query( $query );
211
212  // destruction of the group links for this group
213  $query = 'DELETE FROM '.PREFIX_TABLE.'user_group';
214  $query.= ' WHERE group_id = '.$group_id;
215  $query.= ';';
216  mysql_query( $query );
217
218  // destruction of the group
219  $query = 'DELETE FROM '.PREFIX_TABLE.'groups';
220  $query.= ' WHERE id = '.$group_id;
221  $query.= ';';
222  mysql_query( $query );
223}
224
225// The check_favorites function deletes all the favorites of a user if he is
226// not allowed to see them (the category or an upper category is restricted
227// or invisible)
228function check_favorites( $user_id )
229{
230  $query = 'SELECT status';
231  $query.= ' FROM '.PREFIX_TABLE.'users';
232  $query.= ' WHERE id = '.$user_id;
233  $query.= ';';
234  $row = mysql_fetch_array( mysql_query( $query ) );
235  $status = $row['status'];
236  // retrieving all the restricted categories for this user
237  $restricted_cat = get_all_restrictions( $user_id, $status );
238  // retrieving all the favorites for this user and comparing their
239  // categories to the restricted categories
240  $query = 'SELECT image_id, cat_id';
241  $query.= ' FROM '.PREFIX_TABLE.'favorites, '.PREFIX_TABLE.'images';
242  $query.= ' WHERE user_id = '.$user_id;
243  $query.= ' AND id = image_id';
244  $query.= ';';
245  $result = mysql_query ( $query );
246  while ( $row = mysql_fetch_array( $result ) )
247  {
248    if ( in_array( $row['cat_id'], $restricted_cat ) )
249    {
250      $query = 'DELETE FROM '.PREFIX_TABLE.'favorites';
251      $query.= ' WHERE image_id = '.$row['image_id'];
252      $query.= ' AND user_id = '.$user_id;
253      $query.= ';';
254      mysql_query( $query );
255    }
256  }
257}
258?>
Note: See TracBrowser for help on using the repository browser.