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

Last change on this file since 2182 was 2182, checked in by rub, 16 years ago

Resolved issue 0000791: recent icon is wrong with winter time

Merge branch-1_7 2180:2181 into BSF

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 18.8 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 2182 2008-01-14 21:08:26Z rub $
8// | last update   : $Date: 2008-01-14 21:08:26 +0000 (Mon, 14 Jan 2008) $
9// | last modifier : $Author: rub $
10// | revision      : $Revision: 2182 $
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 = $lang['recent_image'].'&nbsp;'.$user['recent_period']
58          .'&nbsp;'.$lang['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="start">'
109        .$lang['first_page']
110        .'</a>';
111    }
112    else
113    {
114      $navbar.= $lang['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        .$lang['previous_page']
127        .'</a>';
128    }
129    else
130    {
131      $navbar.= $lang['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        .$lang['next_page']
198        .'</a>';
199    }
200    else
201    {
202      $navbar.= $lang['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        .$lang['last_page']
214        .'</a>';
215    }
216    else
217    {
218      $navbar.= $lang['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    if ($is_first)
251    {
252      $is_first = false;
253    }
254    else
255    {
256      $output.= $conf['level_separator'];
257    }
258
259    if ( !isset($url) )
260    {
261      $output.= $cat['name'];
262    }
263    elseif ($url == '')
264    {
265      $output.= '<a href="'
266            .make_index_url(
267                array(
268                  'category' => $cat,
269                  )
270              )
271            .'">';
272      $output.= $cat['name'].'</a>';
273    }
274    else
275    {
276      $output.= '<a href="'.PHPWG_ROOT_PATH.$url.$cat['id'].'">';
277      $output.= $cat['name'].'</a>';
278    }
279  }
280  if ($replace_space)
281  {
282    return replace_space($output);
283  }
284  else
285  {
286    return $output;
287  }
288}
289
290/**
291 * returns the list of categories as a HTML string, with cache of names
292 *
293 * categories string returned contains categories as given in the input
294 * array $cat_informations. $uppercats is the list of category ids to
295 * display in the right order. If url input parameter is empty, returns only
296 * the categories name without links.
297 *
298 * @param string uppercats
299 * @param string url
300 * @param boolean replace_space
301 * @return string
302 */
303function get_cat_display_name_cache($uppercats,
304                                    $url = '',
305                                    $replace_space = true)
306{
307  global $cache, $conf;
308
309  if (!isset($cache['cat_names']))
310  {
311    $query = '
312SELECT id, name, permalink
313  FROM '.CATEGORIES_TABLE.'
314;';
315    $cache['cat_names'] = hash_from_query($query, 'id');
316  }
317
318  $output = '';
319  $is_first = true;
320  foreach (explode(',', $uppercats) as $category_id)
321  {
322    $cat = $cache['cat_names'][$category_id];
323
324    if ($is_first)
325    {
326      $is_first = false;
327    }
328    else
329    {
330      $output.= $conf['level_separator'];
331    }
332
333    if ( !isset($url) )
334    {
335      $output.= $cat['name'];
336    }
337    elseif ($url == '')
338    {
339      $output.= '
340<a href="'
341      .make_index_url(
342          array(
343            'category' => $cat,
344            )
345        )
346      .'">'.$cat['name'].'</a>';
347    }
348    else
349    {
350      $output.= '
351<a href="'.PHPWG_ROOT_PATH.$url.$category_id.'">'.$cat['name'].'</a>';
352    }
353  }
354  if ($replace_space)
355  {
356    return replace_space($output);
357  }
358  else
359  {
360    return $output;
361  }
362}
363
364/**
365 * returns the HTML code for a category item in the menu (for the main page)
366 *
367 * HTML code generated uses logical list tags ul and each category is an
368 * item li. The paramter given is the category informations as an array,
369 * used keys are : id, name, nb_images, max_date_last, date_last
370 * count_images, count_categories
371 *
372 * @param array categories
373 * @return string
374 */
375function get_html_menu_category($categories, $selected_category)
376{
377  global $lang;
378
379  $ref_level = 0;
380  $level = 0;
381
382  $menu = trigger_event('get_html_menu_category', '',
383            $categories, $selected_category);
384  if (strlen($menu))
385  {
386    return $menu;
387  }
388
389  foreach ($categories as $category)
390  {
391    $level = substr_count($category['global_rank'], '.') + 1;
392    if ($level > $ref_level)
393    {
394      $menu.= "\n<ul>";
395    }
396    else if ($level == $ref_level)
397    {
398      $menu.= "\n</li>";
399    }
400    else if ($level < $ref_level)
401    {
402      // we may have to close more than one level at the same time...
403      $menu.= "\n</li>";
404      $menu.= str_repeat("\n</ul></li>",($ref_level-$level));
405    }
406    $ref_level = $level;
407
408    $menu.= "\n\n".'<li';
409    if ($category['id'] == @$selected_category['id'])
410    {
411      $menu.= ' class="selected"';
412    }
413    $menu.= '>';
414
415    $url = make_index_url(
416            array(
417              'category' => $category
418              )
419            );
420
421    $title = get_display_images_count
422                (
423                  $category['nb_images'],
424                  $category['count_images'],
425                  $category['count_categories'],
426                  false,
427                  ' / '
428                );
429
430    $menu.= '<a href="'.$url.'"';
431    if ($selected_category!=null
432        and $category['id'] == $selected_category['id_uppercat'])
433    {
434      $menu.= ' rel="up"';
435    }
436    $menu.= ' title="'.$title.'">'.$category['name'].'</a>';
437
438    if ( $category['count_images']>0 )
439    {// at least one direct or indirect image
440      $menu.= "\n".'<span class="';
441      // at least one image in this category -> class menuInfoCat
442      $menu.= ($category['nb_images'] > 0 ? "menuInfoCat"
443                                          : "menuInfoCatByChild").'"';
444      $menu.= ' title=" '.$title.'">';
445      // show total number of images
446      $menu.= '['.$category['count_images'].']';
447      $menu.= '</span>';
448    }
449    $child_date_last = @$category['max_date_last']> @$category['date_last'];
450    $menu.= get_icon($category['max_date_last'], $child_date_last);
451  }
452
453  $menu.= str_repeat("\n</li></ul>",($level));
454
455  return $menu;
456}
457
458/**
459 * returns HTMLized comment contents retrieved from database
460 *
461 * newlines becomes br tags, _word_ becomes underline, /word/ becomes
462 * italic, *word* becomes bolded
463 *
464 * @param string content
465 * @return string
466 */
467function parse_comment_content($content)
468{
469  $pattern = '/(https?:\/\/\S*)/';
470  $replacement = '<a href="$1" rel="nofollow">$1</a>';
471  $content = preg_replace($pattern, $replacement, $content);
472
473  $content = nl2br($content);
474
475  // replace _word_ by an underlined word
476  $pattern = '/\b_(\S*)_\b/';
477  $replacement = '<span style="text-decoration:underline;">$1</span>';
478  $content = preg_replace($pattern, $replacement, $content);
479
480  // replace *word* by a bolded word
481  $pattern = '/\b\*(\S*)\*\b/';
482  $replacement = '<span style="font-weight:bold;">$1</span>';
483  $content = preg_replace($pattern, $replacement, $content);
484
485  // replace /word/ by an italic word
486  $pattern = "/\/(\S*)\/(\s)/";
487  $replacement = '<span style="font-style:italic;">$1$2</span>';
488  $content = preg_replace($pattern, $replacement, $content);
489
490  $content = '<div>'.$content.'</div>';
491  return $content;
492}
493
494function get_cat_display_name_from_id($cat_id,
495                                      $url = '',
496                                      $replace_space = true)
497{
498  $cat_info = get_cat_info($cat_id);
499  return get_cat_display_name($cat_info['upper_names'], $url, $replace_space);
500}
501
502/**
503 * Returns an HTML list of tags. It can be a multi select field or a list of
504 * checkboxes.
505 *
506 * @param string HTML field name
507 * @param array selected tag ids
508 * @return array
509 */
510function get_html_tag_selection(
511  $tags,
512  $fieldname,
513  $selecteds = array(),
514  $forbidden_categories = null
515  )
516{
517  global $conf;
518
519  if (count ($tags) == 0 )
520  {
521    return '';
522  }
523  $output = '<ul class="tagSelection">';
524  foreach ($tags as $tag)
525  {
526    $output.=
527      '<li>'
528      .'<label>'
529      .'<input type="checkbox" name="'.$fieldname.'[]"'
530      .' value="'.$tag['id'].'"'
531      ;
532
533    if (in_array($tag['id'], $selecteds))
534    {
535      $output.= ' checked="checked"';
536    }
537
538    $output.=
539      ' />'
540      .' '. $tag['name']
541      .'</label>'
542      .'</li>'
543      ."\n"
544      ;
545  }
546  $output.= '</ul>';
547
548  return $output;
549}
550
551function name_compare($a, $b)
552{
553  return strcmp(strtolower($a['name']), strtolower($b['name']));
554}
555
556/**
557 * exits the current script (either exit or redirect)
558 */
559function access_denied()
560{
561  global $user, $lang;
562
563  $login_url =
564      get_root_url().'identification.php?redirect='
565      .urlencode(urlencode($_SERVER['REQUEST_URI']));
566
567  if ( isset($user) and !is_a_guest() )
568  {
569    echo '<div style="text-align:center;">'.$lang['access_forbiden'].'<br />';
570    echo '<a href="'.get_root_url().'identification.php">'.$lang['identification'].'</a>&nbsp;';
571    echo '<a href="'.make_index_url().'">'.$lang['home'].'</a></div>';
572    exit();
573  }
574  else
575  {
576    set_status_header(401);
577    redirect_html($login_url);
578  }
579}
580
581/**
582 * exits the current script with 403 code
583 * @param string msg a message to display
584 * @param string alternate_url redirect to this url
585 */
586function page_forbidden($msg, $alternate_url=null)
587{
588  set_status_header(403);
589  if ($alternate_url==null)
590    $alternate_url = make_index_url();
591  redirect_html( $alternate_url,
592    '<div style="text-align:left; margin-left:5em;margin-bottom:5em;">
593<h1 style="text-align:left; font-size:36px;">Forbidden</h1><br/>'
594.$msg.'</div>',
595    5 );
596}
597
598/**
599 * exits the current script with 400 code
600 * @param string msg a message to display
601 * @param string alternate_url redirect to this url
602 */
603function bad_request($msg, $alternate_url=null)
604{
605  set_status_header(400);
606  if ($alternate_url==null)
607    $alternate_url = make_index_url();
608  redirect_html( $alternate_url,
609    '<div style="text-align:left; margin-left:5em;margin-bottom:5em;">
610<h1 style="text-align:left; font-size:36px;">Bad request</h1><br/>'
611.$msg.'</div>',
612    5 );
613}
614
615/**
616 * exits the current script with 404 code when a page cannot be found
617 * @param string msg a message to display
618 * @param string alternate_url redirect to this url
619 */
620function page_not_found($msg, $alternate_url=null)
621{
622  set_status_header(404);
623  if ($alternate_url==null)
624    $alternate_url = make_index_url();
625  redirect_html( $alternate_url,
626    '<div style="text-align:left; margin-left:5em;margin-bottom:5em;">
627<h1 style="text-align:left; font-size:36px;">Page not found</h1><br/>'
628.$msg.'</div>',
629    5 );
630}
631
632/* returns the title to be displayed above thumbnails on tag page
633 */
634function get_tags_content_title()
635{
636  global $page;
637  $title = count($page['tags']) > 1 ? l10n('Tags') : l10n('Tag');
638  $title.= ' ';
639
640  for ($i=0; $i<count($page['tags']); $i++)
641  {
642    $title.= $i>0 ? ' + ' : '';
643
644    $title.=
645      '<a href="'
646      .make_index_url(
647        array(
648          'tags' => array( $page['tags'][$i] )
649          )
650        )
651      .'" title="'
652      .l10n('See pictures linked to this tag only')
653      .'">'
654      .$page['tags'][$i]['name']
655      .'</a>';
656
657    if ( count($page['tags'])>2 )
658    {
659      $other_tags = $page['tags'];
660      unset ( $other_tags[$i] );
661      $title.=
662        '<a href="'
663        .make_index_url(
664          array(
665            'tags' => $other_tags
666            )
667          )
668        .'" style="border:none;" title="'
669        .l10n('remove this tag')
670        .'"><img src="'
671        .get_root_url().get_themeconf('icon_dir').'/remove_s.png'
672        .'" alt="x" style="vertical-align:bottom;" class="button"/>'
673        .'</a>';
674    }
675
676  }
677  return $title;
678}
679
680/**
681  Sets the http status header (200,401,...)
682 */
683function set_status_header($code, $text='')
684{
685  if (empty($text))
686  {
687    switch ($code)
688    {
689      case 200: $text='OK';break;
690      case 301: $text='Moved permanently';break;
691      case 302: $text='Moved temporarily';break;
692      case 304: $text='Not modified';break;
693      case 400: $text='Bad request';break;
694      case 401: $text='Authorization required';break;
695      case 403: $text='Forbidden';break;
696      case 404: $text='Not found';break;
697      case 500: $text='Server error';break;
698      case 503: $text='Service unavailable';break;
699    }
700  }
701        $protocol = $_SERVER["SERVER_PROTOCOL"];
702        if ( ('HTTP/1.1' != $protocol) && ('HTTP/1.0' != $protocol) )
703                $protocol = 'HTTP/1.0';
704
705        if ( version_compare( phpversion(), '4.3.0', '>=' ) )
706  {
707                header( "$protocol $code $text", true, $code );
708        }
709  else
710  {
711                header( "$protocol $code $text" );
712        }
713  trigger_action('set_status_header', $code, $text);
714}
715
716/**
717 * set a class to display a counter
718 * .zero .one .plural
719 */
720function set_span_class($count)
721{
722  if ($count > 1)
723  {
724    return 'plural';
725  }
726  return ( $count == 0 ) ? 'zero':'one';
727}
728
729/** returns the category comment for rendering in html.
730 * this is an event handler. don't call directly
731 */
732function render_category_description($desc)
733{
734  global $conf;
735  if ( !( $conf['allow_html_descriptions'] and
736          preg_match('/<(div|br|img|script).*>/i', $desc) ) )
737  {
738    $desc = nl2br($desc);
739  }
740  return $desc;
741}
742
743/** returns the category comment for rendering in html textual mode (subcatify)
744 * this is an event handler. don't call directly
745 */
746function render_category_literal_description($desc)
747{
748  return strip_tags($desc, '<span><p><a><br><b><i><small><big><strong><em>');
749}
750?>
Note: See TracBrowser for help on using the repository browser.