"Fossies" - the Fresh Open Source Software Archive

Member "ispconfig3_install/interface/lib/classes/functions.inc.php" (8 Jun 2021, 24413 Bytes) of package /linux/privat/ISPConfig-3.2.5.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "functions.inc.php": 3.2.4_vs_3.2.5.

    1 <?php
    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;
   36     var $idn_converter_name = '';
   37 
   38     public function mail($to, $subject, $text, $from, $filepath = '', $filetype = 'application/pdf', $filename = '', $cc = '', $bcc = '', $from_name = '') {
   39         global $app, $conf;
   40 
   41         if($conf['demo_mode'] == true) $app->error("Mail sending disabled in demo mode.");
   42 
   43         $app->uses('getconf,ispcmail');
   44         $mail_config = $app->getconf->get_global_config('mail');
   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);
   52 
   53         if($filepath != '') {
   54             if(!file_exists($filepath)) $app->error("Mail attachement does not exist ".$filepath);
   55             $app->ispcmail->readAttachFile($filepath);
   56         }
   57 
   58         if($cc != '') $app->ispcmail->setHeader('Cc', $cc);
   59         if($bcc != '') $app->ispcmail->setHeader('Bcc', $bcc);
   60 
   61         if(is_string($to) && strpos($to, ',') !== false) {
   62                 $to = preg_split('/\s*,\s*/', $to);
   63         }
   64 
   65         $app->ispcmail->send($to);
   66         $app->ispcmail->finish();
   67 
   68         return true;
   69     }
   70 
   71     public function array_merge($array1, $array2) {
   72         $out = $array1;
   73         foreach($array2 as $key => $val) {
   74             $out[$key] = $val;
   75         }
   76         return $out;
   77     }
   78 
   79     public function currency_format($number, $view = '') {
   80         global $app;
   81         if($view != '') $number_format_decimals = (int)$app->lng('number_format_decimals_'.$view);
   82         if(!$number_format_decimals) $number_format_decimals = (int)$app->lng('number_format_decimals');
   83 
   84         $number_format_dec_point = $app->lng('number_format_dec_point');
   85         $number_format_thousands_sep = $app->lng('number_format_thousands_sep');
   86         if($number_format_thousands_sep == 'number_format_thousands_sep') $number_format_thousands_sep = '';
   87         return number_format((double)$number, $number_format_decimals, $number_format_dec_point, $number_format_thousands_sep);
   88     }
   89 
   90     //* convert currency formatted number back to floating number
   91     public function currency_unformat($number) {
   92         global $app;
   93 
   94         $number_format_dec_point = $app->lng('number_format_dec_point');
   95         $number_format_thousands_sep = $app->lng('number_format_thousands_sep');
   96         if($number_format_thousands_sep == 'number_format_thousands_sep') $number_format_thousands_sep = '';
   97 
   98         if($number_format_thousands_sep != '') $number = str_replace($number_format_thousands_sep, '', $number);
   99         if($number_format_dec_point != '.' && $number_format_dec_point != '') $number = str_replace($number_format_dec_point, '.', $number);
  100 
  101         return (double)$number;
  102     }
  103 
  104     public function get_ispconfig_url() {
  105         global $app;
  106 
  107         $url = (stristr($_SERVER['SERVER_PROTOCOL'], 'HTTPS') || stristr($_SERVER['HTTPS'], 'on'))?'https':'http';
  108         if($_SERVER['SERVER_NAME'] != '_') {
  109             $url .= '://'.$_SERVER['SERVER_NAME'];
  110             if($_SERVER['SERVER_PORT'] != 80 && $_SERVER['SERVER_PORT'] != 443) {
  111                 $url .= ':'.$_SERVER['SERVER_PORT'];
  112             }
  113         } else {
  114             $app->uses("getconf");
  115             $server_config = $app->getconf->get_server_config(1, 'server');
  116             $url .= '://'.$server_config['hostname'];
  117             if($_SERVER['SERVER_PORT'] != 80 && $_SERVER['SERVER_PORT'] != 443) {
  118                 $url .= ':'.$_SERVER['SERVER_PORT'];
  119             }
  120         }
  121         return $url;
  122     }
  123 
  124     public function json_encode($data) {
  125         if(!function_exists('json_encode')){
  126             if(is_array($data) || is_object($data)){
  127                 $islist = is_array($data) && (empty($data) || array_keys($data) === range(0, count($data)-1));
  128 
  129                 if($islist){
  130                     $json = '[' . implode(',', array_map(array($this, "json_encode"), $data) ) . ']';
  131                 } else {
  132                     $items = array();
  133                     foreach( $data as $key => $value ) {
  134                         $items[] = $this->json_encode("$key") . ':' . $this->json_encode($value);
  135                     }
  136                     $json = '{' . implode(',', $items) . '}';
  137                 }
  138             } elseif(is_string($data)){
  139                 // Escape non-printable or Non-ASCII characters.
  140                 // I also put the \\ character first, as suggested in comments on the 'addclashes' page.
  141                 $string = '"'.addcslashes($data, "\\\"\n\r\t/".chr(8).chr(12)).'"';
  142                 $json = '';
  143                 $len = strlen($string);
  144                 // Convert UTF-8 to Hexadecimal Codepoints.
  145                 for($i = 0; $i < $len; $i++){
  146                     $char = $string[$i];
  147                     $c1 = ord($char);
  148 
  149                     // Single byte;
  150                     if($c1 <128){
  151                         $json .= ($c1 > 31) ? $char : sprintf("\\u%04x", $c1);
  152                         continue;
  153                     }
  154 
  155                     // Double byte
  156                     $c2 = ord($string[++$i]);
  157                     if(($c1 & 32) === 0){
  158                         $json .= sprintf("\\u%04x", ($c1 - 192) * 64 + $c2 - 128);
  159                         continue;
  160                     }
  161 
  162                     // Triple
  163                     $c3 = ord($string[++$i]);
  164                     if(($c1 & 16) === 0){
  165                         $json .= sprintf("\\u%04x", (($c1 - 224) <<12) + (($c2 - 128) << 6) + ($c3 - 128));
  166                         continue;
  167                     }
  168 
  169                     // Quadruple
  170                     $c4 = ord($string[++$i]);
  171                     if(($c1 & 8) === 0){
  172                         $u = (($c1 & 15) << 2) + (($c2>>4) & 3) - 1;
  173 
  174                         $w1 = (54<<10) + ($u<<6) + (($c2 & 15) << 2) + (($c3>>4) & 3);
  175                         $w2 = (55<<10) + (($c3 & 15)<<6) + ($c4-128);
  176                         $json .= sprintf("\\u%04x\\u%04x", $w1, $w2);
  177                     }
  178                 }
  179             } else {
  180                 // int, floats, bools, null
  181                 $json = strtolower(var_export($data, true));
  182             }
  183             return $json;
  184         } else {
  185             return json_encode($data);
  186         }
  187     }
  188 
  189     public function suggest_ips($type = 'IPv4'){
  190         global $app;
  191 
  192         if($type == 'IPv4'){
  193 //          $regex = "/^[0-9]{1,3}(\.)[0-9]{1,3}(\.)[0-9]{1,3}(\.)[0-9]{1,3}$/";
  194             $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]?)$/";
  195         } else {
  196             // IPv6
  197             $regex = "/(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))/";
  198         }
  199 
  200         $server_by_id = array();
  201         $server_by_ip = array();
  202         $servers = $app->db->queryAllRecords("SELECT * FROM server");
  203         if(is_array($servers) && !empty($servers)){
  204             foreach($servers as $server){
  205                 $server_by_id[$server['server_id']] = $server['server_name'];
  206             }
  207         }
  208 
  209         $ips = array();
  210         $results = $app->db->queryAllRecords("SELECT ip_address AS ip, server_id FROM server_ip WHERE ip_type = ?", $type);
  211         if(!empty($results) && is_array($results)){
  212             foreach($results as $result){
  213                 if(preg_match($regex, $result['ip'])){
  214                     $ips[] = $result['ip'];
  215                     $server_by_ip[$result['ip']] = $server_by_id[$result['server_id']];
  216                 }
  217             }
  218         }
  219         $results = $app->db->queryAllRecords("SELECT ip_address AS ip FROM openvz_ip");
  220         if(!empty($results) && is_array($results)){
  221             foreach($results as $result){
  222                 if(preg_match($regex, $result['ip'])) $ips[] = $result['ip'];
  223             }
  224         }
  225         $results = $app->db->queryAllRecords("SELECT data AS ip FROM dns_rr WHERE type = 'A' OR type = 'AAAA'");
  226         if(!empty($results) && is_array($results)){
  227             foreach($results as $result){
  228                 if(preg_match($regex, $result['ip'])) $ips[] = $result['ip'];
  229             }
  230         }
  231         $results = $app->db->queryAllRecords("SELECT ns AS ip FROM dns_slave");
  232         if(!empty($results) && is_array($results)){
  233             foreach($results as $result){
  234                 if(preg_match($regex, $result['ip'])) $ips[] = $result['ip'];
  235             }
  236         }
  237 
  238         $results = $app->db->queryAllRecords("SELECT remote_ips FROM web_database WHERE remote_ips != ''");
  239         if(!empty($results) && is_array($results)){
  240             foreach($results as $result){
  241                 $tmp_ips = explode(',', $result['remote_ips']);
  242                 foreach($tmp_ips as $tmp_ip){
  243                     $tmp_ip = trim($tmp_ip);
  244                     if(preg_match($regex, $tmp_ip)) $ips[] = $tmp_ip;
  245                 }
  246             }
  247         }
  248         $ips = array_unique($ips);
  249         sort($ips, SORT_NUMERIC);
  250 
  251         $result_array = array('cheader' => array(), 'cdata' => array());
  252 
  253         if(!empty($ips)){
  254             $result_array['cheader'] = array('title' => 'IPs',
  255                 'total' => count($ips),
  256                 'limit' => count($ips)
  257             );
  258 
  259             foreach($ips as $ip){
  260                 $result_array['cdata'][] = array( 'title' => $ip,
  261                     'description' => $type.($server_by_ip[$ip] != ''? ' &gt; '.$server_by_ip[$ip] : ''),
  262                     'onclick' => '',
  263                     'fill_text' => $ip
  264                 );
  265             }
  266         }
  267 
  268         return $result_array;
  269     }
  270 
  271     public function intval($string, $force_numeric = false) {
  272         if(intval($string) == 2147483647 || ($string > 0 && intval($string) < 0)) {
  273             if($force_numeric == true) return floatval($string);
  274             elseif(preg_match('/^([-]?)[0]*([1-9][0-9]*)([^0-9].*)*$/', $string, $match)) return $match[1].$match[2];
  275             else return 0;
  276         } else {
  277             return intval($string);
  278         }
  279     }
  280 
  281     /**
  282      * Function to change bytes to kB, MB, GB or TB
  283      * @param int $size - size in bytes
  284      * @param int precicion - after-comma-numbers (default: 2)
  285      * @return string - formated bytes
  286      */
  287     public function formatBytes($size, $precision = 2) {
  288         $base=log($size)/log(1024);
  289         $suffixes=array('', ' kB', ' MB', ' GB', ' TB');
  290         return round(pow(1024, $base-floor($base)), $precision).$suffixes[floor($base)];
  291     }
  292 
  293 
  294     /**
  295      * Normalize a path and strip duplicate slashes from it
  296      *
  297      * This will also remove all /../ from the path, reducing the preceding path elements
  298      *
  299      * @param string $path
  300      * @return string
  301      */
  302     public function normalize_path($path) {
  303         $path = preg_replace('~[/]{2,}~', '/', $path);
  304         $parts = explode('/', $path);
  305         $return_parts = array();
  306 
  307         foreach($parts as $current_part) {
  308             if($current_part === '..') {
  309                 if(!empty($return_parts) && end($return_parts) !== '') {
  310                     array_pop($return_parts);
  311                 }
  312             } else {
  313                 $return_parts[] = $current_part;
  314             }
  315         }
  316 
  317         return implode('/', $return_parts);
  318     }
  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                 if(defined('IDNA_NONTRANSITIONAL_TO_ASCII') && defined('INTL_IDNA_VARIANT_UTS46') && constant('IDNA_NONTRANSITIONAL_TO_ASCII')) {
  338                     $domain = idn_to_ascii($domain, IDNA_NONTRANSITIONAL_TO_ASCII, INTL_IDNA_VARIANT_UTS46);
  339                 } else {
  340                     $domain = idn_to_ascii($domain);
  341                 }
  342             } elseif(file_exists(ISPC_CLASS_PATH.'/idn/idna_convert.class.php')) {
  343                 /* use idna class:
  344                  * @author  Matthias Sommerfeld <mso@phlylabs.de>
  345                  * @copyright 2004-2011 phlyLabs Berlin, http://phlylabs.de
  346                  * @version 0.8.0 2011-03-11
  347                  */
  348 
  349                 if(!is_object($this->idn_converter) || $this->idn_converter_name != 'idna_convert.class') {
  350                     include_once ISPC_CLASS_PATH.'/idn/idna_convert.class.php';
  351                     $this->idn_converter = new idna_convert(array('idn_version' => 2008));
  352                     $this->idn_converter_name = 'idna_convert.class';
  353                 }
  354                 $domain = $this->idn_converter->encode($domain);
  355             }
  356         } else {
  357             if(function_exists('idn_to_utf8')) {
  358                 if(defined('IDNA_NONTRANSITIONAL_TO_ASCII') && defined('INTL_IDNA_VARIANT_UTS46') && constant('IDNA_NONTRANSITIONAL_TO_ASCII')) {
  359                     $domain = idn_to_utf8($domain, IDNA_NONTRANSITIONAL_TO_ASCII, INTL_IDNA_VARIANT_UTS46);
  360                 } else {
  361                     $domain = idn_to_utf8($domain);
  362                 }
  363             } elseif(file_exists(ISPC_CLASS_PATH.'/idn/idna_convert.class.php')) {
  364                 /* use idna class:
  365                  * @author  Matthias Sommerfeld <mso@phlylabs.de>
  366                  * @copyright 2004-2011 phlyLabs Berlin, http://phlylabs.de
  367                  * @version 0.8.0 2011-03-11
  368                  */
  369 
  370                 if(!is_object($this->idn_converter) || $this->idn_converter_name != 'idna_convert.class') {
  371                     include_once ISPC_CLASS_PATH.'/idn/idna_convert.class.php';
  372                     $this->idn_converter = new idna_convert(array('idn_version' => 2008));
  373                     $this->idn_converter_name = 'idna_convert.class';
  374                 }
  375                 $domain = $this->idn_converter->decode($domain);
  376             }
  377         }
  378 
  379         if($user_part !== false) return $user_part . '@' . $domain;
  380         else return $domain;
  381     }
  382 
  383     public function idn_encode($domain) {
  384         $domains = explode("\n", $domain);
  385         for($d = 0; $d < count($domains); $d++) {
  386             $domains[$d] = $this->_idn_encode_decode($domains[$d], true);
  387         }
  388         return implode("\n", $domains);
  389     }
  390 
  391     public function idn_decode($domain) {
  392         $domains = explode("\n", $domain);
  393         for($d = 0; $d < count($domains); $d++) {
  394             $domains[$d] = $this->_idn_encode_decode($domains[$d], false);
  395         }
  396         return implode("\n", $domains);
  397     }
  398 
  399     public function is_allowed_user($username, $restrict_names = false) {
  400         global $app;
  401 
  402         $name_blacklist = array('root','ispconfig','vmail','getmail');
  403         if(in_array($username,$name_blacklist)) return false;
  404 
  405         if(preg_match('/^[a-zA-Z0-9\.\-_]{1,32}$/', $username) == false) return false;
  406 
  407         if($restrict_names == true && preg_match('/^web\d+$/', $username) == false) return false;
  408 
  409         return true;
  410     }
  411 
  412     public function is_allowed_group($groupname, $restrict_names = false) {
  413         global $app;
  414 
  415         $name_blacklist = array('root','ispconfig','vmail','getmail');
  416         if(in_array($groupname,$name_blacklist)) return false;
  417 
  418         if(preg_match('/^[a-zA-Z0-9\.\-_]{1,32}$/', $groupname) == false) return false;
  419 
  420         if($restrict_names == true && preg_match('/^client\d+$/', $groupname) == false) return false;
  421 
  422         return true;
  423     }
  424 
  425     public function getimagesizefromstring($string){
  426         if (!function_exists('getimagesizefromstring')) {
  427             $uri = 'data://application/octet-stream;base64,' . base64_encode($string);
  428             return getimagesize($uri);
  429         } else {
  430             return getimagesizefromstring($string);
  431         }
  432     }
  433 
  434     public function password($minLength = 10, $special = false){
  435         global $app;
  436 
  437         $iteration = 0;
  438         $password = "";
  439         $maxLength = $minLength + 5;
  440         $length = $this->getRandomInt($minLength, $maxLength);
  441 
  442         while($iteration < $length){
  443             $randomNumber = (floor(((mt_rand() / mt_getrandmax()) * 100)) % 94) + 33;
  444             if(!$special){
  445                 if (($randomNumber >=33) && ($randomNumber <=47)) { continue; }
  446                 if (($randomNumber >=58) && ($randomNumber <=64)) { continue; }
  447                 if (($randomNumber >=91) && ($randomNumber <=96)) { continue; }
  448                 if (($randomNumber >=123) && ($randomNumber <=126)) { continue; }
  449             }
  450             $iteration++;
  451             $password .= chr($randomNumber);
  452         }
  453         $app->uses('validate_password');
  454         if($app->validate_password->password_check('', $password, '') !== false) $password = $this->password($minLength, $special);
  455         return $password;
  456     }
  457 
  458     public function getRandomInt($min, $max){
  459         return floor((mt_rand() / mt_getrandmax()) * ($max - $min + 1)) + $min;
  460     }
  461 
  462     public function generate_customer_no(){
  463         global $app;
  464         // generate customer no.
  465         $customer_no = mt_rand(100000, 999999);
  466         while($app->db->queryOneRecord("SELECT client_id FROM client WHERE customer_no = ?", $customer_no)) {
  467             $customer_no = mt_rand(100000, 999999);
  468         }
  469 
  470         return $customer_no;
  471     }
  472 
  473     public function generate_ssh_key($client_id, $username = ''){
  474         global $app;
  475 
  476         // generate the SSH key pair for the client
  477         $id_rsa_file = '/tmp/'.uniqid('',true);
  478         $id_rsa_pub_file = $id_rsa_file.'.pub';
  479         if(file_exists($id_rsa_file)) unset($id_rsa_file);
  480         if(file_exists($id_rsa_pub_file)) unset($id_rsa_pub_file);
  481         if(!file_exists($id_rsa_file) && !file_exists($id_rsa_pub_file)) {
  482             $app->system->exec_safe('ssh-keygen -t rsa -C ? -f ? -N ""', $username.'-rsa-key-'.time(), $id_rsa_file);
  483             $app->db->query("UPDATE client SET created_at = UNIX_TIMESTAMP(), id_rsa = ?, ssh_rsa = ? WHERE client_id = ?", @file_get_contents($id_rsa_file), @file_get_contents($id_rsa_pub_file), $client_id);
  484             $app->system->exec_safe('rm -f ? ?', $id_rsa_file, $id_rsa_pub_file);
  485         } else {
  486             $app->log("Failed to create SSH keypair for ".$username, LOGLEVEL_WARN);
  487         }
  488     }
  489 
  490     public function htmlentities($value) {
  491         global $conf;
  492 
  493         if(is_array($value)) {
  494             $out = array();
  495             foreach($value as $key => $val) {
  496                 if(is_array($val)) {
  497                     $out[$key] = $this->htmlentities($val);
  498                 } else {
  499                     $out[$key] = htmlentities($val, ENT_QUOTES, $conf["html_content_encoding"]);
  500                 }
  501             }
  502         } else {
  503             $out = htmlentities($value, ENT_QUOTES, $conf["html_content_encoding"]);
  504         }
  505 
  506         return $out;
  507     }
  508 
  509     // Function to check paths before we use it as include. Use with absolute paths only.
  510     public function check_include_path($path) {
  511         if(strpos($path,'//') !== false) die('Include path seems to be an URL: '.$this->htmlentities($path));
  512         if(strpos($path,'..') !== false) die('Two dots are not allowed in include path: '.$this->htmlentities($path));
  513         if(!preg_match("/^[a-zA-Z0-9_\/\.\-]+$/", $path)) die('Wrong chars in include path: '.$this->htmlentities($path));
  514         $path = realpath($path);
  515         if($path == '') die('Include path does not exist.');
  516         if(substr($path,0,strlen(ISPC_ROOT_PATH)) != ISPC_ROOT_PATH) die('Path '.$this->htmlentities($path).' is outside of ISPConfig installation directory.');
  517         return $path;
  518     }
  519 
  520     // Function to check language strings
  521     public function check_language($language) {
  522         global $app;
  523         if(preg_match('/^[a-z]{2}$/',$language)) {
  524              return $language;
  525         } else {
  526             $app->log('Wrong language string: '.$this->htmlentities($language),1);
  527             return 'en';
  528         }
  529     }
  530 
  531         // Function to lock a client
  532     public function func_client_lock($client_id,$locked) {
  533         global $app;
  534         $client_data = $app->db->queryOneRecord('SELECT `tmp_data` FROM `client` WHERE `client_id` = ?', $client_id);
  535         if($client_data['tmp_data'] == '') $tmp_data = array();
  536         else $tmp_data = unserialize($client_data['tmp_data']);
  537         if(!is_array($tmp_data)) $tmp_data = array();
  538         $to_disable = array('cron' => 'id',
  539                             'ftp_user' => 'ftp_user_id',
  540                             'mail_domain' => 'domain_id',
  541                             'mail_user' => 'mailuser_id',
  542                             'mail_user_smtp' => 'mailuser_id',
  543                             'mail_forwarding' => 'forwarding_id',
  544                             'mail_get' => 'mailget_id',
  545                             'openvz_vm' => 'vm_id',
  546                             'shell_user' => 'shell_user_id',
  547                             'webdav_user' => 'webdav_user_id',
  548                             'web_database' => 'database_id',
  549                             'web_domain' => 'domain_id',
  550                             'web_folder' => 'web_folder_id',
  551                             'web_folder_user' => 'web_folder_user_id'
  552                             );
  553         $udata = $app->db->queryOneRecord('SELECT `userid` FROM `sys_user` WHERE `client_id` = ?', $client_id);
  554         $gdata = $app->db->queryOneRecord('SELECT `groupid` FROM `sys_group` WHERE `client_id` = ?', $client_id);
  555         $sys_groupid = $gdata['groupid'];
  556         $sys_userid = $udata['userid'];
  557         if($locked == 'y') {
  558             $prev_active = array();
  559             $prev_sysuser = array();
  560             foreach($to_disable as $current => $keycolumn) {
  561                 $active_col = 'active';
  562                 $reverse = false;
  563                 if($current == 'mail_user') {
  564                         $active_col = 'postfix';
  565                 } elseif($current == 'mail_user_smtp') {
  566                         $current = 'mail_user';
  567                         $active_col = 'disablesmtp';
  568                         $reverse = true;
  569                 }
  570 
  571                 if(!isset($prev_active[$current])) $prev_active[$current] = array();
  572                 if(!isset($prev_sysuser[$current])) $prev_sysuser[$current] = array();
  573 
  574                 $entries = $app->db->queryAllRecords('SELECT ?? as `id`, `sys_userid`, ?? FROM ?? WHERE `sys_groupid` = ?', $keycolumn, $active_col, $current, $sys_groupid);
  575                 foreach($entries as $item) {
  576 
  577                         if($item[$active_col] != 'y' && $reverse == false) $prev_active[$current][$item['id']][$active_col] = 'n';
  578                         elseif($item[$active_col] == 'y' && $reverse == true) $prev_active[$current][$item['id']][$active_col] = 'y';
  579                         if($item['sys_userid'] != $sys_userid) $prev_sysuser[$current][$item['id']] = $item['sys_userid'];
  580                         // we don't have to store these if y, as everything without previous state gets enabled later
  581 
  582                         //$app->db->datalogUpdate($current, array($active_col => ($reverse == true ? 'y' : 'n'), 'sys_userid' => $_SESSION["s"]["user"]["userid"]), $keycolumn, $item['id']);
  583                         $app->db->datalogUpdate($current, array($active_col => ($reverse == true ? 'y' : 'n'), 'sys_userid' => $sys_userid), $keycolumn, $item['id']);
  584                 }
  585             }
  586 
  587             $tmp_data['prev_active'] = $prev_active;
  588             $tmp_data['prev_sys_userid'] = $prev_sysuser;
  589             $app->db->query("UPDATE `client` SET `tmp_data` = ? WHERE `client_id` = ?", serialize($tmp_data), $client_id);
  590             unset($prev_active);
  591             unset($prev_sysuser);
  592         } elseif ($locked == 'n') {
  593             foreach($to_disable as $current => $keycolumn) {
  594                 $active_col = 'active';
  595                 $reverse = false;
  596                 if($current == 'mail_user') {
  597                         $active_col = 'postfix';
  598                 } elseif($current == 'mail_user_smtp') {
  599                         $current = 'mail_user';
  600                         $active_col = 'disablesmtp';
  601                         $reverse = true;
  602                 }
  603 
  604                 $entries = $app->db->queryAllRecords('SELECT ?? as `id` FROM ?? WHERE `sys_groupid` = ?', $keycolumn, $current, $sys_groupid);
  605                 foreach($entries as $item) {
  606                         $set_active = ($reverse == true ? 'n' : 'y');
  607                         $set_inactive = ($reverse == true ? 'y' : 'n');
  608                         $set_sysuser = $sys_userid;
  609                         if(array_key_exists('prev_active', $tmp_data) == true
  610                                 && array_key_exists($current, $tmp_data['prev_active']) == true
  611                                 && array_key_exists($item['id'], $tmp_data['prev_active'][$current]) == true
  612                                 && $tmp_data['prev_active'][$current][$item['id']][$active_col] == $set_inactive) $set_active = $set_inactive;
  613                         if(array_key_exists('prev_sysuser', $tmp_data) == true
  614                                 && array_key_exists($current, $tmp_data['prev_sysuser']) == true
  615                                 && array_key_exists($item['id'], $tmp_data['prev_sysuser'][$current]) == true
  616                                 && $tmp_data['prev_sysuser'][$current][$item['id']] != $sys_userid) $set_sysuser = $tmp_data['prev_sysuser'][$current][$item['id']];
  617                         $app->db->datalogUpdate($current, array($active_col => $set_active, 'sys_userid' => $set_sysuser), $keycolumn, $item['id']);
  618                 }
  619             }
  620             if(array_key_exists('prev_active', $tmp_data)) unset($tmp_data['prev_active']);
  621             $app->db->query("UPDATE `client` SET `tmp_data` = ? WHERE `client_id` = ?", serialize($tmp_data), $client_id);
  622         }
  623         unset($tmp_data);
  624         unset($entries);
  625         unset($to_disable);
  626     }
  627     // Function to cancel disable/enable a client
  628     public function func_client_cancel($client_id,$cancel) {
  629         global $app;
  630         if ($cancel == 'y') {
  631             $sql = "UPDATE sys_user SET active = '0' WHERE client_id = ?";
  632             $result = $app->db->query($sql, $client_id);
  633         } elseif($cancel == 'n') {
  634             $sql = "UPDATE sys_user SET active = '1' WHERE client_id = ?";
  635             $result = $app->db->query($sql, $client_id);
  636         } else {
  637             $result = false;
  638         }
  639         return $result;
  640     }   
  641 
  642 }
  643 
  644 ?>