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