421c8acccda6ee3df88acdb9210fdb057e5da872
[friendica.git/.git] / src / Module / Profile / Status.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
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\Profile;
23
24 use Friendica\Content\Nav;
25 use Friendica\Content\Pager;
26 use Friendica\Content\Widget;
27 use Friendica\Core\ACL;
28 use Friendica\Core\Session;
29 use Friendica\Database\DBA;
30 use Friendica\DI;
31 use Friendica\Model\Item;
32 use Friendica\Model\Post\Category;
33 use Friendica\Model\Profile as ProfileModel;
34 use Friendica\Model\User;
35 use Friendica\Module\BaseProfile;
36 use Friendica\Module\Security\Login;
37 use Friendica\Network\HTTPException;
38 use Friendica\Util\DateTimeFormat;
39 use Friendica\Util\Security;
40 use Friendica\Util\Strings;
41 use Friendica\Util\XML;
42
43 class Status extends BaseProfile
44 {
45         public static function content(array $parameters = [])
46         {
47                 $args = DI::args();
48
49                 $a = DI::app();
50
51                 ProfileModel::load($a, $parameters['nickname']);
52
53                 if (empty($a->profile)) {
54                         throw new HTTPException\NotFoundException(DI::l10n()->t('User not found.'));
55                 }
56
57                 if (!$a->profile['net-publish']) {
58                         DI::page()['htmlhead'] .= '<meta content="noindex, noarchive" name="robots" />' . "\n";
59                 }
60
61                 DI::page()['htmlhead'] .= '<link rel="alternate" type="application/atom+xml" href="' . DI::baseUrl() . '/dfrn_poll/' . $parameters['nickname'] . '" title="DFRN: ' . DI::l10n()->t('%s\'s timeline', $a->profile['name']) . '"/>' . "\n";
62                 DI::page()['htmlhead'] .= '<link rel="alternate" type="application/atom+xml" href="' . DI::baseUrl() . '/feed/' . $parameters['nickname'] . '/" title="' . DI::l10n()->t('%s\'s posts', $a->profile['name']) . '"/>' . "\n";
63                 DI::page()['htmlhead'] .= '<link rel="alternate" type="application/atom+xml" href="' . DI::baseUrl() . '/feed/' . $parameters['nickname'] . '/comments" title="' . DI::l10n()->t('%s\'s comments', $a->profile['name']) . '"/>' . "\n";
64                 DI::page()['htmlhead'] .= '<link rel="alternate" type="application/atom+xml" href="' . DI::baseUrl() . '/feed/' . $parameters['nickname'] . '/activity" title="' . DI::l10n()->t('%s\'s timeline', $a->profile['name']) . '"/>' . "\n";
65
66                 $category = $datequery = $datequery2 = '';
67
68                 $dtFormat = DI::dtFormat();
69
70                 if ($args->getArgc() > 3) {
71                         for ($x = 3; $x < $args->getArgc(); $x++) {
72                                 if ($dtFormat->isYearMonthDay($args->get($x))) {
73                                         if ($datequery) {
74                                                 $datequery2 = Strings::escapeHtml($args->get($x));
75                                         } else {
76                                                 $datequery = Strings::escapeHtml($args->get($x));
77                                         }
78                                 } else {
79                                         $category = $args->get($x);
80                                 }
81                         }
82                 }
83
84                 if (empty($category)) {
85                         $category = $_GET['category'] ?? '';
86                 }
87
88                 $hashtags = $_GET['tag'] ?? '';
89
90                 if (DI::config()->get('system', 'block_public') && !local_user() && !Session::getRemoteContactID($a->profile['uid'])) {
91                         return Login::form();
92                 }
93
94                 $o = '';
95
96                 if ($a->profile['uid'] == local_user()) {
97                         Nav::setSelected('home');
98                 }
99
100                 $remote_contact = Session::getRemoteContactID($a->profile['uid']);
101                 $is_owner = local_user() == $a->profile['uid'];
102                 $last_updated_key = "profile:" . $a->profile['uid'] . ":" . local_user() . ":" . $remote_contact;
103
104                 if (!empty($a->profile['hidewall']) && !$is_owner && !$remote_contact) {
105                         notice(DI::l10n()->t('Access to this profile has been restricted.'));
106                         return '';
107                 }
108
109                 $o .= self::getTabsHTML($a, 'status', $is_owner, $a->profile['nickname']);
110
111                 $o .= Widget::commonFriendsVisitor($a->profile['uid'], $a->profile['nickname']);
112
113                 $commpage = $a->profile['page-flags'] == User::PAGE_FLAGS_COMMUNITY;
114                 $commvisitor = $commpage && $remote_contact;
115
116                 DI::page()['aside'] .= Widget::postedByYear(DI::baseUrl() . '/profile/' . $a->profile['nickname'] . '/status', $a->profile['profile_uid'] ?? 0, true);
117                 DI::page()['aside'] .= Widget::categories(DI::baseUrl() . '/profile/' . $a->profile['nickname'] . '/status', XML::escape($category));
118                 DI::page()['aside'] .= Widget::tagCloud();
119
120                 if (Security::canWriteToUserWall($a->profile['uid'])) {
121                         $x = [
122                                 'is_owner' => $is_owner,
123                                 'allow_location' => ($is_owner || $commvisitor) && $a->profile['allow_location'],
124                                 'default_location' => $is_owner ? $a->user['default-location'] : '',
125                                 'nickname' => $a->profile['nickname'],
126                                 'lockstate' => is_array($a->user)
127                                 && (strlen($a->user['allow_cid'])
128                                         || strlen($a->user['allow_gid'])
129                                         || strlen($a->user['deny_cid'])
130                                         || strlen($a->user['deny_gid'])
131                                 ) ? 'lock' : 'unlock',
132                                 'acl' => $is_owner ? ACL::getFullSelectorHTML(DI::page(), $a->user, true) : '',
133                                 'bang' => '',
134                                 'visitor' => $is_owner || $commvisitor ? 'block' : 'none',
135                                 'profile_uid' => $a->profile['uid'],
136                         ];
137
138                         $o .= status_editor($a, $x);
139                 }
140
141                 // Get permissions SQL - if $remote_contact is true, our remote user has been pre-verified and we already have fetched his/her groups
142                 $sql_extra = Item::getPermissionsSQLByUserId($a->profile['uid']);
143                 $sql_extra2 = '';
144
145                 $last_updated_array = Session::get('last_updated', []);
146
147                 $sql_post_table = "";
148
149                 if (!empty($category)) {
150                         $sql_post_table = sprintf("INNER JOIN (SELECT `uri-id` FROM `category-view` WHERE `name` = '%s' AND `type` = %d AND `uid` = %d ORDER BY `uri-id` DESC) AS `category` ON `item`.`uri-id` = `category`.`uri-id` ",
151                                 DBA::escape(Strings::protectSprintf($category)), intval(Category::CATEGORY), intval($a->profile['uid']));
152                 }
153
154                 if (!empty($hashtags)) {
155                         $sql_post_table .= sprintf("INNER JOIN (SELECT `uri-id` FROM `tag-search-view` WHERE `name` = '%s' AND `uid` = %d ORDER BY `uri-id` DESC) AS `tag-search` ON `item`.`uri-id` = `tag-search`.`uri-id` ",
156                                 DBA::escape(Strings::protectSprintf($hashtags)), intval($a->profile['uid']));
157                 }
158
159                 if (!empty($datequery)) {
160                         $sql_extra2 .= Strings::protectSprintf(sprintf(" AND `thread`.`received` <= '%s' ", DBA::escape(DateTimeFormat::convert($datequery, 'UTC', date_default_timezone_get()))));
161                 }
162                 if (!empty($datequery2)) {
163                         $sql_extra2 .= Strings::protectSprintf(sprintf(" AND `thread`.`received` >= '%s' ", DBA::escape(DateTimeFormat::convert($datequery2, 'UTC', date_default_timezone_get()))));
164                 }
165
166                 // Does the profile page belong to a forum?
167                 // If not then we can improve the performance with an additional condition
168                 $condition = ['uid' => $a->profile['uid'], 'page-flags' => [User::PAGE_FLAGS_COMMUNITY, User::PAGE_FLAGS_PRVGROUP]];
169                 if (!DBA::exists('user', $condition)) {
170                         $sql_extra3 = sprintf(" AND `thread`.`contact-id` = %d ", intval(intval($a->profile['id'])));
171                 } else {
172                         $sql_extra3 = "";
173                 }
174
175                 if (DI::mode()->isMobile()) {
176                         $itemspage_network = DI::pConfig()->get(local_user(), 'system', 'itemspage_mobile_network',
177                                 DI::config()->get('system', 'itemspage_network_mobile'));
178                 } else {
179                         $itemspage_network = DI::pConfig()->get(local_user(), 'system', 'itemspage_network',
180                                 DI::config()->get('system', 'itemspage_network'));
181                 }
182
183                 //  now that we have the user settings, see if the theme forces
184                 //  a maximum item number which is lower then the user choice
185                 if (($a->force_max_items > 0) && ($a->force_max_items < $itemspage_network)) {
186                         $itemspage_network = $a->force_max_items;
187                 }
188
189                 $pager = new Pager(DI::l10n(), $args->getQueryString(), $itemspage_network);
190
191                 $pager_sql = sprintf(" LIMIT %d, %d ", $pager->getStart(), $pager->getItemsPerPage());
192
193                 $items_stmt = DBA::p(
194                         "SELECT `item`.`uri`
195                         FROM `thread`
196                         STRAIGHT_JOIN `item` ON `item`.`id` = `thread`.`iid`
197                         $sql_post_table
198                         STRAIGHT_JOIN `contact`
199                         ON `contact`.`id` = `thread`.`contact-id`
200                                 AND NOT `contact`.`blocked`
201                                 AND NOT `contact`.`pending`
202                         WHERE `thread`.`uid` = ?
203                                 AND `thread`.`visible`
204                                 AND NOT `thread`.`deleted`
205                                 AND NOT `thread`.`moderated`
206                                 AND `thread`.`wall`
207                                 $sql_extra3
208                                 $sql_extra
209                                 $sql_extra2
210                         ORDER BY `thread`.`received` DESC
211                         $pager_sql",
212                         $a->profile['uid']
213                 );
214
215                 // Set a time stamp for this page. We will make use of it when we
216                 // search for new items (update routine)
217                 $last_updated_array[$last_updated_key] = time();
218                 Session::set('last_updated', $last_updated_array);
219
220                 if ($is_owner && !DI::config()->get('theme', 'hide_eventlist')) {
221                         $o .= ProfileModel::getBirthdays();
222                         $o .= ProfileModel::getEventsReminderHTML();
223                 }
224
225                 if ($is_owner) {
226                         $unseen = Item::exists(['wall' => true, 'unseen' => true, 'uid' => local_user()]);
227                         if ($unseen) {
228                                 Item::update(['unseen' => false], ['wall' => true, 'unseen' => true, 'uid' => local_user()]);
229                         }
230                 }
231
232                 $items = DBA::toArray($items_stmt);
233
234                 if ($pager->getStart() == 0 && !empty($a->profile['uid'])) {
235                         $condition = ['private' => [Item::PUBLIC, Item::UNLISTED]];
236                         if (remote_user()) {
237                                 $permissionSets = DI::permissionSet()->selectByContactId(remote_user(), $a->profile['uid']);
238                                 if (!empty($permissionSets)) {
239                                         $condition = ['psid' => array_merge($permissionSets->column('id'),
240                                                         [DI::permissionSet()->getIdFromACL($a->profile['uid'], '', '', '', '')])];
241                                 }
242                         } elseif ($a->profile['uid'] == local_user()) {
243                                 $condition = [];
244                         }
245         
246                         $pinned_items = Item::selectPinned($a->profile['uid'], ['uri', 'pinned'], $condition);
247                         $pinned = Item::inArray($pinned_items);
248                         $items = array_merge($items, $pinned);
249                 }
250
251                 $o .= conversation($a, $items, 'profile', false, false, 'pinned_received', $a->profile['uid']);
252
253                 $o .= $pager->renderMinimal(count($items));
254
255                 return $o;
256         }
257 }