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

Last change on this file since 1884 was 1884, checked in by rub, 17 years ago

Add DateTime on administration introduction page (Useful to help user on the forum, ...)
Move history configuration in a other tab.
Use translation on picture hint (Kb).
Show hint oh the menu text (not only on counter)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 18.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 1884 2007-03-09 22:52:22Z rub $
8// | last update   : $Date: 2007-03-09 22:52:22 +0000 (Fri, 09 Mar 2007) $
9// | last modifier : $Author: rub $
10// | revision      : $Revision: 1884 $
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  $menu = '';
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.= "\n".'<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.'">'.$category['name'].'</a>';
447
448    if ( $category['count_images']>0 )
449    {// at least one direct or indirect image
450      $menu.= "\n".'<span class="';
451      // at least one image in this category -> class menuInfoCat
452      $menu.= ($category['nb_images'] > 0 ? "menuInfoCat" 
453                                          : "menuInfoCatByChild").'"';
454      $menu.= ' title=" '.$title.'">';
455      // show total number of images
456      $menu.= '['.$category['count_images'].']';
457      $menu.= '</span>';
458    }
459    $child_date_last = @$category['max_date_last']> @$category['date_last'];
460    $menu.= get_icon($category['max_date_last'], $child_date_last);
461  }
462
463  $menu.= str_repeat("\n</li></ul>",($level));
464
465  return $menu;
466}
467
468/**
469 * returns HTMLized comment contents retrieved from database
470 *
471 * newlines becomes br tags, _word_ becomes underline, /word/ becomes
472 * italic, *word* becomes bolded
473 *
474 * @param string content
475 * @return string
476 */
477function parse_comment_content($content)
478{
479  $pattern = '/(https?:\/\/\S*)/';
480  $replacement = '<a href="$1" rel="nofollow">$1</a>';
481  $content = preg_replace($pattern, $replacement, $content);
482
483  $content = nl2br($content);
484
485  // replace _word_ by an underlined word
486  $pattern = '/\b_(\S*)_\b/';
487  $replacement = '<span style="text-decoration:underline;">$1</span>';
488  $content = preg_replace($pattern, $replacement, $content);
489
490  // replace *word* by a bolded word
491  $pattern = '/\b\*(\S*)\*\b/';
492  $replacement = '<span style="font-weight:bold;">$1</span>';
493  $content = preg_replace($pattern, $replacement, $content);
494
495  // replace /word/ by an italic word
496  $pattern = "/\/(\S*)\/(\s)/";
497  $replacement = '<span style="font-style:italic;">$1$2</span>';
498  $content = preg_replace($pattern, $replacement, $content);
499
500  $content = '<div>'.$content.'</div>';
501  return $content;
502}
503
504function get_cat_display_name_from_id($cat_id,
505                                      $url = '',
506                                      $replace_space = true)
507{
508  $cat_info = get_cat_info($cat_id);
509  return get_cat_display_name($cat_info['upper_names'], $url, $replace_space);
510}
511
512/**
513 * Returns an HTML list of tags. It can be a multi select field or a list of
514 * checkboxes.
515 *
516 * @param string HTML field name
517 * @param array selected tag ids
518 * @return array
519 */
520function get_html_tag_selection(
521  $tags,
522  $fieldname,
523  $selecteds = array(),
524  $forbidden_categories = null
525  )
526{
527  global $conf;
528
529  if (count ($tags) == 0 )
530  {
531    return '';
532  }
533  $output = '<ul class="tagSelection">';
534  foreach ($tags as $tag)
535  {
536    $output.=
537      '<li>'
538      .'<label>'
539      .'<input type="checkbox" name="'.$fieldname.'[]"'
540      .' value="'.$tag['id'].'"'
541      ;
542
543    if (in_array($tag['id'], $selecteds))
544    {
545      $output.= ' checked="checked"';
546    }
547
548    $output.=
549      ' />'
550      .' '. $tag['name']
551      .'</label>'
552      .'</li>'
553      ."\n"
554      ;
555  }
556  $output.= '</ul>';
557
558  return $output;
559}
560
561function name_compare($a, $b)
562{
563  return strcmp(strtolower($a['name']), strtolower($b['name']));
564}
565
566/**
567 * exits the current script (either exit or redirect)
568 */
569function access_denied()
570{
571  global $user, $lang;
572
573  $login_url =
574      get_root_url().'identification.php?redirect='
575      .urlencode(urlencode($_SERVER['REQUEST_URI']));
576
577  if ( isset($user['is_the_guest']) and !$user['is_the_guest'] )
578  {
579    echo '<div style="text-align:center;">'.$lang['access_forbiden'].'<br />';
580    echo '<a href="'.get_root_url().'identification.php">'.$lang['identification'].'</a>&nbsp;';
581    echo '<a href="'.make_index_url().'">'.$lang['home'].'</a></div>';
582    exit();
583  }
584  else
585  {
586    set_status_header(401);
587    redirect_html($login_url);
588  }
589}
590
591/**
592 * exits the current script with 403 code
593 * @param string msg a message to display
594 * @param string alternate_url redirect to this url
595 */
596function page_forbidden($msg, $alternate_url=null)
597{
598  set_status_header(403);
599  if ($alternate_url==null)
600    $alternate_url = make_index_url();
601  redirect_html( $alternate_url,
602    '<div style="text-align:left; margin-left:5em;margin-bottom:5em;">
603<h1 style="text-align:left; font-size:36px;">Forbidden</h1><br/>'
604.$msg.'</div>',
605    5 );
606}
607
608/**
609 * exits the current script with 400 code
610 * @param string msg a message to display
611 * @param string alternate_url redirect to this url
612 */
613function bad_request($msg, $alternate_url=null)
614{
615  set_status_header(400);
616  if ($alternate_url==null)
617    $alternate_url = make_index_url();
618  redirect_html( $alternate_url,
619    '<div style="text-align:left; margin-left:5em;margin-bottom:5em;">
620<h1 style="text-align:left; font-size:36px;">Bad request</h1><br/>'
621.$msg.'</div>',
622    5 );
623}
624
625/**
626 * exits the current script with 404 code when a page cannot be found
627 * @param string msg a message to display
628 * @param string alternate_url redirect to this url
629 */
630function page_not_found($msg, $alternate_url=null)
631{
632  set_status_header(404);
633  if ($alternate_url==null)
634    $alternate_url = make_index_url();
635  redirect_html( $alternate_url,
636    '<div style="text-align:left; margin-left:5em;margin-bottom:5em;">
637<h1 style="text-align:left; font-size:36px;">Page not found</h1><br/>'
638.$msg.'</div>',
639    5 );
640}
641
642/* returns the title to be displayed above thumbnails on tag page
643 */
644function get_tags_content_title()
645{
646  global $page;
647  $title = count($page['tags']) > 1 ? l10n('Tags') : l10n('Tag');
648  $title.= ' ';
649
650  for ($i=0; $i<count($page['tags']); $i++)
651  {
652    $title.= $i>0 ? ' + ' : '';
653
654    $title.=
655      '<a href="'
656      .make_index_url(
657        array(
658          'tags' => array( $page['tags'][$i] )
659          )
660        )
661      .'" title="'
662      .l10n('See pictures linked to this tag only')
663      .'">'
664      .$page['tags'][$i]['name']
665      .'</a>';
666
667    if ( count($page['tags'])>2 )
668    {
669      $other_tags = $page['tags'];
670      unset ( $other_tags[$i] );
671      $title.=
672        '<a href="'
673        .make_index_url(
674          array(
675            'tags' => $other_tags
676            )
677          )
678        .'" style="border:none;" title="'
679        .l10n('remove this tag')
680        .'"><img src="'
681        .get_root_url().get_themeconf('icon_dir').'/remove_s.png'
682        .'" alt="x" style="vertical-align:bottom;" class="button"/>'
683        .'</a>';
684    }
685
686  }
687  return $title;
688}
689
690/**
691  Sets the http status header (200,401,...)
692 */
693function set_status_header($code, $text='')
694{
695  if (empty($text))
696  {
697    switch ($code)
698    {
699      case 200: $text='OK';break;
700      case 301: $text='Moved permanently';break;
701      case 302: $text='Moved temporarily';break;
702      case 304: $text='Not modified';break;
703      case 400: $text='Bad request';break;
704      case 401: $text='Authorization required';break;
705      case 403: $text='Forbidden';break;
706      case 404: $text='Not found';break;
707    }
708  }
709  header("HTTP/1.1 $code $text");
710  header("Status: $code $text");
711  trigger_action('set_status_header', $code, $text);
712}
713
714/**
715 * set a class to display a counter
716 * .zero .one .plural
717 */
718function set_span_class($count)
719{
720  if ($count > 1) 
721  { 
722    return 'plural';
723  }
724  return ( $count == 0 ) ? 'zero':'one'; 
725}
726?>
Note: See TracBrowser for help on using the repository browser.