do not allow to change profile details, only show them
[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 if (isset($_POST['openid_url'])) {
46
47     $_SESSION['openid_url'] = $openid_url;
48     try {
49         $authRequest = $o->prepare();
50     } catch (OpenID_Exception $e) {
51         throw new Exception($e->getMessage());
52     }
53
54     // SREG
55     $sreg = new \OpenID_Extension_SREG11(\OpenID_Extension::REQUEST);
56     $sreg->set('required', 'email,fullname');
57     $authRequest->addExtension($sreg);
58
59     // AX, http://stackoverflow.com/a/7657061/282601
60     $ax = new \OpenID_Extension_AX(\OpenID_Extension::REQUEST);
61     $ax->set('type.email', 'http://axschema.org/contact/email');
62     $ax->set('type.firstname', 'http://axschema.org/namePerson/first');
63     $ax->set('type.lastname', 'http://axschema.org/namePerson/last');
64     $ax->set('type.fullname', 'http://axschema.org/namePerson');
65     $ax->set('mode', 'fetch_request');
66     $ax->set('required', 'email,firstname,lastname,fullname');
67     $authRequest->addExtension($ax);
68
69     $url = $authRequest->getAuthorizeURL();
70
71     header("Location: $url");
72     exit;
73     
74 }
75
76 if (isset($_SESSION['openid_url'])) {
77     $usid = $_SESSION['openid_url'];
78     unset($_SESSION['openid_url']);
79 } else {
80     $usid = null;
81 }
82
83 unset($_SESSION['disable_associations']);
84
85 if (!count($_POST)) {
86     list(, $queryString) = explode('?', $_SERVER['REQUEST_URI']);
87 } else {
88     // I hate php sometimes
89     $queryString = file_get_contents('php://input');
90 }
91
92 $message = new \OpenID_Message($queryString, \OpenID_Message::FORMAT_HTTP);
93 $id      = $message->get('openid.claimed_id');
94 $mode    = $message->get('openid.mode');
95
96 try {
97     $result = $o->verify(new \Net_URL2($returnTo . '?' . $queryString), $message);
98
99     if ($result->success()) {
100         $status  = "<tr><td>Status:</td><td><font color='green'>SUCCESS!";
101         $status .= " ({$result->getAssertionMethod()})</font></td></tr>";
102     } else {
103         $status  = "<tr><td>Status:</td><td><font color='red'>FAIL!";
104         $status .= " ({$result->getAssertionMethod()})</font></td></tr>";
105     }
106 } catch (OpenID_Exception $e) {
107     $status  = "<tr><td>Status:</td><td><font color='red'>EXCEPTION!";
108     $status .= " ({$e->getMessage()} : {$e->getCode()})</font></td></tr>";
109 }
110
111
112 $openid = $message->getArrayFormat();
113
114 $email = isset($openid['openid.ext1.value.email'])
115     ? $openid['openid.ext1.value.email']
116     : null;
117 $email = isset($openid['openid.ext2.value.email']) && !isset($email)
118     ? $openid['openid.ext2.value.email']
119     : $email;
120 $email = isset($openid['openid.sreg.email']) && !isset($email)
121     ? $openid['openid.sreg.email']
122     : $email;
123 $email = isset($openid['openid.ax.value.email'])
124     && isset($openid['openid.ax.type.email'])
125     && $openid['openid.ax.type.email'] == 'http://axschema.org/contact/email'
126     && !isset($email)
127     ? $openid['openid.ax.value.email']
128     : $email;
129 $_SESSION['email'] = isset($email)
130     ? $email
131     : $GLOBALS['phorkie']['auth']['anonymousEmail'];
132
133 $name = isset($openid['openid.ext1.value.firstname'])
134     && isset($openid['openid.ext1.value.lastname'])
135     ? $openid['openid.ext1.value.firstname'] . ' '
136     . $openid['openid.ext1.value.lastname']
137     : null;
138 $name = isset($openid['openid.sreg.fullname']) && !isset($name)
139     ? $openid['openid.sreg.fullname']
140     : $name;
141 $name = isset($openid['openid.ax.value.fullname'])
142     && isset($openid['openid.ax.type.fullname'])
143     && $openid['openid.ax.type.fullname'] == 'http://axschema.org/namePerson'
144     && !isset($name)
145     ? $openid['openid.ax.value.fullname']
146     : $name;
147
148 $_SESSION['name'] = isset($name) ? $name : $_SERVER['REMOTE_ADDR'];
149 $_SESSION['identity'] = $openid['openid.identity'];
150
151 if (isset($_SESSION['REQUEST_URI'])) {
152     $redirect = Tools::fullUrl($_SESSION['REQUEST_URI']);
153 } else {
154     $redirect = Tools::fullUrl('/');
155 }
156 header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
157 exit;
158 ?>