You are here

hosting.inc in Hosting 6.2

Same filename and directory in other branches
  1. 7.4 hosting.inc
  2. 7.3 hosting.inc

General purpose Hosting module functions.

These can be used by both the frontend Hosting module and drush commands.

File

hosting.inc
View source
<?php

/**
 * @file
 *
 * General purpose Hosting module functions.
 *
 * These can be used by both the frontend Hosting module and drush commands.
 */

/**
 * Check if a hostname provided is an ip address.
 *
 * @param $hostname
 *   The hostname to check.
 * @return
 *   TRUE is the $hostname is a valid IP address, FALSE otherwise.
 */
function _hosting_valid_ip($hostname) {
  return is_string(inet_pton($hostname));
}

/**
 * Check if the FQDN provided is valid.
 *
 * @param $fqdn
 *   The Fully Qualified Domain Name (FQDN) to validate.
 * @return
 *   An integer greater than 0 if the $fqdn is valid, or 0 or FALSE if it not
 *   valid.
 */
function _hosting_valid_fqdn($fqdn) {

  // regex is an implementation of RFC1035, a little relaxed to allow
  // commonly registered hostnames (e.g. domaines starting with digits)
  return preg_match("/^([a-z0-9]([a-z0-9-]*[a-z0-9])?\\.?)+\$/i", $fqdn);
}

/**
 * Check if the FQDN provided is valid.
 *
 * This is a different function because wildcards are not part of the
 * RFC and may not be allowed everywhere. For example, the main site
 * title shouldn't have a wildcard entry.
 *
 * @param $fqdn
 *   The Fully Qualified Domain Name (FQDN) to validate.
 * @return
 *   An integer greater than 0 if the $fqdn is valid, or 0 or FALSE if it not
 *   valid.
 */
function _hosting_valid_fqdn_wildcard($fqdn) {

  // regex is an implementation of RFC1035, but allows "star" for wildcards
  return preg_match("/^(\\*\\.)?([a-z0-9]([a-z0-9-]*[a-z0-9])?\\.?)+\$/i", $fqdn) && !preg_match("/^(\\*\\.)[^.]*\$/", $fqdn);

  // don't allow wildcards on TLDs
}

/**
 * Check if the FQDN provided is valid for subdir alias.
 *
 * @param $fqdn
 *   The Fully Qualified Domain Name (FQDN) to validate.
 * @return
 *   An integer greater than 0 if the $fqdn is valid, or 0 or FALSE if it not
 *   valid.
 */
function _hosting_valid_fqdn_subdir($fqdn) {
  return preg_match("/^([a-z0-9]([a-z0-9-\\/]*[a-z0-9])?\\.?)+\$/i", $fqdn) && preg_match("/\\//", $fqdn) && !preg_match("/\\/\\//", $fqdn);
}

/**
 * Format a timestamp as a string in a friendly way.
 *
 * @param $ts
 *   The timestamp to format as a an interval.
 * @return
 *    Returns a string representing the given timestamp:
 *    - If the timestamp is the current time: 'Now'.
 *    - If the timestamp is 0 or FALSE: 'Never'
 *    - Otherwise formatted as 'X ago' where 'X' is for example 1 year or 1
 *      minute etc.
 *
 * @see format_interval()
 */
function hosting_format_interval($ts) {
  if ($ts == time()) {
    return t('Now');
  }
  if (!$ts) {

    //Treats the UNIX EPOCH as never.
    return t('Never');
  }
  return t("@interval ago", array(
    "@interval" => format_interval(time() - $ts, 1),
  ));
}

/**
 * Make a path canonical.
 *
 * This removes duplicate slashes, trailing slashes and /./ occurences. It does
 * not (yet?) resolve .. instances.
 */
function hosting_path_normalize($path) {
  return rtrim(preg_replace('/(\\/\\/*\\.)?\\/\\/*/', '/', $path), '/');
}

/**
 * Helper function to get the node ID of the Aegir front-end site.
 */
function hosting_get_hostmaster_nid() {
  return db_result(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"'));
}

Functions

Namesort descending Description
hosting_format_interval Format a timestamp as a string in a friendly way.
hosting_get_hostmaster_nid Helper function to get the node ID of the Aegir front-end site.
hosting_path_normalize Make a path canonical.
_hosting_valid_fqdn Check if the FQDN provided is valid.
_hosting_valid_fqdn_subdir Check if the FQDN provided is valid for subdir alias.
_hosting_valid_fqdn_wildcard Check if the FQDN provided is valid.
_hosting_valid_ip Check if a hostname provided is an ip address.