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.

1572 lines
46KB

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