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
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based photo gallery                                    |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2014 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 * @package functions\html
26 */
27
28
29/**
30 * Generates breadcrumb from categories list.
31 * Categories string returned contains categories as given in the input
32 * array $cat_informations. $cat_informations array must be an array
33 * of array( id=>?, name=>?, permalink=>?). If url input parameter is null,
34 * returns only the categories name without links.
35 *
36 * @param array $cat_informations
37 * @param string|null $url
38 * @return string
39 */
40function get_cat_display_name($cat_informations, $url='')
41{
42  global $conf;
43
44  //$output = '<a href="'.get_absolute_root_url().$conf['home_page'].'">'.l10n('Home').'</a>';
45  $output = '';
46  $is_first=true;
47
48  foreach ($cat_informations as $cat)
49  {
50    is_array($cat) or trigger_error(
51        'get_cat_display_name wrong type for category ', E_USER_WARNING
52      );
53
54    $cat['name'] = trigger_event(
55      'render_category_name',
56      $cat['name'],
57      'get_cat_display_name'
58      );
59
60    if ($is_first)
61    {
62      $is_first=false;
63    }
64    else
65    {
66      $output.= $conf['level_separator'];
67    }
68
69    if ( !isset($url) )
70    {
71      $output.= $cat['name'];
72    }
73    elseif ($url == '')
74    {
75      $output.= '<a href="'
76            .make_index_url(
77                array(
78                  'category' => $cat,
79                  )
80              )
81            .'">';
82      $output.= $cat['name'].'</a>';
83    }
84    else
85    {
86      $output.= '<a href="'.PHPWG_ROOT_PATH.$url.$cat['id'].'">';
87      $output.= $cat['name'].'</a>';
88    }
89  }
90  return $output;
91}
92
93/**
94 * Generates breadcrumb from categories list using a cache.
95 * @see get_cat_display_name()
96 *
97 * @param string $uppercats
98 * @param string|null $url
99 * @param bool $single_link
100 * @param string|null $link_class
101 * @return string
102 */
103function get_cat_display_name_cache($uppercats,
104                                    $url = '',
105                                    $single_link = false,
106                                    $link_class = null)
107{
108  global $cache, $conf;
109
110  if (!isset($cache['cat_names']))
111  {
112    $query = '
113SELECT id, name, permalink
114  FROM '.CATEGORIES_TABLE.'
115;';
116    $cache['cat_names'] = query2array($query, 'id');
117  }
118
119  $output = '';
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  }
130  $is_first = true;
131  foreach (explode(',', $uppercats) as $category_id)
132  {
133    $cat = $cache['cat_names'][$category_id];
134
135    $cat['name'] = trigger_event(
136      'render_category_name',
137      $cat['name'],
138      'get_cat_display_name_cache'
139      );
140
141    if ($is_first)
142    {
143      $is_first = false;
144    }
145    else
146    {
147      $output.= $conf['level_separator'];
148    }
149
150    if ( !isset($url) or $single_link )
151    {
152      $output.= $cat['name'];
153    }
154    elseif ($url == '')
155    {
156      $output.= '
157<a href="'
158      .make_index_url(
159          array(
160            'category' => $cat,
161            )
162        )
163      .'">'.$cat['name'].'</a>';
164    }
165    else
166    {
167      $output.= '
168<a href="'.PHPWG_ROOT_PATH.$url.$category_id.'">'.$cat['name'].'</a>';
169    }
170  }
171
172  if ($single_link and isset($single_url))
173  {
174    $output.= '</a>';
175  }
176
177  return $output;
178}
179
180/**
181 * Generates breadcrumb for a category.
182 * @see get_cat_display_name()
183 *
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
201 *
202 * @param string $content
203 * @return string
204 */
205function render_comment_content($content)
206{
207  $content = htmlspecialchars($content);
208  $pattern = '/(https?:\/\/\S*)/';
209  $replacement = '<a href="$1" rel="nofollow">$1</a>';
210  $content = preg_replace($pattern, $replacement, $content);
211
212  $content = nl2br($content);
213
214  // replace _word_ by an underlined word
215  $pattern = '/\b_(\S*)_\b/';
216  $replacement = '<span style="text-decoration:underline;">$1</span>';
217  $content = preg_replace($pattern, $replacement, $content);
218
219  // replace *word* by a bolded word
220  $pattern = '/\b\*(\S*)\*\b/';
221  $replacement = '<span style="font-weight:bold;">$1</span>';
222  $content = preg_replace($pattern, $replacement, $content);
223
224  // replace /word/ by an italic word
225  $pattern = "/\/(\S*)\/(\s)/";
226  $replacement = '<span style="font-style:italic;">$1$2</span>';
227  $content = preg_replace($pattern, $replacement, $content);
228
229  // TODO : add a trigger
230
231  return $content;
232}
233
234/**
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;
250
251  if (count ($tags) == 0 )
252  {
253    return '';
254  }
255  $output = '<ul class="tagSelection">';
256  foreach ($tags as $tag)
257  {
258    $output.=
259      '<li>'
260      .'<label>'
261      .'<input type="checkbox" name="'.$fieldname.'[]"'
262      .' value="'.$tag['id'].'"'
263      ;
264
265    if (in_array($tag['id'], $selecteds))
266    {
267      $output.= ' checked="checked"';
268    }
269
270    $output.=
271      '> '
272      .$tag['name']
273      .'</label>'
274      .'</li>'
275      ."\n"
276      ;
277  }
278  $output.= '</ul>';
279
280  return $output;
281}
282
283/**
284 * Callback used for sorting by name.
285 */
286function name_compare($a, $b)
287{
288  return strcmp(strtolower($a['name']), strtolower($b['name']));
289}
290
291/**
292 * Callback used for sorting by name (slug) with cache.
293 */
294function tag_alpha_compare($a, $b)
295{
296  global $cache;
297
298  foreach (array($a, $b) as $tag)
299  {
300    if (!isset($cache[__FUNCTION__][ $tag['name'] ]))
301    {
302      $cache[__FUNCTION__][ $tag['name'] ] = transliterate($tag['name']);
303    }
304  }
305
306  return strcmp($cache[__FUNCTION__][ $a['name'] ], $cache[__FUNCTION__][ $b['name'] ]);
307}
308
309/**
310 * Exits the current script (or redirect to login page if not logged).
311 */
312function access_denied()
313{
314  global $user;
315
316  $login_url =
317      get_root_url().'identification.php?redirect='
318      .urlencode(urlencode($_SERVER['REQUEST_URI']));
319
320  set_status_header(401);
321  if ( isset($user) and !is_a_guest() )
322  {
323    echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8">';
324    echo '<div style="text-align:center;">'.l10n('You are not authorized to access the requested page').'<br>';
325    echo '<a href="'.get_root_url().'identification.php">'.l10n('Identification').'</a>&nbsp;';
326    echo '<a href="'.make_index_url().'">'.l10n('Home').'</a></div>';
327    echo str_repeat( ' ', 512); //IE6 doesn't error output if below a size
328    exit();
329  }
330  else
331  {
332    redirect_html($login_url);
333  }
334}
335
336/**
337 * Exits the current script with 403 code.
338 * @todo nice display if $template loaded
339 *
340 * @param string $msg
341 * @param string|null $alternate_url redirect to this url
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;">
350<h1 style="text-align:left; font-size:36px;">'.l10n('Forbidden').'</h1><br>'
351.$msg.'</div>',
352    5 );
353}
354
355/**
356 * Exits the current script with 400 code.
357 * @todo nice display if $template loaded
358 *
359 * @param string $msg
360 * @param string|null $alternate_url redirect to this url
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;">
369<h1 style="text-align:left; font-size:36px;">'.l10n('Bad request').'</h1><br>'
370.$msg.'</div>',
371    5 );
372}
373
374/**
375 * Exits the current script with 404 code.
376 * @todo nice display if $template loaded
377 *
378 * @param string $msg
379 * @param string|null $alternate_url redirect to this url
380 */
381function page_not_found($msg, $alternate_url=null)
382{
383  set_status_header(404);
384  if ($alternate_url==null)
385    $alternate_url = make_index_url();
386  redirect_html( $alternate_url,
387    '<div style="text-align:left; margin-left:5em;margin-bottom:5em;">
388<h1 style="text-align:left; font-size:36px;">'.l10n('Page not found').'</h1><br>'
389.$msg.'</div>',
390    5 );
391}
392
393/**
394 * Exits the current script with 500 code.
395 * @todo nice display if $template loaded
396 *
397 * @param string $msg
398 * @param string|null $title
399 * @param bool $show_trace
400 */
401function fatal_error($msg, $title=null, $show_trace=true)
402{
403  if (empty($title))
404  {
405    $title = l10n('Piwigo encountered a non recoverable error');
406  }
407
408  $btrace_msg = '';
409  if ($show_trace and function_exists('debug_backtrace'))
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
421  $display = "<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
422<h1>$title</h1>
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);
429  echo $display.str_repeat( ' ', 300); //IE6 doesn't error output if below a size
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
440/**
441 * Returns the breadcrumb to be displayed above thumbnails on tag page.
442 *
443 * @return string
444 */
445function get_tags_content_title()
446{
447  global $page;
448  $title = '<a href="'.get_root_url().'tags.php" title="'.l10n('display available tags').'">'
449    . l10n( count($page['tags']) > 1 ? 'Tags' : 'Tag' )
450    . '</a> ';
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="'
464      .l10n('display photos linked to this tag')
465      .'">'
466      .trigger_event('render_tag_name', $page['tags'][$i]['name'], $page['tags'][$i])
467      .'</a>';
468
469    if (count($page['tags']) > 2)
470    {
471      $other_tags = $page['tags'];
472      unset($other_tags[$i]);
473      $remove_url = make_index_url(
474        array(
475          'tags' => $other_tags
476          )
477        );
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>';
486    }
487  }
488  return $title;
489}
490
491/**
492 * Sets the http status header (200,401,...)
493 * @param int $code
494 * @param string $text for exotic http codes
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;
510      case 500: $text='Server error';break;
511      case 501: $text='Not implemented';break;
512      case 503: $text='Service unavailable';break;
513    }
514  }
515  $protocol = $_SERVER["SERVER_PROTOCOL"];
516  if ( ('HTTP/1.1' != $protocol) && ('HTTP/1.0' != $protocol) )
517    $protocol = 'HTTP/1.0';
518
519  header( "$protocol $code $text", true, $code );
520  trigger_action('set_status_header', $code, $text);
521}
522
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
529 */
530function render_category_literal_description($desc)
531{
532  return strip_tags($desc, '<span><p><a><br><b><i><small><big><strong><em>');
533}
534
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)
542{
543  $menu = & $menu_ref_arr[0];
544  if ($menu->get_id() != 'menubar')
545    return;
546  $menu->register_block( new RegisteredBlock( 'mbLinks', 'Links', 'piwigo'));
547  $menu->register_block( new RegisteredBlock( 'mbCategories', 'Albums', 'piwigo'));
548  $menu->register_block( new RegisteredBlock( 'mbTags', 'Related tags', 'piwigo'));
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') );
552}
553
554/**
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
560 */
561function render_element_name($info)
562{
563  if (!empty($info['name']))
564  {
565    return trigger_event('render_element_name', $info['name']);
566  }
567  return get_name_from_file($info['file']);
568}
569
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 */
577function render_element_description($info, $param='')
578{
579  if (!empty($info['comment']))
580  {
581    return trigger_event('render_element_description', $info['comment'], $param);
582  }
583  return '';
584}
585
586/**
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
593 */
594function get_thumbnail_title($info, $title, $comment='')
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
620  if (!empty($comment))
621  {
622    $comment = strip_tags($comment);
623    $title.= ' '.substr($comment, 0, 100).(strlen($comment) > 100 ? '...' : '');
624  }
625
626  $title = htmlspecialchars(strip_tags($title));
627  $title = trigger_event('get_thumbnail_title', $title, $info);
628
629  return $title;
630}
631
632/**
633 * Event handler to protect src image urls.
634 *
635 * @param string $url
636 * @param SrcImage $src_image
637 * @return string
638 */
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
644/**
645 * Event handler to protect element urls.
646 *
647 * @param string $url
648 * @param array $infos id, path
649 * @return string
650 */
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
665/**
666 * Sends to the template all messages stored in $page and in the session.
667 */
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
689?>
Note: See TracBrowser for help on using the repository browser.