]> git.mxchange.org Git - friendica.git/blob - src/Network/FKOAuth1.php
Move Global Functions - Part 3
[friendica.git] / src / Network / FKOAuth1.php
1 <?php
2 /**
3  * @file src/Network/FKOAuth1.php
4  */
5 namespace Friendica\Network;
6
7 use Friendica\Core\Addon;
8 use Friendica\Core\PConfig;
9 use Friendica\Core\System;
10 use Friendica\Database\DBA;
11 use Friendica\Util\DateTimeFormat;
12 use OAuthServer;
13 use OAuthSignatureMethod_HMAC_SHA1;
14 use OAuthSignatureMethod_PLAINTEXT;
15
16 /**
17  * @brief OAuth protocol
18  */
19 class FKOAuth1 extends OAuthServer
20 {
21         /**
22          * @brief Constructor
23          */
24         public function __construct()
25         {
26                 parent::__construct(new FKOAuthDataStore());
27                 $this->add_signature_method(new OAuthSignatureMethod_PLAINTEXT());
28                 $this->add_signature_method(new OAuthSignatureMethod_HMAC_SHA1());
29         }
30
31         /**
32          * @param string $uid user id
33          * @return void
34          */
35         public function loginUser($uid)
36         {
37                 logger("FKOAuth1::loginUser $uid");
38                 $a = get_app();
39                 $record = DBA::selectFirst('user', [], ['uid' => $uid, 'blocked' => 0, 'account_expired' => 0, 'account_removed' => 0, 'verified' => 1]);
40
41                 if (!DBA::isResult($record)) {
42                         logger('FKOAuth1::loginUser failure: ' . print_r($_SERVER, true), LOGGER_DEBUG);
43                         header('HTTP/1.0 401 Unauthorized');
44                         die('This api requires login');
45                 }
46                 $_SESSION['uid'] = $record['uid'];
47                 $_SESSION['theme'] = $record['theme'];
48                 $_SESSION['mobile-theme'] = PConfig::get($record['uid'], 'system', 'mobile_theme');
49                 $_SESSION['authenticated'] = 1;
50                 $_SESSION['page_flags'] = $record['page-flags'];
51                 $_SESSION['my_url'] = System::baseUrl() . '/profile/' . $record['nickname'];
52                 $_SESSION['addr'] = $_SERVER['REMOTE_ADDR'];
53                 $_SESSION["allow_api"] = true;
54
55                 $a->user = $record;
56
57                 if (strlen($a->user['timezone'])) {
58                         date_default_timezone_set($a->user['timezone']);
59                         $a->timezone = $a->user['timezone'];
60                 }
61
62                 $contact = DBA::selectFirst('contact', [], ['uid' => $_SESSION['uid'], 'self' => 1]);
63                 if (DBA::isResult($contact)) {
64                         $a->contact = $contact;
65                         $a->cid = $contact['id'];
66                         $_SESSION['cid'] = $a->cid;
67                 }
68
69                 DBA::update('user', ['login_date' => DateTimeFormat::utcNow()], ['uid' => $_SESSION['uid']]);
70
71                 Addon::callHooks('logged_in', $a->user);
72         }
73 }