FALSE, 'full' => FALSE);
// Initialize only once
if ($ready[$full ? 'full' : 'half']) {
return TRUE;
}
if (!$vars) {
$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 : NULL;
}
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 = NULL;
}
$init_err_msg = t('Unable to initialize embedded Gallery. You need to configure your embedded Gallery.',
array('@link' => url('admin/settings/gallery/install')));
if ((!$gallery_valid) || (!is_readable($embed_path))) {
if ($report_error) {
gallery_error($init_err_msg);
}
return FALSE;
}
include_once($embed_path);
// i18n url rewrite
$language = gallery_get_language($user);
if (module_exists('i18n')) {
$embed_uri = preg_replace('/index.php\?q=/', i18n_path('index.php?q=', $language), $embed_uri);
}
$params = array('embedUri' => $embed_uri,
'g2Uri' => $g2_uri,
'loginRedirect' => url('user/login', drupal_get_destination(), NULL, TRUE),
'activeUserId' => $uid,
'activeLanguage' => $language,
'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);
}
return FALSE;
}
else {
if ($report_error) {
gallery_error($init_err_msg, $ret, TRUE);
}
return FALSE;
}
}
if (!class_exists('GalleryEmbed') || !class_exists('GalleryCoreApi')) {
if ($report_error) {
gallery_error(t('Initialization seemed successful, but classes \'GalleryEmbed\'
and/or \'GalleryCoreApi\' are still not available.'));
}
return FALSE;
}
$ready['half'] = $full ? ($ready['full'] = TRUE) : TRUE;
return TRUE;
}
/**
* Function gallery_handle_request().
* (handleRequest extension with error handling)
*/
function gallery_handle_request() {
// Remove the language-prefix for G2
if (!empty($_GET['g2_path']) && module_exists('i18n')) {
i18n_get_lang_prefix($_GET['g2_path'], TRUE);
}
ob_start();
$result = GalleryEmbed::handleRequest();
$output = ob_get_contents();
ob_end_clean();
if ($output) {
if (!preg_match('%
\sError\s
%', $output)) {
// If $output does not contain 'Error
', this is an AJAX/Image callback
print $output;
exit();
}
else {
// Otherwise (on regular pages) $output means that an error occured
if (variable_get('gallery_error_redirect', 0) && user_access('search content')) {
drupal_set_message('The requested Gallery URL does not exist. The item may have been
moved or deleted. You can search for it below.', 'notice');
drupal_goto('search/gallery');
}
preg_match('%]*>(.*?)
%is', $output, $matches);
gallery_error(t('Error handling request (invalid request)
or the requested Gallery URL does not exist.'), $matches[1], TRUE);
return NULL;
}
}
return $result;
}
/**
* Function gallery_get_language().
* (get the language for $user)
*/
function gallery_get_language($user) {
// Default is the user-specific language
$language = $user->uid ? $user->language : '';
// If i18n is enabled, use that language instead
if (module_exists('i18n')) {
$language = i18n_get_lang();
}
// If there is still no language available, try to get the default one
if (empty($language)) {
$language = 'en';
if (module_exists('locale')) {
$result = db_query('SELECT locale FROM {locales_meta} WHERE isdefault = 1');
$language = db_result($result);
}
}
// Convert certain lang codes, e.g. 'esl/spa es' => 'es' or 'fr-ca' => 'fr'
return preg_replace(array('/([\w\/]+) ([a-z]{2,3})/i', '/([a-z]{2,3})-(\w+)/i'), array('${2}', '${1}'), $language);
}
/**
* Function gallery_list_themes().
* (retrieve all (active) themes from Gallery2)
*/
function gallery_list_themes() {
if (!_gallery_init()) {
return array();
}
// List of themes
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) {
// Only active themes
if (!empty($g2_themes[$themeid]['active'])) {
// Get theme details
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;
}
/**
* Function gallery_list_image_frames().
* (retrieve all image frames from Gallery2)
*/
function gallery_list_image_frames() {
if (!_gallery_init()) {
return array('none' => t('None'));
}
// List of available image frames
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;
}
/**
* Function gallery_generate_url().
*/
function gallery_generate_url($params, $html = TRUE, $full = TRUE) {
if (!_gallery_init(TRUE)) {
return '';
}
// Get URL generator and generate the url
$url_generator =& $GLOBALS['gallery']->getUrlGenerator();
if (!$url_generator) {
gallery_error(t('Error: UrlGenerator not available'));
return '';
}
$options = array();
$options['forceFullUrl'] = $full;
$options['htmlEntities'] = $html;
if (!isset($params['view'])) {
$params['view'] = 'core.ShowItem';
}
return $url_generator->generateUrl($params, $options);
}
/**
* Function gallery_album_tree().
*/
function gallery_album_tree($root = NULL, $depth = NULL, $uid = NULL) {
if (!_gallery_init(TRUE)) {
return array();
}
// If this is called for the Drupal guest user, pass in G2
// anonymous user id (otherwise fetchAlbumTree() returns nothing)
global $user;
if (!$uid && !$user->uid) {
list($ret, $uid) = GalleryCoreApi::getPluginParameter('module', 'core', 'id.anonymousUser');
if ($ret) {
return array();
}
}
// Fetch G2 album tree
list($ret, $tree) = GalleryCoreApi::fetchAlbumTree($root, $depth, $uid);
if ($ret) {
gallery_error(t('Error fetching album tree'), $ret);
return array();
}
return $tree;
}
/**
* Function gallery_item_details().
*/
function gallery_item_details($id, $verbose = FALSE) {
$details = array();
if (!_gallery_init(TRUE)) {
return $details;
}
// Load entity
list($ret, $entity) = GalleryCoreApi::loadEntitiesById($id);
if ($ret) {
gallery_error(t('Error fetching item details (entityId: :id)',
array(':id' => $id)), $ret);
return $details;
}
// Extract details
$details['g2type'] = $entity->entityType;
$details['g2owner'] = $entity->ownerId;
$details['g2parent'] = $entity->parentId;
// Drupal node properties (no g2 prefix)
$details['title'] = _gallery_htmlcharsdecode($entity->title);
$details['created'] = $entity->creationTimestamp;
if ($verbose) {
$details['g2description'] = _gallery_htmlcharsdecode($entity->description);
$details['g2summary'] = _gallery_htmlcharsdecode($entity->summary);
$details['g2keywords'] = $entity->keywords;
$details['g2theme'] = isset($entity->theme) ? $entity->theme : NULL;
}
return $details;
}
/**
* Function gallery_db_query().
*/
function gallery_db_query($query, $data = NULL) {
if (!_gallery_init(TRUE)) {
return FALSE;
}
// Perform query
list ($ret, $search) = $GLOBALS['gallery']->search($query, $data);
if ($ret) {
return FALSE;
}
$results = array();
while ($result = $search->nextResult()) {
$results += $result;
}
return $results;
}
/**
* Function gallery_flush_entity_cache().
*/
function gallery_flush_entity_cache() {
if (!_gallery_init()) {
return FALSE;
}
// Flush entity cache
$platform =& $GLOBALS['gallery']->getPlatform();
$cache_basedir = $GLOBALS['gallery']->getConfig('data.gallery.cache');
$cache_dir = $cache_basedir .'entity';
if ($platform->file_exists($cache_dir)) {
if (!$platform->recursiveRmDir($cache_dir)) {
return FALSE;
}
}
return TRUE;
}
/**
* Function 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);
}
}
/**
* Function gallery_set_css().
*/
function gallery_set_css($css) {
static $css_memory = array();
if (count($css)) {
// Handle css styles and includes
foreach ($css as $include) {
if (!in_array(($md5 = md5($include)), $css_memory)) {
$css_memory[] = $md5;
drupal_set_html_head($include);
}
}
}
}
/**
* Function gallery_set_javascript().
* (druapl_add_js() ensures proper cascading of included G2 javascript)
*/
function gallery_set_javascript($javascript) {
static $js_memory = array();
if (!empty($javascript)) {
// Handle inline javascript and includes
$files = preg_grep('/