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

Last change on this file since 1744 was 1744, checked in by rvelices, 17 years ago
  • revert feature 564: log the login of each user; but add the possibility to be

done by a plugin

  • create a "standard" way to define PHP functions that we use but might not be

available in the current php version

  • when a comment is rejected (spam, anti-flood etc), put the content back to the

browser in case there is a real user behind it

  • now a comment can be entered only if the page was retrieved between 2 seconds

ago and 1 hour ago

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 18.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-2005 PhpWebGallery Team - http://phpwebgallery.net |
6// +-----------------------------------------------------------------------+
7// | branch        : BSF (Best So Far)
8// | last update   : $Date: 2007-01-23 01:22:52 +0000 (Tue, 23 Jan 2007) $
9// | last modifier : $Author: rvelices $
10// | revision      : $Revision: 1744 $
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  $diff = time() - $unixtime;
59  $day_in_seconds = 24*60*60;
60  $page['get_icon_cache'][$date] = false;
61  if ( $diff < $user['recent_period'] * $day_in_seconds )
62  {
63    if ( !isset($page['get_icon_cache']['_icons_'] ) )
64    {
65      $icons = array(false => 'recent', true => 'recent_by_child' );
66      $title = $lang['recent_image'].'&nbsp;'.$user['recent_period']
67          .'&nbsp;'.$lang['days'];
68      foreach ($icons as $key => $icon)
69      {
70        $icon_url = get_themeconf('icon_dir').'/'.$icon.'.png';
71        $size = getimagesize( PHPWG_ROOT_PATH.$icon_url );
72        $icon_url = get_root_url().$icon_url;
73        $output = '<img title="'.$title.'" src="'.$icon_url.'" class="icon" style="border:0;';
74        $output.= 'height:'.$size[1].'px;width:'.$size[0].'px" alt="(!)" />';
75        $page['get_icon_cache']['_icons_'][$key] = $output;
76      }
77    }
78    $page['get_icon_cache'][$date] = true;
79  }
80  if (! $page['get_icon_cache'][$date] )
81    return '';
82  return $page['get_icon_cache']['_icons_'][$is_child_date];
83}
84
85
86function create_navigation_bar(
87  $url, $nb_element, $start, $nb_element_page, $clean_url = false
88  )
89{
90  global $lang, $conf;
91
92  $pages_around = $conf['paginate_pages_around'];
93  $start_str = $clean_url ? '/start-' :
94    ( ( strstr($url, '?')===false ? '?':'&amp;') . 'start=' );
95
96  $navbar = '';
97
98  // current page detection
99  if (!isset($start)
100      or !is_numeric($start)
101      or (is_numeric($start) and $start < 0))
102  {
103    $start = 0;
104  }
105
106  // navigation bar useful only if more than one page to display !
107  if ($nb_element > $nb_element_page)
108  {
109    // current page and last page
110    $cur_page = ceil($start / $nb_element_page) + 1;
111    $maximum = ceil($nb_element / $nb_element_page);
112
113    // link to first page ?
114    if ($cur_page != 1)
115    {
116      $navbar.=
117        '<a href="'.$url.'" rel="start">'
118        .$lang['first_page']
119        .'</a>';
120    }
121    else
122    {
123      $navbar.= $lang['first_page'];
124    }
125    $navbar.= ' | ';
126    // link on previous page ?
127    if ($start != 0)
128    {
129      $previous = $start - $nb_element_page;
130
131      $navbar.=
132        '<a href="'
133        .$url.($previous > 0 ? $start_str.$previous : '')
134        .'" rel="prev">'
135        .$lang['previous_page']
136        .'</a>';
137    }
138    else
139    {
140      $navbar.= $lang['previous_page'];
141    }
142    $navbar.= ' |';
143
144    if ($cur_page > $pages_around + 1)
145    {
146      $navbar.= '&nbsp;<a href="'.$url.'">1</a>';
147
148      if ($cur_page > $pages_around + 2)
149      {
150        $navbar.= ' ...';
151      }
152    }
153
154    // inspired from punbb source code
155    for ($i = $cur_page - $pages_around, $stop = $cur_page + $pages_around + 1;
156         $i < $stop;
157         $i++)
158    {
159      if ($i < 1 or $i > $maximum)
160      {
161        continue;
162      }
163      else if ($i != $cur_page)
164      {
165        $temp_start = ($i - 1) * $nb_element_page;
166
167        $navbar.=
168          '&nbsp;'
169          .'<a href="'.$url
170          .($temp_start > 0 ? $start_str.$temp_start : '')
171          .'">'
172          .$i
173          .'</a>';
174      }
175      else
176      {
177        $navbar.=
178          '&nbsp;'
179          .'<span class="pageNumberSelected">'
180          .$i
181          .'</span>';
182      }
183    }
184
185    if ($cur_page < ($maximum - $pages_around))
186    {
187      $temp_start = ($maximum - 1) * $nb_element_page;
188
189      if ($cur_page < ($maximum - $pages_around - 1))
190      {
191        $navbar.= ' ...';
192      }
193
194      $navbar.= ' <a href="'.$url.$start_str.$temp_start.'">'.$maximum.'</a>';
195    }
196
197    $navbar.= ' | ';
198    // link on next page ?
199    if ($nb_element > $nb_element_page
200        and $start + $nb_element_page < $nb_element)
201    {
202      $next = $start + $nb_element_page;
203
204      $navbar.=
205        '<a href="'.$url.$start_str.$next.'" rel="next">'
206        .$lang['next_page']
207        .'</a>';
208    }
209    else
210    {
211      $navbar.= $lang['next_page'];
212    }
213
214    $navbar.= ' | ';
215    // link to last page ?
216    if ($cur_page != $maximum)
217    {
218      $temp_start = ($maximum - 1) * $nb_element_page;
219
220      $navbar.=
221        '<a href="'.$url.$start_str.$temp_start.'" rel="last">'
222        .$lang['last_page']
223        .'</a>';
224    }
225    else
226    {
227      $navbar.= $lang['last_page'];
228    }
229  }
230  return $navbar;
231}
232
233//
234// Pick a language, any language ...
235//
236function language_select($default, $select_name = "language")
237{
238  $available_lang = get_languages();
239
240  $lang_select = '<select name="' . $select_name . '">';
241  foreach ($available_lang as $code => $displayname)
242  {
243    $selected = ( strtolower($default) == strtolower($code) ) ? ' selected="selected"' : '';
244    $lang_select .= '<option value="' . $code . '"' . $selected . '>' . ucwords($displayname) . '</option>';
245  }
246  $lang_select .= '</select>';
247
248  return $lang_select;
249}
250
251/**
252 * returns the list of categories as a HTML string
253 *
254 * categories string returned contains categories as given in the input
255 * array $cat_informations. $cat_informations array must be an association
256 * of {category_id => category_name}. If url input parameter is null,
257 * returns only the categories name without links.
258 *
259 * @param array cat_informations
260 * @param string url
261 * @param boolean replace_space
262 * @return string
263 */
264function get_cat_display_name($cat_informations,
265                              $url = '',
266                              $replace_space = true)
267{
268  global $conf;
269
270  $output = '';
271  $is_first = true;
272  foreach ($cat_informations as $id => $name)
273  {
274    if ($is_first)
275    {
276      $is_first = false;
277    }
278    else
279    {
280      $output.= $conf['level_separator'];
281    }
282
283    if ( !isset($url) )
284    {
285      $output.= $name;
286    }
287    elseif ($url == '')
288    {
289      $output.= '<a class=""';
290      $output.= ' href="'
291            .duplicate_index_url(
292                array(
293                  'category'=>$id,
294                  'cat_name'=>$name
295                  ),
296                array('start')
297              )
298            .'">';
299      $output.= $name.'</a>';
300    }
301    else
302    {
303      $output.= '<a class=""';
304      $output.= ' href="'.PHPWG_ROOT_PATH.$url.$id.'">';
305      $output.= $name.'</a>';
306    }
307  }
308  if ($replace_space)
309  {
310    return replace_space($output);
311  }
312  else
313  {
314    return $output;
315  }
316}
317
318/**
319 * returns the list of categories as a HTML string, with cache of names
320 *
321 * categories string returned contains categories as given in the input
322 * array $cat_informations. $uppercats is the list of category ids to
323 * display in the right order. If url input parameter is empty, returns only
324 * the categories name without links.
325 *
326 * @param string uppercats
327 * @param string url
328 * @param boolean replace_space
329 * @return string
330 */
331function get_cat_display_name_cache($uppercats,
332                                    $url = '',
333                                    $replace_space = true)
334{
335  global $cat_names, $conf;
336
337  if (!isset($cat_names))
338  {
339    $query = '
340SELECT id,name
341  FROM '.CATEGORIES_TABLE.'
342;';
343    $result = pwg_query($query);
344    while ($row = mysql_fetch_array($result))
345    {
346      $cat_names[$row['id']] = $row['name'];
347    }
348  }
349
350  $output = '';
351  $is_first = true;
352  foreach (explode(',', $uppercats) as $category_id)
353  {
354    $name = $cat_names[$category_id];
355
356    if ($is_first)
357    {
358      $is_first = false;
359    }
360    else
361    {
362      $output.= $conf['level_separator'];
363    }
364
365    if ( !isset($url) )
366    {
367      $output.= $name;
368    }
369    elseif ($url == '')
370    {
371      $output.= '
372<a class=""
373   href="'
374      .make_index_url(
375          array(
376            'category'=>$category_id,
377            'cat_name'=>$name
378            )
379        )
380      .'">'.$name.'</a>';
381    }
382    else
383    {
384      $output.= '
385<a class=""
386   href="'.PHPWG_ROOT_PATH.$url.$category_id.'">'.$name.'</a>';
387    }
388  }
389  if ($replace_space)
390  {
391    return replace_space($output);
392  }
393  else
394  {
395    return $output;
396  }
397}
398
399/**
400 * returns the HTML code for a category item in the menu (for the main page)
401 *
402 * HTML code generated uses logical list tags ul and each category is an
403 * item li. The paramter given is the category informations as an array,
404 * used keys are : id, name, nb_images, max_date_last, date_last
405 * count_images, count_categories
406 *
407 * @param array categories
408 * @return string
409 */
410function get_html_menu_category($categories)
411{
412  global $page, $lang;
413
414  $ref_level = 0;
415  $level = 0;
416  $menu = '';
417
418  // $page_cat value remains 0 for special sections
419  $page_cat = 0;
420  if (isset($page['category']))
421  {
422    $page_cat = $page['category'];
423  }
424
425  foreach ($categories as $category)
426  {
427    $level = substr_count($category['global_rank'], '.') + 1;
428    if ($level > $ref_level)
429    {
430      $menu.= "\n<ul>";
431    }
432    else if ($level == $ref_level)
433    {
434      $menu.= "\n</li>";
435    }
436    else if ($level < $ref_level)
437    {
438      // we may have to close more than one level at the same time...
439      $menu.= "\n</li>";
440      $menu.= str_repeat("\n</ul></li>",($ref_level-$level));
441    }
442    $ref_level = $level;
443
444    $menu.= "\n\n".'<li';
445    if ($category['id'] == $page_cat)
446    {
447      $menu.= ' class="selected"';
448    }
449    $menu.= '>';
450
451    $url = make_index_url(
452            array(
453              'category'=>$category['id'],
454              'cat_name'=>$category['name']
455              )
456            );
457
458    $menu.= "\n".'<a href="'.$url.'"';
459    if ($page_cat != 0
460        and $category['id'] == $page['cat_id_uppercat'])
461    {
462      $menu.= ' rel="up"';
463    }
464    $menu.= '>'.$category['name'].'</a>';
465
466    // Count of category is main
467    // if not picture on categorie, test on sub-categories
468    if (($category['nb_images'] > 0) or ($category['count_images'] > 0))
469    {
470      $menu.= "\n".'<span class="';
471      $menu.= ($category['nb_images'] > 0 ? "menuInfoCat"
472                                          : "menuInfoCatByChild").'"';
473      $menu.= ' title="';
474      $menu.= ' '.get_display_images_count
475                  (
476                    $category['nb_images'],
477                    $category['count_images'],
478                    $category['count_categories'],
479                    false
480                  ).'">';
481      $menu.= '['.($category['nb_images'] > 0 ? $category['nb_images']
482                                              : $category['count_images']).']';
483      $menu.= '</span>';
484    }
485    $child_date_last = @$category['max_date_last']> @$category['date_last'];
486    $menu.= get_icon($category['max_date_last'], $child_date_last);
487  }
488
489  $menu.= str_repeat("\n</li></ul>",($level));
490
491  return $menu;
492}
493
494/**
495 * returns HTMLized comment contents retrieved from database
496 *
497 * newlines becomes br tags, _word_ becomes underline, /word/ becomes
498 * italic, *word* becomes bolded
499 *
500 * @param string content
501 * @return string
502 */
503function parse_comment_content($content)
504{
505  $pattern = '/(https?:\/\/\S*)/';
506  $replacement = '<a href="$1" rel="nofollow">$1</a>';
507  $content = preg_replace($pattern, $replacement, $content);
508
509  $content = nl2br($content);
510
511  // replace _word_ by an underlined word
512  $pattern = '/\b_(\S*)_\b/';
513  $replacement = '<span style="text-decoration:underline;">$1</span>';
514  $content = preg_replace($pattern, $replacement, $content);
515
516  // replace *word* by a bolded word
517  $pattern = '/\b\*(\S*)\*\b/';
518  $replacement = '<span style="font-weight:bold;">$1</span>';
519  $content = preg_replace($pattern, $replacement, $content);
520
521  // replace /word/ by an italic word
522  $pattern = "/\/(\S*)\/(\s)/";
523  $replacement = '<span style="font-style:italic;">$1$2</span>';
524  $content = preg_replace($pattern, $replacement, $content);
525
526  $content = '<div>'.$content.'</div>';
527  return $content;
528}
529
530function get_cat_display_name_from_id($cat_id,
531                                      $url = '',
532                                      $replace_space = true)
533{
534  $cat_info = get_cat_info($cat_id);
535  return get_cat_display_name($cat_info['name'], $url, $replace_space);
536}
537
538/**
539 * Returns an HTML list of tags. It can be a multi select field or a list of
540 * checkboxes.
541 *
542 * @param string HTML field name
543 * @param array selected tag ids
544 * @return array
545 */
546function get_html_tag_selection(
547  $tags,
548  $fieldname,
549  $selecteds = array(),
550  $forbidden_categories = null
551  )
552{
553  global $conf;
554
555  if (count ($tags) == 0 )
556  {
557    return '';
558  }
559  $output = '<ul class="tagSelection">';
560  foreach ($tags as $tag)
561  {
562    $output.=
563      '<li>'
564      .'<label>'
565      .'<input type="checkbox" name="'.$fieldname.'[]"'
566      .' value="'.$tag['tag_id'].'"'
567      ;
568
569    if (in_array($tag['tag_id'], $selecteds))
570    {
571      $output.= ' checked="checked"';
572    }
573
574    $output.=
575      ' />'
576      .' '. $tag['name']
577      .'</label>'
578      .'</li>'
579      ."\n"
580      ;
581  }
582  $output.= '</ul>';
583
584  return $output;
585}
586
587function name_compare($a, $b)
588{
589  return strcmp(strtolower($a['name']), strtolower($b['name']));
590}
591
592/**
593 * exits the current script (either exit or redirect)
594 */
595function access_denied()
596{
597  global $user, $lang;
598
599  $login_url =
600      get_root_url().'identification.php?redirect='
601      .urlencode(urlencode($_SERVER['REQUEST_URI']));
602
603  if ( isset($user['is_the_guest']) and !$user['is_the_guest'] )
604  {
605    echo '<div style="text-align:center;">'.$lang['access_forbiden'].'<br />';
606    echo '<a href="'.get_root_url().'identification.php">'.$lang['identification'].'</a>&nbsp;';
607    echo '<a href="'.make_index_url().'">'.$lang['home'].'</a></div>';
608    exit();
609  }
610  else
611  {
612    set_status_header(401);
613    redirect_html($login_url);
614  }
615}
616
617/**
618 * exits the current script with 403 code
619 * @param string msg a message to display
620 * @param string alternate_url redirect to this url
621 */
622function page_forbidden($msg, $alternate_url=null)
623{
624  set_status_header(403);
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;">Forbidden</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    }
717  }
718  header("HTTP/1.1 $code $text");
719  header("Status: $code $text");
720  trigger_action('set_status_header', $code, $text);
721}
722?>
Note: See TracBrowser for help on using the repository browser.