Fix issues with dotclear session management and browser cache.
[dotclear.git] / class.xorg.auth.php
1 <?php
2
3 require_once dirname(__FILE__) . '/../../inc/core/class.dc.auth.php';
4
5 class xorgAuth extends dcAuth {
6 private $forceSU = false;
7
8 public $xorg_infos = array('forlife' => null,
9 'prenom' => null,
10 'nom' => null);
11
12 public function __construct(&$core) {
13 parent::__construct($core);
14 }
15
16 private function buildFromSession() {
17 global $core;
18 @header('Last-Modified:');
19 if (!isset($core) || !isset($core->session)) {
20 return;
21 }
22 $core->session->start();
23 $user = @$_SESSION['auth-xorg'];
24 if ($user && is_null($this->xorg_infos['forlife'])) {
25 foreach ($this->xorg_infos as $key => $val) {
26 $this->xorg_infos[$key] = $_SESSION['auth-xorg-' . $key];
27 }
28 $this->user_id = $user;
29 parent::checkUser($this->user_id);
30 }
31 }
32
33 public function callXorg($path = null) {
34 if (is_null($path)) {
35 $path = $_SERVER['REQUEST_URI'];
36 }
37 $this->buildFromSession();
38 if (@$_SESSION['auth-xorg']) {
39 return true;
40 }
41 global $core;
42
43 if (!$this->sessionExists()) {
44 session_write_close();
45 header("Location: " . $core->blog->url . 'auth/Xorg?path=' . $path);
46 exit;
47 }
48
49 $_SESSION["auth-x-challenge"] = md5(uniqid(rand(), 1));
50 $url = "https://www.polytechnique.org/auth-groupex/utf8";
51 $url .= "?session=" . session_id();
52 $url .= "&challenge=" . $_SESSION["auth-x-challenge"];
53 $url .= "&pass=" . md5($_SESSION["auth-x-challenge"] . XORG_AUTH_KEY);
54 $url .= "&url=" . urlencode($core->blog->url . "auth/XorgReturn?path=" . $path);
55 session_write_close();
56 header("Location: $url");
57 exit;
58 }
59
60 private function acquireAdminRights() {
61 $this->forceSU = true;
62 }
63
64 private function releaseAdminRights() {
65 $this->forceSU = false;
66 }
67
68 private function createUser() {
69 global $core;
70 $this->acquireAdminRights();
71 if (!$core->userExists($_SESSION['auth-xorg'])) {
72 $cur = new cursor($this->con, 'dc_user');
73 $cur->user_id = $_SESSION['auth-xorg'];
74 $cur->user_pwd = md5(rand());
75 $cur->user_lang = 'fr';
76 $cur->user_name = $_SESSION['auth-xorg-nom'];
77 $cur->user_firstname = $_SESSION['auth-xorg-prenom'];
78 $cur->user_displayname = $cur->user_firstname . ' ' . $cur->user_name;
79 $cur->user_email = $_SESSION['auth-xorg'] . '@polytechnique.org';
80 $cur->user_options = $core->userDefaults();
81 $cur->user_default_blog = 'default'; // FIXME
82 $core->addUser($cur);
83 $core->setUserBlogPermissions($_SESSION['auth-xorg'], 'default', array('usage' => true,
84 'contentadmin' => true,
85 'admin' => true));
86 }
87 $this->releaseAdminRights();
88 }
89
90 public function returnXorg() {
91 if (!isset($_GET['auth'])) {
92 return false;
93 }
94 $params = '';
95 global $core;
96 $_COOKIE[DC_SESSION_NAME] = $_GET['PHPSESSID'];
97 unset($_GET['PHPSESSID']);
98 $core->session->start();
99 foreach($this->xorg_infos as $key => $val) {
100 if(!isset($_GET[$key])) {
101 return false;
102 }
103 $_SESSION['auth-xorg-' . $key] = $_GET[$key];
104 $params .= $_GET[$key];
105 }
106 if (md5('1' . $_SESSION['auth-x-challenge'] . XORG_AUTH_KEY . $params . '1') == $_GET['auth']) {
107 unset($_GET['auth']);
108 $_SESSION['sess_user_id'] = $_SESSION['auth-xorg'] = $_GET['forlife'];
109 $_SESSION['sess_browser_uid'] = http::browserUID(DC_MASTER_KEY);
110 $_SESSION['sess_blog_id'] = 'default';
111 $this->createUser();
112 $path = $_GET['path'];
113 header("Location: http://murphy.m4x.org" . $_GET['path']);
114 exit;
115 }
116 unset($_SESSION['auth-xorg']);
117 unset($_SESSION['sess_user_id']);
118 unset($_GET['auth']);
119 echo "Failed !!!";
120 return false;
121 }
122
123 public function killSession() {
124 global $core;
125 $core->session->start();
126 $core->session->destroy();
127 header('Location: ' . $core->blog->url);
128 exit;
129 }
130
131 public function checkUser($user_id, $pwd = null, $user_key = null) {
132 return $this->callXorg();
133 }
134
135 public function check($permissions, $blog_id) {
136 $this->buildFromSession();
137 return parent::check($permissions, $blog_id);
138 }
139
140 public function checkPassword($pwd) {
141 $this->buildFromSession();
142 return !empty($this->user_id);
143 }
144
145 public function allowPassChange() {
146 return false;
147 }
148
149 public function userID() {
150 $this->buildFromSession();
151 return parent::userID();
152 }
153
154 public function getPermissions() {
155 $this->buildFromSession();
156 return parent::getPermissions();
157 }
158
159 public function getInfo($n) {
160 $this->buildFromSession();
161 return parent::getInfo($n);
162 }
163
164 public function getOption($n) {
165 $this->buildFromSession();
166 return parent::getOption($n);
167 }
168
169 public function isSuperAdmin() {
170 return $this->forceSU || ($this->user_id == 'florent.bruneau.2003');
171 }
172
173 public function getOptions() {
174 $this->buildFromSession();
175 return parent::getOptions();
176 }
177
178 public function authForm() {
179 global $core;
180 $path = "http://murphy.m4x.org/~x2003bruneau/dotclear/";
181 return '<fieldset>'.
182 '<p><a href="' . $path . 'auth/Xorg?path=/~x2003bruneau/dotclear/admin/index.php">Via Polytechnique.org</a></p>' .
183 '<p><a href="' . $path . 'admin/auth.php">Via le formulaire</a></p>' .
184 '</fieldset>'.
185 '<p>'.__('You must accept cookies in order to use the private area.').'</p>';
186 }
187 }
188
189 ?>