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