Marius Cramer
2014-08-13 65ad3435addc6292e378c88ec861dc58a3a8da3d
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;
7fe908 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 = '') {
7fe908 39         global $app, $conf;
MC 40
532ae5 41         if($conf['demo_mode'] == true) $app->error("Mail sending disabled in demo mode.");
7fe908 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);
7fe908 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         }
7fe908 57
a59498 58         if($cc != '') $app->ispcmail->setHeader('Cc', $cc);
M 59         if($bcc != '') $app->ispcmail->setHeader('Bcc', $bcc);
7fe908 60
a59498 61         $app->ispcmail->send($to);
M 62         $app->ispcmail->finish();
7fe908 63
a59498 64         /* left in here just for the case...
532ae5 65         if($filepath != '') {
L 66             if(!file_exists($filepath)) $app->error("Mail attachement does not exist ".$filepath);
7fe908 67
532ae5 68             $content = file_get_contents($filepath);
L 69             $content = chunk_split(base64_encode($content));
70             $uid = strtoupper(md5(uniqid(time())));
355efb 71             $subject      = "=?utf-8?B?".base64_encode($subject)."?=";
7fe908 72
532ae5 73             if($filename == '') {
L 74                 $path_parts = pathinfo($filepath);
75                 $filename = $path_parts["basename"];
76                 unset($path_parts);
77             }
78
6db277 79             $header = "Return-Path: $from\nFrom: $from\nReply-To: $from\n";
F 80             if($cc != '') $header .= "Cc: $cc\n";
81             if($bcc != '') $header .= "Bcc: $bcc\n";
532ae5 82             $header .= "MIME-Version: 1.0\n";
L 83             $header .= "Content-Type: multipart/mixed; boundary=$uid\n";
84
85             $header .= "--$uid\n";
5645a5 86             $header .= "Content-Type: text/plain;\n\tcharset=\"UTF-8\"\n";
532ae5 87             $header .= "Content-Transfer-Encoding: 8bit\n\n";
L 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";
6db277 102             if($cc != '') $header .= "Cc: $cc\n";
F 103             if($bcc != '') $header .= "Bcc: $bcc\n";
723662 104             $header .= "Content-Type: text/plain;\n\tcharset=\"UTF-8\"\n";
T 105             $header .= "Content-Transfer-Encoding: 8bit\n\n";
355efb 106             $subject      = "=?utf-8?B?".base64_encode($subject)."?=";
532ae5 107             mail($to, $subject, $text, $header);
L 108         }
a59498 109         */
532ae5 110         return true;
L 111     }
7fe908 112
MC 113     public function array_merge($array1, $array2) {
532ae5 114         $out = $array1;
L 115         foreach($array2 as $key => $val) {
116             $out[$key] = $val;
117         }
118         return $out;
119     }
7fe908 120
36d487 121     public function currency_format($number, $view = '') {
532ae5 122         global $app;
36d487 123         if($view != '') $number_format_decimals = (int)$app->lng('number_format_decimals_'.$view);
7fe908 124         if(!$number_format_decimals) $number_format_decimals = (int)$app->lng('number_format_decimals');
MC 125
532ae5 126         $number_format_dec_point = $app->lng('number_format_dec_point');
L 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     }
7fe908 131
2d2fd1 132     //* convert currency formatted number back to floating number
FT 133     public function currency_unformat($number) {
134         global $app;
7fe908 135
2d2fd1 136         $number_format_dec_point = $app->lng('number_format_dec_point');
FT 137         $number_format_thousands_sep = $app->lng('number_format_thousands_sep');
138         if($number_format_thousands_sep == 'number_format_thousands_sep') $number_format_thousands_sep = '';
7fe908 139
2d2fd1 140         if($number_format_thousands_sep != '') $number = str_replace($number_format_thousands_sep, '', $number);
FT 141         if($number_format_dec_point != '.' && $number_format_dec_point != '') $number = str_replace($number_format_dec_point, '.', $number);
7fe908 142
2d2fd1 143         return (double)$number;
FT 144     }
7fe908 145
532ae5 146     public function get_ispconfig_url() {
615a0a 147         global $app;
7fe908 148
MC 149         $url = (stristr($_SERVER['SERVER_PROTOCOL'], 'HTTPS') || stristr($_SERVER['HTTPS'], 'on'))?'https':'http';
615a0a 150         if($_SERVER['SERVER_NAME'] != '_') {
T 151             $url .= '://'.$_SERVER['SERVER_NAME'];
152             if($_SERVER['SERVER_PORT'] != 80 && $_SERVER['SERVER_PORT'] != 443) {
153                 $url .= ':'.$_SERVER['SERVER_PORT'];
154             }
155         } else {
156             $app->uses("getconf");
7fe908 157             $server_config = $app->getconf->get_server_config(1, 'server');
615a0a 158             $url .= '://'.$server_config['hostname'];
T 159             if($_SERVER['SERVER_PORT'] != 80 && $_SERVER['SERVER_PORT'] != 443) {
160                 $url .= ':'.$_SERVER['SERVER_PORT'];
161             }
532ae5 162         }
L 163         return $url;
164     }
7fe908 165
MC 166     public function json_encode($data) {
4c28d9 167         if(!function_exists('json_encode')){
221340 168             if(is_array($data) || is_object($data)){
7fe908 169                 $islist = is_array($data) && (empty($data) || array_keys($data) === range(0, count($data)-1));
221340 170
F 171                 if($islist){
172                     $json = '[' . implode(',', array_map(array($this, "json_encode"), $data) ) . ']';
173                 } else {
7fe908 174                     $items = array();
221340 175                     foreach( $data as $key => $value ) {
F 176                         $items[] = $this->json_encode("$key") . ':' . $this->json_encode($value);
4c28d9 177                     }
221340 178                     $json = '{' . implode(',', $items) . '}';
F 179                 }
180             } elseif(is_string($data)){
7fe908 181                 // Escape non-printable or Non-ASCII characters.
MC 182                 // I also put the \\ character first, as suggested in comments on the 'addclashes' page.
221340 183                 $string = '"'.addcslashes($data, "\\\"\n\r\t/".chr(8).chr(12)).'"';
F 184                 $json = '';
185                 $len = strlen($string);
7fe908 186                 // Convert UTF-8 to Hexadecimal Codepoints.
221340 187                 for($i = 0; $i < $len; $i++){
F 188                     $char = $string[$i];
189                     $c1 = ord($char);
190
7fe908 191                     // Single byte;
221340 192                     if($c1 <128){
F 193                         $json .= ($c1 > 31) ? $char : sprintf("\\u%04x", $c1);
194                         continue;
4c28d9 195                     }
221340 196
7fe908 197                     // Double byte
221340 198                     $c2 = ord($string[++$i]);
F 199                     if(($c1 & 32) === 0){
200                         $json .= sprintf("\\u%04x", ($c1 - 192) * 64 + $c2 - 128);
201                         continue;
202                     }
203
7fe908 204                     // Triple
221340 205                     $c3 = ord($string[++$i]);
F 206                     if(($c1 & 16) === 0){
207                         $json .= sprintf("\\u%04x", (($c1 - 224) <<12) + (($c2 - 128) << 6) + ($c3 - 128));
208                         continue;
209                     }
210
7fe908 211                     // Quadruple
221340 212                     $c4 = ord($string[++$i]);
F 213                     if(($c1 & 8) === 0){
214                         $u = (($c1 & 15) << 2) + (($c2>>4) & 3) - 1;
215
216                         $w1 = (54<<10) + ($u<<6) + (($c2 & 15) << 2) + (($c3>>4) & 3);
217                         $w2 = (55<<10) + (($c3 & 15)<<6) + ($c4-128);
218                         $json .= sprintf("\\u%04x\\u%04x", $w1, $w2);
219                     }
220                 }
221             } else {
7fe908 222                 // int, floats, bools, null
221340 223                 $json = strtolower(var_export($data, true));
4c28d9 224             }
221340 225             return $json;
4c28d9 226         } else {
F 227             return json_encode($data);
228         }
7fe908 229     }
MC 230
5672cd 231     public function suggest_ips($type = 'IPv4'){
F 232         global $app;
7fe908 233
5672cd 234         if($type == 'IPv4'){
F 235             $regex = "/^[0-9]{1,3}(\.)[0-9]{1,3}(\.)[0-9]{1,3}(\.)[0-9]{1,3}$/";
236         } else {
237             // IPv6
238             $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";
239         }
7fe908 240
391e05 241         $server_by_id = array();
FT 242         $server_by_ip = array();
243         $servers = $app->db->queryAllRecords("SELECT * FROM server");
244         if(is_array($servers) && !empty($servers)){
245             foreach($servers as $server){
246                 $server_by_id[$server['server_id']] = $server['server_name'];
247             }
248         }
7fe908 249
5672cd 250         $ips = array();
65ad34 251         $results = $app->db->queryAllRecords("SELECT ip_address AS ip, server_id FROM server_ip WHERE ip_type = '".$app->db->quote($type)."'");
5672cd 252         if(!empty($results) && is_array($results)){
F 253             foreach($results as $result){
391e05 254                 if(preg_match($regex, $result['ip'])){
FT 255                     $ips[] = $result['ip'];
256                     $server_by_ip[$result['ip']] = $server_by_id[$result['server_id']];
257                 }
5672cd 258             }
F 259         }
260         $results = $app->db->queryAllRecords("SELECT ip_address AS ip FROM openvz_ip");
261         if(!empty($results) && is_array($results)){
262             foreach($results as $result){
263                 if(preg_match($regex, $result['ip'])) $ips[] = $result['ip'];
264             }
265         }
266         $results = $app->db->queryAllRecords("SELECT data AS ip FROM dns_rr WHERE type = 'A' OR type = 'AAAA'");
267         if(!empty($results) && is_array($results)){
268             foreach($results as $result){
269                 if(preg_match($regex, $result['ip'])) $ips[] = $result['ip'];
270             }
271         }
272         $results = $app->db->queryAllRecords("SELECT ns AS ip FROM dns_slave");
273         if(!empty($results) && is_array($results)){
274             foreach($results as $result){
275                 if(preg_match($regex, $result['ip'])) $ips[] = $result['ip'];
276             }
277         }
374e5a 278         
TB 279         /*
5672cd 280         $results = $app->db->queryAllRecords("SELECT xfer FROM dns_slave WHERE xfer != ''");
F 281         if(!empty($results) && is_array($results)){
282             foreach($results as $result){
283                 $tmp_ips = explode(',', $result['xfer']);
284                 foreach($tmp_ips as $tmp_ip){
285                     $tmp_ip = trim($tmp_ip);
286                     if(preg_match($regex, $tmp_ip)) $ips[] = $tmp_ip;
287                 }
288             }
289         }
290         $results = $app->db->queryAllRecords("SELECT xfer FROM dns_soa WHERE xfer != ''");
291         if(!empty($results) && is_array($results)){
292             foreach($results as $result){
293                 $tmp_ips = explode(',', $result['xfer']);
294                 foreach($tmp_ips as $tmp_ip){
295                     $tmp_ip = trim($tmp_ip);
296                     if(preg_match($regex, $tmp_ip)) $ips[] = $tmp_ip;
297                 }
298             }
299         }
300         $results = $app->db->queryAllRecords("SELECT also_notify FROM dns_soa WHERE also_notify != ''");
301         if(!empty($results) && is_array($results)){
302             foreach($results as $result){
303                 $tmp_ips = explode(',', $result['also_notify']);
304                 foreach($tmp_ips as $tmp_ip){
305                     $tmp_ip = trim($tmp_ip);
306                     if(preg_match($regex, $tmp_ip)) $ips[] = $tmp_ip;
307                 }
308             }
309         }
374e5a 310         */
TB 311         
5672cd 312         $results = $app->db->queryAllRecords("SELECT remote_ips FROM web_database WHERE remote_ips != ''");
F 313         if(!empty($results) && is_array($results)){
314             foreach($results as $result){
315                 $tmp_ips = explode(',', $result['remote_ips']);
316                 foreach($tmp_ips as $tmp_ip){
317                     $tmp_ip = trim($tmp_ip);
318                     if(preg_match($regex, $tmp_ip)) $ips[] = $tmp_ip;
319                 }
320             }
321         }
322         $ips = array_unique($ips);
323         sort($ips, SORT_NUMERIC);
324
325         $result_array = array('cheader' => array(), 'cdata' => array());
7fe908 326
5672cd 327         if(!empty($ips)){
F 328             $result_array['cheader'] = array('title' => 'IPs',
7fe908 329                 'total' => count($ips),
MC 330                 'limit' => count($ips)
331             );
332
5672cd 333             foreach($ips as $ip){
7fe908 334                 $result_array['cdata'][] = array( 'title' => $ip,
MC 335                     'description' => $type.($server_by_ip[$ip] != ''? ' &gt; '.$server_by_ip[$ip] : ''),
336                     'onclick' => '',
337                     'fill_text' => $ip
338                 );
5672cd 339             }
F 340         }
7fe908 341
5672cd 342         return $result_array;
F 343     }
4c28d9 344
7fe908 345     public function intval($string, $force_numeric = false) {
MC 346         if(intval($string) == 2147483647 || ($string > 0 && intval($string) < 0)) {
347             if($force_numeric == true) return floatval($string);
348             elseif(preg_match('/^([-]?)[0]*([1-9][0-9]*)([^0-9].*)*$/', $string, $match)) return $match[1].$match[2];
349             else return 0;
350         } else {
351             return intval($string);
352         }
353     }
354
355     /** IDN converter wrapper.
356      * all converter classes should be placed in ISPC_CLASS_PATH.'/idn/'
357      */
358
359
360     private function _idn_encode_decode($domain, $encode = true) {
361         if($domain == '') return '';
362         if(preg_match('/^[0-9\.]+$/', $domain)) return $domain; // may be an ip address - anyway does not need to bee encoded
363
364         // get domain and user part if it is an email
365         $user_part = false;
366         if(strpos($domain, '@') !== false) {
367             $user_part = substr($domain, 0, strrpos($domain, '@'));
368             $domain = substr($domain, strrpos($domain, '@') + 1);
369         }
370
371         if($encode == true) {
372             if(function_exists('idn_to_ascii')) {
373                 $domain = idn_to_ascii($domain);
374             } elseif(file_exists(ISPC_CLASS_PATH.'/idn/idna_convert.class.php')) {
375                 /* use idna class:
d6363b 376                  * @author  Matthias Sommerfeld <mso@phlylabs.de>
M 377                  * @copyright 2004-2011 phlyLabs Berlin, http://phlylabs.de
378                  * @version 0.8.0 2011-03-11
379                  */
7fe908 380
MC 381                 if(!is_object($this->idn_converter) || $this->idn_converter_name != 'idna_convert.class') {
382                     include_once ISPC_CLASS_PATH.'/idn/idna_convert.class.php';
383                     $this->idn_converter = new idna_convert(array('idn_version' => 2008));
384                     $this->idn_converter_name = 'idna_convert.class';
385                 }
386                 $domain = $this->idn_converter->encode($domain);
387             }
388         } else {
389             if(function_exists('idn_to_utf8')) {
390                 $domain = idn_to_utf8($domain);
391             } elseif(file_exists(ISPC_CLASS_PATH.'/idn/idna_convert.class.php')) {
392                 /* use idna class:
d6363b 393                  * @author  Matthias Sommerfeld <mso@phlylabs.de>
M 394                  * @copyright 2004-2011 phlyLabs Berlin, http://phlylabs.de
395                  * @version 0.8.0 2011-03-11
396                  */
7fe908 397
MC 398                 if(!is_object($this->idn_converter) || $this->idn_converter_name != 'idna_convert.class') {
399                     include_once ISPC_CLASS_PATH.'/idn/idna_convert.class.php';
400                     $this->idn_converter = new idna_convert(array('idn_version' => 2008));
401                     $this->idn_converter_name = 'idna_convert.class';
402                 }
403                 $domain = $this->idn_converter->decode($domain);
404             }
405         }
406
407         if($user_part !== false) return $user_part . '@' . $domain;
408         else return $domain;
409     }
410
411     public function idn_encode($domain) {
412         $domains = explode("\n", $domain);
413         for($d = 0; $d < count($domains); $d++) {
414             $domains[$d] = $this->_idn_encode_decode($domains[$d], true);
415         }
416         return implode("\n", $domains);
417     }
418
419     public function idn_decode($domain) {
420         $domains = explode("\n", $domain);
421         for($d = 0; $d < count($domains); $d++) {
422             $domains[$d] = $this->_idn_encode_decode($domains[$d], false);
423         }
424         return implode("\n", $domains);
425     }
426
64ea56 427     public function is_allowed_user($username, $restrict_names = false) {
MC 428         global $app;
429         
f2fc77 430         $name_blacklist = array('root','ispconfig','vmail','getmail');
TB 431         if(in_array($username,$name_blacklist)) return false;
432         
373e88 433         if(preg_match('/^[a-zA-Z0-9\.\-_]{1,32}$/', $username) == false) return false;
f2fc77 434         
64ea56 435         if($restrict_names == true && preg_match('/^web\d+$/', $username) == false) return false;
MC 436         
437         return true;
438     }
439     
440     public function is_allowed_group($groupname, $restrict_names = false) {
441         global $app;
442         
f2fc77 443         $name_blacklist = array('root','ispconfig','vmail','getmail');
TB 444         if(in_array($groupname,$name_blacklist)) return false;
445         
373e88 446         if(preg_match('/^[a-zA-Z0-9\.\-_]{1,32}$/', $groupname) == false) return false;
f2fc77 447         
64ea56 448         if($restrict_names == true && preg_match('/^client\d+$/', $groupname) == false) return false;
MC 449         
450         return true;
451     }
452
532ae5 453 }
L 454
7fe908 455 ?>