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
RevLine 
[476]1<?php
2// +-----------------------------------------------------------------------+
[2297]3// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
[5196]5// | Copyright(C) 2008-2010 Piwigo Team                  http://piwigo.org |
[2297]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// +-----------------------------------------------------------------------+
[476]23
[572]24/**
25 * returns the list of categories as a HTML string
26 *
27 * categories string returned contains categories as given in the input
[1866]28 * array $cat_informations. $cat_informations array must be an array
29 * of array( id=>?, name=>?, permalink=>?). If url input parameter is null,
[572]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 */
[569]37function get_cat_display_name($cat_informations,
[1092]38                              $url = '',
[569]39                              $replace_space = true)
[476]40{
[642]41  global $conf;
[1090]42
[5917]43  //$output = '<a href="'.get_absolute_root_url().$conf['home_page'].'">'.l10n('Home').'</a>';
44  $output = '';
45  $is_first = true;
46 
[1866]47  foreach ($cat_informations as $cat)
[476]48  {
[1861]49    is_array($cat) or trigger_error(
[1866]50        'get_cat_display_name wrong type for category ', E_USER_WARNING
[1861]51      );
[2433]52
53    $cat['name'] = trigger_event(
54      'render_category_name',
55      $cat['name'],
56      'get_cat_display_name'
57      );
[5917]58   
59    if (!$is_first)
60    {
61      $output.= $conf['level_separator'];
62      $is_first = false;
63    }
[2433]64
[1092]65    if ( !isset($url) )
[569]66    {
[1861]67      $output.= $cat['name'];
[569]68    }
[1092]69    elseif ($url == '')
70    {
[1789]71      $output.= '<a href="'
[1748]72            .make_index_url(
[1131]73                array(
[1861]74                  'category' => $cat,
[1748]75                  )
[1131]76              )
77            .'">';
[1861]78      $output.= $cat['name'].'</a>';
[1092]79    }
[569]80    else
81    {
[1866]82      $output.= '<a href="'.PHPWG_ROOT_PATH.$url.$cat['id'].'">';
[1861]83      $output.= $cat['name'].'</a>';
[569]84    }
[476]85  }
[569]86  if ($replace_space)
87  {
88    return replace_space($output);
89  }
90  else
91  {
92    return $output;
93  }
[476]94}
95
96/**
[610]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,
[1092]110                                    $url = '',
[610]111                                    $replace_space = true)
112{
[1861]113  global $cache, $conf;
[610]114
[1861]115  if (!isset($cache['cat_names']))
[610]116  {
117    $query = '
[1866]118SELECT id, name, permalink
[610]119  FROM '.CATEGORIES_TABLE.'
120;';
[1866]121    $cache['cat_names'] = hash_from_query($query, 'id');
[610]122  }
[1090]123
[610]124  $output = '';
125  $is_first = true;
126  foreach (explode(',', $uppercats) as $category_id)
127  {
[1861]128    $cat = $cache['cat_names'][$category_id];
[1090]129
[2433]130    $cat['name'] = trigger_event(
131      'render_category_name',
132      $cat['name'],
133      'get_cat_display_name_cache'
134      );
135
[610]136    if ($is_first)
137    {
138      $is_first = false;
139    }
140    else
141    {
[642]142      $output.= $conf['level_separator'];
[610]143    }
144
[1092]145    if ( !isset($url) )
[610]146    {
[1861]147      $output.= $cat['name'];
[610]148    }
[1092]149    elseif ($url == '')
150    {
151      $output.= '
[1789]152<a href="'
[1131]153      .make_index_url(
154          array(
[1861]155            'category' => $cat,
[1131]156            )
157        )
[1861]158      .'">'.$cat['name'].'</a>';
[1092]159    }
[610]160    else
161    {
162      $output.= '
[1861]163<a href="'.PHPWG_ROOT_PATH.$url.$category_id.'">'.$cat['name'].'</a>';
[610]164    }
165  }
166  if ($replace_space)
167  {
168    return replace_space($output);
169  }
170  else
171  {
172    return $output;
173  }
174}
175
176/**
[579]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{
[1031]187  $pattern = '/(https?:\/\/\S*)/';
188  $replacement = '<a href="$1" rel="nofollow">$1</a>';
189  $content = preg_replace($pattern, $replacement, $content);
190
[579]191  $content = nl2br($content);
[1090]192
[579]193  // replace _word_ by an underlined word
[1030]194  $pattern = '/\b_(\S*)_\b/';
195  $replacement = '<span style="text-decoration:underline;">$1</span>';
[579]196  $content = preg_replace($pattern, $replacement, $content);
[1090]197
[579]198  // replace *word* by a bolded word
[1030]199  $pattern = '/\b\*(\S*)\*\b/';
200  $replacement = '<span style="font-weight:bold;">$1</span>';
[579]201  $content = preg_replace($pattern, $replacement, $content);
[1090]202
[579]203  // replace /word/ by an italic word
[1031]204  $pattern = "/\/(\S*)\/(\s)/";
205  $replacement = '<span style="font-style:italic;">$1$2</span>';
206  $content = preg_replace($pattern, $replacement, $content);
[579]207
208  return $content;
209}
[817]210
211function get_cat_display_name_from_id($cat_id,
[1092]212                                      $url = '',
[817]213                                      $replace_space = true)
214{
215  $cat_info = get_cat_info($cat_id);
[1861]216  return get_cat_display_name($cat_info['upper_names'], $url, $replace_space);
[817]217}
[1113]218
219/**
[1119]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;
[1131]235
[1201]236  if (count ($tags) == 0 )
237  {
238    return '';
239  }
[1119]240  $output = '<ul class="tagSelection">';
241  foreach ($tags as $tag)
242  {
243    $output.=
244      '<li>'
245      .'<label>'
246      .'<input type="checkbox" name="'.$fieldname.'[]"'
[1815]247      .' value="'.$tag['id'].'"'
[1119]248      ;
249
[1815]250    if (in_array($tag['id'], $selecteds))
[1119]251    {
252      $output.= ' checked="checked"';
253    }
[1131]254
[1119]255    $output.=
[3185]256      '>'
[1290]257      .' '. $tag['name']
[1119]258      .'</label>'
259      .'</li>'
260      ."\n"
261      ;
262  }
263  $output.= '</ul>';
264
265  return $output;
266}
267
268function name_compare($a, $b)
269{
[1310]270  return strcmp(strtolower($a['name']), strtolower($b['name']));
[1119]271}
272
[2409]273function tag_alpha_compare($a, $b)
274{
275  return strcmp(strtolower($a['url_name']), strtolower($b['url_name']));
276}
277
[1119]278/**
[1113]279 * exits the current script (either exit or redirect)
280 */
281function access_denied()
282{
[2265]283  global $user;
[1113]284
285  $login_url =
286      get_root_url().'identification.php?redirect='
287      .urlencode(urlencode($_SERVER['REQUEST_URI']));
288
[2543]289  set_status_header(401);
[2029]290  if ( isset($user) and !is_a_guest() )
[1113]291  {
[2884]292    echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8">';
[5021]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>';
[2543]296    echo str_repeat( ' ', 512); //IE6 doesn't error output if below a size
[1113]297    exit();
298  }
299  else
300  {
[1649]301    redirect_html($login_url);
[1113]302  }
303}
[1288]304
305/**
[1652]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;">
[3185]317<h1 style="text-align:left; font-size:36px;">Forbidden</h1><br>'
[1652]318.$msg.'</div>',
319    5 );
320}
321
322/**
[1852]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;">
[3185]334<h1 style="text-align:left; font-size:36px;">Bad request</h1><br>'
[1852]335.$msg.'</div>',
336    5 );
337}
338
339/**
[1288]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{
[1643]346  set_status_header(404);
[1288]347  if ($alternate_url==null)
348    $alternate_url = make_index_url();
[1649]349  redirect_html( $alternate_url,
[1288]350    '<div style="text-align:left; margin-left:5em;margin-bottom:5em;">
[3185]351<h1 style="text-align:left; font-size:36px;">Page not found</h1><br>'
[1288]352.$msg.'</div>',
353    5 );
354}
[1606]355
[2502]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
[2747]376  $display = "<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
377<h1>Piwigo encountered a non recoverable error</h1>
[2502]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);
[2543]384  echo $display.str_repeat( ' ', 300); //IE6 doesn't error output if below a size
[2502]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
[1606]395/* returns the title to be displayed above thumbnails on tag page
396 */
397function get_tags_content_title()
398{
399  global $page;
[5021]400  $title = count($page['tags']) > 1 ? l10n('Tag') : l10n('Tag');
[1606]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="'
[5021]415      .l10n('See images linked to this tag only')
[1606]416      .'">'
417      .$page['tags'][$i]['name']
418      .'</a>';
419
[5706]420    $remove_url = null;
421    if (count($page['tags']) == 1)
[1606]422    {
[5706]423      $remove_url = get_root_url().'tags.php';
424    }
425    else
426    {
[1606]427      $other_tags = $page['tags'];
[5706]428      unset($other_tags[$i]);
429      $remove_url = make_index_url(
430        array(
431          'tags' => $other_tags
[1606]432          )
[5706]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="'
[1606]440        .get_root_url().get_themeconf('icon_dir').'/remove_s.png'
[5706]441      .'" alt="x" style="vertical-align:bottom;" class="button">'
442      .'</a>';
[1606]443  }
444  return $title;
445}
[1643]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;
[2053]464      case 500: $text='Server error';break;
[2543]465      case 501: $text='Not implemented';break;
[2053]466      case 503: $text='Service unavailable';break;
[1643]467    }
468  }
[5682]469  $protocol = $_SERVER["SERVER_PROTOCOL"];
470  if ( ('HTTP/1.1' != $protocol) && ('HTTP/1.0' != $protocol) )
471    $protocol = 'HTTP/1.0';
[2053]472
[5682]473  if ( version_compare( phpversion(), '4.3.0', '>=' ) )
[2053]474  {
[5682]475    header( "$protocol $code $text", true, $code );
476  }
[2053]477  else
478  {
[5682]479    header( "$protocol $code $text" );
480  }
[1744]481  trigger_action('set_status_header', $code, $text);
[1643]482}
[1769]483
[2117]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}
[2349]491
[2409]492/** returns the argument_ids array with new sequenced keys based on related
[2349]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
[2488]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'));
[5178]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') );
[2488]520}
521
[5441]522?>
Note: See TracBrowser for help on using the repository browser.