do not make it mandatory to pass template variables
[phorkie.git] / www / login.php
1 <?php
2 /**
3  * OpenID 
4  * 
5  * PHP Version 5.2.0+
6  * 
7  * @category  Auth
8  * @package   OpenID
9  * @author    Bill Shupp <hostmaster@shupp.org> 
10  * @copyright 2009 Bill Shupp
11  * @license   http://www.opensource.org/licenses/bsd-license.php FreeBSD
12  * @link      http://github.com/shupp/openid
13  */
14 namespace phorkie;
15 // A tool for testing Relying Party functionality
16 set_include_path(
17     __DIR__ . '/../../src/'
18     . PATH_SEPARATOR . get_include_path()
19 );
20
21 require_once 'www-header.php';
22 require_once 'openid/config.php';
23
24
25 if (isset($_REQUEST['logout'])) {
26     unset($_SESSION);
27     session_destroy();
28     $redirect = 'http://' . $_SERVER['HTTP_HOST'];
29     header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
30     exit;
31 }
32
33 if (!count($_GET) && !count($_POST)) {
34     render(
35         'login',
36         null
37     );
38     exit;
39 }
40
41 // Hackaround Non-Javascript Login Page
42 if (!count($_POST) && isset($_GET['start'])) {
43     $_POST = $_GET;
44 }
45
46 if (isset($_POST['openid_url'])) {
47     $openid_url = $_POST['openid_url'];
48 } else if (isset($_SESSION['openid_url'])) {
49     $openid_url = $_SESSION['openid_url'];
50 } else {
51     $openid_url = null;
52 }
53
54 try {
55     $o = new \OpenID_RelyingParty($returnTo, $realm, $openid_url);
56 } catch (OpenID_Exception $e) {
57     $contents  = "<div class='openid_results'>\n";
58     $contents .= "<pre>" . $e->getMessage() . "</pre>\n";
59     $contents .= "</div class='openid_results'>";
60     include_once 'openid/wrapper.php';
61     exit;
62 }
63
64 if (!empty($_POST['disable_associations']) || !empty($_SESSION['disable_associations'])) {
65     $o->disableAssociations();
66     $_SESSION['disable_associations'] = true;
67 }
68
69 $log = new \OpenID_Observer_Log;
70 \OpenID::attach($log);
71
72 if (isset($_POST['start'])) {
73
74     $_SESSION['openid_url'] = $openid_url;
75     try {
76         $authRequest = $o->prepare();
77     } catch (OpenID_Exception $e) {
78         $contents  = "<div class='openid_results'>\n";
79         $contents .= "<pre>" . $e->getMessage() . "</pre>\n";
80         $contents .= "</div class='openid_results'>";
81         include_once 'openid/wrapper.php';
82         exit;
83     }
84
85     // checkid_immediate
86     if (!empty($_POST['checkid_immediate'])) {
87         $authRequest->setMode('checkid_immediate');
88     }
89
90     // SREG
91     $sreg = new \OpenID_Extension_SREG11(\OpenID_Extension::REQUEST);
92     $sreg->set('required', 'email,firstname,lastname,nickname');
93     $sreg->set('optional', 'gender,dob');
94     $authRequest->addExtension($sreg);
95
96     // AX
97     $ax = new \OpenID_Extension_AX(\OpenID_Extension::REQUEST);
98     $ax->set('type.email', 'http://axschema.org/contact/email');
99     $ax->set('type.firstname', 'http://axschema.org/namePerson/first');
100     $ax->set('type.lastname', 'http://axschema.org/namePerson/last');
101     $ax->set('mode', 'fetch_request');
102     $ax->set('required', 'email,firstname,lastname');
103     $authRequest->addExtension($ax);
104
105     // UI
106     if (!empty($_POST['ui'])) {
107         $ui = new \OpenID_Extension_UI(\OpenID_Extension::REQUEST);
108         $ui->set('mode', 'popup');
109         $ui->set('language', 'en-US');
110         $authRequest->addExtension($ui);
111     }
112
113     // OAuth
114     if (!empty($_POST['oauth'])) {
115         $oauth = new \OpenID_Extension_OAuth(\OpenID_Extension::REQUEST);
116         $oauth->set('consumer', $_POST['oauth_consumer_key']);
117         $_SESSION['OAuth_consumer_key']    = $_POST['oauth_consumer_key'];
118         $_SESSION['OAuth_consumer_secret'] = $_POST['oauth_consumer_secret'];
119
120         $oauth->set('scope', $_POST['oauth_scope']);
121         $_SESSION['OAuth_scope'] = $_POST['oauth_scope'];
122
123         $_SESSION['OAuth_access_token_url']    = $_POST['oauth_access_token_url'];
124         $_SESSION['OAuth_access_token_method'] = $_POST['oauth_access_token_method'];
125
126         $authRequest->addExtension($oauth);
127     }
128     
129     $url = $authRequest->getAuthorizeURL();
130     
131     if (empty($_POST['debug'])) {
132         header("Location: $url");
133         exit;
134     }
135     
136 } else {
137     if (isset($_SESSION['openid_url'])) {
138         $usid = $_SESSION['openid_url'];
139         unset($_SESSION['openid_url']);
140     } else {
141         $usid = null;
142     }
143
144     unset($_SESSION['disable_associations']);
145
146     if (!count($_POST)) {
147         list(, $queryString) = explode('?', $_SERVER['REQUEST_URI']);
148     } else {
149         // I hate php sometimes
150         $queryString = file_get_contents('php://input');
151     }
152
153     $message = new \OpenID_Message($queryString, \OpenID_Message::FORMAT_HTTP);
154     $id      = $message->get('openid.claimed_id');
155     $mode    = $message->get('openid.mode');
156
157     try {
158         $result = $o->verify(new \Net_URL2($returnTo . '?' . $queryString), $message);
159
160         if ($result->success()) {
161             $status  = "<tr><td>Status:</td><td><font color='green'>SUCCESS!";
162             $status .= " ({$result->getAssertionMethod()})</font></td></tr>";
163         } else {
164             $status  = "<tr><td>Status:</td><td><font color='red'>FAIL!";
165             $status .= " ({$result->getAssertionMethod()})</font></td></tr>";
166         }
167     } catch (OpenID_Exception $e) {
168         $status  = "<tr><td>Status:</td><td><font color='red'>EXCEPTION!";
169         $status .= " ({$e->getMessage()} : {$e->getCode()})</font></td></tr>";
170     }
171
172     // OAuth hyprid fetching access token
173     if (isset($_SESSION['OAuth_consumer_key'],
174               $_SESSION['OAuth_consumer_secret'],
175               $_SESSION['OAuth_access_token_url'],
176               $_SESSION['OAuth_access_token_method'])) {
177
178         try {
179             $oauth = new \OpenID_Extension_OAuth(\OpenID_Extension::RESPONSE,
180                                                 $message);
181
182             // Fix line lengths.
183             $consumerKey    = $_SESSION['OAuth_consumer_key'];
184             $consumerSecret = $_SESSION['OAuth_consumer_key'];
185             $tokenURL       = $_SESSION['OAuth_access_token_url'];
186             $tokenMethod    = $_SESSION['OAuth_access_token_method'];
187
188             $oauthData = $oauth->getAccessToken($consumerKey,
189                                                 $consumerSecret,
190                                                 $tokenURL,
191                                                 array(),
192                                                 $tokenMethod);
193
194         } catch (Exception $e) {
195         }
196     }
197
198     $openid = $message->getArrayFormat();
199
200     $email = (isset($openid['openid.ext1.value.email'])) ? $openid['openid.ext1.value.email'] : null;
201     $email = (isset($openid['openid.ext2.value.email']) && !isset($email)) ? $openid['openid.ext2.value.email'] : $email;
202     $email = (isset($openid['openid.sreg.email']) && !isset($email)) ? $openid['openid.sreg.email'] : $email;
203     $email = (isset($openid['openid.ax.value.email']) && !isset($email)) ? $openid['openid.ax.value.email'] : $email;
204     $_SESSION['email'] = (isset($email)) ? $email : $GLOBALS['phorkie']['auth']['anonymousEmail'];
205
206     $name = (isset($openid['openid.ext1.value.firstname']) && isset($openid['openid.ext1.value.lastname'])) ? $openid['openid.ext1.value.firstname']." ".$openid['openid.ext1.value.lastname'] : null;
207     $name = (isset($openid['openid.sreg.firstname']) && isset($openid['openid.sreg.lastname']) && !isset($name)) ? $openid['openid.sreg.firstname']." ".$openid['openid.sreg.lastname'] : $name;
208     $name = (isset($openid['openid.sreg.nickname']) && !isset($name)) ? $openid['openid.sreg.nickname'] : $name;
209     $_SESSION['name'] = (isset($name)) ? $name : $_SERVER['REMOTE_ADDR'];
210
211     $_SESSION['identity'] = $openid['openid.identity'];
212
213     $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SESSION['REQUEST_URI'];
214     header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
215     exit;
216 }
217
218 ?>