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

Last change on this file since 3282 was 3282, checked in by plg, 15 years ago

change: according to topic:15067, svn:keywords property was removed

  • Property svn:eol-style set to LF
File size: 14.0 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2009 Piwigo Team                  http://piwigo.org |
6// | Copyright(C) 2003-2008 PhpWebGallery Team    http://phpwebgallery.net |
7// | Copyright(C) 2002-2003 Pierrick LE GALL   http://le-gall.net/pierrick |
8// +-----------------------------------------------------------------------+
9// | This program is free software; you can redistribute it and/or modify  |
10// | it under the terms of the GNU General Public License as published by  |
11// | the Free Software Foundation                                          |
12// |                                                                       |
13// | This program is distributed in the hope that it will be useful, but   |
14// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
15// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
16// | General Public License for more details.                              |
17// |                                                                       |
18// | You should have received a copy of the GNU General Public License     |
19// | along with this program; if not, write to the Free Software           |
20// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
21// | USA.                                                                  |
22// +-----------------------------------------------------------------------+
23
24/**
25 * returns the list of categories as a HTML string
26 *
27 * categories string returned contains categories as given in the input
28 * array $cat_informations. $cat_informations array must be an array
29 * of array( id=>?, name=>?, permalink=>?). If url input parameter is null,
30 * returns only the categories name without links.
31 *
32 * @param array cat_informations
33 * @param string url
34 * @param boolean replace_space
35 * @return string
36 */
37function get_cat_display_name($cat_informations,
38                              $url = '',
39                              $replace_space = true)
40{
41  global $conf;
42
43  $output = '';
44  $is_first = true;
45  foreach ($cat_informations as $cat)
46  {
47    is_array($cat) or trigger_error(
48        'get_cat_display_name wrong type for category ', E_USER_WARNING
49      );
50
51    $cat['name'] = trigger_event(
52      'render_category_name',
53      $cat['name'],
54      'get_cat_display_name'
55      );
56
57    if ($is_first)
58    {
59      $is_first = false;
60    }
61    else
62    {
63      $output.= $conf['level_separator'];
64    }
65
66    if ( !isset($url) )
67    {
68      $output.= $cat['name'];
69    }
70    elseif ($url == '')
71    {
72      $output.= '<a href="'
73            .make_index_url(
74                array(
75                  'category' => $cat,
76                  )
77              )
78            .'">';
79      $output.= $cat['name'].'</a>';
80    }
81    else
82    {
83      $output.= '<a href="'.PHPWG_ROOT_PATH.$url.$cat['id'].'">';
84      $output.= $cat['name'].'</a>';
85    }
86  }
87  if ($replace_space)
88  {
89    return replace_space($output);
90  }
91  else
92  {
93    return $output;
94  }
95}
96
97/**
98 * returns the list of categories as a HTML string, with cache of names
99 *
100 * categories string returned contains categories as given in the input
101 * array $cat_informations. $uppercats is the list of category ids to
102 * display in the right order. If url input parameter is empty, returns only
103 * the categories name without links.
104 *
105 * @param string uppercats
106 * @param string url
107 * @param boolean replace_space
108 * @return string
109 */
110function get_cat_display_name_cache($uppercats,
111                                    $url = '',
112                                    $replace_space = true)
113{
114  global $cache, $conf;
115
116  if (!isset($cache['cat_names']))
117  {
118    $query = '
119SELECT id, name, permalink
120  FROM '.CATEGORIES_TABLE.'
121;';
122    $cache['cat_names'] = hash_from_query($query, 'id');
123  }
124
125  $output = '';
126  $is_first = true;
127  foreach (explode(',', $uppercats) as $category_id)
128  {
129    $cat = $cache['cat_names'][$category_id];
130
131    $cat['name'] = trigger_event(
132      'render_category_name',
133      $cat['name'],
134      'get_cat_display_name_cache'
135      );
136
137    if ($is_first)
138    {
139      $is_first = false;
140    }
141    else
142    {
143      $output.= $conf['level_separator'];
144    }
145
146    if ( !isset($url) )
147    {
148      $output.= $cat['name'];
149    }
150    elseif ($url == '')
151    {
152      $output.= '
153<a href="'
154      .make_index_url(
155          array(
156            'category' => $cat,
157            )
158        )
159      .'">'.$cat['name'].'</a>';
160    }
161    else
162    {
163      $output.= '
164<a href="'.PHPWG_ROOT_PATH.$url.$category_id.'">'.$cat['name'].'</a>';
165    }
166  }
167  if ($replace_space)
168  {
169    return replace_space($output);
170  }
171  else
172  {
173    return $output;
174  }
175}
176
177/**
178 * returns HTMLized comment contents retrieved from database
179 *
180 * newlines becomes br tags, _word_ becomes underline, /word/ becomes
181 * italic, *word* becomes bolded
182 *
183 * @param string content
184 * @return string
185 */
186function parse_comment_content($content)
187{
188  $pattern = '/(https?:\/\/\S*)/';
189  $replacement = '<a href="$1" rel="nofollow">$1</a>';
190  $content = preg_replace($pattern, $replacement, $content);
191
192  $content = nl2br($content);
193
194  // replace _word_ by an underlined word
195  $pattern = '/\b_(\S*)_\b/';
196  $replacement = '<span style="text-decoration:underline;">$1</span>';
197  $content = preg_replace($pattern, $replacement, $content);
198
199  // replace *word* by a bolded word
200  $pattern = '/\b\*(\S*)\*\b/';
201  $replacement = '<span style="font-weight:bold;">$1</span>';
202  $content = preg_replace($pattern, $replacement, $content);
203
204  // replace /word/ by an italic word
205  $pattern = "/\/(\S*)\/(\s)/";
206  $replacement = '<span style="font-style:italic;">$1$2</span>';
207  $content = preg_replace($pattern, $replacement, $content);
208
209  $content = '<div>'.$content.'</div>';
210  return $content;
211}
212
213function get_cat_display_name_from_id($cat_id,
214                                      $url = '',
215                                      $replace_space = true)
216{
217  $cat_info = get_cat_info($cat_id);
218  return get_cat_display_name($cat_info['upper_names'], $url, $replace_space);
219}
220
221/**
222 * Returns an HTML list of tags. It can be a multi select field or a list of
223 * checkboxes.
224 *
225 * @param string HTML field name
226 * @param array selected tag ids
227 * @return array
228 */
229function get_html_tag_selection(
230  $tags,
231  $fieldname,
232  $selecteds = array(),
233  $forbidden_categories = null
234  )
235{
236  global $conf;
237
238  if (count ($tags) == 0 )
239  {
240    return '';
241  }
242  $output = '<ul class="tagSelection">';
243  foreach ($tags as $tag)
244  {
245    $output.=
246      '<li>'
247      .'<label>'
248      .'<input type="checkbox" name="'.$fieldname.'[]"'
249      .' value="'.$tag['id'].'"'
250      ;
251
252    if (in_array($tag['id'], $selecteds))
253    {
254      $output.= ' checked="checked"';
255    }
256
257    $output.=
258      '>'
259      .' '. $tag['name']
260      .'</label>'
261      .'</li>'
262      ."\n"
263      ;
264  }
265  $output.= '</ul>';
266
267  return $output;
268}
269
270function name_compare($a, $b)
271{
272  return strcmp(strtolower($a['name']), strtolower($b['name']));
273}
274
275function tag_alpha_compare($a, $b)
276{
277  return strcmp(strtolower($a['url_name']), strtolower($b['url_name']));
278}
279
280/**
281 * exits the current script (either exit or redirect)
282 */
283function access_denied()
284{
285  global $user;
286
287  $login_url =
288      get_root_url().'identification.php?redirect='
289      .urlencode(urlencode($_SERVER['REQUEST_URI']));
290
291  set_status_header(401);
292  if ( isset($user) and !is_a_guest() )
293  {
294    echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8">';
295    echo '<div style="text-align:center;">'.l10n('access_forbiden').'<br>';
296    echo '<a href="'.get_root_url().'identification.php">'.l10n('identification').'</a>&nbsp;';
297    echo '<a href="'.make_index_url().'">'.l10n('home').'</a></div>';
298    echo str_repeat( ' ', 512); //IE6 doesn't error output if below a size
299    exit();
300  }
301  else
302  {
303    redirect_html($login_url);
304  }
305}
306
307/**
308 * exits the current script with 403 code
309 * @param string msg a message to display
310 * @param string alternate_url redirect to this url
311 */
312function page_forbidden($msg, $alternate_url=null)
313{
314  set_status_header(403);
315  if ($alternate_url==null)
316    $alternate_url = make_index_url();
317  redirect_html( $alternate_url,
318    '<div style="text-align:left; margin-left:5em;margin-bottom:5em;">
319<h1 style="text-align:left; font-size:36px;">Forbidden</h1><br>'
320.$msg.'</div>',
321    5 );
322}
323
324/**
325 * exits the current script with 400 code
326 * @param string msg a message to display
327 * @param string alternate_url redirect to this url
328 */
329function bad_request($msg, $alternate_url=null)
330{
331  set_status_header(400);
332  if ($alternate_url==null)
333    $alternate_url = make_index_url();
334  redirect_html( $alternate_url,
335    '<div style="text-align:left; margin-left:5em;margin-bottom:5em;">
336<h1 style="text-align:left; font-size:36px;">Bad request</h1><br>'
337.$msg.'</div>',
338    5 );
339}
340
341/**
342 * exits the current script with 404 code when a page cannot be found
343 * @param string msg a message to display
344 * @param string alternate_url redirect to this url
345 */
346function page_not_found($msg, $alternate_url=null)
347{
348  set_status_header(404);
349  if ($alternate_url==null)
350    $alternate_url = make_index_url();
351  redirect_html( $alternate_url,
352    '<div style="text-align:left; margin-left:5em;margin-bottom:5em;">
353<h1 style="text-align:left; font-size:36px;">Page not found</h1><br>'
354.$msg.'</div>',
355    5 );
356}
357
358/**
359 * exits the current script with 500 http code
360 * this method can be called at any time (does not use template/language/user etc...)
361 * @param string msg a message to display
362 */
363function fatal_error($msg)
364{
365  $btrace_msg = '';
366  if (function_exists('debug_backtrace'))
367  {
368    $bt = debug_backtrace();
369    for ($i=1; $i<count($bt); $i++)
370    {
371      $class = isset($bt[$i]['class']) ? (@$bt[$i]['class'].'::') : '';
372      $btrace_msg .= "#$i\t".$class.@$bt[$i]['function'].' '.@$bt[$i]['file']."(".@$bt[$i]['line'].")\n";
373    }
374    $btrace_msg = trim($btrace_msg);
375    $msg .= "\n";
376  }
377
378  $display = "<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
379<h1>Piwigo encountered a non recoverable error</h1>
380<pre style='font-size:larger;background:white;color:red;padding:1em;margin:0;clear:both;display:block;width:auto;height:auto;overflow:auto'>
381<b>$msg</b>
382$btrace_msg
383</pre>\n";
384
385  @set_status_header(500);
386  echo $display.str_repeat( ' ', 300); //IE6 doesn't error output if below a size
387
388  if ( function_exists('ini_set') )
389  {// if possible turn off error display (we display it)
390    ini_set('display_errors', false);
391  }
392  error_reporting( E_ALL );
393  trigger_error( strip_tags($msg).$btrace_msg, E_USER_ERROR );
394  die(0); // just in case
395}
396
397/* returns the title to be displayed above thumbnails on tag page
398 */
399function get_tags_content_title()
400{
401  global $page;
402  $title = count($page['tags']) > 1 ? l10n('Tags') : l10n('Tag');
403  $title.= ' ';
404
405  for ($i=0; $i<count($page['tags']); $i++)
406  {
407    $title.= $i>0 ? ' + ' : '';
408
409    $title.=
410      '<a href="'
411      .make_index_url(
412        array(
413          'tags' => array( $page['tags'][$i] )
414          )
415        )
416      .'" title="'
417      .l10n('See elements linked to this tag only')
418      .'">'
419      .$page['tags'][$i]['name']
420      .'</a>';
421
422    if ( count($page['tags'])>2 )
423    {
424      $other_tags = $page['tags'];
425      unset ( $other_tags[$i] );
426      $title.=
427        '<a href="'
428        .make_index_url(
429          array(
430            'tags' => $other_tags
431            )
432          )
433        .'" style="border:none;" title="'
434        .l10n('remove this tag')
435        .'"><img src="'
436        .get_root_url().get_themeconf('icon_dir').'/remove_s.png'
437        .'" alt="x" style="vertical-align:bottom;" class="button">'
438        .'</a>';
439    }
440
441  }
442  return $title;
443}
444
445/**
446  Sets the http status header (200,401,...)
447 */
448function set_status_header($code, $text='')
449{
450  if (empty($text))
451  {
452    switch ($code)
453    {
454      case 200: $text='OK';break;
455      case 301: $text='Moved permanently';break;
456      case 302: $text='Moved temporarily';break;
457      case 304: $text='Not modified';break;
458      case 400: $text='Bad request';break;
459      case 401: $text='Authorization required';break;
460      case 403: $text='Forbidden';break;
461      case 404: $text='Not found';break;
462      case 500: $text='Server error';break;
463      case 501: $text='Not implemented';break;
464      case 503: $text='Service unavailable';break;
465    }
466  }
467        $protocol = $_SERVER["SERVER_PROTOCOL"];
468        if ( ('HTTP/1.1' != $protocol) && ('HTTP/1.0' != $protocol) )
469                $protocol = 'HTTP/1.0';
470
471        if ( version_compare( phpversion(), '4.3.0', '>=' ) )
472  {
473                header( "$protocol $code $text", true, $code );
474        }
475  else
476  {
477                header( "$protocol $code $text" );
478        }
479  trigger_action('set_status_header', $code, $text);
480}
481
482/** returns the category comment for rendering in html textual mode (subcatify)
483 * this is an event handler. don't call directly
484 */
485function render_category_literal_description($desc)
486{
487  return strip_tags($desc, '<span><p><a><br><b><i><small><big><strong><em>');
488}
489
490/** returns the argument_ids array with new sequenced keys based on related
491 * names. Sequence is not case sensitive.
492 * Warning: By definition, this function breaks original keys
493 */
494function order_by_name($element_ids,$name)
495{
496  $ordered_element_ids = array();
497  foreach ($element_ids as $k_id => $element_id)
498  {
499    $key = strtolower($name[$element_id]) .'-'. $name[$element_id] .'-'. $k_id;
500    $ordered_element_ids[$key] = $element_id;
501  }
502  ksort($ordered_element_ids);
503  return $ordered_element_ids;
504}
505
506/*event handler for menu*/
507function register_default_menubar_blocks( $menu_ref_arr )
508{
509  $menu = & $menu_ref_arr[0];
510  if ($menu->get_id() != 'menubar')
511    return;
512  $menu->register_block( new RegisteredBlock( 'mbLinks', 'Links', 'piwigo'));
513  $menu->register_block( new RegisteredBlock( 'mbCategories', 'Categories', 'piwigo'));
514  $menu->register_block( new RegisteredBlock( 'mbTags', 'Related tags', 'piwigo'));
515  $menu->register_block( new RegisteredBlock( 'mbSpecials', 'special_categories', 'piwigo'));
516  $menu->register_block( new RegisteredBlock( 'mbMenu', 'title_menu', 'piwigo'));
517  $menu->register_block( new RegisteredBlock( 'mbIdentification', 'identification', 'piwigo') );
518}
519
520?>
Note: See TracBrowser for help on using the repository browser.