Commit | Line | Data |
---|---|---|
3f6bc75f FB |
1 | <?php |
2 | ||
f4737a5a FB |
3 | if (!defined('DC_BLOG_ID')) { |
4 | define('DC_BLOG_ID', $_SERVER['DC_BLOG_ID']); | |
5 | } | |
6 | ||
3f6bc75f | 7 | class xorgAuth extends dcAuth { |
1759942c FB |
8 | public $xorg_infos = array('forlife' => null, |
9 | 'prenom' => null, | |
f525ba66 FB |
10 | 'nom' => null, |
11 | 'grpauth' => null, | |
12 | 'perms' => null); | |
13 | static public function behavior_coreBlogConstruct(&$blog) { | |
14 | global $core; | |
15 | $core->auth->sudo(array($core->auth, 'updateUserPerms'), $blog); | |
16 | } | |
17 | ||
e105d162 FB |
18 | public function __construct(&$core) { |
19 | parent::__construct($core); | |
f525ba66 | 20 | $core->addBehavior('coreBlogConstruct', array('xorgAuth', 'behavior_coreBlogConstruct')); |
e105d162 FB |
21 | } |
22 | ||
f33c2acf | 23 | public function buildFromSession() { |
e105d162 | 24 | global $core; |
001b27af | 25 | @header('Last-Modified:'); |
e105d162 FB |
26 | if (!isset($core) || !isset($core->session)) { |
27 | return; | |
28 | } | |
5a64dfac FB |
29 | if (!session_id()) { |
30 | $core->session->start(); | |
31 | } | |
43e25c76 | 32 | $_SESSION['sess_blog_id'] = $_SERVER['DC_BLOG_ID']; |
001b27af FB |
33 | $user = @$_SESSION['auth-xorg']; |
34 | if ($user && is_null($this->xorg_infos['forlife'])) { | |
1759942c FB |
35 | foreach ($this->xorg_infos as $key => $val) { |
36 | $this->xorg_infos[$key] = $_SESSION['auth-xorg-' . $key]; | |
37 | } | |
001b27af | 38 | $this->user_id = $user; |
e3770a7c | 39 | $this->user_admin = ($_SESSION['auth-xorg-perms'] == 'admin'); |
16237aee | 40 | parent::checkUser($this->user_id); |
f4aeade7 | 41 | $core->getUserBlogs(); |
1759942c FB |
42 | } |
43 | } | |
44 | ||
f33c2acf FB |
45 | public function createUser() { |
46 | global $core; | |
47 | if (!$core->userExists($_SESSION['auth-xorg'])) { | |
48 | $cur = new cursor($this->con, 'dc_user'); | |
49 | $cur->user_id = $_SESSION['auth-xorg']; | |
50 | $cur->user_pwd = md5(rand()); | |
85cd8fee | 51 | $cur->user_super = ($_SESSION['auth-xorg-perms'] == 'admin') ? '1' : '0'; |
f33c2acf FB |
52 | $cur->user_lang = 'fr'; |
53 | $cur->user_name = $_SESSION['auth-xorg-nom']; | |
54 | $cur->user_firstname = $_SESSION['auth-xorg-prenom']; | |
55 | $cur->user_displayname = $cur->user_firstname . ' ' . $cur->user_name; | |
56 | $cur->user_email = $_SESSION['auth-xorg'] . '@polytechnique.org'; | |
e8da64fd FB |
57 | $defaults = $core->userDefaults(); |
58 | $defaults['post_xorg_perms'] = 'public'; | |
59 | $cur->user_options = $defaults; | |
7845399c FB |
60 | $cur->user_lang = 'fr'; |
61 | $cur->user_tz = 'Europe/Paris'; | |
5322432c | 62 | $cur->user_default_blog = $_SERVER['DC_BLOG_ID']; |
f33c2acf FB |
63 | $core->addUser($cur); |
64 | } | |
65 | } | |
66 | ||
f525ba66 | 67 | public function updateUserPerms(&$blog) { |
f33c2acf | 68 | global $core; |
f525ba66 FB |
69 | $this->buildFromSession(); |
70 | if (!isset($_SESSION['auth-xorg'])) { | |
71 | return; | |
72 | } | |
73 | $type = $blog->settings->get('xorg_blog_type'); | |
74 | $owner = $blog->settings->get('xorg_blog_owner'); | |
75 | $level = $this->xorg_infos['grpauth']; | |
9b56dd4e | 76 | $rec = $core->getUser($this->user_id); |
e3770a7c FB |
77 | $wasAdmin = $rec->f('user_super'); |
78 | $isAdmin = $this->xorg_infos['perms'] == 'admin'; | |
79 | if (($wasAdmin && !$isAdmin) || (!$wasAdmin && $isAdmin)) { | |
80 | $cur = new cursor($this->con, 'dc_user'); | |
81 | $cur->user_super = $isAdmin ? '1' : '0'; | |
9b56dd4e | 82 | $core->updUser($this->user_id, $cur); |
e3770a7c | 83 | } |
ee29f18d FB |
84 | if ($_SESSION['xorg-group'] != $owner) { |
85 | $this->killSession(); | |
86 | return; | |
87 | } | |
f525ba66 | 88 | if (($type == 'group-admin' || $type == 'group-member') && $level == 'admin') { |
f525ba66 FB |
89 | $perms = array('usage' => true, |
90 | 'contentadmin' => true, | |
91 | 'admin' => true); | |
92 | } else if ($type == 'group-member' && $level == 'membre') { | |
f525ba66 FB |
93 | $perms = array('usage' => true); |
94 | } else if ($type == 'user' && $owner == $this->xorg_infos['forlife']) { | |
95 | $perms = array('usage' => true, | |
96 | 'contentadmin' => true, | |
97 | 'admin' => true); | |
070afbd1 | 98 | } else if ($type != 'user') { |
f525ba66 | 99 | $perms = array(); |
070afbd1 FB |
100 | } else { |
101 | return; | |
f525ba66 | 102 | } |
f33c2acf | 103 | $core->setUserBlogPermissions($_SESSION['auth-xorg'], |
f525ba66 FB |
104 | $blog->id, |
105 | $perms); | |
f33c2acf FB |
106 | } |
107 | ||
108 | ||
109 | /** Xorg SSO API */ | |
110 | ||
e105d162 | 111 | public function callXorg($path = null) { |
e105d162 | 112 | $this->buildFromSession(); |
be74d9bd | 113 | if (@$_SESSION['auth-xorg']) { |
e105d162 | 114 | return true; |
be74d9bd | 115 | } |
9d447124 | 116 | global $core; |
5a64dfac FB |
117 | if (!session_id()) { |
118 | $core->session->start(); | |
001b27af | 119 | } |
f4737a5a FB |
120 | if (is_null($path)) { |
121 | $path = @$_SERVER['PATH_INFO']; | |
122 | } | |
be74d9bd | 123 | $_SESSION["auth-x-challenge"] = md5(uniqid(rand(), 1)); |
ee29f18d | 124 | $_SESSION['xorg-group'] = $core->blog->settings->get('xorg_blog_owner'); |
be74d9bd FB |
125 | $url = "https://www.polytechnique.org/auth-groupex/utf8"; |
126 | $url .= "?session=" . session_id(); | |
127 | $url .= "&challenge=" . $_SESSION["auth-x-challenge"]; | |
128 | $url .= "&pass=" . md5($_SESSION["auth-x-challenge"] . XORG_AUTH_KEY); | |
f525ba66 FB |
129 | $type = $core->blog->settings->get('xorg_blog_type'); |
130 | if ($type == 'group-member' || $type == 'group-admin') { | |
f525ba66 | 131 | $url .= '&group=' . $core->blog->settings->get('xorg_blog_owner'); |
f525ba66 | 132 | } |
9d447124 | 133 | $url .= "&url=" . urlencode($core->blog->url . "auth/XorgReturn?path=" . $path); |
be74d9bd FB |
134 | session_write_close(); |
135 | header("Location: $url"); | |
136 | exit; | |
137 | } | |
138 | ||
139 | public function returnXorg() { | |
140 | if (!isset($_GET['auth'])) { | |
141 | return false; | |
142 | } | |
143 | $params = ''; | |
e105d162 | 144 | global $core; |
001b27af FB |
145 | $_COOKIE[DC_SESSION_NAME] = $_GET['PHPSESSID']; |
146 | unset($_GET['PHPSESSID']); | |
fc802c03 | 147 | if (!session_id()) { |
5a64dfac FB |
148 | $core->session->start(); |
149 | } | |
be74d9bd FB |
150 | foreach($this->xorg_infos as $key => $val) { |
151 | if(!isset($_GET[$key])) { | |
152 | return false; | |
153 | } | |
154 | $_SESSION['auth-xorg-' . $key] = $_GET[$key]; | |
be74d9bd FB |
155 | $params .= $_GET[$key]; |
156 | } | |
157 | if (md5('1' . $_SESSION['auth-x-challenge'] . XORG_AUTH_KEY . $params . '1') == $_GET['auth']) { | |
158 | unset($_GET['auth']); | |
a1a69528 FB |
159 | $_SESSION['sess_user_id'] = $_SESSION['auth-xorg'] = $_GET['forlife']; |
160 | $_SESSION['sess_browser_uid'] = http::browserUID(DC_MASTER_KEY); | |
161 | $_SESSION['sess_blog_id'] = 'default'; | |
f33c2acf | 162 | $this->sudo(array($this, 'createUser')); |
001b27af | 163 | $path = $_GET['path']; |
f4737a5a | 164 | header('Location: ' . $core->blog->url . $_GET['path']); |
e105d162 | 165 | exit; |
be74d9bd | 166 | } |
a1a69528 FB |
167 | unset($_SESSION['auth-xorg']); |
168 | unset($_SESSION['sess_user_id']); | |
be74d9bd | 169 | unset($_GET['auth']); |
e105d162 | 170 | echo "Failed !!!"; |
be74d9bd FB |
171 | return false; |
172 | } | |
173 | ||
174 | public function killSession() { | |
e105d162 | 175 | global $core; |
5a64dfac FB |
176 | if (!session_id()) { |
177 | $core->session->start(); | |
178 | } | |
e105d162 | 179 | $core->session->destroy(); |
ee29f18d FB |
180 | if (!isset($core->blog)) { |
181 | $blog = $core->getBlog(DC_BLOG_ID); | |
182 | } else { | |
183 | $blog = $core->blog; | |
184 | } | |
185 | $url = @$blog->url; | |
186 | if (!$url) { | |
187 | $url = $blog->f('blog_url'); | |
188 | } | |
189 | ||
190 | header('Location: ' . $url); | |
be74d9bd FB |
191 | exit; |
192 | } | |
a1a69528 | 193 | |
f33c2acf FB |
194 | |
195 | /** Dotclear dcAuth API */ | |
196 | ||
ccfabbd3 | 197 | public function checkUser($user_id, $pwd = null, $user_key = null) { |
16237aee | 198 | return $this->callXorg(); |
ccfabbd3 FB |
199 | } |
200 | ||
201 | public function check($permissions, $blog_id) { | |
202 | $this->buildFromSession(); | |
e3770a7c | 203 | return parent::check($permissions, $blog_id); |
ccfabbd3 FB |
204 | } |
205 | ||
0ff09dcb FB |
206 | public function checkPassword($pwd) { |
207 | $this->buildFromSession(); | |
208 | return !empty($this->user_id); | |
209 | } | |
210 | ||
a1a69528 FB |
211 | public function allowPassChange() { |
212 | return false; | |
213 | } | |
214 | ||
215 | public function userID() { | |
216 | $this->buildFromSession(); | |
a216c10a | 217 | $isadmin = preg_match('@/admin/[^/]+\.php$@i', $_SERVER['SCRIPT_FILENAME']); |
59c9a522 | 218 | if (!$isadmin) { |
85cd8fee FB |
219 | return null; |
220 | } | |
ccfabbd3 | 221 | return parent::userID(); |
a1a69528 FB |
222 | } |
223 | ||
224 | public function getPermissions() { | |
ccfabbd3 FB |
225 | $this->buildFromSession(); |
226 | return parent::getPermissions(); | |
a1a69528 FB |
227 | } |
228 | ||
229 | public function getInfo($n) { | |
ccfabbd3 | 230 | $this->buildFromSession(); |
dc51645d FB |
231 | if ($n == 'xorg_group_member') { |
232 | global $core; | |
233 | if ($core->blog->settings('xorg_blog_owner') != $_SESSION['xorg-group']) { | |
234 | return false; | |
235 | } | |
236 | $perm = $this->xorg_infos['grpauth']; | |
237 | return $this->isSuperAdmin() || $perm == 'admin' || $perm == 'membre'; | |
238 | } | |
ccfabbd3 | 239 | return parent::getInfo($n); |
abb4dd42 FB |
240 | } |
241 | ||
242 | public function getOption($n) { | |
ccfabbd3 FB |
243 | $this->buildFromSession(); |
244 | return parent::getOption($n); | |
a1a69528 | 245 | } |
7d26c37d | 246 | |
abb4dd42 | 247 | public function getOptions() { |
ccfabbd3 FB |
248 | $this->buildFromSession(); |
249 | return parent::getOptions(); | |
a1a69528 | 250 | } |
001b27af FB |
251 | |
252 | public function authForm() { | |
253 | global $core; | |
f4737a5a FB |
254 | if (!isset($core->blog)) { |
255 | $blog = @$core->getBlog(DC_BLOG_ID); | |
256 | } else { | |
257 | $blog = $core->blog; | |
258 | } | |
259 | $path = @$blog->url; | |
260 | if (!$path) { | |
261 | $path = $blog->f('blog_url'); | |
262 | } | |
263 | ||
001b27af | 264 | return '<fieldset>'. |
f4737a5a | 265 | '<p><a href="' . $path . 'auth/Xorg?path=/admin/index.php">Via Polytechnique.org</a></p>' . |
001b27af FB |
266 | '</fieldset>'. |
267 | '<p>'.__('You must accept cookies in order to use the private area.').'</p>'; | |
268 | } | |
3f6bc75f FB |
269 | } |
270 | ||
271 | ?> |