source: branches/2.0/include/functions_html.inc.php @ 5451

Last change on this file since 5451 was 5254, checked in by laurent.duretz, 14 years ago

Issue 1521 : Integration of LinkRoot plugin in Piwigo core

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 21.2 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2009 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 $cache, $user;
27
28  if (empty($date))
29  {
30    return '';
31  }
32
33  if (isset($cache['get_icon'][$date]))
34  {
35    if (! $cache['get_icon'][$date] )
36      return '';
37    return $cache['get_icon']['_icons_'][$is_child_date];
38  }
39
40  if (!isset($cache['get_icon']['sql_recent_date']))
41  {
42    // Use MySql date in order to standardize all recent "actions/queries"
43    list($cache['get_icon']['sql_recent_date']) =
44      mysql_fetch_array(pwg_query('select SUBDATE(
45      CURRENT_DATE,INTERVAL '.$user['recent_period'].' DAY)'));
46  }
47
48  $cache['get_icon'][$date] = false;
49  if ( $date > $cache['get_icon']['sql_recent_date'] )
50  {
51    if ( !isset($cache['get_icon']['_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        $cache['get_icon']['_icons_'][$key] = $output;
66      }
67    }
68    $cache['get_icon'][$date] = true;
69  }
70
71  if (! $cache['get_icon'][$date] )
72    return '';
73  return $cache['get_icon']['_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    ( ( strpos($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 = '<a href="'.PHPWG_ROOT_PATH.$conf['home_page'].'">'.l10n('no_category').'</a>';
243
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    $output.= $conf['level_separator'];
257
258    if ( !isset($url) )
259    {
260      $output.= $cat['name'];
261    }
262    elseif ($url == '')
263    {
264      $output.= '<a href="'
265            .make_index_url(
266                array(
267                  'category' => $cat,
268                  )
269              )
270            .'">';
271      $output.= $cat['name'].'</a>';
272    }
273    else
274    {
275      $output.= '<a href="'.PHPWG_ROOT_PATH.$url.$cat['id'].'">';
276      $output.= $cat['name'].'</a>';
277    }
278  }
279  if ($replace_space)
280  {
281    return replace_space($output);
282  }
283  else
284  {
285    return $output;
286  }
287}
288
289/**
290 * returns the list of categories as a HTML string, with cache of names
291 *
292 * categories string returned contains categories as given in the input
293 * array $cat_informations. $uppercats is the list of category ids to
294 * display in the right order. If url input parameter is empty, returns only
295 * the categories name without links.
296 *
297 * @param string uppercats
298 * @param string url
299 * @param boolean replace_space
300 * @return string
301 */
302function get_cat_display_name_cache($uppercats,
303                                    $url = '',
304                                    $replace_space = true)
305{
306  global $cache, $conf;
307
308  if (!isset($cache['cat_names']))
309  {
310    $query = '
311SELECT id, name, permalink
312  FROM '.CATEGORIES_TABLE.'
313;';
314    $cache['cat_names'] = hash_from_query($query, 'id');
315  }
316
317  $output = '';
318  $is_first = true;
319  foreach (explode(',', $uppercats) as $category_id)
320  {
321    $cat = $cache['cat_names'][$category_id];
322
323    $cat['name'] = trigger_event(
324      'render_category_name',
325      $cat['name'],
326      'get_cat_display_name_cache'
327      );
328
329    if ($is_first)
330    {
331      $is_first = false;
332    }
333    else
334    {
335      $output.= $conf['level_separator'];
336    }
337
338    if ( !isset($url) )
339    {
340      $output.= $cat['name'];
341    }
342    elseif ($url == '')
343    {
344      $output.= '
345<a href="'
346      .make_index_url(
347          array(
348            'category' => $cat,
349            )
350        )
351      .'">'.$cat['name'].'</a>';
352    }
353    else
354    {
355      $output.= '
356<a href="'.PHPWG_ROOT_PATH.$url.$category_id.'">'.$cat['name'].'</a>';
357    }
358  }
359  if ($replace_space)
360  {
361    return replace_space($output);
362  }
363  else
364  {
365    return $output;
366  }
367}
368
369/**
370 * returns the HTML code for a category item in the menu (for the main page)
371 *
372 * HTML code generated uses logical list tags ul and each category is an
373 * item li. The paramter given is the category informations as an array,
374 * used keys are : id, name, nb_images, max_date_last, date_last
375 * count_images, count_categories
376 *
377 * @param array categories
378 * @return string
379 */
380function get_html_menu_category($categories, $selected_category)
381{
382  $ref_level = 0;
383  $level = 0;
384
385  $menu = trigger_event('get_html_menu_category', '',
386            $categories, $selected_category);
387  if (strlen($menu))
388  {
389    return $menu;
390  }
391
392  foreach ($categories as $category)
393  {
394    $level = substr_count($category['global_rank'], '.') + 1;
395    if ($level > $ref_level)
396    {
397      $menu.= "\n<ul>";
398    }
399    else if ($level == $ref_level)
400    {
401      $menu.= "\n</li>";
402    }
403    else if ($level < $ref_level)
404    {
405      // we may have to close more than one level at the same time...
406      $menu.= "\n</li>";
407      $menu.= str_repeat("\n</ul></li>",($ref_level-$level));
408    }
409    $ref_level = $level;
410
411    $menu.= "\n\n".'<li';
412    if ($category['id'] == @$selected_category['id'])
413    {
414      $menu.= ' class="selected"';
415    }
416    $menu.= '>';
417
418    $url = make_index_url(
419            array(
420              'category' => $category
421              )
422            );
423
424    $title = get_display_images_count
425                (
426                  $category['nb_images'],
427                  $category['count_images'],
428                  $category['count_categories'],
429                  false,
430                  ' / '
431                );
432
433    $menu.= '<a href="'.$url.'"';
434    if ($selected_category!=null
435        and $category['id'] == $selected_category['id_uppercat'])
436    {
437      $menu.= ' rel="up"';
438    }
439    $menu.= ' title="'.$title.'">';
440    $menu.= trigger_event(
441      'render_category_name',
442      $category['name'],
443      'get_html_menu_category'
444      );
445    $menu.= '</a>';
446
447    if ( $category['count_images']>0 )
448    {// at least one direct or indirect image
449      $menu.= "\n".'<span class="';
450      // at least one image in this category -> class menuInfoCat
451      $menu.= ($category['nb_images'] > 0 ? "menuInfoCat"
452                                          : "menuInfoCatByChild").'"';
453      $menu.= ' title=" '.$title.'">';
454      // show total number of images
455      $menu.= '['.$category['count_images'].']';
456      $menu.= '</span>';
457    }
458    $child_date_last = @$category['max_date_last']> @$category['date_last'];
459    $menu.= get_icon($category['max_date_last'], $child_date_last);
460  }
461
462  $menu.= str_repeat("\n</li></ul>",($level));
463
464  return $menu;
465}
466
467/**
468 * returns HTMLized comment contents retrieved from database
469 *
470 * newlines becomes br tags, _word_ becomes underline, /word/ becomes
471 * italic, *word* becomes bolded
472 *
473 * @param string content
474 * @return string
475 */
476function parse_comment_content($content)
477{
478  $pattern = '/(https?:\/\/\S*)/';
479  $replacement = '<a href="$1" rel="nofollow">$1</a>';
480  $content = preg_replace($pattern, $replacement, $content);
481
482  $content = nl2br($content);
483
484  // replace _word_ by an underlined word
485  $pattern = '/\b_(\S*)_\b/';
486  $replacement = '<span style="text-decoration:underline;">$1</span>';
487  $content = preg_replace($pattern, $replacement, $content);
488
489  // replace *word* by a bolded word
490  $pattern = '/\b\*(\S*)\*\b/';
491  $replacement = '<span style="font-weight:bold;">$1</span>';
492  $content = preg_replace($pattern, $replacement, $content);
493
494  // replace /word/ by an italic word
495  $pattern = "/\/(\S*)\/(\s)/";
496  $replacement = '<span style="font-style:italic;">$1$2</span>';
497  $content = preg_replace($pattern, $replacement, $content);
498
499  $content = '<div>'.$content.'</div>';
500  return $content;
501}
502
503function get_cat_display_name_from_id($cat_id,
504                                      $url = '',
505                                      $replace_space = true)
506{
507  $cat_info = get_cat_info($cat_id);
508  return get_cat_display_name($cat_info['upper_names'], $url, $replace_space);
509}
510
511/**
512 * Returns an HTML list of tags. It can be a multi select field or a list of
513 * checkboxes.
514 *
515 * @param string HTML field name
516 * @param array selected tag ids
517 * @return array
518 */
519function get_html_tag_selection(
520  $tags,
521  $fieldname,
522  $selecteds = array(),
523  $forbidden_categories = null
524  )
525{
526  global $conf;
527
528  if (count ($tags) == 0 )
529  {
530    return '';
531  }
532  $output = '<ul class="tagSelection">';
533  foreach ($tags as $tag)
534  {
535    $output.=
536      '<li>'
537      .'<label>'
538      .'<input type="checkbox" name="'.$fieldname.'[]"'
539      .' value="'.$tag['id'].'"'
540      ;
541
542    if (in_array($tag['id'], $selecteds))
543    {
544      $output.= ' checked="checked"';
545    }
546
547    $output.=
548      ' />'
549      .' '. $tag['name']
550      .'</label>'
551      .'</li>'
552      ."\n"
553      ;
554  }
555  $output.= '</ul>';
556
557  return $output;
558}
559
560function name_compare($a, $b)
561{
562  return strcmp(strtolower($a['name']), strtolower($b['name']));
563}
564
565function tag_alpha_compare($a, $b)
566{
567  return strcmp(strtolower($a['url_name']), strtolower($b['url_name']));
568}
569
570/**
571 * exits the current script (either exit or redirect)
572 */
573function access_denied()
574{
575  global $user;
576
577  $login_url =
578      get_root_url().'identification.php?redirect='
579      .urlencode(urlencode($_SERVER['REQUEST_URI']));
580
581  set_status_header(401);
582  if ( isset($user) and !is_a_guest() )
583  {
584    echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8">';
585    echo '<div style="text-align:center;">'.l10n('access_forbiden').'<br />';
586    echo '<a href="'.get_root_url().'identification.php">'.l10n('identification').'</a>&nbsp;';
587    echo '<a href="'.make_index_url().'">'.l10n('home').'</a></div>';
588    echo str_repeat( ' ', 512); //IE6 doesn't error output if below a size
589    exit();
590  }
591  else
592  {
593    redirect_html($login_url);
594  }
595}
596
597/**
598 * exits the current script with 403 code
599 * @param string msg a message to display
600 * @param string alternate_url redirect to this url
601 */
602function page_forbidden($msg, $alternate_url=null)
603{
604  set_status_header(403);
605  if ($alternate_url==null)
606    $alternate_url = make_index_url();
607  redirect_html( $alternate_url,
608    '<div style="text-align:left; margin-left:5em;margin-bottom:5em;">
609<h1 style="text-align:left; font-size:36px;">Forbidden</h1><br/>'
610.$msg.'</div>',
611    5 );
612}
613
614/**
615 * exits the current script with 400 code
616 * @param string msg a message to display
617 * @param string alternate_url redirect to this url
618 */
619function bad_request($msg, $alternate_url=null)
620{
621  set_status_header(400);
622  if ($alternate_url==null)
623    $alternate_url = make_index_url();
624  redirect_html( $alternate_url,
625    '<div style="text-align:left; margin-left:5em;margin-bottom:5em;">
626<h1 style="text-align:left; font-size:36px;">Bad request</h1><br/>'
627.$msg.'</div>',
628    5 );
629}
630
631/**
632 * exits the current script with 404 code when a page cannot be found
633 * @param string msg a message to display
634 * @param string alternate_url redirect to this url
635 */
636function page_not_found($msg, $alternate_url=null)
637{
638  set_status_header(404);
639  if ($alternate_url==null)
640    $alternate_url = make_index_url();
641  redirect_html( $alternate_url,
642    '<div style="text-align:left; margin-left:5em;margin-bottom:5em;">
643<h1 style="text-align:left; font-size:36px;">Page not found</h1><br/>'
644.$msg.'</div>',
645    5 );
646}
647
648/**
649 * exits the current script with 500 http code
650 * this method can be called at any time (does not use template/language/user etc...)
651 * @param string msg a message to display
652 */
653function fatal_error($msg)
654{
655  $btrace_msg = '';
656  if (function_exists('debug_backtrace'))
657  {
658    $bt = debug_backtrace();
659    for ($i=1; $i<count($bt); $i++)
660    {
661      $class = isset($bt[$i]['class']) ? (@$bt[$i]['class'].'::') : '';
662      $btrace_msg .= "#$i\t".$class.@$bt[$i]['function'].' '.@$bt[$i]['file']."(".@$bt[$i]['line'].")\n";
663    }
664    $btrace_msg = trim($btrace_msg);
665    $msg .= "\n";
666  }
667
668  $display = "<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
669<h1>Piwigo encountered a non recoverable error</h1>
670<pre style='font-size:larger;background:white;color:red;padding:1em;margin:0;clear:both;display:block;width:auto;height:auto;overflow:auto'>
671<b>$msg</b>
672$btrace_msg
673</pre>\n";
674
675  @set_status_header(500);
676  echo $display.str_repeat( ' ', 300); //IE6 doesn't error output if below a size
677
678  if ( function_exists('ini_set') )
679  {// if possible turn off error display (we display it)
680    ini_set('display_errors', false);
681  }
682  error_reporting( E_ALL );
683  trigger_error( strip_tags($msg).$btrace_msg, E_USER_ERROR );
684  die(0); // just in case
685}
686
687/* returns the title to be displayed above thumbnails on tag page
688 */
689function get_tags_content_title()
690{
691  global $page;
692  $title = count($page['tags']) > 1 ? l10n('Tags') : l10n('Tag');
693  $title.= ' ';
694
695  for ($i=0; $i<count($page['tags']); $i++)
696  {
697    $title.= $i>0 ? ' + ' : '';
698
699    $title.=
700      '<a href="'
701      .make_index_url(
702        array(
703          'tags' => array( $page['tags'][$i] )
704          )
705        )
706      .'" title="'
707      .l10n('See elements linked to this tag only')
708      .'">'
709      .$page['tags'][$i]['name']
710      .'</a>';
711
712    if ( count($page['tags'])>2 )
713    {
714      $other_tags = $page['tags'];
715      unset ( $other_tags[$i] );
716      $title.=
717        '<a href="'
718        .make_index_url(
719          array(
720            'tags' => $other_tags
721            )
722          )
723        .'" style="border:none;" title="'
724        .l10n('remove this tag')
725        .'"><img src="'
726        .get_root_url().get_themeconf('icon_dir').'/remove_s.png'
727        .'" alt="x" style="vertical-align:bottom;" class="button"/>'
728        .'</a>';
729    }
730
731  }
732  return $title;
733}
734
735/**
736  Sets the http status header (200,401,...)
737 */
738function set_status_header($code, $text='')
739{
740  if (empty($text))
741  {
742    switch ($code)
743    {
744      case 200: $text='OK';break;
745      case 301: $text='Moved permanently';break;
746      case 302: $text='Moved temporarily';break;
747      case 304: $text='Not modified';break;
748      case 400: $text='Bad request';break;
749      case 401: $text='Authorization required';break;
750      case 403: $text='Forbidden';break;
751      case 404: $text='Not found';break;
752      case 500: $text='Server error';break;
753      case 501: $text='Not implemented';break;
754      case 503: $text='Service unavailable';break;
755    }
756  }
757        $protocol = $_SERVER["SERVER_PROTOCOL"];
758        if ( ('HTTP/1.1' != $protocol) && ('HTTP/1.0' != $protocol) )
759                $protocol = 'HTTP/1.0';
760
761        if ( version_compare( phpversion(), '4.3.0', '>=' ) )
762  {
763                header( "$protocol $code $text", true, $code );
764        }
765  else
766  {
767                header( "$protocol $code $text" );
768        }
769  trigger_action('set_status_header', $code, $text);
770}
771
772/** returns the category comment for rendering in html textual mode (subcatify)
773 * this is an event handler. don't call directly
774 */
775function render_category_literal_description($desc)
776{
777  return strip_tags($desc, '<span><p><a><br><b><i><small><big><strong><em>');
778}
779
780/** returns the argument_ids array with new sequenced keys based on related
781 * names. Sequence is not case sensitive.
782 * Warning: By definition, this function breaks original keys
783 */
784function order_by_name($element_ids,$name)
785{
786  $ordered_element_ids = array();
787  foreach ($element_ids as $k_id => $element_id)
788  {
789    $key = strtolower($name[$element_id]) .'-'. $name[$element_id] .'-'. $k_id;
790    $ordered_element_ids[$key] = $element_id;
791  }
792  ksort($ordered_element_ids);
793  return $ordered_element_ids;
794}
795
796/*event handler for menu*/
797function register_default_menubar_blocks( $menu_ref_arr )
798{
799  $menu = & $menu_ref_arr[0];
800  if ($menu->get_id() != 'menubar')
801    return;
802  $menu->register_block( new RegisteredBlock( 'mbLinks', 'Links', 'piwigo'));
803  $menu->register_block( new RegisteredBlock( 'mbCategories', 'Categories', 'piwigo'));
804  $menu->register_block( new RegisteredBlock( 'mbTags', 'Related tags', 'piwigo'));
805  $menu->register_block( new RegisteredBlock( 'mbSpecials', 'special_categories', 'piwigo'));
806  $menu->register_block( new RegisteredBlock( 'mbMenu', 'title_menu', 'piwigo'));
807  $menu->register_block( new RegisteredBlock( 'mbIdentification', 'identification', 'piwigo') );
808}
809
810?>
Note: See TracBrowser for help on using the repository browser.