Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

489 líneas
16KB

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