-
Notifications
You must be signed in to change notification settings - Fork 2
/
hosting.inc
175 lines (164 loc) · 5.05 KB
/
hosting.inc
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?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 string $hostname
* The hostname to check.
*
* @return bool
* 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 string $fqdn
* The Fully Qualified Domain Name (FQDN) to validate.
*
* @return int
* 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. domains starting with digits)
// original: return preg_match("/^([a-z0-9]([a-z0-9-]*[a-z0-9])?\.?)+$/i", $fqdn);
//
// We don't allow IPs here.
//
if (function_exists('idna_convert_encode')) {
$fqdn = idna_convert_encode($fqdn);
}
if (preg_match("/\.(?:host8|boa|aegir|o8)\.(?:biz|io|cc)$/", $fqdn)) {
return (preg_match("/^[a-z0-9]+[a-z0-9-.]*[a-z0-9]+\.((?:m|v|o)[1-9]{1}|(?:a|b|m|v|o)[0-9]{1,12})([a-z0-9]*)?\./", $fqdn) &&
!preg_match("/[-.]{2,}/", $fqdn));
}
else {
if (!preg_match("/^([a-z0-9]+\.)?xn--/", $fqdn) && preg_match("/[-]{2,}/", $fqdn)) {
return FALSE;
}
else {
return (preg_match("/^([a-z0-9]([a-z0-9-]*[a-z0-9])?\.?)+[a-z0-9]+$/", $fqdn) &&
preg_match("/[a-z]+/", $fqdn) &&
!preg_match("/[.]{2,}/", $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 string $fqdn
* The Fully Qualified Domain Name (FQDN) to validate.
*
* @return bool
* TRUE if the $fqdn is valid, 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
//
// We don't allow IPs here.
//
if (function_exists('idna_convert_encode')) {
$fqdn = idna_convert_encode($fqdn);
}
if (preg_match("/\.(?:host8|boa|aegir|o8)\.(?:biz|io|cc)$/", $fqdn)) {
return (preg_match("/^[a-z0-9]+[a-z0-9-.]*[a-z0-9]+\.((?:m|v|o)[1-9]{1}|(?:a|b|m|v|o)[0-9]{1,12})([a-z0-9]*)?\./", $fqdn) &&
!preg_match("/[-.]{2,}/", $fqdn));
}
else {
if (!preg_match("/^([a-z0-9]+\.)?xn--/", $fqdn) && preg_match("/[-]{2,}/", $fqdn)) {
return FALSE;
}
else {
return (preg_match("/^(\*\.)?([a-z0-9]([a-z0-9-]*[a-z0-9])?\.?)+[a-z0-9]+$/", $fqdn) &&
preg_match("/[a-z]+/", $fqdn) &&
!preg_match("/[.]{2,}/", $fqdn) &&
!preg_match("/^(\*\.)[a-z]+$/", $fqdn) &&
!preg_match("/^(\*\.)[^.]*$/", $fqdn) &&
!preg_match("/^(\*\.)[a-z]{3}\.[a-z]{2}$/", $fqdn));
}
}
}
/**
* Check if the FQDN or IP provided is valid.
*/
function _hosting_valid_fqdn_ip($fqdn) {
# regex is an implementation of RFC1035
#
# We allow IPs here.
#
return (preg_match("/^([a-z0-9]([a-z0-9-]*[a-z0-9])?\.?)+[a-z0-9]+$/", $fqdn) &&
!preg_match("/[.]{2,}/", $fqdn));
}
/**
* Format a timestamp as a string in a friendly way.
*
* @param int $ts
* The timestamp to format as a an interval.
*
* @return string
* 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 == REQUEST_TIME) {
return t('Now');
}
if ($ts <= 1) {
// Treats the UNIX EPOCH as never.
return t('Never');
}
return t("@interval ago", array("@interval" => format_interval(REQUEST_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), '/');
}
/**
* Shim to preserve API stability. Deprecated. Use hosting_get_hostmaster_site_nid().
*
* @deprecated
*/
function hosting_get_hostmaster_nid() {
return hosting_get_hostmaster_site_nid();
}
/**
* Helper function to get the node ID of the Aegir front-end site.
*/
function hosting_get_hostmaster_site_nid() {
return variable_get('aegir_hostmaster_site_nid', NULL);
}
/**
* Helper function to get the node ID of the Aegir platform.
*/
function hosting_get_hostmaster_platform_nid() {
$hostmaster_site = node_load(hosting_get_hostmaster_site_nid());
if (!is_null($hostmaster_site) && isset($hostmaster_site->platform)) {
return $hostmaster_site->platform;
}
}