bug 978: Move things around (common functions moved to a lib file)
[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 /**
51 * Produces a log entry with the error message with log level E_USER_WARN
52 * and a random ID an returns a message that can be displayed to the user
53 * including the generated ID
54 *
55 * @param $errormessage string
56 * The error message that should be logged
57 * @return string containing the generated ID that can be displayed to the
58 * user
59 */
60 function failWithId($errormessage) {
61 $errorId = rand();
62 trigger_error("$errormessage. ID: $errorId", E_USER_WARNING);
63 return sprintf(_("Something went wrong when processing your request. ".
64 "Please contact %s for help and provide them with the ".
65 "following ID: %d"),
66 "<a href='mailto:support@cacert.org?subject=System%20Error%20-%20".
67 "ID%3A%20$errorId'>support@cacert.org</a>",
68 $errorId);
69 }
70