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