Merge branch 'bug-1394' into testserver-stable
[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 update_points_in_profile();
106
107 if($_SESSION['profile']['language'] == "")
108 {
109 $query = "update `users` set `language`='".L10n::get_translation()."'
110 where `id`='".intval($_SESSION['profile']['id'])."'";
111 mysql_query($query);
112 } else {
113 L10n::set_translation($_SESSION['profile']['language']);
114 L10n::init_gettext();
115 }
116 }
117
118 if(array_key_exists("id",$_REQUEST) && $_REQUEST['id'] == "logout")
119 {
120 $normalhost=$_SESSION['_config']['normalhostname'];
121 $_SESSION['profile']['loggedin'] = 0;
122 $_SESSION['profile'] = "";
123 foreach($_SESSION as $key => $value)
124 {
125 unset($_SESSION[$key]);
126 unset($$key);
127 //session_unregister($key);
128 }
129
130 header("Location: https://{$normalhost}/index.php");
131 exit;
132 }
133
134 if($_SESSION['profile']['loggedin'] < 1)
135 {
136 $_SESSION['_config']['oldlocation'] = $_SERVER['REQUEST_URI'];
137 header("Location: https://{$_SERVER['HTTP_HOST']}/index.php?id=4");
138 exit;
139 }
140
141 if (!isset($_SESSION['profile']['ccaagreement']) || !$_SESSION['profile']['ccaagreement']) {
142 $_SESSION['profile']['ccaagreement']=get_user_agreement_status($_SESSION['profile']['id'],'CCA');
143 if (!$_SESSION['profile']['ccaagreement']) {
144 $_SESSION['_config']['oldlocation'] = $_SERVER['REQUEST_URI'];
145 header("Location: https://{$_SERVER['HTTP_HOST']}/index.php?id=52");
146 exit;
147 }
148 }
149 ?>