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