| 1 |
<?php
|
| 2 |
|
| 3 |
/**
|
| 4 |
* @file
|
| 5 |
*
|
| 6 |
* General purpose functions
|
| 7 |
*
|
| 8 |
* Those functions are destined to be used by the frontend Drupal
|
| 9 |
* module as well as the drush hooks of the frontend
|
| 10 |
*/
|
| 11 |
|
| 12 |
/**
|
| 13 |
* Check if a hostname provided is an ip address
|
| 14 |
*/
|
| 15 |
function _hosting_valid_ip($hostname) {
|
| 16 |
//TODO : provide IPv6 support
|
| 17 |
$parts = explode('.', $hostname);
|
| 18 |
if (sizeof($parts) != 4) {
|
| 19 |
return false;
|
| 20 |
}
|
| 21 |
foreach ($parts as $part) {
|
| 22 |
if (((int) $part < 0) || ((int) $part > 255)) {
|
| 23 |
return false;
|
| 24 |
}
|
| 25 |
}
|
| 26 |
return true;
|
| 27 |
}
|
| 28 |
|
| 29 |
/**
|
| 30 |
* Check if the FQDN provided is valid.
|
| 31 |
*/
|
| 32 |
function _hosting_valid_fqdn($fqdn) {
|
| 33 |
# regex is an implementation of RFC1035
|
| 34 |
return preg_match("/^([a-z0-9]([a-z0-9-]*[a-z0-9])?\.?)+$/i", $fqdn);
|
| 35 |
}
|
| 36 |
|
| 37 |
function hosting_format_interval($ts) {
|
| 38 |
if ($ts==mktime()) {
|
| 39 |
return t('Now');
|
| 40 |
}
|
| 41 |
if (!$ts) {
|
| 42 |
//Treats EPOCH as never
|
| 43 |
return t('Never');
|
| 44 |
}
|
| 45 |
|
| 46 |
return t("@interval ago", array("@interval" => format_interval(mktime() - $ts, 1)));
|
| 47 |
}
|
| 48 |
|
| 49 |
/**
|
| 50 |
* Make a path canonical
|
| 51 |
*
|
| 52 |
* This removes duplicate slashes, trailing slashes and /./ occurences. It does
|
| 53 |
* not (yet?) resolve .. instances.
|
| 54 |
*/
|
| 55 |
function hosting_path_normalize($path) {
|
| 56 |
return rtrim(preg_replace('/(\/\/*\.)?\/\/*/', '/', $path), '/');
|
| 57 |
}
|