f3288766accff14091773c032ab3e5f5face031e
[cacert-devel.git] / scripts / gpgfillmissingemail.php
1 <? /*
2 LibreSSL - CAcert web application
3 Copyright (C) 2004-2008 CAcert Inc.
4
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.
8
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.
13
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
17 */
18 require_once("../includes/mysql.php"); //general.php");
19 //include "../includes/general.php";
20
21 function hex2bin($data)
22 {
23 while(strstr($data, "\\x"))
24 {
25 $pos = strlen($data) - strlen(strstr($data, "\\x"));
26 $before = substr($data, 0, $pos);
27 $char = chr(hexdec(substr($data, $pos + 2, 2)));
28 $after = substr($data, $pos + 4);
29 $data = $before.$char.$after;
30 }
31 return(utf8_decode($data));
32 }
33
34
35 function csvize($str)
36 {
37 if (strpos($str, "\"") != "" || strpos($str, ",") != "") {
38 return "\"" . str_replace("\"", "\"\"", $str) . "\"";
39 }
40 return $str;
41 }
42 mb_regex_encoding("UTF-8");
43
44 echo "Seaching ...\n";
45 $res = mysql_query("SELECT * FROM gpg WHERE crt != '' and email=''");
46 if (!$res) {
47 echo "Query FROM gpg failed!\n";
48 exit;
49 }
50 echo "Found:\n";
51
52 $keys = array();
53 while ($row = mysql_fetch_assoc($res)) {
54 echo "ID: ".$row["id"]."\n";
55 $crt=$row["crt"];
56
57 $gpg = `gpg --with-colons --homedir /tmp $crt 2>/dev/null`;
58 //echo "gpg says\n".htmlspecialchars($gpg);
59 foreach (explode("\n", $gpg) as $line)
60 {
61 $bits = explode(":", $line);
62 if ($bits[0] != "pub" && $bits[0] != "uid") {
63 continue;
64 }
65 if($bits[0] == "pub")
66 {
67
68
69 if (preg_match("/<([\w.-]*\@[\w.-]*)>/", $bits[9],$match))
70 {
71 //echo "Found: ".$match[1];
72 $mail = trim(hex2bin($match[1]));
73
74
75 echo "EMail: *$mail**\n";
76 echo "update gpg set email='$mail' where id=$row[id]\n";
77 mysql_query("update gpg set email='$mail' where id=$row[id];");
78 }
79 }
80 }
81
82
83 }
84 echo "Done\n";
85 mysql_free_result($res);
86
87
88 ?>