Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

334 lines
12KB

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