Newer
Older
<?php
// $Id$
/**
* gallery.module : gallery_base.inc
* Base functions
*/
define(GALLERY_NO_EMBED_THEME, -1);
define(GALLERY_ERROR_WATCHDOG, 1);
define(GALLERY_ERROR_BROWSER, 2);
define(GALLERY_ERROR_VERBOSE, 3);
define(GALLERY_SEVERITY_ERROR, -1);
define(GALLERY_SEVERITY_WARNING, -2);
define(GALLERY_SEVERITY_ADVISE, -3);
define(GALLERY_SEVERITY_UNKNOWN, -4);
define(GALLERY_DEBUGTRACE, TRUE);
/**
* Login user into embedded gallery
*/
function gallery_login() {
_gallery_init();
}
/**
* End user session
function gallery_logout() {
if (variable_get('gallery_valid', FALSE)) {
require_once(variable_get('gallery_dir', './gallery2/') .'embed.php');
GalleryEmbed::logout();
}
}
* Initialize embedded gallery
function _gallery_init($full = FALSE, $vars = NULL, $report_error = TRUE) {
Thilo Viereck
committed
static $ready = array('half' => FALSE, 'full' => FALSE);
Thilo Viereck
committed
if ($ready[$full ? 'full' : 'half']) {
return TRUE;
$embed_path = variable_get('gallery_dir', './gallery2/') .'embed.php';
$g2_uri = variable_get('gallery_uri', '/gallery2/');
$embed_uri = variable_get('gallery_embed_uri', '?q=gallery');
$gallery_valid = variable_get('gallery_valid', 0);
$uid = ($user->uid > 0) ? $user->uid : '';
}
else {
$embed_path = $vars['gallery_dir'] .'embed.php';
$g2_uri = $vars['gallery_uri'];
$embed_uri = $vars['gallery_embed_uri'];
$gallery_valid = $vars['gallery_valid'];
$uid = '';
$init_err_msg = t('Unable to initialize embedded Gallery. You need to <a href="@link"> configure your embedded Gallery</a>.',
array('@link' => url('admin/settings/gallery/install')));
if ((!$gallery_valid) || (!is_readable($embed_path))) {
Thilo Viereck
committed
return FALSE;
include_once($embed_path);
$params = array('embedUri' => $embed_uri,
'g2Uri' => $g2_uri,
'loginRedirect' => url('user/login', NULL, NULL, TRUE),
'activeUserId' => $uid,
'activeLanguage' => gallery_get_language($user),
'fullInit' => $full,
'apiVersion' => array(1, 2));
$ret = GalleryEmbed::init($params);
if ($ret) {
if ($ret->getErrorCode() & ERROR_PLUGIN_VERSION_MISMATCH) {
if ($report_error) {
gallery_error($vars ? t('Embedding API version is incompatible.') : $init_err_msg, $ret, TRUE);
}
Thilo Viereck
committed
return FALSE;
if ($report_error) {
gallery_error($init_err_msg, $ret, TRUE);
}
Thilo Viereck
committed
return FALSE;
Thilo Viereck
committed
if (!class_exists('GalleryEmbed') || !class_exists('GalleryCoreApi')) {
if ($report_error) {
gallery_error(t('Classes \'GalleryEmbed\' and/or \'GalleryCoreApi\' are not available,
although initialization seemed successful.'));
Thilo Viereck
committed
}
return FALSE;
}
$ready['half'] = $full ? ($ready['full'] = TRUE) : TRUE;
}
/**
* handleRequest extension with error handling
*/
function gallery_handle_request() {
ob_start();
$result = GalleryEmbed::handleRequest();
$output = ob_get_contents();
ob_end_clean();
if ($output) {
if (!preg_match('%<h2>\sError\s</h2>%', $output)) {
// If $output does not contain '<h2>Error</h2>', it means that
// this page is an AJAX/Image callback and it printed out a result.
print $output;
exit();
}
else {
// Otherwise (on regular pages) $output means that an error occured
preg_match('%<div id="giStackTrace" [^>]*>(.*?)</div>%is', $output, $matches);
gallery_error(t('Error handling request (invalid request)<br />or the requested Gallery URL does not exist.'), $matches[1], TRUE);
return $result;
}
/**
* Get the language for $user
*/
function gallery_get_language($user) {
$language = $user->language;
if ($user->uid == 0 || empty($language)) {
if (module_exists('i18n')) {
elseif (module_exists('locale')) {
$result = db_query('SELECT locale FROM {locales_meta} WHERE isdefault = 1');
$row = db_fetch_object($result);
// Convert certain lang codes, e.g. 'esl/spa es' => 'es' or 'fr-ca' => 'fr'
$language = preg_replace(array('/([\w\/]+) ([a-z]{2,3})/i', '/([a-z]{2,3})-(\w+)/i'), array('${2}', '${1}'), $language);
* Retrieve all (active) themes from Gallery2
function gallery_get_themes($all = FALSE) {
list($ret, $g2_themes) = GalleryCoreApi::fetchPluginStatus('theme', TRUE);
if ($ret) {
gallery_error(t('Error retrieving theme list'), $ret);
return array();
}
$themes = array();
foreach (array_keys($g2_themes) as $themeid) {
if (!empty($g2_themes[$themeid]['active']) || $all) {
list($ret, $theme) = GalleryCoreApi::loadPlugin('theme', $themeid, TRUE);
if ($ret) {
gallery_error(t('Error getting theme (:themeid) details',
array(':themeid' => $themeid)), $ret);
return array();
}
$themes[$themeid] = $theme->getName();
}
}
return $themes;
/**
* Retrieve all image frames from Gallery2
*/
function gallery_get_image_frames() {
_gallery_init();
list($ret, $imageframe) = GalleryCoreApi::newFactoryInstance('ImageFrameInterface_1_1');
if ($ret || !isset($imageframe)) {
return array('none' => t('None'));
}
list($ret, $list) = $imageframe->getImageFrameList();
if ($ret) {
return array('none' => t('None'));
}
return $list;
/**
* Implementation of gallery_generate_url
*/
function gallery_generate_url($params, $html = TRUE, $full = TRUE) {
$options = array();
$options['forceFullUrl'] = $full;
$options['htmlEntities'] = $html;
$url_generator =& $GLOBALS['gallery']->getUrlGenerator();
if (!$url_generator) {
gallery_error(t('Error: UrlGenerator not available'));
return '';
}
return $url_generator->generateUrl($params, $options);
}
/**
* Implementation of gallery_item_details
*/
function gallery_item_details($id, $verbose = FALSE) {
$details = array();
list($ret, $entity) = GalleryCoreApi::loadEntitiesById($id);
if ($ret) {
gallery_error(t('Error fetching album details (entityId: :id)',
array(':id' => $id)), $ret);
return $details;
}
$details['type'] = $entity->entityType;
$details['title'] = $entity->title;
$details['owner'] = $entity->ownerId;
$details['parent'] = $entity->parentId;
if ($verbose) {
$details['description'] = $entity->description;
$details['summary'] = $entity->summary;
$details['keywords'] = $entity->keywords;
}
return $details;
/**
* Implementation of gallery_set_head
*/
function gallery_set_head($html, $settitle = FALSE) {
if (!empty($html)) {
list($title, $css, $javascript) = GalleryEmbed::parseHead($html);
if ($settitle) {
drupal_set_title($title);
}
gallery_set_css($css);
gallery_set_javascript($javascript);
}
}
/**
* Implementation of gallery_set_css
*/
function gallery_set_css($css) {
global $base_url;
static $css_memory = array();
if (count($css)) {
$css = preg_replace('/<link(.*?)href="([^"]*)"(.*?)\/>/i', '${2}', $css);
$css = preg_replace(array('#^'. $base_url .'#', '#^'. base_path() .'#', '#^/#'), '', $css);
foreach ($css as $include) {
if (!in_array(($md5 = md5($include)), $css_memory)) {
$css_memory[] = $md5;
if (substr($include, 0, 6) == '<style') {
// drupal_add_css() does not support inline styles
drupal_set_html_head($include);
}
elseif (substr($include, 0, 4) == 'http') {
// drupal_add_css() does not support external paths
drupal_set_html_head('<style type="text/css" media="all">@import "'. $include .'";</style>' ."\n");
}
else {
// Gallery's css must always be added first to allow overriding from the module(s)
drupal_add_css($include, 'module', 'all', FALSE);
}
}
}
}
}
/**
* Implementation of gallery_set_javascript
* (druapl_add_js() ensures proper cascading of included G2 javascript)
*/
function gallery_set_javascript($javascript) {
global $base_url;
static $js_memory = array();
if (!empty($javascript)) {
$files = preg_grep('/<script(.*?)src=/i', $javascript);
$inline = array_diff($javascript, $files);
$inline = preg_replace('/<script([^>]*)>(.*?)<\/script>/is', '\2', $inline);
drupal_add_js(implode("\n", $inline), 'inline');
$files = preg_replace('/<script(.*?)src="([^"]*)"([^>]*)>(.*?)<\/script>/i', '${2}', $files);
$files = preg_replace(array('#^'. $base_url .'#', '#^'. base_path() .'#', '#^/#'), '', $files);
foreach ($files as $include) {
if (!in_array(($md5 = md5($include)), $js_memory)) {
$js_memory[] = $md5;
if (substr($include, 0, 4) == 'http') {
// drupal_add_js() does not support external paths
drupal_set_html_head('<script type="text/javascript" src="'. $include .'"></script>' ."\n");
}
else {
drupal_add_js($include);
}
}
}
}
}
/**
* Split an image block result into individual images
*/
function _gallery_split_imageblock($html) {
/**
* From http://uk.php.net/manual/en/function.preg-split.php
* Split the html from image block into <...> parts
*/
$pattern = '/(<(?:[^<>]+(?:"[^"]*"|\'[^\']*\')?)+>)/';
$html_array = preg_split($pattern, trim($html), -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
$ndx = 0;
$images = array();
$image_html = '';
// Iterate through this array and combine again, but on a per-image basis
foreach ($html_array as $value) {
$value = trim($value);
$image_html .= $value;
if (!strcmp($value, '<div class="one-image">')) {
// Found the opening <div> for the image
elseif (!strncmp($value, '<div', 4)) {
// Found a <div> but not the main image one (eg a frame)
elseif (!strcmp($value, '</div>')) {
// Found a </div> but check if it's for the main image or a subcomponent (eg frame)
// This must be the closing div for "one-image" so move to next image
$images[] = $image_html;
$image_html = '';
}
}
}
/*
* --------------------------------------------------------------------------
* Error, Debug and Status Functions
* --------------------------------------------------------------------------
*/
function gallery_error($msg, $ret = NULL, $serious = FALSE) {
$error_mode = variable_get('gallery_error_mode', array(GALLERY_ERROR_WATCHDOG));
$admin = user_access('administer site configuration');
$report = $admin && variable_get('gallery_report', 1);
$debug_info = array();
if (in_array(GALLERY_ERROR_VERBOSE, $error_mode) || variable_get('gallery_debug', 0) || $admin) {
$msg = $ret ? (is_object($ret) ? ($msg .'<br />'. $ret->getAsHtml()) : $ret) : $msg;
if (GALLERY_DEBUGTRACE && function_exists('debug_backtrace')) {
$trace = debug_backtrace();
$source = t('Error in function \':func()\' (:file::line):<br />',
array(':func' => $trace[1]['function'], ':file' => basename($trace[0]['file']), ':line' => $trace[0]['line']));
$message = $source .'<ul><li>'. $msg .'</li></ul>';
$debug_info = array('Debug Trace' => $trace);
}
}
$message = !empty($message) ? $message : $msg;
if (variable_get('gallery_debug', 0) && $admin) {
if ($report) {
_gallery_report_error($debug_info);
}
drupal_set_message($message, 'error');
return;
}
if (in_array(GALLERY_ERROR_BROWSER, $error_mode)) {
if ($report) {
_gallery_report_error($debug_info);
}
drupal_set_message($message, 'error');
}
elseif ($serious) {
if ($report) {
_gallery_report_error($debug_info);
}
drupal_set_message($admin ? $message : t('Embedded Gallery2 is not available or requested Gallery URL does not exist.'), 'error');
if (in_array(GALLERY_ERROR_WATCHDOG, $error_mode)) {
watchdog('gallery', $message, WATCHDOG_ERROR);
function _gallery_report_error($report = array()) {
require_once(drupal_get_path('module', 'gallery') .'/gallery_help.inc');
require_once(drupal_get_path('module', 'gallery') .'/gallery_report.inc');
drupal_set_message(_gallery_report_help(), 'error');
_gallery_report(FALSE, $report, TRUE);
}
function gallery_debug($array, $label = 'Gallery Debug') {
if (variable_get('gallery_debug', 0) && user_access('administer site configuration')) {
drupal_set_message('<strong>'. $label .':</strong><br />'. nl2br(htmlspecialchars(print_r($array, TRUE))), 'error');
}
}
function gallery_set_status($status = array(), $reset = FALSE) {
$status_array = $status;
if (!$reset) {
$status_array = unserialize(variable_get('gallery_status', serialize(array())));
foreach ($status as $key => $value) {
if (!empty($value)) {
$status_array[$key] = $value;
}
elseif (isset($status_array[$key])) {
unset($status_array[$key]);
}
}
}
variable_set('gallery_status', serialize($status_array));
}
function gallery_get_status() {
return unserialize(variable_get('gallery_status', serialize(array())));
}
function gallery_version() {
if (!_gallery_init(FALSE)) {
list($core_major, $core_minor) = GalleryCoreApi::getApiVersion();
list($embed_major, $embed_minor) = GalleryEmbed::getApiVersion();
$version = array(
'core' => array('major' => $core_major, 'minor' => $core_minor),
'embed' => array('major' => $embed_major, 'minor' => $embed_minor)
);
// Update version in status messages
$status = array('version' => array(
'title' => t('Gallery2 API version'),
'info' => "$core_major.$core_minor / $embed_major.$embed_minor")
);
gallery_set_status($status);
return $version;