summaryrefslogtreecommitdiffstats
path: root/gallery.module
blob: 06a76ac707a43d2edafee54d3161916cdc713bd7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
<?php
// $Id$

require_once(drupal_get_path('module', 'gallery') .'/gallery_base.inc');

// Default variable values
define(GALLERY_IMAGEBLOCK_SIZE_METHOD_DEFAULT, 'maxsize');
define(GALLERY_IMAGEBLOCK_SIZE_DEFAULT, 150);
define(GALLERY_GRID_SIZE_METHOD_DEFAULT, 'maxsize');
define(GALLERY_GRID_SIZE_DEFAULT, 90);
define(GALLERY_SEARCH_SIZE_METHOD_DEFAULT, 'maxsize');
define(GALLERY_SEARCH_SIZE_DEFAULT, 150);
define(GALLERY_FILTER_MAXSIZE_DEFAULT, 150);
define(GALLERY_FILTER_EXACTSIZE_DEFAULT, '');

/**
 * Implementation of hook_perm().
 */
function gallery_perm() {
  return array('administer gallery settings', 'access gallery', 'access standalone g2image');
}

/**
 * Implementation of hook_menu().
 */
function gallery_menu($may_cache) {
  global $custom_theme;
  
  $items = array();
  if ($may_cache) {
    if (variable_get('gallery_valid', 0)) {
      $items[] = array(
        'path' => 'gallery',
        'title' => t('Gallery'),
        'description' => t('Visit your embedded Gallery2.'),
        'callback' => variable_get('gallery_page_callback', 'gallery_page'),
        'access' => user_access('access gallery'),
      );
    }
    // settings / general administration
    $items[] = array(
      'path' => 'admin/settings/gallery',
      'title' => t('Gallery settings'),
      'callback' => 'gallery_settings',
      'callback arguments' => '_gallery_settings_general',
      'description' => t('Configure settings for embedding Gallery2 into Drupal.'),
      'access' => user_access('administer gallery settings'),
    );
    $items[] = array(
      'path' => 'admin/settings/gallery/install',
      'title' => t('Install'),
      'callback' => 'gallery_settings',
      'callback arguments' => '_gallery_settings_install',
      'access' => user_access('administer gallery settings'),
      'type' => variable_get('gallery_valid', 0) ? MENU_LOCAL_TASK : MENU_DEFAULT_LOCAL_TASK,
      'weight' => 0
    );
    if (variable_get('gallery_valid', 0)) {
      $items[] = array(
        'path' => 'admin/settings/gallery/general',
        'title' => t('General'),
        'access' => user_access('administer gallery settings'),
        'type' => MENU_DEFAULT_LOCAL_TASK,
        'weight' => 1
      );
      $items[] = array(
        'path' => 'admin/settings/gallery/filter',
        'title' => t('Filter'),
        'callback' => 'gallery_settings',
        'callback arguments' => '_gallery_settings_filter',
        'access' => user_access('administer gallery settings'),
        'type' => MENU_LOCAL_TASK,
        'weight' => 2
      );
      $items[] = array(
        'path' => 'admin/settings/gallery/g2image',
        'title' => t('G2Image'),
        'callback' => 'gallery_settings',
        'callback arguments' => '_gallery_settings_g2image',
        'access' => user_access('administer gallery settings'),
        'type' => MENU_LOCAL_TASK,
        'weight' => 3
      );
      $items[] = array(
        'path' => 'admin/settings/gallery/search',
        'title' => t('Search'),
        'callback' => 'gallery_settings',
        'callback arguments' => '_gallery_settings_search',
        'access' => user_access('administer gallery settings'),
        'type' => MENU_LOCAL_TASK,
        'weight' => 4
      );
      $items[] = array(
        'path' => 'admin/settings/gallery/report',
        'callback' => 'gallery_report',
        'access' => user_access('administer site configuration'),
        'type' => MENU_CALLBACK
      );
      // user administration
      $items[] = array(
        'path' => 'admin/user/gallery', 
        'title' => t('Gallery users'),
        'description' => t('Gallery2 user integration and synchronization'),
        'callback' => 'gallery_users',
        'access' => user_access('administer users'),
      );
      $items[] = array(
        'path' => 'admin/user/gallery/users',
        'title' => t('User Status'),
        'access' => user_access('administer users'),
        'type' => MENU_DEFAULT_LOCAL_TASK,
        'weight' => 0
      );
      $items[] = array(
        'path' => 'admin/user/gallery/advanced',
        'title' => t('Advanced Sync'),
        'callback' => 'gallery_user_admin',
        'callback arguments' => '_gallery_user_advanced',
        'access' => user_access('administer users'),
        'type' => MENU_LOCAL_TASK,
        'weight' => 1
      );
      $items[] = array(
        'path' => 'admin/user/gallery/settings',
        'title' => t('User Settings'),
        'callback' => 'gallery_user_admin',
        'callback arguments' => '_gallery_user_settings',
        'access' => user_access('administer users') && user_access('administer gallery settings'),
        'type' => MENU_LOCAL_TASK,
        'weight' => 2
      );
      $items[] = array(
        'path' => 'admin/user/gallery/advanced_progress',
        'callback' => 'gallery_user_admin_advanced_progress',
        'access' => user_access('administer users'),
        'type' => MENU_CALLBACK
      );
      $items[] = array(
        'path' => 'admin/user/gallery/users/sync',
        'callback' => 'gallery_users',
        'access' => user_access('administer users'),
        'type' => MENU_CALLBACK
      );
    }
  }
  else {
    drupal_add_css(drupal_get_path('module', 'gallery') .'/gallery.css');
    drupal_add_css(drupal_get_path('module', 'gallery') .'/gallery_filter.css');
    // Switch theme for gallery pages
    if (arg(0) == 'gallery' && ($custom_theme = variable_get('gallery_page_theme', NULL)) != 'default') {
      init_theme();
    }
  }
  
  return $items;
} 

/**
 * Implementation of hook_help().
 */
function gallery_help($section) {
  require_once(drupal_get_path('module', 'gallery') .'/gallery_help.inc');
  return _gallery_help($section);
}

/**
 * Function gallery_settings().
 */
function gallery_settings($form = '_gallery_settings_general') {
  require_once(drupal_get_path('module', 'gallery') .'/gallery_settings.inc');
  return drupal_get_form(variable_get('gallery_valid', 0) ? $form : '_gallery_settings_install');
}

/**
 * Function gallery_report().
 */
function gallery_report($download = NULL) {
  require_once(drupal_get_path('module', 'gallery') .'/gallery_report.inc');
  return _gallery_report(isset($download));
}

/**
 * Implementation of hook_info().
 */
function gallery_info($field = NULL) {
  $info['name'] = 'Gallery2';
  $info['protocol'] = 'internal';

  return isset($field) ? $info[$field] : $info;
}

/**
 * Implementation of hook_auth().
 */
function gallery_auth($username, $password, $server) {
  if (_gallery_init()) {
    // Is the user allowed to login?
    list($ret, $disabled) = GalleryCoreApi::isDisabledUsername($username);
    if (!$ret && !$disabled) {
      // Load G2 user to get the hashed password
      list($ret, $g2_user) = GalleryCoreApi::fetchUserByUsername($username);
      if (!$ret) {
        // Authenticate the G2 user
        if (GalleryUtilities::isCorrectPassword($password, $g2_user->hashedPassword)) {
          // Override the Drupal password (so that next time the user can be authenticated directly)
          if ($user = user_load(array('name' => $username, 'status' => 1))) {
            user_save($user, array('pass' => $password));
          }
          return TRUE;
        }
      }
    }
  }

  return FALSE;
}

/**
 * Implementation of hook_user().
 */
function gallery_user($op, &$edit, &$user, $category = NULL) {
  require_once(drupal_get_path('module', 'gallery') .'/gallery_user.inc');

  switch ($op) {
  case 'login':
    gallery_login();
    break;
  case 'logout':
    gallery_logout();
    break;
  case 'view':
    return gallery_user_view($user);
  case 'insert':
    return gallery_user_insert($edit, drupal_clone($user));
  case 'update':
    return gallery_user_update($edit, drupal_clone($user));
  case 'delete':
    return gallery_user_delete($user);
  }
}

/**
 * Function gallery_users().
 * (gallery user administration)
 */
function gallery_users($args = NULL) {
  require_once(drupal_get_path('module', 'gallery') .'/gallery_user_admin.inc');
  return _gallery_user_users($args);
}

/**
 * Function gallery_user_admin().
 */
function gallery_user_admin($form) {
  require_once(drupal_get_path('module', 'gallery') .'/gallery_user_admin.inc');
  return drupal_get_form($form);
}

/**
 * Function gallery_user_admin_advanced_progress().
 */
function gallery_user_admin_advanced_progress() {
  require_once(drupal_get_path('module', 'gallery') .'/gallery_user_admin.inc');
  return _gallery_user_advanced_progress();
}

/**
 * Implementation of hook_search().
 */
function gallery_search($op = 'search', $keys = NULL) {
  require_once(drupal_get_path('module', 'gallery') .'/gallery_search.inc');
  return _gallery_search($op, $keys); 
}

/**
 * Implementation of hook_search_item().
 * (override how to display the item)
 */
function gallery_search_page($results) {
  require_once(drupal_get_path('module', 'gallery') .'/gallery_search.inc');
  return _gallery_search_page($results); 
}

/**
 * Implementation of hook_form_alter().
 */
function gallery_form_alter($form_id, &$form) {
  if (($form_id == 'user_admin_role') || ($form_id == 'user_admin_new_role')) {
    require_once(drupal_get_path('module', 'gallery') .'/gallery_groups.inc');
    $form['#submit']['_gallery_groups_submit'] = array();
  }
  if ($form_id == 'search_form' && arg(1) == 'gallery' && variable_get('gallery_search_advanced', 1) && user_access('access gallery')) {
    require_once(drupal_get_path('module', 'gallery') .'/gallery_search.inc');
    _gallery_search_form($form);
  }
}

/**
 * Implementation of hook_filter().
 */
function gallery_filter($op, $delta = 0, $format = -1, $text = '') {
  require_once(drupal_get_path('module', 'gallery') .'/gallery_filter.inc');
  switch ($op) {
    case 'list' :
      return array(0 => t('Gallery2 filter'));
    case 'description' :
      return t('Allow users to easily reference Gallery2 items from nodes.');
    case 'process' :
      return gallery_filter_process($text);
    case 'no cache': 
      return !variable_get('gallery_filter_can_cache', TRUE);
    default:
      return $text;
  }
}

/**
 * Implementation of hook_filter_tips().
 */
function gallery_filter_tips($delta = 0, $format = -1, $long = FALSE) {
  require_once(drupal_get_path('module', 'gallery') .'/gallery_help.inc');
  if ($long) {
    return gallery_filter_long_tip_translated();
  }
  else {
    return gallery_filter_short_tip_translated();
  }
}

/**
 * Implementation of hook_elements().
 */
function gallery_elements() {
  $type['textarea'] = array('#process' => array('gallery_g2image_textarea' => array()));
  return $type;
}

/**
 * Function gallery_g2image_textarea().
 * (add image link underneath textareas)
 */
function gallery_g2image_textarea($element) {
    require_once(drupal_get_path('module', 'gallery') .'/gallery_g2image.inc');
    if (_gallery_g2image_page_match() && !strstr($_GET['q'], 'gallery')
      && (variable_get('gallery_g2image_mode', 'disabled') == 'standalone')
      && (user_access('access standalone g2image'))) {
    gallery_g2image_add_js();
    $output = theme('gallery_g2image_textarea_link', $element, $link);
    $element['#suffix'] .= $output;
  }
  
  return $element;
}

/**
 * Implementation of hook_block().
 */
function gallery_block($op = 'list', $delta = 0, $edit = array()) {
  require_once(drupal_get_path('module', 'gallery') .'/gallery_block.inc');
  if (variable_get('gallery_valid', 0)) {
    return _gallery_block($op, $delta, $edit);
  }
}

/**
 * Function gallery_page().
 * (main gallery display page)
 */
function gallery_page($internal = FALSE) {
  if (!_gallery_init()) {
    return $internal ? array() : '';
  }
  // Turn off sidebar
  if (variable_get('gallery_move_sidebar_to_block', 1)) {
    GalleryCapabilities::set('showSidebarBlocks', FALSE);
  }
  $result = gallery_handle_request();
  if ($result && !$result['isDone']) {
    gallery_set_head($result['headHtml'], TRUE);
    // Add pathbar (see http://gallery.menalto.com/node/33447)
    if (isset($result['themeData'])) {
      $breadcrumb = array(l(t('Home'), ''));
      // Some themes (eg hybrid) do not set $result['themeData']['parents']
      if ($result['themeData']['parents']) {
        foreach ($result['themeData']['parents'] as $parent) {
          $parent_title = $parent['title'];
          // Simple strip of bbcode (italic, bold)
          $parent_title = str_replace(
            array('[i]', '[/i]', '[b]', '[/b]'),
            array('<i>', '</i>', '<strong>', '</strong>'),
            $parent_title
          );
          $parent_title = str_replace('[/i]', '</i>', $parent_title);
          // Still does not generate a clean url for /gallery (uses index.php?q=gallery)
          $link = gallery_generate_url(array('view' => 'core.ShowItem', 'itemId' => $parent['id']), FALSE);
          $breadcrumb[] = l($parent_title, $link);
        }
      }
      drupal_set_breadcrumb($breadcrumb);
    }
    // Hack to get the admin sidebar
    if (variable_get('gallery_move_admin_sidebar_to_block', 0)) {
      if (preg_match("/^(.*<td id=\"gsSidebarCol\">)(.*?)(<\/td>.*?)$/s", $result['bodyHtml'], $match)) {
        // New body
        $result['bodyHtml'] = $match[1] . $match[3];
        // Insert admin sidebar in $result['sidebarBlocksHtml']
        if (empty($result['sidebarBlocksHtml'][1])) {
          $result['sidebarBlocksHtml'][1] = $match[2];
        } 
        else {
          $result['sidebarBlocksHtml'][] = $match[2];
        }
      }
    }
    // Store the sidebar info in a global variable for use in the gallery navigation block
    $GLOBALS['_gallery_sidebar_'] = $result['sidebarBlocksHtml'];
    
    return $internal ? $result : $result['bodyHtml'];
  }
  
  return $internal ? array() : '';
}

/**
 * Function gallery_gsitemap().
 * (define additional links to add to the site map)
 *
 * This hook allows modules to add additional links to the site map. Links
 * may be associated with nodes, terms, or users, as shown in the example.
 *
 * @param $type
 *   If given, the type of link to associate additional links with.
 * @param $excludes
 *   If given, an array of criteria for excluding links.
 * @return
 *   An array of links or an empty array.
 */
function gallery_gsitemap($type = NULL, $excludes = array()) {
  if (($type != 'xml') || !variable_get('gallery_enable_sitemap', 1)) {
    return;
  }
  // Need to do a full init
  if (!_gallery_init(TRUE)) {
    return;
  }

  $view_name = 'sitemap.Sitemap';
  list($ret, $view) = GalleryView::loadView($view_name);
  if ($ret) {
    gallery_error(t('Error loading the Gallery2 Google Sitemap. Make sure the \'Google Sitemap\' plugin is enabled in Gallery2.'), $ret);
    return;
  }
  list($ret, $root_id) = GalleryCoreApi::getDefaultAlbumId();
  if ($ret) {
    gallery_error(t('Error loading the Gallery2 Default Album Id.'), $ret);
    return;
  }
  
  // Get the sitemap from Gallery2
  ob_start();
  $ret = $view->renderSitemap($root_id);
  $g2_sitemap = ob_get_contents();
  ob_end_clean();

  return $g2_sitemap;
}