]> git.mxchange.org Git - friendica.git/blob - src/Module/Profile.php
Revert logic change in query in NotificationsManager->setSeen
[friendica.git] / src / Module / Profile.php
1 <?php\r
2 \r
3 namespace Friendica\Module;\r
4 \r
5 use Friendica\BaseModule;\r
6 use Friendica\Content\Nav;\r
7 use Friendica\Content\Pager;\r
8 use Friendica\Content\Widget;\r
9 use Friendica\Core\ACL;\r
10 use Friendica\Core\Config;\r
11 use Friendica\Core\Hook;\r
12 use Friendica\Core\L10n;\r
13 use Friendica\Core\PConfig;\r
14 use Friendica\Core\System;\r
15 use Friendica\Database\DBA;\r
16 use Friendica\Model\Contact as ContactModel;\r
17 use Friendica\Model\Group;\r
18 use Friendica\Model\Item;\r
19 use Friendica\Model\Profile AS ProfileModel;\r
20 use Friendica\Model\User;\r
21 use Friendica\Protocol\ActivityPub;\r
22 use Friendica\Protocol\DFRN;\r
23 use Friendica\Util\DateTimeFormat;\r
24 use Friendica\Util\Security;\r
25 use Friendica\Util\Strings;\r
26 use Friendica\Util\XML;\r
27 \r
28 require_once 'boot.php';\r
29 \r
30 class Profile extends BaseModule\r
31 {\r
32         public static $which = '';\r
33         public static $profile = 0;\r
34 \r
35         public static function init()\r
36         {\r
37                 $a = self::getApp();\r
38 \r
39                 if ($a->argc < 2) {\r
40                         System::httpExit(400);\r
41                 }\r
42 \r
43                 self::$which = filter_var($a->argv[1], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_HIGH | FILTER_FLAG_STRIP_BACKTICK);\r
44 \r
45                 if (local_user() && $a->argc > 2 && $a->argv[2] === 'view') {\r
46                         self::$which = $a->user['nickname'];\r
47                         self::$profile = filter_var($a->argv[1], FILTER_SANITIZE_NUMBER_INT);\r
48                 } else {\r
49                         DFRN::autoRedir($a, self::$which);\r
50                 }\r
51         }\r
52 \r
53         public static function rawContent()\r
54         {\r
55                 if (ActivityPub::isRequest()) {\r
56                         $user = DBA::selectFirst('user', ['uid'], ['nickname' => self::$which]);\r
57                         if (DBA::isResult($user)) {\r
58                                 $data = ActivityPub\Transmitter::getProfile($user['uid']);\r
59                                 System::jsonExit($data, 'application/activity+json');\r
60                         } elseif (DBA::exists('userd', ['username' => self::$which])) {\r
61                                 // Known deleted user\r
62                                 System::httpExit(410);\r
63                         } else {\r
64                                 // Unknown user\r
65                                 System::httpExit(404);\r
66                         }\r
67                 }\r
68         }\r
69 \r
70         public static function content($update = 0)\r
71         {\r
72                 $a = self::getApp();\r
73 \r
74                 if (!$update) {\r
75                         ProfileModel::load($a, self::$which, self::$profile);\r
76 \r
77                         $blocked   = !local_user() && !remote_user() && Config::get('system', 'block_public');\r
78                         $userblock = !local_user() && !remote_user() && $a->profile['hidewall'];\r
79 \r
80                         if (!empty($a->profile['page-flags']) && $a->profile['page-flags'] == User::PAGE_FLAGS_COMMUNITY) {\r
81                                 $a->page['htmlhead'] .= '<meta name="friendica.community" content="true" />';\r
82                         }\r
83 \r
84                         if (!empty($a->profile['openidserver'])) {\r
85                                 $a->page['htmlhead'] .= '<link rel="openid.server" href="' . $a->profile['openidserver'] . '" />' . "\n";\r
86                         }\r
87 \r
88                         if (!empty($a->profile['openid'])) {\r
89                                 $delegate = strstr($a->profile['openid'], '://') ? $a->profile['openid'] : 'https://' . $a->profile['openid'];\r
90                                 $a->page['htmlhead'] .= '<link rel="openid.delegate" href="' . $delegate . '" />' . "\n";\r
91                         }\r
92 \r
93                         // site block\r
94                         if (!$blocked && !$userblock) {\r
95                                 $keywords = str_replace(['#', ',', ' ', ',,'], ['', ' ', ',', ','], defaults($a->profile, 'pub_keywords', ''));\r
96                                 if (strlen($keywords)) {\r
97                                         $a->page['htmlhead'] .= '<meta name="keywords" content="' . $keywords . '" />' . "\n";\r
98                                 }\r
99                         }\r
100 \r
101                         $a->page['htmlhead'] .= '<meta name="dfrn-global-visibility" content="' . ($a->profile['net-publish'] ? 'true' : 'false') . '" />' . "\n";\r
102                         $a->page['htmlhead'] .= '<link rel="alternate" type="application/atom+xml" href="' . System::baseUrl() . '/dfrn_poll/' . self::$which . '" title="DFRN: ' . L10n::t('%s\'s timeline', $a->profile['username']) . '"/>' . "\n";\r
103                         $a->page['htmlhead'] .= '<link rel="alternate" type="application/atom+xml" href="' . System::baseUrl() . '/feed/' . self::$which . '/" title="' . L10n::t('%s\'s posts', $a->profile['username']) . '"/>' . "\n";\r
104                         $a->page['htmlhead'] .= '<link rel="alternate" type="application/atom+xml" href="' . System::baseUrl() . '/feed/' . self::$which . '/comments" title="' . L10n::t('%s\'s comments', $a->profile['username']) . '"/>' . "\n";\r
105                         $a->page['htmlhead'] .= '<link rel="alternate" type="application/atom+xml" href="' . System::baseUrl() . '/feed/' . self::$which . '/activity" title="' . L10n::t('%s\'s timeline', $a->profile['username']) . '"/>' . "\n";\r
106                         $uri = urlencode('acct:' . $a->profile['nickname'] . '@' . $a->getHostName() . ($a->getURLPath() ? '/' . $a->getURLPath() : ''));\r
107                         $a->page['htmlhead'] .= '<link rel="lrdd" type="application/xrd+xml" href="' . System::baseUrl() . '/xrd/?uri=' . $uri . '" />' . "\n";\r
108                         header('Link: <' . System::baseUrl() . '/xrd/?uri=' . $uri . '>; rel="lrdd"; type="application/xrd+xml"', false);\r
109 \r
110                         $dfrn_pages = ['request', 'confirm', 'notify', 'poll'];\r
111                         foreach ($dfrn_pages as $dfrn) {\r
112                                 $a->page['htmlhead'] .= '<link rel="dfrn-' . $dfrn . '" href="' . System::baseUrl() . '/dfrn_' . $dfrn . '/' . self::$which . '" />' . "\n";\r
113                         }\r
114                         $a->page['htmlhead'] .= '<link rel="dfrn-poco" href="' . System::baseUrl() . '/poco/' . self::$which . '" />' . "\n";\r
115                 }\r
116 \r
117                 $category = $datequery = $datequery2 = '';\r
118 \r
119                 if ($a->argc > 2) {\r
120                         for ($x = 2; $x < $a->argc; $x ++) {\r
121                                 if (is_a_date_arg($a->argv[$x])) {\r
122                                         if ($datequery) {\r
123                                                 $datequery2 = Strings::escapeHtml($a->argv[$x]);\r
124                                         } else {\r
125                                                 $datequery = Strings::escapeHtml($a->argv[$x]);\r
126                                         }\r
127                                 } else {\r
128                                         $category = $a->argv[$x];\r
129                                 }\r
130                         }\r
131                 }\r
132 \r
133                 if (empty($category)) {\r
134                         $category = defaults($_GET, 'category', '');\r
135                 }\r
136 \r
137                 $hashtags = defaults($_GET, 'tag', '');\r
138 \r
139                 if (Config::get('system', 'block_public') && !local_user() && !remote_user()) {\r
140                         return Login::form();\r
141                 }\r
142 \r
143                 $groups = [];\r
144                 $remote_cid = null;\r
145 \r
146                 $o = '';\r
147 \r
148                 if ($update) {\r
149                         // Ensure we've got a profile owner if updating.\r
150                         $a->profile['profile_uid'] = $update;\r
151                 } elseif ($a->profile['profile_uid'] == local_user()) {\r
152                         Nav::setSelected('home');\r
153                 }\r
154 \r
155                 $remote_contact = ContactModel::isFollower(remote_user(), $a->profile['profile_uid']);\r
156                 $is_owner = local_user() == $a->profile['profile_uid'];\r
157                 $last_updated_key = "profile:" . $a->profile['profile_uid'] . ":" . local_user() . ":" . remote_user();\r
158 \r
159                 if ($remote_contact) {\r
160                         $cdata = ContactModel::getPublicAndUserContacID(remote_user(), $a->profile['profile_uid']);\r
161                         if (!empty($cdata['user'])) {\r
162                                 $groups = Group::getIdsByContactId($cdata['user']);\r
163                                 $remote_cid = $cdata['user'];\r
164                         }\r
165                 }\r
166 \r
167                 if (!empty($a->profile['hidewall']) && !$is_owner && !$remote_contact) {\r
168                         notice(L10n::t('Access to this profile has been restricted.') . EOL);\r
169                         return '';\r
170                 }\r
171 \r
172                 if (!$update) {\r
173                         $tab = false;\r
174                         if (!empty($_GET['tab'])) {\r
175                                 $tab = Strings::escapeTags(trim($_GET['tab']));\r
176                         }\r
177 \r
178                         $o .= ProfileModel::getTabs($a, $is_owner, $a->profile['nickname']);\r
179 \r
180                         if ($tab === 'profile') {\r
181                                 $o .= ProfileModel::getAdvanced($a);\r
182                                 Hook::callAll('profile_advanced', $o);\r
183                                 return $o;\r
184                         }\r
185 \r
186                         $o .= Widget::commonFriendsVisitor($a->profile['profile_uid']);\r
187 \r
188                         $commpage = $a->profile['page-flags'] == User::PAGE_FLAGS_COMMUNITY;\r
189                         $commvisitor = $commpage && $remote_contact;\r
190 \r
191                         $a->page['aside'] .= posted_date_widget(System::baseUrl(true) . '/profile/' . $a->profile['nickname'], $a->profile['profile_uid'], true);\r
192                         $a->page['aside'] .= Widget::categories(System::baseUrl(true) . '/profile/' . $a->profile['nickname'], (!empty($category) ? XML::escape($category) : ''));\r
193                         $a->page['aside'] .= Widget::tagCloud();\r
194 \r
195                         if (Security::canWriteToUserWall($a->profile['profile_uid'])) {\r
196                                 $x = [\r
197                                         'is_owner' => $is_owner,\r
198                                         'allow_location' => ($is_owner || $commvisitor) && $a->profile['allow_location'],\r
199                                         'default_location' => $is_owner ? $a->user['default-location'] : '',\r
200                                         'nickname' => $a->profile['nickname'],\r
201                                         'lockstate' => is_array($a->user)\r
202                                         && (strlen($a->user['allow_cid'])\r
203                                                 || strlen($a->user['allow_gid'])\r
204                                                 || strlen($a->user['deny_cid'])\r
205                                                 || strlen($a->user['deny_gid'])\r
206                                         ) ? 'lock' : 'unlock',\r
207                                         'acl' => $is_owner ? ACL::getFullSelectorHTML($a->user, true) : '',\r
208                                         'bang' => '',\r
209                                         'visitor' => $is_owner || $commvisitor ? 'block' : 'none',\r
210                                         'profile_uid' => $a->profile['profile_uid'],\r
211                                 ];\r
212 \r
213                                 $o .= status_editor($a, $x);\r
214                         }\r
215                 }\r
216 \r
217                 // Get permissions SQL - if $remote_contact is true, our remote user has been pre-verified and we already have fetched his/her groups\r
218                 $sql_extra = Item::getPermissionsSQLByUserId($a->profile['profile_uid'], $remote_contact, $groups, $remote_cid);\r
219                 $sql_extra2 = '';\r
220 \r
221                 if ($update) {\r
222                         $last_updated = (defaults($_SESSION['last_updated'], $last_updated_key, 0));\r
223 \r
224                         // If the page user is the owner of the page we should query for unseen\r
225                         // items. Otherwise use a timestamp of the last succesful update request.\r
226                         if ($is_owner || !$last_updated) {\r
227                                 $sql_extra4 = " AND `item`.`unseen`";\r
228                         } else {\r
229                                 $gmupdate = gmdate(DateTimeFormat::MYSQL, $last_updated);\r
230                                 $sql_extra4 = " AND `item`.`received` > '" . $gmupdate . "'";\r
231                         }\r
232 \r
233                         $items_stmt = DBA::p(\r
234                                 "SELECT DISTINCT(`parent-uri`) AS `uri`, `item`.`created`\r
235                                 FROM `item`\r
236                                 INNER JOIN `contact`\r
237                                 ON `contact`.`id` = `item`.`contact-id`\r
238                                         AND NOT `contact`.`blocked`\r
239                                         AND NOT `contact`.`pending`\r
240                                 WHERE `item`.`uid` = ?\r
241                                         AND `item`.`visible`\r
242                                         AND     (NOT `item`.`deleted` OR `item`.`gravity` = ?)\r
243                                         AND NOT `item`.`moderated`\r
244                                         AND `item`.`wall`\r
245                                         $sql_extra4\r
246                                         $sql_extra\r
247                                 ORDER BY `item`.`created` DESC",\r
248                                 $a->profile['profile_uid'],\r
249                                 GRAVITY_ACTIVITY\r
250                         );\r
251 \r
252                         if (!DBA::isResult($items_stmt)) {\r
253                                 return '';\r
254                         }\r
255 \r
256                         $pager = new Pager($a->query_string);\r
257                 } else {\r
258                         $sql_post_table = "";\r
259 \r
260                         if (!empty($category)) {\r
261                                 $sql_post_table = sprintf("INNER JOIN (SELECT `oid` FROM `term` WHERE `term` = '%s' AND `otype` = %d AND `type` = %d AND `uid` = %d ORDER BY `tid` DESC) AS `term` ON `item`.`id` = `term`.`oid` ",\r
262                                         DBA::escape(Strings::protectSprintf($category)), intval(TERM_OBJ_POST), intval(TERM_CATEGORY), intval($a->profile['profile_uid']));\r
263                         }\r
264 \r
265                         if (!empty($hashtags)) {\r
266                                 $sql_post_table .= sprintf("INNER JOIN (SELECT `oid` FROM `term` WHERE `term` = '%s' AND `otype` = %d AND `type` = %d AND `uid` = %d ORDER BY `tid` DESC) AS `term` ON `item`.`id` = `term`.`oid` ",\r
267                                         DBA::escape(Strings::protectSprintf($hashtags)), intval(TERM_OBJ_POST), intval(TERM_HASHTAG), intval($a->profile['profile_uid']));\r
268                         }\r
269 \r
270                         if (!empty($datequery)) {\r
271                                 $sql_extra2 .= Strings::protectSprintf(sprintf(" AND `thread`.`created` <= '%s' ", DBA::escape(DateTimeFormat::convert($datequery, 'UTC', date_default_timezone_get()))));\r
272                         }\r
273                         if (!empty($datequery2)) {\r
274                                 $sql_extra2 .= Strings::protectSprintf(sprintf(" AND `thread`.`created` >= '%s' ", DBA::escape(DateTimeFormat::convert($datequery2, 'UTC', date_default_timezone_get()))));\r
275                         }\r
276 \r
277                         // Does the profile page belong to a forum?\r
278                         // If not then we can improve the performance with an additional condition\r
279                         $condition = ['uid' => $a->profile['profile_uid'], 'page-flags' => [User::PAGE_FLAGS_COMMUNITY, User::PAGE_FLAGS_PRVGROUP]];\r
280                         if (!DBA::exists('user', $condition)) {\r
281                                 $sql_extra3 = sprintf(" AND `thread`.`contact-id` = %d ", intval(intval($a->profile['contact_id'])));\r
282                         } else {\r
283                                 $sql_extra3 = "";\r
284                         }\r
285 \r
286                         //  check if we serve a mobile device and get the user settings\r
287                         //  accordingly\r
288                         if ($a->is_mobile) {\r
289                                 $itemspage_network = PConfig::get(local_user(), 'system', 'itemspage_mobile_network', 10);\r
290                         } else {\r
291                                 $itemspage_network = PConfig::get(local_user(), 'system', 'itemspage_network', 20);\r
292                         }\r
293 \r
294                         //  now that we have the user settings, see if the theme forces\r
295                         //  a maximum item number which is lower then the user choice\r
296                         if (($a->force_max_items > 0) && ($a->force_max_items < $itemspage_network)) {\r
297                                 $itemspage_network = $a->force_max_items;\r
298                         }\r
299 \r
300                         $pager = new Pager($a->query_string, $itemspage_network);\r
301 \r
302                         $pager_sql = sprintf(" LIMIT %d, %d ", $pager->getStart(), $pager->getItemsPerPage());\r
303 \r
304                         $items_stmt = DBA::p(\r
305                                 "SELECT `item`.`uri`\r
306                                 FROM `thread`\r
307                                 STRAIGHT_JOIN `item` ON `item`.`id` = `thread`.`iid`\r
308                                 $sql_post_table\r
309                                 STRAIGHT_JOIN `contact`\r
310                                 ON `contact`.`id` = `thread`.`contact-id`\r
311                                         AND NOT `contact`.`blocked`\r
312                                         AND NOT `contact`.`pending`\r
313                                 WHERE `thread`.`uid` = ?\r
314                                         AND `thread`.`visible`\r
315                                         AND NOT `thread`.`deleted`\r
316                                         AND NOT `thread`.`moderated`\r
317                                         AND `thread`.`wall`\r
318                                         $sql_extra3\r
319                                         $sql_extra\r
320                                         $sql_extra2\r
321                                 ORDER BY `thread`.`created` DESC\r
322                                 $pager_sql",\r
323                                 $a->profile['profile_uid']\r
324                         );\r
325                 }\r
326 \r
327                 // Set a time stamp for this page. We will make use of it when we\r
328                 // search for new items (update routine)\r
329                 $_SESSION['last_updated'][$last_updated_key] = time();\r
330 \r
331                 if ($is_owner && !$update && !Config::get('theme', 'hide_eventlist')) {\r
332                         $o .= ProfileModel::getBirthdays();\r
333                         $o .= ProfileModel::getEventsReminderHTML();\r
334                 }\r
335 \r
336                 if ($is_owner) {\r
337                         $unseen = Item::exists(['wall' => true, 'unseen' => true, 'uid' => local_user()]);\r
338                         if ($unseen) {\r
339                                 Item::update(['unseen' => false], ['wall' => true, 'unseen' => true, 'uid' => local_user()]);\r
340                         }\r
341                 }\r
342 \r
343                 $items = DBA::toArray($items_stmt);\r
344 \r
345                 $o .= conversation($a, $items, $pager, 'profile', $update, false, 'created', $a->profile['profile_uid']);\r
346 \r
347                 if (!$update) {\r
348                         $o .= $pager->renderMinimal(count($items));\r
349                 }\r
350 \r
351                 return $o;\r
352         }\r
353 }\r