25d2561bb2b023948dbe96bfcc308c0e3de9bdcd
[cacert-devel.git] / includes / lib / general.php
1 <? /*
2 LibreSSL - CAcert web application
3 Copyright (C) 2004-2011 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 /**
20 * Checks if the user may log in and retrieve the user id
21 *
22 * Usually called with $_SERVER['SSL_CLIENT_M_SERIAL'] and
23 * $_SERVER['SSL_CLIENT_I_DN_CN']
24 *
25 * @param $serial string
26 * usually $_SERVER['SSL_CLIENT_M_SERIAL']
27 * @param $issuer_cn string
28 * usually $_SERVER['SSL_CLIENT_I_DN_CN']
29 * @return int
30 * the user id, -1 in case of error
31 */
32 function get_user_id_from_cert($serial, $issuer_cn)
33 {
34 $query = "select `memid` from `emailcerts` where
35 `serial`='".mysql_escape_string($serial)."' and
36 `rootcert`= (select `id` from `root_certs` where
37 `Cert_Text`='".mysql_escape_string($issuer_cn)."') and
38 `revoked`=0 and disablelogin=0 and
39 UNIX_TIMESTAMP(`expire`) - UNIX_TIMESTAMP() > 0";
40 $res = mysql_query($query);
41 if(mysql_num_rows($res) > 0)
42 {
43 $row = mysql_fetch_assoc($res);
44 return intval($row['memid']);
45 }
46
47 return -1;
48 }
49
50 ?>