1 | <?php |
---|
2 | // +-----------------------------------------------------------------------+ |
---|
3 | // | Piwigo - a PHP based picture gallery | |
---|
4 | // +-----------------------------------------------------------------------+ |
---|
5 | // | Copyright(C) 2008-2010 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 | |
---|
24 | /** |
---|
25 | * returns the list of categories as a HTML string |
---|
26 | * |
---|
27 | * categories string returned contains categories as given in the input |
---|
28 | * array $cat_informations. $cat_informations array must be an array |
---|
29 | * of array( id=>?, name=>?, permalink=>?). If url input parameter is null, |
---|
30 | * returns only the categories name without links. |
---|
31 | * |
---|
32 | * @param array cat_informations |
---|
33 | * @param string url |
---|
34 | * @param boolean replace_space |
---|
35 | * @return string |
---|
36 | */ |
---|
37 | function get_cat_display_name($cat_informations, |
---|
38 | $url = '', |
---|
39 | $replace_space = true) |
---|
40 | { |
---|
41 | global $conf; |
---|
42 | |
---|
43 | //$output = '<a href="'.get_absolute_root_url().$conf['home_page'].'">'.l10n('Home').'</a>'; |
---|
44 | $output = ''; |
---|
45 | $is_first=true; |
---|
46 | |
---|
47 | foreach ($cat_informations as $cat) |
---|
48 | { |
---|
49 | is_array($cat) or trigger_error( |
---|
50 | 'get_cat_display_name wrong type for category ', E_USER_WARNING |
---|
51 | ); |
---|
52 | |
---|
53 | $cat['name'] = trigger_event( |
---|
54 | 'render_category_name', |
---|
55 | $cat['name'], |
---|
56 | 'get_cat_display_name' |
---|
57 | ); |
---|
58 | |
---|
59 | if ($is_first) |
---|
60 | { |
---|
61 | $is_first=false; |
---|
62 | } |
---|
63 | else |
---|
64 | { |
---|
65 | $output.= $conf['level_separator']; |
---|
66 | } |
---|
67 | |
---|
68 | if ( !isset($url) ) |
---|
69 | { |
---|
70 | $output.= $cat['name']; |
---|
71 | } |
---|
72 | elseif ($url == '') |
---|
73 | { |
---|
74 | $output.= '<a href="' |
---|
75 | .make_index_url( |
---|
76 | array( |
---|
77 | 'category' => $cat, |
---|
78 | ) |
---|
79 | ) |
---|
80 | .'">'; |
---|
81 | $output.= $cat['name'].'</a>'; |
---|
82 | } |
---|
83 | else |
---|
84 | { |
---|
85 | $output.= '<a href="'.PHPWG_ROOT_PATH.$url.$cat['id'].'">'; |
---|
86 | $output.= $cat['name'].'</a>'; |
---|
87 | } |
---|
88 | } |
---|
89 | if ($replace_space) |
---|
90 | { |
---|
91 | return replace_space($output); |
---|
92 | } |
---|
93 | else |
---|
94 | { |
---|
95 | return $output; |
---|
96 | } |
---|
97 | } |
---|
98 | |
---|
99 | /** |
---|
100 | * returns the list of categories as a HTML string, with cache of names |
---|
101 | * |
---|
102 | * categories string returned contains categories as given in the input |
---|
103 | * array $cat_informations. $uppercats is the list of category ids to |
---|
104 | * display in the right order. If url input parameter is empty, returns only |
---|
105 | * the categories name without links. |
---|
106 | * |
---|
107 | * @param string uppercats |
---|
108 | * @param string url |
---|
109 | * @param boolean replace_space |
---|
110 | * @return string |
---|
111 | */ |
---|
112 | function get_cat_display_name_cache($uppercats, |
---|
113 | $url = '', |
---|
114 | $replace_space = true) |
---|
115 | { |
---|
116 | global $cache, $conf; |
---|
117 | |
---|
118 | if (!isset($cache['cat_names'])) |
---|
119 | { |
---|
120 | $query = ' |
---|
121 | SELECT id, name, permalink |
---|
122 | FROM '.CATEGORIES_TABLE.' |
---|
123 | ;'; |
---|
124 | $cache['cat_names'] = hash_from_query($query, 'id'); |
---|
125 | } |
---|
126 | |
---|
127 | $output = ''; |
---|
128 | $is_first = true; |
---|
129 | foreach (explode(',', $uppercats) as $category_id) |
---|
130 | { |
---|
131 | $cat = $cache['cat_names'][$category_id]; |
---|
132 | |
---|
133 | $cat['name'] = trigger_event( |
---|
134 | 'render_category_name', |
---|
135 | $cat['name'], |
---|
136 | 'get_cat_display_name_cache' |
---|
137 | ); |
---|
138 | |
---|
139 | if ($is_first) |
---|
140 | { |
---|
141 | $is_first = false; |
---|
142 | } |
---|
143 | else |
---|
144 | { |
---|
145 | $output.= $conf['level_separator']; |
---|
146 | } |
---|
147 | |
---|
148 | if ( !isset($url) ) |
---|
149 | { |
---|
150 | $output.= $cat['name']; |
---|
151 | } |
---|
152 | elseif ($url == '') |
---|
153 | { |
---|
154 | $output.= ' |
---|
155 | <a href="' |
---|
156 | .make_index_url( |
---|
157 | array( |
---|
158 | 'category' => $cat, |
---|
159 | ) |
---|
160 | ) |
---|
161 | .'">'.$cat['name'].'</a>'; |
---|
162 | } |
---|
163 | else |
---|
164 | { |
---|
165 | $output.= ' |
---|
166 | <a href="'.PHPWG_ROOT_PATH.$url.$category_id.'">'.$cat['name'].'</a>'; |
---|
167 | } |
---|
168 | } |
---|
169 | if ($replace_space) |
---|
170 | { |
---|
171 | return replace_space($output); |
---|
172 | } |
---|
173 | else |
---|
174 | { |
---|
175 | return $output; |
---|
176 | } |
---|
177 | } |
---|
178 | |
---|
179 | /** |
---|
180 | * returns HTMLized comment contents retrieved from database |
---|
181 | * |
---|
182 | * newlines becomes br tags, _word_ becomes underline, /word/ becomes |
---|
183 | * italic, *word* becomes bolded |
---|
184 | * |
---|
185 | * @param string content |
---|
186 | * @return string |
---|
187 | */ |
---|
188 | function parse_comment_content($content) |
---|
189 | { |
---|
190 | $pattern = '/(https?:\/\/\S*)/'; |
---|
191 | $replacement = '<a href="$1" rel="nofollow">$1</a>'; |
---|
192 | $content = preg_replace($pattern, $replacement, $content); |
---|
193 | |
---|
194 | $content = nl2br($content); |
---|
195 | |
---|
196 | // replace _word_ by an underlined word |
---|
197 | $pattern = '/\b_(\S*)_\b/'; |
---|
198 | $replacement = '<span style="text-decoration:underline;">$1</span>'; |
---|
199 | $content = preg_replace($pattern, $replacement, $content); |
---|
200 | |
---|
201 | // replace *word* by a bolded word |
---|
202 | $pattern = '/\b\*(\S*)\*\b/'; |
---|
203 | $replacement = '<span style="font-weight:bold;">$1</span>'; |
---|
204 | $content = preg_replace($pattern, $replacement, $content); |
---|
205 | |
---|
206 | // replace /word/ by an italic word |
---|
207 | $pattern = "/\/(\S*)\/(\s)/"; |
---|
208 | $replacement = '<span style="font-style:italic;">$1$2</span>'; |
---|
209 | $content = preg_replace($pattern, $replacement, $content); |
---|
210 | |
---|
211 | return $content; |
---|
212 | } |
---|
213 | |
---|
214 | function get_cat_display_name_from_id($cat_id, |
---|
215 | $url = '', |
---|
216 | $replace_space = true) |
---|
217 | { |
---|
218 | $cat_info = get_cat_info($cat_id); |
---|
219 | return get_cat_display_name($cat_info['upper_names'], $url, $replace_space); |
---|
220 | } |
---|
221 | |
---|
222 | /** |
---|
223 | * Returns an HTML list of tags. It can be a multi select field or a list of |
---|
224 | * checkboxes. |
---|
225 | * |
---|
226 | * @param string HTML field name |
---|
227 | * @param array selected tag ids |
---|
228 | * @return array |
---|
229 | */ |
---|
230 | function get_html_tag_selection( |
---|
231 | $tags, |
---|
232 | $fieldname, |
---|
233 | $selecteds = array(), |
---|
234 | $forbidden_categories = null |
---|
235 | ) |
---|
236 | { |
---|
237 | global $conf; |
---|
238 | |
---|
239 | if (count ($tags) == 0 ) |
---|
240 | { |
---|
241 | return ''; |
---|
242 | } |
---|
243 | $output = '<ul class="tagSelection">'; |
---|
244 | foreach ($tags as $tag) |
---|
245 | { |
---|
246 | $output.= |
---|
247 | '<li>' |
---|
248 | .'<label>' |
---|
249 | .'<input type="checkbox" name="'.$fieldname.'[]"' |
---|
250 | .' value="'.$tag['id'].'"' |
---|
251 | ; |
---|
252 | |
---|
253 | if (in_array($tag['id'], $selecteds)) |
---|
254 | { |
---|
255 | $output.= ' checked="checked"'; |
---|
256 | } |
---|
257 | |
---|
258 | $output.= |
---|
259 | '>' |
---|
260 | .' '. $tag['name'] |
---|
261 | .'</label>' |
---|
262 | .'</li>' |
---|
263 | ."\n" |
---|
264 | ; |
---|
265 | } |
---|
266 | $output.= '</ul>'; |
---|
267 | |
---|
268 | return $output; |
---|
269 | } |
---|
270 | |
---|
271 | function name_compare($a, $b) |
---|
272 | { |
---|
273 | return strcmp(strtolower($a['name']), strtolower($b['name'])); |
---|
274 | } |
---|
275 | |
---|
276 | function tag_alpha_compare($a, $b) |
---|
277 | { |
---|
278 | return strcmp(strtolower($a['url_name']), strtolower($b['url_name'])); |
---|
279 | } |
---|
280 | |
---|
281 | /** |
---|
282 | * exits the current script (either exit or redirect) |
---|
283 | */ |
---|
284 | function access_denied() |
---|
285 | { |
---|
286 | global $user; |
---|
287 | |
---|
288 | $login_url = |
---|
289 | get_root_url().'identification.php?redirect=' |
---|
290 | .urlencode(urlencode($_SERVER['REQUEST_URI'])); |
---|
291 | |
---|
292 | set_status_header(401); |
---|
293 | if ( isset($user) and !is_a_guest() ) |
---|
294 | { |
---|
295 | echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8">'; |
---|
296 | echo '<div style="text-align:center;">'.l10n('You are not authorized to access the requested page').'<br>'; |
---|
297 | echo '<a href="'.get_root_url().'identification.php">'.l10n('Identification').'</a> '; |
---|
298 | echo '<a href="'.make_index_url().'">'.l10n('Home').'</a></div>'; |
---|
299 | echo str_repeat( ' ', 512); //IE6 doesn't error output if below a size |
---|
300 | exit(); |
---|
301 | } |
---|
302 | else |
---|
303 | { |
---|
304 | redirect_html($login_url); |
---|
305 | } |
---|
306 | } |
---|
307 | |
---|
308 | /** |
---|
309 | * exits the current script with 403 code |
---|
310 | * @param string msg a message to display |
---|
311 | * @param string alternate_url redirect to this url |
---|
312 | */ |
---|
313 | function page_forbidden($msg, $alternate_url=null) |
---|
314 | { |
---|
315 | set_status_header(403); |
---|
316 | if ($alternate_url==null) |
---|
317 | $alternate_url = make_index_url(); |
---|
318 | redirect_html( $alternate_url, |
---|
319 | '<div style="text-align:left; margin-left:5em;margin-bottom:5em;"> |
---|
320 | <h1 style="text-align:left; font-size:36px;">Forbidden</h1><br>' |
---|
321 | .$msg.'</div>', |
---|
322 | 5 ); |
---|
323 | } |
---|
324 | |
---|
325 | /** |
---|
326 | * exits the current script with 400 code |
---|
327 | * @param string msg a message to display |
---|
328 | * @param string alternate_url redirect to this url |
---|
329 | */ |
---|
330 | function bad_request($msg, $alternate_url=null) |
---|
331 | { |
---|
332 | set_status_header(400); |
---|
333 | if ($alternate_url==null) |
---|
334 | $alternate_url = make_index_url(); |
---|
335 | redirect_html( $alternate_url, |
---|
336 | '<div style="text-align:left; margin-left:5em;margin-bottom:5em;"> |
---|
337 | <h1 style="text-align:left; font-size:36px;">Bad request</h1><br>' |
---|
338 | .$msg.'</div>', |
---|
339 | 5 ); |
---|
340 | } |
---|
341 | |
---|
342 | /** |
---|
343 | * exits the current script with 404 code when a page cannot be found |
---|
344 | * @param string msg a message to display |
---|
345 | * @param string alternate_url redirect to this url |
---|
346 | */ |
---|
347 | function page_not_found($msg, $alternate_url=null) |
---|
348 | { |
---|
349 | set_status_header(404); |
---|
350 | if ($alternate_url==null) |
---|
351 | $alternate_url = make_index_url(); |
---|
352 | redirect_html( $alternate_url, |
---|
353 | '<div style="text-align:left; margin-left:5em;margin-bottom:5em;"> |
---|
354 | <h1 style="text-align:left; font-size:36px;">Page not found</h1><br>' |
---|
355 | .$msg.'</div>', |
---|
356 | 5 ); |
---|
357 | } |
---|
358 | |
---|
359 | /** |
---|
360 | * exits the current script with 500 http code |
---|
361 | * this method can be called at any time (does not use template/language/user etc...) |
---|
362 | * @param string msg a message to display |
---|
363 | */ |
---|
364 | function fatal_error($msg, $title=null, $show_trace=true) |
---|
365 | { |
---|
366 | if (empty($title)) |
---|
367 | { |
---|
368 | $title = 'Piwigo encountered a non recoverable error'; |
---|
369 | } |
---|
370 | |
---|
371 | $btrace_msg = ''; |
---|
372 | if ($show_trace and function_exists('debug_backtrace')) |
---|
373 | { |
---|
374 | $bt = debug_backtrace(); |
---|
375 | for ($i=1; $i<count($bt); $i++) |
---|
376 | { |
---|
377 | $class = isset($bt[$i]['class']) ? (@$bt[$i]['class'].'::') : ''; |
---|
378 | $btrace_msg .= "#$i\t".$class.@$bt[$i]['function'].' '.@$bt[$i]['file']."(".@$bt[$i]['line'].")\n"; |
---|
379 | } |
---|
380 | $btrace_msg = trim($btrace_msg); |
---|
381 | $msg .= "\n"; |
---|
382 | } |
---|
383 | |
---|
384 | $display = "<meta http-equiv='Content-Type' content='text/html; charset=utf-8'> |
---|
385 | <h1>$title</h1> |
---|
386 | <pre style='font-size:larger;background:white;color:red;padding:1em;margin:0;clear:both;display:block;width:auto;height:auto;overflow:auto'> |
---|
387 | <b>$msg</b> |
---|
388 | $btrace_msg |
---|
389 | </pre>\n"; |
---|
390 | |
---|
391 | @set_status_header(500); |
---|
392 | echo $display.str_repeat( ' ', 300); //IE6 doesn't error output if below a size |
---|
393 | |
---|
394 | if ( function_exists('ini_set') ) |
---|
395 | {// if possible turn off error display (we display it) |
---|
396 | ini_set('display_errors', false); |
---|
397 | } |
---|
398 | error_reporting( E_ALL ); |
---|
399 | trigger_error( strip_tags($msg).$btrace_msg, E_USER_ERROR ); |
---|
400 | die(0); // just in case |
---|
401 | } |
---|
402 | |
---|
403 | /* returns the title to be displayed above thumbnails on tag page |
---|
404 | */ |
---|
405 | function get_tags_content_title() |
---|
406 | { |
---|
407 | global $page; |
---|
408 | $title = count($page['tags']) > 1 ? l10n('Tag') : l10n('Tag'); |
---|
409 | $title.= ' '; |
---|
410 | |
---|
411 | for ($i=0; $i<count($page['tags']); $i++) |
---|
412 | { |
---|
413 | $title.= $i>0 ? ' + ' : ''; |
---|
414 | |
---|
415 | $title.= |
---|
416 | '<a href="' |
---|
417 | .make_index_url( |
---|
418 | array( |
---|
419 | 'tags' => array( $page['tags'][$i] ) |
---|
420 | ) |
---|
421 | ) |
---|
422 | .'" title="' |
---|
423 | .l10n('See images linked to this tag only') |
---|
424 | .'">' |
---|
425 | .$page['tags'][$i]['name'] |
---|
426 | .'</a>'; |
---|
427 | |
---|
428 | $remove_url = null; |
---|
429 | if (count($page['tags']) == 1) |
---|
430 | { |
---|
431 | $remove_url = get_root_url().'tags.php'; |
---|
432 | } |
---|
433 | else |
---|
434 | { |
---|
435 | $other_tags = $page['tags']; |
---|
436 | unset($other_tags[$i]); |
---|
437 | $remove_url = make_index_url( |
---|
438 | array( |
---|
439 | 'tags' => $other_tags |
---|
440 | ) |
---|
441 | ); |
---|
442 | } |
---|
443 | |
---|
444 | $title.= |
---|
445 | '<a href="'.$remove_url.'" style="border:none;" title="' |
---|
446 | .l10n('remove this tag from the list') |
---|
447 | .'"><img src="' |
---|
448 | .get_root_url().get_themeconf('icon_dir').'/remove_s.png' |
---|
449 | .'" alt="x" style="vertical-align:bottom;" class="button">' |
---|
450 | .'</a>'; |
---|
451 | } |
---|
452 | return $title; |
---|
453 | } |
---|
454 | |
---|
455 | /** |
---|
456 | Sets the http status header (200,401,...) |
---|
457 | */ |
---|
458 | function set_status_header($code, $text='') |
---|
459 | { |
---|
460 | if (empty($text)) |
---|
461 | { |
---|
462 | switch ($code) |
---|
463 | { |
---|
464 | case 200: $text='OK';break; |
---|
465 | case 301: $text='Moved permanently';break; |
---|
466 | case 302: $text='Moved temporarily';break; |
---|
467 | case 304: $text='Not modified';break; |
---|
468 | case 400: $text='Bad request';break; |
---|
469 | case 401: $text='Authorization required';break; |
---|
470 | case 403: $text='Forbidden';break; |
---|
471 | case 404: $text='Not found';break; |
---|
472 | case 500: $text='Server error';break; |
---|
473 | case 501: $text='Not implemented';break; |
---|
474 | case 503: $text='Service unavailable';break; |
---|
475 | } |
---|
476 | } |
---|
477 | $protocol = $_SERVER["SERVER_PROTOCOL"]; |
---|
478 | if ( ('HTTP/1.1' != $protocol) && ('HTTP/1.0' != $protocol) ) |
---|
479 | $protocol = 'HTTP/1.0'; |
---|
480 | |
---|
481 | if ( version_compare( phpversion(), '4.3.0', '>=' ) ) |
---|
482 | { |
---|
483 | header( "$protocol $code $text", true, $code ); |
---|
484 | } |
---|
485 | else |
---|
486 | { |
---|
487 | header( "$protocol $code $text" ); |
---|
488 | } |
---|
489 | trigger_action('set_status_header', $code, $text); |
---|
490 | } |
---|
491 | |
---|
492 | /** returns the category comment for rendering in html textual mode (subcatify) |
---|
493 | * this is an event handler. don't call directly |
---|
494 | */ |
---|
495 | function render_category_literal_description($desc) |
---|
496 | { |
---|
497 | return strip_tags($desc, '<span><p><a><br><b><i><small><big><strong><em>'); |
---|
498 | } |
---|
499 | |
---|
500 | /** returns the argument_ids array with new sequenced keys based on related |
---|
501 | * names. Sequence is not case sensitive. |
---|
502 | * Warning: By definition, this function breaks original keys |
---|
503 | */ |
---|
504 | function order_by_name($element_ids,$name) |
---|
505 | { |
---|
506 | $ordered_element_ids = array(); |
---|
507 | foreach ($element_ids as $k_id => $element_id) |
---|
508 | { |
---|
509 | $key = strtolower($name[$element_id]) .'-'. $name[$element_id] .'-'. $k_id; |
---|
510 | $ordered_element_ids[$key] = $element_id; |
---|
511 | } |
---|
512 | ksort($ordered_element_ids); |
---|
513 | return $ordered_element_ids; |
---|
514 | } |
---|
515 | |
---|
516 | /*event handler for menu*/ |
---|
517 | function register_default_menubar_blocks( $menu_ref_arr ) |
---|
518 | { |
---|
519 | $menu = & $menu_ref_arr[0]; |
---|
520 | if ($menu->get_id() != 'menubar') |
---|
521 | return; |
---|
522 | $menu->register_block( new RegisteredBlock( 'mbLinks', 'Links', 'piwigo')); |
---|
523 | $menu->register_block( new RegisteredBlock( 'mbCategories', 'Categories', 'piwigo')); |
---|
524 | $menu->register_block( new RegisteredBlock( 'mbTags', 'Related tags', 'piwigo')); |
---|
525 | $menu->register_block( new RegisteredBlock( 'mbSpecials', 'Specials', 'piwigo')); |
---|
526 | $menu->register_block( new RegisteredBlock( 'mbMenu', 'Menu', 'piwigo')); |
---|
527 | $menu->register_block( new RegisteredBlock( 'mbIdentification', 'Identification', 'piwigo') ); |
---|
528 | } |
---|
529 | |
---|
530 | ?> |
---|