Use $core->blog->url when possible.
[dotclear.git] / class.xorg.auth.php
1 <?php
2
3 require_once dirname(__FILE__) . '/../../inc/core/class.dc.auth.php';
4
5 class xorgAuth extends dcAuth {
6 private $forceSU = false;
7
8 public $xorg_infos = array('forlife' => null,
9 'prenom' => null,
10 'nom' => null);
11
12 public function __construct(&$core) {
13 parent::__construct($core);
14 }
15
16 private function buildFromSession() {
17 global $core;
18 if (!isset($core) || !isset($core->session)) {
19 return;
20 }
21 $core->session->start();
22 if (@$_SESSION['auth-xorg'] && 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 = $_SESSION['auth-xorg'];
27 parent::checkUser($this->user_id);
28 }
29 }
30
31 public function callXorg($path = null) {
32 if (is_null($path)) {
33 $path = $_SERVER['REQUEST_URI'];
34 }
35 $this->buildFromSession();
36 if (@$_SESSION['auth-xorg']) {
37 return true;
38 }
39 global $core;
40 $_SESSION["auth-x-challenge"] = md5(uniqid(rand(), 1));
41 $url = "https://www.polytechnique.org/auth-groupex/utf8";
42 $url .= "?session=" . session_id();
43 $url .= "&challenge=" . $_SESSION["auth-x-challenge"];
44 $url .= "&pass=" . md5($_SESSION["auth-x-challenge"] . XORG_AUTH_KEY);
45 $url .= "&url=" . urlencode($core->blog->url . "auth/XorgReturn?path=" . $path);
46 session_write_close();
47 header("Location: $url");
48 exit;
49 }
50
51 private function acquireAdminRights() {
52 $this->forceSU = true;
53 }
54
55 private function releaseAdminRights() {
56 $this->forceSU = false;
57 }
58
59 private function createUser() {
60 global $core;
61 $this->acquireAdminRights();
62 if (!$core->userExists($_SESSION['auth-xorg'])) {
63 $cur = new cursor($this->con, 'dc_user');
64 $cur->user_id = $_SESSION['auth-xorg'];
65 $cur->user_pwd = md5(rand());
66 $cur->user_lang = 'fr';
67 $cur->user_name = $_SESSION['auth-xorg-nom'];
68 $cur->user_firstname = $_SESSION['auth-xorg-prenom'];
69 $cur->user_displayname = $cur->user_firstname . ' ' . $cur->user_name;
70 $cur->user_email = $_SESSION['auth-xorg'] . '@polytechnique.org';
71 $cur->user_options = $core->userDefaults();
72 $cur->user_default_blog = 'default'; // FIXME
73 $core->addUser($cur);
74 $core->setUserBlogPermissions($_SESSION['auth-xorg'], 'default', array('usage' => true,
75 'contentadmin' => true,
76 'admin' => true));
77 }
78 $this->releaseAdminRights();
79 }
80
81 public function returnXorg() {
82 if (!isset($_GET['auth'])) {
83 return false;
84 }
85 $params = '';
86 global $core;
87 $core->session->start();
88 foreach($this->xorg_infos as $key => $val) {
89 if(!isset($_GET[$key])) {
90 return false;
91 }
92 $_SESSION['auth-xorg-' . $key] = $_GET[$key];
93 $params .= $_GET[$key];
94 }
95 if (md5('1' . $_SESSION['auth-x-challenge'] . XORG_AUTH_KEY . $params . '1') == $_GET['auth']) {
96 unset($_GET['auth']);
97 $_SESSION['sess_user_id'] = $_SESSION['auth-xorg'] = $_GET['forlife'];
98 $_SESSION['sess_browser_uid'] = http::browserUID(DC_MASTER_KEY);
99 $_SESSION['sess_blog_id'] = 'default';
100 $this->createUser();
101 header("Location: http://murphy.m4x.org" . $_GET['path']);
102 exit;
103 }
104 unset($_SESSION['auth-xorg']);
105 unset($_SESSION['sess_user_id']);
106 unset($_GET['auth']);
107 echo "Failed !!!";
108 return false;
109 }
110
111 public function killSession() {
112 global $core;
113 $core->session->start();
114 $core->session->destroy();
115 header('Location: ' . $core->blog->url);
116 exit;
117 }
118
119 public function checkUser($user_id, $pwd = null, $user_key = null) {
120 return $this->callXorg();
121 }
122
123 public function check($permissions, $blog_id) {
124 $this->buildFromSession();
125 return parent::check($permissions, $blog_id);
126 }
127
128 public function allowPassChange() {
129 return false;
130 }
131
132 public function userID() {
133 $this->buildFromSession();
134 return parent::userID();
135 }
136
137 public function getPermissions() {
138 $this->buildFromSession();
139 return parent::getPermissions();
140 }
141
142 public function getInfo($n) {
143 $this->buildFromSession();
144 return parent::getInfo($n);
145 }
146
147 public function getOption($n) {
148 $this->buildFromSession();
149 return parent::getOption($n);
150 }
151
152 public function isSuperAdmin() {
153 return $this->forceSU || ($this->user_id == 'florent.bruneau.2003');
154 }
155
156 public function getOptions() {
157 $this->buildFromSession();
158 return parent::getOptions();
159 }
160 }
161
162 ?>