?, name=>?, permalink=>?). If url input parameter is null,
* returns only the categories name without links.
*
* @param array $cat_informations
* @param string|null $url
* @return string
*/
function get_cat_display_name($cat_informations, $url='')
{
global $conf;
//$output = ''.l10n('Home').'';
$output = '';
$is_first=true;
foreach ($cat_informations as $cat)
{
is_array($cat) or trigger_error(
'get_cat_display_name wrong type for category ', E_USER_WARNING
);
$cat['name'] = trigger_change(
'render_category_name',
$cat['name'],
'get_cat_display_name'
);
if ($is_first)
{
$is_first=false;
}
else
{
$output.= $conf['level_separator'];
}
if ( !isset($url) )
{
$output.= $cat['name'];
}
elseif ($url == '')
{
$output.= '';
$output.= $cat['name'].'';
}
else
{
$output.= '';
$output.= $cat['name'].'';
}
}
return $output;
}
/**
* Generates breadcrumb from categories list using a cache.
* @see get_cat_display_name()
*
* @param string $uppercats
* @param string|null $url
* @param bool $single_link
* @param string|null $link_class
* @return string
*/
function get_cat_display_name_cache($uppercats,
$url = '',
$single_link = false,
$link_class = null)
{
global $cache, $conf;
if (!isset($cache['cat_names']))
{
$query = '
SELECT id, name, permalink
FROM '.CATEGORIES_TABLE.'
;';
$cache['cat_names'] = query2array($query, 'id');
}
$output = '';
if ($single_link)
{
$single_url = get_root_url().$url.array_pop(explode(',', $uppercats));
$output.= '';
}
$is_first = true;
foreach (explode(',', $uppercats) as $category_id)
{
$cat = $cache['cat_names'][$category_id];
$cat['name'] = trigger_change(
'render_category_name',
$cat['name'],
'get_cat_display_name_cache'
);
if ($is_first)
{
$is_first = false;
}
else
{
$output.= $conf['level_separator'];
}
if ( !isset($url) or $single_link )
{
$output.= $cat['name'];
}
elseif ($url == '')
{
$output.= '
'.$cat['name'].'';
}
else
{
$output.= '
'.$cat['name'].'';
}
}
if ($single_link and isset($single_url))
{
$output.= '';
}
return $output;
}
/**
* Generates breadcrumb for a category.
* @see get_cat_display_name()
*
* @param int $cat_id
* @param string|null $url
* @return string
*/
function get_cat_display_name_from_id($cat_id, $url = '')
{
$cat_info = get_cat_info($cat_id);
return get_cat_display_name($cat_info['upper_names'], $url);
}
/**
* Apply basic markdown transformations to a text.
* newlines becomes br tags
* _word_ becomes underline
* /word/ becomes italic
* *word* becomes bolded
* urls becomes a tags
*
* @param string $content
* @return string
*/
function render_comment_content($content)
{
$content = htmlspecialchars($content);
$pattern = '/(https?:\/\/\S*)/';
$replacement = '$1';
$content = preg_replace($pattern, $replacement, $content);
$content = nl2br($content);
// replace _word_ by an underlined word
$pattern = '/\b_(\S*)_\b/';
$replacement = '$1';
$content = preg_replace($pattern, $replacement, $content);
// replace *word* by a bolded word
$pattern = '/\b\*(\S*)\*\b/';
$replacement = '$1';
$content = preg_replace($pattern, $replacement, $content);
// replace /word/ by an italic word
$pattern = "/\/(\S*)\/(\s)/";
$replacement = '$1$2';
$content = preg_replace($pattern, $replacement, $content);
// TODO : add a trigger
return $content;
}
/**
* Callback used for sorting by name.
*/
function name_compare($a, $b)
{
return strcmp(strtolower($a['name']), strtolower($b['name']));
}
/**
* Callback used for sorting by name (slug) with cache.
*/
function tag_alpha_compare($a, $b)
{
global $cache;
foreach (array($a, $b) as $tag)
{
if (!isset($cache[__FUNCTION__][ $tag['name'] ]))
{
$cache[__FUNCTION__][ $tag['name'] ] = transliterate($tag['name']);
}
}
return strcmp($cache[__FUNCTION__][ $a['name'] ], $cache[__FUNCTION__][ $b['name'] ]);
}
/**
* Exits the current script (or redirect to login page if not logged).
*/
function access_denied()
{
global $user;
$login_url =
get_root_url().'identification.php?redirect='
.urlencode(urlencode($_SERVER['REQUEST_URI']));
set_status_header(401);
if ( isset($user) and !is_a_guest() )
{
echo '';
echo '
';
echo str_repeat( ' ', 512); //IE6 doesn't error output if below a size
exit();
}
else
{
redirect_html($login_url);
}
}
/**
* Exits the current script with 403 code.
* @todo nice display if $template loaded
*
* @param string $msg
* @param string|null $alternate_url redirect to this url
*/
function page_forbidden($msg, $alternate_url=null)
{
set_status_header(403);
if ($alternate_url==null)
$alternate_url = make_index_url();
redirect_html( $alternate_url,
'
'.l10n('Forbidden').'
'
.$msg.'',
5 );
}
/**
* Exits the current script with 400 code.
* @todo nice display if $template loaded
*
* @param string $msg
* @param string|null $alternate_url redirect to this url
*/
function bad_request($msg, $alternate_url=null)
{
set_status_header(400);
if ($alternate_url==null)
$alternate_url = make_index_url();
redirect_html( $alternate_url,
'
'.l10n('Bad request').'
'
.$msg.'',
5 );
}
/**
* Exits the current script with 404 code.
* @todo nice display if $template loaded
*
* @param string $msg
* @param string|null $alternate_url redirect to this url
*/
function page_not_found($msg, $alternate_url=null)
{
set_status_header(404);
if ($alternate_url==null)
$alternate_url = make_index_url();
redirect_html( $alternate_url,
'
'.l10n('Page not found').'
'
.$msg.'',
5 );
}
/**
* Exits the current script with 500 code.
* @todo nice display if $template loaded
*
* @param string $msg
* @param string|null $title
* @param bool $show_trace
*/
function fatal_error($msg, $title=null, $show_trace=true)
{
if (empty($title))
{
$title = l10n('Piwigo encountered a non recoverable error');
}
$btrace_msg = '';
if ($show_trace and function_exists('debug_backtrace'))
{
$bt = debug_backtrace();
for ($i=1; $i
$title
$msg
$btrace_msg
\n";
@set_status_header(500);
echo $display.str_repeat( ' ', 300); //IE6 doesn't error output if below a size
if ( function_exists('ini_set') )
{// if possible turn off error display (we display it)
ini_set('display_errors', false);
}
error_reporting( E_ALL );
trigger_error( strip_tags($msg).$btrace_msg, E_USER_ERROR );
die(0); // just in case
}
/**
* Returns the breadcrumb to be displayed above thumbnails on tag page.
*
* @return string
*/
function get_tags_content_title()
{
global $page;
$title = ''
. l10n( count($page['tags']) > 1 ? 'Tags' : 'Tag' )
. ' ';
for ($i=0; $i0 ? ' + ' : '';
$title.=
''
.trigger_change('render_tag_name', $page['tags'][$i]['name'], $page['tags'][$i])
.'';
if (count($page['tags']) > 2)
{
$other_tags = $page['tags'];
unset($other_tags[$i]);
$remove_url = make_index_url(
array(
'tags' => $other_tags
)
);
$title.=
''
.'';
}
}
return $title;
}
/**
* Sets the http status header (200,401,...)
* @param int $code
* @param string $text for exotic http codes
*/
function set_status_header($code, $text='')
{
if (empty($text))
{
switch ($code)
{
case 200: $text='OK';break;
case 301: $text='Moved permanently';break;
case 302: $text='Moved temporarily';break;
case 304: $text='Not modified';break;
case 400: $text='Bad request';break;
case 401: $text='Authorization required';break;
case 403: $text='Forbidden';break;
case 404: $text='Not found';break;
case 500: $text='Server error';break;
case 501: $text='Not implemented';break;
case 503: $text='Service unavailable';break;
}
}
$protocol = $_SERVER["SERVER_PROTOCOL"];
if ( ('HTTP/1.1' != $protocol) && ('HTTP/1.0' != $protocol) )
$protocol = 'HTTP/1.0';
header( "$protocol $code $text", true, $code );
trigger_notify('set_status_header', $code, $text);
}
/**
* Returns the category comment for rendering in html textual mode (subcatify)
* This method is called by a trigger_notify()
*
* @param string $desc
* @return string
*/
function render_category_literal_description($desc)
{
return strip_tags($desc, '
');
}
/**
* Add known menubar blocks.
* This method is called by a trigger_change()
*
* @param BlockManager[] $menu_ref_arr
*/
function register_default_menubar_blocks($menu_ref_arr)
{
$menu = & $menu_ref_arr[0];
if ($menu->get_id() != 'menubar')
return;
$menu->register_block( new RegisteredBlock( 'mbLinks', 'Links', 'piwigo'));
$menu->register_block( new RegisteredBlock( 'mbCategories', 'Albums', 'piwigo'));
$menu->register_block( new RegisteredBlock( 'mbTags', 'Related tags', 'piwigo'));
$menu->register_block( new RegisteredBlock( 'mbSpecials', 'Specials', 'piwigo'));
$menu->register_block( new RegisteredBlock( 'mbMenu', 'Menu', 'piwigo'));
$menu->register_block( new RegisteredBlock( 'mbIdentification', 'Identification', 'piwigo') );
}
/**
* Returns display name for an element.
* Returns 'name' if exists of name from 'file'.
*
* @param array $info at least file or name
* @return string
*/
function render_element_name($info)
{
if (!empty($info['name']))
{
return trigger_change('render_element_name', $info['name']);
}
return get_name_from_file($info['file']);
}
/**
* Returns display description for an element.
*
* @param array $info at least comment
* @param string $param used to identify the trigger
* @return string
*/
function render_element_description($info, $param='')
{
if (!empty($info['comment']))
{
return trigger_change('render_element_description', $info['comment'], $param);
}
return '';
}
/**
* Add info to the title of the thumbnail based on photo properties.
*
* @param array $info hit, rating_score, nb_comments
* @param string $title
* @param string $comment
* @return string
*/
function get_thumbnail_title($info, $title, $comment='')
{
global $conf, $user;
$details = array();
if (!empty($info['hit']))
{
$details[] = $info['hit'].' '.strtolower(l10n('Visits'));
}
if ($conf['rate'] and !empty($info['rating_score']))
{
$details[] = strtolower(l10n('Rating score')).' '.$info['rating_score'];
}
if (isset($info['nb_comments']) and $info['nb_comments'] != 0)
{
$details[] = l10n_dec('%d comment', '%d comments', $info['nb_comments']);
}
if (count($details) > 0)
{
$title.= ' ('.implode(', ', $details).')';
}
if (!empty($comment))
{
$comment = strip_tags($comment);
$title.= ' '.substr($comment, 0, 100).(strlen($comment) > 100 ? '...' : '');
}
$title = htmlspecialchars(strip_tags($title));
$title = trigger_change('get_thumbnail_title', $title, $info);
return $title;
}
/**
* Event handler to protect src image urls.
*
* @param string $url
* @param SrcImage $src_image
* @return string
*/
function get_src_image_url_protection_handler($url, $src_image)
{
return get_action_url($src_image->id, $src_image->is_original() ? 'e' : 'r', false);
}
/**
* Event handler to protect element urls.
*
* @param string $url
* @param array $infos id, path
* @return string
*/
function get_element_url_protection_handler($url, $infos)
{
global $conf;
if ('images'==$conf['original_url_protection'])
{// protect only images and not other file types (for example large movies that we don't want to send through our file proxy)
$ext = get_extension($infos['path']);
if (!in_array($ext, $conf['picture_ext']))
{
return $url;
}
}
return get_action_url($infos['id'], 'e', false);
}
/**
* Sends to the template all messages stored in $page and in the session.
*/
function flush_page_messages()
{
global $template, $page;
if ($template->get_template_vars('page_refresh') === null)
{
foreach (array('errors','infos','warnings') as $mode)
{
if (isset($_SESSION['page_'.$mode]))
{
$page[$mode] = array_merge($page[$mode], $_SESSION['page_'.$mode]);
unset($_SESSION['page_'.$mode]);
}
if (count($page[$mode]) != 0)
{
$template->assign($mode, $page[$mode]);
}
}
}
}
?>