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.
 
 
 
 
 
 

1660 lines
49 KiB

  1. <?php
  2. /**
  3. * @file include/conversation.php
  4. */
  5. use Friendica\App;
  6. use Friendica\Core\Config;
  7. use Friendica\Core\PConfig;
  8. use Friendica\Core\System;
  9. use Friendica\Database\DBM;
  10. use Friendica\Object\Conversation;
  11. use Friendica\Object\Item;
  12. require_once "include/bbcode.php";
  13. require_once "include/acl_selectors.php";
  14. /*
  15. * Note: the code in 'item_extract_images' and 'item_redir_and_replace_images'
  16. * is identical to the code in mod/message.php for 'item_extract_images' and
  17. * 'item_redir_and_replace_images'
  18. */
  19. if (! function_exists('item_extract_images')) {
  20. function item_extract_images($body) {
  21. $saved_image = array();
  22. $orig_body = $body;
  23. $new_body = '';
  24. $cnt = 0;
  25. $img_start = strpos($orig_body, '[img');
  26. $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
  27. $img_end = ($img_start !== false ? strpos(substr($orig_body, $img_start), '[/img]') : false);
  28. while (($img_st_close !== false) && ($img_end !== false)) {
  29. $img_st_close++; // make it point to AFTER the closing bracket
  30. $img_end += $img_start;
  31. if (! strcmp(substr($orig_body, $img_start + $img_st_close, 5), 'data:')) {
  32. // This is an embedded image
  33. $saved_image[$cnt] = substr($orig_body, $img_start + $img_st_close, $img_end - ($img_start + $img_st_close));
  34. $new_body = $new_body . substr($orig_body, 0, $img_start) . '[!#saved_image' . $cnt . '#!]';
  35. $cnt++;
  36. } else {
  37. $new_body = $new_body . substr($orig_body, 0, $img_end + strlen('[/img]'));
  38. }
  39. $orig_body = substr($orig_body, $img_end + strlen('[/img]'));
  40. if ($orig_body === false) {
  41. // in case the body ends on a closing image tag
  42. $orig_body = '';
  43. }
  44. $img_start = strpos($orig_body, '[img');
  45. $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
  46. $img_end = ($img_start !== false ? strpos(substr($orig_body, $img_start), '[/img]') : false);
  47. }
  48. $new_body = $new_body . $orig_body;
  49. return array('body' => $new_body, 'images' => $saved_image);
  50. }}
  51. if (! function_exists('item_redir_and_replace_images')) {
  52. function item_redir_and_replace_images($body, $images, $cid) {
  53. $origbody = $body;
  54. $newbody = '';
  55. $cnt = 1;
  56. $pos = get_bb_tag_pos($origbody, 'url', 1);
  57. while ($pos !== false && $cnt < 1000) {
  58. $search = '/\[url\=(.*?)\]\[!#saved_image([0-9]*)#!\]\[\/url\]' . '/is';
  59. $replace = '[url=' . System::baseUrl() . '/redir/' . $cid
  60. . '?f=1&url=' . '$1' . '][!#saved_image' . '$2' .'#!][/url]';
  61. $newbody .= substr($origbody, 0, $pos['start']['open']);
  62. $subject = substr($origbody, $pos['start']['open'], $pos['end']['close'] - $pos['start']['open']);
  63. $origbody = substr($origbody, $pos['end']['close']);
  64. if ($origbody === false) {
  65. $origbody = '';
  66. }
  67. $subject = preg_replace($search, $replace, $subject);
  68. $newbody .= $subject;
  69. $cnt++;
  70. $pos = get_bb_tag_pos($origbody, 'url', 1);
  71. }
  72. $newbody .= $origbody;
  73. $cnt = 0;
  74. foreach ($images as $image) {
  75. /*
  76. * We're depending on the property of 'foreach' (specified on the PHP website) that
  77. * it loops over the array starting from the first element and going sequentially
  78. * to the last element.
  79. */
  80. $newbody = str_replace('[!#saved_image' . $cnt . '#!]', '[img]' . $image . '[/img]', $newbody);
  81. $cnt++;
  82. }
  83. return $newbody;
  84. }}
  85. /**
  86. * Render actions localized
  87. */
  88. function localize_item(&$item) {
  89. $extracted = item_extract_images($item['body']);
  90. if ($extracted['images']) {
  91. $item['body'] = item_redir_and_replace_images($extracted['body'], $extracted['images'], $item['contact-id']);
  92. }
  93. /// @Separted ???
  94. $xmlhead = "<" . "?xml version='1.0' encoding='UTF-8' ?" . ">";
  95. if (activity_match($item['verb'], ACTIVITY_LIKE)
  96. || activity_match($item['verb'], ACTIVITY_DISLIKE)
  97. || activity_match($item['verb'], ACTIVITY_ATTEND)
  98. || activity_match($item['verb'], ACTIVITY_ATTENDNO)
  99. || activity_match($item['verb'], ACTIVITY_ATTENDMAYBE)) {
  100. /// @TODO may hurt performance
  101. $r = q("SELECT * FROM `item`, `contact`
  102. WHERE `item`.`contact-id`=`contact`.`id`
  103. AND `item`.`uri`='%s'",
  104. dbesc($item['parent-uri']));
  105. if (!DBM::is_result($r)) {
  106. return;
  107. }
  108. $obj = $r[0];
  109. $author = '[url=' . $item['author-link'] . ']' . $item['author-name'] . '[/url]';
  110. $objauthor = '[url=' . $obj['author-link'] . ']' . $obj['author-name'] . '[/url]';
  111. switch ($obj['verb']) {
  112. case ACTIVITY_POST:
  113. switch ($obj['object-type']) {
  114. case ACTIVITY_OBJ_EVENT:
  115. $post_type = t('event');
  116. break;
  117. default:
  118. $post_type = t('status');
  119. }
  120. break;
  121. default:
  122. if ($obj['resource-id']) {
  123. $post_type = t('photo');
  124. $m = array();
  125. preg_match("/\[url=([^]]*)\]/", $obj['body'], $m);
  126. $rr['plink'] = $m[1];
  127. } else {
  128. $post_type = t('status');
  129. }
  130. }
  131. $plink = '[url=' . $obj['plink'] . ']' . $post_type . '[/url]';
  132. if (activity_match($item['verb'], ACTIVITY_LIKE)) {
  133. $bodyverb = t('%1$s likes %2$s\'s %3$s');
  134. }
  135. elseif (activity_match($item['verb'], ACTIVITY_DISLIKE)) {
  136. $bodyverb = t('%1$s doesn\'t like %2$s\'s %3$s');
  137. }
  138. elseif (activity_match($item['verb'], ACTIVITY_ATTEND)) {
  139. $bodyverb = t('%1$s attends %2$s\'s %3$s');
  140. }
  141. elseif (activity_match($item['verb'], ACTIVITY_ATTENDNO)) {
  142. $bodyverb = t('%1$s doesn\'t attend %2$s\'s %3$s');
  143. }
  144. elseif (activity_match($item['verb'], ACTIVITY_ATTENDMAYBE)) {
  145. $bodyverb = t('%1$s attends maybe %2$s\'s %3$s');
  146. }
  147. $item['body'] = sprintf($bodyverb, $author, $objauthor, $plink);
  148. }
  149. if (activity_match($item['verb'], ACTIVITY_FRIEND)) {
  150. if ($item['object-type']=="" || $item['object-type']!== ACTIVITY_OBJ_PERSON) return;
  151. $Aname = $item['author-name'];
  152. $Alink = $item['author-link'];
  153. $xmlhead="<"."?xml version='1.0' encoding='UTF-8' ?".">";
  154. $obj = parse_xml_string($xmlhead.$item['object']);
  155. $links = parse_xml_string($xmlhead."<links>".unxmlify($obj->link)."</links>");
  156. $Bname = $obj->title;
  157. $Blink = ""; $Bphoto = "";
  158. foreach ($links->link as $l) {
  159. $atts = $l->attributes();
  160. switch ($atts['rel']) {
  161. case "alternate": $Blink = $atts['href'];
  162. case "photo": $Bphoto = $atts['href'];
  163. }
  164. }
  165. $A = '[url=' . zrl($Alink) . ']' . $Aname . '[/url]';
  166. $B = '[url=' . zrl($Blink) . ']' . $Bname . '[/url]';
  167. if ($Bphoto != "") {
  168. $Bphoto = '[url=' . zrl($Blink) . '][img]' . $Bphoto . '[/img][/url]';
  169. }
  170. $item['body'] = sprintf( t('%1$s is now friends with %2$s'), $A, $B)."\n\n\n".$Bphoto;
  171. }
  172. if (stristr($item['verb'], ACTIVITY_POKE)) {
  173. $verb = urldecode(substr($item['verb'],strpos($item['verb'],'#')+1));
  174. if (! $verb) {
  175. return;
  176. }
  177. if ($item['object-type']=="" || $item['object-type']!== ACTIVITY_OBJ_PERSON) {
  178. return;
  179. }
  180. $Aname = $item['author-name'];
  181. $Alink = $item['author-link'];
  182. $xmlhead = "<" . "?xml version='1.0' encoding='UTF-8' ?" . ">";
  183. $obj = parse_xml_string($xmlhead.$item['object']);
  184. $links = parse_xml_string($xmlhead."<links>".unxmlify($obj->link)."</links>");
  185. $Bname = $obj->title;
  186. $Blink = "";
  187. $Bphoto = "";
  188. foreach ($links->link as $l) {
  189. $atts = $l->attributes();
  190. switch ($atts['rel']) {
  191. case "alternate": $Blink = $atts['href'];
  192. case "photo": $Bphoto = $atts['href'];
  193. }
  194. }
  195. $A = '[url=' . zrl($Alink) . ']' . $Aname . '[/url]';
  196. $B = '[url=' . zrl($Blink) . ']' . $Bname . '[/url]';
  197. if ($Bphoto != "") {
  198. $Bphoto = '[url=' . zrl($Blink) . '][img=80x80]' . $Bphoto . '[/img][/url]';
  199. }
  200. /*
  201. * we can't have a translation string with three positions but no distinguishable text
  202. * So here is the translate string.
  203. */
  204. $txt = t('%1$s poked %2$s');
  205. // now translate the verb
  206. $poked_t = trim(sprintf($txt, "", ""));
  207. $txt = str_replace( $poked_t, t($verb), $txt);
  208. // then do the sprintf on the translation string
  209. $item['body'] = sprintf($txt, $A, $B). "\n\n\n" . $Bphoto;
  210. }
  211. if (stristr($item['verb'], ACTIVITY_MOOD)) {
  212. $verb = urldecode(substr($item['verb'], strpos($item['verb'], '#') + 1));
  213. if (! $verb) {
  214. return;
  215. }
  216. $Aname = $item['author-name'];
  217. $Alink = $item['author-link'];
  218. $A = '[url=' . zrl($Alink) . ']' . $Aname . '[/url]';
  219. $txt = t('%1$s is currently %2$s');
  220. $item['body'] = sprintf($txt, $A, t($verb));
  221. }
  222. if (activity_match($item['verb'], ACTIVITY_TAG)) {
  223. /// @TODO may hurt performance "joining" two tables + asterisk
  224. $r = q("SELECT * FROM `item`, `contact`
  225. WHERE `item`.`contact-id`=`contact`.`id`
  226. AND `item`.`uri`='%s'",
  227. dbesc($item['parent-uri']));
  228. if (!DBM::is_result($r)) {
  229. return;
  230. }
  231. $obj = $r[0];
  232. $author = '[url=' . zrl($item['author-link']) . ']' . $item['author-name'] . '[/url]';
  233. $objauthor = '[url=' . zrl($obj['author-link']) . ']' . $obj['author-name'] . '[/url]';
  234. switch ($obj['verb']) {
  235. case ACTIVITY_POST:
  236. switch ($obj['object-type']) {
  237. case ACTIVITY_OBJ_EVENT:
  238. $post_type = t('event');
  239. break;
  240. default:
  241. $post_type = t('status');
  242. }
  243. break;
  244. default:
  245. if ($obj['resource-id']) {
  246. $post_type = t('photo');
  247. $m=array(); preg_match("/\[url=([^]]*)\]/", $obj['body'], $m);
  248. $rr['plink'] = $m[1];
  249. } else {
  250. $post_type = t('status');
  251. }
  252. // Let's break everthing ... ;-)
  253. break;
  254. }
  255. $plink = '[url=' . $obj['plink'] . ']' . $post_type . '[/url]';
  256. $parsedobj = parse_xml_string($xmlhead.$item['object']);
  257. $tag = sprintf('#[url=%s]%s[/url]', $parsedobj->id, $parsedobj->content);
  258. $item['body'] = sprintf( t('%1$s tagged %2$s\'s %3$s with %4$s'), $author, $objauthor, $plink, $tag );
  259. }
  260. if (activity_match($item['verb'], ACTIVITY_FAVORITE)) {
  261. if ($item['object-type'] == "") {
  262. return;
  263. }
  264. $Aname = $item['author-name'];
  265. $Alink = $item['author-link'];
  266. $xmlhead = "<" . "?xml version='1.0' encoding='UTF-8' ?" . ">";
  267. $obj = parse_xml_string($xmlhead.$item['object']);
  268. if (strlen($obj->id)) {
  269. $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
  270. dbesc($obj->id),
  271. intval($item['uid'])
  272. );
  273. if (DBM::is_result($r) && $r[0]['plink']) {
  274. $target = $r[0];
  275. $Bname = $target['author-name'];
  276. $Blink = $target['author-link'];
  277. $A = '[url=' . zrl($Alink) . ']' . $Aname . '[/url]';
  278. $B = '[url=' . zrl($Blink) . ']' . $Bname . '[/url]';
  279. $P = '[url=' . $target['plink'] . ']' . t('post/item') . '[/url]';
  280. $item['body'] = sprintf( t('%1$s marked %2$s\'s %3$s as favorite'), $A, $B, $P)."\n";
  281. }
  282. }
  283. }
  284. $matches = null;
  285. if (preg_match_all('/@\[url=(.*?)\]/is', $item['body'], $matches, PREG_SET_ORDER)) {
  286. foreach ($matches as $mtch) {
  287. if (! strpos($mtch[1], 'zrl=')) {
  288. $item['body'] = str_replace($mtch[0], '@[url=' . zrl($mtch[1]) . ']', $item['body']);
  289. }
  290. }
  291. }
  292. // add zrl's to public images
  293. $photo_pattern = "/\[url=(.*?)\/photos\/(.*?)\/image\/(.*?)\]\[img(.*?)\]h(.*?)\[\/img\]\[\/url\]/is";
  294. if (preg_match($photo_pattern, $item['body'])) {
  295. $photo_replace = '[url=' . zrl('$1' . '/photos/' . '$2' . '/image/' . '$3' ,true) . '][img' . '$4' . ']h' . '$5' . '[/img][/url]';
  296. $item['body'] = bb_tag_preg_replace($photo_pattern, $photo_replace, 'url', $item['body']);
  297. }
  298. // add sparkle links to appropriate permalinks
  299. $x = stristr($item['plink'],'/display/');
  300. if ($x) {
  301. $sparkle = false;
  302. $y = best_link_url($item, $sparkle);
  303. if (strstr($y, '/redir/')) {
  304. $item['plink'] = $y . '?f=&url=' . $item['plink'];
  305. }
  306. }
  307. }
  308. /**
  309. * Count the total of comments on this item and its desendants
  310. * @TODO proper type-hint + doc-tag
  311. */
  312. function count_descendants($item) {
  313. $total = count($item['children']);
  314. if ($total > 0) {
  315. foreach ($item['children'] as $child) {
  316. if (! visible_activity($child)) {
  317. $total --;
  318. }
  319. $total += count_descendants($child);
  320. }
  321. }
  322. return $total;
  323. }
  324. function visible_activity($item) {
  325. /*
  326. * likes (etc.) can apply to other things besides posts. Check if they are post children,
  327. * in which case we handle them specially
  328. */
  329. $hidden_activities = array(ACTIVITY_LIKE, ACTIVITY_DISLIKE, ACTIVITY_ATTEND, ACTIVITY_ATTENDNO, ACTIVITY_ATTENDMAYBE);
  330. foreach ($hidden_activities as $act) {
  331. if (activity_match($item['verb'], $act)) {
  332. return false;
  333. }
  334. }
  335. if (activity_match($item['verb'], ACTIVITY_FOLLOW) && $item['object-type'] === ACTIVITY_OBJ_NOTE) {
  336. if (! (($item['self']) && ($item['uid'] == local_user()))) {
  337. return false;
  338. }
  339. }
  340. return true;
  341. }
  342. /**
  343. * @brief SQL query for items
  344. */
  345. function item_query() {
  346. return "SELECT " . item_fieldlists() . " FROM `item` " .
  347. item_joins() . " WHERE " . item_condition();
  348. }
  349. /**
  350. * @brief List of all data fields that are needed for displaying items
  351. */
  352. function item_fieldlists() {
  353. /*
  354. These Fields are not added below (yet). They are here to for bug search.
  355. `item`.`type`,
  356. `item`.`extid`,
  357. `item`.`changed`,
  358. `item`.`moderated`,
  359. `item`.`target-type`,
  360. `item`.`target`,
  361. `item`.`resource-id`,
  362. `item`.`tag`,
  363. `item`.`inform`,
  364. `item`.`pubmail`,
  365. `item`.`visible`,
  366. `item`.`spam`,
  367. `item`.`bookmark`,
  368. `item`.`unseen`,
  369. `item`.`deleted`,
  370. `item`.`origin`,
  371. `item`.`forum_mode`,
  372. `item`.`last-child`,
  373. `item`.`mention`,
  374. `item`.`global`,
  375. `item`.`gcontact-id`,
  376. `item`.`shadow`,
  377. */
  378. return "`item`.`author-id`, `item`.`author-link`, `item`.`author-name`, `item`.`author-avatar`,
  379. `item`.`owner-id`, `item`.`owner-link`, `item`.`owner-name`, `item`.`owner-avatar`,
  380. `item`.`contact-id`, `item`.`uid`, `item`.`id`, `item`.`parent`,
  381. `item`.`uri`, `item`.`thr-parent`, `item`.`parent-uri`,
  382. `item`.`commented`, `item`.`created`, `item`.`edited`, `item`.`received`,
  383. `item`.`verb`, `item`.`object-type`, `item`.`postopts`, `item`.`plink`,
  384. `item`.`guid`, `item`.`wall`, `item`.`private`, `item`.`starred`,
  385. `item`.`title`, `item`.`body`, `item`.`file`, `item`.`event-id`,
  386. `item`.`location`, `item`.`coord`, `item`.`app`, `item`.`attach`,
  387. `item`.`rendered-hash`, `item`.`rendered-html`, `item`.`object`,
  388. `item`.`allow_cid`, `item`.`allow_gid`, `item`.`deny_cid`, `item`.`deny_gid`,
  389. `item`.`id` AS `item_id`, `item`.`network` AS `item_network`,
  390. `author`.`thumb` AS `author-thumb`, `owner`.`thumb` AS `owner-thumb`,
  391. `contact`.`network`, `contact`.`url`, `contact`.`name`, `contact`.`writable`,
  392. `contact`.`self`, `contact`.`id` AS `cid`, `contact`.`alias`,
  393. `event`.`created` AS `event-created`, `event`.`edited` AS `event-edited`,
  394. `event`.`start` AS `event-start`,`event`.`finish` AS `event-finish`,
  395. `event`.`summary` AS `event-summary`,`event`.`desc` AS `event-desc`,
  396. `event`.`location` AS `event-location`, `event`.`type` AS `event-type`,
  397. `event`.`nofinish` AS `event-nofinish`,`event`.`adjust` AS `event-adjust`,
  398. `event`.`ignore` AS `event-ignore`, `event`.`id` AS `event-id`";
  399. }
  400. /**
  401. * @brief SQL join for contacts that are needed for displaying items
  402. */
  403. function item_joins() {
  404. return "STRAIGHT_JOIN `contact` ON `contact`.`id` = `item`.`contact-id` AND
  405. (NOT `contact`.`blocked` OR `contact`.`pending`)
  406. LEFT JOIN `contact` AS `author` ON `author`.`id`=`item`.`author-id`
  407. LEFT JOIN `contact` AS `owner` ON `owner`.`id`=`item`.`owner-id`
  408. LEFT JOIN `event` ON `event-id` = `event`.`id`";
  409. }
  410. /**
  411. * @brief SQL condition for items that are needed for displaying items
  412. */
  413. function item_condition() {
  414. return "`item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`";
  415. }
  416. if (!function_exists('conversation')) {
  417. /**
  418. * "Render" a conversation or list of items for HTML display.
  419. * There are two major forms of display:
  420. * - Sequential or unthreaded ("New Item View" or search results)
  421. * - conversation view
  422. * The $mode parameter decides between the various renderings and also
  423. * figures out how to determine page owner and other contextual items
  424. * that are based on unique features of the calling module.
  425. *
  426. */
  427. function conversation(App $a, $items, $mode, $update, $preview = false) {
  428. require_once 'include/bbcode.php';
  429. require_once 'include/Contact.php';
  430. require_once 'mod/proxy.php';
  431. $ssl_state = ((local_user()) ? true : false);
  432. $profile_owner = 0;
  433. $page_writeable = false;
  434. $live_update_div = '';
  435. $arr_blocked = null;
  436. if (local_user()) {
  437. $str_blocked = PConfig::get(local_user(), 'system', 'blocked');
  438. if ($str_blocked) {
  439. $arr_blocked = explode(',', $str_blocked);
  440. for ($x = 0; $x < count($arr_blocked); $x ++) {
  441. $arr_blocked[$x] = trim($arr_blocked[$x]);
  442. }
  443. }
  444. }
  445. $previewing = (($preview) ? ' preview ' : '');
  446. if ($mode === 'network') {
  447. $profile_owner = local_user();
  448. $page_writeable = true;
  449. if (!$update) {
  450. /*
  451. * The special div is needed for liveUpdate to kick in for this page.
  452. * We only launch liveUpdate if you aren't filtering in some incompatible
  453. * way and also you aren't writing a comment (discovered in javascript).
  454. */
  455. $live_update_div = '<div id="live-network"></div>' . "\r\n"
  456. . "<script> var profile_uid = " . $_SESSION['uid']
  457. . "; var netargs = '" . substr($a->cmd, 8)
  458. . '?f='
  459. . ((x($_GET, 'cid')) ? '&cid=' . $_GET['cid'] : '')
  460. . ((x($_GET, 'search')) ? '&search=' . $_GET['search'] : '')
  461. . ((x($_GET, 'star')) ? '&star=' . $_GET['star'] : '')
  462. . ((x($_GET, 'order')) ? '&order=' . $_GET['order'] : '')
  463. . ((x($_GET, 'bmark')) ? '&bmark=' . $_GET['bmark'] : '')
  464. . ((x($_GET, 'liked')) ? '&liked=' . $_GET['liked'] : '')
  465. . ((x($_GET, 'conv')) ? '&conv=' . $_GET['conv'] : '')
  466. . ((x($_GET, 'spam')) ? '&spam=' . $_GET['spam'] : '')
  467. . ((x($_GET, 'nets')) ? '&nets=' . $_GET['nets'] : '')
  468. . ((x($_GET, 'cmin')) ? '&cmin=' . $_GET['cmin'] : '')
  469. . ((x($_GET, 'cmax')) ? '&cmax=' . $_GET['cmax'] : '')
  470. . ((x($_GET, 'file')) ? '&file=' . $_GET['file'] : '')
  471. . "'; var profile_page = " . $a->pager['page'] . "; </script>\r\n";
  472. }
  473. } elseif ($mode === 'profile') {
  474. $profile_owner = $a->profile['profile_uid'];
  475. $page_writeable = can_write_wall($a,$profile_owner);
  476. if (!$update) {
  477. $tab = notags(trim($_GET['tab']));
  478. $tab = ( $tab ? $tab : 'posts' );
  479. if ($tab === 'posts') {
  480. /*
  481. * This is ugly, but we can't pass the profile_uid through the session to the ajax updater,
  482. * because browser prefetching might change it on us. We have to deliver it with the page.
  483. */
  484. $live_update_div = '<div id="live-profile"></div>' . "\r\n"
  485. . "<script> var profile_uid = " . $a->profile['profile_uid']
  486. . "; var netargs = '?f='; var profile_page = " . $a->pager['page'] . "; </script>\r\n";
  487. }
  488. }
  489. } elseif ($mode === 'notes') {
  490. $profile_owner = local_user();
  491. $page_writeable = true;
  492. if (!$update) {
  493. $live_update_div = '<div id="live-notes"></div>' . "\r\n"
  494. . "<script> var profile_uid = " . local_user()
  495. . "; var netargs = '/?f='; var profile_page = " . $a->pager['page'] . "; </script>\r\n";
  496. }
  497. } elseif ($mode === 'display') {
  498. $profile_owner = $a->profile['uid'];
  499. $page_writeable = can_write_wall($a,$profile_owner);
  500. if (!$update) {
  501. $live_update_div = '<div id="live-display"></div>' . "\r\n"
  502. . "<script> var profile_uid = " . $_SESSION['uid'] . ";"
  503. . " var profile_page = 1; </script>";
  504. }
  505. } elseif ($mode === 'community') {
  506. $profile_owner = 0;
  507. $page_writeable = false;
  508. if (!$update) {
  509. $live_update_div = '<div id="live-community"></div>' . "\r\n"
  510. . "<script> var profile_uid = -1; var netargs = '/?f='; var profile_page = " . $a->pager['page'] . "; </script>\r\n";
  511. }
  512. } elseif ($mode === 'search') {
  513. $live_update_div = '<div id="live-search"></div>' . "\r\n";
  514. }
  515. $page_dropping = ((local_user() && local_user() == $profile_owner) ? true : false);
  516. if ($update) {
  517. $return_url = $_SESSION['return_url'];
  518. } else {
  519. $return_url = $_SESSION['return_url'] = $a->query_string;
  520. }
  521. $cb = array('items' => $items, 'mode' => $mode, 'update' => $update, 'preview' => $preview);
  522. call_hooks('conversation_start',$cb);
  523. $items = $cb['items'];
  524. $cmnt_tpl = get_markup_template('comment_item.tpl');
  525. $hide_comments_tpl = get_markup_template('hide_comments.tpl');
  526. $conv_responses = array(
  527. 'like' => array('title' => t('Likes','title')), 'dislike' => array('title' => t('Dislikes','title')),
  528. 'attendyes' => array('title' => t('Attending','title')), 'attendno' => array('title' => t('Not attending','title')), 'attendmaybe' => array('title' => t('Might attend','title'))
  529. );
  530. // array with html for each thread (parent+comments)
  531. $threads = array();
  532. $threadsid = -1;
  533. $page_template = get_markup_template("conversation.tpl");
  534. if ($items && count($items)) {
  535. if ($mode === 'network-new' || $mode === 'search' || $mode === 'community') {
  536. /*
  537. * "New Item View" on network page or search page results
  538. * - just loop through the items and format them minimally for display
  539. */
  540. /// @TODO old lost code?
  541. // $tpl = get_markup_template('search_item.tpl');
  542. $tpl = 'search_item.tpl';
  543. foreach ($items as $item) {
  544. if ($arr_blocked) {
  545. $blocked = false;
  546. foreach ($arr_blocked as $b) {
  547. if ($b && link_compare($item['author-link'], $b)) {
  548. $blocked = true;
  549. break;
  550. }
  551. }
  552. if ($blocked) {
  553. continue;
  554. }
  555. }
  556. $threadsid++;
  557. $comment = '';
  558. $owner_url = '';
  559. $owner_name = '';
  560. $sparkle = '';
  561. if ($mode === 'search' || $mode === 'community') {
  562. if (((activity_match($item['verb'], ACTIVITY_LIKE)) || (activity_match($item['verb'], ACTIVITY_DISLIKE)))
  563. && ($item['id'] != $item['parent']))
  564. continue;
  565. $nickname = $item['nickname'];
  566. } else {
  567. $nickname = $a->user['nickname'];
  568. }
  569. // prevent private email from leaking.
  570. if ($item['network'] === NETWORK_MAIL && local_user() != $item['uid']) {
  571. continue;
  572. }
  573. $profile_name = ((strlen($item['author-name'])) ? $item['author-name'] : $item['name']);
  574. if ($item['author-link'] && (! $item['author-name'])) {
  575. $profile_name = $item['author-link'];
  576. }
  577. $tags = array();
  578. $hashtags = array();
  579. $mentions = array();
  580. $searchpath = System::baseUrl()."/search?tag=";
  581. $taglist = dba::select('term', array('type', 'term', 'url'),
  582. array("`otype` = ? AND `oid` = ? AND `type` IN (?, ?)", TERM_OBJ_POST, $item['id'], TERM_HASHTAG, TERM_MENTION),
  583. array('order' => array('tid')));
  584. while ($tag = dba::fetch($taglist)) {
  585. if ($tag["url"] == "") {
  586. $tag["url"] = $searchpath . strtolower($tag["term"]);
  587. }
  588. $tag["url"] = best_link_url($item, $sp, $tag["url"]);
  589. if ($tag["type"] == TERM_HASHTAG) {
  590. $hashtags[] = "#<a href=\"" . $tag["url"] . "\" target=\"_blank\">" . $tag["term"] . "</a>";
  591. $prefix = "#";
  592. } elseif ($tag["type"] == TERM_MENTION) {
  593. $mentions[] = "@<a href=\"" . $tag["url"] . "\" target=\"_blank\">" . $tag["term"] . "</a>";
  594. $prefix = "@";
  595. }
  596. $tags[] = $prefix."<a href=\"" . $tag["url"] . "\" target=\"_blank\">" . $tag["term"] . "</a>";
  597. }
  598. dba::close($taglist);
  599. $sp = false;
  600. $profile_link = best_link_url($item, $sp);
  601. if ($profile_link === 'mailbox') {
  602. $profile_link = '';
  603. }
  604. if ($sp) {
  605. $sparkle = ' sparkle';
  606. } else {
  607. $profile_link = zrl($profile_link);
  608. }
  609. if (!x($item, 'author-thumb') || ($item['author-thumb'] == "")) {
  610. $author_contact = get_contact_details_by_url($item['author-link'], $profile_owner);
  611. if ($author_contact["thumb"]) {
  612. $item['author-thumb'] = $author_contact["thumb"];
  613. } else {
  614. $item['author-thumb'] = $item['author-avatar'];
  615. }
  616. }
  617. if (!isset($item['owner-thumb']) || ($item['owner-thumb'] == "")) {
  618. $owner_contact = get_contact_details_by_url($item['owner-link'], $profile_owner);
  619. if ($owner_contact["thumb"]) {
  620. $item['owner-thumb'] = $owner_contact["thumb"];
  621. } else {
  622. $item['owner-thumb'] = $item['owner-avatar'];
  623. }
  624. }
  625. $locate = array('location' => $item['location'], 'coord' => $item['coord'], 'html' => '');
  626. call_hooks('render_location',$locate);
  627. $location = ((strlen($locate['html'])) ? $locate['html'] : render_location_dummy($locate));
  628. localize_item($item);
  629. if ($mode === 'network-new') {
  630. $dropping = true;
  631. } else {
  632. $dropping = false;
  633. }
  634. $drop = array(
  635. 'dropping' => $dropping,
  636. 'pagedrop' => $page_dropping,
  637. 'select' => t('Select'),
  638. 'delete' => t('Delete'),
  639. );
  640. $star = false;
  641. $isstarred = "unstarred";
  642. $lock = false;
  643. $likebuttons = false;
  644. $shareable = false;
  645. $body = prepare_body($item, true, $preview);
  646. list($categories, $folders) = get_cats_and_terms($item);
  647. if ($a->theme['template_engine'] === 'internal') {
  648. $profile_name_e = template_escape($profile_name);
  649. $item['title_e'] = template_escape($item['title']);
  650. $body_e = template_escape($body);
  651. $tags_e = template_escape($tags);
  652. $hashtags_e = template_escape($hashtags);
  653. $mentions_e = template_escape($mentions);
  654. $location_e = template_escape($location);
  655. $owner_name_e = template_escape($owner_name);
  656. } else {
  657. $profile_name_e = $profile_name;
  658. $item['title_e'] = $item['title'];
  659. $body_e = $body;
  660. $tags_e = $tags;
  661. $hashtags_e = $hashtags;
  662. $mentions_e = $mentions;
  663. $location_e = $location;
  664. $owner_name_e = $owner_name;
  665. }
  666. if ($item['item_network'] == "") {
  667. $item['item_network'] = $item['network'];
  668. }
  669. $tmp_item = array(
  670. 'template' => $tpl,
  671. 'id' => (($preview) ? 'P0' : $item['item_id']),
  672. 'guid' => (($preview) ? 'Q0' : $item['guid']),
  673. 'network' => $item['item_network'],
  674. 'network_name' => network_to_name($item['item_network'], $profile_link),
  675. 'linktitle' => sprintf( t('View %s\'s profile @ %s'), $profile_name, ((strlen($item['author-link'])) ? $item['author-link'] : $item['url'])),
  676. 'profile_url' => $profile_link,
  677. 'item_photo_menu' => item_photo_menu($item),
  678. 'name' => $profile_name_e,
  679. 'sparkle' => $sparkle,
  680. 'lock' => $lock,
  681. 'thumb' => System::removedBaseUrl(proxy_url($item['author-thumb'], false, PROXY_SIZE_THUMB)),
  682. 'title' => $item['title_e'],
  683. 'body' => $body_e,
  684. 'tags' => $tags_e,
  685. 'hashtags' => $hashtags_e,
  686. 'mentions' => $mentions_e,
  687. 'txt_cats' => t('Categories:'),
  688. 'txt_folders' => t('Filed under:'),
  689. 'has_cats' => ((count($categories)) ? 'true' : ''),
  690. 'has_folders' => ((count($folders)) ? 'true' : ''),
  691. 'categories' => $categories,
  692. 'folders' => $folders,
  693. 'text' => strip_tags($body_e),
  694. 'localtime' => datetime_convert('UTC', date_default_timezone_get(), $item['created'], 'r'),
  695. 'ago' => (($item['app']) ? sprintf( t('%s from %s'),relative_date($item['created']),$item['app']) : relative_date($item['created'])),
  696. 'location' => $location_e,
  697. 'indent' => '',
  698. 'owner_name' => $owner_name_e,
  699. 'owner_url' => $owner_url,
  700. 'owner_photo' => System::removedBaseUrl(proxy_url($item['owner-thumb'], false, PROXY_SIZE_THUMB)),
  701. 'plink' => get_plink($item),
  702. 'edpost' => false,
  703. 'isstarred' => $isstarred,
  704. 'star' => $star,
  705. 'drop' => $drop,
  706. 'vote' => $likebuttons,
  707. 'like' => '',
  708. 'dislike' => '',
  709. 'comment' => '',
  710. //'conv' => (($preview) ? '' : array('href'=> 'display/' . $nickname . '/' . $item['id'], 'title'=> t('View in context'))),
  711. 'conv' => (($preview) ? '' : array('href'=> 'display/'.$item['guid'], 'title'=> t('View in context'))),
  712. 'previewing' => $previewing,
  713. 'wait' => t('Please wait'),
  714. 'thread_level' => 1,
  715. );
  716. $arr = array('item' => $item, 'output' => $tmp_item);
  717. call_hooks('display_item', $arr);
  718. $threads[$threadsid]['id'] = $item['item_id'];
  719. $threads[$threadsid]['network'] = $item['item_network'];
  720. $threads[$threadsid]['items'] = array($arr['output']);
  721. }
  722. } else {
  723. // Normal View
  724. $page_template = get_markup_template("threaded_conversation.tpl");
  725. $conv = new Conversation($mode, $preview);
  726. /*
  727. * get all the topmost parents
  728. * this shouldn't be needed, as we should have only them in our array
  729. * But for now, this array respects the old style, just in case
  730. */
  731. $threads = array();
  732. foreach ($items as $item) {
  733. if ($arr_blocked) {
  734. $blocked = false;
  735. foreach ($arr_blocked as $b) {
  736. if ($b && link_compare($item['author-link'], $b)) {
  737. $blocked = true;
  738. break;
  739. }
  740. }
  741. if ($blocked) {
  742. continue;
  743. }
  744. }
  745. // Can we put this after the visibility check?
  746. builtin_activity_puller($item, $conv_responses);
  747. // Only add what is visible
  748. if ($item['network'] === NETWORK_MAIL && local_user() != $item['uid']) {
  749. continue;
  750. }
  751. if (! visible_activity($item)) {
  752. continue;
  753. }
  754. call_hooks('display_item', $arr);
  755. $item['pagedrop'] = $page_dropping;
  756. if ($item['id'] == $item['parent']) {
  757. $item_object = new Item($item);
  758. $conv->addThread($item_object);
  759. }
  760. }
  761. $threads = $conv->getTemplateData($conv_responses);
  762. if (!$threads) {
  763. logger('[ERROR] conversation : Failed to get template data.', LOGGER_DEBUG);
  764. $threads = array();
  765. }
  766. }
  767. }
  768. $o = replace_macros($page_template, array(
  769. '$baseurl' => System::baseUrl($ssl_state),
  770. '$return_path' => $a->query_string,
  771. '$live_update' => $live_update_div,
  772. '$remove' => t('remove'),
  773. '$mode' => $mode,
  774. '$user' => $a->user,
  775. '$threads' => $threads,
  776. '$dropping' => ($page_dropping && feature_enabled(local_user(), 'multi_delete') ? t('Delete Selected Items') : False),
  777. ));
  778. return $o;
  779. }}
  780. function best_link_url($item, &$sparkle, $url = '') {
  781. $best_url = '';
  782. $sparkle = false;
  783. $clean_url = normalise_link($item['author-link']);
  784. if (local_user()) {
  785. $r = dba::select('contact', array('id'),
  786. array('network' => NETWORK_DFRN, 'uid' => local_user(), 'nurl' => normalise_link($clean_url), 'pending' => false),
  787. array('limit' => 1));
  788. if (DBM::is_result($r)) {
  789. $best_url = 'redir/' . $r['id'];
  790. $sparkle = true;
  791. if ($url != '') {
  792. $hostname = get_app()->get_hostname();
  793. if (!strstr($url, $hostname)) {
  794. $best_url .= "?url=".$url;
  795. } else {
  796. $best_url = $url;
  797. }
  798. }
  799. }
  800. }
  801. if (! $best_url) {
  802. if ($url != '') {
  803. $best_url = $url;
  804. } elseif (strlen($item['author-link'])) {
  805. $best_url = $item['author-link'];
  806. } else {
  807. $best_url = $item['url'];
  808. }
  809. }
  810. return $best_url;
  811. }
  812. function item_photo_menu($item) {
  813. $sub_link = '';
  814. $poke_link = '';
  815. $contact_url = '';
  816. $pm_url = '';
  817. $status_link = '';
  818. $photos_link = '';
  819. $posts_link = '';
  820. $network = '';
  821. if ((local_user()) && local_user() == $item['uid'] && $item['parent'] == $item['id'] && (! $item['self'])) {
  822. $sub_link = 'javascript:dosubthread(' . $item['id'] . '); return false;';
  823. }
  824. $sparkle = false;
  825. $profile_link = best_link_url($item, $sparkle);
  826. if ($profile_link === 'mailbox') {
  827. $profile_link = '';
  828. }
  829. $cid = 0;
  830. $network = '';
  831. $rel = 0;
  832. $r = dba::select('contact', array('id', 'network', 'rel'), array('uid' => local_user(), 'nurl' => normalise_link($item['author-link'])), array('limit' => 1));
  833. if (DBM::is_result($r)) {
  834. $cid = $r['id'];
  835. $network = $r['network'];
  836. $rel = $r['rel'];
  837. }
  838. if ($sparkle) {
  839. $status_link = $profile_link . '?url=status';
  840. $photos_link = $profile_link . '?url=photos';
  841. $profile_link = $profile_link . '?url=profile';
  842. $zurl = '';
  843. } else {
  844. $profile_link = zrl($profile_link);
  845. }
  846. if ($cid && !$item['self']) {
  847. $poke_link = 'poke/?f=&c=' . $cid;
  848. $contact_url = 'contacts/' . $cid;
  849. $posts_link = 'contacts/' . $cid . '/posts';
  850. if (in_array($network, array(NETWORK_DFRN, NETWORK_DIASPORA))) {
  851. $pm_url = 'message/new/' . $cid;
  852. }
  853. }
  854. if (local_user()) {
  855. $menu = array(
  856. t('Follow Thread') => $sub_link,
  857. t('View Status') => $status_link,
  858. t('View Profile') => $profile_link,
  859. t('View Photos') => $photos_link,
  860. t('Network Posts') => $posts_link,
  861. t('View Contact') => $contact_url,
  862. t('Send PM') => $pm_url
  863. );
  864. if ($network == NETWORK_DFRN) {
  865. $menu[t("Poke")] = $poke_link;
  866. }
  867. if ((($cid == 0) || ($rel == CONTACT_IS_FOLLOWER)) &&
  868. in_array($item['network'], array(NETWORK_DFRN, NETWORK_OSTATUS, NETWORK_DIASPORA))) {
  869. $menu[t('Connect/Follow')] = 'follow?url=' . urlencode($item['author-link']);
  870. }
  871. } else {
  872. $menu = array(t('View Profile') => $item['author-link']);
  873. }
  874. $args = array('item' => $item, 'menu' => $menu);
  875. call_hooks('item_photo_menu', $args);
  876. $menu = $args['menu'];
  877. $o = '';
  878. foreach ($menu as $k => $v) {
  879. if (strpos($v, 'javascript:') === 0) {
  880. $v = substr($v, 11);
  881. $o .= '<li role="menuitem"><a onclick="' . $v . '">' . $k . '</a></li>' . PHP_EOL;
  882. } elseif ($v!='') {
  883. $o .= '<li role="menuitem"><a href="' . $v . '">' . $k . '</a></li>' . PHP_EOL;
  884. }
  885. }
  886. return $o;
  887. }
  888. if (! function_exists('builtin_activity_puller')) {
  889. /**
  890. * @brief Checks item to see if it is one of the builtin activities (like/dislike, event attendance, consensus items, etc.)
  891. * Increments the count of each matching activity and adds a link to the author as needed.
  892. *
  893. * @param array $item
  894. * @param array &$conv_responses (already created with builtin activity structure)
  895. * @return void
  896. */
  897. function builtin_activity_puller($item, &$conv_responses) {
  898. foreach ($conv_responses as $mode => $v) {
  899. $url = '';
  900. $sparkle = '';
  901. switch ($mode) {
  902. case 'like':
  903. $verb = ACTIVITY_LIKE;
  904. break;
  905. case 'dislike':
  906. $verb = ACTIVITY_DISLIKE;
  907. break;
  908. case 'attendyes':
  909. $verb = ACTIVITY_ATTEND;
  910. break;
  911. case 'attendno':
  912. $verb = ACTIVITY_ATTENDNO;
  913. break;
  914. case 'attendmaybe':
  915. $verb = ACTIVITY_ATTENDMAYBE;
  916. break;
  917. default:
  918. return;
  919. break;
  920. }
  921. if ((activity_match($item['verb'], $verb)) && ($item['id'] != $item['parent'])) {
  922. $url = $item['author-link'];
  923. if ((local_user()) && (local_user() == $item['uid']) && ($item['network'] === NETWORK_DFRN) && (! $item['self']) && (link_compare($item['author-link'], $item['url']))) {
  924. $url = 'redir/' . $item['contact-id'];
  925. $sparkle = ' class="sparkle" ';
  926. } else {
  927. $url = zrl($url);
  928. }
  929. $url = '<a href="'. $url . '"'. $sparkle .'>' . htmlentities($item['author-name']) . '</a>';
  930. if (! $item['thr-parent']) {
  931. $item['thr-parent'] = $item['parent-uri'];
  932. }
  933. if (! ((isset($conv_responses[$mode][$item['thr-parent'] . '-l']))
  934. && (is_array($conv_responses[$mode][$item['thr-parent'] . '-l'])))) {
  935. $conv_responses[$mode][$item['thr-parent'] . '-l'] = array();
  936. }
  937. // only list each unique author once
  938. if (in_array($url,$conv_responses[$mode][$item['thr-parent'] . '-l'])) {
  939. continue;
  940. }
  941. if (! isset($conv_responses[$mode][$item['thr-parent']])) {
  942. $conv_responses[$mode][$item['thr-parent']] = 1;
  943. } else {
  944. $conv_responses[$mode][$item['thr-parent']] ++;
  945. }
  946. if (public_contact() == $item['author-id']) {
  947. $conv_responses[$mode][$item['thr-parent'] . '-self'] = 1;
  948. }
  949. $conv_responses[$mode][$item['thr-parent'] . '-l'][] = $url;
  950. // there can only be one activity verb per item so if we found anything, we can stop looking
  951. return;
  952. }
  953. }
  954. }}
  955. if (! function_exists('format_like')) {
  956. /**
  957. * Format the vote text for a profile item
  958. * @param int $cnt = number of people who vote the item
  959. * @param array $arr = array of pre-linked names of likers/dislikers
  960. * @param string $type = one of 'like, 'dislike', 'attendyes', 'attendno', 'attendmaybe'
  961. * @param int $id = item id
  962. * @return formatted text
  963. */
  964. function format_like($cnt, array $arr, $type, $id) {
  965. $o = '';
  966. $expanded = '';
  967. if ($cnt == 1) {
  968. $likers = $arr[0];
  969. // Phrase if there is only one liker. In other cases it will be uses for the expanded
  970. // list which show all likers
  971. switch ($type) {
  972. case 'like' :
  973. $phrase = sprintf( t('%s likes this.'), $likers);
  974. break;
  975. case 'dislike' :
  976. $phrase = sprintf( t('%s doesn\'t like this.'), $likers);
  977. break;
  978. case 'attendyes' :
  979. $phrase = sprintf( t('%s attends.'), $likers);
  980. break;
  981. case 'attendno' :
  982. $phrase = sprintf( t('%s doesn\'t attend.'), $likers);
  983. break;
  984. case 'attendmaybe' :
  985. $phrase = sprintf( t('%s attends maybe.'), $likers);
  986. break;
  987. }
  988. }
  989. if ($cnt > 1) {
  990. $total = count($arr);
  991. if ($total >= MAX_LIKERS) {
  992. $arr = array_slice($arr, 0, MAX_LIKERS - 1);
  993. }
  994. if ($total < MAX_LIKERS) {
  995. $last = t('and') . ' ' . $arr[count($arr)-1];
  996. $arr2 = array_slice($arr, 0, -1);
  997. $str = implode(', ', $arr2) . ' ' . $last;
  998. }
  999. if ($total >= MAX_LIKERS) {
  1000. $str = implode(', ', $arr);
  1001. $str .= sprintf( t(', and %d other people'), $total - MAX_LIKERS );
  1002. }
  1003. $likers = $str;
  1004. $spanatts = "class=\"fakelink\" onclick=\"openClose('{$type}list-$id');\"";
  1005. switch ($type) {
  1006. case 'like':
  1007. $phrase = sprintf( t('<span %1$s>%2$d people</span> like this'), $spanatts, $cnt);
  1008. $explikers = sprintf( t('%s like this.'), $likers);
  1009. break;
  1010. case 'dislike':
  1011. $phrase = sprintf( t('<span %1$s>%2$d people</span> don\'t like this'), $spanatts, $cnt);
  1012. $explikers = sprintf( t('%s don\'t like this.'), $likers);
  1013. break;
  1014. case 'attendyes':
  1015. $phrase = sprintf( t('<span %1$s>%2$d people</span> attend'), $spanatts, $cnt);
  1016. $explikers = sprintf( t('%s attend.'), $likers);
  1017. break;
  1018. case 'attendno':
  1019. $phrase = sprintf( t('<span %1$s>%2$d people</span> don\'t attend'), $spanatts, $cnt);
  1020. $explikers = sprintf( t('%s don\'t attend.'), $likers);
  1021. break;
  1022. case 'attendmaybe':
  1023. $phrase = sprintf( t('<span %1$s>%2$d people</span> attend maybe'), $spanatts, $cnt);
  1024. $explikers = sprintf( t('%s anttend maybe.'), $likers);
  1025. break;
  1026. }
  1027. $expanded .= "\t" . '<div class="wall-item-' . $type . '-expanded" id="' . $type . 'list-' . $id . '" style="display: none;" >' . $explikers . EOL . '</div>';
  1028. }
  1029. $phrase .= EOL ;
  1030. $o .= replace_macros(get_markup_template('voting_fakelink.tpl'), array(
  1031. '$phrase' => $phrase,
  1032. '$type' => $type,
  1033. '$id' => $id
  1034. ));
  1035. $o .= $expanded;
  1036. return $o;
  1037. }}
  1038. function status_editor(App $a, $x, $notes_cid = 0, $popup = false) {
  1039. $o = '';
  1040. $geotag = (x($x, 'allow_location') ? replace_macros(get_markup_template('jot_geotag.tpl'), array()) : '');
  1041. $tpl = get_markup_template('jot-header.tpl');
  1042. $a->page['htmlhead'] .= replace_macros($tpl, array(
  1043. '$newpost' => 'true',
  1044. '$baseurl' => System::baseUrl(true),
  1045. '$geotag' => $geotag,
  1046. '$nickname' => $x['nickname'],
  1047. '$ispublic' => t('Visible to <strong>everybody</strong>'),
  1048. '$linkurl' => t('Please enter a link URL:'),
  1049. '$vidurl' => t("Please enter a video link/URL:"),
  1050. '$audurl' => t("Please enter an audio link/URL:"),
  1051. '$term' => t('Tag term:'),
  1052. '$fileas' => t('Save to Folder:'),
  1053. '$whereareu' => t('Where are you right now?'),
  1054. '$delitems' => t('Delete item(s)?')
  1055. ));
  1056. $tpl = get_markup_template('jot-end.tpl');
  1057. $a->page['end'] .= replace_macros($tpl, array(
  1058. '$newpost' => 'true',
  1059. '$baseurl' => System::baseUrl(true),
  1060. '$geotag' => $geotag,
  1061. '$nickname' => $x['nickname'],
  1062. '$ispublic' => t('Visible to <strong>everybody</strong>'),
  1063. '$linkurl' => t('Please enter a link URL:'),
  1064. '$vidurl' => t("Please enter a video link/URL:"),
  1065. '$audurl' => t("Please enter an audio link/URL:"),
  1066. '$term' => t('Tag term:'),
  1067. '$fileas' => t('Save to Folder:'),
  1068. '$whereareu' => t('Where are you right now?')
  1069. ));
  1070. $jotplugins = '';
  1071. call_hooks('jot_tool', $jotplugins);
  1072. // Private/public post links for the non-JS ACL form
  1073. $private_post = 1;
  1074. if (x($_REQUEST, 'public')) {
  1075. $private_post = 0;
  1076. }
  1077. $query_str = $a->query_string;
  1078. if (strpos($query_str, 'public=1') !== false) {
  1079. $query_str = str_replace(array('?public=1', '&public=1'), array('', ''), $query_str);
  1080. }
  1081. /*
  1082. * I think $a->query_string may never have ? in it, but I could be wrong
  1083. * It looks like it's from the index.php?q=[etc] rewrite that the web
  1084. * server does, which converts any ? to &, e.g. suggest&ignore=61 for suggest?ignore=61
  1085. */
  1086. if (strpos($query_str, '?') === false) {
  1087. $public_post_link = '?public=1';
  1088. } else {
  1089. $public_post_link = '&public=1';
  1090. }
  1091. // $tpl = replace_macros($tpl,array('$jotplugins' => $jotplugins));
  1092. $tpl = get_markup_template("jot.tpl");
  1093. $o .= replace_macros($tpl,array(
  1094. '$return_path' => $query_str,
  1095. '$action' => 'item',
  1096. '$share' => (x($x,'button') ? $x['button'] : t('Share')),
  1097. '$upload' => t('Upload photo'),
  1098. '$shortupload' => t('upload photo'),
  1099. '$attach' => t('Attach file'),
  1100. '$shortattach' => t('attach file'),
  1101. '$weblink' => t('Insert web link'),
  1102. '$shortweblink' => t('web link'),
  1103. '$video' => t('Insert video link'),
  1104. '$shortvideo' => t('video link'),
  1105. '$audio' => t('Insert audio link'),
  1106. '$shortaudio' => t('audio link'),
  1107. '$setloc' => t('Set your location'),
  1108. '$shortsetloc' => t('set location'),
  1109. '$noloc' => t('Clear browser location'),
  1110. '$shortnoloc' => t('clear location'),
  1111. '$title' => $x['title'],
  1112. '$placeholdertitle' => t('Set title'),
  1113. '$category' => $x['category'],
  1114. '$placeholdercategory' => (feature_enabled(local_user(), 'categories') ? t('Categories (comma-separated list)') : ''),
  1115. '$wait' => t('Please wait'),
  1116. '$permset' => t('Permission settings'),
  1117. '$shortpermset' => t('permissions'),
  1118. '$ptyp' => (($notes_cid) ? 'note' : 'wall'),
  1119. '$content' => $x['content'],
  1120. '$post_id' => $x['post_id'],
  1121. '$baseurl' => System::baseUrl(true),
  1122. '$defloc' => $x['default_location'],
  1123. '$visitor' => $x['visitor'],
  1124. '$pvisit' => (($notes_cid) ? 'none' : $x['visitor']),
  1125. '$public' => t('Public post'),
  1126. '$jotnets' => $jotnets,
  1127. '$lockstate' => $x['lockstate'],
  1128. '$bang' => $x['bang'],
  1129. '$profile_uid' => $x['profile_uid'],
  1130. '$preview' => ((feature_enabled($x['profile_uid'],'preview')) ? t('Preview') : ''),
  1131. '$jotplugins' => $jotplugins,
  1132. '$notes_cid' => $notes_cid,
  1133. '$sourceapp' => t($a->sourcename),
  1134. '$cancel' => t('Cancel'),
  1135. '$rand_num' => random_digits(12),
  1136. // ACL permissions box
  1137. '$acl' => $x['acl'],
  1138. '$acl_data' => $x['acl_data'],
  1139. '$group_perms' => t('Post to Groups'),
  1140. '$contact_perms' => t('Post to Contacts'),
  1141. '$private' => t('Private post'),
  1142. '$is_private' => $private_post,
  1143. '$public_link' => $public_post_link,
  1144. //jot nav tab (used in some themes)
  1145. '$message' => t('Message'),
  1146. '$browser' => t('Browser'),
  1147. ));
  1148. if ($popup == true) {
  1149. $o = '<div id="jot-popup" style="display: none;">'.$o.'</div>';
  1150. }
  1151. return $o;
  1152. }
  1153. /**
  1154. * Plucks the children of the given parent from a given item list.
  1155. *
  1156. * @brief Plucks all the children in the given item list of the given parent
  1157. *
  1158. * @param array $item_list
  1159. * @param array $parent
  1160. * @param bool $recursive
  1161. * @return type
  1162. */
  1163. function get_item_children(array &$item_list, array $parent, $recursive = true)
  1164. {
  1165. $children = [];
  1166. foreach ($item_list as $i => $item) {
  1167. if ($item['id'] != $item['parent']) {
  1168. if ($recursive) {
  1169. // Fallback to parent-uri if thr-parent is not set
  1170. $thr_parent = $item['thr-parent'];
  1171. if ($thr_parent == '') {
  1172. $thr_parent = $item['parent-uri'];
  1173. }
  1174. if ($thr_parent == $parent['uri']) {
  1175. $item['children'] = get_item_children($item_list, $item);
  1176. $children[] = $item;
  1177. unset($item_list[$i]);
  1178. }
  1179. } elseif ($item['parent'] == $parent['id']) {
  1180. $children[] = $item;
  1181. unset($item_list[$i]);
  1182. }
  1183. }
  1184. }
  1185. return $children;
  1186. }
  1187. /**
  1188. * @brief Recursively sorts a tree-like item array
  1189. *
  1190. * @param array $items
  1191. * @return array
  1192. */
  1193. function sort_item_children(array $items)
  1194. {
  1195. $result = $items;
  1196. usort($result, 'sort_thr_created_rev');
  1197. foreach ($result as $k => $i) {
  1198. if (isset($result[$k]['children'])) {
  1199. $result[$k]['children'] = sort_item_children($result[$k]['children']);
  1200. }
  1201. }
  1202. return $result;
  1203. }
  1204. /**
  1205. * @brief Recursively add all children items at the top level of a list
  1206. *
  1207. * @param array $children List of items to append
  1208. * @param array $item_list
  1209. */
  1210. function add_children_to_list(array $children, array &$item_list)
  1211. {
  1212. foreach ($children as $child) {
  1213. $item_list[] = $child;
  1214. if (isset($child['children'])) {
  1215. add_children_to_list($child['children'], $item_list);
  1216. }
  1217. }
  1218. }
  1219. /**
  1220. * This recursive function takes the item tree structure created by conv_sort() and
  1221. * flatten the extraneous depth levels when people reply sequentially, removing the
  1222. * stairs effect in threaded conversations limiting the available content width.
  1223. *
  1224. * The basic principle is the following: if a post item has only one reply and is
  1225. * the last reply of its parent, then the reply is moved to the parent.
  1226. *
  1227. * This process is rendered somewhat more complicated because items can be either
  1228. * replies or likes, and these don't factor at all in the reply count/last reply.
  1229. *
  1230. * @brief Selectively flattens a tree-like item structure to prevent threading stairs
  1231. *
  1232. * @param array $parent A tree-like array of items
  1233. * @return array
  1234. */
  1235. function smart_flatten_conversation(array $parent)
  1236. {
  1237. if (! isset($parent['children']) || count($parent['children']) == 0) {
  1238. return $parent;
  1239. }
  1240. // We use a for loop to ensure we process the newly-moved items
  1241. for ($i = 0; $i < count($parent['children']); $i++) {
  1242. $child = $parent['children'][$i];
  1243. if (isset($child['children']) && count($child['children'])) {
  1244. // This helps counting only the regular posts
  1245. $count_post_closure = function($var) {
  1246. return $var['verb'] === ACTIVITY_POST;
  1247. };
  1248. $child_post_count = count(array_filter($child['children'], $count_post_closure));
  1249. $remaining_post_count = count(array_filter(array_slice($parent['children'], $i), $count_post_closure));
  1250. // If there's only one child's children post and this is the last child post
  1251. if ($child_post_count == 1 && $remaining_post_count == 1) {
  1252. // Searches the post item in the children
  1253. $j = 0;
  1254. while($child['children'][$j]['verb'] !== ACTIVITY_POST && $j < count($child['children'])) {
  1255. $j ++;
  1256. }
  1257. $moved_item = $child['children'][$j];
  1258. unset($parent['children'][$i]['children'][$j]);
  1259. $parent['children'][] = $moved_item;
  1260. } else {
  1261. $parent['children'][$i] = smart_flatten_conversation($child);
  1262. }
  1263. }
  1264. }
  1265. return $parent;
  1266. }
  1267. /**
  1268. * Expands a flat list of items into corresponding tree-like conversation structures,
  1269. * sort the top-level posts either on "created" or "commented", and finally
  1270. * append all the items at the top level (???)
  1271. *
  1272. * @brief Expands a flat item list into a conversation array for display
  1273. *
  1274. * @param array $item_list A list of items belonging to one or more conversations
  1275. * @param string $order Either on "created" or "commented"
  1276. * @return array
  1277. */
  1278. function conv_sort(array $item_list, $order)
  1279. {
  1280. $parents = [];
  1281. if (!(is_array($item_list) && count($item_list))) {
  1282. return $parents;
  1283. }
  1284. $item_array = [];
  1285. // Dedupes the item list on the uri to prevent infinite loops
  1286. foreach ($item_list as $item) {
  1287. $item_array[$item['uri']] = $item;
  1288. }
  1289. // Extract the top level items
  1290. foreach ($item_array as $item) {
  1291. if ($item['id'] == $item['parent']) {
  1292. $parents[] = $item;
  1293. }
  1294. }
  1295. if (stristr($order, 'created')) {
  1296. usort($parents, 'sort_thr_created');
  1297. } elseif (stristr($order, 'commented')) {
  1298. usort($parents, 'sort_thr_commented');
  1299. }
  1300. $thread_allowed = Config::get('system', 'thread_allow') && get_app()->theme_thread_allow;
  1301. /*
  1302. * Plucks children from the item_array, second pass collects eventual orphan
  1303. * items and add them as children of their top-level post.
  1304. */
  1305. foreach ($parents as $i => $parent) {
  1306. $parents[$i]['children'] =
  1307. get_item_children($item_array, $parent, $thread_allowed)
  1308. + get_item_children($item_array, $parent, false);
  1309. }
  1310. foreach ($parents as $i => $parent) {
  1311. $parents[$i]['children'] = sort_item_children($parents[$i]['children']);
  1312. }
  1313. if ($thread_allowed && PConfig::get(local_user(), 'system', 'smart_threading', 0)) {
  1314. foreach ($parents as $i => $parent) {
  1315. $parents[$i] = smart_flatten_conversation($parent);
  1316. }
  1317. }
  1318. /// @TODO: Stop recusrsively adding all children back to the top level (!!!)
  1319. /// However, this apparently ensures responses (likes, attendance) display (?!)
  1320. foreach ($parents as $parent) {
  1321. if (count($parent['children'])) {
  1322. add_children_to_list($parent['children'], $parents);
  1323. }
  1324. }
  1325. return $parents;
  1326. }
  1327. /**
  1328. * @brief usort() callback to sort item arrays by the created key
  1329. *
  1330. * @param array $a
  1331. * @param array $b
  1332. * @return int
  1333. */
  1334. function sort_thr_created(array $a, array $b)
  1335. {
  1336. return strcmp($b['created'], $a['created']);
  1337. }
  1338. /**
  1339. * @brief usort() callback to reverse sort item arrays by the created key
  1340. *
  1341. * @param array $a
  1342. * @param array $b
  1343. * @return int
  1344. */
  1345. function sort_thr_created_rev(array $a, array $b)
  1346. {
  1347. return strcmp($a['created'], $b['created']);
  1348. }
  1349. /**
  1350. * @brief usort() callback to sort item arrays by the commented key
  1351. *
  1352. * @param array $a
  1353. * @param array $b
  1354. * @return type
  1355. */
  1356. function sort_thr_commented(array $a, array $b)
  1357. {
  1358. return strcmp($b['commented'], $a['commented']);
  1359. }
  1360. /// @TODO Add type-hint
  1361. function render_location_dummy($item) {
  1362. if ($item['location'] != "") {
  1363. return $item['location'];
  1364. }
  1365. if ($item['coord'] != "") {
  1366. return $item['coord'];
  1367. }
  1368. }
  1369. /// @TODO Add type-hint
  1370. function get_responses($conv_responses, $response_verbs, $ob, $item) {
  1371. $ret = array();
  1372. foreach ($response_verbs as $v) {
  1373. $ret[$v] = array();
  1374. $ret[$v]['count'] = ((x($conv_responses[$v], $item['uri'])) ? $conv_responses[$v][$item['uri']] : '');
  1375. $ret[$v]['list'] = ((x($conv_responses[$v], $item['uri'])) ? $conv_responses[$v][$item['uri'] . '-l'] : '');
  1376. $ret[$v]['self'] = ((x($conv_responses[$v], $item['uri'])) ? $conv_responses[$v][$item['uri'] . '-self'] : '0');
  1377. if (count($ret[$v]['list']) > MAX_LIKERS) {
  1378. $ret[$v]['list_part'] = array_slice($ret[$v]['list'], 0, MAX_LIKERS);
  1379. array_push($ret[$v]['list_part'], '<a href="#" data-toggle="modal" data-target="#' . $v . 'Modal-'
  1380. . (($ob) ? $ob->getId() : $item['id']) . '"><b>' . t('View all') . '</b></a>');
  1381. } else {
  1382. $ret[$v]['list_part'] = '';
  1383. }
  1384. $ret[$v]['button'] = get_response_button_text($v, $ret[$v]['count']);
  1385. $ret[$v]['title'] = $conv_responses[$v]['title'];
  1386. }
  1387. $count = 0;
  1388. foreach ($ret as $key) {
  1389. if ($key['count'] == true) {
  1390. $count++;
  1391. }
  1392. }
  1393. $ret['count'] = $count;
  1394. return $ret;
  1395. }
  1396. function get_response_button_text($v, $count) {
  1397. switch ($v) {
  1398. case 'like':
  1399. return tt('Like', 'Likes', $count, 'noun');
  1400. break;
  1401. case 'dislike':
  1402. return tt('Dislike', 'Dislikes', $count, 'noun');
  1403. break;
  1404. case 'attendyes':
  1405. return tt('Attending', 'Attending', $count, 'noun');
  1406. break;
  1407. case 'attendno':
  1408. return tt('Not Attending', 'Not Attending', $count, 'noun');
  1409. break;
  1410. case 'attendmaybe':
  1411. return tt('Undecided', 'Undecided', $count, 'noun');
  1412. break;
  1413. }
  1414. }