source: trunk/include/functions_html.inc.php @ 5917

Last change on this file since 5917 was 5917, checked in by laurent.duretz, 14 years ago

Issue 1521 : correction for admin pages

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