bug 1318: Fix a check for if there were any servers
[cacert-devel.git] / includes / general.php
index d762586..065daaa 100644 (file)
        session_name("cacert");
        session_start();
 
        session_name("cacert");
        session_start();
 
-       session_register("_config");
-       session_register("profile");
-       session_register("signup");
-       session_register("lostpw");
+//     session_register("_config");
+//     session_register("profile");
+//     session_register("signup");
+//     session_register("lostpw");
 //     if($_SESSION['profile']['id'] > 0)
 //             session_regenerate_id();
 
 //     if($_SESSION['profile']['id'] > 0)
 //             session_regenerate_id();
 
@@ -57,7 +57,7 @@
                exit;
        }
 
                exit;
        }
 
-       if(array_key_exists('HTTP_HOST',$_SERVER) && 
+       if(array_key_exists('HTTP_HOST',$_SERVER) &&
                        ($_SERVER['HTTP_HOST'] == $_SESSION['_config']['securehostname'] ||
                        $_SERVER['HTTP_HOST'] == $_SESSION['_config']['tverify']))
        {
                        ($_SERVER['HTTP_HOST'] == $_SESSION['_config']['securehostname'] ||
                        $_SERVER['HTTP_HOST'] == $_SESSION['_config']['tverify']))
        {
        L10n::detect_language();
        L10n::init_gettext();
 
        L10n::detect_language();
        L10n::init_gettext();
 
-        if(array_key_exists('profile',$_SESSION) && is_array($_SESSION['profile']) && array_key_exists('id',$_SESSION['profile']) && $_SESSION['profile']['id'] > 0)
+       if(array_key_exists('profile',$_SESSION) && is_array($_SESSION['profile']) && array_key_exists('id',$_SESSION['profile']) && $_SESSION['profile']['id'] > 0)
        {
        {
-               $locked = mysql_fetch_assoc(mysql_query("select `locked` from `users` where `id`='".$_SESSION['profile']['id']."'"));
+               $locked = mysql_fetch_assoc(mysql_query("select `locked` from `users` where `id`='".intval($_SESSION['profile']['id'])."'"));
                if($locked['locked'] == 0)
                {
                if($locked['locked'] == 0)
                {
-                       $query = "select sum(`points`) as `total` from `notary` where `to`='".$_SESSION['profile']['id']."' group by `to`";
+                       $query = "select sum(`points`) as `total` from `notary` where `to`='".intval($_SESSION['profile']['id'])."' and `deleted` = 0 group by `to`";
                        $res = mysql_query($query);
                        $row = mysql_fetch_assoc($res);
                        $_SESSION['profile']['points'] = $row['total'];
                        $res = mysql_query($query);
                        $row = mysql_fetch_assoc($res);
                        $_SESSION['profile']['points'] = $row['total'];
                        $points++;
 
                //echo "Points due to length and charset: $points<br/>";
                        $points++;
 
                //echo "Points due to length and charset: $points<br/>";
-               
+
                // check for historical password proposal
                if ($pwd === "Fr3d Sm|7h") {
                        return 0;
                }
                // check for historical password proposal
                if ($pwd === "Fr3d Sm|7h") {
                        return 0;
                }
-               
+
                return $points;
        }
 
        function checkpw($pwd, $email, $fname, $mname, $lname, $suffix)
        {
                $points = checkpwlight($pwd);
                return $points;
        }
 
        function checkpw($pwd, $email, $fname, $mname, $lname, $suffix)
        {
                $points = checkpwlight($pwd);
-               
+
                if(@strstr(strtolower($pwd), strtolower($email)))
                        $points--;
 
                if(@strstr(strtolower($pwd), strtolower($email)))
                        $points--;
 
                //echo "Points due to name matches: $points<br/>";
 
                $shellpwd = escapeshellarg($pwd);
                //echo "Points due to name matches: $points<br/>";
 
                $shellpwd = escapeshellarg($pwd);
-               $do = `grep $shellpwd /usr/share/dict/american-english`;
+               $do = shell_exec("grep -F -- $shellpwd /usr/share/dict/american-english");
                if($do)
                        $points--;
 
                if($do)
                        $points--;
 
        {
                $bits = explode(": ", $_SESSION['_config']['subject'], 2);
                $bits = str_replace(", ", "|", str_replace("/", "|", array_key_exists('1',$bits)?$bits['1']:""));
        {
                $bits = explode(": ", $_SESSION['_config']['subject'], 2);
                $bits = str_replace(", ", "|", str_replace("/", "|", array_key_exists('1',$bits)?$bits['1']:""));
-               $bits = explode("|", $bits);    
+               $bits = explode("|", $bits);
 
                $_SESSION['_config']['cnc'] = $_SESSION['_config']['subaltc'] = 0;
                $_SESSION['_config']['OU'] = "";
 
                $_SESSION['_config']['cnc'] = $_SESSION['_config']['subaltc'] = 0;
                $_SESSION['_config']['OU'] = "";
                                        $dom = $bits[$i];
                                $_SESSION['_config']['row'] = "";
                                $dom = mysql_real_escape_string($dom);
                                        $dom = $bits[$i];
                                $_SESSION['_config']['row'] = "";
                                $dom = mysql_real_escape_string($dom);
-                               $query = "select * from domains where `memid`='".$_SESSION['profile']['id']."' and `domain` like '$dom' and `deleted`=0 and `hash`=''";
+                               $query = "select * from domains where `memid`='".intval($_SESSION['profile']['id'])."' and `domain` like '$dom' and `deleted`=0 and `hash`=''";
                                $res = mysql_query($query);
                                if(mysql_num_rows($res) > 0)
                                {
                                $res = mysql_query($query);
                                if(mysql_num_rows($res) > 0)
                                {
                                        $dom = $bits[$i];
                                $_SESSION['_config']['altrow'] = "";
                                $dom = mysql_real_escape_string($dom);
                                        $dom = $bits[$i];
                                $_SESSION['_config']['altrow'] = "";
                                $dom = mysql_real_escape_string($dom);
-                               $query = "select * from domains where `memid`='".$_SESSION['profile']['id']."' and `domain` like '$dom' and `deleted`=0 and `hash`=''";
+                               $query = "select * from domains where `memid`='".intval($_SESSION['profile']['id'])."' and `domain` like '$dom' and `deleted`=0 and `hash`=''";
                                $res = mysql_query($query);
                                if(mysql_num_rows($res) > 0)
                                {
                                $res = mysql_query($query);
                                if(mysql_num_rows($res) > 0)
                                {
                                $_SESSION['_config']['row'] = "";
                                $dom = mysql_real_escape_string($dom);
                                $query = "select *, `orginfo`.`id` as `id` from `orginfo`,`orgdomains`,`org` where
                                $_SESSION['_config']['row'] = "";
                                $dom = mysql_real_escape_string($dom);
                                $query = "select *, `orginfo`.`id` as `id` from `orginfo`,`orgdomains`,`org` where
-                                               `org`.`memid`='".$_SESSION['profile']['id']."' and
+                                               `org`.`memid`='".intval($_SESSION['profile']['id'])."' and
                                                `org`.`orgid`=`orginfo`.`id` and
                                                `orgdomains`.`orgid`=`orginfo`.`id` and
                                                `orgdomains`.`domain`='$dom'";
                                                `org`.`orgid`=`orginfo`.`id` and
                                                `orgdomains`.`orgid`=`orginfo`.`id` and
                                                `orgdomains`.`domain`='$dom'";
                                $_SESSION['_config']['altrow'] = "";
                                $dom = mysql_real_escape_string($dom);
                                $query = "select * from `orginfo`,`orgdomains`,`org` where
                                $_SESSION['_config']['altrow'] = "";
                                $dom = mysql_real_escape_string($dom);
                                $query = "select * from `orginfo`,`orgdomains`,`org` where
-                                               `org`.`memid`='".$_SESSION['profile']['id']."' and
+                                               `org`.`memid`='".intval($_SESSION['profile']['id'])."' and
                                                `org`.`orgid`=`orginfo`.`id` and
                                                `orgdomains`.`orgid`=`orginfo`.`id` and
                                                `orgdomains`.`domain`='$dom'";
                                                `org`.`orgid`=`orginfo`.`id` and
                                                `orgdomains`.`orgid`=`orginfo`.`id` and
                                                `orgdomains`.`domain`='$dom'";
                                $dom = $bits[$i];
                        $dom = mysql_real_escape_string($dom);
                        $query = "select * from `org`,`orgdomains`,`orginfo`
                                $dom = $bits[$i];
                        $dom = mysql_real_escape_string($dom);
                        $query = "select * from `org`,`orgdomains`,`orginfo`
-                                       where `org`.`memid`='".$_SESSION['profile']['id']."'
+                                       where `org`.`memid`='".intval($_SESSION['profile']['id'])."'
                                        and `orgdomains`.`orgid`=`org`.`orgid`
                                        and `orginfo`.`id`=`org`.`orgid`
                                        and `orgdomains`.`domain`='$dom'";
                                        and `orgdomains`.`orgid`=`org`.`orgid`
                                        and `orginfo`.`id`=`org`.`orgid`
                                        and `orgdomains`.`domain`='$dom'";
                if($id <= 0)
                        $id = $_SESSION['profile']['id'];
 
                if($id <= 0)
                        $id = $_SESSION['profile']['id'];
 
-               $query = "select sum(`points`) as `points` from `notary` where `to`='$id' group by `to`";
+               $query = "select sum(`points`) as `points` from `notary` where `to`='$id' and `deleted` = 0 group by `to`";
                $row = mysql_fetch_assoc(mysql_query($query));
                $points = $row['points'];
 
                $dob = date("Y-m-d", mktime(0,0,0,date("m"),date("d"),date("Y")-18));
                $row = mysql_fetch_assoc(mysql_query($query));
                $points = $row['points'];
 
                $dob = date("Y-m-d", mktime(0,0,0,date("m"),date("d"),date("Y")-18));
-               $query = "select * from `users` where `id`='".$_SESSION['profile']['id']."' and `dob` < '$dob'";
+               $query = "select * from `users` where `id`='".intval($_SESSION['profile']['id'])."' and `dob` < '$dob'";
                if(mysql_num_rows(mysql_query($query)) < 1)
                {
                        if($points >= 100)
                if(mysql_num_rows(mysql_query($query)) < 1)
                {
                        if($points >= 100)
                return(0);
        }
 
                return(0);
        }
 
-       function hex2bin($data)
+       function gpg_hex2bin($data)
        {
                while(strstr($data, "\\x"))
                {
        {
                while(strstr($data, "\\x"))
                {
                $fp = fopen($tmpfname, "w");
                fputs($fp, $message);
                fclose($fp);
                $fp = fopen($tmpfname, "w");
                fputs($fp, $message);
                fclose($fp);
-               $do = `/usr/bin/gpg --homedir /home/gpg --clearsign "$tmpfname"|/usr/sbin/sendmail "$to"`;
+               $to_esc = escapeshellarg($to);
+               $do = shell_exec("/usr/bin/gpg --homedir /home/gpg --clearsign \"$tmpfname\"|/usr/sbin/sendmail ".$to_esc);
                @unlink($tmpfname);
        }
 
                @unlink($tmpfname);
        }
 
                $myemail = mysql_real_escape_string($email);
                if(preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\+\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/" , $email))
                {
                $myemail = mysql_real_escape_string($email);
                if(preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\+\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/" , $email))
                {
-                       list($username,$domain)=split('@',$email);
-                       $dom = escapeshellarg($domain);
-                       $line = trim(`dig +short MX $dom 2>&1`);
-#echo $email."-$dom-$line-\n";
-#echo `dig +short mx heise.de 2>&1`."-<br>\n";
-
-                       $list = explode("\n", $line);
-                       foreach($list as $row)
-                               list($pri, $mxhosts[]) = explode(" ", substr(trim($row), 0, -1));
-                       $mxhosts[] = $domain;
-#print_r($mxhosts); die;
+                       list($username,$domain)=explode('@',$email,2);
+                       $mxhosts = array();
+                       $mxweight = array();
+                       if( !getmxrr($domain, $mxhosts, $mxweight) ) {
+                               $mxhostrr = array($domain);
+                               $mxweight = array(0);
+                       } else if ( empty($mxhosts) ) {
+                               $mxhostrr = array($domain);
+                               $mxweight = array(0);
+                       }
+
+                       $mxhostprio = array();
+                       for($i = 0; $i < count($mxhostrr); $i++) {
+                               $mx_host = trim($mxhostrr[$i], '.');
+                               $mx_prio = $mxweight[$i];
+                               if(empty($mxhostprio[$mx_prio])) {
+                                       $mxhostprio[$mx_prio] = arraY();
+                               }
+                               $mxhostprio[$mx_prio][] = $mx_host;
+                       }
+
+                       array_walk($mxhostprio, function(&$mx) { shuffle($mx); } );
+
+                       $mxhosts = array();
+                       foreach($mxhostprio as $mx_prio => $mxhostnames) {
+                               foreach($mxhostnames as $mx_host) {
+                                       $mxhosts[] = $mx_host;
+                               }
+                       }
+
                        foreach($mxhosts as $key => $domain)
                        {
                                $fp = @fsockopen($domain,25,$errno,$errstr,5);
                                if($fp)
                                {
                        foreach($mxhosts as $key => $domain)
                        {
                                $fp = @fsockopen($domain,25,$errno,$errstr,5);
                                if($fp)
                                {
-                               
+
                                        $line = fgets($fp, 4096);
                                         while(substr($line, 0, 4) == "220-")
                                                $line = fgets($fp, 4096);
                                        $line = fgets($fp, 4096);
                                         while(substr($line, 0, 4) == "220-")
                                                $line = fgets($fp, 4096);
 
                                        $line = mysql_real_escape_string(trim(strip_tags($line)));
                                        $query = "insert into `pinglog` set `when`=NOW(), `email`='$myemail', `result`='$line'";
 
                                        $line = mysql_real_escape_string(trim(strip_tags($line)));
                                        $query = "insert into `pinglog` set `when`=NOW(), `email`='$myemail', `result`='$line'";
-                                       if(is_array($_SESSION['profile'])) $query.=", `uid`='".$_SESSION['profile']['id']."'";
+                                       if(is_array($_SESSION['profile'])) $query.=", `uid`='".intval($_SESSION['profile']['id'])."'";
                                        mysql_query($query);
 
                                        if(substr($line, 0, 3) != "250")
                                        mysql_query($query);
 
                                        if(substr($line, 0, 3) != "250")
                                }
                        }
                }
                                }
                        }
                }
-               $query = "insert into `pinglog` set `when`=NOW(), `uid`='".$_SESSION['profile']['id']."',
+               $query = "insert into `pinglog` set `when`=NOW(), `uid`='".intval($_SESSION['profile']['id'])."',
                                `email`='$myemail', `result`='Failed to make a connection to the mail server'";
                mysql_query($query);
                return _("Failed to make a connection to the mail server");
                                `email`='$myemail', `result`='Failed to make a connection to the mail server'";
                mysql_query($query);
                return _("Failed to make a connection to the mail server");
                return $ticket;
        }
 
                return $ticket;
        }
 
-       function sanitizeHTML($input) 
+       function sanitizeHTML($input)
        {
        {
-               return htmlentities(strip_tags($input), ENT_QUOTES);
+               return htmlentities(strip_tags($input), ENT_QUOTES, 'ISO-8859-1');
                //In case of problems, please use the following line again:
                //return htmlentities(strip_tags(utf8_decode($input)), ENT_QUOTES);
                //return htmlspecialchars(strip_tags($input));
                //In case of problems, please use the following line again:
                //return htmlentities(strip_tags(utf8_decode($input)), ENT_QUOTES);
                //return htmlspecialchars(strip_tags($input));
                $text=preg_replace("/[^\w-.@]/","",$text);
                return($text);
        }
                $text=preg_replace("/[^\w-.@]/","",$text);
                return($text);
        }
-       
+
 
        // returns text message to be shown to the user given the result of is_no_assurer
        function no_assurer_text($Status)
 
        // returns text message to be shown to the user given the result of is_no_assurer
        function no_assurer_text($Status)
                        $name="../$type/$kind/".intval($id/1000)."/$kind-".intval($id).".$type";
                        if (!is_dir("../csr")) { mkdir("../csr",0777); }
                        if (!is_dir("../crt")) { mkdir("../crt",0777); }
                        $name="../$type/$kind/".intval($id/1000)."/$kind-".intval($id).".$type";
                        if (!is_dir("../csr")) { mkdir("../csr",0777); }
                        if (!is_dir("../crt")) { mkdir("../crt",0777); }
-                       
+
                        if (!is_dir("../csr/$kind")) { mkdir("../csr/$kind",0777); }
                        if (!is_dir("../crt/$kind")) { mkdir("../crt/$kind",0777); }
                        if (!is_dir("../csr/$kind/".intval($id/1000))) { mkdir("../csr/$kind/".intval($id/1000)); }
                        if (!is_dir("../csr/$kind")) { mkdir("../csr/$kind",0777); }
                        if (!is_dir("../crt/$kind")) { mkdir("../crt/$kind",0777); }
                        if (!is_dir("../csr/$kind/".intval($id/1000))) { mkdir("../csr/$kind/".intval($id/1000)); }
                return $res;
        }
 
                return $res;
        }
 
+
 ?>
 ?>