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

Last change on this file since 2117 was 2117, checked in by rvelices, 17 years ago
  • render_category_description and render_category_literal_description events refactoring
  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 19.3 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | PhpWebGallery - a PHP based picture gallery                           |
4// | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net |
5// | Copyright (C) 2003-2007 PhpWebGallery Team - http://phpwebgallery.net |
6// +-----------------------------------------------------------------------+
7// | file          : $Id: functions_html.inc.php 2117 2007-10-02 05:38:54Z rvelices $
8// | last update   : $Date: 2007-10-02 05:38:54 +0000 (Tue, 02 Oct 2007) $
9// | last modifier : $Author: rvelices $
10// | revision      : $Revision: 2117 $
11// +-----------------------------------------------------------------------+
12// | This program is free software; you can redistribute it and/or modify  |
13// | it under the terms of the GNU General Public License as published by  |
14// | the Free Software Foundation                                          |
15// |                                                                       |
16// | This program is distributed in the hope that it will be useful, but   |
17// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
18// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
19// | General Public License for more details.                              |
20// |                                                                       |
21// | You should have received a copy of the GNU General Public License     |
22// | along with this program; if not, write to the Free Software           |
23// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
24// | USA.                                                                  |
25// +-----------------------------------------------------------------------+
26
27function get_icon($date, $is_child_date = false)
28{
29  global $page, $user, $lang;
30
31  if (empty($date))
32  {
33    return '';
34  }
35
36  if (isset($page['get_icon_cache'][$date]))
37  {
38    if (! $page['get_icon_cache'][$date] )
39      return '';
40    return $page['get_icon_cache']['_icons_'][$is_child_date];
41  }
42
43  if (!preg_match('/^(\d{4})-(\d{2})-(\d{2})/', $date, $matches))
44  {// date can be empty, no icon to display
45    $page['get_icon_cache'][$date] = false;
46    return '';
47  }
48
49  list($devnull, $year, $month, $day) = $matches;
50  $unixtime = mktime( 0, 0, 0, $month, $day, $year );
51  if ($unixtime === false  // PHP 5.1.0 and above
52      or $unixtime === -1) // PHP prior to 5.1.0
53  {
54    $page['get_icon_cache'][$date] = false;
55    return '';
56  }
57
58  if (!isset($page['get_icon_cache']['unix_timestamp']))
59  {
60    // Use MySql date in order to standardize all recent "actions/queries"
61    list($page['get_icon_cache']['unix_timestamp']) =
62      mysql_fetch_array(pwg_query('select UNIX_TIMESTAMP(CURRENT_DATE)'));
63  }
64
65  $diff = $page['get_icon_cache']['unix_timestamp'] - $unixtime;
66  $day_in_seconds = 24*60*60;
67  $page['get_icon_cache'][$date] = false;
68  if ( $diff <= $user['recent_period'] * $day_in_seconds )
69  {
70    if ( !isset($page['get_icon_cache']['_icons_'] ) )
71    {
72      $icons = array(false => 'recent', true => 'recent_by_child' );
73      $title = $lang['recent_image'].'&nbsp;'.$user['recent_period']
74          .'&nbsp;'.$lang['days'];
75      foreach ($icons as $key => $icon)
76      {
77        $icon_url = get_themeconf('icon_dir').'/'.$icon.'.png';
78        $size = getimagesize( PHPWG_ROOT_PATH.$icon_url );
79        $icon_url = get_root_url().$icon_url;
80        $output = '<img title="'.$title.'" src="'.$icon_url.'" class="icon" style="border:0;';
81        $output.= 'height:'.$size[1].'px;width:'.$size[0].'px" alt="(!)" />';
82        $page['get_icon_cache']['_icons_'][$key] = $output;
83      }
84    }
85    $page['get_icon_cache'][$date] = true;
86  }
87  if (! $page['get_icon_cache'][$date] )
88    return '';
89  return $page['get_icon_cache']['_icons_'][$is_child_date];
90}
91
92
93function create_navigation_bar(
94  $url, $nb_element, $start, $nb_element_page, $clean_url = false
95  )
96{
97  global $lang, $conf;
98
99  $pages_around = $conf['paginate_pages_around'];
100  $start_str = $clean_url ? '/start-' :
101    ( ( strstr($url, '?')===false ? '?':'&amp;') . 'start=' );
102
103  $navbar = '';
104
105  // current page detection
106  if (!isset($start)
107      or !is_numeric($start)
108      or (is_numeric($start) and $start < 0))
109  {
110    $start = 0;
111  }
112
113  // navigation bar useful only if more than one page to display !
114  if ($nb_element > $nb_element_page)
115  {
116    // current page and last page
117    $cur_page = ceil($start / $nb_element_page) + 1;
118    $maximum = ceil($nb_element / $nb_element_page);
119
120    // link to first page ?
121    if ($cur_page != 1)
122    {
123      $navbar.=
124        '<a href="'.$url.'" rel="start">'
125        .$lang['first_page']
126        .'</a>';
127    }
128    else
129    {
130      $navbar.= $lang['first_page'];
131    }
132    $navbar.= ' | ';
133    // link on previous page ?
134    if ($start != 0)
135    {
136      $previous = $start - $nb_element_page;
137
138      $navbar.=
139        '<a href="'
140        .$url.($previous > 0 ? $start_str.$previous : '')
141        .'" rel="prev">'
142        .$lang['previous_page']
143        .'</a>';
144    }
145    else
146    {
147      $navbar.= $lang['previous_page'];
148    }
149    $navbar.= ' |';
150
151    if ($cur_page > $pages_around + 1)
152    {
153      $navbar.= '&nbsp;<a href="'.$url.'">1</a>';
154
155      if ($cur_page > $pages_around + 2)
156      {
157        $navbar.= ' ...';
158      }
159    }
160
161    // inspired from punbb source code
162    for ($i = $cur_page - $pages_around, $stop = $cur_page + $pages_around + 1;
163         $i < $stop;
164         $i++)
165    {
166      if ($i < 1 or $i > $maximum)
167      {
168        continue;
169      }
170      else if ($i != $cur_page)
171      {
172        $temp_start = ($i - 1) * $nb_element_page;
173
174        $navbar.=
175          '&nbsp;'
176          .'<a href="'.$url
177          .($temp_start > 0 ? $start_str.$temp_start : '')
178          .'">'
179          .$i
180          .'</a>';
181      }
182      else
183      {
184        $navbar.=
185          '&nbsp;'
186          .'<span class="pageNumberSelected">'
187          .$i
188          .'</span>';
189      }
190    }
191
192    if ($cur_page < ($maximum - $pages_around))
193    {
194      $temp_start = ($maximum - 1) * $nb_element_page;
195
196      if ($cur_page < ($maximum - $pages_around - 1))
197      {
198        $navbar.= ' ...';
199      }
200
201      $navbar.= ' <a href="'.$url.$start_str.$temp_start.'">'.$maximum.'</a>';
202    }
203
204    $navbar.= ' | ';
205    // link on next page ?
206    if ($nb_element > $nb_element_page
207        and $start + $nb_element_page < $nb_element)
208    {
209      $next = $start + $nb_element_page;
210
211      $navbar.=
212        '<a href="'.$url.$start_str.$next.'" rel="next">'
213        .$lang['next_page']
214        .'</a>';
215    }
216    else
217    {
218      $navbar.= $lang['next_page'];
219    }
220
221    $navbar.= ' | ';
222    // link to last page ?
223    if ($cur_page != $maximum)
224    {
225      $temp_start = ($maximum - 1) * $nb_element_page;
226
227      $navbar.=
228        '<a href="'.$url.$start_str.$temp_start.'" rel="last">'
229        .$lang['last_page']
230        .'</a>';
231    }
232    else
233    {
234      $navbar.= $lang['last_page'];
235    }
236  }
237  return $navbar;
238}
239
240/**
241 * returns the list of categories as a HTML string
242 *
243 * categories string returned contains categories as given in the input
244 * array $cat_informations. $cat_informations array must be an array
245 * of array( id=>?, name=>?, permalink=>?). If url input parameter is null,
246 * returns only the categories name without links.
247 *
248 * @param array cat_informations
249 * @param string url
250 * @param boolean replace_space
251 * @return string
252 */
253function get_cat_display_name($cat_informations,
254                              $url = '',
255                              $replace_space = true)
256{
257  global $conf;
258
259  $output = '';
260  $is_first = true;
261  foreach ($cat_informations as $cat)
262  {
263    is_array($cat) or trigger_error(
264        'get_cat_display_name wrong type for category ', E_USER_WARNING
265      );
266    if ($is_first)
267    {
268      $is_first = false;
269    }
270    else
271    {
272      $output.= $conf['level_separator'];
273    }
274
275    if ( !isset($url) )
276    {
277      $output.= $cat['name'];
278    }
279    elseif ($url == '')
280    {
281      $output.= '<a href="'
282            .make_index_url(
283                array(
284                  'category' => $cat,
285                  )
286              )
287            .'">';
288      $output.= $cat['name'].'</a>';
289    }
290    else
291    {
292      $output.= '<a href="'.PHPWG_ROOT_PATH.$url.$cat['id'].'">';
293      $output.= $cat['name'].'</a>';
294    }
295  }
296  if ($replace_space)
297  {
298    return replace_space($output);
299  }
300  else
301  {
302    return $output;
303  }
304}
305
306/**
307 * returns the list of categories as a HTML string, with cache of names
308 *
309 * categories string returned contains categories as given in the input
310 * array $cat_informations. $uppercats is the list of category ids to
311 * display in the right order. If url input parameter is empty, returns only
312 * the categories name without links.
313 *
314 * @param string uppercats
315 * @param string url
316 * @param boolean replace_space
317 * @return string
318 */
319function get_cat_display_name_cache($uppercats,
320                                    $url = '',
321                                    $replace_space = true)
322{
323  global $cache, $conf;
324
325  if (!isset($cache['cat_names']))
326  {
327    $query = '
328SELECT id, name, permalink
329  FROM '.CATEGORIES_TABLE.'
330;';
331    $cache['cat_names'] = hash_from_query($query, 'id');
332  }
333
334  $output = '';
335  $is_first = true;
336  foreach (explode(',', $uppercats) as $category_id)
337  {
338    $cat = $cache['cat_names'][$category_id];
339
340    if ($is_first)
341    {
342      $is_first = false;
343    }
344    else
345    {
346      $output.= $conf['level_separator'];
347    }
348
349    if ( !isset($url) )
350    {
351      $output.= $cat['name'];
352    }
353    elseif ($url == '')
354    {
355      $output.= '
356<a href="'
357      .make_index_url(
358          array(
359            'category' => $cat,
360            )
361        )
362      .'">'.$cat['name'].'</a>';
363    }
364    else
365    {
366      $output.= '
367<a href="'.PHPWG_ROOT_PATH.$url.$category_id.'">'.$cat['name'].'</a>';
368    }
369  }
370  if ($replace_space)
371  {
372    return replace_space($output);
373  }
374  else
375  {
376    return $output;
377  }
378}
379
380/**
381 * returns the HTML code for a category item in the menu (for the main page)
382 *
383 * HTML code generated uses logical list tags ul and each category is an
384 * item li. The paramter given is the category informations as an array,
385 * used keys are : id, name, nb_images, max_date_last, date_last
386 * count_images, count_categories
387 *
388 * @param array categories
389 * @return string
390 */
391function get_html_menu_category($categories, $selected_category)
392{
393  global $lang;
394
395  $ref_level = 0;
396  $level = 0;
397
398  $menu = trigger_event('get_html_menu_category', '',
399            $categories, $selected_category);
400  if (strlen($menu))
401  {
402    return $menu;
403  }
404
405  foreach ($categories as $category)
406  {
407    $level = substr_count($category['global_rank'], '.') + 1;
408    if ($level > $ref_level)
409    {
410      $menu.= "\n<ul>";
411    }
412    else if ($level == $ref_level)
413    {
414      $menu.= "\n</li>";
415    }
416    else if ($level < $ref_level)
417    {
418      // we may have to close more than one level at the same time...
419      $menu.= "\n</li>";
420      $menu.= str_repeat("\n</ul></li>",($ref_level-$level));
421    }
422    $ref_level = $level;
423
424    $menu.= "\n\n".'<li';
425    if ($category['id'] == @$selected_category['id'])
426    {
427      $menu.= ' class="selected"';
428    }
429    $menu.= '>';
430
431    $url = make_index_url(
432            array(
433              'category' => $category
434              )
435            );
436
437    $title = get_display_images_count
438                (
439                  $category['nb_images'],
440                  $category['count_images'],
441                  $category['count_categories'],
442                  false,
443                  ' / '
444                );
445
446    $menu.= '<a href="'.$url.'"';
447    if ($selected_category!=null
448        and $category['id'] == $selected_category['id_uppercat'])
449    {
450      $menu.= ' rel="up"';
451    }
452    $menu.= ' title="'.$title.'">'.$category['name'].'</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
572/**
573 * exits the current script (either exit or redirect)
574 */
575function access_denied()
576{
577  global $user, $lang;
578
579  $login_url =
580      get_root_url().'identification.php?redirect='
581      .urlencode(urlencode($_SERVER['REQUEST_URI']));
582
583  if ( isset($user) and !is_a_guest() )
584  {
585    echo '<div style="text-align:center;">'.$lang['access_forbiden'].'<br />';
586    echo '<a href="'.get_root_url().'identification.php">'.$lang['identification'].'</a>&nbsp;';
587    echo '<a href="'.make_index_url().'">'.$lang['home'].'</a></div>';
588    exit();
589  }
590  else
591  {
592    set_status_header(401);
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/* returns the title to be displayed above thumbnails on tag page
649 */
650function get_tags_content_title()
651{
652  global $page;
653  $title = count($page['tags']) > 1 ? l10n('Tags') : l10n('Tag');
654  $title.= ' ';
655
656  for ($i=0; $i<count($page['tags']); $i++)
657  {
658    $title.= $i>0 ? ' + ' : '';
659
660    $title.=
661      '<a href="'
662      .make_index_url(
663        array(
664          'tags' => array( $page['tags'][$i] )
665          )
666        )
667      .'" title="'
668      .l10n('See pictures linked to this tag only')
669      .'">'
670      .$page['tags'][$i]['name']
671      .'</a>';
672
673    if ( count($page['tags'])>2 )
674    {
675      $other_tags = $page['tags'];
676      unset ( $other_tags[$i] );
677      $title.=
678        '<a href="'
679        .make_index_url(
680          array(
681            'tags' => $other_tags
682            )
683          )
684        .'" style="border:none;" title="'
685        .l10n('remove this tag')
686        .'"><img src="'
687        .get_root_url().get_themeconf('icon_dir').'/remove_s.png'
688        .'" alt="x" style="vertical-align:bottom;" class="button"/>'
689        .'</a>';
690    }
691
692  }
693  return $title;
694}
695
696/**
697  Sets the http status header (200,401,...)
698 */
699function set_status_header($code, $text='')
700{
701  if (empty($text))
702  {
703    switch ($code)
704    {
705      case 200: $text='OK';break;
706      case 301: $text='Moved permanently';break;
707      case 302: $text='Moved temporarily';break;
708      case 304: $text='Not modified';break;
709      case 400: $text='Bad request';break;
710      case 401: $text='Authorization required';break;
711      case 403: $text='Forbidden';break;
712      case 404: $text='Not found';break;
713      case 500: $text='Server error';break;
714      case 503: $text='Service unavailable';break;
715    }
716  }
717        $protocol = $_SERVER["SERVER_PROTOCOL"];
718        if ( ('HTTP/1.1' != $protocol) && ('HTTP/1.0' != $protocol) )
719                $protocol = 'HTTP/1.0';
720
721        if ( version_compare( phpversion(), '4.3.0', '>=' ) )
722  {
723                header( "$protocol $code $text", true, $code );
724        }
725  else
726  {
727                header( "$protocol $code $text" );
728        }
729  trigger_action('set_status_header', $code, $text);
730}
731
732/**
733 * set a class to display a counter
734 * .zero .one .plural
735 */
736function set_span_class($count)
737{
738  if ($count > 1)
739  {
740    return 'plural';
741  }
742  return ( $count == 0 ) ? 'zero':'one';
743}
744
745/** returns the category comment for rendering in html.
746 * this is an event handler. don't call directly
747 */
748function render_category_description($desc)
749{
750  global $conf;
751  if ( !( $conf['allow_html_descriptions'] and
752          preg_match('/<(div|br|img|script).*>/i', $desc) ) )
753  {
754    $desc = nl2br($desc);
755  }
756  return $desc;
757}
758
759/** returns the category comment for rendering in html textual mode (subcatify)
760 * this is an event handler. don't call directly
761 */
762function render_category_literal_description($desc)
763{
764  return strip_tags($desc, '<span><p><a><br><b><i><small><big><strong><em>');
765}
766?>
Note: See TracBrowser for help on using the repository browser.