]> git.mxchange.org Git - friendica.git/blob - mod/openid.php
Catch HTTPExceptions in App::runFrontend()
[friendica.git] / mod / openid.php
1 <?php
2 /**
3  * @file mod/openid.php
4  */
5
6 use Friendica\App;
7 use Friendica\Core\Authentication;
8 use Friendica\Core\Config;
9 use Friendica\Core\L10n;
10 use Friendica\Core\Logger;
11 use Friendica\Core\System;
12 use Friendica\Database\DBA;
13 use Friendica\Util\Strings;
14
15 function openid_content(App $a) {
16
17         $noid = Config::get('system','no_openid');
18         if($noid)
19                 $a->internalRedirect();
20
21         Logger::log('mod_openid ' . print_r($_REQUEST,true), Logger::DATA);
22
23         if(!empty($_GET['openid_mode']) && !empty($_SESSION['openid'])) {
24
25                 $openid = new LightOpenID($a->getHostName());
26
27                 if($openid->validate()) {
28
29                         $authid = $_REQUEST['openid_identity'];
30
31                         if(! strlen($authid)) {
32                                 Logger::log(L10n::t('OpenID protocol error. No ID returned.') . EOL);
33                                 $a->internalRedirect();
34                         }
35
36                         // NOTE: we search both for normalised and non-normalised form of $authid
37                         //       because the normalization step was removed from setting
38                         //       mod/settings.php in 8367cad so it might have left mixed
39                         //       records in the user table
40                         //
41                         $r = q("SELECT *
42                                 FROM `user`
43                                 WHERE ( `openid` = '%s' OR `openid` = '%s' )
44                                 AND `blocked` = 0 AND `account_expired` = 0
45                                 AND `account_removed` = 0 AND `verified` = 1
46                                 LIMIT 1",
47                                 DBA::escape($authid), DBA::escape(Strings::normaliseOpenID($authid))
48                         );
49
50                         if (DBA::isResult($r)) {
51
52                                 // successful OpenID login
53
54                                 unset($_SESSION['openid']);
55
56                                 Authentication::setAuthenticatedSessionForUser($r[0],true,true);
57
58                                 // just in case there was no return url set
59                                 // and we fell through
60
61                                 $a->internalRedirect();
62                         }
63
64                         // Successful OpenID login - but we can't match it to an existing account.
65                         // New registration?
66
67                         if (intval(Config::get('config', 'register_policy')) === REGISTER_CLOSED) {
68                                 notice(L10n::t('Account not found and OpenID registration is not permitted on this site.') . EOL);
69                                 $a->internalRedirect();
70                         }
71
72                         unset($_SESSION['register']);
73                         $args = '';
74                         $attr = $openid->getAttributes();
75                         if (is_array($attr) && count($attr)) {
76                                 foreach ($attr as $k => $v) {
77                                         if ($k === 'namePerson/friendly') {
78                                                 $nick = Strings::escapeTags(trim($v));
79                                         }
80                                         if($k === 'namePerson/first') {
81                                                 $first = Strings::escapeTags(trim($v));
82                                         }
83                                         if($k === 'namePerson') {
84                                                 $args .= '&username=' . urlencode(Strings::escapeTags(trim($v)));
85                                         }
86                                         if ($k === 'contact/email') {
87                                                 $args .= '&email=' . urlencode(Strings::escapeTags(trim($v)));
88                                         }
89                                         if ($k === 'media/image/aspect11') {
90                                                 $photosq = bin2hex(trim($v));
91                                         }
92                                         if ($k === 'media/image/default') {
93                                                 $photo = bin2hex(trim($v));
94                                         }
95                                 }
96                         }
97                         if ($nick) {
98                                 $args .= '&nickname=' . urlencode($nick);
99                         }
100                         elseif ($first) {
101                                 $args .= '&nickname=' . urlencode($first);
102                         }
103
104                         if ($photosq) {
105                                 $args .= '&photo=' . urlencode($photosq);
106                         }
107                         elseif ($photo) {
108                                 $args .= '&photo=' . urlencode($photo);
109                         }
110
111                         $args .= '&openid_url=' . urlencode(Strings::escapeTags(trim($authid)));
112
113                         $a->internalRedirect('register?' . $args);
114
115                         // NOTREACHED
116                 }
117         }
118         notice(L10n::t('Login failed.') . EOL);
119         $a->internalRedirect();
120         // NOTREACHED
121 }