790a73a0acb9fee9d31d84305d2985d1fcf57310
[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 $pageRequiresLogin = false;
22 require_once 'www-header.php';
23 require_once 'openid/config.php';
24
25
26 if (isset($_REQUEST['logout'])) {
27     unset($_SESSION);
28     session_destroy();
29     $redirect = 'http://' . $_SERVER['HTTP_HOST'];
30     header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
31     exit;
32 }
33
34 if (!count($_GET) && !count($_POST)) {
35     render(
36         'login',
37         null
38     );
39     exit;
40 }
41
42 // Hackaround Non-Javascript Login Page
43 if (!count($_POST) && isset($_GET['start'])) {
44     $_POST = $_GET;
45 }
46
47 if (isset($_POST['openid_url'])) {
48     $openid_url = $_POST['openid_url'];
49 } else if (isset($_SESSION['openid_url'])) {
50     $openid_url = $_SESSION['openid_url'];
51 } else {
52     $openid_url = null;
53 }
54
55 try {
56     $o = new \OpenID_RelyingParty($returnTo, $realm, $openid_url);
57 } catch (OpenID_Exception $e) {
58     $contents  = "<div class='openid_results'>\n";
59     $contents .= "<pre>" . $e->getMessage() . "</pre>\n";
60     $contents .= "</div class='openid_results'>";
61     include_once 'openid/wrapper.php';
62     exit;
63 }
64
65 if (!empty($_POST['disable_associations']) || !empty($_SESSION['disable_associations'])) {
66     $o->disableAssociations();
67     $_SESSION['disable_associations'] = true;
68 }
69
70 $log = new \OpenID_Observer_Log;
71 \OpenID::attach($log);
72
73 if (isset($_POST['start'])) {
74
75     $_SESSION['openid_url'] = $openid_url;
76     try {
77         $authRequest = $o->prepare();
78     } catch (OpenID_Exception $e) {
79         $contents  = "<div class='openid_results'>\n";
80         $contents .= "<pre>" . $e->getMessage() . "</pre>\n";
81         $contents .= "</div class='openid_results'>";
82         include_once 'openid/wrapper.php';
83         exit;
84     }
85
86     // SREG
87     $sreg = new \OpenID_Extension_SREG11(\OpenID_Extension::REQUEST);
88     $sreg->set('required', 'email,fullname');
89     $authRequest->addExtension($sreg);
90
91     // AX
92     $ax = new \OpenID_Extension_AX(\OpenID_Extension::REQUEST);
93     $ax->set('type.email', 'http://axschema.org/contact/email');
94     $ax->set('type.firstname', 'http://axschema.org/namePerson/first');
95     $ax->set('type.lastname', 'http://axschema.org/namePerson/last');
96     $ax->set('mode', 'fetch_request');
97     $ax->set('required', 'email,firstname,lastname');
98     $authRequest->addExtension($ax);
99
100     $url = $authRequest->getAuthorizeURL();
101     
102     header("Location: $url");
103     exit;
104     
105 }
106
107 if (isset($_SESSION['openid_url'])) {
108     $usid = $_SESSION['openid_url'];
109     unset($_SESSION['openid_url']);
110 } else {
111     $usid = null;
112 }
113
114 unset($_SESSION['disable_associations']);
115
116 if (!count($_POST)) {
117     list(, $queryString) = explode('?', $_SERVER['REQUEST_URI']);
118 } else {
119     // I hate php sometimes
120     $queryString = file_get_contents('php://input');
121 }
122
123 $message = new \OpenID_Message($queryString, \OpenID_Message::FORMAT_HTTP);
124 $id      = $message->get('openid.claimed_id');
125 $mode    = $message->get('openid.mode');
126
127 try {
128     $result = $o->verify(new \Net_URL2($returnTo . '?' . $queryString), $message);
129
130     if ($result->success()) {
131         $status  = "<tr><td>Status:</td><td><font color='green'>SUCCESS!";
132         $status .= " ({$result->getAssertionMethod()})</font></td></tr>";
133     } else {
134         $status  = "<tr><td>Status:</td><td><font color='red'>FAIL!";
135         $status .= " ({$result->getAssertionMethod()})</font></td></tr>";
136     }
137 } catch (OpenID_Exception $e) {
138     $status  = "<tr><td>Status:</td><td><font color='red'>EXCEPTION!";
139     $status .= " ({$e->getMessage()} : {$e->getCode()})</font></td></tr>";
140   }
141
142
143 $openid = $message->getArrayFormat();
144
145 $email = isset($openid['openid.ext1.value.email'])
146     ? $openid['openid.ext1.value.email']
147     : null;
148 $email = isset($openid['openid.ext2.value.email']) && !isset($email)
149     ? $openid['openid.ext2.value.email']
150     : $email;
151 $email = isset($openid['openid.sreg.email']) && !isset($email)
152     ? $openid['openid.sreg.email']
153     : $email;
154 $email = isset($openid['openid.ax.value.email']) && !isset($email)
155     ? $openid['openid.ax.value.email']
156     : $email;
157 $_SESSION['email'] = isset($email)
158     ? $email
159     : $GLOBALS['phorkie']['auth']['anonymousEmail'];
160
161 $name = isset($openid['openid.ext1.value.firstname'])
162     && isset($openid['openid.ext1.value.lastname'])
163     ? $openid['openid.ext1.value.firstname'] . ' '
164     . $openid['openid.ext1.value.lastname']
165     : null;
166 $name = isset($openid['openid.sreg.fullname']) && !isset($name)
167     ? $openid['openid.sreg.fullname']
168     : $name;
169
170 $_SESSION['name'] = isset($name) ? $name : $_SERVER['REMOTE_ADDR'];
171 $_SESSION['identity'] = $openid['openid.identity'];
172
173 $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SESSION['REQUEST_URI'];
174 header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
175 exit;
176 ?>