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