3029c5d19a4343c7bba6675e87ad49bfc1d78347
[project/gallery.git] / gallery_install.inc
1 <?php
2 // $Id$
3
4 require_once(drupal_get_path('module', 'gallery') .'/gallery_settings.inc');
5
6 /**
7 * gallery.module : gallery_install.inc
8 * Configure embedded Gallery install (directory options, etc.)
9 */
10
11 define('GALLERY_INSTALL_STEP_PHPMEMORY', 1);
12 define('GALLERY_INSTALL_STEP_LOCATIONS', 2);
13 define('GALLERY_INSTALL_STEP_PLUGINS', 3);
14 define('GALLERY_INSTALL_STEP_URLREWRITE', 4);
15 define('GALLERY_INSTALL_STEP_USERSYNC', 5);
16
17 /**
18 * Function _gallery_install_status_init().
19 */
20 function _gallery_install_status_init() {
21 return array(
22 'phpmemory' => array(
23 'title' => t('PHP Memory Limit'),
24 'severity' => GALLERY_SEVERITY_ERROR,
25 'status' => FALSE
26 ),
27 'locations' => array(
28 'title' => t('Gallery2 locations'),
29 'severity' => GALLERY_SEVERITY_ERROR,
30 'status' => FALSE
31 ),
32 'plugins' => array(
33 'title' => t('Drupal Modules / Gallery2 Plugins'),
34 'severity' => GALLERY_SEVERITY_WARNING,
35 'status' => FALSE
36 ),
37 'urlrewrite' => array(
38 'title' => t('Clean URL / URL Rewrite'),
39 'severity' => GALLERY_SEVERITY_WARNING,
40 'status' => FALSE
41 ),
42 'usersync' => array(
43 'title' => t('Initial User Synchronization'),
44 'severity' => GALLERY_SEVERITY_ERROR,
45 'status' => FALSE
46 ),
47 );
48 }
49
50 /**
51 * Function _gallery_install_status_report().
52 */
53 function _gallery_install_status_report($install_status) {
54 // Install status table
55 $error = FALSE;
56 $rows = array();
57 foreach ($install_status as $step => $status) {
58 // Find the combination of status and severity of the plugin
59 $severity = ($status['status'] <= 0) ? $status['severity'] : GALLERY_SEVERITY_SUCCESS;
60 if (!$status['status']) {
61 $error = TRUE;
62 }
63 $row = array();
64 $row[] = array('data' => $status['title'], 'align' => 'left', 'width' => '20%');
65 $row[] = array('data' => theme('gallery_severity_message', $severity), 'align' => 'center');
66 $row[] = array('data' => isset($status['info'])? $status['info'] : '', 'class' => 'description', 'id' => $step);
67 $rows[] = $row;
68 }
69
70 if ($error) {
71 $rows[] = array(array(
72 'data' => t('Critical errors are present.'),
73 'colspan' => '3',
74 'align' => 'center',
75 'class' => 'message')
76 );
77 }
78
79 // Set the global status variable 'gallery_valid'
80 gallery_set_status(array(
81 'gallery_valid' => array(
82 'title' => t('Overall Status (Installation)'),
83 'severity' => $error ? GALLERY_SEVERITY_ERROR : GALLERY_SEVERITY_SUCCESS,
84 ),
85 ));
86
87 return theme('table', array(t('Step'), t('Status'), t('Description')), $rows, array('class' => 'package'));
88 }
89
90 /**
91 * Function _gallery_install_status().
92 */
93 function _gallery_install_status($form_state) {
94 $form['install'] = array(
95 '#type' => 'fieldset',
96 '#title' => t('Install status'),
97 '#collapsible' => FALSE,
98 '#collapsed' => FALSE,
99 '#description' => t('Correct configuration of the following items is essential for the
100 embedded gallery to function properly.'),
101 );
102
103 $install_status = _gallery_install_status_init();
104
105 // Step 1: Check PHP Memory
106 $phpmemory_info = _gallery_php_memcheck();
107 $install_status['phpmemory']['status'] = $phpmemory_info['status'];
108 $install_status['phpmemory']['info'] = $phpmemory_info['info'];
109
110 // Step 2: Gallery2 Locations
111 $values['gallery_uri'] = variable_get('gallery_uri', '/gallery2/');
112 $values['gallery_dir'] = variable_get('gallery_dir', './gallery2/');
113 $values['gallery_embed_uri'] = variable_get('gallery_embed_uri', '?q=gallery');
114 $autodetect = variable_get('gallery_autodetect_dir', 1);
115 list($install_status['locations']['status'], $values, $result) = _gallery_install_locations($values, $autodetect);
116 $install_status['locations']['info'] = $install_status['locations']['status'] ?
117 t('The Gallery2 location settings appear to be correct.') :
118 gallery_format_status($result, t('Errors in your current Gallery2 location settings:'));
119
120 // Step 3: Drupal Modules and Gallery2 Plugins check
121 $form += _gallery_install_plugins($install_status);
122
123 // Step 4: Clean URL configuration
124 _gallery_install_urlrewrite_check($install_status);
125
126 // Step 5: User Synchronization
127 // Always TRUE (= gallery_valid), otherwise we would be redirected to the install wizard page instead
128 $install_status['usersync']['status'] = TRUE;
129 $install_status['usersync']['info'] = t('Your initial user synchronization has been completed already.<br /> If you
130 want to resync all your users or you want to import users from an existing
131 Gallery2 setup, you can always use the <a href= "@gallery-usersync">Advanced
132 Sync</a> options in the Gallery user administration.',
133 array('@gallery-usersync' => url('admin/user/gallery/advanced')));
134
135 $form['install']['status'] = array(
136 '#value' => _gallery_install_status_report($install_status)
137 );
138
139 $form['buttons']['reset'] = array(
140 '#type' => 'submit',
141 '#value' => t('Reset & Restart'),
142 '#submit' => array('_gallery_install_reset')
143 );
144
145 return $form;
146 }
147
148 /**
149 * Function _gallery_install().
150 */
151 function _gallery_install($form_state) {
152 $form['install'] = array(
153 '#type' => 'fieldset',
154 '#title' => t('Install status'),
155 '#collapsible' => FALSE,
156 '#collapsed' => FALSE,
157 '#description' => t(''),
158 );
159
160 if (!isset($form_state['storage'])) {
161 $install_status = _gallery_install_status_init();
162 $install_status['step'] = GALLERY_INSTALL_STEP_PHPMEMORY;
163 }
164 else {
165 $install_status = $form_state['storage'];
166 }
167
168 // Step 1: Check PHP Memory
169 _gallery_install_step_phpmemory($form, $form_state, $install_status);
170
171 // Step 2 : Gallery2 Locations
172 if ($install_status['step'] >= GALLERY_INSTALL_STEP_LOCATIONS) {
173 _gallery_install_step_locations($form, $form_state, $install_status);
174 }
175
176 // Step 3: Drupal Modules and Gallery2 Plugins check
177 if ($install_status['step'] >= GALLERY_INSTALL_STEP_PLUGINS) {
178 _gallery_install_step_plugins($form, $form_state, $install_status);
179 }
180
181 // Step 4: Clean URL configuration
182 if ($install_status['step'] >= GALLERY_INSTALL_STEP_URLREWRITE) {
183 _gallery_install_step_urlrewrite($form, $form_state, $install_status);
184 }
185
186 // Step 5: User Synchronization
187 if ($install_status['step'] >= GALLERY_INSTALL_STEP_USERSYNC) {
188 _gallery_install_step_usersync($form, $form_state, $install_status);
189 }
190
191 $form['install']['status'] = array(
192 '#value' => _gallery_install_status_report($install_status)
193 );
194
195 $form['install_status'] = array(
196 '#type' => 'value',
197 '#value' => $install_status
198 );
199
200 $form['buttons']['reset'] = array(
201 '#type' => 'submit',
202 '#value' => t('Reset & Restart'),
203 '#submit' => array('_gallery_install_reset')
204 );
205
206 return $form;
207 }
208
209 /**
210 * Function _gallery_install_reset().
211 */
212 function _gallery_install_reset($form, &$form_state) {
213 variable_del('gallery_autodetect_dir');
214 variable_del('gallery_uri');
215 variable_del('gallery_dir');
216 variable_del('gallery_embed_uri');
217 variable_del('gallery_valid');
218 variable_del('gallery_rewrite_disable');
219 menu_rebuild();
220
221 unset($form_state['storage']);
222 $form_state['redirect'] = 'admin/settings/gallery/install';
223 }
224
225 /**
226 * Function _gallery_install_step_phpmemory().
227 * Step 1: Check PHP Memory
228 */
229 function _gallery_install_step_phpmemory(&$form, &$form_state, &$install_status) {
230 if ($install_status['step'] == GALLERY_INSTALL_STEP_PHPMEMORY) {
231 $phpmemory_info = _gallery_php_memcheck();
232 $install_status['phpmemory']['status'] = $phpmemory_info['status'];
233 $install_status['phpmemory']['info'] = $phpmemory_info['info'];
234 if ($phpmemory_info['status']) {
235 $install_status['step'] = GALLERY_INSTALL_STEP_LOCATIONS;
236 }
237 }
238
239 $form['phpmemory'] = array(
240 '#type' => 'fieldset',
241 '#title' => t('Step 1: PHP Memory Test') . ($install_status['phpmemory']['status'] ? ' '. t('(OK)') : ''),
242 '#description' => $install_status['phpmemory']['info'],
243 '#collapsible' => ($install_status['step'] > GALLERY_INSTALL_STEP_PHPMEMORY),
244 '#collapsed' => $install_status['phpmemory']['status'],
245 );
246 if (!$install_status['phpmemory']['status']) {
247 $form['phpmemory']['#description'] .= ' '. t('Until this is fixed your Gallery2 cannot be installed.');
248 $form['phpmemory']['check'] = array('#type' => 'submit', '#value' => t('Check PHP memory again'));
249 }
250 }
251
252 /**
253 * Function _gallery_install_step_locations().
254 * Step 2: Gallery2 Locations
255 */
256 function _gallery_install_step_locations(&$form, &$form_state, &$install_status) {
257 $autodetect_dir = variable_get('gallery_autodetect_dir', 1);
258 $title = t('Step 2: Gallery2 location settings') .' - '. ($autodetect_dir ? t('Auto Configuration') : t('Manual Configuration'));
259 $desc = $install_status['locations']['status'] ? '' : $autodetect_dir ?
260 t('Enter the \'Gallery2 URL or URI\' and click \'Test location settings\'
261 to automatically configure the settings needed for embedding Gallery2 into Drupal. Note that
262 the auto-config will not work for all host configurations. If you have tried it with a value
263 you know is correct and it did not work then just use the manual configuration instead.') :
264 t('Enter the \'Gallery2 URL or URI\' and \'Gallery2 filesystem path\' and then click
265 \'Test location settings\'. This will check and configure the settings needed for embedding
266 Gallery2 into Drupal.');
267
268 $form['locations'] = array(
269 '#type' => 'fieldset',
270 '#title' => $title . ($install_status['locations']['status'] ? ' '. t('(OK)') : ''),
271 '#description' => isset($install_status['locations']['details']) ? ($desc .'<p>'. $install_status['locations']['details'] .'</p>') : $desc,
272 '#collapsible' => ($install_status['step'] > GALLERY_INSTALL_STEP_LOCATIONS),
273 '#collapsed' => $install_status['locations']['status']
274 );
275
276 if ($autodetect_dir) {
277 // Autodetect
278 $gallery_uri = isset($form_state['values']['gallery_uri_auto']) ?
279 $form_state['values']['gallery_uri_auto'] : ($install_status['locations']['status'] ?
280 variable_get('gallery_uri', '/gallery2/') : '');
281
282 $gallery_dir = isset($form_state['values']['gallery_dir_auto']) ?
283 $form_state['values']['gallery_dir_auto'] : ($install_status['locations']['status'] ?
284 variable_get('gallery_dir', './gallery2/') : '');
285
286 $embed_uri = isset($form_state['values']['gallery_embed_uri_auto']) ?
287 $form_state['values']['gallery_embed_uri_auto'] : ($install_status['locations']['status'] ?
288 variable_get('gallery_embed_uri', '?q=gallery') : '');
289
290 $form['locations']['gallery_uri_auto'] = array(
291 '#type' => 'textfield',
292 '#title' => t('Gallery2 URL or URI'),
293 '#default_value' => $gallery_uri,
294 '#size' => 64,
295 '#maxlength' => 128,
296 '#description' => t('URL or URI of the G2 standalone location. This can be either the full URL to Gallery2
297 (e.g. http://www.example.com/gallery2) or the path from docroot to the Gallery2 directory
298 (e.g. /gallery2). If using a URI the protocol/hostname are both optional.'),
299 '#disabled' => $install_status['locations']['status']
300 );
301
302 if (!empty($gallery_dir)) {
303 $form['locations']['gallery_dir_auto_item'] = array(
304 '#title' => t('Gallery2 filesystem path'),
305 '#type' => 'item',
306 '#value' => $gallery_dir
307 );
308 }
309 $form['locations']['gallery_dir_auto'] = array(
310 '#type' => 'value',
311 '#value' => $gallery_dir
312 );
313
314 if (!empty($embed_uri)) {
315 $form['locations']['gallery_embed_uri_auto_item'] = array(
316 '#title' => t('Embed URI'),
317 '#type' => 'item',
318 '#value' => $embed_uri
319 );
320 }
321 $form['locations']['gallery_embed_uri_auto'] = array(
322 '#type' => 'value',
323 '#value' => $embed_uri
324 );
325 }
326 else {
327 // No Autodetect (manual)
328 $gallery_uri = isset($form_state['values']['gallery_uri_man']) ?
329 $form_state['values']['gallery_uri_man'] : ($install_status['locations']['status'] ?
330 variable_get('gallery_uri', '/gallery2/') : '');
331
332 $gallery_dir = isset($form_state['values']['gallery_dir_man']) ?
333 $form_state['values']['gallery_dir_man'] : ($install_status['locations']['status'] ?
334 variable_get('gallery_dir', './gallery2/') : '');
335
336 $embed_uri = isset($form_state['values']['gallery_embed_uri_man']) ?
337 $form_state['values']['gallery_embed_uri_man'] : ($install_status['locations']['status'] ?
338 variable_get('gallery_embed_uri', '?q=gallery') : '');
339
340 $form['locations']['gallery_uri_man'] = array(
341 '#type' => 'textfield',
342 '#title' => t('Gallery2 URL or URI'),
343 '#default_value' => $gallery_uri,
344 '#size' => 64,
345 '#maxlength' => 128,
346 '#description' => t('URL or URI of the G2 standalone location. This can be either the full URL to Gallery2
347 (e.g. http://www.example.com/gallery2) or the path from docroot to the Gallery2 directory
348 (e.g. /gallery2). If using a URI the protocol/hostname are both optional.'),
349 '#disabled' => $install_status['locations']['status']
350 );
351
352 $form['locations']['gallery_dir_man'] = array(
353 '#type' => 'textfield',
354 '#title' => t('Gallery2 filesystem path'),
355 '#default_value' => $gallery_dir,
356 '#size' => 64,
357 '#maxlength' => 128,
358 '#description' => t('The filesystem path of your Gallery2 directory. This can be either an absolute path
359 (e.g. /home/mysite/htdocs/gallery2) or relative to the root directory of your Drupal
360 installation (e.g. ../gallery2).'),
361 '#disabled' => $install_status['locations']['status']
362 );
363
364 $form['locations']['gallery_embed_uri_man'] = array(
365 '#type' => 'textfield',
366 '#title' => t('Embed URI'),
367 '#default_value' => $embed_uri,
368 '#size' => 64,
369 '#maxlength' => 128,
370 '#description' => t('URI to access G2 via Drupal. Don\'t worry if you are using clean urls in Drupal and this
371 still ends in \'index.php?q=gallery\'. This is needed to get the Gallery2 URL rewrite module
372 to work correctly and will not be visible.'),
373 '#disabled' => $install_status['locations']['status']
374 );
375 }
376
377 if (!$install_status['locations']['status']) {
378 $form['locations']['location_button'] = array(
379 '#type' => 'submit',
380 '#value' => t('Test location settings'),
381 '#submit' => array('_gallery_install_step_locations_check')
382 );
383 $form['locations']['switch_config_button'] = array(
384 '#type' => 'submit',
385 '#value' => $autodetect_dir ? t('Use Manual Configuration') : t('Use Auto Configuration'),
386 '#submit' => array('_gallery_install_step_locations_switch')
387 );
388 }
389 }
390
391 /**
392 * Function _gallery_install_step_locations_check().
393 */
394 function _gallery_install_step_locations_check($form, &$form_state) {
395 $install_status = $form_state['values']['install_status'];
396 $autodetect = variable_get('gallery_autodetect_dir', 1);
397 if ($autodetect) {
398 $values['gallery_uri'] = $form_state['values']['gallery_uri_auto'];
399 }
400 else {
401 $values['gallery_uri'] = $form_state['values']['gallery_uri_man'];
402 $values['gallery_dir'] = $form_state['values']['gallery_dir_man'];
403 $values['gallery_embed_uri'] = $form_state['values']['gallery_embed_uri_man'];
404 }
405
406 list($install_status['locations']['status'], $values, $result) = _gallery_install_locations($values, $autodetect);
407 if (!$install_status['locations']['status']) {
408 $install_status['locations']['details'] = gallery_format_status($result, t('Errors in your current Gallery2 location settings:'));
409 }
410 else {
411 variable_set('gallery_uri', $values['gallery_uri']);
412 variable_set('gallery_dir', $values['gallery_dir']);
413 variable_set('gallery_embed_uri', $values['gallery_embed_uri']);
414 $install_status['step'] = GALLERY_INSTALL_STEP_PLUGINS;
415 }
416
417 if ($autodetect) {
418 $form_state['values']['gallery_uri_auto'] = $values['gallery_uri'];
419 $form_state['values']['gallery_dir_auto'] = $values['gallery_dir'];
420 $form_state['values']['gallery_embed_uri_auto'] = $values['gallery_embed_uri'];
421 }
422 else {
423 $form_state['values']['gallery_uri_man'] = $values['gallery_uri'];
424 $form_state['values']['gallery_dir_man'] = $values['gallery_dir'];
425 $form_state['values']['gallery_embed_uri_man'] = $values['gallery_embed_uri'];
426 }
427
428 $install_status['locations']['info'] = $install_status['locations']['status'] ?
429 t('The Gallery2 location settings appear to be correct.') :
430 t('There is a problem with the Gallery2 location settings. Please check below and retest.
431 Remember that the auto-config will not work for all hosts.');
432
433 $form_state['storage'] = $install_status;
434 }
435
436 /**
437 * Function _gallery_install_step_locations_switch().
438 */
439 function _gallery_install_step_locations_switch($form, &$form_state) {
440 variable_set('gallery_autodetect_dir', !variable_get('gallery_autodetect_dir', 1));
441 }
442
443 /**
444 * Function _gallery_install_step_plugins().
445 * Step 3: Drupal Modules / Gallery2 Plugins
446 */
447 function _gallery_install_step_plugins(&$form, &$form_state, &$install_status) {
448 $title = t('Step 3: Drupal Modules / Gallery2 Plugins') . ($install_status['plugins']['status'] ? ' '. t('(OK)') : '');
449 $form += _gallery_install_plugins($install_status, $title, ($install_status['step'] > GALLERY_INSTALL_STEP_PLUGINS));
450
451 if ($install_status['step'] == GALLERY_INSTALL_STEP_PLUGINS) {
452 if ($install_status['plugins']['status']) {
453 $install_status['step'] = GALLERY_INSTALL_STEP_URLREWRITE;
454 $form['plugins']['#collapsed'] = TRUE;
455 }
456 else {
457 $form['plugins']['plugins_button_desc'] = array(
458 '#type' => 'item',
459 '#value' => t('There are errors in your plugin configuration. Enable/Disable the
460 respective plugins to meet the requirements.')
461 );
462 $form['plugins']['plugins_button'] = array(
463 '#type' => 'submit',
464 '#value' => t('Check Plugins status again'),
465 '#submit' => array('_gallery_install_step_plugins_check')
466 );
467 }
468 }
469 }
470
471 /**
472 * Function _gallery_install_step_plugins_check().
473 */
474 function _gallery_install_step_plugins_check($form, &$form_state) {
475 $install_status = $form_state['values']['install_status'];
476 _gallery_init(TRUE, array(), FALSE);
477 $form_state['storage'] = $install_status;
478 }
479
480 /**
481 * Function _gallery_install_step_urlrewrite().
482 * Step 4: Clean URL configuration
483 */
484 function _gallery_install_step_urlrewrite(&$form, &$form_state, &$install_status) {
485 $title = t('Step 4: Clean URLs / URL Rewrite settings') . ($install_status['urlrewrite']['status'] ? ' '. t('(OK)') : '');
486 $form['urlrewrite'] = array(
487 '#type' => 'fieldset',
488 '#title' => $title,
489 '#collapsible' => ($install_status['step'] > GALLERY_INSTALL_STEP_URLREWRITE),
490 '#collapsed' => $install_status['urlrewrite']['status']
491 );
492
493 // User selected to skip the URLRewrite step
494 if (variable_get('gallery_rewrite_disable', 0)) {
495 $form['urlrewrite']['#description'] = $install_status['urlrewrite']['info'];
496 $form['urlrewrite']['#description'] .= ' '. t('\'Reset & Restart\' your configuration to change these settings.');
497 return;
498 }
499
500 $clean_urls_status = variable_get('clean_url', 0);
501 $rewrite_status = gallery_single_plugin_status('rewrite');
502
503 if ($clean_urls_status) {
504 if ($rewrite_status < GALLERY_PLUGIN_ENABLED) {
505 $form['urlrewrite']['#description'] = t('Clean URLs are enabled in Drupal, but the Gallery2 URL Rewrite plugin is unavailable (!status).',
506 array('!status' => theme('gallery_plugin_status_message', $rewrite_status)));
507 $form['urlrewrite']['check'] = array(
508 '#type' => 'submit',
509 '#value' => t('Check URL Rewrite status again'),
510 '#submit' => array('_gallery_install_step_urlrewrite_check')
511 );
512 }
513 else {
514 // CleanURL and URLRewrite are both enabled
515 $form['urlrewrite']['#description'] = t('Clean URLs are !clean_url_status in Drupal and the Gallery2 URL Rewrite plugin is
516 !rewrite_status. It is possible to automatically configure the URL Rewrite plugin.
517 The configuration assumes that the rules should be placed in your Drupal .htaccess
518 file (it will add them to the beginning) which works in most applications. If you
519 need a special configuration, enter the desired values manually.',
520 array(
521 '!clean_url_status' => theme('gallery_module_status_message', $clean_urls_status),
522 '!rewrite_status' => theme('gallery_plugin_status_message', $rewrite_status)
523 )
524 );
525
526 _gallery_install_urlrewrite($public_path, $htaccess_path, TRUE);
527 $form['urlrewrite']['htaccess_public_path'] = array(
528 '#type' => 'textfield',
529 '#title' => t('Public path to your .htaccess file'),
530 '#size' => 64,
531 '#maxlength' => 128,
532 '#default_value' => $public_path,
533 '#description' => t('This is the location of your Drupal .htaccess file relative to your webserver document root.'),
534 '#disabled' => $$install_status['urlrewrite']['status'],
535 );
536 $form['urlrewrite']['htaccess_filesystem_path'] = array(
537 '#type' => 'textfield',
538 '#title' => t('Filesystem path to your .htaccess file'),
539 '#size' => 64,
540 '#maxlength' => 128,
541 '#default_value' => $htaccess_path,
542 '#description' => t('This is the absolute directory path of your Drupal .htaccess file.'),
543 '#disabled' => $install_status['urlrewrite']['status'],
544 );
545
546 $form['urlrewrite']['config'] = array(
547 '#type' => 'submit',
548 '#value' => t('Configure URL Rewrite plugin'),
549 '#submit' => array('_gallery_install_step_urlrewrite_configure')
550 );
551 }
552 }
553 else {
554 $form['urlrewrite']['check'] = array(
555 '#type' => 'submit',
556 '#value' => t('Check URL Rewrite status again'),
557 '#submit' => array('_gallery_install_step_urlrewrite_check')
558 );
559 }
560
561 $form['urlrewrite']['skip'] = array(
562 '#type' => 'submit',
563 '#value' => t('Skip URL Rewrite Config'),
564 '#submit' => array('_gallery_install_step_urlrewrite_skip')
565 );
566 }
567
568 /**
569 * Function _gallery_install_step_urlrewrite_check().
570 */
571 function _gallery_install_step_urlrewrite_check($form, &$form_state) {
572 $install_status = $form_state['values']['install_status'];
573 _gallery_init(TRUE, array(), FALSE);
574 $form_state['storage'] = $install_status;
575 }
576
577 /**
578 * Function _gallery_install_step_urlrewrite_skip().
579 */
580 function _gallery_install_step_urlrewrite_skip($form, &$form_state) {
581 $install_status = $form_state['values']['install_status'];
582
583 variable_set('gallery_rewrite_disable', 1);
584
585 _gallery_init(TRUE, array(), FALSE);
586 _gallery_install_urlrewrite_check($install_status);
587
588 $install_status['step'] = GALLERY_INSTALL_STEP_USERSYNC;
589 $form_state['storage'] = $install_status;
590 }
591
592 /**
593 * Function _gallery_install_step_urlrewrite_configure().
594 */
595 function _gallery_install_step_urlrewrite_configure($form, &$form_state) {
596 $install_status = $form_state['values']['install_status'];
597
598 _gallery_init(TRUE, array(), FALSE);
599 _gallery_install_urlrewrite($form_state['values']['htaccess_public_path'], $form_state['values']['htaccess_filesystem_path']);
600 _gallery_install_urlrewrite_check($install_status);
601
602 if ($install_status['urlrewrite']['status']) {
603 $install_status['step'] = GALLERY_INSTALL_STEP_USERSYNC;
604 }
605 $form_state['storage'] = $install_status;
606 }
607
608 /**
609 * Function _gallery_install_step_usersync().
610 * Step 5: User Synchronization
611 */
612 function _gallery_install_step_usersync(&$form, &$form_state, &$install_status) {
613 $title = t('Step 5: Initial User Synchronization') . ($install_status['usersync']['status'] ? ' '. t('(OK)') : '');
614 $form['usersync'] = array(
615 '#type' => 'fieldset',
616 '#title' => $title,
617 '#collapsible' => ($install_status['step'] > GALLERY_INSTALL_STEP_USERSYNC),
618 '#collapsed' => $install_status['usersync']['status'],
619 );
620
621 if ($install_status['usersync']['status']) {
622 $form['usersync']['#description'] = t('Your initial user synchronization has been completed already.<br />
623 If you want to resync all your users or you want to import users from an existing
624 Gallery2 setup, you can always use the <a href= "@gallery-usersync">Advanced Sync
625 </a> options in the Gallery user administration.',
626 array('@gallery-usersync' => url('admin/user/gallery/advanced')));
627 }
628 else {
629 $form['usersync']['#description'] = t('User synchronization is essential for the embedded Gallery to work correctly.
630 Drupal users/groups are usually synced with their Gallery counterparts when a
631 user/group is modified in Drupal. Missing user mappings will cause errors in your
632 embedded Gallery.<br /><br />
633 It is recommended that you sync your users now. However there can be reasons to
634 skip this step, e.g. if you have a working Gallery with many users and a fresh
635 Drupal installation. Note that the current user and the Drupal superuser (uid=1)
636 will always be sync.');
637
638 $form['usersync']['sync'] = array(
639 '#type' => 'submit',
640 '#value' => t('Sync users/groups'),
641 '#submit' => array('_gallery_install_step_usersync_sync')
642 );
643 $form['usersync']['skip'] = array(
644 '#type' => 'submit',
645 '#value' => t('Skip sync'),
646 '#submit' => array('_gallery_install_step_usersync_skip')
647 );
648 }
649 }
650
651 /**
652 * Function _gallery_install_step_usersync_skip().
653 */
654 function _gallery_install_step_usersync_skip($form, &$form_state) {
655 require_once(drupal_get_path('module', 'gallery') .'/gallery_user.inc');
656 global $user;
657 // Always sync (at least) the current user to avoid
658 // errors (unknown user) during GalleryEmbed::init()
659 gallery_user_modify($user, 'update', TRUE, array());
660 // Make sure the Drupal superuser (uid=1) always gets admin right
661 if ($user->uid != 1) {
662 gallery_user_modify(user_load(array('uid' => 1)), 'update', FALSE, array());
663 }
664
665 // Set the global status variable 'gallery_valid'
666 gallery_set_status(array(
667 'gallery_valid' => array(
668 'title' => t('Overall Status (Installation)'),
669 'severity' => GALLERY_SEVERITY_SUCCESS,
670 ),
671 ));
672 variable_set('gallery_valid', TRUE);
673 menu_rebuild();
674
675 $form_state['redirect'] = 'admin/settings/gallery';
676 $form_state['storage'] = array();
677 }
678
679 /**
680 * Function _gallery_install_step_usersync_sync().
681 */
682 function _gallery_install_step_usersync_sync($form, &$form_state) {
683 _gallery_install_step_usersync_skip($form, $form_state);
684
685 // Trigger bulk user/group sync
686 require_once(drupal_get_path('module', 'gallery') .'/gallery_user_admin.inc');
687 $batch = array(
688 'title' => t('Initial User Synchronization'),
689 'operations' => array(array('_gallery_user_advanced_sync', array())),
690 'file' => drupal_get_path('module', 'gallery') .'/gallery_user_admin.inc',
691 'finished' => '_gallery_user_advanced_finished'
692 );
693 batch_set($batch);
694 }
695
696 /**
697 * Function _gallery_install_locations().
698 */
699 function _gallery_install_locations($orig, $autodetect = TRUE) {
700 include_once(drupal_get_path('module', 'gallery') .'/G2EmbedDiscoveryUtilities.class');
701
702 // Result status array
703 $result = array(
704 'gallery_uri' => array(
705 'title' => t('URI of Gallery2'),
706 'severity' => GALLERY_SEVERITY_ERROR,
707 ),
708 'gallery_dir' => array(
709 'title' => t('Location of Gallery2'),
710 'severity' => GALLERY_SEVERITY_ERROR,
711 ),
712 'init' => array(
713 'title' => t('Gallery Init'),
714 'severity' => GALLERY_SEVERITY_ERROR,
715 ),
716 );
717
718 $vars = array();
719 $vars['locations_valid'] = TRUE;
720
721 // Strip the end /gallery if present and replace with index.php?q=gallery to avoid any
722 // url rewrite issues. See http://gallery.menalto.com/node/46181
723 // Note the use of index.php in all cases. Not needed for Apache, but for IIS
724
725 $embed_uri = isset($orig['gallery_embed_uri']) ? $orig['gallery_embed_uri'] : '';
726 if ($autodetect || empty($embed_uri)) {
727 $embed_uri = url('gallery');
728 // TODO
729 // i18n/locale rewrites the link to gallery to include a ?q=en or something
730 /*if (function_exists('language_url_rewrite')) {
731 $embed_uri = trim(substr($embed_uri, strlen($prefix)), '/');
732 }*/
733 if (substr($embed_uri, -8) == '/gallery') {
734 $embed_uri = substr($embed_uri, 0, -7) .'index.php?q=gallery';
735 }
736 }
737
738 // Normalise the Embed Application URI
739 $embed_uri = G2EmbedDiscoveryUtilities::normalizeEmbedUri($embed_uri);
740 $vars['gallery_embed_uri'] = $embed_uri;
741
742 // Normalise the G2 URI
743 $g2_uri = G2EmbedDiscoveryUtilities::normalizeG2Uri($orig['gallery_uri']);
744 $vars['gallery_uri'] = $g2_uri;
745
746 // Find the Gallery Directory and Embed URI
747 if ($autodetect) {
748 // Auto-Detect the G2 embed path
749 list($success, $embed_php_path, $error_string) = G2EmbedDiscoveryUtilities::getG2EmbedPathByG2Uri($g2_uri);
750 if ($success) {
751 // G2 Embed Path is found OK
752 $vars['gallery_dir'] = rtrim($embed_php_path, 'embed.php');
753 }
754 else {
755 // Something is wrong with the G2 URI given as the G2 embed path is not found
756 // Try getG2EmbedPathByG2UriEmbedUriAndLocation instead
757 $drupal_path = realpath(".") .'/';
758 list($success, $embed_php_path, $error_string) =
759 G2EmbedDiscoveryUtilities::getG2EmbedPathByG2UriEmbedUriAndLocation($g2_uri, $embed_uri, $drupal_path);
760 if ($success) {
761 // G2 Embed Path is found OK
762 $vars['gallery_dir'] = rtrim($embed_php_path, 'embed.php');
763 }
764 else {
765 $result['gallery_uri']['info'] = $error_string;
766 $result['gallery_uri']['status'] = FALSE;
767 $vars['gallery_dir'] = '';
768 $vars['locations_valid'] = FALSE;
769 }
770 }
771 // Check for config.php in the embed_path (not existing => invalid path)
772 // e.g. this is the path to the G2 codebase, but we have a multisite configuration
773 if (!empty($vars['gallery_dir'])) {
774 list($ok, $error_string) = G2EmbedDiscoveryUtilities::isFileReadable($vars['gallery_dir'] .'config.php');
775 if (!$ok) {
776 $result['gallery_dir']['status'] = FALSE;
777 $result['gallery_dir']['info'] = $error_string;
778 $vars['gallery_dir'] = '';
779 $vars['locations_valid'] = FALSE;
780 }
781 }
782 }
783 else {
784 // Do not autodetect the variables, but check the manually entered ones.
785 $embed_php_path = $orig['gallery_dir'];
786 $embed_php_path .= (substr($embed_php_path, -1) != '/') ? '/' : '';
787 // Check path can be read, adding embed.php if needed
788 list($ok, $error_string) = G2EmbedDiscoveryUtilities::isFileReadable($embed_php_path .'embed.php');
789 $vars['gallery_dir'] = $embed_php_path;
790 if (!$ok) {
791 $result['gallery_dir']['status'] = FALSE;
792 $result['gallery_dir']['info'] = $error_string;
793 $vars['locations_valid'] = FALSE;
794 }
795 else {
796 // Check for config.php in the embed_path (not existing => invalid path)
797 // e.g. this is the path to the G2 codebase, but we have a multisite configuration
798 list($ok, $error_string) = G2EmbedDiscoveryUtilities::isFileReadable($embed_php_path .'config.php');
799 if (!$ok) {
800 $result['gallery_dir']['status'] = FALSE;
801 $result['gallery_dir']['info'] = $error_string;
802 $vars['locations_valid'] = FALSE;
803 }
804 }
805 }
806
807 // If the filepaths seem correct then check Gallery2 works and has the correct module configs
808 if ($vars['locations_valid']) {
809 // Gallery install is thought valid, so init it.
810 if (!_gallery_init(TRUE, $vars, FALSE)) {
811 // Gallery install was supposed to be valid, but it still failed. Something is wrong.
812 $result['gallery_init']['status'] = FALSE;
813 $result['gallery_init']['info'] = t('Everything seemed OK, but the Gallery could still not be initialised.
814 Perhaps a manually entered value is incorrect.');
815 $vars['locations_valid'] = FALSE;
816 }
817 else {
818 gallery_version();
819 GalleryEmbed::done();
820 }
821 }
822
823 // Only return items where status has been set
824 foreach ($result as $key => $value) {
825 if (!isset($value['status'])) {
826 unset($result[$key]);
827 }
828 }
829
830 return array($vars['locations_valid'], $vars, $result);
831 }
832
833 /**
834 * Function _gallery_install_plugins().
835 */
836 function _gallery_install_plugins(&$install_status, $title = NULL, $collapse = FALSE) {
837 $form['plugins'] = array(
838 '#type' => 'fieldset',
839 '#title' => isset($title) ? $title : t('Drupal Modules / Gallery2 Plugins'),
840 '#collapsible' => TRUE,
841 '#collapsed' => $collapse,
842 );
843
844 $header = array(t('Name'), t('Status'), t('Description'));
845 $overall_plugin_severity = GALLERY_SEVERITY_UNKNOWN - 1;
846 $overall_plugin_status = GALLERY_PLUGIN_ENABLED;
847
848 // Wanted (required) G2 plugins
849 $rows = array();
850 $wanted_g2_plugins = module_invoke_all('gallery_plugin_info', GALLERY_PLUGIN_WANTED);
851 foreach ($wanted_g2_plugins as $key => $module) {
852 // Find the combination of status and severity of the plugin
853 $severity = ($module['status'] != GALLERY_PLUGIN_ENABLED) ? $module['severity'] : GALLERY_SEVERITY_SUCCESS;
854 $row = array();
855 $row[] = array('data' => $module['title'], 'align' => 'left', 'width' => '15%');
856 $row[] = array('data' => theme('gallery_severity_status_message', $severity, $module['status']), 'align' => 'center');
857 $row[] = array('data' => $module['info'], 'class' => 'description', 'id' => $key);
858 $rows[] = $row;
859 if ($module['status'] != GALLERY_PLUGIN_ENABLED && $severity > $overall_plugin_severity) {
860 $overall_plugin_severity = $severity;
861 $overall_plugin_status = $module['status'];
862 }
863 }
864 $form['plugins']['wanted_g2_plugins'] = array(
865 '#type' => 'fieldset',
866 '#title' => t('Recommended Gallery2 plugins'),
867 '#collapsible' => FALSE,
868 '#collapsed' => FALSE,
869 '#value' => theme('table', $header, $rows, array('class' => 'package')),
870 '#description' => t('These Gallery2 plugins support much of the functionality that the Drupal Gallery module provides.
871 None of them are strictly required, but you will almost certainly want to activate at least some
872 of them (particularly the Image Block plugin).'),
873 );
874
875 // Unwanted (interfering) G2 plugins
876 $rows = array();
877 $unwanted_g2_plugins = module_invoke_all('gallery_plugin_info', GALLERY_PLUGIN_UNWANTED);
878 foreach ($unwanted_g2_plugins as $key => $module) {
879 // Find the combination of status and severity of the plugin
880 $severity = ($module['status'] == GALLERY_PLUGIN_ENABLED) ? $module['severity'] : GALLERY_SEVERITY_SUCCESS;
881 $row = array();
882 $row[] = array('data' => $module['title'], 'align' => 'left', 'width' => '15%');
883 $row[] = array('data' => theme('gallery_severity_status_message', $severity, $module['status'], TRUE, TRUE), 'align' => 'center');
884 $row[] = array('data' => $module['info'], 'class' => 'description', 'id' => $key);
885 $rows[] = $row;
886 if ($module['status'] == GALLERY_PLUGIN_ENABLED && $severity > $overall_plugin_severity) {
887 $overall_plugin_severity = $severity;
888 $overall_plugin_status = $module['status'];
889 }
890 }
891 $form['plugins']['unwanted_g2_plugins'] = array(
892 '#type' => 'fieldset',
893 '#title' => t('Gallery2 plugins which should not be installed'),
894 '#collapsible' => FALSE,
895 '#collapsed' => FALSE,
896 '#value' => theme('table', $header, $rows, array('class' => 'package')),
897 '#description' => t('These Gallery2 plugins should not be used when Gallery2 is
898 embedded into Drupal as they may cause problems.'),
899 );
900
901 // Wanted (optional) Drupal modules
902 $rows = array();
903 $wanted_modules = module_invoke_all('gallery_plugin_info', GALLERY_PLUGIN_DRUPAL);
904 foreach ($wanted_modules as $key => $module) {
905 // Find the combination of status and severity of the plugin
906 $severity = $module['status'] ? GALLERY_SEVERITY_SUCCESS : $module['severity'];
907 $row = array();
908 $row[] = array('data' => $module['title'], 'align' => 'left', 'width' => '15%');
909 $row[] = array('data' => theme('gallery_severity_status_message', $severity, $module['status']), 'align' => 'center');
910 $row[] = array('data' => $module['info'], 'class' => 'description', 'id' => $key);
911 $rows[] = $row;
912 if ($module['status'] != GALLERY_PLUGIN_ENABLED && $severity > $overall_plugin_severity) {
913 $overall_plugin_severity = $severity;
914 $overall_plugin_status = $module['status'];
915 }
916 }
917 $form['plugins']['wanted_modules'] = array(
918 '#type' => 'fieldset',
919 '#title' => t('Optional Drupal modules'),
920 '#collapsible' => FALSE,
921 '#collapsed' => FALSE,
922 '#value' => theme('table', $header, $rows, array('class' => 'package')),
923 '#description' => t('These Drupal modules can optionally be used to enhance the functionality of the Gallery module.'),
924 );
925
926 // Check if no issue was found.
927 if ($overall_plugin_severity < GALLERY_SEVERITY_UNKNOWN) {
928 $overall_plugin_severity = GALLERY_SEVERITY_SUCCESS;
929 }
930
931 $install_status['plugins']['status'] = $overall_plugin_status;
932 $install_status['plugins']['severity'] = $overall_plugin_severity;
933 $install_status['plugins']['info'] = ($overall_plugin_status <= 0) ?
934 t('You may have some loss in functionality in your embedded Gallery2 (see below for details). You should check
935 the details and if the functionality is not important for your site you can just ignore this.') :
936 t('The status of all Drupal modules and Gallery2 plugins is optimal for your embedded Gallery2.');
937
938 return $form;
939 }
940
941 /**
942 * Function _gallery_install_urlrewrite_check().
943 */
944 function _gallery_install_urlrewrite_check(&$install_status) {
945 if (variable_get('clean_url', 0)) {
946 if (gallery_single_plugin_status('rewrite') == GALLERY_PLUGIN_ENABLED) {
947 _gallery_install_urlrewrite($public_path, $htaccess_path, TRUE);
948 // Check for writable .htaccess file
949 if (file_exists($htaccess_path .'.htaccess')) {
950 if (is_writable($htaccess_path .'.htaccess')) {
951 $install_status['urlrewrite']['status'] = TRUE;
952 $install_status['urlrewrite']['info'] = t('There is a writable .htaccess file in your defined Drupal directory
953 (@dir).', array('@dir' => $htaccess_path));
954 }
955 else {
956 $install_status['urlrewrite']['status'] = FALSE;
957 $install_status['urlrewrite']['info'] = t('The .htaccess file in your defined Drupal directory (@dir) is not
958 writable. Please CHMOD it to 644 (or 666 if 644 does not work).',
959 array('@dir' => $htaccess_path));
960 }
961 }
962 else {
963 $install_status['urlrewrite']['status'] = FALSE;
964 $install_status['urlrewrite']['info'] = t('There is no .htaccess file in your defined Drupal directory (@dir) or
965 the directory does not exist.', array('@dir' => $htaccess_path));
966 }
967 }
968 else {
969 $install_status['urlrewrite']['status'] = FALSE;
970 $install_status['urlrewrite']['info'] = t('Clean URLs are enabled in Drupal, but the Gallery2 URL Rewrite plugin
971 is unavailable.');
972 }
973 }
974 else {
975 $install_status['urlrewrite']['status'] = TRUE;
976 $install_status['urlrewrite']['info'] = t('Clean URLs are disabled in Drupal and the URL Rewrite plugin will not
977 be configured.');
978 }
979
980 // User selected to skip the URLRewrite step
981 if (variable_get('gallery_rewrite_disable', 0)) {
982 $install_status['urlrewrite']['status'] = TRUE;
983 $install_status['urlrewrite']['info'] = t('URL Rewrite has been disabled manually.');
984 }
985 }
986
987 /**
988 * Function _gallery_install_urlrewrite().
989 */
990 function _gallery_install_urlrewrite(&$public_path, &$htaccess_path, $load_config = FALSE) {
991 list($ret, $rewrite_api) = GalleryCoreApi::newFactoryInstance('RewriteApi');
992 if ($ret || !$rewrite_api) {
993 gallery_error(t('Error trying to create URL Rewrite plugin instance.'), $ret);
994 return FALSE;
995 }
996
997 list($ret, $params) = $rewrite_api->fetchEmbedConfig();
998 if ($ret) {
999 gallery_error(t('Error trying to fetch Embedded URL Rewrite configuration.'), $ret);
1000 return FALSE;
1001 }
1002
1003 // Load the configuration from G2 (or derive from Drupal path)
1004 if ($load_config) {
1005 if (empty($params['embeddedHtaccess'])) {
1006 $http = 'http'. (isset($_SERVER['HTTPS']) ? (($_SERVER['HTTPS'] == 'on') ? 's' : '') : '');
1007 $public_path = str_replace($http .'://'. $_SERVER['HTTP_HOST'], '', base_path());
1008 $htaccess_path = realpath(".") .'/';
1009 }
1010 else {
1011 $public_path = $params['embeddedLocation'];
1012 $htaccess_path = $params['embeddedHtaccess'];
1013 }
1014 }
1015
1016 // Check for trailing slash
1017 $public_path .= (substr($public_path, -1) != '/') ? '/' : '';
1018 $htaccess_path .= (substr($htaccess_path, -1) != '/') ? '/' : '';
1019
1020 if (!$load_config) {
1021 if (!file_exists($htaccess_path .'.htaccess')) {
1022 // File .htaccess does not exist
1023 if (is_writable($htaccess_path .'.htaccess')) {
1024 // Directory exists and is writable => try to create .htaccess
1025 $f = fopen($htaccess_path .'.htaccess', 'w');
1026 fclose($f);
1027 }
1028 else {
1029 return FALSE;
1030 }
1031 // Save the G2 rewrite values
1032 $params['embeddedLocation'] = $public_path;
1033 $params['embeddedHtaccess'] = $htaccess_path;
1034 list($ret, $code, $err) = $rewriteApi->saveEmbedConfig($params);
1035 if ($code != REWRITE_STATUS_OK) {
1036 list($ret, $errstr) = $err;
1037 $errstr = $code .' - '. $errstr;
1038 gallery_error(t('The Gallery2 URL Rewrite plugin returned the following error:') .'<br /><pre>'. $errstr .'</pre>');
1039 return FALSE;
1040 }
1041 return FALSE;
1042 }
1043 }
1044
1045 return TRUE;
1046 }