Merge branch 'bug-1459' into release (Emergency bugfix)
[cacert-devel.git] / includes / loggedin.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 include_once("../includes/lib/general.php");
20 require_once("../includes/lib/l10n.php");
21 include_once("../includes/mysql.php");
22 require_once('../includes/notary.inc.php');
23
24 if(!isset($_SESSION['profile']) || !is_array($_SESSION['profile'])) {
25 $_SESSION['profile'] = array( 'id' => 0, 'loggedin' => 0 );
26 }
27 if(!isset($_SESSION['profile']['id']) || !isset($_SESSION['profile']['loggedin'])) {
28 $_SESSION['profile']['id'] = 0;
29 $_SESSION['profile']['loggedin'] = 0;
30 }
31
32 if($_SERVER['HTTP_HOST'] == $_SESSION['_config']['securehostname'] && $_SESSION['profile']['id'] > 0 && $_SESSION['profile']['loggedin'] != 0)
33 {
34 $uid = $_SESSION['profile']['id'];
35 $_SESSION['profile']['loggedin'] = 0;
36 $_SESSION['profile'] = "";
37 foreach($_SESSION as $key => $value)
38 {
39 if($key == '_config' || $key == 'mconn' || 'csrf_' == substr($key, 0, 5))
40 continue;
41 if(is_int($key) || is_string($key))
42 unset($_SESSION[$key]);
43 unset($$key);
44 //session_unregister($key);
45 }
46
47 $_SESSION['profile'] = mysql_fetch_assoc(mysql_query("select * from `users` where `id`='".intval($uid)."'"));
48 if($_SESSION['profile']['locked'] == 0)
49 $_SESSION['profile']['loggedin'] = 1;
50 else
51 unset($_SESSION['profile']);
52 }
53
54 if($_SERVER['HTTP_HOST'] == $_SESSION['_config']['securehostname'] && ($_SESSION['profile']['id'] == 0 || $_SESSION['profile']['loggedin'] == 0))
55 {
56 $user_id = get_user_id_from_cert($_SERVER['SSL_CLIENT_M_SERIAL'],
57 $_SERVER['SSL_CLIENT_I_DN_CN']);
58
59 if($user_id >= 0)
60 {
61 $_SESSION['profile']['loggedin'] = 0;
62 $_SESSION['profile'] = "";
63 foreach($_SESSION as $key => $value)
64 {
65 if($key == '_config' || $key == 'mconn' || 'csrf_' == substr($key, 0, 5))
66 continue;
67 if(is_int($key) || is_string($key))
68 unset($_SESSION[$key]);
69 unset($$key);
70 //session_unregister($key);
71 }
72
73 $_SESSION['profile'] = mysql_fetch_assoc(mysql_query(
74 "select * from `users` where `id`='".intval($user_id)."'"));
75 if($_SESSION['profile']['locked'] == 0)
76 $_SESSION['profile']['loggedin'] = 1;
77 else
78 unset($_SESSION['profile']);
79 } else {
80 $_SESSION['profile']['loggedin'] = 0;
81 $_SESSION['profile'] = "";
82 foreach($_SESSION as $key => $value)
83 {
84 if($key == '_config' || $key == 'mconn' || 'csrf_' == substr($key, 0, 5))
85 continue;
86 unset($_SESSION[$key]);
87 unset($$key);
88 //session_unregister($key);
89 }
90
91 $_SESSION['_config']['oldlocation'] = $_SERVER['REQUEST_URI'];
92 header("Location: https://{$_SESSION['_config']['securehostname']}/index.php?id=4");
93 exit;
94 }
95 }
96
97 if($_SERVER['HTTP_HOST'] == $_SESSION['_config']['securehostname'] && ($_SESSION['profile']['id'] <= 0 || $_SESSION['profile']['loggedin'] == 0))
98 {
99 header("Location: https://{$_SESSION['_config']['normalhostname']}");
100 exit;
101 }
102
103 if($_SERVER['HTTP_HOST'] == $_SESSION['_config']['securehostname'] && $_SESSION['profile']['id'] > 0 && $_SESSION['profile']['loggedin'] > 0)
104 {
105 $query = "select sum(`points`) as `total` from `notary` where `to`='".intval($_SESSION['profile']['id'])."' and `deleted` = 0 group by `to`";
106 $res = mysql_query($query);
107 $row = mysql_fetch_assoc($res);
108 $_SESSION['profile']['points'] = $row['total'];
109
110 if($_SESSION['profile']['language'] == "")
111 {
112 $query = "update `users` set `language`='".L10n::get_translation()."'
113 where `id`='".intval($_SESSION['profile']['id'])."'";
114 mysql_query($query);
115 } else {
116 L10n::set_translation($_SESSION['profile']['language']);
117 L10n::init_gettext();
118 }
119 }
120
121 if(array_key_exists("id",$_REQUEST) && $_REQUEST['id'] == "logout")
122 {
123 $normalhost=$_SESSION['_config']['normalhostname'];
124 $_SESSION['profile']['loggedin'] = 0;
125 $_SESSION['profile'] = "";
126 foreach($_SESSION as $key => $value)
127 {
128 unset($_SESSION[$key]);
129 unset($$key);
130 //session_unregister($key);
131 }
132
133 header("Location: https://{$normalhost}/index.php");
134 exit;
135 }
136
137 if($_SESSION['profile']['loggedin'] < 1)
138 {
139 $_SESSION['_config']['oldlocation'] = $_SERVER['REQUEST_URI'];
140 header("Location: https://{$_SERVER['HTTP_HOST']}/index.php?id=4");
141 exit;
142 }
143
144 if (!isset($_SESSION['profile']['ccaagreement']) || !$_SESSION['profile']['ccaagreement']) {
145 $_SESSION['profile']['ccaagreement']=get_user_agreement_status($_SESSION['profile']['id'],'CCA');
146 if (!$_SESSION['profile']['ccaagreement']) {
147 $_SESSION['_config']['oldlocation'] = $_SERVER['REQUEST_URI'];
148 header("Location: https://{$_SERVER['HTTP_HOST']}/index.php?id=52");
149 exit;
150 }
151 }
152 ?>