- very early D6 version (Oct-01, 2007)
[project/gallery.git] / gallery.module
1 <?php
2 // $Id$
3
4 require_once(drupal_get_path('module', 'gallery') .'/gallery_base.inc');
5
6 // Default variable values
7 define('GALLERY_IMAGEBLOCK_SIZE_METHOD_DEFAULT', 'maxsize');
8 define('GALLERY_IMAGEBLOCK_SIZE_DEFAULT', 150);
9 define('GALLERY_GRID_SIZE_METHOD_DEFAULT', 'maxsize');
10 define('GALLERY_GRID_SIZE_DEFAULT', 90);
11 define('GALLERY_SEARCH_SIZE_METHOD_DEFAULT', 'maxsize');
12 define('GALLERY_SEARCH_SIZE_DEFAULT', 150);
13 define('GALLERY_FILTER_MAXSIZE_DEFAULT', 150);
14 define('GALLERY_FILTER_EXACTSIZE_DEFAULT', '');
15
16 /**
17 * Implementation of hook_perm().
18 */
19 function gallery_perm() {
20 return array('administer gallery settings', 'access gallery', 'access standalone g2image');
21 }
22
23 /**
24 * Implementation of hook_theme().
25 */
26 function gallery_theme() {
27 return array(
28 'gallery_module_status_message' => array(
29 'arguments' => array('status' => NULL),
30 ),
31 'gallery_severity_message' => array(
32 'arguments' => array('severity' => NULL),
33 ),
34 'gallery_plugin_status_message' => array(
35 'arguments' => array('status' => NULL, 'invert' => FALSE),
36 ),
37 'gallery_severity_status_message' => array(
38 'arguments' => array('severity' => NULL, 'status' => NULL, 'full_msg' => FALSE, 'invert' => FALSE),
39 ),
40 'gallery_report' => array(
41 'arguments' => array('report' => FALSE),
42 )
43 );
44 }
45
46 /**
47 * Implementation of hook_init().
48 */
49 function gallery_init() {
50 global $custom_theme;
51 // Include CSS
52 drupal_add_css(drupal_get_path('module', 'gallery') .'/gallery.css');
53 drupal_add_css(drupal_get_path('module', 'gallery') .'/gallery_filter.css');
54 // Switch theme for gallery pages
55 if (arg(0) == 'gallery' && (($theme = variable_get('gallery_page_theme', NULL)) != 'default')) {
56 $custom_theme = $theme;
57 init_theme();
58 }
59 }
60
61 /**
62 * Implementation of hook_menu().
63 */
64 function gallery_menu() {
65 $items = array();
66 $gallery_valid = variable_get('gallery_valid', 0);
67 if ($gallery_valid) {
68 $items['gallery'] = array(
69 'title' => 'Gallery',
70 'description' => 'Visit your embedded Gallery2.',
71 'access callback' => 'user_access',
72 'access arguments' => array('access gallery'),
73 'page callback' => variable_get('gallery_page_callback', 'gallery_page')
74 );
75 }
76 // Settings / General administration
77 $items['admin/settings/gallery'] = array(
78 'title' => 'Gallery settings',
79 'description' => 'Configure settings for embedding Gallery2 into Drupal.',
80 'access callback' => 'user_access',
81 'access arguments' => array('administer gallery settings'),
82 'file' => $gallery_valid ? 'gallery_settings.inc' : 'gallery_install.inc',
83 'page callback' => 'drupal_get_form',
84 'page arguments' => $gallery_valid ? array('_gallery_settings_general') : array('_gallery_install')
85 );
86 $items['admin/settings/gallery/install'] = array(
87 'title' => 'Install',
88 'access callback' => 'user_access',
89 'access arguments' => array('administer gallery settings'),
90 'file' => 'gallery_install.inc',
91 'page callback' => 'drupal_get_form',
92 'page arguments' => $gallery_valid ? array('_gallery_install_status') : array('_gallery_install'),
93 'type' => $gallery_valid ? MENU_LOCAL_TASK : MENU_DEFAULT_LOCAL_TASK,
94 'weight' => 0
95 );
96 if ($gallery_valid) {
97 $items['admin/settings/gallery/general'] = array(
98 'title' => 'General',
99 'type' => MENU_DEFAULT_LOCAL_TASK,
100 'weight' => 1
101 );
102 $items['admin/settings/gallery/filter'] = array(
103 'title' => 'Filter',
104 'access callback' => 'user_access',
105 'access arguments' => array('administer gallery settings'),
106 'file' => 'gallery_settings.inc',
107 'page callback' => 'drupal_get_form',
108 'page arguments' => array('_gallery_settings_filter'),
109 'type' => MENU_LOCAL_TASK,
110 'weight' => 2
111 );
112 $items['admin/settings/gallery/g2image'] = array(
113 'title' => 'G2Image',
114 'access callback' => 'user_access',
115 'access arguments' => array('administer gallery settings'),
116 'file' => 'gallery_settings.inc',
117 'page callback' => 'drupal_get_form',
118 'page arguments' => array('_gallery_settings_g2image'),
119 'type' => MENU_LOCAL_TASK,
120 'weight' => 3
121 );
122 if (module_exists('search')) {
123 $items['admin/settings/gallery/search'] = array(
124 'title' => 'Search',
125 'access callback' => 'user_access',
126 'access arguments' => array('administer gallery settings'),
127 'file' => 'gallery_settings.inc',
128 'page callback' => 'drupal_get_form',
129 'page arguments' => array('_gallery_settings_search'),
130 'type' => MENU_LOCAL_TASK,
131 'weight' => 4
132 );
133 }
134 $items['admin/settings/gallery/report'] = array(
135 'access callback' => 'user_access',
136 'access arguments' => array('administer site configuration'),
137 'file' => 'gallery_report.inc',
138 'page callback' => '_gallery_report',
139 'page arguments' => array(FALSE),
140 'type' => MENU_CALLBACK
141 );
142 $items['admin/settings/gallery/report/download'] = array(
143 'access callback' => 'user_access',
144 'access arguments' => array('administer site configuration'),
145 'file' => 'gallery_report.inc',
146 'page callback' => '_gallery_report',
147 'page arguments' => array(TRUE),
148 'type' => MENU_CALLBACK
149 );
150 // User administration
151 $items['admin/user/gallery'] = array(
152 'title' => 'Gallery users',
153 'description' => 'Gallery2 user integration and synchronization.',
154 'access callback' => 'user_access',
155 'access arguments' => array('administer users'),
156 'file' => 'gallery_user_admin.inc',
157 'page callback' => '_gallery_user_users'
158 );
159 $items['admin/user/gallery/users'] = array(
160 'title' => 'User Status',
161 'type' => MENU_DEFAULT_LOCAL_TASK,
162 'weight' => 0
163 );
164 $items['admin/user/gallery/advanced'] = array(
165 'title' => 'Advanced Sync',
166 'access callback' => 'user_access',
167 'access arguments' => array('administer users'),
168 'file' => 'gallery_user_admin.inc',
169 'page callback' => 'drupal_get_form',
170 'page arguments' => array('_gallery_user_advanced'),
171 'type' => MENU_LOCAL_TASK,
172 'weight' => 1
173 );
174 $items['admin/user/gallery/settings'] = array(
175 'title' => 'User Settings',
176 'access callback' => 'gallery_admin_access',
177 'access arguments' => array(array('administer users', 'administer gallery settings')),
178 'file' => 'gallery_user_admin.inc',
179 'page callback' => 'drupal_get_form',
180 'page arguments' => array('_gallery_user_settings'),
181 'type' => MENU_LOCAL_TASK,
182 'weight' => 2
183 );
184 $items['admin/user/gallery/users/sync/%'] = array(
185 'access callback' => 'user_access',
186 'access arguments' => array('administer users'),
187 'file' => 'gallery_user_admin.inc',
188 'page callback' => '_gallery_user_users_sync',
189 'page arguments' => array(5),
190 'type' => MENU_CALLBACK
191 );
192 }
193
194 return $items;
195 }
196
197 /**
198 * Function gallery_admin_access().
199 */
200 function gallery_admin_access($perms) {
201 foreach ($perms as $perm) {
202 if (!user_access($perm)) {
203 return FALSE;
204 }
205 }
206
207 return count($perms);
208 }
209
210 /**
211 * Implementation of hook_help().
212 */
213 function gallery_help($section) {
214 require_once(drupal_get_path('module', 'gallery') .'/gallery_help.inc');
215 return _gallery_help($section);
216 }
217
218 /**
219 * Function gallery_auth_validate().
220 * (authenticate user against G2 directly)
221 */
222 function gallery_auth_validate($form, &$form_state) {
223 if (_gallery_init()) {
224 $username = $form_state['values']['name'];
225 $password = trim($form_state['values']['pass']);
226 // Is the user allowed to login?
227 list($ret, $disabled) = GalleryCoreApi::isDisabledUsername($username);
228 if (!$ret && !$disabled) {
229 // Load G2 user to get the hashed password
230 list($ret, $g2_user) = GalleryCoreApi::fetchUserByUsername($username);
231 if (!$ret) {
232 // Authenticate the G2 user
233 if (GalleryUtilities::isCorrectPassword($password, $g2_user->hashedPassword)) {
234 // Does this user exist in Drupal, then override password
235 // (so that next time the user can be authenticated directly)
236 if ($user = user_load(array('name' => $username, 'status' => 1))) {
237 user_save($user, array('pass' => $password));
238 return TRUE;
239 }
240 }
241 }
242 }
243 }
244
245 return FALSE;
246 }
247
248 /**
249 * Implementation of hook_user().
250 */
251 function gallery_user($op, &$edit, &$user, $category = NULL) {
252 require_once(drupal_get_path('module', 'gallery') .'/gallery_user.inc');
253
254 switch ($op) {
255 case 'login':
256 gallery_login();
257 break;
258 case 'logout':
259 gallery_logout();
260 break;
261 case 'view':
262 return gallery_user_view($user);
263 case 'insert':
264 return gallery_user_insert($edit, drupal_clone($user));
265 case 'update':
266 return gallery_user_update($edit, drupal_clone($user));
267 case 'delete':
268 return gallery_user_delete($user);
269 }
270 }
271
272 /**
273 * Implementation of hook_search().
274 */
275 function gallery_search($op = 'search', $keys = NULL) {
276 require_once(drupal_get_path('module', 'gallery') .'/gallery_search.inc');
277 return _gallery_search($op, $keys);
278 }
279
280 /**
281 * Implementation of hook_search_item().
282 * (override how to display the item)
283 */
284 function gallery_search_page($results) {
285 require_once(drupal_get_path('module', 'gallery') .'/gallery_search.inc');
286 return _gallery_search_page($results);
287 }
288
289 /**
290 * Implementation of hook_form_alter().
291 */
292 function gallery_form_alter(&$form, $form_state, $form_id) {
293 // Trigger groups sync if Drupal roles are added/modified
294 if (($form_id == 'user_admin_role') || ($form_id == 'user_admin_new_role')) {
295 require_once(drupal_get_path('module', 'gallery') .'/gallery_groups.inc');
296 $form['#submit'][] = '_gallery_groups_submit';
297 }
298 // Add a custom search form
299 if ($form_id == 'search_form' && arg(1) == 'gallery' && variable_get('gallery_search_advanced', 1) && user_access('access gallery')) {
300 require_once(drupal_get_path('module', 'gallery') .'/gallery_search.inc');
301 _gallery_search_form($form);
302 }
303 // Add _validate() handler for external authentication
304 if ($form_id == 'user_login_block' || $form_id == 'user_login') {
305 $form['#validate'][] = 'gallery_auth_validate';
306 }
307 }
308
309 /**
310 * Implementation of hook_filter().
311 */
312 function gallery_filter($op, $delta = 0, $format = -1, $text = '') {
313 require_once(drupal_get_path('module', 'gallery') .'/gallery_filter.inc');
314 switch ($op) {
315 case 'list' :
316 return array(0 => t('Gallery2 filter'));
317 case 'description' :
318 return t('Allow users to easily reference Gallery2 items from nodes.');
319 case 'process' :
320 return gallery_filter_process($text);
321 case 'no cache':
322 return !variable_get('gallery_filter_can_cache', TRUE);
323 default:
324 return $text;
325 }
326 }
327
328 /**
329 * Implementation of hook_filter_tips().
330 */
331 function gallery_filter_tips($delta = 0, $format = -1, $long = FALSE) {
332 require_once(drupal_get_path('module', 'gallery') .'/gallery_help.inc');
333 return $long ? gallery_filter_long_tip_translated() : gallery_filter_short_tip_translated();
334 }
335
336 /**
337 * Implementation of hook_elements().
338 */
339 function gallery_elements() {
340 $type['textarea'] = array('#process' => array('gallery_g2image_textarea'));
341 return $type;
342 }
343
344 /**
345 * Function gallery_g2image_textarea().
346 * (add image link underneath textareas)
347 */
348 function gallery_g2image_textarea($element) {
349 require_once(drupal_get_path('module', 'gallery') .'/gallery_g2image.inc');
350 if (_gallery_g2image_page_match() && !strstr($_GET['q'], 'gallery')
351 && (variable_get('gallery_g2image_mode', 'disabled') == 'standalone')
352 && (user_access('access standalone g2image'))) {
353 gallery_g2image_add_js();
354 $output = theme('gallery_g2image_textarea_link', $element, $link);
355 $element['#suffix'] .= $output;
356 }
357
358 return $element;
359 }
360
361 /**
362 * Implementation of hook_block().
363 */
364 function gallery_block($op = 'list', $delta = 0, $edit = array()) {
365 require_once(drupal_get_path('module', 'gallery') .'/gallery_block.inc');
366 if (variable_get('gallery_valid', 0)) {
367 return _gallery_block($op, $delta, $edit);
368 }
369 }
370
371 /**
372 * Function gallery_page().
373 * (main gallery display page)
374 */
375 function gallery_page() {
376 if (!_gallery_init(FALSE)) {
377 return '';
378 }
379 // Turn off sidebar
380 if (variable_get('gallery_move_sidebar_to_block', 1)) {
381 GalleryCapabilities::set('showSidebarBlocks', FALSE);
382 }
383 $result = gallery_handle_request();
384 if ($result && !$result['isDone']) {
385 // Allow modules to alter the page
386 drupal_alter('gallery_page', $result);
387 // Add css/js for this page
388 gallery_set_head($result['headHtml'], TRUE);
389 // Add pathbar. See http://gallery.menalto.com/node/33447
390 if (isset($result['themeData'])) {
391 $breadcrumb = array(l(t('Home'), ''));
392 // Some themes (eg hybrid) do not set $result['themeData']['parents']
393 if ($result['themeData']['parents']) {
394 foreach ($result['themeData']['parents'] as $parent) {
395 $parent_title = $parent['title'];
396 // Simple strip of BBCode (italic, bold)
397 $parent_title = str_replace(
398 array('[i]', '[/i]', '[b]', '[/b]'),
399 array('<i>', '</i>', '<strong>', '</strong>'),
400 $parent_title
401 );
402 $parent_title = str_replace('[/i]', '</i>', $parent_title);
403 // Still does not generate a clean url for /gallery (uses index.php?q=gallery)
404 $link = gallery_generate_url(array('itemId' => $parent['id']), FALSE);
405 // TODO (#154507 + i18n-rewrite): $link = TRUE ? str_replace('?q=gallery', 'gallery', $link) : $link;
406 $breadcrumb[] = l($parent_title, $link);
407 }
408 }
409 drupal_set_breadcrumb($breadcrumb);
410 }
411 // Hack to get the admin sidebar
412 if (variable_get('gallery_move_admin_sidebar_to_block', 0)) {
413 if (preg_match("/^(.*<td id=\"gsSidebarCol\">)(.*?)(<\/td>.*?)$/s", $result['bodyHtml'], $match)) {
414 // New body
415 $result['bodyHtml'] = $match[1] . $match[3];
416 // Insert admin sidebar in $result['sidebarBlocksHtml']
417 if (empty($result['sidebarBlocksHtml'][1])) {
418 $result['sidebarBlocksHtml'][1] = $match[2];
419 }
420 else {
421 $result['sidebarBlocksHtml'][] = $match[2];
422 }
423 }
424 }
425 // Store the sidebar info in a global variable for use in the gallery navigation block
426 $GLOBALS['_gallery_sidebar_'] = $result['sidebarBlocksHtml'];
427
428 return $result['bodyHtml'];
429 }
430
431 return '';
432 }
433
434 /**
435 * Function gallery_gsitemap().
436 * (define additional links to add to the site map)
437 *
438 * This hook allows modules to add additional links to the site map. Links
439 * may be associated with nodes, terms, or users, as shown in the example.
440 *
441 * @param $type
442 * If given, the type of link to associate additional links with.
443 * @param $excludes
444 * If given, an array of criteria for excluding links.
445 * @return
446 * An array of links or an empty array.
447 */
448 function gallery_gsitemap($type = NULL, $excludes = array()) {
449 if (($type != 'xml') || !variable_get('gallery_enable_sitemap', 1) || !_gallery_init(TRUE)) {
450 return array();
451 }
452
453 list($ret, $view) = GalleryView::loadView('sitemap.Sitemap');
454 if ($ret) {
455 gallery_error(t('Error loading the Gallery2 Google Sitemap. Make sure the \'Google Sitemap\' plugin is enabled in Gallery2.'), $ret);
456 return array();
457 }
458 list($ret, $root_id) = GalleryCoreApi::getDefaultAlbumId();
459 if ($ret) {
460 gallery_error(t('Error loading the Gallery2 Default Album Id.'), $ret);
461 return array();
462 }
463
464 // Get the sitemap from Gallery2
465 ob_start();
466 $ret = $view->renderSitemap($root_id);
467 $g2_sitemap = ob_get_contents();
468 ob_end_clean();
469
470 return $g2_sitemap;
471 }