Merge remote-tracking branch 'origin/bug-1213' into release
[cacert-devel.git] / scripts / gpgfillmissingkeyid.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
20 function csvize($str)
21 {
22 if (strpos($str, "\"") != "" || strpos($str, ",") != "") {
23 return "\"" . str_replace("\"", "\"\"", $str) . "\"";
24 }
25 return $str;
26 }
27 mb_regex_encoding("UTF-8");
28
29 echo "Seaching ...\n";
30 $res = mysql_query("SELECT * FROM gpg WHERE crt != '' and keyid is null");
31 if (!$res) {
32 echo "Query FROM gpg failed!\n";
33 exit;
34 }
35 echo "Found:\n";
36
37 $keys = array();
38 while ($row = mysql_fetch_assoc($res)) {
39 echo "ID: ".$row["id"]."\n";
40 $crt=$row["crt"];
41
42 $gpg = `gpg --with-colons --homedir /tmp $crt 2>/dev/null`;
43 echo "gpg says\n".htmlspecialchars($gpg);
44 foreach (explode("\n", $gpg) as $line)
45 {
46 $bits = explode(":", $line);
47 if ($bits[0] != "pub" && $bits[0] != "uid") {
48 continue;
49 }
50 if($bits[0] == "pub")
51 {
52 echo "KeyID: ".$bits[4]."\n";
53 echo "update gpg set keyid='$bits[4]' where id=$row[id]\n";
54 echo "laenge: ".strlen($bits[4])."\n";
55 if($row[id]>=1 && $row[id]<=100000 && strlen($bits[4])==16)
56 {
57 mysql_query("update gpg set keyid='$bits[4]' where id=$row[id]\n");
58
59 }
60 }
61 $match = false;
62 $problem = "";
63 $uid = " ".preg_replace('~\\\\x([0-9a-f])([0-9a-f])~ei', 'chr(hexdec("\\1\\2"))', $bits[9]);
64 print "UID: $uid\n";
65 }
66
67
68 }
69 echo "Done\n";
70 mysql_free_result($res);
71
72
73 ?>