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

Last change on this file since 5441 was 5441, checked in by laurent.duretz, 14 years ago

Issue 1521 : Integration of LinkRoot plugin in Piwigo core

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