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