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