source: tags/release-1_7_2/include/functions_html.inc.php @ 2458

Last change on this file since 2458 was 2432, checked in by patdenice, 16 years ago

Add triggers for category name (render_category_name).
Add strip_tags for ALT attribute on category thumbnail.

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