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.

1551 lines
45 KiB

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