]> git.mxchange.org Git - friendica.git/blob - src/Module/Update/Profile.php
Standards
[friendica.git] / src / Module / Update / Profile.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Module\Update;
23
24 use Friendica\BaseModule;
25 use Friendica\Core\Session;
26 use Friendica\Core\System;
27 use Friendica\Database\DBA;
28 use Friendica\DI;
29 use Friendica\Model\Item;
30 use Friendica\Model\Post;
31 use Friendica\Model\Profile as ProfileModel;
32 use Friendica\Model\User;
33 use Friendica\Network\HTTPException\ForbiddenException;
34 use Friendica\Util\DateTimeFormat;
35
36 class Profile extends BaseModule
37 {
38         protected function rawContent(array $request = [])
39         {
40                 $a = DI::app();
41
42                 // Ensure we've got a profile owner if updating.
43                 $a->setProfileOwner($_GET['p'] ?? 0);
44
45                 if (DI::config()->get('system', 'block_public') && !local_user() && !Session::getRemoteContactID($a->getProfileOwner())) {
46                         throw new ForbiddenException();
47                 }
48
49                 $remote_contact = Session::getRemoteContactID($a->getProfileOwner());
50                 $is_owner = local_user() == $a->getProfileOwner();
51                 $last_updated_key = "profile:" . $a->getProfileOwner() . ":" . local_user() . ":" . $remote_contact;
52
53                 if (!$is_owner && !$remote_contact) {
54                         $user = User::getById($a->getProfileOwner(), ['hidewall']);
55                         if ($user['hidewall']) {
56                                 throw new ForbiddenException(DI::l10n()->t('Access to this profile has been restricted.'));
57                         }
58                 }
59
60                 $o = '';
61
62                 if (empty($_GET['force']) && DI::pConfig()->get(local_user(), 'system', 'no_auto_update')) {
63                         System::htmlUpdateExit($o);
64                 }
65
66                 // Get permissions SQL - if $remote_contact is true, our remote user has been pre-verified and we already have fetched his/her groups
67                 $sql_extra = Item::getPermissionsSQLByUserId($a->getProfileOwner());
68
69                 $last_updated_array = Session::get('last_updated', []);
70
71                 $last_updated = $last_updated_array[$last_updated_key] ?? 0;
72
73                 if ($_GET['force'] && !empty($_GET['item'])) {
74                         // When the parent is provided, we only fetch this
75                         $sql_extra4 = " AND `parent` = " . intval($_GET['item']);
76                 } elseif ($is_owner || !$last_updated) {
77                         // If the page user is the owner of the page we should query for unseen
78                         // items. Otherwise use a timestamp of the last succesful update request.
79                         $sql_extra4 = " AND `unseen`";
80                 } else {
81                         $gmupdate = gmdate(DateTimeFormat::MYSQL, $last_updated);
82                         $sql_extra4 = " AND `received` > '" . $gmupdate . "'";
83                 }
84
85                 $items_stmt = DBA::p(
86                         "SELECT `parent-uri-id` AS `uri-id`, MAX(`created`), MAX(`received`) FROM `post-user-view`
87                                 WHERE `uid` = ? AND NOT `contact-blocked` AND NOT `contact-pending`
88                                 AND `visible` AND (NOT `deleted` OR `gravity` = ?)
89                                 AND `wall` $sql_extra4 $sql_extra
90                         GROUP BY `parent-uri-id` ORDER BY `received` DESC",
91                         $a->getProfileOwner(),
92                         GRAVITY_ACTIVITY
93                 );
94
95                 if (!DBA::isResult($items_stmt)) {
96                         return;
97                 }
98
99                 // Set a time stamp for this page. We will make use of it when we
100                 // search for new items (update routine)
101                 $last_updated_array[$last_updated_key] = time();
102                 Session::set('last_updated', $last_updated_array);
103
104                 if ($is_owner && !$a->getProfileOwner() && !DI::config()->get('theme', 'hide_eventlist')) {
105                         $o .= ProfileModel::getBirthdays();
106                         $o .= ProfileModel::getEventsReminderHTML();
107                 }
108
109                 if ($is_owner) {
110                         $unseen = Post::exists(['wall' => true, 'unseen' => true, 'uid' => local_user()]);
111                         if ($unseen) {
112                                 Item::update(['unseen' => false], ['wall' => true, 'unseen' => true, 'uid' => local_user()]);
113                         }
114                 }
115
116                 $items = DBA::toArray($items_stmt);
117
118                 $o .= DI::conversation()->create($items, 'profile', $a->getProfileOwner(), false, 'received', $a->getProfileOwner());
119
120                 System::htmlUpdateExit($o);
121         }
122 }