3 * @copyright Copyright (C) 2010-2022, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Module\Update;
24 use Friendica\BaseModule;
25 use Friendica\Core\Session;
26 use Friendica\Core\System;
27 use Friendica\Database\DBA;
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;
36 class Profile extends BaseModule
38 protected function rawContent(array $request = [])
42 // Ensure we've got a profile owner if updating.
43 $a->setProfileOwner($_GET['p'] ?? 0);
45 if (DI::config()->get('system', 'block_public') && !local_user() && !Session::getRemoteContactID($a->getProfileOwner())) {
46 throw new ForbiddenException();
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;
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.'));
62 if (empty($_GET['force']) && DI::pConfig()->get(local_user(), 'system', 'no_auto_update')) {
63 System::htmlUpdateExit($o);
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());
69 $last_updated_array = Session::get('last_updated', []);
71 $last_updated = $last_updated_array[$last_updated_key] ?? 0;
73 $condition = ["`uid` = ? AND NOT `contact-blocked` AND NOT `contact-pending`
74 AND `visible` AND (NOT `deleted` OR `gravity` = ?)
75 AND `wall` " . $sql_extra, $a->getProfileOwner(), GRAVITY_ACTIVITY];
77 if ($_GET['force'] && !empty($_GET['item'])) {
78 // When the parent is provided, we only fetch this
79 $condition = DBA::mergeConditions($condition, ['parent' => $_GET['item']]);
80 } elseif ($is_owner || !$last_updated) {
81 // If the page user is the owner of the page we should query for unseen
82 // items. Otherwise use a timestamp of the last succesful update request.
83 $condition = DBA::mergeConditions($condition, ['unseen' => true]);
85 $gmupdate = gmdate(DateTimeFormat::MYSQL, $last_updated);
86 $condition = DBA::mergeConditions($condition, ["`received` > ?", $gmupdate]);
89 $items = Post::selectToArray(['parent-uri-id', 'created', 'received'], $condition, ['group_by' => ['parent-uri-id'], 'order' => ['received' => true]]);
90 if (!DBA::isResult($items)) {
94 // @todo the DBA functions should handle "SELECT field AS alias" in the future,
95 // so that this workaround here could be removed.
96 $items = array_map(function ($item) {
97 $item['uri-id'] = $item['parent-uri-id'];
98 unset($item['parent-uri-id']);
102 // Set a time stamp for this page. We will make use of it when we
103 // search for new items (update routine)
104 $last_updated_array[$last_updated_key] = time();
105 Session::set('last_updated', $last_updated_array);
107 if ($is_owner && !$a->getProfileOwner() && !DI::config()->get('theme', 'hide_eventlist')) {
108 $o .= ProfileModel::getBirthdays();
109 $o .= ProfileModel::getEventsReminderHTML();
113 $unseen = Post::exists(['wall' => true, 'unseen' => true, 'uid' => local_user()]);
115 Item::update(['unseen' => false], ['wall' => true, 'unseen' => true, 'uid' => local_user()]);
119 $o .= DI::conversation()->create($items, 'profile', $a->getProfileOwner(), false, 'received', $a->getProfileOwner());
121 System::htmlUpdateExit($o);