hosting.ip.inc 2.95 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
<?php
// $Id$ 
/**
 * @file General IP address CRUD routines.
 */

/**
 * Get an array from the text ip address list.
 */
function _hosting_ip_list($addresses) {
  $return = array();
  $ip_list = is_array($addresses) ? $addresses : explode("\n", $addresses);
  
  foreach ($ip_list as $key => $ip) {
    $ip = trim($ip);
    if (!empty($ip)) {
      $return[$key] = $ip;
    }
  }

  return $return;
}

/**
 * Display the ip address on the node. IP utility function for hook_view.
 */
function hosting_ip_view(&$node) {
28
  $ip_list = isset($node->ip_addresses) ? _hosting_ip_list($node->ip_addresses) : array();
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49

  if (sizeof($ip_list)) {
    $node->content['info']['ip_addresses'] = array(
      '#type' => 'item',
      '#title' => t('IP addresses'),
      '#value' => implode('<br />', $ip_list),
    );  
  }
}

/**
 * IP Utility function for hook_update/hook_insert.
 */
function hosting_ip_save($node, $update = FALSE) {
  $ip_list = _hosting_ip_list($node->ip_addresses);

  if ($update) {
    hosting_ip_delete_revision($node);
  }

  foreach ($ip_list as $ip) {
50
    db_query("INSERT INTO {hosting_ip_addresses} (nid, ip_address) VALUES (%d, '%s')", $node->nid, $ip);
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
  }
}

/**
 * Validate that IP list is valid. IP utility function for hook_validate.
 */
function hosting_ip_validate($node) { 
  foreach (_hosting_ip_list($node->ip_addresses) as $ip) {
    if (!_hosting_valid_ip($ip)) {
      form_set_error('ip_addresses', t('Invalid IP address: %ip.', array(
        '%ip' => $ip )));
    }
  }
}


/**
 * IP Utility function for hook_load.
 */
function hosting_ip_load($node) {
  $ip_list = array();
72
  $result = db_query("SELECT ip_address FROM {hosting_ip_addresses} WHERE nid=%d", $node->nid);
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
  while ($obj = db_fetch_object($result)) {
    $ip_list[] = $obj->ip_address;
  }

  return $ip_list;
}

/**
 * IP Utility function for hook_delete.
 */
function hosting_ip_delete($node) {
  db_query("DELETE FROM {hosting_ip_addresses} WHERE nid=%d", $node->nid);
}

/**
 * IP Utility function for hook_delete_revision.
 */
function hosting_ip_delete_revision($node) {
91
  db_query("DELETE FROM {hosting_ip_addresses} WHERE nid=%d", $node->nid);
92 93 94 95 96
}

/**
 * Allocate an IP for a given site on a given server.
 */
97 98
function hosting_ip_allocate($cert, $site) {
  // make sure the IP is not allocated while we pick ours
99 100
  db_query("LOCK TABLES {hosting_ssl_cert_ips} WRITE, {hosting_ip_addresses} WRITE");
  $platform = node_load($site->platform);
101
  $server = node_load($platform->web_server);
102
  // guess the next available IP
103 104 105
  $ip = db_result(db_query("SELECT hosting_ip_addresses.id FROM {hosting_ip_addresses}
                             LEFT JOIN {hosting_ssl_cert_ips} ON hosting_ip_addresses.id = hosting_ssl_cert_ips.ip_address
                             WHERE hosting_ssl_cert_ips.ip_address IS NULL AND nid = %d LIMIT 1;", $server->nid));
106
  if ($ip) {
107
    db_query("INSERT INTO {hosting_ssl_cert_ips} (cid, ip_address) VALUES (%d, %d)", $cert->cid, $ip);
108
  }
109
  db_query("UNLOCK TABLES");
110 111
  return $ip;
}