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.

503 lines
16 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. <?php
  2. use Friendica\App;
  3. require_once('include/dfrn.php');
  4. function display_init(App $a) {
  5. if ((get_config('system','block_public')) && (! local_user()) && (! remote_user())) {
  6. return;
  7. }
  8. $nick = (($a->argc > 1) ? $a->argv[1] : '');
  9. $profiledata = array();
  10. if ($a->argc == 3) {
  11. if (substr($a->argv[2], -5) == '.atom') {
  12. $item_id = substr($a->argv[2], 0, -5);
  13. $xml = dfrn::itemFeed($item_id);
  14. if ($xml == '') {
  15. http_status_exit(500);
  16. }
  17. header("Content-type: application/atom+xml");
  18. echo $xml;
  19. killme();
  20. }
  21. }
  22. // If there is only one parameter, then check if this parameter could be a guid
  23. if ($a->argc == 2) {
  24. $nick = "";
  25. $itemuid = 0;
  26. // Does the local user have this item?
  27. if (local_user()) {
  28. $r = dba::fetch_first("SELECT `id`, `parent`, `author-name`, `author-link`, `author-avatar`, `network`, `body`, `uid`, `owner-link` FROM `item`
  29. WHERE `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
  30. AND `guid` = ? AND `uid` = ? LIMIT 1", $a->argv[1], local_user());
  31. if (dbm::is_result($r)) {
  32. $nick = $a->user["nickname"];
  33. $itemuid = local_user();
  34. }
  35. }
  36. // Or is it anywhere on the server?
  37. if ($nick == "") {
  38. $r = dba::fetch_first("SELECT `user`.`nickname`, `item`.`id`, `item`.`parent`, `item`.`author-name`,
  39. `item`.`author-link`, `item`.`author-avatar`, `item`.`network`, `item`.`uid`, `item`.`owner-link`, `item`.`body`
  40. FROM `item` STRAIGHT_JOIN `user` ON `user`.`uid` = `item`.`uid`
  41. WHERE `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
  42. AND `item`.`allow_cid` = '' AND `item`.`allow_gid` = ''
  43. AND `item`.`deny_cid` = '' AND `item`.`deny_gid` = ''
  44. AND NOT `item`.`private` AND NOT `user`.`hidewall`
  45. AND `item`.`guid` = ? LIMIT 1", $a->argv[1]);
  46. if (dbm::is_result($r)) {
  47. $nick = $r["nickname"];
  48. $itemuid = $r["uid"];
  49. }
  50. }
  51. // Is it an item with uid=0?
  52. if ($nick == "") {
  53. $r = dba::fetch_first("SELECT `item`.`id`, `item`.`parent`, `item`.`author-name`, `item`.`author-link`,
  54. `item`.`author-avatar`, `item`.`network`, `item`.`uid`, `item`.`owner-link`, `item`.`body`
  55. FROM `item` WHERE `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
  56. AND `item`.`allow_cid` = '' AND `item`.`allow_gid` = ''
  57. AND `item`.`deny_cid` = '' AND `item`.`deny_gid` = ''
  58. AND NOT `item`.`private` AND `item`.`uid` = 0
  59. AND `item`.`guid` = ? LIMIT 1", $a->argv[1]);
  60. }
  61. if (dbm::is_result($r)) {
  62. if ($r["id"] != $r["parent"]) {
  63. $r = dba::fetch_first("SELECT `id`, `author-name`, `author-link`, `author-avatar`, `network`, `body`, `uid`, `owner-link` FROM `item`
  64. WHERE `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
  65. AND `id` = ?", $r["parent"]);
  66. }
  67. if (($itemuid != local_user()) && local_user()) {
  68. // Do we know this contact but we haven't got this item?
  69. // Copy the wohle thread to our local storage so that we can interact.
  70. // We really should change this need for the future since it scales very bad.
  71. $contactid = get_contact($r['owner-link'], local_user());
  72. if ($contactid) {
  73. $items = dba::p("SELECT * FROM `item` WHERE `parent` = ? ORDER BY `id`", $r["id"]);
  74. while ($item = self::fetch($items)) {
  75. $itemcontactid = get_contact($item['owner-link'], local_user());
  76. if (!$itemcontactid) {
  77. $itemcontactid = $contactid;
  78. }
  79. unset($item['id']);
  80. $item['uid'] = local_user();
  81. $item['origin'] = 0;
  82. $item['contact-id'] = $itemcontactid;
  83. $local_copy = item_store($item, false, false, true);
  84. logger("Stored local copy for post ".$item['guid']." under id ".$local_copy, LOGGER_DEBUG);
  85. }
  86. dba::close($items);
  87. }
  88. }
  89. $profiledata = display_fetchauthor($a, $r);
  90. if (strstr(normalise_link($profiledata["url"]), normalise_link(App::get_baseurl()))) {
  91. $nickname = str_replace(normalise_link(App::get_baseurl())."/profile/", "", normalise_link($profiledata["url"]));
  92. if (($nickname != $a->user["nickname"])) {
  93. $r = dba::fetch_first("SELECT `profile`.`uid` AS `profile_uid`, `profile`.* , `contact`.`avatar-date` AS picdate, `user`.* FROM `profile`
  94. INNER JOIN `contact` on `contact`.`uid` = `profile`.`uid` INNER JOIN `user` ON `profile`.`uid` = `user`.`uid`
  95. WHERE `user`.`nickname` = ? AND `profile`.`is-default` AND `contact`.`self` LIMIT 1",
  96. $nickname
  97. );
  98. if (dbm::is_result($r)) {
  99. $profiledata = $r;
  100. }
  101. $profiledata["network"] = NETWORK_DFRN;
  102. } else {
  103. $profiledata = array();
  104. }
  105. }
  106. } else {
  107. $a->error = 404;
  108. notice(t('Item not found.') . EOL);
  109. return;
  110. }
  111. }
  112. profile_load($a, $nick, 0, $profiledata);
  113. }
  114. function display_fetchauthor($a, $item) {
  115. require_once("include/Contact.php");
  116. $profiledata = array();
  117. $profiledata["uid"] = -1;
  118. $profiledata["nickname"] = $item["author-name"];
  119. $profiledata["name"] = $item["author-name"];
  120. $profiledata["picdate"] = "";
  121. $profiledata["photo"] = $item["author-avatar"];
  122. $profiledata["url"] = $item["author-link"];
  123. $profiledata["network"] = $item["network"];
  124. // Check for a repeated message
  125. $skip = false;
  126. $body = trim($item["body"]);
  127. // Skip if it isn't a pure repeated messages
  128. // Does it start with a share?
  129. if (!$skip && strpos($body, "[share") > 0) {
  130. $skip = true;
  131. }
  132. // Does it end with a share?
  133. if (!$skip && (strlen($body) > (strrpos($body, "[/share]") + 8))) {
  134. $skip = true;
  135. }
  136. if (!$skip) {
  137. $attributes = preg_replace("/\[share(.*?)\]\s?(.*?)\s?\[\/share\]\s?/ism","$1",$body);
  138. // Skip if there is no shared message in there
  139. if ($body == $attributes) {
  140. $skip = true;
  141. }
  142. }
  143. if (!$skip) {
  144. $author = "";
  145. preg_match("/author='(.*?)'/ism", $attributes, $matches);
  146. if ($matches[1] != "") {
  147. $profiledata["name"] = html_entity_decode($matches[1],ENT_QUOTES,'UTF-8');
  148. }
  149. preg_match('/author="(.*?)"/ism', $attributes, $matches);
  150. if ($matches[1] != "") {
  151. $profiledata["name"] = html_entity_decode($matches[1],ENT_QUOTES,'UTF-8');
  152. }
  153. $profile = "";
  154. preg_match("/profile='(.*?)'/ism", $attributes, $matches);
  155. if ($matches[1] != "") {
  156. $profiledata["url"] = $matches[1];
  157. }
  158. preg_match('/profile="(.*?)"/ism', $attributes, $matches);
  159. if ($matches[1] != "") {
  160. $profiledata["url"] = $matches[1];
  161. }
  162. $avatar = "";
  163. preg_match("/avatar='(.*?)'/ism", $attributes, $matches);
  164. if ($matches[1] != "") {
  165. $profiledata["photo"] = $matches[1];
  166. }
  167. preg_match('/avatar="(.*?)"/ism', $attributes, $matches);
  168. if ($matches[1] != "") {
  169. $profiledata["photo"] = $matches[1];
  170. }
  171. $profiledata["nickname"] = $profiledata["name"];
  172. $profiledata["network"] = GetProfileUsername($profiledata["url"], "", false, true);
  173. $profiledata["address"] = "";
  174. $profiledata["about"] = "";
  175. }
  176. $profiledata = get_contact_details_by_url($profiledata["url"], local_user(), $profiledata);
  177. $profiledata["photo"] = App::remove_baseurl($profiledata["photo"]);
  178. if (local_user()) {
  179. if (in_array($profiledata["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS))) {
  180. $profiledata["remoteconnect"] = App::get_baseurl()."/follow?url=".urlencode($profiledata["url"]);
  181. }
  182. } elseif ($profiledata["network"] == NETWORK_DFRN) {
  183. $connect = str_replace("/profile/", "/dfrn_request/", $profiledata["url"]);
  184. $profiledata["remoteconnect"] = $connect;
  185. }
  186. return($profiledata);
  187. }
  188. function display_content(App $a, $update = 0) {
  189. if ((get_config('system','block_public')) && (! local_user()) && (! remote_user())) {
  190. notice(t('Public access denied.') . EOL);
  191. return;
  192. }
  193. require_once('include/security.php');
  194. require_once('include/conversation.php');
  195. require_once('include/acl_selectors.php');
  196. $o = '';
  197. if ($update) {
  198. $nick = $_REQUEST['nick'];
  199. } else {
  200. $nick = (($a->argc > 1) ? $a->argv[1] : '');
  201. }
  202. if ($update) {
  203. $item_id = $_REQUEST['item_id'];
  204. $a->profile = array('uid' => intval($update), 'profile_uid' => intval($update));
  205. } else {
  206. $item_id = (($a->argc > 2) ? $a->argv[2] : 0);
  207. if ($a->argc == 2) {
  208. $nick = "";
  209. if (local_user()) {
  210. $r = dba::fetch_first("SELECT `id` FROM `item`
  211. WHERE `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
  212. AND `guid` = ? AND `uid` = ?", $a->argv[1], local_user());
  213. if (dbm::is_result($r)) {
  214. $item_id = $r["id"];
  215. $nick = $a->user["nickname"];
  216. }
  217. }
  218. if ($nick == "") {
  219. $r = dba::fetch_first("SELECT `user`.`nickname`, `item`.`id` FROM `item` STRAIGHT_JOIN `user` ON `user`.`uid` = `item`.`uid`
  220. WHERE `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
  221. AND `item`.`allow_cid` = '' AND `item`.`allow_gid` = ''
  222. AND `item`.`deny_cid` = '' AND `item`.`deny_gid` = ''
  223. AND NOT `item`.`private` AND NOT `user`.`hidewall`
  224. AND `item`.`guid` = ?", $a->argv[1]);
  225. if (dbm::is_result($r)) {
  226. $item_id = $r["id"];
  227. $nick = $r["nickname"];
  228. }
  229. }
  230. if ($nick == "") {
  231. $r = dba::fetch_first("SELECT `item`.`id` FROM `item`
  232. WHERE `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
  233. AND `item`.`allow_cid` = '' AND `item`.`allow_gid` = ''
  234. AND `item`.`deny_cid` = '' AND `item`.`deny_gid` = ''
  235. AND NOT `item`.`private` AND `item`.`uid` = 0
  236. AND `item`.`guid` = ?", $a->argv[1]);
  237. if (dbm::is_result($r)) {
  238. $item_id = $r["id"];
  239. }
  240. }
  241. }
  242. }
  243. if ($item_id && !is_numeric($item_id)) {
  244. $r = dba::select('item', array('id'), array('uri' => $item_id, 'uid' => $a->profile['uid']), array('limit' => 1));
  245. if (dbm::is_result($r)) {
  246. $item_id = $r["id"];
  247. } else {
  248. $item_id = false;
  249. }
  250. }
  251. if (!$item_id) {
  252. $a->error = 404;
  253. notice(t('Item not found.').EOL);
  254. return;
  255. }
  256. // We are displaying an "alternate" link if that post was public. See issue 2864
  257. $is_public = dba::exists('item', array('id' => $item_id, 'private' => false, 'wall' => true));
  258. if ($is_public) {
  259. $alternate = App::get_baseurl().'/display/'.$nick.'/'.$item_id.'.atom';
  260. } else {
  261. $alternate = '';
  262. }
  263. $a->page['htmlhead'] .= replace_macros(get_markup_template('display-head.tpl'),
  264. array('$alternate' => $alternate));
  265. $groups = array();
  266. $contact = null;
  267. $remote_contact = false;
  268. $contact_id = 0;
  269. if (is_array($_SESSION['remote'])) {
  270. foreach ($_SESSION['remote'] as $v) {
  271. if ($v['uid'] == $a->profile['uid']) {
  272. $contact_id = $v['cid'];
  273. break;
  274. }
  275. }
  276. }
  277. if ($contact_id) {
  278. $groups = init_groups_visitor($contact_id);
  279. $r = dba::fetch_first("SELECT * FROM `contact` WHERE `id` = ? AND `uid` = ? LIMIT 1",
  280. $contact_id,
  281. $a->profile['uid']
  282. );
  283. if (dbm::is_result($r)) {
  284. $contact = $r;
  285. $remote_contact = true;
  286. }
  287. }
  288. if (!$remote_contact) {
  289. if (local_user()) {
  290. $contact_id = $_SESSION['cid'];
  291. $contact = $a->contact;
  292. }
  293. }
  294. $r = dba::fetch_first("SELECT * FROM `contact` WHERE `uid` = ? AND `self` LIMIT 1", $a->profile['uid']);
  295. if (dbm::is_result($r)) {
  296. $a->page_contact = $r;
  297. }
  298. $is_owner = ((local_user()) && (local_user() == $a->profile['profile_uid']) ? true : false);
  299. if ($a->profile['hidewall'] && (! $is_owner) && (! $remote_contact)) {
  300. notice(t('Access to this profile has been restricted.') . EOL);
  301. return;
  302. }
  303. // We need the editor here to be able to reshare an item.
  304. if ($is_owner) {
  305. $x = array(
  306. 'is_owner' => true,
  307. 'allow_location' => $a->user['allow_location'],
  308. 'default_location' => $a->user['default-location'],
  309. 'nickname' => $a->user['nickname'],
  310. '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'),
  311. 'acl' => populate_acl($a->user, true),
  312. 'bang' => '',
  313. 'visitor' => 'block',
  314. 'profile_uid' => local_user(),
  315. 'acl_data' => construct_acl_data($a, $a->user), // For non-Javascript ACL selector
  316. );
  317. $o .= status_editor($a,$x,0,true);
  318. }
  319. $sql_extra = item_permissions_sql($a->profile['uid'],$remote_contact,$groups);
  320. if ($update) {
  321. $r = dba::p("SELECT `id` FROM `item` WHERE `item`.`uid` = ?
  322. AND `item`.`parent` = (SELECT `parent` FROM `item` WHERE `id` = ?)
  323. $sql_extra AND `unseen`",
  324. $a->profile['uid'],
  325. $item_id
  326. );
  327. if (dba::num_rows($r) == 0) {
  328. return '';
  329. }
  330. }
  331. $r = dba::p(item_query()." AND `item`.`uid` = ?
  332. AND `item`.`parent` = (SELECT `parent` FROM `item` WHERE `id` = ?)
  333. $sql_extra
  334. ORDER BY `parent` DESC, `gravity` ASC, `id` ASC",
  335. $a->profile['uid'],
  336. $item_id
  337. );
  338. if (!dbm::is_result($r) && local_user()) {
  339. // Check if this is another person's link to a post that we have
  340. $r = dba::fetch_first("SELECT `item`.uri FROM `item`
  341. WHERE (`item`.`id` = ? OR `item`.`uri` = ?)
  342. LIMIT 1",
  343. $item_id,
  344. $item_id
  345. );
  346. if (dbm::is_result($r)) {
  347. $item_uri = $r['uri'];
  348. $r = dba::p(item_query()." AND `item`.`uid` = ?
  349. AND `item`.`parent` = (SELECT `parent` FROM `item` WHERE `uri` = ? AND uid = ?)
  350. ORDER BY `parent` DESC, `gravity` ASC, `id` ASC",
  351. local_user(),
  352. $item_uri,
  353. local_user()
  354. );
  355. }
  356. }
  357. if (dbm::is_result($r)) {
  358. $s = dba::inArray($r);
  359. if ((local_user()) && (local_user() == $a->profile['uid'])) {
  360. $unseen = dba::select('item', array('id'), array('parent' => $s[0]['parent'], 'unseen' => true), array('limit' => 1));
  361. if (dbm::is_result($unseen)) {
  362. dba::update('item', array('unseen' => false), array('parent' => $s[0]['parent'], 'unseen' => true));
  363. }
  364. }
  365. $items = conv_sort($s, "`commented`");
  366. if (!$update) {
  367. $o .= "<script> var netargs = '?f=&nick=" . $nick . "&item_id=" . $item_id . "'; </script>";
  368. }
  369. $o .= conversation($a, $items, 'display', $update);
  370. // Preparing the meta header
  371. require_once('include/bbcode.php');
  372. require_once("include/html2plain.php");
  373. $description = trim(html2plain(bbcode($s[0]["body"], false, false), 0, true));
  374. $title = trim(html2plain(bbcode($s[0]["title"], false, false), 0, true));
  375. $author_name = $s[0]["author-name"];
  376. $image = $a->remove_baseurl($s[0]["author-thumb"]);
  377. if ($title == "") {
  378. $title = $author_name;
  379. }
  380. // Limit the description to 160 characters
  381. if (strlen($description) > 160) {
  382. $description = substr($description, 0, 157) . '...';
  383. }
  384. $description = htmlspecialchars($description, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
  385. $title = htmlspecialchars($title, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
  386. $author_name = htmlspecialchars($author_name, ENT_COMPAT, 'UTF-8', true); // allow double encoding here
  387. //<meta name="keywords" content="">
  388. $a->page['htmlhead'] .= '<meta name="author" content="'.$author_name.'" />'."\n";
  389. $a->page['htmlhead'] .= '<meta name="title" content="'.$title.'" />'."\n";
  390. $a->page['htmlhead'] .= '<meta name="fulltitle" content="'.$title.'" />'."\n";
  391. $a->page['htmlhead'] .= '<meta name="description" content="'.$description.'" />'."\n";
  392. // Schema.org microdata
  393. $a->page['htmlhead'] .= '<meta itemprop="name" content="'.$title.'" />'."\n";
  394. $a->page['htmlhead'] .= '<meta itemprop="description" content="'.$description.'" />'."\n";
  395. $a->page['htmlhead'] .= '<meta itemprop="image" content="'.$image.'" />'."\n";
  396. $a->page['htmlhead'] .= '<meta itemprop="author" content="'.$author_name.'" />'."\n";
  397. // Twitter cards
  398. $a->page['htmlhead'] .= '<meta name="twitter:card" content="summary" />'."\n";
  399. $a->page['htmlhead'] .= '<meta name="twitter:title" content="'.$title.'" />'."\n";
  400. $a->page['htmlhead'] .= '<meta name="twitter:description" content="'.$description.'" />'."\n";
  401. $a->page['htmlhead'] .= '<meta name="twitter:image" content="'.$image.'" />'."\n";
  402. $a->page['htmlhead'] .= '<meta name="twitter:url" content="'.$s[0]["plink"].'" />'."\n";
  403. // Dublin Core
  404. $a->page['htmlhead'] .= '<meta name="DC.title" content="'.$title.'" />'."\n";
  405. $a->page['htmlhead'] .= '<meta name="DC.description" content="'.$description.'" />'."\n";
  406. // Open Graph
  407. $a->page['htmlhead'] .= '<meta property="og:type" content="website" />'."\n";
  408. $a->page['htmlhead'] .= '<meta property="og:title" content="'.$title.'" />'."\n";
  409. $a->page['htmlhead'] .= '<meta property="og:image" content="'.$image.'" />'."\n";
  410. $a->page['htmlhead'] .= '<meta property="og:url" content="'.$s[0]["plink"].'" />'."\n";
  411. $a->page['htmlhead'] .= '<meta property="og:description" content="'.$description.'" />'."\n";
  412. $a->page['htmlhead'] .= '<meta name="og:article:author" content="'.$author_name.'" />'."\n";
  413. // article:tag
  414. return $o;
  415. }
  416. $r = dba::fetch_first("SELECT `id`,`deleted` FROM `item` WHERE `id` = ? OR `uri` = ? LIMIT 1",
  417. $item_id,
  418. $item_id
  419. );
  420. if ($r) {
  421. if ($r['deleted']) {
  422. notice(t('Item has been removed.') . EOL);
  423. } else {
  424. notice(t('Permission denied.') . EOL);
  425. }
  426. } else {
  427. notice(t('Item not found.') . EOL);
  428. }
  429. return $o;
  430. }