56dd0cfdfc4233369a3aef7adb3195d5c9cf5ef1
[cacert-devel.git] / www / cats / cats_import.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
19 // Comment (to be romeved): better to disable shot open tags in php.ini
20
21 /*
22 cats_import.php
23
24 API for CATS to import passed tests into main CAcert database.
25 */
26
27 require_once('../../includes/lib/account.php');
28
29 function sanitize_string($buffer) {
30 return htmlentities(utf8_decode($buffer), (int)ENQ_QUOTES);
31 }
32
33 define ('UNDEFINED', 'nd');
34 define ('ALLOWED_IP', '213.154.225.228');
35 define ('ALLOWED_IP2', '193.238.157.112');
36 define ('CONFIG_FILEPATH', '/www/');
37
38 $remote_addr = (isset($_SERVER['REMOTE_ADDR']))?$_SERVER['REMOTE_ADDR']:UNDEFINED;
39 $server_name = (isset($_SERVER['SERVER_NAME']))?$_SERVER['SERVER_NAME']:UNDEFINED;
40 $https = (isset($_SERVER['HTTPS']))?$_SERVER['HTTPS']:UNDEFINED;
41 $ssl_client_s_dn = (isset($_SERVER['SSL_CLIENT_S_DN']))?$_SERVER['SSL_CLIENT_S_DN']:UNDEFINED;
42
43 $access = FALSE;
44
45 // Access only from CATS.cacert.org with a client certificate for cats@cacert.org
46 if (
47 ($remote_addr == ALLOWED_IP || $remote_addr == ALLOWED_IP2) &&
48 $https == 'on' &&
49 // Comment (to be romeved): better to use preg_match matching the end of the line (since this is on the end of the line right?)
50 // Ted: Is this specified? I don't think so, therefore I'd keep stristr
51 strlen(stristr($ssl_client_s_dn, '/emailAddress=cats@cacert.org')) > 0
52 ) $access = TRUE;
53
54 if ($access !== TRUE) {
55 echo 'UNAUTHORIZED ACCESS<br>'."\r\n";
56 echo 'IP: '.sanitize_string($remote_addr).'<br>'."\r\n";
57 echo 'Server: '.sanitize_string($server_name).'<br>'."\r\n";
58 echo 'HTTPS: '.sanitize_string($https).'<br>'."\r\n";
59 echo 'Client cert: '.sanitize_string($ssl_client_s_dn).'<br>'."\r\n";
60 trigger_error('Unauthorized access: ip('.$remote_addr.') server('.$server_name.') https('.$https.') cert('.$ssl_client_s_dn.')', E_USER_ERROR);
61 exit();
62 }
63
64 // Comment (to be romeved): do you we session autostart in php.ini??
65 // Ted: Sessions are quite meaningless for me since the upload protocol is stateless. Should session_start be called nevertheless?
66 session_start();
67
68 require_once(CONFIG_FILEPATH.'includes/mysql.php');
69
70 // Comment (to be romeved): dunno the difference between stripslashes and stripcslashes
71 // manual is iunclear too, please make sure there are no decoding issues
72 // Ted: I just used it here because I saw it elsewhere and it seems to work. Would you prefer stripslashes?
73 if (get_magic_quotes_gpc()) {
74 $serial = stripcslashes($_POST['serial']);
75 $root = stripcslashes($_POST['root']);
76 $type = stripcslashes($_POST['type']);
77 $variant = stripcslashes($_POST['variant']);
78 $date = stripcslashes($_POST['date']);
79 } else {
80 $serial = $_POST['serial'];
81 $root = $_POST['root'];
82 $type = $_POST['type'];
83 $variant = $_POST['variant'];
84 $date = $_POST['date'];
85 }
86
87 // Explicitly select all those IDs so I can insert new rows if needed.
88 $query = mysql_query('SELECT `id` FROM `cats_type` WHERE `type_text` = \''.mysql_real_escape_string($type).'\';');
89 if (!$query) {
90 echo 'Invalid query'."\r\n";
91 trigger_error('Invalid query', E_USER_ERROR);
92 exit();
93 }
94
95 if (mysql_num_rows($query) > 0) {
96 $result = mysql_fetch_array($query);
97 $typeID = $result['0'];
98 } else {
99 $query = mysql_query('INSERT INTO `cats_type` (`type_text`) VALUES (\''.mysql_real_escape_string($type).'\');');
100 if (!$query) {
101 echo 'Invalid query'."\r\n";
102 trigger_error('Invalid query', E_USER_ERROR);
103 exit();
104 }
105
106 $typeID = mysql_insert_id();
107 }
108
109 $query = mysql_query('SELECT `id` FROM `cats_variant` WHERE `type_id` = \''.(int)intval($typeID).'\' AND `test_text` = \''.mysql_real_escape_string($variant).'\';');
110 if (!$query) {
111 echo 'Invalid query'."\r\n";
112 trigger_error('Invalid query', E_USER_ERROR);
113 exit();
114 }
115
116 if (mysql_num_rows($query) > 0) {
117 $result = mysql_fetch_array($query);
118 $variantID = $result['0'];
119 } else {
120 $query = mysql_query('INSERT INTO `cats_variant` (`type_id`, `test_text`) VALUES (\''.(int)intval($typeID).'\', \''.mysql_real_escape_string($variant).'\');');
121 if (!$query) {
122 echo 'Invalid query'."\r\n";
123 trigger_error('Invalid query', E_USER_ERROR);
124 exit();
125 }
126
127 $variantID = mysql_insert_id();
128 }
129
130 // Now find the userid from cert serial
131 $query = mysql_query('SELECT `ec`.`memid` FROM `emailcerts` AS `ec`, `root_certs` AS `rc` WHERE `ec`.`rootcert` = `rc`.`id` AND `ec`.`serial` = \''.mysql_real_escape_string($serial).'\' AND `rc`.`cert_text` = \''.mysql_real_escape_string($root).'\';');
132 if (!$query) {
133 echo 'Invalid query'."\r\n";
134 trigger_error('Invalid query', E_USER_ERROR);
135 exit();
136 }
137
138 if (mysql_num_rows($query) > 0) {
139 $result = mysql_fetch_array($query);
140 $userID = $result['0'];
141 } else {
142 echo 'Cannot find cert '.sanitize_string($serial).' / '.sanitize_string($root)."\r\n";
143 // Let's treat this as an error, since it should not happen.
144 trigger_error('Cannot find cert '.$serial.' / '.$root.'!'.mysql_error(), E_USER_ERROR);
145 exit();
146 }
147
148 // The unique constraint on cats_passed assures that records are not stored multiply
149 $query = mysql_query('INSERT INTO `cats_passed` (`user_id`, `variant_id`, `pass_date`) VALUES (\''.(int)intval($userID).'\', \''.(int)intval($variantID).'\', \''.mysql_real_escape_string($date).'\');');
150 if (!$query) {
151 if (mysql_errno() != 1062) { // Duplicate Entry is considered success
152 echo 'Invalid query'."\r\n";
153 trigger_error('Invalid query', E_USER_ERROR);
154 exit();
155 }
156 }
157
158 // Update Assurer-Flag on users table if 100 points. Should the number of points be SUM(points) or SUM(awarded)?
159 if (!fix_assurer_flag($userID)) {
160 echo 'Invalid query'."\r\n";
161 trigger_error('Invalid query', E_USER_ERROR);
162 exit();
163 }
164
165 echo 'OK'."\r\n";
166
167 ?>