Add X.org settings to the blog.
[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 $core->getUserBlogs();
32 }
33 }
34
35 public function createUser() {
36 global $core;
37 if (!$core->userExists($_SESSION['auth-xorg'])) {
38 $cur = new cursor($this->con, 'dc_user');
39 $cur->user_id = $_SESSION['auth-xorg'];
40 $cur->user_pwd = md5(rand());
41 $cur->user_lang = 'fr';
42 $cur->user_name = $_SESSION['auth-xorg-nom'];
43 $cur->user_firstname = $_SESSION['auth-xorg-prenom'];
44 $cur->user_displayname = $cur->user_firstname . ' ' . $cur->user_name;
45 $cur->user_email = $_SESSION['auth-xorg'] . '@polytechnique.org';
46 $cur->user_options = $core->userDefaults();
47 $cur->user_options['post_xorg_perms'] = 'public';
48 $cur->user_default_blog = 'default'; // FIXME
49 $core->addUser($cur);
50 }
51 }
52
53 private function updateUserPerms() {
54 global $core;
55 $core->setUserBlogPermissions($_SESSION['auth-xorg'],
56 $core->blog->id,
57 array('usage' => true,
58 'contentadmin' => true,
59 'admin' => true));
60 }
61
62
63 /** Xorg SSO API */
64
65 public function callXorg($path = null) {
66 if (is_null($path)) {
67 $path = $_SERVER['REQUEST_URI'];
68 }
69 $this->buildFromSession();
70 if (@$_SESSION['auth-xorg']) {
71 return true;
72 }
73 global $core;
74 if (!session_id()) {
75 $core->session->start();
76 }
77 $_SESSION["auth-x-challenge"] = md5(uniqid(rand(), 1));
78 $url = "https://www.polytechnique.org/auth-groupex/utf8";
79 $url .= "?session=" . session_id();
80 $url .= "&challenge=" . $_SESSION["auth-x-challenge"];
81 $url .= "&pass=" . md5($_SESSION["auth-x-challenge"] . XORG_AUTH_KEY);
82 $url .= "&url=" . urlencode($core->blog->url . "auth/XorgReturn?path=" . $path);
83 session_write_close();
84 header("Location: $url");
85 exit;
86 }
87
88 public function returnXorg() {
89 if (!isset($_GET['auth'])) {
90 return false;
91 }
92 $params = '';
93 global $core;
94 $_COOKIE[DC_SESSION_NAME] = $_GET['PHPSESSID'];
95 unset($_GET['PHPSESSID']);
96 if (!session_id()) {
97 $core->session->start();
98 }
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->sudo(array($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 if (!session_id()) {
126 $core->session->start();
127 }
128 $core->session->destroy();
129 header('Location: ' . $core->blog->url);
130 exit;
131 }
132
133
134 /** Dotclear dcAuth API */
135
136 public function checkUser($user_id, $pwd = null, $user_key = null) {
137 return $this->callXorg();
138 }
139
140 public function check($permissions, $blog_id) {
141 $this->buildFromSession();
142 return parent::check($permissions, $blog_id);
143 }
144
145 public function checkPassword($pwd) {
146 $this->buildFromSession();
147 return !empty($this->user_id);
148 }
149
150 public function allowPassChange() {
151 return false;
152 }
153
154 public function userID() {
155 $this->buildFromSession();
156 return parent::userID();
157 }
158
159 public function getPermissions() {
160 $this->buildFromSession();
161 return parent::getPermissions();
162 }
163
164 public function getInfo($n) {
165 $this->buildFromSession();
166 return parent::getInfo($n);
167 }
168
169 public function getOption($n) {
170 $this->buildFromSession();
171 return parent::getOption($n);
172 }
173
174 public function isSuperAdmin() {
175 return parent::isSuperAdmin() || ($this->user_id == 'florent.bruneau.2003');
176 }
177
178 public function getOptions() {
179 $this->buildFromSession();
180 return parent::getOptions();
181 }
182
183 public function authForm() {
184 global $core;
185 $path = "http://murphy.m4x.org/~x2003bruneau/dotclear/";
186 return '<fieldset>'.
187 '<p><a href="' . $path . 'auth/Xorg?path=/~x2003bruneau/dotclear/admin/index.php">Via Polytechnique.org</a></p>' .
188 '</fieldset>'.
189 '<p>'.__('You must accept cookies in order to use the private area.').'</p>';
190 }
191 }
192
193 ?>