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

Last change on this file since 2502 was 2502, checked in by rvelices, 16 years ago
  • better management of fatal errors (instead of die or trigger_error rather use fatal_error ...)
  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 21.3 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008      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
24function get_icon($date, $is_child_date = false)
25{
26  global $page, $user;
27
28  if (empty($date))
29  {
30    return '';
31  }
32
33  if (isset($page['get_icon_cache'][$date]))
34  {
35    if (! $page['get_icon_cache'][$date] )
36      return '';
37    return $page['get_icon_cache']['_icons_'][$is_child_date];
38  }
39
40  if (!isset($page['get_icon_cache']['sql_recent_date']))
41  {
42    // Use MySql date in order to standardize all recent "actions/queries"
43    list($page['get_icon_cache']['sql_recent_date']) =
44      mysql_fetch_array(pwg_query('select SUBDATE(
45      CURRENT_DATE,INTERVAL '.$user['recent_period'].' DAY)'));
46  }
47
48  $page['get_icon_cache'][$date] = false;
49  if ( $date > $page['get_icon_cache']['sql_recent_date'] )
50  {
51    if ( !isset($page['get_icon_cache']['_icons_'] ) )
52    {
53      $icons = array(false => 'recent', true => 'recent_by_child' );
54      $title = sprintf(
55        l10n('elements posted during the last %d days'),
56        $user['recent_period']
57        );
58      foreach ($icons as $key => $icon)
59      {
60        $icon_url = get_themeconf('icon_dir').'/'.$icon.'.png';
61        $size = getimagesize( PHPWG_ROOT_PATH.$icon_url );
62        $icon_url = get_root_url().$icon_url;
63        $output = '<img title="'.$title.'" src="'.$icon_url.'" class="icon" style="border:0;';
64        $output.= 'height:'.$size[1].'px;width:'.$size[0].'px" alt="(!)" />';
65        $page['get_icon_cache']['_icons_'][$key] = $output;
66      }
67    }
68    $page['get_icon_cache'][$date] = true;
69  }
70
71  if (! $page['get_icon_cache'][$date] )
72    return '';
73  return $page['get_icon_cache']['_icons_'][$is_child_date];
74}
75
76function create_navigation_bar(
77  $url, $nb_element, $start, $nb_element_page, $clean_url = false
78  )
79{
80  global $conf;
81
82  $pages_around = $conf['paginate_pages_around'];
83  $start_str = $clean_url ? '/start-' :
84    ( ( strstr($url, '?')===false ? '?':'&amp;') . 'start=' );
85
86  $navbar = '';
87
88  // current page detection
89  if (!isset($start)
90      or !is_numeric($start)
91      or (is_numeric($start) and $start < 0))
92  {
93    $start = 0;
94  }
95
96  // navigation bar useful only if more than one page to display !
97  if ($nb_element > $nb_element_page)
98  {
99    // current page and last page
100    $cur_page = ceil($start / $nb_element_page) + 1;
101    $maximum = ceil($nb_element / $nb_element_page);
102
103    // link to first page ?
104    if ($cur_page != 1)
105    {
106      $navbar.=
107        '<a href="'.$url.'" rel="first">'
108        .l10n('first_page')
109        .'</a>';
110    }
111    else
112    {
113      $navbar.= l10n('first_page');
114    }
115    $navbar.= ' | ';
116    // link on previous page ?
117    if ($start != 0)
118    {
119      $previous = $start - $nb_element_page;
120
121      $navbar.=
122        '<a href="'
123        .$url.($previous > 0 ? $start_str.$previous : '')
124        .'" rel="prev">'
125        .l10n('previous_page')
126        .'</a>';
127    }
128    else
129    {
130      $navbar.= l10n('previous_page');
131    }
132    $navbar.= ' |';
133
134    if ($cur_page > $pages_around + 1)
135    {
136      $navbar.= '&nbsp;<a href="'.$url.'">1</a>';
137
138      if ($cur_page > $pages_around + 2)
139      {
140        $navbar.= ' ...';
141      }
142    }
143
144    // inspired from punbb source code
145    for ($i = $cur_page - $pages_around, $stop = $cur_page + $pages_around + 1;
146         $i < $stop;
147         $i++)
148    {
149      if ($i < 1 or $i > $maximum)
150      {
151        continue;
152      }
153      else if ($i != $cur_page)
154      {
155        $temp_start = ($i - 1) * $nb_element_page;
156
157        $navbar.=
158          '&nbsp;'
159          .'<a href="'.$url
160          .($temp_start > 0 ? $start_str.$temp_start : '')
161          .'">'
162          .$i
163          .'</a>';
164      }
165      else
166      {
167        $navbar.=
168          '&nbsp;'
169          .'<span class="pageNumberSelected">'
170          .$i
171          .'</span>';
172      }
173    }
174
175    if ($cur_page < ($maximum - $pages_around))
176    {
177      $temp_start = ($maximum - 1) * $nb_element_page;
178
179      if ($cur_page < ($maximum - $pages_around - 1))
180      {
181        $navbar.= ' ...';
182      }
183
184      $navbar.= ' <a href="'.$url.$start_str.$temp_start.'">'.$maximum.'</a>';
185    }
186
187    $navbar.= ' | ';
188    // link on next page ?
189    if ($nb_element > $nb_element_page
190        and $start + $nb_element_page < $nb_element)
191    {
192      $next = $start + $nb_element_page;
193
194      $navbar.=
195        '<a href="'.$url.$start_str.$next.'" rel="next">'
196        .l10n('next_page')
197        .'</a>';
198    }
199    else
200    {
201      $navbar.= l10n('next_page');
202    }
203
204    $navbar.= ' | ';
205    // link to last page ?
206    if ($cur_page != $maximum)
207    {
208      $temp_start = ($maximum - 1) * $nb_element_page;
209
210      $navbar.=
211        '<a href="'.$url.$start_str.$temp_start.'" rel="last">'
212        .l10n('last_page')
213        .'</a>';
214    }
215    else
216    {
217      $navbar.= l10n('last_page');
218    }
219  }
220  return $navbar;
221}
222
223/**
224 * returns the list of categories as a HTML string
225 *
226 * categories string returned contains categories as given in the input
227 * array $cat_informations. $cat_informations array must be an array
228 * of array( id=>?, name=>?, permalink=>?). If url input parameter is null,
229 * returns only the categories name without links.
230 *
231 * @param array cat_informations
232 * @param string url
233 * @param boolean replace_space
234 * @return string
235 */
236function get_cat_display_name($cat_informations,
237                              $url = '',
238                              $replace_space = true)
239{
240  global $conf;
241
242  $output = '';
243  $is_first = true;
244  foreach ($cat_informations as $cat)
245  {
246    is_array($cat) or trigger_error(
247        'get_cat_display_name wrong type for category ', E_USER_WARNING
248      );
249
250    $cat['name'] = trigger_event(
251      'render_category_name',
252      $cat['name'],
253      'get_cat_display_name'
254      );
255
256    if ($is_first)
257    {
258      $is_first = false;
259    }
260    else
261    {
262      $output.= $conf['level_separator'];
263    }
264
265    if ( !isset($url) )
266    {
267      $output.= $cat['name'];
268    }
269    elseif ($url == '')
270    {
271      $output.= '<a href="'
272            .make_index_url(
273                array(
274                  'category' => $cat,
275                  )
276              )
277            .'">';
278      $output.= $cat['name'].'</a>';
279    }
280    else
281    {
282      $output.= '<a href="'.PHPWG_ROOT_PATH.$url.$cat['id'].'">';
283      $output.= $cat['name'].'</a>';
284    }
285  }
286  if ($replace_space)
287  {
288    return replace_space($output);
289  }
290  else
291  {
292    return $output;
293  }
294}
295
296/**
297 * returns the list of categories as a HTML string, with cache of names
298 *
299 * categories string returned contains categories as given in the input
300 * array $cat_informations. $uppercats is the list of category ids to
301 * display in the right order. If url input parameter is empty, returns only
302 * the categories name without links.
303 *
304 * @param string uppercats
305 * @param string url
306 * @param boolean replace_space
307 * @return string
308 */
309function get_cat_display_name_cache($uppercats,
310                                    $url = '',
311                                    $replace_space = true)
312{
313  global $cache, $conf;
314
315  if (!isset($cache['cat_names']))
316  {
317    $query = '
318SELECT id, name, permalink
319  FROM '.CATEGORIES_TABLE.'
320;';
321    $cache['cat_names'] = hash_from_query($query, 'id');
322  }
323
324  $output = '';
325  $is_first = true;
326  foreach (explode(',', $uppercats) as $category_id)
327  {
328    $cat = $cache['cat_names'][$category_id];
329
330    $cat['name'] = trigger_event(
331      'render_category_name',
332      $cat['name'],
333      'get_cat_display_name_cache'
334      );
335
336    if ($is_first)
337    {
338      $is_first = false;
339    }
340    else
341    {
342      $output.= $conf['level_separator'];
343    }
344
345    if ( !isset($url) )
346    {
347      $output.= $cat['name'];
348    }
349    elseif ($url == '')
350    {
351      $output.= '
352<a href="'
353      .make_index_url(
354          array(
355            'category' => $cat,
356            )
357        )
358      .'">'.$cat['name'].'</a>';
359    }
360    else
361    {
362      $output.= '
363<a href="'.PHPWG_ROOT_PATH.$url.$category_id.'">'.$cat['name'].'</a>';
364    }
365  }
366  if ($replace_space)
367  {
368    return replace_space($output);
369  }
370  else
371  {
372    return $output;
373  }
374}
375
376/**
377 * returns the HTML code for a category item in the menu (for the main page)
378 *
379 * HTML code generated uses logical list tags ul and each category is an
380 * item li. The paramter given is the category informations as an array,
381 * used keys are : id, name, nb_images, max_date_last, date_last
382 * count_images, count_categories
383 *
384 * @param array categories
385 * @return string
386 */
387function get_html_menu_category($categories, $selected_category)
388{
389  $ref_level = 0;
390  $level = 0;
391
392  $menu = trigger_event('get_html_menu_category', '',
393            $categories, $selected_category);
394  if (strlen($menu))
395  {
396    return $menu;
397  }
398
399  foreach ($categories as $category)
400  {
401    $level = substr_count($category['global_rank'], '.') + 1;
402    if ($level > $ref_level)
403    {
404      $menu.= "\n<ul>";
405    }
406    else if ($level == $ref_level)
407    {
408      $menu.= "\n</li>";
409    }
410    else if ($level < $ref_level)
411    {
412      // we may have to close more than one level at the same time...
413      $menu.= "\n</li>";
414      $menu.= str_repeat("\n</ul></li>",($ref_level-$level));
415    }
416    $ref_level = $level;
417
418    $menu.= "\n\n".'<li';
419    if ($category['id'] == @$selected_category['id'])
420    {
421      $menu.= ' class="selected"';
422    }
423    $menu.= '>';
424
425    $url = make_index_url(
426            array(
427              'category' => $category
428              )
429            );
430
431    $title = get_display_images_count
432                (
433                  $category['nb_images'],
434                  $category['count_images'],
435                  $category['count_categories'],
436                  false,
437                  ' / '
438                );
439
440    $menu.= '<a href="'.$url.'"';
441    if ($selected_category!=null
442        and $category['id'] == $selected_category['id_uppercat'])
443    {
444      $menu.= ' rel="up"';
445    }
446    $menu.= ' title="'.$title.'">';
447    $menu.= trigger_event(
448      'render_category_name',
449      $category['name'],
450      'get_html_menu_category'
451      );
452    $menu.= '</a>';
453
454    if ( $category['count_images']>0 )
455    {// at least one direct or indirect image
456      $menu.= "\n".'<span class="';
457      // at least one image in this category -> class menuInfoCat
458      $menu.= ($category['nb_images'] > 0 ? "menuInfoCat"
459                                          : "menuInfoCatByChild").'"';
460      $menu.= ' title=" '.$title.'">';
461      // show total number of images
462      $menu.= '['.$category['count_images'].']';
463      $menu.= '</span>';
464    }
465    $child_date_last = @$category['max_date_last']> @$category['date_last'];
466    $menu.= get_icon($category['max_date_last'], $child_date_last);
467  }
468
469  $menu.= str_repeat("\n</li></ul>",($level));
470
471  return $menu;
472}
473
474/**
475 * returns HTMLized comment contents retrieved from database
476 *
477 * newlines becomes br tags, _word_ becomes underline, /word/ becomes
478 * italic, *word* becomes bolded
479 *
480 * @param string content
481 * @return string
482 */
483function parse_comment_content($content)
484{
485  $pattern = '/(https?:\/\/\S*)/';
486  $replacement = '<a href="$1" rel="nofollow">$1</a>';
487  $content = preg_replace($pattern, $replacement, $content);
488
489  $content = nl2br($content);
490
491  // replace _word_ by an underlined word
492  $pattern = '/\b_(\S*)_\b/';
493  $replacement = '<span style="text-decoration:underline;">$1</span>';
494  $content = preg_replace($pattern, $replacement, $content);
495
496  // replace *word* by a bolded word
497  $pattern = '/\b\*(\S*)\*\b/';
498  $replacement = '<span style="font-weight:bold;">$1</span>';
499  $content = preg_replace($pattern, $replacement, $content);
500
501  // replace /word/ by an italic word
502  $pattern = "/\/(\S*)\/(\s)/";
503  $replacement = '<span style="font-style:italic;">$1$2</span>';
504  $content = preg_replace($pattern, $replacement, $content);
505
506  $content = '<div>'.$content.'</div>';
507  return $content;
508}
509
510function get_cat_display_name_from_id($cat_id,
511                                      $url = '',
512                                      $replace_space = true)
513{
514  $cat_info = get_cat_info($cat_id);
515  return get_cat_display_name($cat_info['upper_names'], $url, $replace_space);
516}
517
518/**
519 * Returns an HTML list of tags. It can be a multi select field or a list of
520 * checkboxes.
521 *
522 * @param string HTML field name
523 * @param array selected tag ids
524 * @return array
525 */
526function get_html_tag_selection(
527  $tags,
528  $fieldname,
529  $selecteds = array(),
530  $forbidden_categories = null
531  )
532{
533  global $conf;
534
535  if (count ($tags) == 0 )
536  {
537    return '';
538  }
539  $output = '<ul class="tagSelection">';
540  foreach ($tags as $tag)
541  {
542    $output.=
543      '<li>'
544      .'<label>'
545      .'<input type="checkbox" name="'.$fieldname.'[]"'
546      .' value="'.$tag['id'].'"'
547      ;
548
549    if (in_array($tag['id'], $selecteds))
550    {
551      $output.= ' checked="checked"';
552    }
553
554    $output.=
555      ' />'
556      .' '. $tag['name']
557      .'</label>'
558      .'</li>'
559      ."\n"
560      ;
561  }
562  $output.= '</ul>';
563
564  return $output;
565}
566
567function name_compare($a, $b)
568{
569  return strcmp(strtolower($a['name']), strtolower($b['name']));
570}
571
572function tag_alpha_compare($a, $b)
573{
574  return strcmp(strtolower($a['url_name']), strtolower($b['url_name']));
575}
576
577/**
578 * exits the current script (either exit or redirect)
579 */
580function access_denied()
581{
582  global $user;
583
584  $login_url =
585      get_root_url().'identification.php?redirect='
586      .urlencode(urlencode($_SERVER['REQUEST_URI']));
587
588  if ( isset($user) and !is_a_guest() )
589  {
590    echo '<div style="text-align:center;">'.l10n('access_forbiden').'<br />';
591    echo '<a href="'.get_root_url().'identification.php">'.l10n('identification').'</a>&nbsp;';
592    echo '<a href="'.make_index_url().'">'.l10n('home').'</a></div>';
593    exit();
594  }
595  else
596  {
597    set_status_header(401);
598    redirect_html($login_url);
599  }
600}
601
602/**
603 * exits the current script with 403 code
604 * @param string msg a message to display
605 * @param string alternate_url redirect to this url
606 */
607function page_forbidden($msg, $alternate_url=null)
608{
609  set_status_header(403);
610  if ($alternate_url==null)
611    $alternate_url = make_index_url();
612  redirect_html( $alternate_url,
613    '<div style="text-align:left; margin-left:5em;margin-bottom:5em;">
614<h1 style="text-align:left; font-size:36px;">Forbidden</h1><br/>'
615.$msg.'</div>',
616    5 );
617}
618
619/**
620 * exits the current script with 400 code
621 * @param string msg a message to display
622 * @param string alternate_url redirect to this url
623 */
624function bad_request($msg, $alternate_url=null)
625{
626  set_status_header(400);
627  if ($alternate_url==null)
628    $alternate_url = make_index_url();
629  redirect_html( $alternate_url,
630    '<div style="text-align:left; margin-left:5em;margin-bottom:5em;">
631<h1 style="text-align:left; font-size:36px;">Bad request</h1><br/>'
632.$msg.'</div>',
633    5 );
634}
635
636/**
637 * exits the current script with 404 code when a page cannot be found
638 * @param string msg a message to display
639 * @param string alternate_url redirect to this url
640 */
641function page_not_found($msg, $alternate_url=null)
642{
643  set_status_header(404);
644  if ($alternate_url==null)
645    $alternate_url = make_index_url();
646  redirect_html( $alternate_url,
647    '<div style="text-align:left; margin-left:5em;margin-bottom:5em;">
648<h1 style="text-align:left; font-size:36px;">Page not found</h1><br/>'
649.$msg.'</div>',
650    5 );
651}
652
653/**
654 * exits the current script with 500 http code
655 * this method can be called at any time (does not use template/language/user etc...)
656 * @param string msg a message to display
657 */
658function fatal_error($msg)
659{
660  $btrace_msg = '';
661  if (function_exists('debug_backtrace'))
662  {
663    $bt = debug_backtrace();
664    for ($i=1; $i<count($bt); $i++)
665    {
666      $class = isset($bt[$i]['class']) ? (@$bt[$i]['class'].'::') : '';
667      $btrace_msg .= "#$i\t".$class.@$bt[$i]['function'].' '.@$bt[$i]['file']."(".@$bt[$i]['line'].")\n";
668    }
669    $btrace_msg = trim($btrace_msg);
670    $msg .= "\n";
671  }
672
673  $display = "<h1>Piwigo encountered a non recoverable error</h1>
674<pre style='font-size:larger;background:white;color:red;padding:1em;margin:0;clear:both;display:block;width:auto;height:auto;overflow:auto'>
675<b>$msg</b>
676$btrace_msg
677</pre>\n";
678
679  @set_status_header(500);
680  echo $display.str_repeat( ' ', 300); //IE doesn't error output if below a size
681
682  if ( function_exists('ini_set') )
683  {// if possible turn off error display (we display it)
684    ini_set('display_errors', false);
685  }
686  error_reporting( E_ALL );
687  trigger_error( strip_tags($msg).$btrace_msg, E_USER_ERROR );
688  die(0); // just in case
689}
690
691/* returns the title to be displayed above thumbnails on tag page
692 */
693function get_tags_content_title()
694{
695  global $page;
696  $title = count($page['tags']) > 1 ? l10n('Tags') : l10n('Tag');
697  $title.= ' ';
698
699  for ($i=0; $i<count($page['tags']); $i++)
700  {
701    $title.= $i>0 ? ' + ' : '';
702
703    $title.=
704      '<a href="'
705      .make_index_url(
706        array(
707          'tags' => array( $page['tags'][$i] )
708          )
709        )
710      .'" title="'
711      .l10n('See elements linked to this tag only')
712      .'">'
713      .$page['tags'][$i]['name']
714      .'</a>';
715
716    if ( count($page['tags'])>2 )
717    {
718      $other_tags = $page['tags'];
719      unset ( $other_tags[$i] );
720      $title.=
721        '<a href="'
722        .make_index_url(
723          array(
724            'tags' => $other_tags
725            )
726          )
727        .'" style="border:none;" title="'
728        .l10n('remove this tag')
729        .'"><img src="'
730        .get_root_url().get_themeconf('icon_dir').'/remove_s.png'
731        .'" alt="x" style="vertical-align:bottom;" class="button"/>'
732        .'</a>';
733    }
734
735  }
736  return $title;
737}
738
739/**
740  Sets the http status header (200,401,...)
741 */
742function set_status_header($code, $text='')
743{
744  if (empty($text))
745  {
746    switch ($code)
747    {
748      case 200: $text='OK';break;
749      case 301: $text='Moved permanently';break;
750      case 302: $text='Moved temporarily';break;
751      case 304: $text='Not modified';break;
752      case 400: $text='Bad request';break;
753      case 401: $text='Authorization required';break;
754      case 403: $text='Forbidden';break;
755      case 404: $text='Not found';break;
756      case 500: $text='Server error';break;
757      case 503: $text='Service unavailable';break;
758    }
759  }
760        $protocol = $_SERVER["SERVER_PROTOCOL"];
761        if ( ('HTTP/1.1' != $protocol) && ('HTTP/1.0' != $protocol) )
762                $protocol = 'HTTP/1.0';
763
764        if ( version_compare( phpversion(), '4.3.0', '>=' ) )
765  {
766                header( "$protocol $code $text", true, $code );
767        }
768  else
769  {
770                header( "$protocol $code $text" );
771        }
772  trigger_action('set_status_header', $code, $text);
773}
774
775/** returns the category comment for rendering in html.
776 * this is an event handler. don't call directly
777 */
778function render_category_description($desc)
779{
780  global $conf;
781  if ( !( $conf['allow_html_descriptions'] and
782          preg_match('/<(div|br|img|script).*>/i', $desc) ) )
783  {
784    $desc = nl2br($desc);
785  }
786  return $desc;
787}
788
789/** returns the category comment for rendering in html textual mode (subcatify)
790 * this is an event handler. don't call directly
791 */
792function render_category_literal_description($desc)
793{
794  return strip_tags($desc, '<span><p><a><br><b><i><small><big><strong><em>');
795}
796
797/** returns the argument_ids array with new sequenced keys based on related
798 * names. Sequence is not case sensitive.
799 * Warning: By definition, this function breaks original keys
800 */
801function order_by_name($element_ids,$name)
802{
803  $ordered_element_ids = array();
804  foreach ($element_ids as $k_id => $element_id)
805  {
806    $key = strtolower($name[$element_id]) .'-'. $name[$element_id] .'-'. $k_id;
807    $ordered_element_ids[$key] = $element_id;
808  }
809  ksort($ordered_element_ids);
810  return $ordered_element_ids;
811}
812
813/*event handler for menu*/
814function register_default_menubar_blocks( $menu_ref_arr )
815{
816  $menu = & $menu_ref_arr[0];
817  if ($menu->get_id() != 'menubar')
818    return;
819  $menu->register_block( new RegisteredBlock( 'mbLinks', 'Links', 'piwigo'));
820  $menu->register_block( new RegisteredBlock( 'mbCategories', 'Categories', 'piwigo'));
821  $menu->register_block( new RegisteredBlock( 'mbTags', 'Related tags', 'piwigo'));
822  $menu->register_block( new RegisteredBlock( 'mbSpecials', 'special_categories', 'piwigo'));
823  $menu->register_block( new RegisteredBlock( 'mbMenu', 'title_menu', 'piwigo'));
824  $menu->register_block( new RegisteredBlock( 'mbIdentification', 'identification', 'piwigo') );
825}
826
827?>
Note: See TracBrowser for help on using the repository browser.