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