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

Last change on this file since 1201 was 1201, checked in by chrisaga, 18 years ago
  • HTML validation : get_html_tag_selection() must never return an empty list (<ul></ul>)
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.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-2005 PhpWebGallery Team - http://phpwebgallery.net |
6// +-----------------------------------------------------------------------+
7// | branch        : BSF (Best So Far)
8// | last update   : $Date: 2006-04-18 22:57:05 +0000 (Tue, 18 Apr 2006) $
9// | last modifier : $Author: chrisaga $
10// | revision      : $Revision: 1201 $
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)
28{
29  global $page, $user, $conf, $lang;
30
31  if (empty($date))
32  {
33    $date = 'NULL';
34  }
35
36  if (isset($page['get_icon_cache'][$date]))
37  {
38    return $page['get_icon_cache'][$date];
39  }
40
41  if (!preg_match('/^(\d{4})-(\d{2})-(\d{2})/', $date, $matches))
42  {
43    // date can be empty, no icon to display
44    $page['get_icon_cache'][$date] = '';
45    return $page['get_icon_cache'][$date];
46  }
47
48  list($devnull, $year, $month, $day) = $matches;
49  $unixtime = mktime( 0, 0, 0, $month, $day, $year );
50
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] = '';
55    return $page['get_icon_cache'][$date];
56  }
57
58  $diff = time() - $unixtime;
59  $day_in_seconds = 24*60*60;
60  $output = '';
61  $title = $lang['recent_image'].'&nbsp;';
62  if ( $diff < $user['recent_period'] * $day_in_seconds )
63  {
64    $icon_url = get_themeconf('icon_dir').'/recent.png';
65    $title .= $user['recent_period'];
66    $title .=  '&nbsp;'.$lang['days'];
67    $size = getimagesize( $icon_url );
68    $icon_url = get_root_url().$icon_url;
69    $output = '<img title="'.$title.'" src="'.$icon_url.'" class="icon" style="border:0;';
70    $output.= 'height:'.$size[1].'px;width:'.$size[0].'px" alt="(!)" />';
71  }
72
73  $page['get_icon_cache'][$date] = $output;
74
75  return $page['get_icon_cache'][$date];
76}
77
78function create_navigation_bar(
79  $url, $nb_element, $start, $nb_element_page, $clean_url = false
80  )
81{
82  global $lang, $conf;
83
84  $pages_around = $conf['paginate_pages_around'];
85  $start_str = $clean_url ? '/start-' : '&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// Pick a language, any language ...
226//
227function language_select($default, $select_name = "language")
228{
229  $available_lang = get_languages();
230
231  $lang_select = '<select name="' . $select_name . '">';
232  foreach ($available_lang as $code => $displayname)
233  {
234    $selected = ( strtolower($default) == strtolower($code) ) ? ' selected="selected"' : '';
235    $lang_select .= '<option value="' . $code . '"' . $selected . '>' . ucwords($displayname) . '</option>';
236  }
237  $lang_select .= '</select>';
238
239  return $lang_select;
240}
241
242/**
243 * returns the list of categories as a HTML string
244 *
245 * categories string returned contains categories as given in the input
246 * array $cat_informations. $cat_informations array must be an association
247 * of {category_id => category_name}. If url input parameter is null,
248 * returns only the categories name without links.
249 *
250 * @param array cat_informations
251 * @param string url
252 * @param boolean replace_space
253 * @return string
254 */
255function get_cat_display_name($cat_informations,
256                              $url = '',
257                              $replace_space = true)
258{
259  global $conf;
260
261  $output = '';
262  $is_first = true;
263  foreach ($cat_informations as $id => $name)
264  {
265    if ($is_first)
266    {
267      $is_first = false;
268    }
269    else
270    {
271      $output.= $conf['level_separator'];
272    }
273
274    if ( !isset($url) )
275    {
276      $output.= $name;
277    }
278    elseif ($url == '')
279    {
280      $output.= '<a class=""';
281      $output.= ' href="'
282            .make_index_url(
283                array(
284                  'category'=>$id,
285                  'cat_name'=>$name
286                  )
287              )
288            .'">';
289      $output.= $name.'</a>';
290    }
291    else
292    {
293      $output.= '<a class=""';
294      $output.= ' href="'.PHPWG_ROOT_PATH.$url.$id.'">';
295      $output.= $name.'</a>';
296    }
297  }
298  if ($replace_space)
299  {
300    return replace_space($output);
301  }
302  else
303  {
304    return $output;
305  }
306}
307
308/**
309 * returns the list of categories as a HTML string, with cache of names
310 *
311 * categories string returned contains categories as given in the input
312 * array $cat_informations. $uppercats is the list of category ids to
313 * display in the right order. If url input parameter is empty, returns only
314 * the categories name without links.
315 *
316 * @param string uppercats
317 * @param string url
318 * @param boolean replace_space
319 * @return string
320 */
321function get_cat_display_name_cache($uppercats,
322                                    $url = '',
323                                    $replace_space = true)
324{
325  global $cat_names, $conf;
326
327  if (!isset($cat_names))
328  {
329    $query = '
330SELECT id,name
331  FROM '.CATEGORIES_TABLE.'
332;';
333    $result = pwg_query($query);
334    while ($row = mysql_fetch_array($result))
335    {
336      $cat_names[$row['id']] = $row['name'];
337    }
338  }
339
340  $output = '';
341  $is_first = true;
342  foreach (explode(',', $uppercats) as $category_id)
343  {
344    $name = $cat_names[$category_id];
345
346    if ($is_first)
347    {
348      $is_first = false;
349    }
350    else
351    {
352      $output.= $conf['level_separator'];
353    }
354
355    if ( !isset($url) )
356    {
357      $output.= $name;
358    }
359    elseif ($url == '')
360    {
361      $output.= '
362<a class=""
363   href="'
364      .make_index_url(
365          array(
366            'category'=>$category_id,
367            'cat_name'=>$name
368            )
369        )
370      .'">'.$name.'</a>';
371    }
372    else
373    {
374      $output.= '
375<a class=""
376   href="'.PHPWG_ROOT_PATH.$url.$category_id.'">'.$name.'</a>';
377    }
378  }
379  if ($replace_space)
380  {
381    return replace_space($output);
382  }
383  else
384  {
385    return $output;
386  }
387}
388
389/**
390 * returns the HTML code for a category item in the menu (for the main page)
391 *
392 * HTML code generated uses logical list tags ul and each category is an
393 * item li. The paramter given is the category informations as an array,
394 * used keys are : id, name, nb_images, date_last
395 *
396 * @param array categories
397 * @return string
398 */
399function get_html_menu_category($categories)
400{
401  global $page, $lang;
402
403  $ref_level = 0;
404  $level = 0;
405  $menu = '';
406
407  // $page_cat value remains 0 for special sections
408  $page_cat = 0;
409  if (isset($page['category']))
410  {
411    $page_cat = $page['category'];
412  }
413
414  foreach ($categories as $category)
415  {
416    $level = substr_count($category['global_rank'], '.') + 1;
417    if ($level > $ref_level)
418    {
419      $menu.= "\n<ul>";
420    }
421    else if ($level == $ref_level)
422    {
423      $menu.= "\n</li>";
424    }
425    else if ($level < $ref_level)
426    {
427      // we may have to close more than one level at the same time...
428      $menu.= "\n</li>";
429      $menu.= str_repeat("\n</ul></li>",($ref_level-$level));
430    }
431    $ref_level = $level;
432
433    $menu.= "\n\n".'<li';
434    if ($category['id'] == $page_cat)
435    {
436      $menu.= ' class="selected"';
437    }
438    $menu.= '>';
439
440    $url = make_index_url(
441            array(
442              'category'=>$category['id'],
443              'cat_name'=>$category['name']
444              )
445            );
446
447    $menu.= "\n".'<a href="'.$url.'"';
448    if ($page_cat != 0
449        and $category['id'] == $page['cat_id_uppercat'])
450    {
451      $menu.= ' rel="up"';
452    }
453    $menu.= '>'.$category['name'].'</a>';
454
455    if ($category['nb_images'] > 0)
456    {
457      $menu.= "\n".'<span class="menuInfoCat"';
458      $menu.= ' title="'.$category['nb_images'];
459      $menu.= ' '.$lang['images_available'].'">';
460      $menu.= '['.$category['nb_images'].']';
461      $menu.= '</span>';
462      $menu.= get_icon($category['date_last']);
463    }
464  }
465
466  $menu.= str_repeat("\n</li></ul>",($level));
467
468  return $menu;
469}
470
471/**
472 * returns HTMLized comment contents retrieved from database
473 *
474 * newlines becomes br tags, _word_ becomes underline, /word/ becomes
475 * italic, *word* becomes bolded
476 *
477 * @param string content
478 * @return string
479 */
480function parse_comment_content($content)
481{
482  $pattern = '/(https?:\/\/\S*)/';
483  $replacement = '<a href="$1" rel="nofollow">$1</a>';
484  $content = preg_replace($pattern, $replacement, $content);
485
486  $content = nl2br($content);
487
488  // replace _word_ by an underlined word
489  $pattern = '/\b_(\S*)_\b/';
490  $replacement = '<span style="text-decoration:underline;">$1</span>';
491  $content = preg_replace($pattern, $replacement, $content);
492
493  // replace *word* by a bolded word
494  $pattern = '/\b\*(\S*)\*\b/';
495  $replacement = '<span style="font-weight:bold;">$1</span>';
496  $content = preg_replace($pattern, $replacement, $content);
497
498  // replace /word/ by an italic word
499  $pattern = "/\/(\S*)\/(\s)/";
500  $replacement = '<span style="font-style:italic;">$1$2</span>';
501  $content = preg_replace($pattern, $replacement, $content);
502
503  $content = '<div>'.$content.'</div>';
504  return $content;
505}
506
507function get_cat_display_name_from_id($cat_id,
508                                      $url = '',
509                                      $replace_space = true)
510{
511  $cat_info = get_cat_info($cat_id);
512  return get_cat_display_name($cat_info['name'], $url, $replace_space);
513}
514
515/**
516 * Returns an HTML list of tags. It can be a multi select field or a list of
517 * checkboxes.
518 *
519 * @param string HTML field name
520 * @param array selected tag ids
521 * @return array
522 */
523function get_html_tag_selection(
524  $tags,
525  $fieldname,
526  $selecteds = array(),
527  $forbidden_categories = null
528  )
529{
530  global $conf;
531
532  if (count ($tags) == 0 )
533  {
534    return '';
535  }
536  $output = '<ul class="tagSelection">';
537  foreach ($tags as $tag)
538  {
539    $output.=
540      '<li>'
541      .'<label>'
542      .'<input type="checkbox" name="'.$fieldname.'[]"'
543      .' value="'.$tag['tag_id'].'"'
544      ;
545
546    if (in_array($tag['tag_id'], $selecteds))
547    {
548      $output.= ' checked="checked"';
549    }
550
551    $output.=
552      ' />'
553      .' '.$tag['name']
554      .'</label>'
555      .'</li>'
556      ."\n"
557      ;
558  }
559  $output.= '</ul>';
560
561  return $output;
562}
563
564function name_compare($a, $b)
565{
566  return strcmp($a['name'], $b['name']);
567}
568
569/**
570 * exits the current script (either exit or redirect)
571 */
572function access_denied()
573{
574  global $user, $lang;
575
576  $login_url =
577      get_root_url().'identification.php?redirect='
578      .urlencode(urlencode($_SERVER['REQUEST_URI']));
579
580  if ( isset($user['is_the_guest']) and !$user['is_the_guest'] )
581  {
582    echo '<div style="text-align:center;">'.$lang['access_forbiden'].'<br />';
583    echo '<a href="'.get_root_url().'identification.php">'.$lang['identification'].'</a>&nbsp;';
584    echo '<a href="'.make_index_url().'">'.$lang['home'].'</a></div>';
585    exit();
586  }
587  else
588  {
589    header('HTTP/1.1 401 Authorization required');
590    header('Status: 401 Authorization required');
591    redirect($login_url);
592  }
593}
594?>
Note: See TracBrowser for help on using the repository browser.