You are here

hosting_db_server.module in Hosting 7.4

Allow Hostmaster to configure database servers.

File

db_server/hosting_db_server.module
View source
<?php

/**
 * @file
 * Allow Hostmaster to configure database servers.
 */

/**
 * Implements hook_hosting_service().
 */
function hosting_db_server_hosting_service() {
  return array(
    'mysql' => 'db',
  );
}

/**
 * Implements hook_hosting_service_type().
 */
function hosting_db_server_hosting_service_type() {
  return array(
    'db' => array(
      'title' => t('Database'),
    ),
  );
}

/**
 * Find the database server.
 */
function hosting_get_db_server($hostname) {
  if (_hosting_valid_ip($hostname)) {
    $ip = $hostname;
    $hostname = gethostbyaddr($hostname);
  }
  else {
    $ip = gethostbyname($hostname);
  }
  $result = db_query("SELECT nid FROM {node} WHERE title in ('%s', '%s') and type = :type", array(
    ':type' => 'server',
    '' => $ip,
    '' => $hostname,
  ))
    ->fetchField();
  if ($result) {
    return node_load($result);
  }
  return FALSE;
}

/**
 * Get the databases hosted on a server.
 *
 * @param int $db_server_id
 *   The node ID of the db server.
 */
function hosting_get_sites_on_db_server($db_server_id) {
  $return = array();
  $result = db_query("SELECT nid, db_name FROM {hosting_site} WHERE db_server = :id", array(
    ':id' => $db_server_id,
  ));
  while ($site = $result
    ->fetch()) {
    $return[$site->nid] = $site->db_name;
  }
  return $return;
}

/**
 * Implements hook_nodeapi_TYPE_OP().
 */
function hosting_db_server_nodeapi_server_presave(&$node) {
  if (empty($node->db_passwd) && !empty($node->nid)) {
    $old = node_load($node->nid);
    if (isset($old->db_passwd)) {
      $node->db_passwd = $old->db_passwd;
    }
  }
}

/**
 * Implements hook_views_api().
 */
function hosting_db_server_views_api() {
  return array(
    'api' => 2,
    'path' => drupal_get_path('module', 'hosting_db_server'),
  );
}