2 LibreSSL - CAcert web application
3 Copyright (C) 2004-2008 CAcert Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; version 2 of the License.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 session_name("cacert");
21 session_register("_config");
22 session_register("profile");
23 session_register("signup");
24 session_register("lostpw");
25 // if($_SESSION['profile']['id'] > 0)
26 // session_regenerate_id();
28 $junk = array(_("Face to Face Meeting"), _("Trusted Third Parties"), _("Thawte Points Transfer"), _("Administrative Increase"),
29 _("CT Magazine - Germany"), _("Temporary Increase"), _("Unknown"));
31 $_SESSION['_config']['errmsg']="";
33 $id = 0; if(array_key_exists("id",$_REQUEST)) $id=intval($_REQUEST['id']);
34 $oldid = 0; if(array_key_exists("oldid",$_REQUEST)) $oldid=intval($_REQUEST['oldid']);
36 $_SESSION['_config']['filepath'] = "/www";
38 require_once($_SESSION['_config']['filepath']."/includes/mysql.php");
40 if(array_key_exists('HTTP_HOST',$_SERVER) &&
41 $_SERVER['HTTP_HOST'] != $_SESSION['_config']['normalhostname'] &&
42 $_SERVER['HTTP_HOST'] != $_SESSION['_config']['securehostname'] &&
43 $_SERVER['HTTP_HOST'] != $_SESSION['_config']['tverify'] &&
44 $_SERVER['HTTP_HOST'] != "stamp.cacert.org")
46 if(array_key_exists('HTTPS',$_SERVER) && $_SERVER['HTTPS'] == "on")
47 header("location: https://".$_SESSION['_config']['normalhostname']);
49 header("location: http://".$_SESSION['_config']['normalhostname']);
53 if(array_key_exists('HTTP_HOST',$_SERVER) &&
54 ($_SERVER['HTTP_HOST'] == $_SESSION['_config']['securehostname'] ||
55 $_SERVER['HTTP_HOST'] == $_SESSION['_config']['tverify']))
57 if(array_key_exists('HTTPS',$_SERVER) && $_SERVER['HTTPS'] == "on")
62 if($_SERVER['HTTP_HOST'] == $_SESSION['_config']['securehostname'])
63 header("location: https://". $_SESSION['_config']['securehostname']);
64 if($_SERVER['HTTP_HOST'] == $_SESSION['_config']['tverify'])
65 header("location: https://".$_SESSION['_config']['tverify']);
71 if(array_key_exists("lang",$_REQUEST))
72 $lang=mysql_escape_string(substr(trim($_REQUEST['lang']), 0, 5));
74 $_SESSION['_config']['language'] = $lang;
76 //if($_SESSION['profile']['id'] == 1 && 1 == 2)
77 // echo $_SESSION['_config']['language'];
79 $_SESSION['_config']['translations'] = array(
80 "ar_JO" => "العربية",
81 "bg_BG" => "Български",
82 "cs_CZ" => "Čeština",
85 "el_GR" => "Ελληνικά",
87 "eo_EO" => "Esperanto",
88 "es_ES" => "Español",
91 "fr_FR" => "Français",
92 "he_IL" => "עברית",
93 "hr_HR" => "Hrvatski",
95 "is_IS" => "Íslenska",
96 "it_IT" => "Italiano",
97 "ja_JP" => "日本語",
98 "ka_GE" => "Georgian",
99 "nl_NL" => "Nederlands",
101 "pt_PT" => "Português",
102 "pt_BR" => "Português Brasileiro",
103 "ru_RU" => "Русский",
104 "ro_RO" => "Română",
105 "sv_SE" => "Svenska",
106 "tr_TR" => "Türkçe",
107 "zh_CN" => "中文(简体)");
111 if(!(array_key_exists('language',$_SESSION['_config']) && $_SESSION['_config']['language'] != ""))
113 $bits = explode(",", strtolower(str_replace(" ", "", mysql_real_escape_string(array_key_exists('HTTP_ACCEPT_LANGUAGE',$_SERVER)?
$_SERVER['HTTP_ACCEPT_LANGUAGE']:""))));
114 foreach($bits as $lang)
116 $b = explode(";", $lang);
117 if(count($b)>1 && substr($b[1], 0, 2) == "q=")
118 $c = floatval(substr($b[1], 2));
121 $value["$c"] = trim($b[0]);
128 foreach($value as $key => $val)
130 $val = substr(escapeshellarg($val), 1, -1);
131 $short = substr($val, 0, 2);
132 if($val == "en" ||
$short == "en")
134 $_SESSION['_config']['language'] = "en";
137 if(file_exists($_SESSION['_config']['filepath']."/locale/$val/LC_MESSAGES/messages.mo"))
139 $_SESSION['_config']['language'] = $val;
142 if(file_exists($_SESSION['_config']['filepath']."/locale/$short/LC_MESSAGES/messages.mo"))
144 $_SESSION['_config']['language'] = $short;
149 if(!array_key_exists('_config',$_SESSION) ||
!array_key_exists('language',$_SESSION['_config']) ||
strlen($_SESSION['_config']['language']) != 5)
151 $lang = array_key_exists('language',$_SESSION['_config'])?
$_SESSION['_config']['language']:"";
152 $_SESSION['_config']['language'] = "en_AU";
153 foreach($_SESSION['_config']['translations'] as $key => $val)
155 if(substr($lang, 0, 2) == substr($key, 0, 2))
157 $_SESSION['_config']['language'] = $val;
163 $_SESSION['_config']['recode'] = "html..latin-1";
164 if($_SESSION['_config']['language'] == "zh_CN")
166 $_SESSION['_config']['recode'] = "html..gb2312";
167 } else if($_SESSION['_config']['language'] == "pl_PL" ||
$_SESSION['_config']['language'] == "hu_HU") {
168 $_SESSION['_config']['recode'] = "html..ISO-8859-2";
169 } else if($_SESSION['_config']['language'] == "ja_JP") {
170 $_SESSION['_config']['recode'] = "html..SHIFT-JIS";
171 } else if($_SESSION['_config']['language'] == "ru_RU") {
172 $_SESSION['_config']['recode'] = "html..ISO-8859-5";
173 } else if($_SESSION['_config']['language'] == "lt_LT") {
174 $_SESSION['_config']['recode'] = "html..ISO-8859-13";
177 putenv("LANG=".$_SESSION['_config']['language']);
178 setlocale(LC_ALL
, $_SESSION['_config']['language']);
179 $domain = 'messages';
180 bindtextdomain($domain, $_SESSION['_config']['filepath']."/locale");
183 //if($_SESSION['profile']['id'] == -1)
184 // echo $_SESSION['_config']['language']." - ".$_SESSION['_config']['filepath']."/locale";
187 if(array_key_exists('profile',$_SESSION) && is_array($_SESSION['profile']) && array_key_exists('id',$_SESSION['profile']) && $_SESSION['profile']['id'] > 0)
189 $locked = mysql_fetch_assoc(mysql_query("select `locked` from `users` where `id`='".$_SESSION['profile']['id']."'"));
190 if($locked['locked'] == 0)
192 $query = "select sum(`points`) as `total` from `notary` where `to`='".$_SESSION['profile']['id']."' group by `to`";
193 $res = mysql_query($query);
194 $row = mysql_fetch_assoc($res);
195 $_SESSION['profile']['points'] = $row['total'];
197 $_SESSION['profile'] = "";
198 unset($_SESSION['profile']);
202 function loadem($section = "index")
204 if($section != "index" && $section != "account" && $section != "tverify")
209 if($section == "account")
210 include_once($_SESSION['_config']['filepath']."/includes/account_stuff.php");
212 if($section == "index")
213 include_once($_SESSION['_config']['filepath']."/includes/general_stuff.php");
215 if($section == "tverify")
216 include_once($_SESSION['_config']['filepath']."/includes/tverify_stuff.php");
219 function includeit($id = "0", $section = "index")
222 if($section != "index" && $section != "account" && $section != "wot" && $section != "help" && $section != "gpg" && $section != "disputes" && $section != "tverify" && $section != "advertising")
227 if($section == "tverify" && file_exists($_SESSION['_config']['filepath']."/tverify/index/$id.php"))
228 include_once($_SESSION['_config']['filepath']."/tverify/index/$id.php");
229 else if(file_exists($_SESSION['_config']['filepath']."/pages/$section/$id.php"))
230 include_once($_SESSION['_config']['filepath']."/pages/$section/$id.php");
234 if(file_exists($_SESSION['_config']['filepath']."/pages/$section/$id.php"))
235 include_once($_SESSION['_config']['filepath']."/pages/$section/$id.php");
241 if(file_exists($_SESSION['_config']['filepath']."/pages/$section/$id.php"))
242 include_once($_SESSION['_config']['filepath']."/pages/$section/$id.php");
244 include_once($_SESSION['_config']['filepath']."/www/error404.php");
249 function checkpw($pwd, $email, $fname, $mname, $lname, $suffix)
253 if(strlen($pwd) > 15)
255 if(strlen($pwd) > 20)
257 if(strlen($pwd) > 25)
259 if(strlen($pwd) > 30)
262 //echo "Points due to length: $points<br/>";
264 if(preg_match("/\d/", $pwd))
267 if(preg_match("/[a-z]/", $pwd))
270 if(preg_match("/[A-Z]/", $pwd))
273 if(preg_match("/\W/", $pwd))
276 if(preg_match("/\s/", $pwd))
279 //echo "Points due to length and charset: $points<br/>";
281 if(@strstr
(strtolower($pwd), strtolower($email)))
284 if(@strstr
(strtolower($email), strtolower($pwd)))
287 if(@strstr
(strtolower($pwd), strtolower($fname)))
290 if(@strstr
(strtolower($fname), strtolower($pwd)))
294 if(@strstr
(strtolower($pwd), strtolower($mname)))
298 if(@strstr
(strtolower($mname), strtolower($pwd)))
301 if(@strstr
(strtolower($pwd), strtolower($lname)))
304 if(@strstr
(strtolower($lname), strtolower($pwd)))
308 if(@strstr
(strtolower($pwd), strtolower($suffix)))
312 if(@strstr
(strtolower($suffix), strtolower($pwd)))
315 //echo "Points due to name matches: $points<br/>";
317 $do = `grep
'$pwd' /usr
/share
/dict
/american
-english`
;
321 //echo "Points due to wordlist: $points<br/>";
328 $bits = explode(": ", $_SESSION['_config']['subject'], 2);
329 $bits = str_replace(", ", "|", str_replace("/", "|", array_key_exists('1',$bits)?
$bits['1']:""));
330 $bits = explode("|", $bits);
332 $_SESSION['_config']['cnc'] = $_SESSION['_config']['subaltc'] = 0;
333 $_SESSION['_config']['OU'] = "";
336 foreach($bits as $val)
338 if(!strstr($val, "="))
341 $split = explode("=", $val);
344 $split['1'] = trim($split['1']);
345 if($k == "CN" && $split['1'])
347 $k = $_SESSION['_config']['cnc'].".".$k;
348 $_SESSION['_config']['cnc']++
;
349 $_SESSION['_config'][$k] = $split['1'];
351 if($k == "OU" && $split['1'] && $_SESSION['_config']['OU'] == "")
353 $_SESSION['_config']['OU'] = $split['1'];
355 if($k == "subjectAltName" && $split['1'])
357 $k = $_SESSION['_config']['subaltc'].".".$k;
358 $_SESSION['_config']['subaltc']++
;
359 $_SESSION['_config'][$k] = $split['1'];
366 unset($_SESSION['_config']['rows']);
367 unset($_SESSION['_config']['rowid']);
368 unset($_SESSION['_config']['rejected']);
371 for($cnc = 0; $cnc < $_SESSION['_config']['cnc']; $cnc++
)
373 $CN = $_SESSION['_config']["$cnc.CN"];
374 $bits = explode(".", $CN);
377 for($i = count($bits) - 1; $i >= 0; $i--)
380 $dom = $bits[$i].".".$dom;
383 $_SESSION['_config']['row'] = "";
384 $dom = mysql_real_escape_string($dom);
385 $query = "select * from domains where `memid`='".$_SESSION['profile']['id']."' and `domain` like '$dom' and `deleted`=0 and `hash`=''";
386 $res = mysql_query($query);
387 if(mysql_num_rows($res) > 0)
390 $_SESSION['_config']['row'] = mysql_fetch_assoc($res);
391 $rowid[] = $_SESSION['_config']['row']['id'];
397 $_SESSION['_config']['rejected'][] = $CN;
399 if($_SESSION['_config']['row'] != "")
402 // if(count($rows) <= 0)
404 // echo _("There were no valid CommonName fields on the CSR, or I was unable to match any of these against your account. Please review your CSR, or add and verify domains contained in it to your account before trying again.");
408 $_SESSION['_config']['rows'] = $rows;
409 $_SESSION['_config']['rowid'] = $rowid;
414 unset($_SESSION['_config']['altrows']);
415 unset($_SESSION['_config']['altid']);
418 for($altc = 0; $altc < $_SESSION['_config']['subaltc']; $altc++
)
420 $subalt = $_SESSION['_config']["$altc.subjectAltName"];
421 if(substr($subalt, 0, 4) == "DNS:")
422 $alt = substr($subalt, 4);
426 $bits = explode(".", $alt);
429 for($i = count($bits) - 1; $i >= 0; $i--)
432 $dom = $bits[$i].".".$dom;
435 $_SESSION['_config']['altrow'] = "";
436 $dom = mysql_real_escape_string($dom);
437 $query = "select * from domains where `memid`='".$_SESSION['profile']['id']."' and `domain` like '$dom' and `deleted`=0 and `hash`=''";
438 $res = mysql_query($query);
439 if(mysql_num_rows($res) > 0)
442 $_SESSION['_config']['altrow'] = mysql_fetch_assoc($res);
443 $altid[] = $_SESSION['_config']['altrow']['id'];
449 $_SESSION['_config']['rejected'][] = $alt;
451 if($_SESSION['_config']['altrow'] != "")
452 $altrows[] = $subalt;
454 $_SESSION['_config']['altrows'] = $altrows;
455 $_SESSION['_config']['altid'] = $altid;
462 for($cnc = 0; $cnc < $_SESSION['_config']['cnc']; $cnc++
)
464 $CN = $_SESSION['_config']["$cnc.CN"];
465 $bits = explode(".", $CN);
467 for($i = count($bits) - 1; $i >= 0; $i--)
470 $dom = $bits[$i].".".$dom;
473 $_SESSION['_config']['row'] = "";
474 $dom = mysql_real_escape_string($dom);
475 $query = "select *, `orginfo`.`id` as `id` from `orginfo`,`orgdomains`,`org` where
476 `org`.`memid`='".$_SESSION['profile']['id']."' and
477 `org`.`orgid`=`orginfo`.`id` and
478 `orgdomains`.`orgid`=`orginfo`.`id` and
479 `orgdomains`.`domain`='$dom'";
480 $res = mysql_query($query);
481 if(mysql_num_rows($res) > 0)
483 $_SESSION['_config']['row'] = mysql_fetch_assoc($res);
484 $rowid[] = $_SESSION['_config']['row']['id'];
489 if($_SESSION['_config']['row'] != "")
492 // if(count($rows) <= 0)
494 // echo _("There were no valid CommonName fields on the CSR, or I was unable to match any of these against your account. Please review your CSR, or add and verify domains contained in it to your account before trying again.");
497 $_SESSION['_config']['rows'] = $rows;
498 $_SESSION['_config']['rowid'] = $rowid;
505 for($altc = 0; $altc < $_SESSION['_config']['subaltc']; $altc++
)
507 $subalt = $_SESSION['_config']["$altc.subjectAltName"];
508 if(substr($subalt, 0, 4) == "DNS:")
509 $alt = substr($subalt, 4);
513 $bits = explode(".", $alt);
515 for($i = count($bits) - 1; $i >= 0; $i--)
518 $dom = $bits[$i].".".$dom;
521 $_SESSION['_config']['altrow'] = "";
522 $dom = mysql_real_escape_string($dom);
523 $query = "select * from `orginfo`,`orgdomains`,`org` where
524 `org`.`memid`='".$_SESSION['profile']['id']."' and
525 `org`.`orgid`=`orginfo`.`id` and
526 `orgdomains`.`orgid`=`orginfo`.`id` and
527 `orgdomains`.`domain`='$dom'";
528 $res = mysql_query($query);
529 if(mysql_num_rows($res) > 0)
531 $_SESSION['_config']['altrow'] = mysql_fetch_assoc($res);
532 $altid[] = $_SESSION['_config']['altrow']['id'];
537 if($_SESSION['_config']['altrow'] != "")
538 $altrows[] = $subalt;
540 $_SESSION['_config']['altrows'] = $altrows;
541 $_SESSION['_config']['altid'] = $altid;
544 function checkownership($hostname)
546 $bits = explode(".", $hostname);
548 for($i = count($bits) - 1; $i >= 0; $i--)
551 $dom = $bits[$i].".".$dom;
554 $dom = mysql_real_escape_string($dom);
555 $query = "select * from `org`,`orgdomains`,`orginfo`
556 where `org`.`memid`='".$_SESSION['profile']['id']."'
557 and `orgdomains`.`orgid`=`org`.`orgid`
558 and `orginfo`.`id`=`org`.`orgid`
559 and `orgdomains`.`domain`='$dom'";
560 $res = mysql_query($query);
561 if(mysql_num_rows($res) > 0)
563 $_SESSION['_config']['row'] = mysql_fetch_assoc($res);
570 function maxpoints($id = 0)
573 $id = $_SESSION['profile']['id'];
575 $query = "select sum(`points`) as `points` from `notary` where `to`='$id' group by `to`";
576 $row = mysql_fetch_assoc(mysql_query($query));
577 $points = $row['points'];
579 $dob = date("Y-m-d", mktime(0,0,0,date("m"),date("d"),date("Y")-18));
580 $query = "select * from `users` where `id`='".$_SESSION['profile']['id']."' and `dob` < '$dob'";
581 if(mysql_num_rows(mysql_query($query)) < 1)
608 function hex2bin($data)
610 while(strstr($data, "\\x"))
612 $pos = strlen($data) - strlen(strstr($data, "\\x"));
613 $before = substr($data, 0, $pos);
614 $char = chr(hexdec(substr($data, $pos +
2, 2)));
615 $after = substr($data, $pos +
4);
616 $data = $before.$char.$after;
618 return(utf8_decode($data));
621 function screenshot($img)
623 if(file_exists("../screenshots/".$_SESSION['_config']['language']."/$img"))
624 return("/screenshots/".$_SESSION['_config']['language']."/$img");
626 return("/screenshots/en/$img");
629 function signmail($to, $subject, $message, $from, $replyto = "")
633 $tmpfname = tempnam("/tmp", "CSR");
634 $fp = fopen($tmpfname, "w");
635 fputs($fp, $message);
637 $do = `
/usr
/bin
/gpg
--homedir
/home
/gpg
--clearsign
"$tmpfname"|
/usr
/sbin
/sendmail
"$to"`
;
641 function checkEmail($email)
643 $myemail = mysql_real_escape_string($email);
644 if(preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\+\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/" , $email))
646 list($username,$domain)=split('@',$email);
647 $dom = escapeshellarg($domain);
648 $line = trim(`dig +short MX
$dom 2>&1`
);
649 #echo $email."-$dom-$line-\n";
650 #echo `dig +short mx heise.de 2>&1`."-<br>\n";
652 $list = explode("\n", $line);
653 foreach($list as $row)
654 list($pri, $mxhosts[]) = explode(" ", substr(trim($row), 0, -1));
655 $mxhosts[] = $domain;
656 #print_r($mxhosts); die;
657 foreach($mxhosts as $key => $domain)
659 $fp = @fsockopen
($domain,25,$errno,$errstr,5);
663 $line = fgets($fp, 4096);
664 if(substr($line, 0, 3) != "220")
666 fputs($fp, "HELO hlin.cacert.org\r\n");
667 $line = fgets($fp, 4096);
668 while(substr($line, 0, 3) == "220")
669 $line = fgets($fp, 4096);
670 if(substr($line, 0, 3) != "250")
672 fputs($fp, "MAIL FROM: <returns@cacert.org>\r\n");
673 $line = fgets($fp, 4096);
675 if(substr($line, 0, 3) != "250")
677 fputs($fp, "RCPT TO: <$email>\r\n");
678 $line = trim(fgets($fp, 4096));
679 fputs($fp, "QUIT\r\n");
682 $line = mysql_real_escape_string(trim(strip_tags($line)));
683 $query = "insert into `pinglog` set `when`=NOW(), `email`='$myemail', `result`='$line'";
684 if(is_array($_SESSION['profile'])) $query.=", `uid`='".$_SESSION['profile']['id']."'";
687 if(substr($line, 0, 3) != "250")
694 $query = "insert into `pinglog` set `when`=NOW(), `uid`='".$_SESSION['profile']['id']."',
695 `email`='$myemail', `result`='Failed to make a connection to the mail server'";
697 return _("Failed to make a connection to the mail server");
700 function waitForResult($table, $certid, $id = 0, $show = 1)
702 $found = $trycount = 0;
705 if($show) showheader(_("My CAcert.org Account!"));
706 echo _("ERROR: The new Certificate ID is wrong. Please contact support.\n");
707 if($show) showfooter();
711 while($trycount++
<= 40)
714 $query = "select * from `$table` where `id`='".intval($certid)."' and `crt` != ''";
716 $query = "select * from `$table` where `id`='".intval($certid)."' and `crt_name` != ''";
717 $res = mysql_query($query);
718 if(mysql_num_rows($res) > 0)
728 if($show) showheader(_("My CAcert.org Account!"));
729 $query = "select * from `$table` where `id`='".intval($certid)."' ";
730 $res = mysql_query($query);
733 if(mysql_num_rows($res) > 0)
735 printf(_("Your certificate request is still queued and hasn't been processed yet. Please wait, and go to Certificates -> View to see it's status."));
736 $subject="[CAcert.org] Certificate TIMEOUT";
737 $body = "A certificate has timed out!\n\n";
741 printf(_("Your certificate request has failed to be processed correctly, see %sthe WIKI page%s for reasons and solutions.")." certid:$table:".intval($certid), "<a href='http://wiki.cacert.org/wiki/FAQ/CertificateRenewal'>", "</a>");
742 $subject="[CAcert.org] Certificate FAILURE";
743 $body = "A certificate has failed: $table $certid $id $show\n\n";
746 $body .= _("Best regards")."\n"._("CAcert.org Support!");
748 sendmail("philipp@cacert.org", $subject, $body, "returns@cacert.org", "", "", "CAcert Support");
750 if($show) showfooter();
757 function generateTicket()
759 $query = "insert into tickets (timestamp) values (now()) ";
761 $ticket = mysql_insert_id();
765 function sanitizeHTML($input)
767 return htmlentities(strip_tags($input), ENT_QUOTES
);
768 //In case of problems, please use the following line again:
769 //return htmlentities(strip_tags(utf8_decode($input)), ENT_QUOTES);
770 //return htmlspecialchars(strip_tags($input));
775 if(function_exists("dio_open"))
777 $rnd = dio_open("/dev/urandom",O_RDONLY
);
778 $hash = md5(dio_read($rnd,64));
781 $rnd = fopen("/dev/urandom", "r");
782 $hash = md5(fgets($rnd, 64));
788 function csrf_check($nam, $show=1)
790 if(!array_key_exists('csrf',$_REQUEST) ||
!array_key_exists('csrf_'.$nam,$_SESSION))
792 showheader(_("My CAcert.org Account!"));
793 echo _("CSRF Hash is missing. Please try again.")."\n";
797 if(strlen($_REQUEST['csrf'])!=32)
799 showheader(_("My CAcert.org Account!"));
800 echo _("CSRF Hash is wrong. Please try again.")."\n";
804 if(!array_key_exists($_REQUEST['csrf'],$_SESSION['csrf_'.$nam]))
806 showheader(_("My CAcert.org Account!"));
807 echo _("CSRF Hash is wrong. Please try again.")."\n";
812 function make_csrf($nam)
815 $_SESSION['csrf_'.$nam][$hash]=1;
819 function clean_csr($CSR)
821 $newcsr = str_replace("\r\n","\n",trim($CSR));
822 $newcsr = str_replace("\n\n","\n",$newcsr);
823 return(preg_replace("/[^A-Za-z0-9\n\r\-\:\=\+\/ ]/","",$newcsr));
826 function sanitizeFilename($text)
828 $text=preg_replace("/[^\w-.@]/","",$text);
832 function fix_assurer_flag($userID)
834 // Update Assurer-Flag on users table if 100 points. Should the number of points be SUM(points) or SUM(awarded)?
835 $query = mysql_query('UPDATE `users` AS `u` SET `assurer` = 1 WHERE `u`.`id` = \''.(int)intval($userID).
836 '\' AND EXISTS(SELECT 1 FROM `cats_passed` AS `tp`, `cats_variant` AS `cv` WHERE `tp`.`variant_id` = `cv`.`id` AND `cv`.`type_id` = 1 AND `tp`.`user_id` = `u`.`id`)'.
837 ' AND (SELECT SUM(`points`) FROM `notary` AS `n` WHERE `n`.`to` = `u`.`id` AND `expire` < now()) >= 100'); // Challenge has been passed and non-expired points >= 100
839 // Reset flag if requirements are not met
840 $query = mysql_query('UPDATE `users` AS `u` SET `assurer` = 0 WHERE `u`.`id` = \''.(int)intval($userID).
841 '\' AND (NOT EXISTS(SELECT 1 FROM `cats_passed` AS `tp`, `cats_variant` AS `cv` WHERE `tp`.`variant_id` = `cv`.`id` AND `cv`.`type_id` = 1 AND `tp`.`user_id` = `u`.`id`)'.
842 ' OR (SELECT SUM(`points`) FROM `notary` AS `n` WHERE `n`.`to` = `u`.`id` AND `n`.`expire` < now()) < 100)');
845 // returns 0 if $userID is an Assurer
847 // Bit 0 is always set
848 // Bit 1 is set if 100 Assurance Points are not reached
849 // Bit 2 is set if Assurer Test is missing
850 // Bit 3 is set if the user is not allowed to be an Assurer (assurer_blocked > 0)
851 function get_assurer_status($userID)
854 $query = mysql_query('SELECT * FROM `cats_passed` AS `tp`, `cats_variant` AS `cv` '.
855 ' WHERE `tp`.`variant_id` = `cv`.`id` AND `cv`.`type_id` = 1 AND `tp`.`user_id` = \''.(int)intval($userID).'\'');
856 if(mysql_num_rows($query) < 1)
861 $query = mysql_query('SELECT SUM(`points`) AS `points` FROM `notary` AS `n` WHERE `n`.`to` = \''.(int)intval($userID).'\' AND `n`.`expire` < now()');
862 $row = mysql_fetch_assoc($query);
863 if ($row['points'] < 100) {
867 $query = mysql_query('SELECT `assurer_blocked` FROM `users` WHERE `id` = \''.(int)intval($userID).'\'');
868 $row = mysql_fetch_assoc($query);
869 if ($row['assurer_blocked'] > 0) {
876 // returns text message to be shown to the user given the result of is_no_assurer
877 function no_assurer_text($Status)
880 $Result = _("You have passed the Assurer Challenge and collected at least 100 Assurance Points, you are an Assurer.");
881 } elseif ($Status == 3) {
882 $Result = _("You have passed the Assurer Challenge, but to become an Assurer you still have to reach 100 Assurance Points!");
883 } elseif ($Status == 5) {
884 $Result = _("You have at least 100 Assurance Points, if you want to become an assurer try the").' <a href="https://cats.cacert.org/"><?=_("Assurer Challenge")?></a>!';
885 } elseif ($Status == 7) {
886 $Result = _("To become an Assurer you have to collect 100 Assurance Points and pass the").' <a href="https://cats.cacert.org/"><?=_("Assurer Challenge")?></a>!';
887 } elseif ($Status & 8 > 0) {
888 $Result = _("Sorry, you are not allowed to be an Assurer. Please contact").' <a href="mailto:cacert-support@lists.cacert.org">cacert-support@lists.cacert.org</a>'._(" if you feel that this is not corect.");
890 $Result = _("You are not an Assurer, but the reason is not stored in the database. Please contact").' <a href="mailto:cacert-support@lists.cacert.org">cacert-support@lists.cacert.org</a>.';
895 function is_assurer($userID)
897 if (get_assurer_status($userID))
903 function get_assurer_reason($userID)
905 return no_assurer_text(get_assurer_status($userID));