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.
 
 
 
 
 
 

364 line
14 KiB

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