Marius Burkard
2016-04-20 4569cae57f127afd093794310ccd290d2d9fdf36
commit | author | age
5de2af 1 <?php
M 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 {
35     var $idn_converter = null;
b1a6a5 36     var $idn_converter_name = '';
5de2af 37
M 38     public function mail($to, $subject, $text, $from, $filepath = '', $filetype = 'application/pdf', $filename = '', $cc = '', $bcc = '', $from_name = '') {
b1a6a5 39         global $app, $conf;
MC 40
5de2af 41         if($conf['demo_mode'] == true) $app->error("Mail sending disabled in demo mode.");
b1a6a5 42
MC 43         $app->uses('getconf,ispcmail');
5de2af 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         }
49         $app->ispcmail->setSender($from, $from_name);
50         $app->ispcmail->setSubject($subject);
51         $app->ispcmail->setMailText($text);
b1a6a5 52
5de2af 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
5de2af 58         if($cc != '') $app->ispcmail->setHeader('Cc', $cc);
M 59         if($bcc != '') $app->ispcmail->setHeader('Bcc', $bcc);
b1a6a5 60
5de2af 61         $app->ispcmail->send($to);
M 62         $app->ispcmail->finish();
b1a6a5 63
5de2af 64         /* left in here just for the case...
M 65         if($filepath != '') {
66             if(!file_exists($filepath)) $app->error("Mail attachement does not exist ".$filepath);
b1a6a5 67
5de2af 68             $content = file_get_contents($filepath);
M 69             $content = chunk_split(base64_encode($content));
70             $uid = strtoupper(md5(uniqid(time())));
71             $subject      = "=?utf-8?B?".base64_encode($subject)."?=";
b1a6a5 72
5de2af 73             if($filename == '') {
M 74                 $path_parts = pathinfo($filepath);
75                 $filename = $path_parts["basename"];
76                 unset($path_parts);
77             }
78
79             $header = "Return-Path: $from\nFrom: $from\nReply-To: $from\n";
80             if($cc != '') $header .= "Cc: $cc\n";
81             if($bcc != '') $header .= "Bcc: $bcc\n";
82             $header .= "MIME-Version: 1.0\n";
83             $header .= "Content-Type: multipart/mixed; boundary=$uid\n";
84
85             $header .= "--$uid\n";
86             $header .= "Content-Type: text/plain;\n\tcharset=\"UTF-8\"\n";
87             $header .= "Content-Transfer-Encoding: 8bit\n\n";
88             $header .= "$text\n";
89
90             $header .= "--$uid\n";
91             $header .= "Content-Type: $filetype; name=\"$filename\"\n";
92
93             $header .= "Content-Transfer-Encoding: base64\n";
94             $header .= "Content-Disposition: attachment; filename=\"$filename\"\n\n";
95             $header .= "$content\n";
96
97             $header .= "--$uid--";
98
99             mail($to, $subject, "", $header);
100         } else {
101             $header = "From: $from\nReply-To: $from\n";
102             if($cc != '') $header .= "Cc: $cc\n";
103             if($bcc != '') $header .= "Bcc: $bcc\n";
104             $header .= "Content-Type: text/plain;\n\tcharset=\"UTF-8\"\n";
105             $header .= "Content-Transfer-Encoding: 8bit\n\n";
106             $subject      = "=?utf-8?B?".base64_encode($subject)."?=";
107             mail($to, $subject, $text, $header);
108         }
109         */
110         return true;
111     }
b1a6a5 112
MC 113     public function array_merge($array1, $array2) {
5de2af 114         $out = $array1;
M 115         foreach($array2 as $key => $val) {
116             $out[$key] = $val;
117         }
118         return $out;
119     }
b1a6a5 120
5de2af 121     public function currency_format($number, $view = '') {
M 122         global $app;
123         if($view != '') $number_format_decimals = (int)$app->lng('number_format_decimals_'.$view);
b1a6a5 124         if(!$number_format_decimals) $number_format_decimals = (int)$app->lng('number_format_decimals');
MC 125
5de2af 126         $number_format_dec_point = $app->lng('number_format_dec_point');
M 127         $number_format_thousands_sep = $app->lng('number_format_thousands_sep');
128         if($number_format_thousands_sep == 'number_format_thousands_sep') $number_format_thousands_sep = '';
129         return number_format((double)$number, $number_format_decimals, $number_format_dec_point, $number_format_thousands_sep);
130     }
b1a6a5 131
5561eb 132     /**
FS 133     * Function to change bytes to kB, MB, GB or TB
134     * @param int $size - size in bytes
135     * @param int precicion - after-comma-numbers (default: 2)
136     * @return string - formated bytes
137     */
138     public function formatBytes($size, $precision = 2) {
139         $base=log($size)/log(1024);
140         $suffixes=array('', ' kB', ' MB', ' GB', ' TB');
141         return round(pow(1024, $base-floor($base)), $precision).$suffixes[floor($base)];
142     }
143
5de2af 144     public function get_ispconfig_url() {
M 145         global $app;
b1a6a5 146
MC 147         $url = (stristr($_SERVER['SERVER_PROTOCOL'], 'HTTPS') || stristr($_SERVER['HTTPS'], 'on'))?'https':'http';
5de2af 148         if($_SERVER['SERVER_NAME'] != '_') {
M 149             $url .= '://'.$_SERVER['SERVER_NAME'];
150             if($_SERVER['SERVER_PORT'] != 80 && $_SERVER['SERVER_PORT'] != 443) {
151                 $url .= ':'.$_SERVER['SERVER_PORT'];
152             }
153         } else {
154             $app->uses("getconf");
b1a6a5 155             $server_config = $app->getconf->get_server_config(1, 'server');
5de2af 156             $url .= '://'.$server_config['hostname'];
M 157             if($_SERVER['SERVER_PORT'] != 80 && $_SERVER['SERVER_PORT'] != 443) {
158                 $url .= ':'.$_SERVER['SERVER_PORT'];
159             }
160         }
161         return $url;
162     }
b1a6a5 163
MC 164     public function json_encode($data) {
5de2af 165         if(!function_exists('json_encode')){
M 166             if(is_array($data) || is_object($data)){
b1a6a5 167                 $islist = is_array($data) && (empty($data) || array_keys($data) === range(0, count($data)-1));
5de2af 168
M 169                 if($islist){
170                     $json = '[' . implode(',', array_map(array($this, "json_encode"), $data) ) . ']';
171                 } else {
b1a6a5 172                     $items = array();
5de2af 173                     foreach( $data as $key => $value ) {
M 174                         $items[] = $this->json_encode("$key") . ':' . $this->json_encode($value);
175                     }
176                     $json = '{' . implode(',', $items) . '}';
177                 }
178             } elseif(is_string($data)){
b1a6a5 179                 // Escape non-printable or Non-ASCII characters.
MC 180                 // I also put the \\ character first, as suggested in comments on the 'addclashes' page.
5de2af 181                 $string = '"'.addcslashes($data, "\\\"\n\r\t/".chr(8).chr(12)).'"';
M 182                 $json = '';
183                 $len = strlen($string);
b1a6a5 184                 // Convert UTF-8 to Hexadecimal Codepoints.
5de2af 185                 for($i = 0; $i < $len; $i++){
M 186                     $char = $string[$i];
187                     $c1 = ord($char);
188
b1a6a5 189                     // Single byte;
5de2af 190                     if($c1 <128){
M 191                         $json .= ($c1 > 31) ? $char : sprintf("\\u%04x", $c1);
192                         continue;
193                     }
194
b1a6a5 195                     // Double byte
5de2af 196                     $c2 = ord($string[++$i]);
M 197                     if(($c1 & 32) === 0){
198                         $json .= sprintf("\\u%04x", ($c1 - 192) * 64 + $c2 - 128);
199                         continue;
200                     }
201
b1a6a5 202                     // Triple
5de2af 203                     $c3 = ord($string[++$i]);
M 204                     if(($c1 & 16) === 0){
205                         $json .= sprintf("\\u%04x", (($c1 - 224) <<12) + (($c2 - 128) << 6) + ($c3 - 128));
206                         continue;
207                     }
208
b1a6a5 209                     // Quadruple
5de2af 210                     $c4 = ord($string[++$i]);
M 211                     if(($c1 & 8) === 0){
212                         $u = (($c1 & 15) << 2) + (($c2>>4) & 3) - 1;
213
214                         $w1 = (54<<10) + ($u<<6) + (($c2 & 15) << 2) + (($c3>>4) & 3);
215                         $w2 = (55<<10) + (($c3 & 15)<<6) + ($c4-128);
216                         $json .= sprintf("\\u%04x\\u%04x", $w1, $w2);
217                     }
218                 }
219             } else {
b1a6a5 220                 // int, floats, bools, null
5de2af 221                 $json = strtolower(var_export($data, true));
M 222             }
223             return $json;
224         } else {
225             return json_encode($data);
226         }
b1a6a5 227     }
MC 228
5de2af 229     public function suggest_ips($type = 'IPv4'){
M 230         global $app;
b1a6a5 231
5de2af 232         if($type == 'IPv4'){
2df8c0 233 //            $regex = "/^[0-9]{1,3}(\.)[0-9]{1,3}(\.)[0-9]{1,3}(\.)[0-9]{1,3}$/";
FS 234             $regex = "/^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/";
5de2af 235         } else {
M 236             // IPv6
237             $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";
238         }
b1a6a5 239
5de2af 240         $ips = array();
cc7a82 241         $results = $app->db->queryAllRecords("SELECT ip_address AS ip FROM server_ip WHERE ip_type = ?", $type);
5de2af 242         if(!empty($results) && is_array($results)){
M 243             foreach($results as $result){
244                 if(preg_match($regex, $result['ip'])) $ips[] = $result['ip'];
245             }
246         }
247         $results = $app->db->queryAllRecords("SELECT ip_address AS ip FROM openvz_ip");
248         if(!empty($results) && is_array($results)){
249             foreach($results as $result){
250                 if(preg_match($regex, $result['ip'])) $ips[] = $result['ip'];
251             }
252         }
253         $results = $app->db->queryAllRecords("SELECT data AS ip FROM dns_rr WHERE type = 'A' OR type = 'AAAA'");
254         if(!empty($results) && is_array($results)){
255             foreach($results as $result){
256                 if(preg_match($regex, $result['ip'])) $ips[] = $result['ip'];
257             }
258         }
259         $results = $app->db->queryAllRecords("SELECT ns AS ip FROM dns_slave");
260         if(!empty($results) && is_array($results)){
261             foreach($results as $result){
262                 if(preg_match($regex, $result['ip'])) $ips[] = $result['ip'];
263             }
264         }
b1a6a5 265
5de2af 266         $results = $app->db->queryAllRecords("SELECT xfer FROM dns_slave WHERE xfer != ''");
M 267         if(!empty($results) && is_array($results)){
268             foreach($results as $result){
269                 $tmp_ips = explode(',', $result['xfer']);
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         $results = $app->db->queryAllRecords("SELECT xfer FROM dns_soa WHERE xfer != ''");
277         if(!empty($results) && is_array($results)){
278             foreach($results as $result){
279                 $tmp_ips = explode(',', $result['xfer']);
280                 foreach($tmp_ips as $tmp_ip){
281                     $tmp_ip = trim($tmp_ip);
282                     if(preg_match($regex, $tmp_ip)) $ips[] = $tmp_ip;
283                 }
284             }
285         }
286         $results = $app->db->queryAllRecords("SELECT also_notify FROM dns_soa WHERE also_notify != ''");
287         if(!empty($results) && is_array($results)){
288             foreach($results as $result){
289                 $tmp_ips = explode(',', $result['also_notify']);
290                 foreach($tmp_ips as $tmp_ip){
291                     $tmp_ip = trim($tmp_ip);
292                     if(preg_match($regex, $tmp_ip)) $ips[] = $tmp_ip;
293                 }
294             }
295         }
296         $results = $app->db->queryAllRecords("SELECT remote_ips FROM web_database WHERE remote_ips != ''");
297         if(!empty($results) && is_array($results)){
298             foreach($results as $result){
299                 $tmp_ips = explode(',', $result['remote_ips']);
300                 foreach($tmp_ips as $tmp_ip){
301                     $tmp_ip = trim($tmp_ip);
302                     if(preg_match($regex, $tmp_ip)) $ips[] = $tmp_ip;
303                 }
304             }
305         }
306         $ips = array_unique($ips);
307         sort($ips, SORT_NUMERIC);
308
309         $result_array = array('cheader' => array(), 'cdata' => array());
b1a6a5 310
5de2af 311         if(!empty($ips)){
M 312             $result_array['cheader'] = array('title' => 'IPs',
b1a6a5 313                 'total' => count($ips),
MC 314                 'limit' => count($ips)
315             );
316
5de2af 317             foreach($ips as $ip){
b1a6a5 318                 $result_array['cdata'][] = array( 'title' => $ip,
MC 319                     'description' => $type,
320                     'onclick' => '',
321                     'fill_text' => $ip
322                 );
5de2af 323             }
M 324         }
b1a6a5 325
5de2af 326         return $result_array;
M 327     }
328
b1a6a5 329     public function intval($string, $force_numeric = false) {
MC 330         if(intval($string) == 2147483647 || ($string > 0 && intval($string) < 0)) {
331             if($force_numeric == true) return floatval($string);
332             elseif(preg_match('/^([-]?)[0]*([1-9][0-9]*)([^0-9].*)*$/', $string, $match)) return $match[1].$match[2];
333             else return 0;
334         } else {
335             return intval($string);
336         }
337     }
338
339     /** IDN converter wrapper.
340      * all converter classes should be placed in ISPC_CLASS_PATH.'/idn/'
341      */
342
343
344     private function _idn_encode_decode($domain, $encode = true) {
345         if($domain == '') return '';
346         if(preg_match('/^[0-9\.]+$/', $domain)) return $domain; // may be an ip address - anyway does not need to bee encoded
347
348         // get domain and user part if it is an email
349         $user_part = false;
350         if(strpos($domain, '@') !== false) {
351             $user_part = substr($domain, 0, strrpos($domain, '@'));
352             $domain = substr($domain, strrpos($domain, '@') + 1);
353         }
354
355         if($encode == true) {
356             if(function_exists('idn_to_ascii')) {
e9d5c9 357                 $domain = idn_to_ascii($domain, IDNA_NONTRANSITIONAL_TO_ASCII, INTL_IDNA_VARIANT_UTS46);
b1a6a5 358             } elseif(file_exists(ISPC_CLASS_PATH.'/idn/idna_convert.class.php')) {
MC 359                 /* use idna class:
5de2af 360                  * @author  Matthias Sommerfeld <mso@phlylabs.de>
M 361                  * @copyright 2004-2011 phlyLabs Berlin, http://phlylabs.de
362                  * @version 0.8.0 2011-03-11
363                  */
b1a6a5 364
MC 365                 if(!is_object($this->idn_converter) || $this->idn_converter_name != 'idna_convert.class') {
366                     include_once ISPC_CLASS_PATH.'/idn/idna_convert.class.php';
367                     $this->idn_converter = new idna_convert(array('idn_version' => 2008));
368                     $this->idn_converter_name = 'idna_convert.class';
369                 }
370                 $domain = $this->idn_converter->encode($domain);
371             }
372         } else {
373             if(function_exists('idn_to_utf8')) {
e9d5c9 374                 $domain = idn_to_utf8($domain, IDNA_NONTRANSITIONAL_TO_ASCII, INTL_IDNA_VARIANT_UTS46);
b1a6a5 375             } elseif(file_exists(ISPC_CLASS_PATH.'/idn/idna_convert.class.php')) {
MC 376                 /* use idna class:
5de2af 377                  * @author  Matthias Sommerfeld <mso@phlylabs.de>
M 378                  * @copyright 2004-2011 phlyLabs Berlin, http://phlylabs.de
379                  * @version 0.8.0 2011-03-11
380                  */
b1a6a5 381
MC 382                 if(!is_object($this->idn_converter) || $this->idn_converter_name != 'idna_convert.class') {
383                     include_once ISPC_CLASS_PATH.'/idn/idna_convert.class.php';
384                     $this->idn_converter = new idna_convert(array('idn_version' => 2008));
385                     $this->idn_converter_name = 'idna_convert.class';
386                 }
387                 $domain = $this->idn_converter->decode($domain);
388             }
389         }
390
391         if($user_part !== false) return $user_part . '@' . $domain;
392         else return $domain;
393     }
394
395     public function idn_encode($domain) {
396         $domains = explode("\n", $domain);
397         for($d = 0; $d < count($domains); $d++) {
398             $domains[$d] = $this->_idn_encode_decode($domains[$d], true);
399         }
400         return implode("\n", $domains);
401     }
402
403     public function idn_decode($domain) {
404         $domains = explode("\n", $domain);
405         for($d = 0; $d < count($domains); $d++) {
406             $domains[$d] = $this->_idn_encode_decode($domains[$d], false);
407         }
408         return implode("\n", $domains);
409     }
410
5de2af 411 }
M 412
b1a6a5 413 ?>