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.
 
 
 
 
 
 

1199 lines
36 KiB

  1. <?php
  2. require_once("include/bbcode.php");
  3. // Note: the code in 'item_extract_images' and 'item_redir_and_replace_images'
  4. // is identical to the code in mod/message.php for 'item_extract_images' and
  5. // 'item_redir_and_replace_images'
  6. if(! function_exists('item_extract_images')) {
  7. function item_extract_images($body) {
  8. $saved_image = array();
  9. $orig_body = $body;
  10. $new_body = '';
  11. $cnt = 0;
  12. $img_start = strpos($orig_body, '[img');
  13. $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
  14. $img_end = ($img_start !== false ? strpos(substr($orig_body, $img_start), '[/img]') : false);
  15. while(($img_st_close !== false) && ($img_end !== false)) {
  16. $img_st_close++; // make it point to AFTER the closing bracket
  17. $img_end += $img_start;
  18. if(! strcmp(substr($orig_body, $img_start + $img_st_close, 5), 'data:')) {
  19. // This is an embedded image
  20. $saved_image[$cnt] = substr($orig_body, $img_start + $img_st_close, $img_end - ($img_start + $img_st_close));
  21. $new_body = $new_body . substr($orig_body, 0, $img_start) . '[!#saved_image' . $cnt . '#!]';
  22. $cnt++;
  23. }
  24. else
  25. $new_body = $new_body . substr($orig_body, 0, $img_end + strlen('[/img]'));
  26. $orig_body = substr($orig_body, $img_end + strlen('[/img]'));
  27. if($orig_body === false) // in case the body ends on a closing image tag
  28. $orig_body = '';
  29. $img_start = strpos($orig_body, '[img');
  30. $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
  31. $img_end = ($img_start !== false ? strpos(substr($orig_body, $img_start), '[/img]') : false);
  32. }
  33. $new_body = $new_body . $orig_body;
  34. return array('body' => $new_body, 'images' => $saved_image);
  35. }}
  36. if(! function_exists('item_redir_and_replace_images')) {
  37. function item_redir_and_replace_images($body, $images, $cid) {
  38. $origbody = $body;
  39. $newbody = '';
  40. $cnt = 1;
  41. $pos = get_bb_tag_pos($origbody, 'url', 1);
  42. while($pos !== false && $cnt < 1000) {
  43. $search = '/\[url\=(.*?)\]\[!#saved_image([0-9]*)#!\]\[\/url\]' . '/is';
  44. $replace = '[url=' . z_path() . '/redir/' . $cid
  45. . '?f=1&url=' . '$1' . '][!#saved_image' . '$2' .'#!][/url]';
  46. $newbody .= substr($origbody, 0, $pos['start']['open']);
  47. $subject = substr($origbody, $pos['start']['open'], $pos['end']['close'] - $pos['start']['open']);
  48. $origbody = substr($origbody, $pos['end']['close']);
  49. if($origbody === false)
  50. $origbody = '';
  51. $subject = preg_replace($search, $replace, $subject);
  52. $newbody .= $subject;
  53. $cnt++;
  54. $pos = get_bb_tag_pos($origbody, 'url', 1);
  55. }
  56. $newbody .= $origbody;
  57. $cnt = 0;
  58. foreach($images as $image) {
  59. // We're depending on the property of 'foreach' (specified on the PHP website) that
  60. // it loops over the array starting from the first element and going sequentially
  61. // to the last element
  62. $newbody = str_replace('[!#saved_image' . $cnt . '#!]', '[img]' . $image . '[/img]', $newbody);
  63. $cnt++;
  64. }
  65. return $newbody;
  66. }}
  67. /**
  68. * Render actions localized
  69. */
  70. function localize_item(&$item){
  71. $extracted = item_extract_images($item['body']);
  72. if($extracted['images'])
  73. $item['body'] = item_redir_and_replace_images($extracted['body'], $extracted['images'], $item['contact-id']);
  74. $xmlhead="<"."?xml version='1.0' encoding='UTF-8' ?".">";
  75. if (activity_match($item['verb'],ACTIVITY_LIKE) || activity_match($item['verb'],ACTIVITY_DISLIKE)){
  76. $r = q("SELECT * from `item`,`contact` WHERE
  77. `item`.`contact-id`=`contact`.`id` AND `item`.`uri`='%s';",
  78. dbesc($item['parent-uri']));
  79. if(count($r)==0) return;
  80. $obj=$r[0];
  81. $author = '[url=' . $item['author-link'] . ']' . $item['author-name'] . '[/url]';
  82. $objauthor = '[url=' . $obj['author-link'] . ']' . $obj['author-name'] . '[/url]';
  83. switch($obj['verb']){
  84. case ACTIVITY_POST:
  85. switch ($obj['object-type']){
  86. case ACTIVITY_OBJ_EVENT:
  87. $post_type = t('event');
  88. break;
  89. default:
  90. $post_type = t('status');
  91. }
  92. break;
  93. default:
  94. if($obj['resource-id']){
  95. $post_type = t('photo');
  96. $m=array(); preg_match("/\[url=([^]]*)\]/", $obj['body'], $m);
  97. $rr['plink'] = $m[1];
  98. } else {
  99. $post_type = t('status');
  100. }
  101. }
  102. $plink = '[url=' . $obj['plink'] . ']' . $post_type . '[/url]';
  103. if(activity_match($item['verb'],ACTIVITY_LIKE)) {
  104. $bodyverb = t('%1$s likes %2$s\'s %3$s');
  105. }
  106. elseif(activity_match($item['verb'],ACTIVITY_DISLIKE)) {
  107. $bodyverb = t('%1$s doesn\'t like %2$s\'s %3$s');
  108. }
  109. $item['body'] = sprintf($bodyverb, $author, $objauthor, $plink);
  110. }
  111. if (activity_match($item['verb'],ACTIVITY_FRIEND)) {
  112. if ($item['object-type']=="" || $item['object-type']!== ACTIVITY_OBJ_PERSON) return;
  113. $Aname = $item['author-name'];
  114. $Alink = $item['author-link'];
  115. $xmlhead="<"."?xml version='1.0' encoding='UTF-8' ?".">";
  116. $obj = parse_xml_string($xmlhead.$item['object']);
  117. $links = parse_xml_string($xmlhead."<links>".unxmlify($obj->link)."</links>");
  118. $Bname = $obj->title;
  119. $Blink = ""; $Bphoto = "";
  120. foreach ($links->link as $l){
  121. $atts = $l->attributes();
  122. switch($atts['rel']){
  123. case "alternate": $Blink = $atts['href'];
  124. case "photo": $Bphoto = $atts['href'];
  125. }
  126. }
  127. $A = '[url=' . zrl($Alink) . ']' . $Aname . '[/url]';
  128. $B = '[url=' . zrl($Blink) . ']' . $Bname . '[/url]';
  129. if ($Bphoto!="") $Bphoto = '[url=' . zrl($Blink) . '][img]' . $Bphoto . '[/img][/url]';
  130. $item['body'] = sprintf( t('%1$s is now friends with %2$s'), $A, $B)."\n\n\n".$Bphoto;
  131. }
  132. if (stristr($item['verb'],ACTIVITY_POKE)) {
  133. $verb = urldecode(substr($item['verb'],strpos($item['verb'],'#')+1));
  134. if(! $verb)
  135. return;
  136. if ($item['object-type']=="" || $item['object-type']!== ACTIVITY_OBJ_PERSON) return;
  137. $Aname = $item['author-name'];
  138. $Alink = $item['author-link'];
  139. $xmlhead="<"."?xml version='1.0' encoding='UTF-8' ?".">";
  140. $obj = parse_xml_string($xmlhead.$item['object']);
  141. $links = parse_xml_string($xmlhead."<links>".unxmlify($obj->link)."</links>");
  142. $Bname = $obj->title;
  143. $Blink = ""; $Bphoto = "";
  144. foreach ($links->link as $l){
  145. $atts = $l->attributes();
  146. switch($atts['rel']){
  147. case "alternate": $Blink = $atts['href'];
  148. case "photo": $Bphoto = $atts['href'];
  149. }
  150. }
  151. $A = '[url=' . zrl($Alink) . ']' . $Aname . '[/url]';
  152. $B = '[url=' . zrl($Blink) . ']' . $Bname . '[/url]';
  153. if ($Bphoto!="") $Bphoto = '[url=' . zrl($Blink) . '][img=80x80]' . $Bphoto . '[/img][/url]';
  154. // we can't have a translation string with three positions but no distinguishable text
  155. // So here is the translate string.
  156. $txt = t('%1$s poked %2$s');
  157. // now translate the verb
  158. $txt = str_replace( t('poked'), t($verb), $txt);
  159. // then do the sprintf on the translation string
  160. $item['body'] = sprintf($txt, $A, $B). "\n\n\n" . $Bphoto;
  161. }
  162. if (stristr($item['verb'],ACTIVITY_MOOD)) {
  163. $verb = urldecode(substr($item['verb'],strpos($item['verb'],'#')+1));
  164. if(! $verb)
  165. return;
  166. $Aname = $item['author-name'];
  167. $Alink = $item['author-link'];
  168. $A = '[url=' . zrl($Alink) . ']' . $Aname . '[/url]';
  169. $txt = t('%1$s is currently %2$s');
  170. $item['body'] = sprintf($txt, $A, t($verb));
  171. }
  172. if (activity_match($item['verb'],ACTIVITY_TAG)) {
  173. $r = q("SELECT * from `item`,`contact` WHERE
  174. `item`.`contact-id`=`contact`.`id` AND `item`.`uri`='%s';",
  175. dbesc($item['parent-uri']));
  176. if(count($r)==0) return;
  177. $obj=$r[0];
  178. $author = '[url=' . zrl($item['author-link']) . ']' . $item['author-name'] . '[/url]';
  179. $objauthor = '[url=' . zrl($obj['author-link']) . ']' . $obj['author-name'] . '[/url]';
  180. switch($obj['verb']){
  181. case ACTIVITY_POST:
  182. switch ($obj['object-type']){
  183. case ACTIVITY_OBJ_EVENT:
  184. $post_type = t('event');
  185. break;
  186. default:
  187. $post_type = t('status');
  188. }
  189. break;
  190. default:
  191. if($obj['resource-id']){
  192. $post_type = t('photo');
  193. $m=array(); preg_match("/\[url=([^]]*)\]/", $obj['body'], $m);
  194. $rr['plink'] = $m[1];
  195. } else {
  196. $post_type = t('status');
  197. }
  198. }
  199. $plink = '[url=' . $obj['plink'] . ']' . $post_type . '[/url]';
  200. $parsedobj = parse_xml_string($xmlhead.$item['object']);
  201. $tag = sprintf('#[url=%s]%s[/url]', $parsedobj->id, $parsedobj->content);
  202. $item['body'] = sprintf( t('%1$s tagged %2$s\'s %3$s with %4$s'), $author, $objauthor, $plink, $tag );
  203. }
  204. if (activity_match($item['verb'],ACTIVITY_FAVORITE)){
  205. if ($item['object-type']== "")
  206. return;
  207. $Aname = $item['author-name'];
  208. $Alink = $item['author-link'];
  209. $xmlhead="<"."?xml version='1.0' encoding='UTF-8' ?".">";
  210. $obj = parse_xml_string($xmlhead.$item['object']);
  211. if(strlen($obj->id)) {
  212. $r = q("select * from item where uri = '%s' and uid = %d limit 1",
  213. dbesc($obj->id),
  214. intval($item['uid'])
  215. );
  216. if(count($r) && $r[0]['plink']) {
  217. $target = $r[0];
  218. $Bname = $target['author-name'];
  219. $Blink = $target['author-link'];
  220. $A = '[url=' . zrl($Alink) . ']' . $Aname . '[/url]';
  221. $B = '[url=' . zrl($Blink) . ']' . $Bname . '[/url]';
  222. $P = '[url=' . $target['plink'] . ']' . t('post/item') . '[/url]';
  223. $item['body'] = sprintf( t('%1$s marked %2$s\'s %3$s as favorite'), $A, $B, $P)."\n";
  224. }
  225. }
  226. }
  227. $matches = null;
  228. if(preg_match_all('/@\[url=(.*?)\]/is',$item['body'],$matches,PREG_SET_ORDER)) {
  229. foreach($matches as $mtch) {
  230. if(! strpos($mtch[1],'zrl='))
  231. $item['body'] = str_replace($mtch[0],'@[url=' . zrl($mtch[1]). ']',$item['body']);
  232. }
  233. }
  234. // add zrl's to public images
  235. $photo_pattern = "/\[url=(.*?)\/photos\/(.*?)\/image\/(.*?)\]\[img(.*?)\]h(.*?)\[\/img\]\[\/url\]/is";
  236. if(preg_match($photo_pattern,$item['body'])) {
  237. $photo_replace = '[url=' . zrl('$1' . '/photos/' . '$2' . '/image/' . '$3' ,true) . '][img' . '$4' . ']h' . '$5' . '[/img][/url]';
  238. $item['body'] = bb_tag_preg_replace($photo_pattern, $photo_replace, 'url', $item['body']);
  239. }
  240. // add sparkle links to appropriate permalinks
  241. $x = stristr($item['plink'],'/display/');
  242. if($x) {
  243. $sparkle = false;
  244. $y = best_link_url($item,$sparkle,true);
  245. if(strstr($y,'/redir/'))
  246. $item['plink'] = $y . '?f=&url=' . $item['plink'];
  247. }
  248. }
  249. /**
  250. * Count the total of comments on this item and its desendants
  251. */
  252. function count_descendants($item) {
  253. $total = count($item['children']);
  254. if($total > 0) {
  255. foreach($item['children'] as $child) {
  256. if(! visible_activity($child))
  257. $total --;
  258. $total += count_descendants($child);
  259. }
  260. }
  261. return $total;
  262. }
  263. function visible_activity($item) {
  264. if(activity_match($item['verb'],ACTIVITY_LIKE) || activity_match($item['verb'],ACTIVITY_DISLIKE))
  265. return false;
  266. if(activity_match($item['verb'],ACTIVITY_FOLLOW) && $item['object-type'] === ACTIVITY_OBJ_NOTE) {
  267. if(! (($item['self']) && ($item['uid'] == local_user()))) {
  268. return false;
  269. }
  270. }
  271. return true;
  272. }
  273. /**
  274. * "Render" a conversation or list of items for HTML display.
  275. * There are two major forms of display:
  276. * - Sequential or unthreaded ("New Item View" or search results)
  277. * - conversation view
  278. * The $mode parameter decides between the various renderings and also
  279. * figures out how to determine page owner and other contextual items
  280. * that are based on unique features of the calling module.
  281. *
  282. */
  283. if(!function_exists('conversation')) {
  284. function conversation(&$a, $items, $mode, $update, $preview = false) {
  285. require_once('include/bbcode.php');
  286. $ssl_state = ((local_user()) ? true : false);
  287. $profile_owner = 0;
  288. $page_writeable = false;
  289. $live_update_div = '';
  290. $previewing = (($preview) ? ' preview ' : '');
  291. if($mode === 'network') {
  292. $profile_owner = local_user();
  293. $page_writeable = true;
  294. if(!$update) {
  295. // The special div is needed for liveUpdate to kick in for this page.
  296. // We only launch liveUpdate if you aren't filtering in some incompatible
  297. // way and also you aren't writing a comment (discovered in javascript).
  298. $live_update_div = '<div id="live-network"></div>' . "\r\n"
  299. . "<script> var profile_uid = " . $_SESSION['uid']
  300. . "; var netargs = '" . substr($a->cmd,8)
  301. . '?f='
  302. . ((x($_GET,'cid')) ? '&cid=' . $_GET['cid'] : '')
  303. . ((x($_GET,'search')) ? '&search=' . $_GET['search'] : '')
  304. . ((x($_GET,'star')) ? '&star=' . $_GET['star'] : '')
  305. . ((x($_GET,'order')) ? '&order=' . $_GET['order'] : '')
  306. . ((x($_GET,'bmark')) ? '&bmark=' . $_GET['bmark'] : '')
  307. . ((x($_GET,'liked')) ? '&liked=' . $_GET['liked'] : '')
  308. . ((x($_GET,'conv')) ? '&conv=' . $_GET['conv'] : '')
  309. . ((x($_GET,'spam')) ? '&spam=' . $_GET['spam'] : '')
  310. . ((x($_GET,'nets')) ? '&nets=' . $_GET['nets'] : '')
  311. . ((x($_GET,'cmin')) ? '&cmin=' . $_GET['cmin'] : '')
  312. . ((x($_GET,'cmax')) ? '&cmax=' . $_GET['cmax'] : '')
  313. . ((x($_GET,'file')) ? '&file=' . $_GET['file'] : '')
  314. . "'; var profile_page = " . $a->pager['page'] . "; </script>\r\n";
  315. }
  316. }
  317. else if($mode === 'profile') {
  318. $profile_owner = $a->profile['profile_uid'];
  319. $page_writeable = can_write_wall($a,$profile_owner);
  320. if(!$update) {
  321. $tab = notags(trim($_GET['tab']));
  322. $tab = ( $tab ? $tab : 'posts' );
  323. if($tab === 'posts') {
  324. // This is ugly, but we can't pass the profile_uid through the session to the ajax updater,
  325. // because browser prefetching might change it on us. We have to deliver it with the page.
  326. $live_update_div = '<div id="live-profile"></div>' . "\r\n"
  327. . "<script> var profile_uid = " . $a->profile['profile_uid']
  328. . "; var netargs = '?f='; var profile_page = " . $a->pager['page'] . "; </script>\r\n";
  329. }
  330. }
  331. }
  332. else if($mode === 'notes') {
  333. $profile_owner = local_user();
  334. $page_writeable = true;
  335. if(!$update) {
  336. $live_update_div = '<div id="live-notes"></div>' . "\r\n"
  337. . "<script> var profile_uid = " . local_user()
  338. . "; var netargs = '/?f='; var profile_page = " . $a->pager['page'] . "; </script>\r\n";
  339. }
  340. }
  341. else if($mode === 'display') {
  342. $profile_owner = $a->profile['uid'];
  343. $page_writeable = can_write_wall($a,$profile_owner);
  344. if(!$update) {
  345. $live_update_div = '<div id="live-display"></div>' . "\r\n"
  346. . "<script> var profile_uid = " . $_SESSION['uid'] . ";"
  347. . " var profile_page = 1; </script>";
  348. }
  349. }
  350. else if($mode === 'community') {
  351. $profile_owner = 0;
  352. $page_writeable = false;
  353. if(!$update) {
  354. $live_update_div = '<div id="live-community"></div>' . "\r\n"
  355. . "<script> var profile_uid = -1; var netargs = '/?f='; var profile_page = " . $a->pager['page'] . "; </script>\r\n";
  356. }
  357. }
  358. else if($mode === 'search') {
  359. $live_update_div = '<div id="live-search"></div>' . "\r\n";
  360. }
  361. $page_dropping = ((local_user() && local_user() == $profile_owner) ? true : false);
  362. if($update)
  363. $return_url = $_SESSION['return_url'];
  364. else
  365. $return_url = $_SESSION['return_url'] = $a->query_string;
  366. load_contact_links(local_user());
  367. $cb = array('items' => $items, 'mode' => $mode, 'update' => $update, 'preview' => $preview);
  368. call_hooks('conversation_start',$cb);
  369. $items = $cb['items'];
  370. $cmnt_tpl = get_markup_template('comment_item.tpl');
  371. $hide_comments_tpl = get_markup_template('hide_comments.tpl');
  372. $alike = array();
  373. $dlike = array();
  374. // array with html for each thread (parent+comments)
  375. $threads = array();
  376. $threadsid = -1;
  377. $page_template = get_markup_template("conversation.tpl");
  378. if($items && count($items)) {
  379. if($mode === 'network-new' || $mode === 'search' || $mode === 'community') {
  380. // "New Item View" on network page or search page results
  381. // - just loop through the items and format them minimally for display
  382. // $tpl = get_markup_template('search_item.tpl');
  383. $tpl = 'search_item.tpl';
  384. foreach($items as $item) {
  385. $threadsid++;
  386. $comment = '';
  387. $owner_url = '';
  388. $owner_photo = '';
  389. $owner_name = '';
  390. $sparkle = '';
  391. if($mode === 'search' || $mode === 'community') {
  392. if(((activity_match($item['verb'],ACTIVITY_LIKE)) || (activity_match($item['verb'],ACTIVITY_DISLIKE)))
  393. && ($item['id'] != $item['parent']))
  394. continue;
  395. $nickname = $item['nickname'];
  396. }
  397. else
  398. $nickname = $a->user['nickname'];
  399. // prevent private email from leaking.
  400. if($item['network'] === NETWORK_MAIL && local_user() != $item['uid'])
  401. continue;
  402. $profile_name = ((strlen($item['author-name'])) ? $item['author-name'] : $item['name']);
  403. if($item['author-link'] && (! $item['author-name']))
  404. $profile_name = $item['author-link'];
  405. $tags=array();
  406. $hashtags = array();
  407. $mentions = array();
  408. $taglist = q("SELECT `type`, `term`, `url` FROM `term` WHERE `otype` = %d AND `oid` = %d AND `type` IN (%d, %d) ORDER BY `tid`",
  409. intval(TERM_OBJ_POST), intval($item['id']), intval(TERM_HASHTAG), intval(TERM_MENTION));
  410. foreach($taglist as $tag) {
  411. if ($tag["url"] == "")
  412. $tag["url"] = $searchpath.strtolower($tag["term"]);
  413. if ($tag["type"] == TERM_HASHTAG) {
  414. $hashtags[] = "#<a href=\"".$tag["url"]."\" target=\"external-link\">".$tag["term"]."</a>";
  415. $prefix = "#";
  416. } elseif ($tag["type"] == TERM_MENTION) {
  417. $mentions[] = "@<a href=\"".$tag["url"]."\" target=\"external-link\">".$tag["term"]."</a>";
  418. $prefix = "@";
  419. }
  420. $tags[] = $prefix."<a href=\"".$tag["url"]."\" target=\"external-link\">".$tag["term"]."</a>";
  421. }
  422. /*foreach(explode(',',$item['tag']) as $tag){
  423. $tag = trim($tag);
  424. if ($tag!="") {
  425. $t = bbcode($tag);
  426. $tags[] = $t;
  427. if($t[0] == '#')
  428. $hashtags[] = $t;
  429. elseif($t[0] == '@')
  430. $mentions[] = $t;
  431. }
  432. }*/
  433. $sp = false;
  434. $profile_link = best_link_url($item,$sp);
  435. if($profile_link === 'mailbox')
  436. $profile_link = '';
  437. if($sp)
  438. $sparkle = ' sparkle';
  439. else
  440. $profile_link = zrl($profile_link);
  441. $normalised = normalise_link((strlen($item['author-link'])) ? $item['author-link'] : $item['url']);
  442. if(($normalised != 'mailbox') && (x($a->contacts[$normalised])))
  443. $profile_avatar = $a->contacts[$normalised]['thumb'];
  444. else
  445. $profile_avatar = ((strlen($item['author-avatar'])) ? $a->get_cached_avatar_image($item['author-avatar']) : $item['thumb']);
  446. $locate = array('location' => $item['location'], 'coord' => $item['coord'], 'html' => '');
  447. call_hooks('render_location',$locate);
  448. $location = ((strlen($locate['html'])) ? $locate['html'] : render_location_google($locate));
  449. localize_item($item);
  450. if($mode === 'network-new')
  451. $dropping = true;
  452. else
  453. $dropping = false;
  454. $drop = array(
  455. 'dropping' => $dropping,
  456. 'pagedrop' => $page_dropping,
  457. 'select' => t('Select'),
  458. 'delete' => t('Delete'),
  459. );
  460. $star = false;
  461. $isstarred = "unstarred";
  462. $lock = false;
  463. $likebuttons = false;
  464. $shareable = false;
  465. $body = prepare_body($item,true);
  466. list($categories, $folders) = get_cats_and_terms($item);
  467. if($a->theme['template_engine'] === 'internal') {
  468. $profile_name_e = template_escape($profile_name);
  469. $item['title_e'] = template_escape($item['title']);
  470. $body_e = template_escape($body);
  471. $tags_e = template_escape($tags);
  472. $hashtags_e = template_escape($hashtags);
  473. $mentions_e = template_escape($mentions);
  474. $location_e = template_escape($location);
  475. $owner_name_e = template_escape($owner_name);
  476. }
  477. else {
  478. $profile_name_e = $profile_name;
  479. $item['title_e'] = $item['title'];
  480. $body_e = $body;
  481. $tags_e = $tags;
  482. $hashtags_e = $hashtags;
  483. $mentions_e = $mentions;
  484. $location_e = $location;
  485. $owner_name_e = $owner_name;
  486. }
  487. $tmp_item = array(
  488. 'template' => $tpl,
  489. 'id' => (($preview) ? 'P0' : $item['item_id']),
  490. 'linktitle' => sprintf( t('View %s\'s profile @ %s'), $profile_name, ((strlen($item['author-link'])) ? $item['author-link'] : $item['url'])),
  491. 'profile_url' => $profile_link,
  492. 'item_photo_menu' => item_photo_menu($item),
  493. 'name' => $profile_name_e,
  494. 'sparkle' => $sparkle,
  495. 'lock' => $lock,
  496. 'thumb' => $profile_avatar,
  497. 'title' => $item['title_e'],
  498. 'body' => $body_e,
  499. 'tags' => $tags_e,
  500. 'hashtags' => $hashtags_e,
  501. 'mentions' => $mentions_e,
  502. 'txt_cats' => t('Categories:'),
  503. 'txt_folders' => t('Filed under:'),
  504. 'has_cats' => ((count($categories)) ? 'true' : ''),
  505. 'has_folders' => ((count($folders)) ? 'true' : ''),
  506. 'categories' => $categories,
  507. 'folders' => $folders,
  508. 'text' => strip_tags($body_e),
  509. 'localtime' => datetime_convert('UTC', date_default_timezone_get(), $item['created'], 'r'),
  510. 'ago' => (($item['app']) ? sprintf( t('%s from %s'),relative_date($item['created']),$item['app']) : relative_date($item['created'])),
  511. 'location' => $location_e,
  512. 'indent' => '',
  513. 'owner_name' => $owner_name_e,
  514. 'owner_url' => $owner_url,
  515. 'owner_photo' => $owner_photo,
  516. 'plink' => get_plink($item),
  517. 'edpost' => false,
  518. 'isstarred' => $isstarred,
  519. 'star' => $star,
  520. 'drop' => $drop,
  521. 'vote' => $likebuttons,
  522. 'like' => '',
  523. 'dislike' => '',
  524. 'comment' => '',
  525. 'conv' => (($preview) ? '' : array('href'=> $a->get_baseurl($ssl_state) . '/display/' . $nickname . '/' . $item['id'], 'title'=> t('View in context'))),
  526. 'previewing' => $previewing,
  527. 'wait' => t('Please wait'),
  528. 'thread_level' => 1,
  529. );
  530. $arr = array('item' => $item, 'output' => $tmp_item);
  531. call_hooks('display_item', $arr);
  532. $threads[$threadsid]['id'] = $item['item_id'];
  533. $threads[$threadsid]['items'] = array($arr['output']);
  534. }
  535. }
  536. else
  537. {
  538. // Normal View
  539. $page_template = get_markup_template("threaded_conversation.tpl");
  540. require_once('object/Conversation.php');
  541. require_once('object/Item.php');
  542. $conv = new Conversation($mode, $preview);
  543. // get all the topmost parents
  544. // this shouldn't be needed, as we should have only them in our array
  545. // But for now, this array respects the old style, just in case
  546. $threads = array();
  547. foreach($items as $item) {
  548. // Can we put this after the visibility check?
  549. like_puller($a,$item,$alike,'like');
  550. like_puller($a,$item,$dlike,'dislike');
  551. // Only add what is visible
  552. if($item['network'] === NETWORK_MAIL && local_user() != $item['uid']) {
  553. continue;
  554. }
  555. if(! visible_activity($item)) {
  556. continue;
  557. }
  558. $item['pagedrop'] = $page_dropping;
  559. if($item['id'] == $item['parent']) {
  560. $item_object = new Item($item);
  561. $conv->add_thread($item_object);
  562. }
  563. }
  564. $threads = $conv->get_template_data($alike, $dlike);
  565. if(!$threads) {
  566. logger('[ERROR] conversation : Failed to get template data.', LOGGER_DEBUG);
  567. $threads = array();
  568. }
  569. }
  570. }
  571. $o = replace_macros($page_template, array(
  572. '$baseurl' => $a->get_baseurl($ssl_state),
  573. '$live_update' => $live_update_div,
  574. '$remove' => t('remove'),
  575. '$mode' => $mode,
  576. '$user' => $a->user,
  577. '$threads' => $threads,
  578. '$dropping' => ($page_dropping && feature_enabled(local_user(),'multi_delete') ? t('Delete Selected Items') : False),
  579. ));
  580. return $o;
  581. }}
  582. function best_link_url($item,&$sparkle,$ssl_state = false) {
  583. $a = get_app();
  584. $best_url = '';
  585. $sparkle = false;
  586. $clean_url = normalise_link($item['author-link']);
  587. if((local_user()) && (local_user() == $item['uid'])) {
  588. if(isset($a->contacts) && x($a->contacts,$clean_url)) {
  589. if($a->contacts[$clean_url]['network'] === NETWORK_DFRN) {
  590. $best_url = $a->get_baseurl($ssl_state) . '/redir/' . $a->contacts[$clean_url]['id'];
  591. $sparkle = true;
  592. }
  593. else
  594. $best_url = $a->contacts[$clean_url]['url'];
  595. }
  596. }
  597. if(! $best_url) {
  598. if(strlen($item['author-link']))
  599. $best_url = $item['author-link'];
  600. else
  601. $best_url = $item['url'];
  602. }
  603. return $best_url;
  604. }
  605. if(! function_exists('item_photo_menu')){
  606. function item_photo_menu($item){
  607. $a = get_app();
  608. $ssl_state = false;
  609. if(local_user()) {
  610. $ssl_state = true;
  611. if(! count($a->contacts))
  612. load_contact_links(local_user());
  613. }
  614. $sub_link="";
  615. $poke_link="";
  616. $contact_url="";
  617. $pm_url="";
  618. $status_link="";
  619. $photos_link="";
  620. $posts_link="";
  621. if((local_user()) && local_user() == $item['uid'] && $item['parent'] == $item['id'] && (! $item['self'])) {
  622. $sub_link = 'javascript:dosubthread(' . $item['id'] . '); return false;';
  623. }
  624. $sparkle = false;
  625. $profile_link = best_link_url($item,$sparkle,$ssl_state);
  626. if($profile_link === 'mailbox')
  627. $profile_link = '';
  628. if($sparkle) {
  629. $cid = intval(basename($profile_link));
  630. $status_link = $profile_link . "?url=status";
  631. $photos_link = $profile_link . "?url=photos";
  632. $profile_link = $profile_link . "?url=profile";
  633. $pm_url = $a->get_baseurl($ssl_state) . '/message/new/' . $cid;
  634. $zurl = '';
  635. }
  636. else {
  637. $profile_link = zrl($profile_link);
  638. if(local_user() && local_user() == $item['uid'] && link_compare($item['url'],$item['author-link'])) {
  639. $cid = $item['contact-id'];
  640. }
  641. else {
  642. $cid = 0;
  643. }
  644. }
  645. if(($cid) && (! $item['self'])) {
  646. $poke_link = $a->get_baseurl($ssl_state) . '/poke/?f=&c=' . $cid;
  647. $contact_url = $a->get_baseurl($ssl_state) . '/contacts/' . $cid;
  648. $posts_link = $a->get_baseurl($ssl_state) . '/network/?cid=' . $cid;
  649. $clean_url = normalise_link($item['author-link']);
  650. if((local_user()) && (local_user() == $item['uid'])) {
  651. if(isset($a->contacts) && x($a->contacts,$clean_url)) {
  652. if($a->contacts[$clean_url]['network'] === NETWORK_DIASPORA) {
  653. $pm_url = $a->get_baseurl($ssl_state) . '/message/new/' . $cid;
  654. }
  655. }
  656. }
  657. }
  658. $menu = Array(
  659. t("Follow Thread") => $sub_link,
  660. t("View Status") => $status_link,
  661. t("View Profile") => $profile_link,
  662. t("View Photos") => $photos_link,
  663. t("Network Posts") => $posts_link,
  664. t("Edit Contact") => $contact_url,
  665. t("Send PM") => $pm_url,
  666. t("Poke") => $poke_link
  667. );
  668. $args = array('item' => $item, 'menu' => $menu);
  669. call_hooks('item_photo_menu', $args);
  670. $menu = $args['menu'];
  671. $o = "";
  672. foreach($menu as $k=>$v){
  673. if(strpos($v,'javascript:') === 0) {
  674. $v = substr($v,11);
  675. $o .= "<li><a href=\"#\" onclick=\"$v\">$k</a></li>\n";
  676. }
  677. elseif ($v!="") $o .= "<li><a href=\"$v\">$k</a></li>\n";
  678. }
  679. return $o;
  680. }}
  681. if(! function_exists('like_puller')) {
  682. function like_puller($a,$item,&$arr,$mode) {
  683. $url = '';
  684. $sparkle = '';
  685. $verb = (($mode === 'like') ? ACTIVITY_LIKE : ACTIVITY_DISLIKE);
  686. if((activity_match($item['verb'],$verb)) && ($item['id'] != $item['parent'])) {
  687. $url = $item['author-link'];
  688. if((local_user()) && (local_user() == $item['uid']) && ($item['network'] === 'dfrn') && (! $item['self']) && (link_compare($item['author-link'],$item['url']))) {
  689. $url = $a->get_baseurl(true) . '/redir/' . $item['contact-id'];
  690. $sparkle = ' class="sparkle" ';
  691. }
  692. else
  693. $url = zrl($url);
  694. if(! $item['thr-parent'])
  695. $item['thr-parent'] = $item['parent-uri'];
  696. if(! ((isset($arr[$item['thr-parent'] . '-l'])) && (is_array($arr[$item['thr-parent'] . '-l']))))
  697. $arr[$item['thr-parent'] . '-l'] = array();
  698. if(! isset($arr[$item['thr-parent']]))
  699. $arr[$item['thr-parent']] = 1;
  700. else
  701. $arr[$item['thr-parent']] ++;
  702. $arr[$item['thr-parent'] . '-l'][] = '<a href="'. $url . '"'. $sparkle .'>' . $item['author-name'] . '</a>';
  703. }
  704. return;
  705. }}
  706. // Format the like/dislike text for a profile item
  707. // $cnt = number of people who like/dislike the item
  708. // $arr = array of pre-linked names of likers/dislikers
  709. // $type = one of 'like, 'dislike'
  710. // $id = item id
  711. // returns formatted text
  712. if(! function_exists('format_like')) {
  713. function format_like($cnt,$arr,$type,$id) {
  714. $o = '';
  715. if($cnt == 1)
  716. $o .= (($type === 'like') ? sprintf( t('%s likes this.'), $arr[0]) : sprintf( t('%s doesn\'t like this.'), $arr[0])) . EOL ;
  717. else {
  718. $spanatts = "class=\"fakelink\" onclick=\"openClose('{$type}list-$id');\"";
  719. switch($type) {
  720. case 'like':
  721. $phrase = sprintf( t('<span %1$s>%2$d people</span> like this'), $spanatts, $cnt);
  722. break;
  723. case 'dislike':
  724. $phrase = sprintf( t('<span %1$s>%2$d people</span> don\'t like this'), $spanatts, $cnt);
  725. break;
  726. }
  727. $phrase .= EOL ;
  728. $o .= replace_macros(get_markup_template('voting_fakelink.tpl'), array(
  729. '$phrase' => $phrase,
  730. '$type' => $type,
  731. '$id' => $id
  732. ));
  733. $total = count($arr);
  734. if($total >= MAX_LIKERS)
  735. $arr = array_slice($arr, 0, MAX_LIKERS - 1);
  736. if($total < MAX_LIKERS) {
  737. $last = t('and') . ' ' . $arr[count($arr)-1];
  738. $arr2 = array_slice($arr, 0, -1);
  739. $str = implode(', ', $arr2) . ' ' . $last;
  740. }
  741. if($total >= MAX_LIKERS) {
  742. $str = implode(', ', $arr);
  743. $str .= sprintf( t(', and %d other people'), $total - MAX_LIKERS );
  744. }
  745. $str = (($type === 'like') ? sprintf( t('%s like this.'), $str) : sprintf( t('%s don\'t like this.'), $str));
  746. $o .= "\t" . '<div id="' . $type . 'list-' . $id . '" style="display: none;" >' . $str . '</div>';
  747. }
  748. return $o;
  749. }}
  750. function status_editor($a,$x, $notes_cid = 0, $popup=false) {
  751. $o = '';
  752. $geotag = (($x['allow_location']) ? replace_macros(get_markup_template('jot_geotag.tpl'), array()) : '');
  753. /* $plaintext = false;
  754. if( local_user() && (intval(get_pconfig(local_user(),'system','plaintext')) || !feature_enabled(local_user(),'richtext')) )
  755. $plaintext = true;*/
  756. $plaintext = true;
  757. if( local_user() && feature_enabled(local_user(),'richtext') )
  758. $plaintext = false;
  759. $tpl = get_markup_template('jot-header.tpl');
  760. $a->page['htmlhead'] .= replace_macros($tpl, array(
  761. '$newpost' => 'true',
  762. '$baseurl' => $a->get_baseurl(true),
  763. '$editselect' => (($plaintext) ? 'none' : '/(profile-jot-text|prvmail-text)/'),
  764. '$geotag' => $geotag,
  765. '$nickname' => $x['nickname'],
  766. '$ispublic' => t('Visible to <strong>everybody</strong>'),
  767. '$linkurl' => t('Please enter a link URL:'),
  768. '$vidurl' => t("Please enter a video link/URL:"),
  769. '$audurl' => t("Please enter an audio link/URL:"),
  770. '$term' => t('Tag term:'),
  771. '$fileas' => t('Save to Folder:'),
  772. '$whereareu' => t('Where are you right now?'),
  773. '$delitems' => t('Delete item(s)?')
  774. ));
  775. $tpl = get_markup_template('jot-end.tpl');
  776. $a->page['end'] .= replace_macros($tpl, array(
  777. '$newpost' => 'true',
  778. '$baseurl' => $a->get_baseurl(true),
  779. '$editselect' => (($plaintext) ? 'none' : '/(profile-jot-text|prvmail-text)/'),
  780. '$geotag' => $geotag,
  781. '$nickname' => $x['nickname'],
  782. '$ispublic' => t('Visible to <strong>everybody</strong>'),
  783. '$linkurl' => t('Please enter a link URL:'),
  784. '$vidurl' => t("Please enter a video link/URL:"),
  785. '$audurl' => t("Please enter an audio link/URL:"),
  786. '$term' => t('Tag term:'),
  787. '$fileas' => t('Save to Folder:'),
  788. '$whereareu' => t('Where are you right now?')
  789. ));
  790. $tpl = get_markup_template("jot.tpl");
  791. $jotplugins = '';
  792. $jotnets = '';
  793. $mail_disabled = ((function_exists('imap_open') && (! get_config('system','imap_disabled'))) ? 0 : 1);
  794. $mail_enabled = false;
  795. $pubmail_enabled = false;
  796. if(($x['is_owner']) && (! $mail_disabled)) {
  797. $r = q("SELECT * FROM `mailacct` WHERE `uid` = %d AND `server` != '' LIMIT 1",
  798. intval(local_user())
  799. );
  800. if(count($r)) {
  801. $mail_enabled = true;
  802. if(intval($r[0]['pubmail']))
  803. $pubmail_enabled = true;
  804. }
  805. }
  806. if($mail_enabled) {
  807. $selected = (($pubmail_enabled) ? ' checked="checked" ' : '');
  808. $jotnets .= '<div class="profile-jot-net"><input type="checkbox" name="pubmail_enable"' . $selected . ' value="1" /> ' . t("Post to Email") . '</div>';
  809. }
  810. call_hooks('jot_tool', $jotplugins);
  811. call_hooks('jot_networks', $jotnets);
  812. if($notes_cid)
  813. $jotnets .= '<input type="hidden" name="contact_allow[]" value="' . $notes_cid .'" />';
  814. // $tpl = replace_macros($tpl,array('$jotplugins' => $jotplugins));
  815. $o .= replace_macros($tpl,array(
  816. '$return_path' => $a->query_string,
  817. '$action' => $a->get_baseurl(true) . '/item',
  818. '$share' => (x($x,'button') ? $x['button'] : t('Share')),
  819. '$upload' => t('Upload photo'),
  820. '$shortupload' => t('upload photo'),
  821. '$attach' => t('Attach file'),
  822. '$shortattach' => t('attach file'),
  823. '$weblink' => t('Insert web link'),
  824. '$shortweblink' => t('web link'),
  825. '$video' => t('Insert video link'),
  826. '$shortvideo' => t('video link'),
  827. '$audio' => t('Insert audio link'),
  828. '$shortaudio' => t('audio link'),
  829. '$setloc' => t('Set your location'),
  830. '$shortsetloc' => t('set location'),
  831. '$noloc' => t('Clear browser location'),
  832. '$shortnoloc' => t('clear location'),
  833. '$title' => "",
  834. '$placeholdertitle' => t('Set title'),
  835. '$category' => "",
  836. '$placeholdercategory' => (feature_enabled(local_user(),'categories') ? t('Categories (comma-separated list)') : ''),
  837. '$wait' => t('Please wait'),
  838. '$permset' => t('Permission settings'),
  839. '$shortpermset' => t('permissions'),
  840. '$ptyp' => (($notes_cid) ? 'note' : 'wall'),
  841. '$content' => '',
  842. '$post_id' => '',
  843. '$baseurl' => $a->get_baseurl(true),
  844. '$defloc' => $x['default_location'],
  845. '$visitor' => $x['visitor'],
  846. '$pvisit' => (($notes_cid) ? 'none' : $x['visitor']),
  847. '$emailcc' => t('CC: email addresses'),
  848. '$public' => t('Public post'),
  849. '$jotnets' => $jotnets,
  850. '$emtitle' => t('Example: bob@example.com, mary@example.com'),
  851. '$lockstate' => $x['lockstate'],
  852. '$acl' => $x['acl'],
  853. '$bang' => $x['bang'],
  854. '$profile_uid' => $x['profile_uid'],
  855. '$preview' => ((feature_enabled($x['profile_uid'],'preview')) ? t('Preview') : ''),
  856. '$jotplugins' => $jotplugins,
  857. '$sourceapp' => t($a->sourcename),
  858. '$cancel' => t('Cancel'),
  859. '$rand_num' => random_digits(12)
  860. ));
  861. if ($popup==true){
  862. $o = '<div id="jot-popup" style="display: none;">'.$o.'</div>';
  863. }
  864. return $o;
  865. }
  866. function get_item_children($arr, $parent) {
  867. $children = array();
  868. $a = get_app();
  869. foreach($arr as $item) {
  870. if($item['id'] != $item['parent']) {
  871. if(get_config('system','thread_allow') && $a->theme_thread_allow) {
  872. // Fallback to parent-uri if thr-parent is not set
  873. $thr_parent = $item['thr-parent'];
  874. if($thr_parent == '')
  875. $thr_parent = $item['parent-uri'];
  876. if($thr_parent == $parent['uri']) {
  877. $item['children'] = get_item_children($arr, $item);
  878. $children[] = $item;
  879. }
  880. }
  881. else if($item['parent'] == $parent['id']) {
  882. $children[] = $item;
  883. }
  884. }
  885. }
  886. return $children;
  887. }
  888. function sort_item_children($items) {
  889. $result = $items;
  890. usort($result,'sort_thr_created_rev');
  891. foreach($result as $k => $i) {
  892. if(count($result[$k]['children'])) {
  893. $result[$k]['children'] = sort_item_children($result[$k]['children']);
  894. }
  895. }
  896. return $result;
  897. }
  898. function add_children_to_list($children, &$arr) {
  899. foreach($children as $y) {
  900. $arr[] = $y;
  901. if(count($y['children']))
  902. add_children_to_list($y['children'], $arr);
  903. }
  904. }
  905. function conv_sort($arr,$order) {
  906. if((!(is_array($arr) && count($arr))))
  907. return array();
  908. $parents = array();
  909. $children = array();
  910. foreach($arr as $x)
  911. if($x['id'] == $x['parent'])
  912. $parents[] = $x;
  913. if(stristr($order,'created'))
  914. usort($parents,'sort_thr_created');
  915. elseif(stristr($order,'commented'))
  916. usort($parents,'sort_thr_commented');
  917. if(count($parents))
  918. foreach($parents as $i=>$_x)
  919. $parents[$i]['children'] = get_item_children($arr, $_x);
  920. /*foreach($arr as $x) {
  921. if($x['id'] != $x['parent']) {
  922. $p = find_thread_parent_index($parents,$x);
  923. if($p !== false)
  924. $parents[$p]['children'][] = $x;
  925. }
  926. }*/
  927. if(count($parents)) {
  928. foreach($parents as $k => $v) {
  929. if(count($parents[$k]['children'])) {
  930. $parents[$k]['children'] = sort_item_children($parents[$k]['children']);
  931. /*$y = $parents[$k]['children'];
  932. usort($y,'sort_thr_created_rev');
  933. $parents[$k]['children'] = $y;*/
  934. }
  935. }
  936. }
  937. $ret = array();
  938. if(count($parents)) {
  939. foreach($parents as $x) {
  940. $ret[] = $x;
  941. if(count($x['children']))
  942. add_children_to_list($x['children'], $ret);
  943. /*foreach($x['children'] as $y)
  944. $ret[] = $y;*/
  945. }
  946. }
  947. return $ret;
  948. }
  949. function sort_thr_created($a,$b) {
  950. return strcmp($b['created'],$a['created']);
  951. }
  952. function sort_thr_created_rev($a,$b) {
  953. return strcmp($a['created'],$b['created']);
  954. }
  955. function sort_thr_commented($a,$b) {
  956. return strcmp($b['commented'],$a['commented']);
  957. }
  958. function find_thread_parent_index($arr,$x) {
  959. foreach($arr as $k => $v)
  960. if($v['id'] == $x['parent'])
  961. return $k;
  962. return false;
  963. }
  964. function render_location_google($item) {
  965. $location = (($item['location']) ? '<a target="map" title="' . $item['location'] . '" href="http://maps.google.com/?q=' . urlencode($item['location']) . '">' . $item['location'] . '</a>' : '');
  966. $coord = (($item['coord']) ? '<a target="map" title="' . $item['coord'] . '" href="http://maps.google.com/?q=' . urlencode($item['coord']) . '">' . $item['coord'] . '</a>' : '');
  967. if($coord) {
  968. if($location)
  969. $location .= '<br /><span class="smalltext">(' . $coord . ')</span>';
  970. else
  971. $location = '<span class="smalltext">' . $coord . '</span>';
  972. }
  973. return $location;
  974. }