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

Last change on this file since 5682 was 5682, checked in by grum, 14 years ago

bug 1580 - remove some hard coded markup (move them to the template)

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