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.

431 lines
14KB

  1. <?php
  2. /**
  3. * @file mod/display.php
  4. */
  5. use Friendica\App;
  6. use Friendica\Content\Pager;
  7. use Friendica\Content\Text\BBCode;
  8. use Friendica\Content\Text\HTML;
  9. use Friendica\Core\ACL;
  10. use Friendica\Core\Config;
  11. use Friendica\Core\L10n;
  12. use Friendica\Core\Logger;
  13. use Friendica\Core\Protocol;
  14. use Friendica\Core\Renderer;
  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\Protocol\ActivityPub;
  22. use Friendica\Protocol\DFRN;
  23. use Friendica\Util\Strings;
  24. use Friendica\Module\Objects;
  25. function display_init(App $a)
  26. {
  27. if (ActivityPub::isRequest()) {
  28. Objects::rawContent();
  29. }
  30. if (Config::get('system', 'block_public') && !local_user() && !remote_user()) {
  31. return;
  32. }
  33. $nick = (($a->argc > 1) ? $a->argv[1] : '');
  34. $profiledata = [];
  35. if ($a->argc == 3) {
  36. if (substr($a->argv[2], -5) == '.atom') {
  37. $item_id = substr($a->argv[2], 0, -5);
  38. displayShowFeed($item_id, false);
  39. }
  40. }
  41. if ($a->argc == 4) {
  42. if ($a->argv[3] == 'conversation.atom') {
  43. $item_id = $a->argv[2];
  44. displayShowFeed($item_id, true);
  45. }
  46. }
  47. $item = null;
  48. $item_user = local_user();
  49. $fields = ['id', 'parent', 'author-id', 'body', 'uid', 'guid'];
  50. // If there is only one parameter, then check if this parameter could be a guid
  51. if ($a->argc == 2) {
  52. $nick = "";
  53. // Does the local user have this item?
  54. if (local_user()) {
  55. $item = Item::selectFirstForUser(local_user(), $fields, ['guid' => $a->argv[1], 'uid' => local_user()]);
  56. if (DBA::isResult($item)) {
  57. $nick = $a->user["nickname"];
  58. }
  59. // Is this item private but could be visible to the remove visitor?
  60. } elseif (remote_user()) {
  61. $item = Item::selectFirst($fields, ['guid' => $a->argv[1], 'private' => 1]);
  62. if (DBA::isResult($item)) {
  63. if (!Contact::isFollower(remote_user(), $item['uid'])) {
  64. $item = null;
  65. } else {
  66. $item_user = $item['uid'];
  67. }
  68. }
  69. }
  70. // Is it an item with uid=0?
  71. if (!DBA::isResult($item)) {
  72. $item = Item::selectFirstForUser(local_user(), $fields, ['guid' => $a->argv[1], 'private' => [0, 2], 'uid' => 0]);
  73. }
  74. } elseif (($a->argc == 3) && ($nick == 'feed-item')) {
  75. $item = Item::selectFirstForUser(local_user(), $fields, ['id' => $a->argv[2], 'private' => [0, 2], 'uid' => 0]);
  76. }
  77. if (!DBA::isResult($item)) {
  78. System::httpExit(404);
  79. }
  80. if (!empty($_SERVER['HTTP_ACCEPT']) && strstr($_SERVER['HTTP_ACCEPT'], 'application/atom+xml')) {
  81. Logger::log('Directly serving XML for id '.$item["id"], Logger::DEBUG);
  82. displayShowFeed($item["id"], false);
  83. }
  84. if ($item["id"] != $item["parent"]) {
  85. $item = Item::selectFirstForUser(local_user(), $fields, ['id' => $item["parent"]]);
  86. }
  87. $profiledata = display_fetchauthor($a, $item);
  88. if (strstr(Strings::normaliseLink($profiledata["url"]), Strings::normaliseLink(System::baseUrl()))) {
  89. $nickname = str_replace(Strings::normaliseLink(System::baseUrl())."/profile/", "", Strings::normaliseLink($profiledata["url"]));
  90. if (($nickname != $a->user["nickname"])) {
  91. $profile = DBA::fetchFirst("SELECT `profile`.`uid` AS `profile_uid`, `profile`.* , `contact`.`avatar-date` AS picdate, `user`.* FROM `profile`
  92. INNER JOIN `contact` on `contact`.`uid` = `profile`.`uid` INNER JOIN `user` ON `profile`.`uid` = `user`.`uid`
  93. WHERE `user`.`nickname` = ? AND `profile`.`is-default` AND `contact`.`self` LIMIT 1",
  94. $nickname
  95. );
  96. if (DBA::isResult($profile)) {
  97. $profiledata = $profile;
  98. }
  99. $profiledata["network"] = Protocol::DFRN;
  100. } else {
  101. $profiledata = [];
  102. }
  103. }
  104. Profile::load($a, $nick, 0, $profiledata);
  105. }
  106. function display_fetchauthor($a, $item)
  107. {
  108. $author = DBA::selectFirst('contact', ['name', 'nick', 'photo', 'network', 'url'], ['id' => $item['author-id']]);
  109. $profiledata = [];
  110. $profiledata['uid'] = -1;
  111. $profiledata['nickname'] = $author['nick'];
  112. $profiledata['name'] = $author['name'];
  113. $profiledata['picdate'] = '';
  114. $profiledata['photo'] = $author['photo'];
  115. $profiledata['url'] = $author['url'];
  116. $profiledata['network'] = $author['network'];
  117. // Check for a repeated message
  118. $skip = false;
  119. $body = trim($item["body"]);
  120. // Skip if it isn't a pure repeated messages
  121. // Does it start with a share?
  122. if (!$skip && strpos($body, "[share") > 0) {
  123. $skip = true;
  124. }
  125. // Does it end with a share?
  126. if (!$skip && (strlen($body) > (strrpos($body, "[/share]") + 8))) {
  127. $skip = true;
  128. }
  129. if (!$skip) {
  130. $attributes = preg_replace("/\[share(.*?)\]\s?(.*?)\s?\[\/share\]\s?/ism","$1",$body);
  131. // Skip if there is no shared message in there
  132. if ($body == $attributes) {
  133. $skip = true;
  134. }
  135. }
  136. if (!$skip) {
  137. $author = "";
  138. preg_match("/author='(.*?)'/ism", $attributes, $matches);
  139. if (!empty($matches[1])) {
  140. $profiledata["name"] = html_entity_decode($matches[1],ENT_QUOTES,'UTF-8');
  141. }
  142. preg_match('/author="(.*?)"/ism', $attributes, $matches);
  143. if (!empty($matches[1])) {
  144. $profiledata["name"] = html_entity_decode($matches[1],ENT_QUOTES,'UTF-8');
  145. }
  146. $profile = "";
  147. preg_match("/profile='(.*?)'/ism", $attributes, $matches);
  148. if (!empty($matches[1])) {
  149. $profiledata["url"] = $matches[1];
  150. }
  151. preg_match('/profile="(.*?)"/ism', $attributes, $matches);
  152. if (!empty($matches[1])) {
  153. $profiledata["url"] = $matches[1];
  154. }
  155. $avatar = "";
  156. preg_match("/avatar='(.*?)'/ism", $attributes, $matches);
  157. if (!empty($matches[1])) {
  158. $profiledata["photo"] = $matches[1];
  159. }
  160. preg_match('/avatar="(.*?)"/ism', $attributes, $matches);
  161. if (!empty($matches[1])) {
  162. $profiledata["photo"] = $matches[1];
  163. }
  164. $profiledata["nickname"] = $profiledata["name"];
  165. $profiledata["network"] = Protocol::matchByProfileUrl($profiledata["url"]);
  166. $profiledata["address"] = "";
  167. $profiledata["about"] = "";
  168. }
  169. $profiledata = Contact::getDetailsByURL($profiledata["url"], local_user(), $profiledata);
  170. $profiledata["photo"] = System::removedBaseUrl($profiledata["photo"]);
  171. if (local_user()) {
  172. if (in_array($profiledata["network"], [Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS])) {
  173. $profiledata["remoteconnect"] = System::baseUrl()."/follow?url=".urlencode($profiledata["url"]);
  174. }
  175. } elseif ($profiledata["network"] == Protocol::DFRN) {
  176. $connect = str_replace("/profile/", "/dfrn_request/", $profiledata["url"]);
  177. $profiledata["remoteconnect"] = $connect;
  178. }
  179. return($profiledata);
  180. }
  181. function display_content(App $a, $update = false, $update_uid = 0)
  182. {
  183. if (Config::get('system','block_public') && !local_user() && !remote_user()) {
  184. notice(L10n::t('Public access denied.') . EOL);
  185. return;
  186. }
  187. $o = '';
  188. if ($update) {
  189. $item_id = $_REQUEST['item_id'];
  190. $item = Item::selectFirst(['uid', 'parent', 'parent-uri'], ['id' => $item_id]);
  191. if ($item['uid'] != 0) {
  192. $a->profile = ['uid' => intval($item['uid']), 'profile_uid' => intval($item['uid'])];
  193. } else {
  194. $a->profile = ['uid' => intval($update_uid), 'profile_uid' => intval($update_uid)];
  195. }
  196. $item_parent = $item['parent'];
  197. $item_parent_uri = $item['parent-uri'];
  198. } else {
  199. $item_id = (($a->argc > 2) ? $a->argv[2] : 0);
  200. $item_parent = $item_id;
  201. if ($a->argc == 2) {
  202. $item_parent = 0;
  203. $fields = ['id', 'parent', 'parent-uri', 'uid'];
  204. if (local_user()) {
  205. $condition = ['guid' => $a->argv[1], 'uid' => local_user()];
  206. $item = Item::selectFirstForUser(local_user(), $fields, $condition);
  207. if (DBA::isResult($item)) {
  208. $item_id = $item["id"];
  209. $item_parent = $item["parent"];
  210. $item_parent_uri = $item['parent-uri'];
  211. }
  212. } elseif (remote_user()) {
  213. $item = Item::selectFirst($fields, ['guid' => $a->argv[1], 'private' => 1]);
  214. if (DBA::isResult($item) && Contact::isFollower(remote_user(), $item['uid'])) {
  215. $item_id = $item["id"];
  216. $item_parent = $item["parent"];
  217. $item_parent_uri = $item['parent-uri'];
  218. }
  219. }
  220. if ($item_parent == 0) {
  221. $condition = ['private' => [0, 2], 'guid' => $a->argv[1], 'uid' => 0];
  222. $item = Item::selectFirstForUser(local_user(), $fields, $condition);
  223. if (DBA::isResult($item)) {
  224. $item_id = $item["id"];
  225. $item_parent = $item["parent"];
  226. $item_parent_uri = $item['parent-uri'];
  227. }
  228. }
  229. }
  230. }
  231. if (!$item_id) {
  232. System::httpExit(404);
  233. }
  234. // We are displaying an "alternate" link if that post was public. See issue 2864
  235. $is_public = Item::exists(['id' => $item_id, 'private' => [0, 2]]);
  236. if ($is_public) {
  237. // For the atom feed the nickname doesn't matter at all, we only need the item id.
  238. $alternate = System::baseUrl().'/display/feed-item/'.$item_id.'.atom';
  239. $conversation = System::baseUrl().'/display/feed-item/'.$item_parent.'/conversation.atom';
  240. } else {
  241. $alternate = '';
  242. $conversation = '';
  243. }
  244. $a->page['htmlhead'] .= Renderer::replaceMacros(Renderer::getMarkupTemplate('display-head.tpl'),
  245. ['$alternate' => $alternate,
  246. '$conversation' => $conversation]);
  247. $groups = [];
  248. $remote_cid = null;
  249. $is_remote_contact = false;
  250. $item_uid = local_user();
  251. $parent = Item::selectFirst(['uid'], ['uri' => $item_parent_uri, 'wall' => true]);
  252. if (DBA::isResult($parent)) {
  253. $a->profile['uid'] = $parent['uid'];
  254. $a->profile['profile_uid'] = $parent['uid'];
  255. $is_remote_contact = Contact::isFollower(remote_user(), $a->profile['profile_uid']);
  256. }
  257. if ($is_remote_contact) {
  258. $cdata = Contact::getPublicAndUserContacID(remote_user(), $a->profile['profile_uid']);
  259. if (!empty($cdata['user'])) {
  260. $groups = Group::getIdsByContactId($cdata['user']);
  261. $remote_cid = $cdata['user'];
  262. $item_uid = $parent['uid'];
  263. }
  264. }
  265. $page_contact = DBA::selectFirst('contact', [], ['self' => true, 'uid' => $a->profile['uid']]);
  266. if (DBA::isResult($page_contact)) {
  267. $a->page_contact = $page_contact;
  268. }
  269. $is_owner = (local_user() && (in_array($a->profile['profile_uid'], [local_user(), 0])) ? true : false);
  270. if (!empty($a->profile['hidewall']) && !$is_owner && !$is_remote_contact) {
  271. notice(L10n::t('Access to this profile has been restricted.') . EOL);
  272. return;
  273. }
  274. // We need the editor here to be able to reshare an item.
  275. if ($is_owner) {
  276. $x = [
  277. 'is_owner' => true,
  278. 'allow_location' => $a->user['allow_location'],
  279. 'default_location' => $a->user['default-location'],
  280. 'nickname' => $a->user['nickname'],
  281. 'lockstate' => (is_array($a->user) && (strlen($a->user['allow_cid']) || strlen($a->user['allow_gid']) || strlen($a->user['deny_cid']) || strlen($a->user['deny_gid'])) ? 'lock' : 'unlock'),
  282. 'acl' => ACL::getFullSelectorHTML($a->user, true),
  283. 'bang' => '',
  284. 'visitor' => 'block',
  285. 'profile_uid' => local_user(),
  286. ];
  287. $o .= status_editor($a, $x, 0, true);
  288. }
  289. $sql_extra = Item::getPermissionsSQLByUserId($a->profile['profile_uid'], $is_remote_contact, $groups, $remote_cid);
  290. if (local_user() && (local_user() == $a->profile['profile_uid'])) {
  291. $condition = ['parent-uri' => $item_parent_uri, 'uid' => local_user(), 'unseen' => true];
  292. $unseen = Item::exists($condition);
  293. } else {
  294. $unseen = false;
  295. }
  296. if ($update && !$unseen) {
  297. return '';
  298. }
  299. $condition = ["`id` = ? AND `item`.`uid` IN (0, ?) " . $sql_extra, $item_id, $item_uid];
  300. $fields = ['parent-uri', 'body', 'title', 'author-name', 'author-avatar', 'plink'];
  301. $item = Item::selectFirstForUser(local_user(), $fields, $condition);
  302. if (!DBA::isResult($item)) {
  303. System::httpExit(404);
  304. }
  305. $item['uri'] = $item['parent-uri'];
  306. if ($unseen) {
  307. $condition = ['parent-uri' => $item_parent_uri, 'uid' => local_user(), 'unseen' => true];
  308. Item::update(['unseen' => false], $condition);
  309. }
  310. if (!$update) {
  311. $o .= "<script> var netargs = '?f=&item_id=" . $item_id . "'; </script>";
  312. }
  313. $o .= conversation($a, [$item], new Pager($a->query_string), 'display', $update_uid, false, 'commented', $item_uid);
  314. // Preparing the meta header
  315. $description = trim(HTML::toPlaintext(BBCode::convert($item["body"], false), 0, true));
  316. $title = trim(HTML::toPlaintext(BBCode::convert($item["title"], false), 0, true));
  317. $author_name = $item["author-name"];
  318. $image = $a->removeBaseURL($item["author-avatar"]);
  319. if ($title == "") {
  320. $title = $author_name;
  321. }
  322. // Limit the description to 160 characters
  323. if (strlen($description) > 160) {
  324. $description = substr($description, 0, 157) . '...';
  325. }
  326. $description = htmlspecialchars($description, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
  327. $title = htmlspecialchars($title, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
  328. $author_name = htmlspecialchars($author_name, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
  329. //<meta name="keywords" content="">
  330. $a->page['htmlhead'] .= '<meta name="author" content="'.$author_name.'" />'."\n";
  331. $a->page['htmlhead'] .= '<meta name="title" content="'.$title.'" />'."\n";
  332. $a->page['htmlhead'] .= '<meta name="fulltitle" content="'.$title.'" />'."\n";
  333. $a->page['htmlhead'] .= '<meta name="description" content="'.$description.'" />'."\n";
  334. // Schema.org microdata
  335. $a->page['htmlhead'] .= '<meta itemprop="name" content="'.$title.'" />'."\n";
  336. $a->page['htmlhead'] .= '<meta itemprop="description" content="'.$description.'" />'."\n";
  337. $a->page['htmlhead'] .= '<meta itemprop="image" content="'.$image.'" />'."\n";
  338. $a->page['htmlhead'] .= '<meta itemprop="author" content="'.$author_name.'" />'."\n";
  339. // Twitter cards
  340. $a->page['htmlhead'] .= '<meta name="twitter:card" content="summary" />'."\n";
  341. $a->page['htmlhead'] .= '<meta name="twitter:title" content="'.$title.'" />'."\n";
  342. $a->page['htmlhead'] .= '<meta name="twitter:description" content="'.$description.'" />'."\n";
  343. $a->page['htmlhead'] .= '<meta name="twitter:image" content="'.System::baseUrl().'/'.$image.'" />'."\n";
  344. $a->page['htmlhead'] .= '<meta name="twitter:url" content="'.$item["plink"].'" />'."\n";
  345. // Dublin Core
  346. $a->page['htmlhead'] .= '<meta name="DC.title" content="'.$title.'" />'."\n";
  347. $a->page['htmlhead'] .= '<meta name="DC.description" content="'.$description.'" />'."\n";
  348. // Open Graph
  349. $a->page['htmlhead'] .= '<meta property="og:type" content="website" />'."\n";
  350. $a->page['htmlhead'] .= '<meta property="og:title" content="'.$title.'" />'."\n";
  351. $a->page['htmlhead'] .= '<meta property="og:image" content="'.System::baseUrl().'/'.$image.'" />'."\n";
  352. $a->page['htmlhead'] .= '<meta property="og:url" content="'.$item["plink"].'" />'."\n";
  353. $a->page['htmlhead'] .= '<meta property="og:description" content="'.$description.'" />'."\n";
  354. $a->page['htmlhead'] .= '<meta name="og:article:author" content="'.$author_name.'" />'."\n";
  355. // article:tag
  356. return $o;
  357. }
  358. function displayShowFeed($item_id, $conversation)
  359. {
  360. $xml = DFRN::itemFeed($item_id, $conversation);
  361. if ($xml == '') {
  362. System::httpExit(500);
  363. }
  364. header("Content-type: application/atom+xml");
  365. echo $xml;
  366. killme();
  367. }