You are here

hosting.install in Hosting 7.4

Same filename and directory in other branches
  1. 5 hosting.install
  2. 6.2 hosting.install
  3. 7.3 hosting.install

Define database schemas and update functions for the hosting module.

File

hosting.install
View source
<?php

/**
 * @file
 *   Define database schemas and update functions for the hosting module.
 */

// HOSTING_MAX_ALIAS_LENGTH is defined here:
require_once dirname(__FILE__) . '/hosting.module';

/**
 * Implements hook_uninstall().
 */
function hosting_uninstall() {
  variable_del('hosting_dispatch_last_run');
  variable_del('hosting_features_form_settings');
  variable_del('hosting_queue_tasks_items');
  variable_del('hosting_default_db_server');
  variable_del('hosting_own_db_server');
  variable_del('hosting_update_6008_run');
  variable_del('hosting_dispatch_enabled');
  variable_del('hosting_cron_backup');
  variable_del('hosting_task_update_6000_run');
  variable_del('hosting_package_update_6002_run');
  variable_del('hosting_package_update_6004_run');
  variable_del('hosting_platform_update_6004_run');
  variable_del('hosting_queued_process_started');
  variable_del('hosting_queue_tasks_enabled');
}

/**
 * Implements hook_schema().
 */
function hosting_schema() {
  $schema['hosting_context'] = array(
    'fields' => array(
      'nid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'name' => array(
        'type' => 'varchar',
        'length' => HOSTING_MAX_ALIAS_LENGTH,
        'not null' => TRUE,
      ),
    ),
    'primary key' => array(
      'nid',
    ),
    'unique keys' => array(
      'name' => array(
        'name',
      ),
    ),
  );
  return $schema;
}

/**
 * Implements hook_install().
 */
function hosting_install() {
  hosting_create_roles();
  variable_set('error_level', ERROR_REPORTING_HIDE);
}

/**
 * Implements hook_update_N().
 *
 * Set weight for hosting module.
 */
function hosting_update_1() {
  $ret = array();
  $ret[] = update_sql("UPDATE {system} SET weight = 10 WHERE type='module' and name='hosting'");
  return $ret;
}

/**
 * Implements hook_update_N().
 *
 * Update function to remove unused table
 */
function hosting_update_2() {
  $ret = array();
  $ret[] = update_sql("DROP TABLE {hosting_config_template}");
  return $ret;
}

/**
 * Implements hook_update_N().
 *
 * Hide hostmaster and hostslave profiles.
 */
function hosting_update_3() {
  $ret = array();
  $result = db_query("select n.nid from {node} n left join hosting_package p on n.nid = p.nid where short_name in ('hostslave', 'hostmaster')");
  while ($obj = db_fetch_object($result)) {
    $in[] = (int) $obj->nid;
  }
  $in = implode(", ", $in);
  $ret[] = update_sql("UPDATE {node} SET status = 0 WHERE nid IN (" . $in . ")", $values);
  return $ret;
}

/**
 * Implements hook_update_N().
 *
 * Enable the modalframe and jquery_ui modules.
 */
function hosting_update_6000() {
  $ret = array();
  module_enable(array(
    'modalframe',
    'jquery_ui',
  ));
  return $ret;
}

/**
 * Implements hook_update_N().
 *
 * Change the default configured blocks to match eldir.
 */
function hosting_update_6001() {
  $ret = array();
  module_enable(array(
    'install_profile_api',
  ));
  drupal_load('module', 'install_profile_api');
  install_include(array(
    'block',
  ));
  $theme = 'eldir';
  install_disable_block('hosting', 'hosting_queues_summary', $theme);
  install_set_block('user', 0, $theme, 'right', 0);
  install_set_block('user', 1, $theme, 'right', 0);
  install_set_block('hosting', 'hosting_queues', $theme, 'right', 5);
  install_set_block('hosting', 'hosting_summary', $theme, 'right', 10);
  return $ret;
}

/**
 * Implements hook_update_N().
 *
 * no-op - this used to verify one platform and all sites but breaks
 * major upgrades.
 */
function hosting_update_6002() {
  return array();
}

/**
 * Implements hook_update_N().
 *
 * Lower the default amount of concurrent tasks.
 */
function hosting_update_6003() {
  $ret = array();
  variable_set('hosting_queue_tasks_items', 5);
  return $ret;
}

/**
 * Implements hook_update_N().
 *
 * Stricter initial permissions for the anonymous user.
 */
function hosting_update_6004() {
  $ret = array();
  install_include(array(
    'user',
  ));
  install_remove_permissions(install_get_rid('anonymous user'), array(
    'access content',
    'access all views',
  ));
  install_remove_permissions(install_get_rid('authenticated user'), array(
    'access content',
    'access all views',
  ));
  return $ret;
}

/**
 * Implements hook_update_N().
 *
 * Move some menu items to the primary links menu.
 */
function hosting_update_6005() {
  drupal_install_modules(array(
    'hosting_server',
  ));
  menu_rebuild();
  install_include(array(
    'menu',
  ));
  $menu_name = variable_get('menu_primary_links_source', 'primary-links');
  $items = install_menu_get_items('hosting/servers');
  $item = db_fetch_array(db_query("SELECT * FROM {menu_links} WHERE mlid = %d", $items[0]['mlid']));
  $item['menu_name'] = $menu_name;
  $item['customized'] = 1;
  $item['options'] = unserialize($item['options']);
  install_menu_update_menu_item($item);
  $items = install_menu_get_items('hosting/sites');
  $item = db_fetch_array(db_query("SELECT * FROM {menu_links} WHERE mlid = %d", $items[0]['mlid']));
  $item['menu_name'] = $menu_name;
  $item['customized'] = 1;
  $item['options'] = unserialize($item['options']);
  install_menu_update_menu_item($item);
  menu_rebuild();
  return array();
}

/**
 * Implements hook_update_N().
 *
 * Move the platforms into primary links.
 */
function hosting_update_6006() {
  install_include(array(
    'menu',
  ));
  $menu_name = variable_get('menu_primary_links_source', 'primary-links');
  $items = install_menu_get_items('hosting/platforms');
  $item = db_fetch_array(db_query("SELECT * FROM {menu_links} WHERE mlid = %d", $items[0]['mlid']));
  $item['menu_name'] = $menu_name;
  $item['customized'] = 1;
  $item['options'] = unserialize($item['options']);
  install_menu_update_menu_item($item);
  return array();
}

/**
 * Implements hook_update_N().
 *
 * Update the default db_server now that we've merged the webserver and dbserver nodes
 * See http://drupal.org/node/731550
 */
function hosting_update_6007() {
  variable_set('hosting_default_db_server', variable_get('hosting_default_web_server', 3));
  variable_set('hosting_own_db_server', variable_get('hosting_default_web_server', 3));
  return array();
}

/**
 * Implements hook_update_N().
 *
 * Add hosting_context table to map alias names.
 */
function hosting_update_6008() {
  $return = array();
  if (!variable_get('hosting_update_6008_run', FALSE)) {
    db_create_table($return, 'hosting_context', array(
      'fields' => array(
        'nid' => array(
          'type' => 'int',
          'unsigned' => TRUE,
          'not null' => TRUE,
          'default' => 0,
        ),
        'name' => array(
          'type' => 'varchar',
          'length' => 50,
          'not null' => TRUE,
        ),
      ),
      'primary key' => array(
        'nid',
      ),
      'unique keys' => array(
        'name' => array(
          'name',
        ),
      ),
    ));
    variable_set('hosting_update_6008_run', TRUE);
    $records = array();
    $result = db_query("SELECT n.nid, title FROM {node} n LEFT JOIN {hosting_site} s ON n.nid=s.nid WHERE s.status <> -2 AND n.type='site'");
    while ($object = db_fetch_object($result)) {
      $records[$object->nid] = $object->title;
    }
    $result = db_query("SELECT n.nid, title FROM {node} n LEFT JOIN {hosting_server} s ON n.nid=s.nid WHERE n.status = 1 AND n.type='server'");
    while ($object = db_fetch_object($result)) {
      $records[$object->nid] = 'server_' . preg_replace("/[!\\W\\.\\-]/", "", $object->title);
    }

    // We start with the web server because we assume that the main hostmaster site is installed locally.
    $server_id = variable_get('hosting_default_web_server', 3);
    $records[$server_id] = 'server_master';
    foreach ($records as $nid => $name) {
      hosting_context_register($nid, $name);
    }
  }
  return $return;
}

/**
 * Implements hook_update_N().
 *
 * Fix the reference to the master server so that it no longer points at localhost.
 */
function hosting_update_6009() {

  // This undoes the change we made previously in update_6008, but can be run again.
  $ret = array();
  db_query("UPDATE {hosting_context} SET name='server_master' WHERE name='server_localhost'");
  $nid = db_result(db_query("SELECT nid from {hosting_context} WHERE name='server_master'"));
  $uname = php_uname('n');
  db_query("UPDATE {node} SET title='%s' WHERE nid=%d", $uname, $nid);
  db_query("UPDATE {node_revisions} SET title='%s' WHERE nid=%d", $uname, $nid);
  return $ret;
}

/**
 * Implements hook_update_N().
 *
 * Add url aliases for the context names that are registered.
 */
function hosting_update_6010() {

  // We include the file instead of enabling the module,
  // because we do not want the overhead of having all the
  // path UI stuff on nodes.
  require_once "./modules/path/path.module";
  $ret = array();
  $result = db_query("SELECT nid, name FROM {hosting_context}");
  while ($obj = db_fetch_object($result)) {
    path_set_alias("node/{$obj->nid}", "hosting/c/{$obj->name}");
  }
  return $ret;
}

/**
 * Implements hook_update_N().
 *
 * Clients and packages were imported as belonging to the anonymous user. Reassign them to the admin user.
 */
function hosting_update_6011() {
  $ret = array();
  db_query("UPDATE {node} SET uid=1 WHERE uid=0 AND type IN ('client', 'package')");
  return $ret;
}

/**
 * Implements hook_update_N().
 *
 * Remove the hosting summary block.
 */
function hosting_update_6012() {
  $ret = array();
  drupal_load('module', 'install_profile_api');
  install_include(array(
    'block',
  ));
  $theme = 'eldir';
  install_disable_block('hosting', 'hosting_summary', $theme);
  return $ret;
}

/**
 * Implements hook_update_N().
 *
 * Add a permission allowing users to view the 'site disabled' message, which got
 * lost when 'access content' permission was revoked a while ago.
 */
function hosting_update_6013() {
  $ret = array();
  install_include(array(
    'user',
  ));
  install_add_permissions(install_get_rid('anonymous user'), array(
    'access disabled sites',
  ));
  install_add_permissions(install_get_rid('authenticated user'), array(
    'access disabled sites',
  ));
  return $ret;
}

/**
 * Implements hook_update_N().
 *
 * Restore proper weight to the hosting module.
 *
 * For some obscure reason, the weight is 10 which makes its updates run
 * after all others, which gave us constant issues during upgrades.
 */
function hosting_update_6014() {
  $ret = array();
  $ret[] = update_sql("UPDATE {system} SET weight = 0 WHERE type='module' and name='hosting'");
  return $ret;
}

/**
 * Implements hook_update_N().
 *
 * Delete URL aliases for sites that have been deleted.
 *
 * Ideally we'd do this in a single DB query, but there isn't a nice performant
 * way to do it, so we do more DB queries instead of using potentially a lot of
 * memory. Basically this may take a long time to run, but it shouldn't run out
 * of resources doing so.
 */
function hosting_update_6015(&$sandbox) {
  $ret = array();
  if (!isset($sandbox['progress'])) {
    $sandbox['progress'] = 0;
    $sandbox['current_pid'] = 0;
    $sandbox['max'] = db_result(db_query('SELECT COUNT(pid) FROM {url_alias} WHERE dst LIKE "hosting/c/%" AND pid > 0'));
  }
  $aliases = db_query_range("SELECT pid, src, dst FROM {url_alias} WHERE dst LIKE 'hosting/c/%' AND pid > %d ORDER BY pid ASC", $sandbox['current_pid'], 0, 50);
  while ($alias = db_fetch_object($aliases)) {

    // Get the node ID for the alias.
    $nid = preg_replace('#^node/#', '', $alias->src);
    if (is_numeric($nid)) {

      // Check to see if the corresponding context has been deleted.
      if (!db_result(db_query('SELECT COUNT(nid) FROM {hosting_context} WHERE nid = %d', $nid))) {
        db_query('DELETE FROM {url_alias} WHERE pid = %d', $alias->pid);
      }
    }
    $sandbox['progress']++;
    $sandbox['current_pid'] = $alias->pid;
  }
  $ret['#finished'] = empty($sandbox['max']) ? 1 : $sandbox['progress'] / $sandbox['max'];
  return $ret;
}

/**
 * Replace lists with Views.
 */
function hosting_update_6201() {
  $ret = array();

  // Remove menu links that will be replaced by Views
  menu_link_delete(NULL, 'hosting/platforms');
  menu_link_delete(NULL, 'hosting/sites');

  // Enable and install Views and Views Bulk operations.
  drupal_install_modules(array(
    'views',
    'views_bulk_operations',
  ));

  // Flush all Drupal caches.
  drupal_flush_all_caches();

  // Temporarily enable Install Profile API module and load includes.
  module_enable(array(
    'install_profile_api',
  ));
  drupal_load('module', 'install_profile_api');
  install_include(array(
    'block',
  ));

  // Enable blocks.
  $theme = 'eldir';
  install_set_block('views', 'hosting_task_list-block', $theme, 'right', 0);
  install_set_block('views', '-exp-hosting_site_list-page_sites', $theme, 'content_top', 0, 1, 'hosting/sites');
  install_set_block('views', 'hosting_site_list-block_platform', $theme, 'content_bottom', 0, 1, 'hosting/c/platform_*');
  install_set_block('views', 'hosting_site_list-block_profile', $theme, 'content_bottom', 0, 2, "<?php\n\$node = menu_get_object();\nif (!empty(\$node)) {\n  return \$node->package_type == 'profile';\n}\n?>");
  install_set_block('views', 'hosting_site_list-block_client', $theme, 'content_bottom', 0, 2, "<?php\n\$node = menu_get_object();\nif (!empty(\$node)) {\n  return \$node->type == 'client';\n}\n?>");
  module_disable(array(
    'install_profile_api',
  ));
  return $ret;
}

/**
 * Provide a welcome message for anonymous users.
 */
function hosting_update_6202() {
  $ret = array();

  // Flush the menu cache.
  menu_rebuild();
  return $ret;
}

/**
 * Delete hosting features junk variables.
 */
function hosting_update_6203() {
  $ret = array();
  $features = hosting_get_features(TRUE);
  foreach ($features as $feature => $info) {
    if (variable_get($feature, FALSE) != FALSE) {
      variable_del($feature);
    }
  }
}

/**
 * Create new roles.
 */
function hosting_update_6204() {
  $ret = array();

  // Temporarily enable Install Profile API module and load includes.
  module_enable(array(
    'install_profile_api',
  ));
  module_load_include('inc', 'install_profile_api', 'core/user');
  install_remove_permissions(install_get_rid('aegir client'), array(
    'access all views',
  ));
  install_add_role('aegir platform manager');
  install_add_permissions(install_get_rid('aegir platform manager'), array(
    'create clone task',
    'create migrate task',
    'create platform',
    'delete platform',
    'edit platform',
    'view locked platforms',
    'view platform',
    'create site',
    'delete site',
    'edit site',
    'view site',
    'access task logs',
    'create lock task',
    'create unlock task',
    'view revisions',
    'search content',
    'use advanced search',
  ));
  install_add_role('aegir administrator');
  install_add_permissions(install_get_rid('aegir administrator'), array(
    'access disabled sites',
    'access hosting wizard',
    'administer hosting',
    'administer hosting features',
    'administer hosting queues',
    'administer hosting settings',
    'administer hosting aliases',
    'create site aliases',
    'administer clients',
    'create client',
    'delete own client',
    'edit client uname',
    'edit client users',
    'edit own client',
    'view client',
    'create clone task',
    'create migrate task',
    'create package',
    'delete package',
    'edit package',
    'view package',
    'create platform',
    'delete platform',
    'edit platform',
    'view locked platforms',
    'view platform',
    'edit all quotas',
    'view all quotas',
    'view own quota',
    'create server',
    'delete server',
    'edit server',
    'view server',
    'access hosting signup form',
    'administer sites',
    'create site',
    'delete site',
    'edit site',
    'view site',
    'create ssl certificate',
    'access task logs',
    'administer tasks',
    'cancel own tasks',
    'create backup task',
    'create backup-delete task',
    'create delete task',
    'create disable task',
    'create enable task',
    'create lock task',
    'create login-reset task',
    'create restore task',
    'create unlock task',
    'create verify task',
    'retry failed tasks',
    'view own tasks',
    'view task',
  ));
  module_disable(array(
    'install_profile_api',
  ));
  return $ret;
}

/**
 * Add permissions to roles.
 */
function hosting_update_6205() {
  $ret = array();

  // Temporarily enable Install Profile API module and load includes.
  module_enable(array(
    'install_profile_api',
  ));
  module_load_include('inc', 'install_profile_api', 'core/user');
  install_add_permissions(install_get_rid('aegir client'), array(
    'view own quota',
    'create site aliases',
    'create verify task',
    'create clone task',
    'create migrate task',
    'create ssl certificate',
    'access task logs',
    'view package',
    'view platform',
    'view revisions',
    'edit site',
  ));
  install_add_permissions(install_get_rid('aegir account manager'), array(
    'administer clients',
    'access content',
    'access user profiles',
    'edit all quotas',
    'view all quotas',
    'view own quota',
    'edit client uname',
    'view_site',
  ));
  install_add_permissions(install_get_rid('aegir platform manager'), array(
    'view package',
    'administer platforms',
    'cancel own tasks',
    'view own tasks',
    'access content',
    'view task',
    'administer sites',
    'create verify task',
    'retry failed tasks',
    'administer clients',
    'administer tasks',
  ));
  install_add_permissions(install_get_rid('aegir administrator'), array(
    'access administration menu',
    'administer platforms',
    'administer servers',
    'access content',
    'view revisions',
    'administer hosting aliases',
    'create site aliases',
    'edit all quotas',
    'view all quotas',
    'view own quota',
    'access hosting signup form',
    'create ssl certificate',
    'access user profiles',
    'update status of tasks',
  ));
  module_disable(array(
    'install_profile_api',
  ));
  return $ret;
}

/**
 * Enable the jquery_update module.
 */
function hosting_update_6206() {
  $ret = array();
  module_enable(array(
    'jquery_update',
  ));
  return $ret;
}

/**
 * Alter the length available for a site alias to 235.
 *
 * https://drupal.org/node/1971348
 */
function hosting_update_6207() {
  $ret = array();
  $spec = array(
    'type' => 'varchar',
    // @see HOSTING_MAX_ALIAS_LENGTH
    'length' => 235,
    'not null' => TRUE,
  );
  db_change_field($ret, 'hosting_context', 'name', 'name', $spec);
  return $ret;
}

/**
 * Save 'aegir_hostmaster_site_nid' variable.
 */
function hosting_update_7000() {
  $nid = db_query('SELECT site.nid
                     FROM hosting_site site
                     JOIN hosting_package_instance pkgi
                       ON pkgi.rid = site.nid
                     JOIN hosting_package pkg
                       ON pkg.nid = pkgi.package_id
                    WHERE pkg.short_name = \'hostmaster\'')
    ->fetchField();
  variable_set('aegir_hostmaster_site_nid', $nid);
}

/**
 * Suppress error messages by default.
 */
function hosting_update_7001() {
  variable_set('error_level', ERROR_REPORTING_HIDE);
}

/**
 * Force a re-verification of all enabled sites,
 * to re-generate their Apache virtualhost configurations to use
 * mitigation measures for SA 2018-002.
 */
function hosting_update_7302() {

  // node_access_rebuild from the the update hook fails without the following defintion.
  if (!defined('HOSTING_ADMIN_CLIENT')) {
    define('HOSTING_ADMIN_CLIENT', variable_get('hosting_admin_client', 1));
  }
  $servers = hosting_get_servers('http');
  foreach ($servers as $server_nid => $title) {
    hosting_add_task($server_nid, 'verify');
  }
  $result = db_query("SELECT nid FROM {hosting_site} WHERE status=1");
  while ($site = $result
    ->fetch()) {
    hosting_add_task($site->nid, 'verify');
  }
  $ret = array();
  return $ret;
}

/**
 * Force a re-verification of all enabled sites.
 */
function hosting_update_7303() {

  // node_access_rebuild from the the update hook fails without the following defintion.
  if (!defined('HOSTING_ADMIN_CLIENT')) {
    define('HOSTING_ADMIN_CLIENT', variable_get('hosting_admin_client', 1));
  }
  $apache_nids = array();
  $http_servers = hosting_get_servers('http', FALSE);
  $servers = node_load_multiple(array_keys($http_servers));
  foreach ($servers as $server) {
    if (!empty($server->services['http'])) {
      if ($server->services['http']->type === 'apache') {
        $apache_nids[] = $server->nid;
      }
    }
  }
  if ($apache_nids) {
    $site_query = db_select('hosting_site', 'hs');
    $site_query
      ->innerJoin('hosting_platform', 'hp', 'hs.platform = hp.nid');
    $site_query
      ->addField('hs', 'nid');
    $site_query
      ->condition('hs.status', 1);
    $site_query
      ->condition('hp.status', 1);
    $site_query
      ->condition('hp.web_server', $apache_nids, 'IN');
    $result = $site_query
      ->execute();
    while ($site = $result
      ->fetch()) {
      hosting_add_task($site->nid, 'verify');
    }
  }
}

Functions

Namesort descending Description
hosting_install Implements hook_install().
hosting_schema Implements hook_schema().
hosting_uninstall Implements hook_uninstall().
hosting_update_1 Implements hook_update_N().
hosting_update_2 Implements hook_update_N().
hosting_update_3 Implements hook_update_N().
hosting_update_6000 Implements hook_update_N().
hosting_update_6001 Implements hook_update_N().
hosting_update_6002 Implements hook_update_N().
hosting_update_6003 Implements hook_update_N().
hosting_update_6004 Implements hook_update_N().
hosting_update_6005 Implements hook_update_N().
hosting_update_6006 Implements hook_update_N().
hosting_update_6007 Implements hook_update_N().
hosting_update_6008 Implements hook_update_N().
hosting_update_6009 Implements hook_update_N().
hosting_update_6010 Implements hook_update_N().
hosting_update_6011 Implements hook_update_N().
hosting_update_6012 Implements hook_update_N().
hosting_update_6013 Implements hook_update_N().
hosting_update_6014 Implements hook_update_N().
hosting_update_6015 Implements hook_update_N().
hosting_update_6201 Replace lists with Views.
hosting_update_6202 Provide a welcome message for anonymous users.
hosting_update_6203 Delete hosting features junk variables.
hosting_update_6204 Create new roles.
hosting_update_6205 Add permissions to roles.
hosting_update_6206 Enable the jquery_update module.
hosting_update_6207 Alter the length available for a site alias to 235.
hosting_update_7000 Save 'aegir_hostmaster_site_nid' variable.
hosting_update_7001 Suppress error messages by default.
hosting_update_7302 Force a re-verification of all enabled sites, to re-generate their Apache virtualhost configurations to use mitigation measures for SA 2018-002.
hosting_update_7303 Force a re-verification of all enabled sites.