1 | <?php |
---|
2 | // +-----------------------------------------------------------------------+ |
---|
3 | // | PhpWebGallery - a PHP based picture gallery | |
---|
4 | // | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net | |
---|
5 | // | Copyright (C) 2003-2005 PhpWebGallery Team - http://phpwebgallery.net | |
---|
6 | // +-----------------------------------------------------------------------+ |
---|
7 | // | branch : BSF (Best So Far) |
---|
8 | // | file : $RCSfile$ |
---|
9 | // | last update : $Date: 2006-03-01 04:33:52 +0000 (Wed, 01 Mar 2006) $ |
---|
10 | // | last modifier : $Author: rvelices $ |
---|
11 | // | revision : $Revision: 1061 $ |
---|
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 | /** |
---|
29 | * Provides functions to handle categories. |
---|
30 | * |
---|
31 | * |
---|
32 | */ |
---|
33 | |
---|
34 | /** |
---|
35 | * Is the category accessible to the connected user ? |
---|
36 | * |
---|
37 | * Note : if the user is not authorized to see this category, page creation |
---|
38 | * ends (exit command in this function) |
---|
39 | * |
---|
40 | * @param int category id to verify |
---|
41 | * @return void |
---|
42 | */ |
---|
43 | function check_restrictions($category_id) |
---|
44 | { |
---|
45 | global $user, $lang; |
---|
46 | |
---|
47 | if (in_array($category_id, explode(',', $user['forbidden_categories']))) |
---|
48 | { |
---|
49 | $login_url = './identification.php?redirect='. |
---|
50 | urlencode(urlencode($_SERVER['REQUEST_URI'])); |
---|
51 | if ( ! $user['is_the_guest'] ) |
---|
52 | { |
---|
53 | echo '<div style="text-align:center;">'; |
---|
54 | echo $lang['hello'].' '.$user['username'].'! '; |
---|
55 | echo $lang['access_forbiden'].'<br />'; |
---|
56 | echo '<a href="./category.php">'.$lang['thumbnails'].'</a> '; |
---|
57 | echo '</div>'; |
---|
58 | exit(); |
---|
59 | } |
---|
60 | else |
---|
61 | { |
---|
62 | redirect($login_url); |
---|
63 | } |
---|
64 | } |
---|
65 | } |
---|
66 | |
---|
67 | function get_categories_menu() |
---|
68 | { |
---|
69 | global $page,$user; |
---|
70 | |
---|
71 | $infos = array(''); |
---|
72 | |
---|
73 | $query = ' |
---|
74 | SELECT name,id,date_last,nb_images,global_rank |
---|
75 | FROM '.CATEGORIES_TABLE.' |
---|
76 | WHERE 1 = 1'; // stupid but permit using AND after it ! |
---|
77 | if (!$user['expand']) |
---|
78 | { |
---|
79 | $query.= ' |
---|
80 | AND (id_uppercat is NULL'; |
---|
81 | if ( isset( $page['cat'] ) and is_numeric( $page['cat'] ) ) |
---|
82 | { |
---|
83 | $query.= ' OR id_uppercat IN ('.$page['uppercats'].')'; |
---|
84 | } |
---|
85 | $query.= ')'; |
---|
86 | } |
---|
87 | if ($user['forbidden_categories'] != '') |
---|
88 | { |
---|
89 | $query.= ' |
---|
90 | AND id NOT IN ('.$user['forbidden_categories'].')'; |
---|
91 | } |
---|
92 | $query.= ' |
---|
93 | ;'; |
---|
94 | |
---|
95 | $result = pwg_query($query); |
---|
96 | $cats = array(); |
---|
97 | while ($row = mysql_fetch_array($result)) |
---|
98 | { |
---|
99 | array_push($cats, $row); |
---|
100 | } |
---|
101 | usort($cats, 'global_rank_compare'); |
---|
102 | |
---|
103 | return get_html_menu_category($cats); |
---|
104 | } |
---|
105 | |
---|
106 | /** |
---|
107 | * returns the total number of elements viewable in the gallery by the |
---|
108 | * connected user |
---|
109 | * |
---|
110 | * @return int |
---|
111 | */ |
---|
112 | function count_user_total_images() |
---|
113 | { |
---|
114 | global $user; |
---|
115 | |
---|
116 | $query = ' |
---|
117 | SELECT COUNT(DISTINCT(image_id)) as total |
---|
118 | FROM '.IMAGE_CATEGORY_TABLE.' |
---|
119 | WHERE category_id NOT IN ('.$user['forbidden_categories'].') |
---|
120 | ;'; |
---|
121 | list($total) = mysql_fetch_array(pwg_query($query)); |
---|
122 | |
---|
123 | return $total; |
---|
124 | } |
---|
125 | |
---|
126 | /** |
---|
127 | * Retrieve informations about a category in the database |
---|
128 | * |
---|
129 | * Returns an array with following keys : |
---|
130 | * |
---|
131 | * - comment |
---|
132 | * - dir : directory, might be empty for virtual categories |
---|
133 | * - name : an array with indexes from 0 (lowest cat name) to n (most |
---|
134 | * uppercat name findable) |
---|
135 | * - nb_images |
---|
136 | * - id_uppercat |
---|
137 | * - site_id |
---|
138 | * - |
---|
139 | * |
---|
140 | * @param int category id |
---|
141 | * @return array |
---|
142 | */ |
---|
143 | function get_cat_info( $id ) |
---|
144 | { |
---|
145 | $infos = array('nb_images','id_uppercat','comment','site_id' |
---|
146 | ,'dir','date_last','uploadable','status','visible' |
---|
147 | ,'representative_picture_id','uppercats','commentable'); |
---|
148 | |
---|
149 | $query = ' |
---|
150 | SELECT '.implode(',', $infos).' |
---|
151 | FROM '.CATEGORIES_TABLE.' |
---|
152 | WHERE id = '.$id.' |
---|
153 | ;'; |
---|
154 | $row = mysql_fetch_array(pwg_query($query)); |
---|
155 | |
---|
156 | $cat = array(); |
---|
157 | foreach ($infos as $info) |
---|
158 | { |
---|
159 | if (isset($row[$info])) |
---|
160 | { |
---|
161 | $cat[$info] = $row[$info]; |
---|
162 | } |
---|
163 | else |
---|
164 | { |
---|
165 | $cat[$info] = ''; |
---|
166 | } |
---|
167 | // If the field is true or false, the variable is transformed into a |
---|
168 | // boolean value. |
---|
169 | if ($cat[$info] == 'true' or $cat[$info] == 'false') |
---|
170 | { |
---|
171 | $cat[$info] = get_boolean( $cat[$info] ); |
---|
172 | } |
---|
173 | } |
---|
174 | global $conf; |
---|
175 | if ( !( $conf['allow_html_descriptions'] and |
---|
176 | preg_match('/<(div|br|img).*>/i', $cat['comment']) ) ) |
---|
177 | { |
---|
178 | $cat['comment'] = nl2br($cat['comment']); |
---|
179 | } |
---|
180 | |
---|
181 | $names = array(); |
---|
182 | $query = ' |
---|
183 | SELECT name,id |
---|
184 | FROM '.CATEGORIES_TABLE.' |
---|
185 | WHERE id IN ('.$cat['uppercats'].') |
---|
186 | ;'; |
---|
187 | $result = pwg_query($query); |
---|
188 | while($row = mysql_fetch_array($result)) |
---|
189 | { |
---|
190 | $names[$row['id']] = $row['name']; |
---|
191 | } |
---|
192 | |
---|
193 | // category names must be in the same order than uppercats list |
---|
194 | $cat['name'] = array(); |
---|
195 | foreach (explode(',', $cat['uppercats']) as $cat_id) |
---|
196 | { |
---|
197 | $cat['name'][$cat_id] = $names[$cat_id]; |
---|
198 | } |
---|
199 | |
---|
200 | return $cat; |
---|
201 | } |
---|
202 | |
---|
203 | // get_complete_dir returns the concatenation of get_site_url and |
---|
204 | // get_local_dir |
---|
205 | // Example : "pets > rex > 1_year_old" is on the the same site as the |
---|
206 | // PhpWebGallery files and this category has 22 for identifier |
---|
207 | // get_complete_dir(22) returns "./galleries/pets/rex/1_year_old/" |
---|
208 | function get_complete_dir( $category_id ) |
---|
209 | { |
---|
210 | return get_site_url($category_id).get_local_dir($category_id); |
---|
211 | } |
---|
212 | |
---|
213 | // get_local_dir returns an array with complete path without the site url |
---|
214 | // Example : "pets > rex > 1_year_old" is on the the same site as the |
---|
215 | // PhpWebGallery files and this category has 22 for identifier |
---|
216 | // get_local_dir(22) returns "pets/rex/1_year_old/" |
---|
217 | function get_local_dir( $category_id ) |
---|
218 | { |
---|
219 | global $page; |
---|
220 | |
---|
221 | $uppercats = ''; |
---|
222 | $local_dir = ''; |
---|
223 | |
---|
224 | if ( isset( $page['plain_structure'][$category_id]['uppercats'] ) ) |
---|
225 | { |
---|
226 | $uppercats = $page['plain_structure'][$category_id]['uppercats']; |
---|
227 | } |
---|
228 | else |
---|
229 | { |
---|
230 | $query = 'SELECT uppercats'; |
---|
231 | $query.= ' FROM '.CATEGORIES_TABLE.' WHERE id = '.$category_id; |
---|
232 | $query.= ';'; |
---|
233 | $row = mysql_fetch_array( pwg_query( $query ) ); |
---|
234 | $uppercats = $row['uppercats']; |
---|
235 | } |
---|
236 | |
---|
237 | $upper_array = explode( ',', $uppercats ); |
---|
238 | |
---|
239 | $database_dirs = array(); |
---|
240 | $query = 'SELECT id,dir'; |
---|
241 | $query.= ' FROM '.CATEGORIES_TABLE.' WHERE id IN ('.$uppercats.')'; |
---|
242 | $query.= ';'; |
---|
243 | $result = pwg_query( $query ); |
---|
244 | while( $row = mysql_fetch_array( $result ) ) |
---|
245 | { |
---|
246 | $database_dirs[$row['id']] = $row['dir']; |
---|
247 | } |
---|
248 | foreach ($upper_array as $id) |
---|
249 | { |
---|
250 | $local_dir.= $database_dirs[$id].'/'; |
---|
251 | } |
---|
252 | |
---|
253 | return $local_dir; |
---|
254 | } |
---|
255 | |
---|
256 | // retrieving the site url : "http://domain.com/gallery/" or |
---|
257 | // simply "./galleries/" |
---|
258 | function get_site_url($category_id) |
---|
259 | { |
---|
260 | global $page; |
---|
261 | |
---|
262 | $query = ' |
---|
263 | SELECT galleries_url |
---|
264 | FROM '.SITES_TABLE.' AS s,'.CATEGORIES_TABLE.' AS c |
---|
265 | WHERE s.id = c.site_id |
---|
266 | AND c.id = '.$category_id.' |
---|
267 | ;'; |
---|
268 | $row = mysql_fetch_array(pwg_query($query)); |
---|
269 | return $row['galleries_url']; |
---|
270 | } |
---|
271 | |
---|
272 | // returns an array of image orders available for users/visitors |
---|
273 | function get_category_preferred_image_orders() |
---|
274 | { |
---|
275 | global $conf; |
---|
276 | return array( |
---|
277 | array(l10n('default_sort'), '', true), |
---|
278 | array(l10n('Average rate'), 'average_rate DESC', $conf['rate']), |
---|
279 | array(l10n('most_visited_cat'), 'hit DESC', true), |
---|
280 | array(l10n('Creation date'), 'date_creation DESC', true), |
---|
281 | array(l10n('Post date'), 'date_available DESC', true), |
---|
282 | array(l10n('File name'), 'file ASC', true) |
---|
283 | ); |
---|
284 | } |
---|
285 | |
---|
286 | function display_select_categories($categories, |
---|
287 | $selecteds, |
---|
288 | $blockname, |
---|
289 | $fullname = true) |
---|
290 | { |
---|
291 | global $template; |
---|
292 | |
---|
293 | foreach ($categories as $category) |
---|
294 | { |
---|
295 | $selected = ''; |
---|
296 | if (in_array($category['id'], $selecteds)) |
---|
297 | { |
---|
298 | $selected = ' selected="selected"'; |
---|
299 | } |
---|
300 | |
---|
301 | if ($fullname) |
---|
302 | { |
---|
303 | $option = get_cat_display_name_cache($category['uppercats'], |
---|
304 | '', |
---|
305 | false); |
---|
306 | } |
---|
307 | else |
---|
308 | { |
---|
309 | $option = str_repeat(' ', |
---|
310 | (3 * substr_count($category['global_rank'], '.'))); |
---|
311 | $option.= '- '.$category['name']; |
---|
312 | } |
---|
313 | |
---|
314 | $template->assign_block_vars( |
---|
315 | $blockname, |
---|
316 | array('SELECTED'=>$selected, |
---|
317 | 'VALUE'=>$category['id'], |
---|
318 | 'OPTION'=>$option |
---|
319 | )); |
---|
320 | } |
---|
321 | } |
---|
322 | |
---|
323 | function display_select_cat_wrapper($query, $selecteds, $blockname, |
---|
324 | $fullname = true) |
---|
325 | { |
---|
326 | $result = pwg_query($query); |
---|
327 | $categories = array(); |
---|
328 | if (!empty($result)) |
---|
329 | { |
---|
330 | while ($row = mysql_fetch_array($result)) |
---|
331 | { |
---|
332 | array_push($categories, $row); |
---|
333 | } |
---|
334 | } |
---|
335 | usort($categories, 'global_rank_compare'); |
---|
336 | display_select_categories($categories, $selecteds, $blockname, $fullname); |
---|
337 | } |
---|
338 | |
---|
339 | /** |
---|
340 | * returns all subcategory identifiers of given category ids |
---|
341 | * |
---|
342 | * @param array ids |
---|
343 | * @return array |
---|
344 | */ |
---|
345 | function get_subcat_ids($ids) |
---|
346 | { |
---|
347 | $query = ' |
---|
348 | SELECT DISTINCT(id) |
---|
349 | FROM '.CATEGORIES_TABLE.' |
---|
350 | WHERE '; |
---|
351 | foreach ($ids as $num => $category_id) |
---|
352 | { |
---|
353 | if ($num > 0) |
---|
354 | { |
---|
355 | $query.= ' |
---|
356 | OR '; |
---|
357 | } |
---|
358 | $query.= 'uppercats REGEXP \'(^|,)'.$category_id.'(,|$)\''; |
---|
359 | } |
---|
360 | $query.= ' |
---|
361 | ;'; |
---|
362 | $result = pwg_query($query); |
---|
363 | |
---|
364 | $subcats = array(); |
---|
365 | while ($row = mysql_fetch_array($result)) |
---|
366 | { |
---|
367 | array_push($subcats, $row['id']); |
---|
368 | } |
---|
369 | return $subcats; |
---|
370 | } |
---|
371 | |
---|
372 | function global_rank_compare($a, $b) |
---|
373 | { |
---|
374 | return strnatcasecmp($a['global_rank'], $b['global_rank']); |
---|
375 | } |
---|
376 | |
---|
377 | function rank_compare($a, $b) |
---|
378 | { |
---|
379 | if ($a['rank'] == $b['rank']) |
---|
380 | { |
---|
381 | return 0; |
---|
382 | } |
---|
383 | |
---|
384 | return ($a['rank'] < $b['rank']) ? -1 : 1; |
---|
385 | } |
---|
386 | ?> |
---|