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

Last change on this file since 27369 was 27369, checked in by rvelices, 10 years ago

more query2array

  • Property svn:eol-style set to LF
File size: 17.5 KB
RevLine 
[476]1<?php
2// +-----------------------------------------------------------------------+
[8728]3// | Piwigo - a PHP based photo gallery                                    |
[2297]4// +-----------------------------------------------------------------------+
[26461]5// | Copyright(C) 2008-2014 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/**
[25548]25 * @package functions\html
26 */
27
28
29/**
30 * Generates breadcrumb from categories list.
31 * Categories string returned contains categories as given in the input
[1866]32 * array $cat_informations. $cat_informations array must be an array
33 * of array( id=>?, name=>?, permalink=>?). If url input parameter is null,
[572]34 * returns only the categories name without links.
35 *
[25548]36 * @param array $cat_informations
37 * @param string|null $url
[572]38 * @return string
39 */
[25548]40function get_cat_display_name($cat_informations, $url='')
[476]41{
[642]42  global $conf;
[1090]43
[5917]44  //$output = '<a href="'.get_absolute_root_url().$conf['home_page'].'">'.l10n('Home').'</a>';
45  $output = '';
[5924]46  $is_first=true;
[8247]47
[1866]48  foreach ($cat_informations as $cat)
[476]49  {
[1861]50    is_array($cat) or trigger_error(
[1866]51        'get_cat_display_name wrong type for category ', E_USER_WARNING
[1861]52      );
[2433]53
54    $cat['name'] = trigger_event(
55      'render_category_name',
56      $cat['name'],
57      'get_cat_display_name'
58      );
[5924]59
60    if ($is_first)
[5917]61    {
[5924]62      $is_first=false;
63    }
64    else
65    {
[5917]66      $output.= $conf['level_separator'];
67    }
[2433]68
[1092]69    if ( !isset($url) )
[569]70    {
[1861]71      $output.= $cat['name'];
[569]72    }
[1092]73    elseif ($url == '')
74    {
[1789]75      $output.= '<a href="'
[1748]76            .make_index_url(
[1131]77                array(
[1861]78                  'category' => $cat,
[1748]79                  )
[1131]80              )
81            .'">';
[1861]82      $output.= $cat['name'].'</a>';
[1092]83    }
[569]84    else
85    {
[1866]86      $output.= '<a href="'.PHPWG_ROOT_PATH.$url.$cat['id'].'">';
[1861]87      $output.= $cat['name'].'</a>';
[569]88    }
[476]89  }
[25425]90  return $output;
[476]91}
92
93/**
[25548]94 * Generates breadcrumb from categories list using a cache.
95 * @see get_cat_display_name()
[610]96 *
[25548]97 * @param string $uppercats
98 * @param string|null $url
99 * @param bool $single_link
100 * @param string|null $link_class
[610]101 * @return string
102 */
103function get_cat_display_name_cache($uppercats,
[1092]104                                    $url = '',
[9367]105                                    $single_link = false,
106                                    $link_class = null)
[610]107{
[1861]108  global $cache, $conf;
[610]109
[1861]110  if (!isset($cache['cat_names']))
[610]111  {
112    $query = '
[1866]113SELECT id, name, permalink
[610]114  FROM '.CATEGORIES_TABLE.'
115;';
[27369]116    $cache['cat_names'] = query2array($query, 'id');
[610]117  }
[1090]118
[610]119  $output = '';
[9367]120  if ($single_link)
121  {
122    $single_url = get_root_url().$url.array_pop(explode(',', $uppercats));
123    $output.= '<a href="'.$single_url.'"';
124    if (isset($link_class))
125    {
126      $output.= ' class="'.$link_class.'"';
127    }
128    $output.= '>';
129  }
[610]130  $is_first = true;
131  foreach (explode(',', $uppercats) as $category_id)
132  {
[1861]133    $cat = $cache['cat_names'][$category_id];
[1090]134
[2433]135    $cat['name'] = trigger_event(
136      'render_category_name',
137      $cat['name'],
138      'get_cat_display_name_cache'
139      );
140
[610]141    if ($is_first)
142    {
143      $is_first = false;
144    }
145    else
146    {
[642]147      $output.= $conf['level_separator'];
[610]148    }
149
[9367]150    if ( !isset($url) or $single_link )
[610]151    {
[1861]152      $output.= $cat['name'];
[610]153    }
[1092]154    elseif ($url == '')
155    {
156      $output.= '
[1789]157<a href="'
[1131]158      .make_index_url(
159          array(
[1861]160            'category' => $cat,
[1131]161            )
162        )
[1861]163      .'">'.$cat['name'].'</a>';
[1092]164    }
[610]165    else
166    {
167      $output.= '
[1861]168<a href="'.PHPWG_ROOT_PATH.$url.$category_id.'">'.$cat['name'].'</a>';
[610]169    }
170  }
[12920]171
[9367]172  if ($single_link and isset($single_url))
173  {
174    $output.= '</a>';
175  }
176
[25425]177  return $output;
[610]178}
179
180/**
[25548]181 * Generates breadcrumb for a category.
182 * @see get_cat_display_name()
[579]183 *
[25548]184 * @param int $cat_id
185 * @param string|null $url
186 * @return string
187 */
188function get_cat_display_name_from_id($cat_id, $url = '')
189{
190  $cat_info = get_cat_info($cat_id);
191  return get_cat_display_name($cat_info['upper_names'], $url);
192}
193
194/**
195 * Apply basic markdown transformations to a text.
196 * newlines becomes br tags
197 * _word_ becomes underline
198 * /word/ becomes italic
199 * *word* becomes bolded
200 * urls becomes a tags
[579]201 *
[25548]202 * @param string $content
[579]203 * @return string
204 */
[8247]205function render_comment_content($content)
[579]206{
[8247]207  $content = htmlspecialchars($content);
[1031]208  $pattern = '/(https?:\/\/\S*)/';
209  $replacement = '<a href="$1" rel="nofollow">$1</a>';
210  $content = preg_replace($pattern, $replacement, $content);
211
[579]212  $content = nl2br($content);
[1090]213
[579]214  // replace _word_ by an underlined word
[1030]215  $pattern = '/\b_(\S*)_\b/';
216  $replacement = '<span style="text-decoration:underline;">$1</span>';
[579]217  $content = preg_replace($pattern, $replacement, $content);
[1090]218
[579]219  // replace *word* by a bolded word
[1030]220  $pattern = '/\b\*(\S*)\*\b/';
221  $replacement = '<span style="font-weight:bold;">$1</span>';
[579]222  $content = preg_replace($pattern, $replacement, $content);
[1090]223
[579]224  // replace /word/ by an italic word
[1031]225  $pattern = "/\/(\S*)\/(\s)/";
226  $replacement = '<span style="font-style:italic;">$1$2</span>';
227  $content = preg_replace($pattern, $replacement, $content);
[579]228
[25548]229  // TODO : add a trigger
230
[579]231  return $content;
232}
[817]233
[1113]234/**
[1119]235 * Returns an HTML list of tags. It can be a multi select field or a list of
236 * checkboxes.
237 *
238 * @param string HTML field name
239 * @param array selected tag ids
240 * @return array
241 */
242function get_html_tag_selection(
243  $tags,
244  $fieldname,
245  $selecteds = array(),
246  $forbidden_categories = null
247  )
248{
249  global $conf;
[1131]250
[1201]251  if (count ($tags) == 0 )
252  {
253    return '';
254  }
[1119]255  $output = '<ul class="tagSelection">';
256  foreach ($tags as $tag)
257  {
258    $output.=
259      '<li>'
260      .'<label>'
261      .'<input type="checkbox" name="'.$fieldname.'[]"'
[1815]262      .' value="'.$tag['id'].'"'
[1119]263      ;
264
[1815]265    if (in_array($tag['id'], $selecteds))
[1119]266    {
267      $output.= ' checked="checked"';
268    }
[1131]269
[1119]270    $output.=
[11317]271      '> '
[8725]272      .$tag['name']
[1119]273      .'</label>'
274      .'</li>'
275      ."\n"
276      ;
277  }
278  $output.= '</ul>';
279
280  return $output;
281}
282
[25548]283/**
284 * Callback used for sorting by name.
285 */
[1119]286function name_compare($a, $b)
287{
[1310]288  return strcmp(strtolower($a['name']), strtolower($b['name']));
[1119]289}
290
[25548]291/**
292 * Callback used for sorting by name (slug) with cache.
293 */
[2409]294function tag_alpha_compare($a, $b)
295{
[13240]296  global $cache;
[12920]297
[12761]298  foreach (array($a, $b) as $tag)
299  {
[13240]300    if (!isset($cache[__FUNCTION__][ $tag['name'] ]))
[12761]301    {
[17748]302      $cache[__FUNCTION__][ $tag['name'] ] = transliterate($tag['name']);
[12761]303    }
304  }
305
[13240]306  return strcmp($cache[__FUNCTION__][ $a['name'] ], $cache[__FUNCTION__][ $b['name'] ]);
[2409]307}
308
[1119]309/**
[25548]310 * Exits the current script (or redirect to login page if not logged).
[1113]311 */
312function access_denied()
313{
[2265]314  global $user;
[1113]315
316  $login_url =
317      get_root_url().'identification.php?redirect='
318      .urlencode(urlencode($_SERVER['REQUEST_URI']));
319
[2543]320  set_status_header(401);
[2029]321  if ( isset($user) and !is_a_guest() )
[1113]322  {
[2884]323    echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8">';
[5021]324    echo '<div style="text-align:center;">'.l10n('You are not authorized to access the requested page').'<br>';
[6316]325    echo '<a href="'.get_root_url().'identification.php">'.l10n('Identification').'</a>&nbsp;';
[5021]326    echo '<a href="'.make_index_url().'">'.l10n('Home').'</a></div>';
[2543]327    echo str_repeat( ' ', 512); //IE6 doesn't error output if below a size
[1113]328    exit();
329  }
330  else
331  {
[1649]332    redirect_html($login_url);
[1113]333  }
334}
[1288]335
336/**
[25548]337 * Exits the current script with 403 code.
[25602]338 * @todo nice display if $template loaded
[25548]339 *
340 * @param string $msg
341 * @param string|null $alternate_url redirect to this url
[1652]342 */
343function page_forbidden($msg, $alternate_url=null)
344{
345  set_status_header(403);
346  if ($alternate_url==null)
347    $alternate_url = make_index_url();
348  redirect_html( $alternate_url,
349    '<div style="text-align:left; margin-left:5em;margin-bottom:5em;">
[15384]350<h1 style="text-align:left; font-size:36px;">'.l10n('Forbidden').'</h1><br>'
[1652]351.$msg.'</div>',
352    5 );
353}
354
355/**
[25548]356 * Exits the current script with 400 code.
[25602]357 * @todo nice display if $template loaded
[25548]358 *
359 * @param string $msg
360 * @param string|null $alternate_url redirect to this url
[1852]361 */
362function bad_request($msg, $alternate_url=null)
363{
364  set_status_header(400);
365  if ($alternate_url==null)
366    $alternate_url = make_index_url();
367  redirect_html( $alternate_url,
368    '<div style="text-align:left; margin-left:5em;margin-bottom:5em;">
[15384]369<h1 style="text-align:left; font-size:36px;">'.l10n('Bad request').'</h1><br>'
[1852]370.$msg.'</div>',
371    5 );
372}
373
374/**
[25548]375 * Exits the current script with 404 code.
[25602]376 * @todo nice display if $template loaded
[25548]377 *
378 * @param string $msg
379 * @param string|null $alternate_url redirect to this url
[1288]380 */
381function page_not_found($msg, $alternate_url=null)
382{
[1643]383  set_status_header(404);
[1288]384  if ($alternate_url==null)
385    $alternate_url = make_index_url();
[1649]386  redirect_html( $alternate_url,
[1288]387    '<div style="text-align:left; margin-left:5em;margin-bottom:5em;">
[15384]388<h1 style="text-align:left; font-size:36px;">'.l10n('Page not found').'</h1><br>'
[1288]389.$msg.'</div>',
390    5 );
391}
[1606]392
[2502]393/**
[25548]394 * Exits the current script with 500 code.
[25602]395 * @todo nice display if $template loaded
[25548]396 *
397 * @param string $msg
398 * @param string|null $title
399 * @param bool $show_trace
[2502]400 */
[5985]401function fatal_error($msg, $title=null, $show_trace=true)
[2502]402{
[5985]403  if (empty($title))
404  {
[15381]405    $title = l10n('Piwigo encountered a non recoverable error');
[5985]406  }
[8247]407
[2502]408  $btrace_msg = '';
[5985]409  if ($show_trace and function_exists('debug_backtrace'))
[2502]410  {
411    $bt = debug_backtrace();
412    for ($i=1; $i<count($bt); $i++)
413    {
414      $class = isset($bt[$i]['class']) ? (@$bt[$i]['class'].'::') : '';
415      $btrace_msg .= "#$i\t".$class.@$bt[$i]['function'].' '.@$bt[$i]['file']."(".@$bt[$i]['line'].")\n";
416    }
417    $btrace_msg = trim($btrace_msg);
418    $msg .= "\n";
419  }
420
[2747]421  $display = "<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
[5985]422<h1>$title</h1>
[2502]423<pre style='font-size:larger;background:white;color:red;padding:1em;margin:0;clear:both;display:block;width:auto;height:auto;overflow:auto'>
424<b>$msg</b>
425$btrace_msg
426</pre>\n";
427
428  @set_status_header(500);
[2543]429  echo $display.str_repeat( ' ', 300); //IE6 doesn't error output if below a size
[2502]430
431  if ( function_exists('ini_set') )
432  {// if possible turn off error display (we display it)
433    ini_set('display_errors', false);
434  }
435  error_reporting( E_ALL );
436  trigger_error( strip_tags($msg).$btrace_msg, E_USER_ERROR );
437  die(0); // just in case
438}
439
[25548]440/**
441 * Returns the breadcrumb to be displayed above thumbnails on tag page.
442 *
443 * @return string
[1606]444 */
445function get_tags_content_title()
446{
447  global $page;
[18667]448  $title = '<a href="'.get_root_url().'tags.php" title="'.l10n('display available tags').'">'
449    . l10n( count($page['tags']) > 1 ? 'Tags' : 'Tag' )
450    . '</a> ';
[1606]451
452  for ($i=0; $i<count($page['tags']); $i++)
453  {
454    $title.= $i>0 ? ' + ' : '';
455
456    $title.=
457      '<a href="'
458      .make_index_url(
459        array(
460          'tags' => array( $page['tags'][$i] )
461          )
462        )
463      .'" title="'
[8665]464      .l10n('display photos linked to this tag')
[1606]465      .'">'
[26649]466      .trigger_event('render_tag_name', $page['tags'][$i]['name'], $page['tags'][$i])
[1606]467      .'</a>';
468
[18667]469    if (count($page['tags']) > 2)
[1606]470    {
471      $other_tags = $page['tags'];
[5706]472      unset($other_tags[$i]);
473      $remove_url = make_index_url(
474        array(
475          'tags' => $other_tags
[1606]476          )
[5706]477        );
[18667]478
479      $title.=
480        '<a href="'.$remove_url.'" style="border:none;" title="'
481        .l10n('remove this tag from the list')
482        .'"><img src="'
483          .get_root_url().get_themeconf('icon_dir').'/remove_s.png'
484        .'" alt="x" style="vertical-align:bottom;">'
485        .'</a>';
[5706]486    }
[1606]487  }
488  return $title;
489}
[1643]490
491/**
[25548]492 * Sets the http status header (200,401,...)
493 * @param int $code
494 * @param string $text for exotic http codes
[1643]495 */
496function set_status_header($code, $text='')
497{
498  if (empty($text))
499  {
500    switch ($code)
501    {
502      case 200: $text='OK';break;
503      case 301: $text='Moved permanently';break;
504      case 302: $text='Moved temporarily';break;
505      case 304: $text='Not modified';break;
506      case 400: $text='Bad request';break;
507      case 401: $text='Authorization required';break;
508      case 403: $text='Forbidden';break;
509      case 404: $text='Not found';break;
[2053]510      case 500: $text='Server error';break;
[2543]511      case 501: $text='Not implemented';break;
[2053]512      case 503: $text='Service unavailable';break;
[1643]513    }
514  }
[5682]515  $protocol = $_SERVER["SERVER_PROTOCOL"];
516  if ( ('HTTP/1.1' != $protocol) && ('HTTP/1.0' != $protocol) )
517    $protocol = 'HTTP/1.0';
[2053]518
[8247]519  header( "$protocol $code $text", true, $code );
[1744]520  trigger_action('set_status_header', $code, $text);
[1643]521}
[1769]522
[25548]523/**
524 * Returns the category comment for rendering in html textual mode (subcatify)
525 * This method is called by a trigger_action()
526 *
527 * @param string $desc
528 * @return string
[2117]529 */
530function render_category_literal_description($desc)
531{
532  return strip_tags($desc, '<span><p><a><br><b><i><small><big><strong><em>');
533}
[2349]534
[25548]535/**
536 * Add known menubar blocks.
537 * This method is called by a trigger_event()
538 *
539 * @param BlockManager[] $menu_ref_arr
540 */
541function register_default_menubar_blocks($menu_ref_arr)
[2488]542{
543  $menu = & $menu_ref_arr[0];
544  if ($menu->get_id() != 'menubar')
545    return;
546  $menu->register_block( new RegisteredBlock( 'mbLinks', 'Links', 'piwigo'));
[6993]547  $menu->register_block( new RegisteredBlock( 'mbCategories', 'Albums', 'piwigo'));
[2488]548  $menu->register_block( new RegisteredBlock( 'mbTags', 'Related tags', 'piwigo'));
[5178]549  $menu->register_block( new RegisteredBlock( 'mbSpecials', 'Specials', 'piwigo'));
550  $menu->register_block( new RegisteredBlock( 'mbMenu', 'Menu', 'piwigo'));
551  $menu->register_block( new RegisteredBlock( 'mbIdentification', 'Identification', 'piwigo') );
[2488]552}
553
[12920]554/**
[25548]555 * Returns display name for an element.
556 * Returns 'name' if exists of name from 'file'.
557 *
558 * @param array $info at least file or name
559 * @return string
[12920]560 */
561function render_element_name($info)
562{
[25548]563  if (!empty($info['name']))
[12920]564  {
[25548]565    return trigger_event('render_element_name', $info['name']);
[12920]566  }
567  return get_name_from_file($info['file']);
568}
569
[25548]570/**
571 * Returns display description for an element.
572 *
573 * @param array $info at least comment
574 * @param string $param used to identify the trigger
575 * @return string
576 */
[25202]577function render_element_description($info, $param='')
[12920]578{
[25548]579  if (!empty($info['comment']))
[12920]580  {
[25548]581    return trigger_event('render_element_description', $info['comment'], $param);
[12920]582  }
583  return '';
584}
585
586/**
[25548]587 * Add info to the title of the thumbnail based on photo properties.
588 *
589 * @param array $info hit, rating_score, nb_comments
590 * @param string $title
591 * @param string $comment
592 * @return string
[12920]593 */
[25548]594function get_thumbnail_title($info, $title, $comment='')
[12920]595{
596  global $conf, $user;
597
598  $details = array();
599
600  if (!empty($info['hit']))
601  {
602    $details[] = $info['hit'].' '.strtolower(l10n('Visits'));
603  }
604
605  if ($conf['rate'] and !empty($info['rating_score']))
606  {
607    $details[] = strtolower(l10n('Rating score')).' '.$info['rating_score'];
608  }
609
610  if (isset($info['nb_comments']) and $info['nb_comments'] != 0)
611  {
612    $details[] = l10n_dec('%d comment', '%d comments', $info['nb_comments']);
613  }
614
615  if (count($details) > 0)
616  {
617    $title.= ' ('.implode(', ', $details).')';
618  }
619
[22520]620  if (!empty($comment))
[12920]621  {
[22520]622    $comment = strip_tags($comment);
623    $title.= ' '.substr($comment, 0, 100).(strlen($comment) > 100 ? '...' : '');
[12920]624  }
625
626  $title = htmlspecialchars(strip_tags($title));
627  $title = trigger_event('get_thumbnail_title', $title, $info);
[25548]628
[12920]629  return $title;
630}
631
[25548]632/**
633 * Event handler to protect src image urls.
634 *
635 * @param string $url
636 * @param SrcImage $src_image
637 * @return string
638 */
[20516]639function get_src_image_url_protection_handler($url, $src_image)
640{
641  return get_action_url($src_image->id, $src_image->is_original() ? 'e' : 'r', false);
642}
643
[25548]644/**
645 * Event handler to protect element urls.
646 *
647 * @param string $url
648 * @param array $infos id, path
649 * @return string
650 */
[20516]651function get_element_url_protection_handler($url, $infos)
652{
653  global $conf;
654  if ('images'==$conf['original_url_protection'])
655  {// protect only images and not other file types (for example large movies that we don't want to send through our file proxy)
656    $ext = get_extension($infos['path']);
657    if (!in_array($ext, $conf['picture_ext']))
658    {
659      return $url;
660    }
661  }
662  return get_action_url($infos['id'], 'e', false);
663}
664
[25548]665/**
666 * Sends to the template all messages stored in $page and in the session.
667 */
[20609]668function flush_page_messages()
669{
670  global $template, $page;
671  if ($template->get_template_vars('page_refresh') === null)
672  {
673    foreach (array('errors','infos','warnings') as $mode)
674    {
675      if (isset($_SESSION['page_'.$mode]))
676      {
677        $page[$mode] = array_merge($page[$mode], $_SESSION['page_'.$mode]);
678        unset($_SESSION['page_'.$mode]);
679      }
680
681      if (count($page[$mode]) != 0)
682      {
683        $template->assign($mode, $page[$mode]);
684      }
685    }
686  }
687}
688
[6993]689?>
Note: See TracBrowser for help on using the repository browser.