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

Last change on this file since 2265 was 2265, checked in by rvelices, 16 years ago
  • upload.tpl goes smarty
  • start some language cleanup and a small attempt to standardize a bit ...
  • debug_language now calls trigger_error instead of echo when missing language key
  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 18.7 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 2265 2008-03-08 01:38:37Z rvelices $
8// | last update   : $Date: 2008-03-08 01:38:37 +0000 (Sat, 08 Mar 2008) $
9// | last modifier : $Author: rvelices $
10// | revision      : $Revision: 2265 $
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;
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 = sprintf(
58        l10n('elements posted during the last %d days'),
59        $user['recent_period']
60        );
61      foreach ($icons as $key => $icon)
62      {
63        $icon_url = get_themeconf('icon_dir').'/'.$icon.'.png';
64        $size = getimagesize( PHPWG_ROOT_PATH.$icon_url );
65        $icon_url = get_root_url().$icon_url;
66        $output = '<img title="'.$title.'" src="'.$icon_url.'" class="icon" style="border:0;';
67        $output.= 'height:'.$size[1].'px;width:'.$size[0].'px" alt="(!)" />';
68        $page['get_icon_cache']['_icons_'][$key] = $output;
69      }
70    }
71    $page['get_icon_cache'][$date] = true;
72  }
73
74  if (! $page['get_icon_cache'][$date] )
75    return '';
76  return $page['get_icon_cache']['_icons_'][$is_child_date];
77}
78
79function create_navigation_bar(
80  $url, $nb_element, $start, $nb_element_page, $clean_url = false
81  )
82{
83  global $conf;
84
85  $pages_around = $conf['paginate_pages_around'];
86  $start_str = $clean_url ? '/start-' :
87    ( ( strstr($url, '?')===false ? '?':'&amp;') . 'start=' );
88
89  $navbar = '';
90
91  // current page detection
92  if (!isset($start)
93      or !is_numeric($start)
94      or (is_numeric($start) and $start < 0))
95  {
96    $start = 0;
97  }
98
99  // navigation bar useful only if more than one page to display !
100  if ($nb_element > $nb_element_page)
101  {
102    // current page and last page
103    $cur_page = ceil($start / $nb_element_page) + 1;
104    $maximum = ceil($nb_element / $nb_element_page);
105
106    // link to first page ?
107    if ($cur_page != 1)
108    {
109      $navbar.=
110        '<a href="'.$url.'" rel="start">'
111        .l10n('first_page')
112        .'</a>';
113    }
114    else
115    {
116      $navbar.= l10n('first_page');
117    }
118    $navbar.= ' | ';
119    // link on previous page ?
120    if ($start != 0)
121    {
122      $previous = $start - $nb_element_page;
123
124      $navbar.=
125        '<a href="'
126        .$url.($previous > 0 ? $start_str.$previous : '')
127        .'" rel="prev">'
128        .l10n('previous_page')
129        .'</a>';
130    }
131    else
132    {
133      $navbar.= l10n('previous_page');
134    }
135    $navbar.= ' |';
136
137    if ($cur_page > $pages_around + 1)
138    {
139      $navbar.= '&nbsp;<a href="'.$url.'">1</a>';
140
141      if ($cur_page > $pages_around + 2)
142      {
143        $navbar.= ' ...';
144      }
145    }
146
147    // inspired from punbb source code
148    for ($i = $cur_page - $pages_around, $stop = $cur_page + $pages_around + 1;
149         $i < $stop;
150         $i++)
151    {
152      if ($i < 1 or $i > $maximum)
153      {
154        continue;
155      }
156      else if ($i != $cur_page)
157      {
158        $temp_start = ($i - 1) * $nb_element_page;
159
160        $navbar.=
161          '&nbsp;'
162          .'<a href="'.$url
163          .($temp_start > 0 ? $start_str.$temp_start : '')
164          .'">'
165          .$i
166          .'</a>';
167      }
168      else
169      {
170        $navbar.=
171          '&nbsp;'
172          .'<span class="pageNumberSelected">'
173          .$i
174          .'</span>';
175      }
176    }
177
178    if ($cur_page < ($maximum - $pages_around))
179    {
180      $temp_start = ($maximum - 1) * $nb_element_page;
181
182      if ($cur_page < ($maximum - $pages_around - 1))
183      {
184        $navbar.= ' ...';
185      }
186
187      $navbar.= ' <a href="'.$url.$start_str.$temp_start.'">'.$maximum.'</a>';
188    }
189
190    $navbar.= ' | ';
191    // link on next page ?
192    if ($nb_element > $nb_element_page
193        and $start + $nb_element_page < $nb_element)
194    {
195      $next = $start + $nb_element_page;
196
197      $navbar.=
198        '<a href="'.$url.$start_str.$next.'" rel="next">'
199        .l10n('next_page')
200        .'</a>';
201    }
202    else
203    {
204      $navbar.= l10n('next_page');
205    }
206
207    $navbar.= ' | ';
208    // link to last page ?
209    if ($cur_page != $maximum)
210    {
211      $temp_start = ($maximum - 1) * $nb_element_page;
212
213      $navbar.=
214        '<a href="'.$url.$start_str.$temp_start.'" rel="last">'
215        .l10n('last_page')
216        .'</a>';
217    }
218    else
219    {
220      $navbar.= l10n('last_page');
221    }
222  }
223  return $navbar;
224}
225
226/**
227 * returns the list of categories as a HTML string
228 *
229 * categories string returned contains categories as given in the input
230 * array $cat_informations. $cat_informations array must be an array
231 * of array( id=>?, name=>?, permalink=>?). If url input parameter is null,
232 * returns only the categories name without links.
233 *
234 * @param array cat_informations
235 * @param string url
236 * @param boolean replace_space
237 * @return string
238 */
239function get_cat_display_name($cat_informations,
240                              $url = '',
241                              $replace_space = true)
242{
243  global $conf;
244
245  $output = '';
246  $is_first = true;
247  foreach ($cat_informations as $cat)
248  {
249    is_array($cat) or trigger_error(
250        'get_cat_display_name wrong type for category ', E_USER_WARNING
251      );
252    if ($is_first)
253    {
254      $is_first = false;
255    }
256    else
257    {
258      $output.= $conf['level_separator'];
259    }
260
261    if ( !isset($url) )
262    {
263      $output.= $cat['name'];
264    }
265    elseif ($url == '')
266    {
267      $output.= '<a href="'
268            .make_index_url(
269                array(
270                  'category' => $cat,
271                  )
272              )
273            .'">';
274      $output.= $cat['name'].'</a>';
275    }
276    else
277    {
278      $output.= '<a href="'.PHPWG_ROOT_PATH.$url.$cat['id'].'">';
279      $output.= $cat['name'].'</a>';
280    }
281  }
282  if ($replace_space)
283  {
284    return replace_space($output);
285  }
286  else
287  {
288    return $output;
289  }
290}
291
292/**
293 * returns the list of categories as a HTML string, with cache of names
294 *
295 * categories string returned contains categories as given in the input
296 * array $cat_informations. $uppercats is the list of category ids to
297 * display in the right order. If url input parameter is empty, returns only
298 * the categories name without links.
299 *
300 * @param string uppercats
301 * @param string url
302 * @param boolean replace_space
303 * @return string
304 */
305function get_cat_display_name_cache($uppercats,
306                                    $url = '',
307                                    $replace_space = true)
308{
309  global $cache, $conf;
310
311  if (!isset($cache['cat_names']))
312  {
313    $query = '
314SELECT id, name, permalink
315  FROM '.CATEGORIES_TABLE.'
316;';
317    $cache['cat_names'] = hash_from_query($query, 'id');
318  }
319
320  $output = '';
321  $is_first = true;
322  foreach (explode(',', $uppercats) as $category_id)
323  {
324    $cat = $cache['cat_names'][$category_id];
325
326    if ($is_first)
327    {
328      $is_first = false;
329    }
330    else
331    {
332      $output.= $conf['level_separator'];
333    }
334
335    if ( !isset($url) )
336    {
337      $output.= $cat['name'];
338    }
339    elseif ($url == '')
340    {
341      $output.= '
342<a href="'
343      .make_index_url(
344          array(
345            'category' => $cat,
346            )
347        )
348      .'">'.$cat['name'].'</a>';
349    }
350    else
351    {
352      $output.= '
353<a href="'.PHPWG_ROOT_PATH.$url.$category_id.'">'.$cat['name'].'</a>';
354    }
355  }
356  if ($replace_space)
357  {
358    return replace_space($output);
359  }
360  else
361  {
362    return $output;
363  }
364}
365
366/**
367 * returns the HTML code for a category item in the menu (for the main page)
368 *
369 * HTML code generated uses logical list tags ul and each category is an
370 * item li. The paramter given is the category informations as an array,
371 * used keys are : id, name, nb_images, max_date_last, date_last
372 * count_images, count_categories
373 *
374 * @param array categories
375 * @return string
376 */
377function get_html_menu_category($categories, $selected_category)
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;
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;">'.l10n('access_forbiden').'<br />';
570    echo '<a href="'.get_root_url().'identification.php">'.l10n('identification').'</a>&nbsp;';
571    echo '<a href="'.make_index_url().'">'.l10n('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 elements 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.