- task: sync with latest Drupal-5 branch changes
[project/gallery.git] / gallery_base.inc
CommitLineData
c15e9178 1<?php
2// $Id$
3
4/**
5 * gallery.module : gallery_base.inc
6 * Base functions
7 */
088bea1c
TW
8
9define(GALLERY_NO_EMBED_THEME, -1);
10
11define(GALLERY_ERROR_WATCHDOG, 1);
12define(GALLERY_ERROR_BROWSER, 2);
13define(GALLERY_ERROR_VERBOSE, 3);
14
15/**
16 * Login user into embedded gallery
17 */
18function gallery_login() {
19 _gallery_init();
20}
21
22/**
23 * End user session
c15e9178 24 */
088bea1c
TW
25function gallery_logout() {
26 if (variable_get('gallery_valid', FALSE)) {
27 require_once(variable_get('gallery_dir', './gallery2/') . 'embed.php');
28 GalleryEmbed::logout();
29 }
30}
31
c15e9178 32/**
088bea1c 33 * Initialize embedded gallery
c15e9178 34 */
088bea1c 35function _gallery_init($full = FALSE, $vars = NULL) {
c15e9178 36 global $user;
088bea1c
TW
37 static $ready = FALSE;
38
39 // initialize only once
40 if ($ready) {
41 return $ready;
42 }
c15e9178 43
44 if (!$vars) {
45 $embedPath = variable_get('gallery_dir', './gallery2/') . 'embed.php';
46 $g2Uri = variable_get('gallery_uri', '/gallery2/');
4fb8e101 47 $embedUri = variable_get('gallery_embed_uri', '?q=gallery');
c15e9178 48 $gallery_valid = variable_get('gallery_valid', 0);
4fb8e101 49 $uid = ($user->uid > 0) ? $user->uid : '';
c15e9178 50 } else {
51 $embedPath = $vars['gallery_dir'] . 'embed.php';
52 $g2Uri = $vars['gallery_uri'];
4fb8e101
TW
53 $embedUri = $vars['gallery_embed_uri'];
54 $gallery_valid = $vars['gallery_valid'];
55 $uid = '';
c15e9178 56 }
088bea1c
TW
57
58 $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')));
c15e9178 59
18807c9c 60 if ((!$gallery_valid) || (!is_readable($embedPath))) {
088bea1c
TW
61 gallery_error($init_err_msg, NULL, TRUE);
62 return $ready;
c15e9178 63 }
64
65 include_once($embedPath);
4fb8e101 66
c15e9178 67 $params = array('embedUri' => $embedUri,
68 'g2Uri' => $g2Uri,
088bea1c
TW
69 'loginRedirect' => url('user/login', NULL, NULL, TRUE),
70 'activeUserId' => $uid,
c15e9178 71 'activeLanguage' => gallery_get_language($user),
088bea1c
TW
72 'fullInit' => $full,
73 'apiVersion' => array(1,2));
c15e9178 74
75 $ret = GalleryEmbed::init($params);
088bea1c
TW
76 if ($ret) {
77 if ($ret->getErrorCode() & ERROR_PLUGIN_VERSION_MISMATCH) {
78 gallery_error($vars ? t('Embedding API version is incompatible') : $init_err_msg, $ret, TRUE);
79 return $ready;
c15e9178 80 }
088bea1c
TW
81 else {
82 gallery_error($init_err_msg, $ret, TRUE);
83 return $ready;
c15e9178 84 }
088bea1c
TW
85 }
86
87 $ready = TRUE;
88
89 return $ready;
90}
91
92/**
93 * handleRequest extension with error handling
94 */
95function gallery_handle_request() {
96 ob_start();
97 $result = GalleryEmbed::handleRequest();
98 $output = ob_get_contents();
99 ob_end_clean();
100
101 if ($output) {
102 preg_match('%<div id="giStackTrace" [^>]*>(.*?)</div>%is', $output, $matches);
4fb8e101
TW
103 gallery_error(t('Error handling request (invalid request)<br />or the requested Gallery URL does not exist.'), $matches[1]);
104 return NULL;
105 }
106
107 /*
108 // switch theme
109 $themeid = variable_get('gallery_theme', GALLERY_NO_EMBED_THEME);
110 if ($themeid != GALLERY_NO_EMBED_THEME) {
111 list ($ret, $theme) = GalleryCoreApi::loadPlugin('theme', $themeid, false, true);
112 if ($ret) {
113 gallery_error(t('Error switching theme'), $ret);
114 return NULL;
115 }
116 }
117 //////
118 $ret = GalleryCoreApi::setPluginParameter('module', 'core', 'theme', $themeid);
119 if ($ret) {
120 gallery_error(t('Error switching theme'), $ret);
088bea1c
TW
121 return NULL;
122 }
4fb8e101 123 */
088bea1c
TW
124
125 return $result;
126}
127
128/**
129 * Get the language for $user
130 */
131function gallery_get_language($user) {
132 if (($user->uid == 0 || !($user->language))) {
133 if (module_exists('i18n')) {
134 return i18n_get_lang();
135 }
136 else if (module_exists('locale')) {
137 $result = db_query('SELECT locale FROM {locales_meta} WHERE isdefault = 1');
138 $row = db_fetch_object($result);
139 return $row->locale;
c15e9178 140 }
141 }
088bea1c
TW
142
143 return $user->language;
c15e9178 144}
145
146/**
c15e9178 147 * Include head information with check made for uniqueness (see drupal_add_js)
148 */
088bea1c
TW
149function gallery_set_html_head($info, $unique = TRUE) {
150 static $sent = array();
c15e9178 151 if ($unique) {
088bea1c 152 // only set html head if this info has not been sent before.
c15e9178 153 $hash_info = md5($info);
154 if (!isset($sent[$hash_info])) {
155 drupal_set_html_head($info);
088bea1c
TW
156 $sent[$hash_info] = TRUE;
157 }
c15e9178 158 } else {
159 drupal_set_html_head($info);
160 }
161}
162
163/**
088bea1c 164 * Retrieve all (active) themes from Gallery2
c15e9178 165 */
088bea1c
TW
166function gallery_get_themes($all = FALSE) {
167 // list of themes
168 list ($ret, $g2_themes) = GalleryCoreApi::fetchPluginStatus('theme', TRUE);
169 if ($ret) {
170 gallery_error(t('Error retrieving theme list'), $ret);
171 return array();
172 }
173
174 $themes = array();
175 foreach (array_keys($g2_themes) as $themeid) {
176 // only active themes
177 if (!empty($g2_themes[$themeid]['active']) || $all) {
178 // get theme details
179 list ($ret, $theme) = GalleryCoreApi::loadPlugin('theme', $themeid, TRUE);
180 if ($ret) {
181 gallery_error(t('Error getting theme (:themeid) details',
182 array(':themeid' => $themeid)), $ret);
183 return array();
184 }
185 $themes[$themeid] = $theme->getName();
186 }
187 }
188
189 return $themes;
190}
191
192/**
193 * Implementation of gallery_generate_url
194 */
195function gallery_generate_url($params, $html = TRUE, $full = TRUE) {
196 $options = array();
197 $options['forceFullUrl'] = $full;
198 $options['htmlEntities'] = $html;
199
200 $urlGenerator =& $GLOBALS['gallery']->getUrlGenerator();
201 if (!$urlGenerator) {
202 gallery_error(t('Error: UrlGenerator not available'));
203 return '';
204 }
205
206 return $urlGenerator->generateUrl($params, $options);
207}
208
209/**
210 * Implementation of gallery_item_details
211 */
212function gallery_item_details($id, $verbose = FALSE) {
213 $details = array();
214 // load entity
215 list ($ret, $entity) = GalleryCoreApi::loadEntitiesById($id);
216 if ($ret) {
217 gallery_error(t('Error fetching album details (entityId: :id)',
218 array(':id' => $id)), $ret);
219 return $details;
220 }
221 // extract details
222 $details['type'] = $entity->entityType;
223 $details['title'] = $entity->title;
224 $details['owner'] = $entity->ownerId;
225 $details['parent'] = $entity->parentId;
226
227 if ($verbose) {
228 $details['description'] = $entity->description;
229 $details['summary'] = $entity->summary;
230 $details['keywords'] = $entity->keywords;
231 }
232
233 return $details;
c15e9178 234}
235
55260521 236/**
237 * Split an image block result into individual images
55260521 238 */
239function _gallery_split_imageblock($html) {
240 /**
241 * From http://uk.php.net/manual/en/function.preg-split.php
242 * Split the html from image block into <...> parts
243 */
244 $pattern = '/(<(?:[^<>]+(?:"[^"]*"|\'[^\']*\')?)+>)/';
088bea1c 245 $html_array = preg_split ($pattern, trim ($html), -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
55260521 246
247 $ndx = 0;
248 $images = array();
249 $image_html = '';
088bea1c 250 // iterate through this array and combine again, but on a per-image basis
55260521 251 foreach ($html_array as $value) {
252 $value = trim($value);
253 $image_html .= $value;
254 if (!strcmp($value, '<div class="one-image">')) {
088bea1c 255 // found the opening <div> for the image
55260521 256 $open_divs = 0;
257 } else if (!strncmp($value, '<div', 4)) {
088bea1c 258 // found a <div> but not the main image one (eg a frame)
55260521 259 $open_divs++;
260 } else if (!strcmp($value, '</div>')) {
088bea1c 261 // found a </div> but check if it's for the main image or a subcomponent (eg frame)
55260521 262 if ($open_divs>0) {
263 $open_divs--;
264 } else {
088bea1c 265 // this must be the closing div for "one-image" so move to next image
55260521 266 $images[] = $image_html;
267 $image_html = '';
268 }
269 }
270 }
088bea1c 271
55260521 272 return $images;
273}
274
c15e9178 275/*
276 * --------------------------------------------------------------------------
088bea1c 277 * Error, Debug and Status Functions
c15e9178 278 * --------------------------------------------------------------------------
279 */
088bea1c
TW
280
281function gallery_error($msg, $ret = NULL, $force = FALSE) {
282 $error_mode = variable_get('gallery_error_mode', array(GALLERY_ERROR_WATCHDOG));
283 // verbose error messages
4fb8e101 284 if (in_array(GALLERY_ERROR_VERBOSE, $error_mode) || variable_get('gallery_debug', 0)) {
088bea1c
TW
285 $msg = $ret ? (is_object($ret) ? ($msg . '<br />' . $ret->getAsHtml()) : $ret) : $msg;
286 if (function_exists('debug_backtrace')) {
287 $trace = debug_backtrace();
288 $source = t('Error in function \':func()\' (:file::line):<br />',
289 array(':func' => $trace[1]['function'], ':file' => basename($trace[0]['file']), ':line' => $trace[0]['line']));
290 $message = $source . '<ul><li>' . $msg . '</li></ul>';
291 }
292 }
293 $message = !empty($message) ? $message : $msg;
294 // debug output (skip watchdog)
295 if (variable_get('gallery_debug', 0)) {
296 drupal_set_message($message, 'error');
297 return;
298 }
299 // error output to browser
300 if (in_array(GALLERY_ERROR_BROWSER, $error_mode)) {
301 drupal_set_message($message, 'error');
302 }
303 else if ($force) {
304 drupal_set_message(user_access('administer site configuration') ? $message : t('Embedded Gallery2 is not available.'), 'error');
305 }
306 // error output to watchdog
307 if (in_array(GALLERY_ERROR_WATCHDOG, $error_mode)) {
308 watchdog('gallery', $message, WATCHDOG_ERROR);
c15e9178 309 }
088bea1c 310}
c15e9178 311
088bea1c
TW
312function gallery_debug($array, $label = 'Gallery Debug') {
313 if (variable_get('gallery_debug', 0) && user_access('administer site configuration')) {
314 drupal_set_message('<strong>' . $label . ':</strong><br />' . nl2br(htmlspecialchars(print_r($array, TRUE))), 'error');
315 }
316}
317
318function gallery_set_status($status = array(), $reset = FALSE) {
319 $status_array = $status;
320 if (!$reset) {
321 $status_array = unserialize(variable_get('gallery_status', serialize(array())));
322 foreach ($status as $key => $value) {
323 $status_array[$key] = $value;
324 }
325 }
326
327 variable_set('gallery_status', serialize($status_array));
328}
329
330function gallery_get_status() {
331 return unserialize(variable_get('gallery_status', serialize(array())));
332}
333
334function gallery_format_status($status = array(), $title = 'Gallery message(s):') {
335 $message = $title . '<ul>';
336 if (count($status)) {
337 foreach ($status as $item) {
338 $message .= '<li>' . t($item['title']);
339 if (isset($item['success']) && $item['success']) {
340 $message .= ' <span class=\'g2_embed_success\'>' . t('Success') . '</span>';
341 } else if (isset($item['warning']) && $item['warning']) {
342 $message .= ' <span class=\'g2_embed_warning\'>' . t('Warning') . '</span>';
343 $message .= '<ul><li>' . t($item['notice']) . '</li></ul>';
344 } else if (isset($item['error']) && $item['error']) {
345 $message .= ' <span class=\'g2_embed_error\'>' . t('Error') . '</span>';
346 $message .= '<ul><li>' . t($item['notice']) . '</li></ul>';
347 } else if (isset($item['advise']) && $item['advise']) {
348 $message .= ' <span class=\'g2_embed_warning\'>' . t('Advisory') . '</span>';
349 $message .= '<ul><li>' . t($item['notice']) . '</li></ul>';
350 } else if (isset($item['message']) && $item['message']) {
351 $message .= ': ' . t($item['message']);
352 }
353 }
c15e9178 354 }
088bea1c
TW
355 else {
356 $message = t('Status not available');
357 }
358 $message .= '</ul>';
359
360 return $message;
361}
c15e9178 362
088bea1c 363function gallery_version($setstatus = TRUE) {
4fb8e101 364 if (!_gallery_init(FALSE)) {
088bea1c 365 return '';
c15e9178 366 }
088bea1c
TW
367
368 list ($core_major, $core_minor) = GalleryCoreApi::getApiVersion();
369 list ($embed_major, $embed_minor) = GalleryEmbed::getApiVersion();
370 $version = "$core_major.$core_minor / $embed_major.$embed_minor";
371
372 if ($setstatus) {
373 $status = array('version' => array('title' => t('Gallery2 API version'), 'message' => $version));
374 gallery_set_status($status);
375 }
376
377 GalleryEmbed::done();
378 return $version;
c15e9178 379}
088bea1c
TW
380
381?>