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.
 
 
 
 
 
 

498 lines
16 KiB

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