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