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