secure.php is not used anymore
[phorkie.git] / www / login.php
1 <?php
2 namespace phorkie;
3 $noSecurityCheck = true;
4 require_once 'www-header.php';
5
6 if (isset($_REQUEST['logout'])) {
7     unset($_SESSION);
8     session_destroy();
9     header('Location: ' . Tools::fullUrl('/'));
10     exit();
11 }
12
13 if (!count($_GET) && !count($_POST)) {
14     render('login');
15     exit();
16 }
17
18 // Hackaround Non-Javascript Login Page
19 if (!count($_POST) && isset($_GET['openid_url'])) {
20     $_POST = $_GET;
21 }
22
23 if (isset($_POST['openid_url'])) {
24     $openid_url = $_POST['openid_url'];
25 } else if (isset($_SESSION['openid_url'])) {
26     $openid_url = $_SESSION['openid_url'];
27 } else {
28     $openid_url = null;
29 }
30
31 $realm    = Tools::fullUrl('/');
32 $returnTo = Tools::fullUrl('/login');
33
34 try {
35     $o = new \OpenID_RelyingParty($returnTo, $realm, $openid_url);
36 } catch (OpenID_Exception $e) {
37     throw new Exception($e->getMessage());
38 }
39
40 if (!empty($_POST['disable_associations']) || !empty($_SESSION['disable_associations'])) {
41     $o->disableAssociations();
42     $_SESSION['disable_associations'] = true;
43 }
44
45 $log = new \OpenID_Observer_Log;
46 \OpenID::attach($log);
47
48 if (isset($_POST['openid_url'])) {
49
50     $_SESSION['openid_url'] = $openid_url;
51     try {
52         $authRequest = $o->prepare();
53     } catch (OpenID_Exception $e) {
54         throw new Exception($e->getMessage());
55     }
56
57     // SREG
58     $sreg = new \OpenID_Extension_SREG11(\OpenID_Extension::REQUEST);
59     $sreg->set('required', 'email,fullname');
60     $authRequest->addExtension($sreg);
61
62     // AX
63     $ax = new \OpenID_Extension_AX(\OpenID_Extension::REQUEST);
64     $ax->set('type.email', 'http://axschema.org/contact/email');
65     $ax->set('type.firstname', 'http://axschema.org/namePerson/first');
66     $ax->set('type.lastname', 'http://axschema.org/namePerson/last');
67     $ax->set('mode', 'fetch_request');
68     $ax->set('required', 'email,firstname,lastname');
69     $authRequest->addExtension($ax);
70
71     $url = $authRequest->getAuthorizeURL();
72
73     header("Location: $url");
74     exit;
75     
76 }
77
78 if (isset($_SESSION['openid_url'])) {
79     $usid = $_SESSION['openid_url'];
80     unset($_SESSION['openid_url']);
81 } else {
82     $usid = null;
83 }
84
85 unset($_SESSION['disable_associations']);
86
87 if (!count($_POST)) {
88     list(, $queryString) = explode('?', $_SERVER['REQUEST_URI']);
89 } else {
90     // I hate php sometimes
91     $queryString = file_get_contents('php://input');
92 }
93
94 $message = new \OpenID_Message($queryString, \OpenID_Message::FORMAT_HTTP);
95 $id      = $message->get('openid.claimed_id');
96 $mode    = $message->get('openid.mode');
97
98 try {
99     $result = $o->verify(new \Net_URL2($returnTo . '?' . $queryString), $message);
100
101     if ($result->success()) {
102         $status  = "<tr><td>Status:</td><td><font color='green'>SUCCESS!";
103         $status .= " ({$result->getAssertionMethod()})</font></td></tr>";
104     } else {
105         $status  = "<tr><td>Status:</td><td><font color='red'>FAIL!";
106         $status .= " ({$result->getAssertionMethod()})</font></td></tr>";
107     }
108 } catch (OpenID_Exception $e) {
109     $status  = "<tr><td>Status:</td><td><font color='red'>EXCEPTION!";
110     $status .= " ({$e->getMessage()} : {$e->getCode()})</font></td></tr>";
111 }
112
113
114 $openid = $message->getArrayFormat();
115
116 $email = isset($openid['openid.ext1.value.email'])
117     ? $openid['openid.ext1.value.email']
118     : null;
119 $email = isset($openid['openid.ext2.value.email']) && !isset($email)
120     ? $openid['openid.ext2.value.email']
121     : $email;
122 $email = isset($openid['openid.sreg.email']) && !isset($email)
123     ? $openid['openid.sreg.email']
124     : $email;
125 $email = isset($openid['openid.ax.value.email']) && !isset($email)
126     ? $openid['openid.ax.value.email']
127     : $email;
128 $_SESSION['email'] = isset($email)
129     ? $email
130     : $GLOBALS['phorkie']['auth']['anonymousEmail'];
131
132 $name = isset($openid['openid.ext1.value.firstname'])
133     && isset($openid['openid.ext1.value.lastname'])
134     ? $openid['openid.ext1.value.firstname'] . ' '
135     . $openid['openid.ext1.value.lastname']
136     : null;
137 $name = isset($openid['openid.sreg.fullname']) && !isset($name)
138     ? $openid['openid.sreg.fullname']
139     : $name;
140
141 $_SESSION['name'] = isset($name) ? $name : $_SERVER['REMOTE_ADDR'];
142 $_SESSION['identity'] = $openid['openid.identity'];
143
144 if (isset($_SESSION['REQUEST_URI'])) {
145     $redirect = Tools::fullUrl($_SESSION['REQUEST_URI']);
146 } else {
147     $redirect = Tools::fullUrl('/');
148 }
149 header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
150 exit;
151 ?>