diff --git a/sites/all/modules/unl/cron.php b/sites/all/modules/unl/cron.php index c5df327bb7bd3d71add7aed1d5bb2a5a319ea345..dcad3ae526633963b4e59111cb8a32cf95e000a8 100644 --- a/sites/all/modules/unl/cron.php +++ b/sites/all/modules/unl/cron.php @@ -37,13 +37,14 @@ function unl_add_sites() { ->fields(array('installed' => 1)) ->condition('site_id', $row['site_id']) ->execute(); - if (unl_add_site($row['site_path'], $row['uri'], $row['clean_url'], $row['db_prefix'], $row['site_id'])) { + try { + unl_add_site($row['site_path'], $row['uri'], $row['clean_url'], $row['db_prefix'], $row['site_id']); db_update('unl_sites') ->fields(array('installed' => 2)) ->condition('site_id', $row['site_id']) ->execute(); - } - else { + } catch (Exception $e) { + watchdog('unl cron', $e->getMessage(), array(), WATCHDOG_ERROR); db_update('unl_sites') ->fields(array('installed' => 5)) ->condition('site_id', $row['site_id']) @@ -59,12 +60,13 @@ function unl_remove_sites() { ->fields(array('installed' => 4)) ->condition('site_id', $row['site_id']) ->execute(); - if (unl_remove_site($row['site_path'], $row['uri'], $row['db_prefix'], $row['site_id'])) { + try { + unl_remove_site($row['site_path'], $row['uri'], $row['db_prefix'], $row['site_id']); db_delete('unl_sites') ->condition('site_id', $row['site_id']) ->execute(); - } - else { + } catch (Exception $e) { + watchdog('unl cron', $e->getMessage(), array(), WATCHDOG_ERROR); db_update('unl_sites') ->fields(array('installed' => 5)) ->condition('site_id', $row['site_id']) @@ -86,13 +88,14 @@ function unl_add_aliases() { ->fields(array('installed' => 1)) ->condition('site_alias_id', $row->site_alias_id) ->execute(); - if (unl_add_alias($row->uri, $row->base_uri, $row->path, $row->site_alias_id)) { + try { + unl_add_alias($row->uri, $row->base_uri, $row->path, $row->site_alias_id); db_update('unl_sites_aliases') ->fields(array('installed' => 2)) ->condition('site_alias_id', $row->site_alias_id) ->execute(); - } - else { + } catch (Exception $e) { + watchdog('unl cron', $e->getMessage(), array(), WATCHDOG_ERROR); db_update('unl_sites_aliases') ->fields(array('installed' => 5)) ->condition('site_alias_id', $row->site_alias_id) @@ -112,12 +115,13 @@ function unl_remove_aliases() { ->fields(array('installed' => 4)) ->condition('site_alias_id', $row->site_alias_id) ->execute(); - if (unl_remove_alias($row->base_uri, $row->path, $row->site_alias_id)) { + try { + unl_remove_alias($row->base_uri, $row->path, $row->site_alias_id); db_delete('unl_sites_aliases') ->condition('site_alias_id', $row->site_alias_id) ->execute(); - } - else { + } catch (Exception $e) { + watchdog('unl cron', $e->getMessage(), array(), WATCHDOG_ERROR); db_update('unl_sites_aliases') ->fields(array('installed' => 5)) ->condition('site_alias_id', $row->site_alias_id) @@ -137,13 +141,14 @@ function unl_add_page_aliases() { ->fields(array('installed' => 1)) ->condition('page_alias_id', $row->page_alias_id) ->execute(); - if (unl_add_page_alias($row->from_uri, $row->to_uri, $row->page_alias_id)) { + try { + unl_add_page_alias($row->from_uri, $row->to_uri, $row->page_alias_id); db_update('unl_page_aliases') ->fields(array('installed' => 2)) ->condition('page_alias_id', $row->page_alias_id) ->execute(); - } - else { + } catch (Exception $e) { + watchdog('unl cron', $e->getMessage(), array(), WATCHDOG_ERROR); db_update('unl_sites_aliases') ->fields(array('installed' => 5)) ->condition('site_alias_id', $row->page_alias_id) @@ -163,12 +168,13 @@ function unl_remove_page_aliases() { ->fields(array('installed' => 4)) ->condition('page_alias_id', $row->page_alias_id) ->execute(); - if (unl_remove_page_alias($row->page_alias_id)) { + try { + unl_remove_page_alias($row->page_alias_id); db_delete('unl_page_aliases') ->condition('page_alias_id', $row->page_alias_id) ->execute(); - } - else { + } catch (Exception $e) { + watchdog('unl cron', $e->getMessage(), array(), WATCHDOG_ERROR); db_update('unl_page_aliases') ->fields(array('installed' => 5)) ->condition('page_alias_id', $row->page_alias_id) @@ -209,11 +215,14 @@ function unl_add_site($site_path, $uri, $clean_url, $db_prefix, $site_id) { if ($site_mail) { $command .= " --site-mail=$site_mail"; } - echo shell_exec($command); + + $result = shell_exec($command); + echo $result; + if (stripos($result, 'Drush command terminated abnormally due to an unrecoverable error.') !== FALSE) { + throw new Exception('Error while running drush site-install.'); + } unl_add_site_to_htaccess($site_id, $site_path, FALSE); - - return TRUE; } function unl_remove_site($site_path, $uri, $db_prefix, $site_id) { @@ -232,11 +241,11 @@ function unl_remove_site($site_path, $uri, $db_prefix, $site_id) { // A couple checks to make sure we aren't deleting something we shouldn't be. if (substr($sites_subdir, 0, strlen(DRUPAL_ROOT . '/sites/')) != DRUPAL_ROOT . '/sites/') { - return FALSE; + throw new Exception('Attempt to delete a directory outside DRUPAL_ROOT was aborted.'); } if (strlen($sites_subdir) <= strlen(DRUPAL_ROOT . '/sites/')) { - return FALSE; + throw new Exception('Attempt to delete a directory outside DRUPAL_ROOT was aborted.'); } // Drop the site's tables @@ -260,31 +269,29 @@ function unl_remove_site($site_path, $uri, $db_prefix, $site_id) { if (class_exists('MemCacheDrupal', FALSE)) { dmemcache_flush(); } - - return TRUE; } function unl_add_alias($site_uri, $base_uri, $path, $alias_id) { $alias_uri = $base_uri . $path; $real_config_dir = unl_get_sites_subdir($site_uri); $alias_config_dir = unl_get_sites_subdir($alias_uri, FALSE); - $result = symlink($real_config_dir, DRUPAL_ROOT . '/sites/' . $alias_config_dir); - + + unl_add_alias_to_sites_php($alias_config_dir, $real_config_dir, $alias_id); if ($path) { unl_add_site_to_htaccess($alias_id, $path, TRUE); } - - return TRUE; } function unl_remove_alias($base_uri, $path, $alias_id) { $alias_uri = $base_uri . $path; $alias_config_dir = unl_get_sites_subdir($alias_uri, FALSE); + /* TODO: Remove the next line once all sites have been converted + * to the new method of creating aliases. + */ unlink(DRUPAL_ROOT . '/sites/' . $alias_config_dir); - + + unl_remove_alias_from_sites_php($alias_id); unl_remove_site_from_htaccess($alias_id, TRUE); - - return TRUE; } function unl_add_page_alias($from_uri, $to_uri, $alias_id) { @@ -292,14 +299,10 @@ function unl_add_page_alias($from_uri, $to_uri, $alias_id) { $path = parse_url($from_uri, PHP_URL_PATH); unl_add_page_alias_to_htaccess($alias_id, $host, $path, $to_uri); - - return TRUE; } function unl_remove_page_alias($alias_id) { unl_remove_page_alias_from_htaccess($alias_id); - - return TRUE; } function unl_add_site_to_htaccess($site_id, $site_path, $is_alias) { @@ -314,11 +317,13 @@ function unl_add_site_to_htaccess($site_id, $site_path, $is_alias) { $site_path .= '/'; } + unl_require_writable(DRUPAL_ROOT . '/.htaccess'); + $stub_token = ' # %UNL_CREATION_TOOL_STUB%'; $htaccess = file_get_contents(DRUPAL_ROOT . '/.htaccess'); $stub_pos = strpos($htaccess, $stub_token); if ($stub_pos === FALSE) { - return FALSE; + throw new Exception('Unable to find stub site entry in .htaccess.'); } $new_htaccess = substr($htaccess, 0, $stub_pos) . " # %UNL_START_{$site_or_alias}_ID_{$site_id}%\n"; @@ -340,6 +345,8 @@ function unl_remove_site_from_htaccess($site_id, $is_alias) { $site_or_alias = 'SITE'; } + unl_require_writable(DRUPAL_ROOT . '/.htaccess'); + $htaccess = file_get_contents(DRUPAL_ROOT . '/.htaccess'); $site_start_token = "\n # %UNL_START_{$site_or_alias}_ID_{$site_id}%"; $site_end_token = " # %UNL_END_{$site_or_alias}_ID_{$site_id}%\n"; @@ -347,8 +354,9 @@ function unl_remove_site_from_htaccess($site_id, $is_alias) { $start_pos = strpos($htaccess, $site_start_token); $end_pos = strpos($htaccess, $site_end_token); + // If its already gone, we don't need to do anything. if ($start_pos === FALSE || $end_pos === FALSE) { - return FALSE; + return; } $new_htaccess = substr($htaccess, 0, $start_pos) . substr($htaccess, $end_pos + strlen($site_end_token)) @@ -357,11 +365,13 @@ function unl_remove_site_from_htaccess($site_id, $is_alias) { } function unl_add_page_alias_to_htaccess($site_id, $host, $path, $to_uri) { + unl_require_writable(DRUPAL_ROOT . '/.htaccess'); + $stub_token = ' # %UNL_CREATION_TOOL_STUB%'; $htaccess = file_get_contents(DRUPAL_ROOT . '/.htaccess'); $stub_pos = strpos($htaccess, $stub_token); if ($stub_pos === FALSE) { - return FALSE; + throw new Exception('Unable to find stub page alias entry in .htaccess.'); } $new_htaccess = substr($htaccess, 0, $stub_pos) . " # %UNL_START_PAGE_ALIAS_ID_{$site_id}%\n" @@ -376,6 +386,8 @@ function unl_add_page_alias_to_htaccess($site_id, $host, $path, $to_uri) { } function unl_remove_page_alias_from_htaccess($site_id) { + unl_require_writable(DRUPAL_ROOT . '/.htaccess'); + $htaccess = file_get_contents(DRUPAL_ROOT . '/.htaccess'); $site_start_token = "\n # %UNL_START_PAGE_ALIAS_ID_{$site_id}%"; $site_end_token = " # %UNL_END_PAGE_ALIAS_ID_{$site_id}%\n"; @@ -383,11 +395,57 @@ function unl_remove_page_alias_from_htaccess($site_id) { $start_pos = strpos($htaccess, $site_start_token); $end_pos = strpos($htaccess, $site_end_token); + // If its already gone, we don't need to do anything. if ($start_pos === FALSE || $end_pos === FALSE) { - return FALSE; + return; } $new_htaccess = substr($htaccess, 0, $start_pos) . substr($htaccess, $end_pos + strlen($site_end_token)) ; file_put_contents(DRUPAL_ROOT . '/.htaccess', $new_htaccess); } + +function unl_add_alias_to_sites_php($alias_site_dir, $real_site_dir, $alias_id) { + unl_require_writable(DRUPAL_ROOT . '/sites/sites.php'); + + $stub_token = '# %UNL_CREATION_TOOL_STUB%'; + $sites_php = file_get_contents(DRUPAL_ROOT . '/sites/sites.php'); + $stub_pos = strpos($sites_php, $stub_token); + if ($stub_pos === FALSE) { + throw new Exception('Unable to find stub alias entry in sites.php.'); + } + $new_sites_php = substr($sites_php, 0, $stub_pos) + . "# %UNL_START_ALIAS_ID_{$alias_id}%\n" + . "\$sites['$alias_site_dir'] = '$real_site_dir';\n" + . "# %UNL_END_ALIAS_ID_{$alias_id}%\n\n" + . $stub_token + . substr($sites_php, $stub_pos + strlen($stub_token)) + ; + file_put_contents(DRUPAL_ROOT . '/sites/sites.php', $new_sites_php); +} + +function unl_remove_alias_from_sites_php($alias_id) { + unl_require_writable(DRUPAL_ROOT . '/sites/sites.php'); + + $sites_php = file_get_contents(DRUPAL_ROOT . '/sites/sites.php'); + $site_start_token = "\n# %UNL_START_ALIAS_ID_{$alias_id}%"; + $site_end_token = "# %UNL_END_ALIAS_ID_{$alias_id}%\n"; + + $start_pos = strpos($sites_php, $site_start_token); + $end_pos = strpos($sites_php, $site_end_token); + + // If its already gone, we don't need to do anything. + if ($start_pos === FALSE || $end_pos === FALSE) { + return; + } + $new_sites_php = substr($sites_php, 0, $start_pos) + . substr($sites_php, $end_pos + strlen($site_end_token)) + ; + file_put_contents(DRUPAL_ROOT . '/sites/sites.php', $new_sites_php); +} + +function unl_require_writable($path) { + if (!is_writable($path)) { + throw new Exception('The file "' . $path . '" needs to be writable and is not.'); + } +} diff --git a/sites/example.sites.php b/sites/example.sites.php index 06f392e874c330970dd150ab4f78695f62f7dd8d..4fa065da5ca745753a958bd447b6d814f0123ab4 100644 --- a/sites/example.sites.php +++ b/sites/example.sites.php @@ -50,4 +50,17 @@ * (starting with unl.edu). The purpose is to prevent sites like * unlcms.unl.edu/ncard from showing up at bike.unl.edu/ncard. */ -# $default_domains = array('example.unl.edu', 'example-test.unl.edu'); \ No newline at end of file +# $default_domains = array('example.unl.edu', 'example-test.unl.edu'); + +/** + * Stub for UNL Site creation tool. + * + * The following comments are needed for the UNL Site creation tool to + * create site aliases. Make sure to include them in your sites.php! + */ +# THIS SECTION IS AUTOMATICALY GENERATED. +# DO NOT EDIT!!!! + +# %UNL_CREATION_TOOL_STUB% + +# END AUTOMATICALLY GENERATED AREA.