commit | author | age
|
532ae5
|
1 |
<?php |
L |
2 |
|
|
3 |
/* |
|
4 |
Copyright (c) 2010, Till Brehm, projektfarm Gmbh |
|
5 |
All rights reserved. |
|
6 |
|
|
7 |
Redistribution and use in source and binary forms, with or without modification, |
|
8 |
are permitted provided that the following conditions are met: |
|
9 |
|
|
10 |
* Redistributions of source code must retain the above copyright notice, |
|
11 |
this list of conditions and the following disclaimer. |
|
12 |
* Redistributions in binary form must reproduce the above copyright notice, |
|
13 |
this list of conditions and the following disclaimer in the documentation |
|
14 |
and/or other materials provided with the distribution. |
|
15 |
* Neither the name of ISPConfig nor the names of its contributors |
|
16 |
may be used to endorse or promote products derived from this software without |
|
17 |
specific prior written permission. |
|
18 |
|
|
19 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
|
20 |
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
|
21 |
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
|
22 |
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
|
23 |
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
|
24 |
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
25 |
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
|
26 |
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
|
27 |
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
|
28 |
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
29 |
*/ |
|
30 |
|
|
31 |
//* The purpose of this library is to provide some general functions. |
|
32 |
//* This class is loaded automatically by the ispconfig framework. |
|
33 |
|
|
34 |
class functions { |
8c1761
|
35 |
var $idn_converter = null; |
b1a6a5
|
36 |
var $idn_converter_name = ''; |
532ae5
|
37 |
|
120cce
|
38 |
public function mail($to, $subject, $text, $from, $filepath = '', $filetype = 'application/pdf', $filename = '', $cc = '', $bcc = '', $from_name = '') { |
b1a6a5
|
39 |
global $app, $conf; |
MC |
40 |
|
532ae5
|
41 |
if($conf['demo_mode'] == true) $app->error("Mail sending disabled in demo mode."); |
b1a6a5
|
42 |
|
MC |
43 |
$app->uses('getconf,ispcmail'); |
a59498
|
44 |
$mail_config = $app->getconf->get_global_config('mail'); |
M |
45 |
if($mail_config['smtp_enabled'] == 'y') { |
|
46 |
$mail_config['use_smtp'] = true; |
|
47 |
$app->ispcmail->setOptions($mail_config); |
|
48 |
} |
120cce
|
49 |
$app->ispcmail->setSender($from, $from_name); |
a59498
|
50 |
$app->ispcmail->setSubject($subject); |
M |
51 |
$app->ispcmail->setMailText($text); |
b1a6a5
|
52 |
|
a59498
|
53 |
if($filepath != '') { |
M |
54 |
if(!file_exists($filepath)) $app->error("Mail attachement does not exist ".$filepath); |
|
55 |
$app->ispcmail->readAttachFile($filepath); |
|
56 |
} |
b1a6a5
|
57 |
|
a59498
|
58 |
if($cc != '') $app->ispcmail->setHeader('Cc', $cc); |
M |
59 |
if($bcc != '') $app->ispcmail->setHeader('Bcc', $bcc); |
b1a6a5
|
60 |
|
a59498
|
61 |
$app->ispcmail->send($to); |
M |
62 |
$app->ispcmail->finish(); |
b1a6a5
|
63 |
|
532ae5
|
64 |
return true; |
L |
65 |
} |
b1a6a5
|
66 |
|
MC |
67 |
public function array_merge($array1, $array2) { |
532ae5
|
68 |
$out = $array1; |
L |
69 |
foreach($array2 as $key => $val) { |
|
70 |
$out[$key] = $val; |
|
71 |
} |
|
72 |
return $out; |
|
73 |
} |
b1a6a5
|
74 |
|
36d487
|
75 |
public function currency_format($number, $view = '') { |
532ae5
|
76 |
global $app; |
36d487
|
77 |
if($view != '') $number_format_decimals = (int)$app->lng('number_format_decimals_'.$view); |
b1a6a5
|
78 |
if(!$number_format_decimals) $number_format_decimals = (int)$app->lng('number_format_decimals'); |
MC |
79 |
|
532ae5
|
80 |
$number_format_dec_point = $app->lng('number_format_dec_point'); |
L |
81 |
$number_format_thousands_sep = $app->lng('number_format_thousands_sep'); |
|
82 |
if($number_format_thousands_sep == 'number_format_thousands_sep') $number_format_thousands_sep = ''; |
|
83 |
return number_format((double)$number, $number_format_decimals, $number_format_dec_point, $number_format_thousands_sep); |
|
84 |
} |
b1a6a5
|
85 |
|
2d2fd1
|
86 |
//* convert currency formatted number back to floating number |
FT |
87 |
public function currency_unformat($number) { |
|
88 |
global $app; |
b1a6a5
|
89 |
|
2d2fd1
|
90 |
$number_format_dec_point = $app->lng('number_format_dec_point'); |
FT |
91 |
$number_format_thousands_sep = $app->lng('number_format_thousands_sep'); |
|
92 |
if($number_format_thousands_sep == 'number_format_thousands_sep') $number_format_thousands_sep = ''; |
b1a6a5
|
93 |
|
2d2fd1
|
94 |
if($number_format_thousands_sep != '') $number = str_replace($number_format_thousands_sep, '', $number); |
FT |
95 |
if($number_format_dec_point != '.' && $number_format_dec_point != '') $number = str_replace($number_format_dec_point, '.', $number); |
b1a6a5
|
96 |
|
2d2fd1
|
97 |
return (double)$number; |
FT |
98 |
} |
b1a6a5
|
99 |
|
532ae5
|
100 |
public function get_ispconfig_url() { |
615a0a
|
101 |
global $app; |
b1a6a5
|
102 |
|
MC |
103 |
$url = (stristr($_SERVER['SERVER_PROTOCOL'], 'HTTPS') || stristr($_SERVER['HTTPS'], 'on'))?'https':'http'; |
615a0a
|
104 |
if($_SERVER['SERVER_NAME'] != '_') { |
T |
105 |
$url .= '://'.$_SERVER['SERVER_NAME']; |
|
106 |
if($_SERVER['SERVER_PORT'] != 80 && $_SERVER['SERVER_PORT'] != 443) { |
|
107 |
$url .= ':'.$_SERVER['SERVER_PORT']; |
|
108 |
} |
|
109 |
} else { |
|
110 |
$app->uses("getconf"); |
b1a6a5
|
111 |
$server_config = $app->getconf->get_server_config(1, 'server'); |
615a0a
|
112 |
$url .= '://'.$server_config['hostname']; |
T |
113 |
if($_SERVER['SERVER_PORT'] != 80 && $_SERVER['SERVER_PORT'] != 443) { |
|
114 |
$url .= ':'.$_SERVER['SERVER_PORT']; |
|
115 |
} |
532ae5
|
116 |
} |
L |
117 |
return $url; |
|
118 |
} |
b1a6a5
|
119 |
|
MC |
120 |
public function json_encode($data) { |
4c28d9
|
121 |
if(!function_exists('json_encode')){ |
221340
|
122 |
if(is_array($data) || is_object($data)){ |
b1a6a5
|
123 |
$islist = is_array($data) && (empty($data) || array_keys($data) === range(0, count($data)-1)); |
221340
|
124 |
|
F |
125 |
if($islist){ |
|
126 |
$json = '[' . implode(',', array_map(array($this, "json_encode"), $data) ) . ']'; |
|
127 |
} else { |
b1a6a5
|
128 |
$items = array(); |
221340
|
129 |
foreach( $data as $key => $value ) { |
F |
130 |
$items[] = $this->json_encode("$key") . ':' . $this->json_encode($value); |
4c28d9
|
131 |
} |
221340
|
132 |
$json = '{' . implode(',', $items) . '}'; |
F |
133 |
} |
|
134 |
} elseif(is_string($data)){ |
b1a6a5
|
135 |
// Escape non-printable or Non-ASCII characters. |
MC |
136 |
// I also put the \\ character first, as suggested in comments on the 'addclashes' page. |
221340
|
137 |
$string = '"'.addcslashes($data, "\\\"\n\r\t/".chr(8).chr(12)).'"'; |
F |
138 |
$json = ''; |
|
139 |
$len = strlen($string); |
b1a6a5
|
140 |
// Convert UTF-8 to Hexadecimal Codepoints. |
221340
|
141 |
for($i = 0; $i < $len; $i++){ |
F |
142 |
$char = $string[$i]; |
|
143 |
$c1 = ord($char); |
|
144 |
|
b1a6a5
|
145 |
// Single byte; |
221340
|
146 |
if($c1 <128){ |
F |
147 |
$json .= ($c1 > 31) ? $char : sprintf("\\u%04x", $c1); |
|
148 |
continue; |
4c28d9
|
149 |
} |
221340
|
150 |
|
b1a6a5
|
151 |
// Double byte |
221340
|
152 |
$c2 = ord($string[++$i]); |
F |
153 |
if(($c1 & 32) === 0){ |
|
154 |
$json .= sprintf("\\u%04x", ($c1 - 192) * 64 + $c2 - 128); |
|
155 |
continue; |
|
156 |
} |
|
157 |
|
b1a6a5
|
158 |
// Triple |
221340
|
159 |
$c3 = ord($string[++$i]); |
F |
160 |
if(($c1 & 16) === 0){ |
|
161 |
$json .= sprintf("\\u%04x", (($c1 - 224) <<12) + (($c2 - 128) << 6) + ($c3 - 128)); |
|
162 |
continue; |
|
163 |
} |
|
164 |
|
b1a6a5
|
165 |
// Quadruple |
221340
|
166 |
$c4 = ord($string[++$i]); |
F |
167 |
if(($c1 & 8) === 0){ |
|
168 |
$u = (($c1 & 15) << 2) + (($c2>>4) & 3) - 1; |
|
169 |
|
|
170 |
$w1 = (54<<10) + ($u<<6) + (($c2 & 15) << 2) + (($c3>>4) & 3); |
|
171 |
$w2 = (55<<10) + (($c3 & 15)<<6) + ($c4-128); |
|
172 |
$json .= sprintf("\\u%04x\\u%04x", $w1, $w2); |
|
173 |
} |
|
174 |
} |
|
175 |
} else { |
b1a6a5
|
176 |
// int, floats, bools, null |
221340
|
177 |
$json = strtolower(var_export($data, true)); |
4c28d9
|
178 |
} |
221340
|
179 |
return $json; |
4c28d9
|
180 |
} else { |
F |
181 |
return json_encode($data); |
|
182 |
} |
b1a6a5
|
183 |
} |
MC |
184 |
|
5672cd
|
185 |
public function suggest_ips($type = 'IPv4'){ |
F |
186 |
global $app; |
b1a6a5
|
187 |
|
5672cd
|
188 |
if($type == 'IPv4'){ |
F |
189 |
$regex = "/^[0-9]{1,3}(\.)[0-9]{1,3}(\.)[0-9]{1,3}(\.)[0-9]{1,3}$/"; |
|
190 |
} else { |
|
191 |
// IPv6 |
|
192 |
$regex = "/^(\:\:([a-f0-9]{1,4}\:){0,6}?[a-f0-9]{0,4}|[a-f0-9]{1,4}(\:[a-f0-9]{1,4}){0,6}?\:\:|[a-f0-9]{1,4}(\:[a-f0-9]{1,4}){1,6}?\:\:([a-f0-9]{1,4}\:){1,6}?[a-f0-9]{1,4})(\/\d{1,3})?$/i"; |
|
193 |
} |
b1a6a5
|
194 |
|
391e05
|
195 |
$server_by_id = array(); |
FT |
196 |
$server_by_ip = array(); |
|
197 |
$servers = $app->db->queryAllRecords("SELECT * FROM server"); |
|
198 |
if(is_array($servers) && !empty($servers)){ |
|
199 |
foreach($servers as $server){ |
|
200 |
$server_by_id[$server['server_id']] = $server['server_name']; |
|
201 |
} |
|
202 |
} |
b1a6a5
|
203 |
|
5672cd
|
204 |
$ips = array(); |
391e05
|
205 |
$results = $app->db->queryAllRecords("SELECT ip_address AS ip, server_id FROM server_ip WHERE ip_type = '".$type."'"); |
5672cd
|
206 |
if(!empty($results) && is_array($results)){ |
F |
207 |
foreach($results as $result){ |
391e05
|
208 |
if(preg_match($regex, $result['ip'])){ |
FT |
209 |
$ips[] = $result['ip']; |
|
210 |
$server_by_ip[$result['ip']] = $server_by_id[$result['server_id']]; |
|
211 |
} |
5672cd
|
212 |
} |
F |
213 |
} |
|
214 |
$results = $app->db->queryAllRecords("SELECT ip_address AS ip FROM openvz_ip"); |
|
215 |
if(!empty($results) && is_array($results)){ |
|
216 |
foreach($results as $result){ |
|
217 |
if(preg_match($regex, $result['ip'])) $ips[] = $result['ip']; |
|
218 |
} |
|
219 |
} |
|
220 |
$results = $app->db->queryAllRecords("SELECT data AS ip FROM dns_rr WHERE type = 'A' OR type = 'AAAA'"); |
|
221 |
if(!empty($results) && is_array($results)){ |
|
222 |
foreach($results as $result){ |
|
223 |
if(preg_match($regex, $result['ip'])) $ips[] = $result['ip']; |
|
224 |
} |
|
225 |
} |
|
226 |
$results = $app->db->queryAllRecords("SELECT ns AS ip FROM dns_slave"); |
|
227 |
if(!empty($results) && is_array($results)){ |
|
228 |
foreach($results as $result){ |
|
229 |
if(preg_match($regex, $result['ip'])) $ips[] = $result['ip']; |
|
230 |
} |
|
231 |
} |
374e5a
|
232 |
|
TB |
233 |
/* |
5672cd
|
234 |
$results = $app->db->queryAllRecords("SELECT xfer FROM dns_slave WHERE xfer != ''"); |
F |
235 |
if(!empty($results) && is_array($results)){ |
|
236 |
foreach($results as $result){ |
|
237 |
$tmp_ips = explode(',', $result['xfer']); |
|
238 |
foreach($tmp_ips as $tmp_ip){ |
|
239 |
$tmp_ip = trim($tmp_ip); |
|
240 |
if(preg_match($regex, $tmp_ip)) $ips[] = $tmp_ip; |
|
241 |
} |
|
242 |
} |
|
243 |
} |
|
244 |
$results = $app->db->queryAllRecords("SELECT xfer FROM dns_soa WHERE xfer != ''"); |
|
245 |
if(!empty($results) && is_array($results)){ |
|
246 |
foreach($results as $result){ |
|
247 |
$tmp_ips = explode(',', $result['xfer']); |
|
248 |
foreach($tmp_ips as $tmp_ip){ |
|
249 |
$tmp_ip = trim($tmp_ip); |
|
250 |
if(preg_match($regex, $tmp_ip)) $ips[] = $tmp_ip; |
|
251 |
} |
|
252 |
} |
|
253 |
} |
|
254 |
$results = $app->db->queryAllRecords("SELECT also_notify FROM dns_soa WHERE also_notify != ''"); |
|
255 |
if(!empty($results) && is_array($results)){ |
|
256 |
foreach($results as $result){ |
|
257 |
$tmp_ips = explode(',', $result['also_notify']); |
|
258 |
foreach($tmp_ips as $tmp_ip){ |
|
259 |
$tmp_ip = trim($tmp_ip); |
|
260 |
if(preg_match($regex, $tmp_ip)) $ips[] = $tmp_ip; |
|
261 |
} |
|
262 |
} |
|
263 |
} |
374e5a
|
264 |
*/ |
TB |
265 |
|
5672cd
|
266 |
$results = $app->db->queryAllRecords("SELECT remote_ips FROM web_database WHERE remote_ips != ''"); |
F |
267 |
if(!empty($results) && is_array($results)){ |
|
268 |
foreach($results as $result){ |
|
269 |
$tmp_ips = explode(',', $result['remote_ips']); |
|
270 |
foreach($tmp_ips as $tmp_ip){ |
|
271 |
$tmp_ip = trim($tmp_ip); |
|
272 |
if(preg_match($regex, $tmp_ip)) $ips[] = $tmp_ip; |
|
273 |
} |
|
274 |
} |
|
275 |
} |
|
276 |
$ips = array_unique($ips); |
|
277 |
sort($ips, SORT_NUMERIC); |
|
278 |
|
|
279 |
$result_array = array('cheader' => array(), 'cdata' => array()); |
b1a6a5
|
280 |
|
5672cd
|
281 |
if(!empty($ips)){ |
F |
282 |
$result_array['cheader'] = array('title' => 'IPs', |
b1a6a5
|
283 |
'total' => count($ips), |
MC |
284 |
'limit' => count($ips) |
|
285 |
); |
|
286 |
|
5672cd
|
287 |
foreach($ips as $ip){ |
b1a6a5
|
288 |
$result_array['cdata'][] = array( 'title' => $ip, |
MC |
289 |
'description' => $type.($server_by_ip[$ip] != ''? ' > '.$server_by_ip[$ip] : ''), |
|
290 |
'onclick' => '', |
|
291 |
'fill_text' => $ip |
|
292 |
); |
5672cd
|
293 |
} |
F |
294 |
} |
b1a6a5
|
295 |
|
5672cd
|
296 |
return $result_array; |
F |
297 |
} |
4c28d9
|
298 |
|
b1a6a5
|
299 |
public function intval($string, $force_numeric = false) { |
MC |
300 |
if(intval($string) == 2147483647 || ($string > 0 && intval($string) < 0)) { |
|
301 |
if($force_numeric == true) return floatval($string); |
|
302 |
elseif(preg_match('/^([-]?)[0]*([1-9][0-9]*)([^0-9].*)*$/', $string, $match)) return $match[1].$match[2]; |
|
303 |
else return 0; |
|
304 |
} else { |
|
305 |
return intval($string); |
|
306 |
} |
|
307 |
} |
71accc
|
308 |
|
b1a6a5
|
309 |
/** |
MC |
310 |
* Function to change bytes to kB, MB, GB or TB |
|
311 |
* @param int $size - size in bytes |
|
312 |
* @param int precicion - after-comma-numbers (default: 2) |
|
313 |
* @return string - formated bytes |
|
314 |
*/ |
|
315 |
public function formatBytes($size, $precision = 2) { |
|
316 |
$base=log($size)/log(1024); |
5561eb
|
317 |
$suffixes=array('', ' kB', ' MB', ' GB', ' TB'); |
b1a6a5
|
318 |
return round(pow(1024, $base-floor($base)), $precision).$suffixes[floor($base)]; |
MC |
319 |
} |
|
320 |
|
|
321 |
/** IDN converter wrapper. |
|
322 |
* all converter classes should be placed in ISPC_CLASS_PATH.'/idn/' |
|
323 |
*/ |
|
324 |
private function _idn_encode_decode($domain, $encode = true) { |
|
325 |
if($domain == '') return ''; |
|
326 |
if(preg_match('/^[0-9\.]+$/', $domain)) return $domain; // may be an ip address - anyway does not need to bee encoded |
|
327 |
|
|
328 |
// get domain and user part if it is an email |
|
329 |
$user_part = false; |
|
330 |
if(strpos($domain, '@') !== false) { |
|
331 |
$user_part = substr($domain, 0, strrpos($domain, '@')); |
|
332 |
$domain = substr($domain, strrpos($domain, '@') + 1); |
|
333 |
} |
|
334 |
|
|
335 |
if($encode == true) { |
|
336 |
if(function_exists('idn_to_ascii')) { |
|
337 |
$domain = idn_to_ascii($domain); |
|
338 |
} elseif(file_exists(ISPC_CLASS_PATH.'/idn/idna_convert.class.php')) { |
|
339 |
/* use idna class: |
d6363b
|
340 |
* @author Matthias Sommerfeld <mso@phlylabs.de> |
M |
341 |
* @copyright 2004-2011 phlyLabs Berlin, http://phlylabs.de |
|
342 |
* @version 0.8.0 2011-03-11 |
|
343 |
*/ |
b1a6a5
|
344 |
|
MC |
345 |
if(!is_object($this->idn_converter) || $this->idn_converter_name != 'idna_convert.class') { |
|
346 |
include_once ISPC_CLASS_PATH.'/idn/idna_convert.class.php'; |
|
347 |
$this->idn_converter = new idna_convert(array('idn_version' => 2008)); |
|
348 |
$this->idn_converter_name = 'idna_convert.class'; |
|
349 |
} |
|
350 |
$domain = $this->idn_converter->encode($domain); |
|
351 |
} |
|
352 |
} else { |
|
353 |
if(function_exists('idn_to_utf8')) { |
|
354 |
$domain = idn_to_utf8($domain); |
|
355 |
} elseif(file_exists(ISPC_CLASS_PATH.'/idn/idna_convert.class.php')) { |
|
356 |
/* use idna class: |
d6363b
|
357 |
* @author Matthias Sommerfeld <mso@phlylabs.de> |
M |
358 |
* @copyright 2004-2011 phlyLabs Berlin, http://phlylabs.de |
|
359 |
* @version 0.8.0 2011-03-11 |
|
360 |
*/ |
b1a6a5
|
361 |
|
MC |
362 |
if(!is_object($this->idn_converter) || $this->idn_converter_name != 'idna_convert.class') { |
|
363 |
include_once ISPC_CLASS_PATH.'/idn/idna_convert.class.php'; |
|
364 |
$this->idn_converter = new idna_convert(array('idn_version' => 2008)); |
|
365 |
$this->idn_converter_name = 'idna_convert.class'; |
|
366 |
} |
|
367 |
$domain = $this->idn_converter->decode($domain); |
|
368 |
} |
|
369 |
} |
|
370 |
|
|
371 |
if($user_part !== false) return $user_part . '@' . $domain; |
|
372 |
else return $domain; |
|
373 |
} |
|
374 |
|
|
375 |
public function idn_encode($domain) { |
|
376 |
$domains = explode("\n", $domain); |
|
377 |
for($d = 0; $d < count($domains); $d++) { |
|
378 |
$domains[$d] = $this->_idn_encode_decode($domains[$d], true); |
|
379 |
} |
|
380 |
return implode("\n", $domains); |
|
381 |
} |
|
382 |
|
|
383 |
public function idn_decode($domain) { |
|
384 |
$domains = explode("\n", $domain); |
|
385 |
for($d = 0; $d < count($domains); $d++) { |
|
386 |
$domains[$d] = $this->_idn_encode_decode($domains[$d], false); |
|
387 |
} |
|
388 |
return implode("\n", $domains); |
|
389 |
} |
|
390 |
|
532ae5
|
391 |
} |
L |
392 |
|
71accc
|
393 |
?> |