php8
[web.mtrack] / MTrackWeb / OpenId.php
1 <?php # vim:ts=2:sw=2:et:
2 /* For licensing and copyright terms, see the file named LICENSE */
3
4 die("open id code needs migrating.... - do not need it at present...");
5
6
7 require_once 'Auth/OpenID/Consumer.php';
8 require_once 'Auth/OpenID/FileStore.php';
9 require_once 'Auth/OpenID/SReg.php';
10 require_once 'Auth/OpenID/PAPE.php';
11
12 $store_location = MTrackConfig::get('openid', 'store_dir');
13 if (!$store_location) {
14   $store_location = MTrackConfig::get('core', 'vardir') . '/openid';
15 }
16 if (!is_dir($store_location)) {
17   mkdir($store_location);
18 }
19 $store = new Auth_OpenID_FileStore($store_location);
20 $consumer = new Auth_OpenID_Consumer($store);
21
22 $message = null;
23
24 $pi = mtrack_get_pathinfo();
25 if ($_SERVER['REQUEST_METHOD'] == 'POST' && $pi != 'register') {
26
27   $req = null;
28
29   if (!isset($_POST['openid_identifier']) ||
30       !strlen($_POST['openid_identifier'])) {
31     $message = "you must fill in your OpenID";
32   } else {
33     $id = $_POST['openid_identifier'];
34     if (!preg_match('/^https?:\/\//', $id)) {
35       $id = "http://$id";
36     }
37     $req = $consumer->begin($id);
38     if (!$req) {
39       $message = "not a valid OpenID";
40     }
41   }
42   if ($req) {
43     $sreg = Auth_OpenID_SRegRequest::build(
44       array('nickname', 'fullname', 'email')
45     );
46     $req->addExtension($sreg);
47
48     if ($req->shouldSendRedirect()) {
49       $rurl = $req->redirectURL(
50         $ABSWEB, $ABSWEB . 'openid.php/callback');
51       if (Auth_OpenID::isFailure($rurl)) {
52         $message = "Unable to redirect to server: " . $rurl->message;
53       } else {
54         header("Location: $rurl");
55         exit;
56       }
57     } else {
58       $html = $req->htmlMarkup($ABSWEB, $ABSWEB . 'openid.php/callback',
59         false, array('id' => 'openid_message'));
60       if (Auth_OpenID::isFailure($html)) {
61         $message = "Unable to redirect to server: " . $html->message;
62       } else {
63         echo $html;
64       }
65     }
66   }
67 } else if ($pi == 'callback') {
68   $res = $consumer->complete($ABSWEB . 'openid.php/callback');
69
70   if ($res->status == Auth_OpenID_CANCEL) {
71     $message = 'Verification cancelled';
72   } else if ($res->status == Auth_OpenID_FAILURE) {
73     $message = 'OpenID authentication failed: ' . $res->message;
74   } else if ($res->status == Auth_OpenID_SUCCESS) {
75     $id = $res->getDisplayIdentifier();
76     $sreg = Auth_OpenID_SRegResponse::fromSuccessResponse($res)->contents();
77
78     if (!empty($sreg['nickname'])) {
79       $name = $sreg['nickname'];
80     } else if (!empty($sreg['fullname'])) {
81       $name = $sreg['fullname'];
82     } else {
83       $name = $id;
84     }
85     $message = 'Authenticated as ' . $name;
86
87     $_SESSION['openid.id'] = $id;
88     unset($_SESSION['openid.userid']);
89     $_SESSION['openid.name'] = $name;
90     if (!empty($sreg['email'])) {
91       $_SESSION['openid.email'] = $sreg['email'];
92     }
93     /* See if we can find a canonical identity for the user */
94     foreach (MTrackDB::q('select userid from useraliases where alias = ?',
95         $id)->fetchAll() as $row) {
96       $_SESSION['openid.userid'] = $row[0];
97       break;
98     }
99
100     if (!isset($_SESSION['openid.userid'])) {
101       /* no alias; is there a direct userinfo entry? */
102       foreach (MTrackDB::q('select userid from userinfo where userid = ?',
103           $id)->fetchAll() as $row) {
104         $_SERVER['openid.userid'] = $row[0];
105         break;
106       }
107     }
108
109     if (!isset($_SESSION['openid.userid'])) {
110       /* prompt the user to fill out some basic details so that we can create
111        * a local identity and associate their OpenID with it */
112       header("Location: {$ABSWEB}openid.php/register?" .
113         http_build_query($sreg));
114     } else {
115       header("Location: " . $ABSWEB);
116     }
117     exit;
118   } else {
119     $message = 'An error occurred while talking to your OpenID provider';
120   }
121 } else if ($pi == 'signout') {
122   session_destroy();
123   header('Location: ' . $ABSWEB);
124   exit;
125 } else if ($pi == 'register') {
126
127   if (!isset($_SESSION['openid.id'])) {
128     header("Location: " . $ABSWEB);
129     exit;
130   }
131
132   $userid = isset($_REQUEST['nickname']) ? $_REQUEST['nickname'] : '';
133   $email = isset($_REQUEST['email']) ? $_REQUEST['email'] : '';
134   $message = null;
135
136   /* See if we can find a canonical identity for the user */
137   foreach (MTrackDB::q('select userid from useraliases where alias = ?',
138       $_SESSION['openid.id'])->fetchAll() as $row) {
139     header("Location: " . $ABSWEB);
140     exit;
141   }
142
143   if ($_SERVER['REQUEST_METHOD'] == 'POST') {
144     if (!strlen($userid)) {
145       $message = 'You must enter a userid';
146     } else {
147       /* is the requested id available? */
148       $avail = true;
149       foreach (MTrackDB::q('select userid from userinfo where userid = ?',
150             $userid)->fetchAll() as $row) {
151         $avail = false;
152         $message = "Your selected user ID is not available";
153       }
154       if ($avail) {
155         MTrackDB::q('insert into userinfo (userid, email, active) values (?, ?, 1)', $userid, $email);
156         /* we know the alias doesn't already exist, because we double-checked
157          * for it above */
158         MTrackDB::q('insert into useraliases (userid, alias) values (?,?)',
159           $userid, $_SESSION['openid.id']);
160         header("Location: {$ABSWEB}user.php?user=$userid&edit=1");
161         exit;
162       }
163     }
164   }
165
166   mtrack_head('Register');
167
168   $userid = htmlentities($userid, ENT_QUOTES, 'utf-8');
169   $email = htmlentities($email, ENT_QUOTES, 'utf-8');
170
171   if ($message) {
172     $message = htmlentities($message, ENT_QUOTES, 'utf-8');
173     echo <<<HTML
174 <div class='ui-state-error ui-corner-all'>
175     <span class='ui-icon ui-icon-alert'></span>
176     $message
177 </div>
178 HTML;
179   }
180
181   echo <<<HTML
182 <h1>Set up your local account</h1>
183 <form method='post'>
184   User ID: <input type='text' name='nickname' value='$userid'><br>
185   Email: <input type='text' name='email' value='$email'><br>
186   <button type='submit'>Save</button>
187 </form>
188
189
190 HTML;
191   mtrack_foot();
192   exit;
193 }
194
195 mtrack_head('Authentication Required');
196 echo "<h1>Please sign in with your <a id='openidlink' href='http://openid.net'><img src='{$ABSWEB}images/logo_openid.png' alt='OpenID' border='0'></a></h1>\n";
197 echo "<form method='post' action='{$ABSWEB}openid.php'>";
198 echo "<input type='text' name='openid_identifier' id='openid_identifier'>";
199 echo " <button type='submit' id='openid-sign-in'>Sign In</button>";
200
201 if ($message) {
202   $message = htmlentities($message, ENT_QUOTES, 'utf-8');
203   echo <<<HTML
204 <div class='ui-state-highlight ui-corner-all'>
205     <span class='ui-icon ui-icon-info'></span>
206     $message
207 </div>
208 HTML;
209 }
210
211 echo "</form>";
212
213
214 mtrack_foot();
215