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