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.
 
 
 
 
 
 

1134 lines
34 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('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. if($tab === 'posts') {
  323. // This is ugly, but we can't pass the profile_uid through the session to the ajax updater,
  324. // because browser prefetching might change it on us. We have to deliver it with the page.
  325. $live_update_div = '<div id="live-profile"></div>' . "\r\n"
  326. . "<script> var profile_uid = " . $a->profile['profile_uid']
  327. . "; var netargs = '?f='; var profile_page = " . $a->pager['page'] . "; </script>\r\n";
  328. }
  329. }
  330. }
  331. else if($mode === 'notes') {
  332. $profile_owner = local_user();
  333. $page_writeable = true;
  334. if(!$update) {
  335. $live_update_div = '<div id="live-notes"></div>' . "\r\n"
  336. . "<script> var profile_uid = " . local_user()
  337. . "; var netargs = '/?f='; var profile_page = " . $a->pager['page'] . "; </script>\r\n";
  338. }
  339. }
  340. else if($mode === 'display') {
  341. $profile_owner = $a->profile['uid'];
  342. $page_writeable = can_write_wall($a,$profile_owner);
  343. $live_update_div = '<div id="live-display"></div>' . "\r\n";
  344. }
  345. else if($mode === 'community') {
  346. $profile_owner = 0;
  347. $page_writeable = false;
  348. if(!$update) {
  349. $live_update_div = '<div id="live-community"></div>' . "\r\n"
  350. . "<script> var profile_uid = -1; var netargs = '/?f='; var profile_page = " . $a->pager['page'] . "; </script>\r\n";
  351. }
  352. }
  353. else if($mode === 'search') {
  354. $live_update_div = '<div id="live-search"></div>' . "\r\n";
  355. }
  356. $page_dropping = ((local_user() && local_user() == $profile_owner) ? true : false);
  357. if($update)
  358. $return_url = $_SESSION['return_url'];
  359. else
  360. $return_url = $_SESSION['return_url'] = $a->query_string;
  361. load_contact_links(local_user());
  362. $cb = array('items' => $items, 'mode' => $mode, 'update' => $update, 'preview' => $preview);
  363. call_hooks('conversation_start',$cb);
  364. $items = $cb['items'];
  365. $cmnt_tpl = get_markup_template('comment_item.tpl');
  366. $hide_comments_tpl = get_markup_template('hide_comments.tpl');
  367. $alike = array();
  368. $dlike = array();
  369. // array with html for each thread (parent+comments)
  370. $threads = array();
  371. $threadsid = -1;
  372. $page_template = get_markup_template("conversation.tpl");
  373. if($items && count($items)) {
  374. if($mode === 'network-new' || $mode === 'search' || $mode === 'community') {
  375. // "New Item View" on network page or search page results
  376. // - just loop through the items and format them minimally for display
  377. // $tpl = get_markup_template('search_item.tpl');
  378. $tpl = 'search_item.tpl';
  379. foreach($items as $item) {
  380. $threadsid++;
  381. $comment = '';
  382. $owner_url = '';
  383. $owner_photo = '';
  384. $owner_name = '';
  385. $sparkle = '';
  386. if($mode === 'search' || $mode === 'community') {
  387. if(((activity_match($item['verb'],ACTIVITY_LIKE)) || (activity_match($item['verb'],ACTIVITY_DISLIKE)))
  388. && ($item['id'] != $item['parent']))
  389. continue;
  390. $nickname = $item['nickname'];
  391. }
  392. else
  393. $nickname = $a->user['nickname'];
  394. // prevent private email from leaking.
  395. if($item['network'] === NETWORK_MAIL && local_user() != $item['uid'])
  396. continue;
  397. $profile_name = ((strlen($item['author-name'])) ? $item['author-name'] : $item['name']);
  398. if($item['author-link'] && (! $item['author-name']))
  399. $profile_name = $item['author-link'];
  400. $tags=array();
  401. $hashtags = array();
  402. $mentions = array();
  403. foreach(explode(',',$item['tag']) as $tag){
  404. $tag = trim($tag);
  405. if ($tag!="") {
  406. $t = bbcode($tag);
  407. $tags[] = $t;
  408. if($t[0] == '#')
  409. $hashtags[] = $t;
  410. elseif($t[0] == '@')
  411. $mentions[] = $t;
  412. }
  413. }
  414. $sp = false;
  415. $profile_link = best_link_url($item,$sp);
  416. if($profile_link === 'mailbox')
  417. $profile_link = '';
  418. if($sp)
  419. $sparkle = ' sparkle';
  420. else
  421. $profile_link = zrl($profile_link);
  422. $normalised = normalise_link((strlen($item['author-link'])) ? $item['author-link'] : $item['url']);
  423. if(($normalised != 'mailbox') && (x($a->contacts[$normalised])))
  424. $profile_avatar = $a->contacts[$normalised]['thumb'];
  425. else
  426. $profile_avatar = ((strlen($item['author-avatar'])) ? $a->get_cached_avatar_image($item['author-avatar']) : $item['thumb']);
  427. $locate = array('location' => $item['location'], 'coord' => $item['coord'], 'html' => '');
  428. call_hooks('render_location',$locate);
  429. $location = ((strlen($locate['html'])) ? $locate['html'] : render_location_google($locate));
  430. localize_item($item);
  431. if($mode === 'network-new')
  432. $dropping = true;
  433. else
  434. $dropping = false;
  435. $drop = array(
  436. 'dropping' => $dropping,
  437. 'pagedrop' => $page_dropping,
  438. 'select' => t('Select'),
  439. 'delete' => t('Delete'),
  440. );
  441. $star = false;
  442. $isstarred = "unstarred";
  443. $lock = false;
  444. $likebuttons = false;
  445. $shareable = false;
  446. $body = prepare_body($item,true);
  447. list($categories, $folders) = get_cats_and_terms($item);
  448. $tmp_item = array(
  449. 'template' => $tpl,
  450. 'id' => (($preview) ? 'P0' : $item['item_id']),
  451. 'linktitle' => sprintf( t('View %s\'s profile @ %s'), $profile_name, ((strlen($item['author-link'])) ? $item['author-link'] : $item['url'])),
  452. 'profile_url' => $profile_link,
  453. 'item_photo_menu' => item_photo_menu($item),
  454. 'name' => template_escape($profile_name),
  455. 'sparkle' => $sparkle,
  456. 'lock' => $lock,
  457. 'thumb' => $profile_avatar,
  458. 'title' => template_escape($item['title']),
  459. 'body' => template_escape($body),
  460. 'tags' => template_escape($tags),
  461. 'hashtags' => template_escape($hashtags),
  462. 'mentions' => template_escape($mentions),
  463. 'txt_cats' => t('Categories:'),
  464. 'txt_folders' => t('Filed under:'),
  465. 'has_cats' => ((count($categories)) ? 'true' : ''),
  466. 'has_folders' => ((count($folders)) ? 'true' : ''),
  467. 'categories' => $categories,
  468. 'folders' => $folders,
  469. 'text' => strip_tags(template_escape($body)),
  470. 'localtime' => datetime_convert('UTC', date_default_timezone_get(), $item['created'], 'r'),
  471. 'ago' => (($item['app']) ? sprintf( t('%s from %s'),relative_date($item['created']),$item['app']) : relative_date($item['created'])),
  472. 'location' => template_escape($location),
  473. 'indent' => '',
  474. 'owner_name' => template_escape($owner_name),
  475. 'owner_url' => $owner_url,
  476. 'owner_photo' => $owner_photo,
  477. 'plink' => get_plink($item),
  478. 'edpost' => false,
  479. 'isstarred' => $isstarred,
  480. 'star' => $star,
  481. 'drop' => $drop,
  482. 'vote' => $likebuttons,
  483. 'like' => '',
  484. 'dislike' => '',
  485. 'comment' => '',
  486. 'conv' => (($preview) ? '' : array('href'=> $a->get_baseurl($ssl_state) . '/display/' . $nickname . '/' . $item['id'], 'title'=> t('View in context'))),
  487. 'previewing' => $previewing,
  488. 'wait' => t('Please wait'),
  489. 'thread_level' => 1,
  490. );
  491. $arr = array('item' => $item, 'output' => $tmp_item);
  492. call_hooks('display_item', $arr);
  493. $threads[$threadsid]['id'] = $item['item_id'];
  494. $threads[$threadsid]['items'] = array($arr['output']);
  495. }
  496. }
  497. else
  498. {
  499. // Normal View
  500. $page_template = get_markup_template("threaded_conversation.tpl");
  501. require_once('object/Conversation.php');
  502. require_once('object/Item.php');
  503. $conv = new Conversation($mode, $preview);
  504. // get all the topmost parents
  505. // this shouldn't be needed, as we should have only them in our array
  506. // But for now, this array respects the old style, just in case
  507. $threads = array();
  508. foreach($items as $item) {
  509. // Can we put this after the visibility check?
  510. like_puller($a,$item,$alike,'like');
  511. like_puller($a,$item,$dlike,'dislike');
  512. // Only add what is visible
  513. if($item['network'] === NETWORK_MAIL && local_user() != $item['uid']) {
  514. continue;
  515. }
  516. if(! visible_activity($item)) {
  517. continue;
  518. }
  519. $item['pagedrop'] = $page_dropping;
  520. if($item['id'] == $item['parent']) {
  521. $item_object = new Item($item);
  522. $conv->add_thread($item_object);
  523. }
  524. }
  525. $threads = $conv->get_template_data($alike, $dlike);
  526. if(!$threads) {
  527. logger('[ERROR] conversation : Failed to get template data.', LOGGER_DEBUG);
  528. $threads = array();
  529. }
  530. }
  531. }
  532. $o = replace_macros($page_template, array(
  533. '$baseurl' => $a->get_baseurl($ssl_state),
  534. '$live_update' => $live_update_div,
  535. '$remove' => t('remove'),
  536. '$mode' => $mode,
  537. '$user' => $a->user,
  538. '$threads' => $threads,
  539. '$dropping' => ($page_dropping?t('Delete Selected Items'):False),
  540. ));
  541. return $o;
  542. }}
  543. function best_link_url($item,&$sparkle,$ssl_state = false) {
  544. $a = get_app();
  545. $best_url = '';
  546. $sparkle = false;
  547. $clean_url = normalise_link($item['author-link']);
  548. if((local_user()) && (local_user() == $item['uid'])) {
  549. if(isset($a->contacts) && x($a->contacts,$clean_url)) {
  550. if($a->contacts[$clean_url]['network'] === NETWORK_DFRN) {
  551. $best_url = $a->get_baseurl($ssl_state) . '/redir/' . $a->contacts[$clean_url]['id'];
  552. $sparkle = true;
  553. }
  554. else
  555. $best_url = $a->contacts[$clean_url]['url'];
  556. }
  557. }
  558. if(! $best_url) {
  559. if(strlen($item['author-link']))
  560. $best_url = $item['author-link'];
  561. else
  562. $best_url = $item['url'];
  563. }
  564. return $best_url;
  565. }
  566. if(! function_exists('item_photo_menu')){
  567. function item_photo_menu($item){
  568. $a = get_app();
  569. $ssl_state = false;
  570. if(local_user()) {
  571. $ssl_state = true;
  572. if(! count($a->contacts))
  573. load_contact_links(local_user());
  574. }
  575. $sub_link="";
  576. $poke_link="";
  577. $contact_url="";
  578. $pm_url="";
  579. $status_link="";
  580. $photos_link="";
  581. $posts_link="";
  582. if((local_user()) && local_user() == $item['uid'] && $item['parent'] == $item['id'] && (! $item['self'])) {
  583. $sub_link = 'javascript:dosubthread(' . $item['id'] . '); return false;';
  584. }
  585. $sparkle = false;
  586. $profile_link = best_link_url($item,$sparkle,$ssl_state);
  587. if($profile_link === 'mailbox')
  588. $profile_link = '';
  589. if($sparkle) {
  590. $cid = intval(basename($profile_link));
  591. $status_link = $profile_link . "?url=status";
  592. $photos_link = $profile_link . "?url=photos";
  593. $profile_link = $profile_link . "?url=profile";
  594. $pm_url = $a->get_baseurl($ssl_state) . '/message/new/' . $cid;
  595. $zurl = '';
  596. }
  597. else {
  598. $profile_link = zrl($profile_link);
  599. if(local_user() && local_user() == $item['uid'] && link_compare($item['url'],$item['author-link'])) {
  600. $cid = $item['contact-id'];
  601. }
  602. else {
  603. $cid = 0;
  604. }
  605. }
  606. if(($cid) && (! $item['self'])) {
  607. $poke_link = $a->get_baseurl($ssl_state) . '/poke/?f=&c=' . $cid;
  608. $contact_url = $a->get_baseurl($ssl_state) . '/contacts/' . $cid;
  609. $posts_link = $a->get_baseurl($ssl_state) . '/network/?cid=' . $cid;
  610. $clean_url = normalise_link($item['author-link']);
  611. if((local_user()) && (local_user() == $item['uid'])) {
  612. if(isset($a->contacts) && x($a->contacts,$clean_url)) {
  613. if($a->contacts[$clean_url]['network'] === NETWORK_DIASPORA) {
  614. $pm_url = $a->get_baseurl($ssl_state) . '/message/new/' . $cid;
  615. }
  616. }
  617. }
  618. }
  619. $menu = Array(
  620. t("Follow Thread") => $sub_link,
  621. t("View Status") => $status_link,
  622. t("View Profile") => $profile_link,
  623. t("View Photos") => $photos_link,
  624. t("Network Posts") => $posts_link,
  625. t("Edit Contact") => $contact_url,
  626. t("Send PM") => $pm_url,
  627. t("Poke") => $poke_link
  628. );
  629. $args = array('item' => $item, 'menu' => $menu);
  630. call_hooks('item_photo_menu', $args);
  631. $menu = $args['menu'];
  632. $o = "";
  633. foreach($menu as $k=>$v){
  634. if(strpos($v,'javascript:') === 0) {
  635. $v = substr($v,11);
  636. $o .= "<li><a href=\"#\" onclick=\"$v\">$k</a></li>\n";
  637. }
  638. elseif ($v!="") $o .= "<li><a href=\"$v\">$k</a></li>\n";
  639. }
  640. return $o;
  641. }}
  642. if(! function_exists('like_puller')) {
  643. function like_puller($a,$item,&$arr,$mode) {
  644. $url = '';
  645. $sparkle = '';
  646. $verb = (($mode === 'like') ? ACTIVITY_LIKE : ACTIVITY_DISLIKE);
  647. if((activity_match($item['verb'],$verb)) && ($item['id'] != $item['parent'])) {
  648. $url = $item['author-link'];
  649. if((local_user()) && (local_user() == $item['uid']) && ($item['network'] === 'dfrn') && (! $item['self']) && (link_compare($item['author-link'],$item['url']))) {
  650. $url = $a->get_baseurl(true) . '/redir/' . $item['contact-id'];
  651. $sparkle = ' class="sparkle" ';
  652. }
  653. else
  654. $url = zrl($url);
  655. if(! $item['thr-parent'])
  656. $item['thr-parent'] = $item['parent-uri'];
  657. if(! ((isset($arr[$item['thr-parent'] . '-l'])) && (is_array($arr[$item['thr-parent'] . '-l']))))
  658. $arr[$item['thr-parent'] . '-l'] = array();
  659. if(! isset($arr[$item['thr-parent']]))
  660. $arr[$item['thr-parent']] = 1;
  661. else
  662. $arr[$item['thr-parent']] ++;
  663. $arr[$item['thr-parent'] . '-l'][] = '<a href="'. $url . '"'. $sparkle .'>' . $item['author-name'] . '</a>';
  664. }
  665. return;
  666. }}
  667. // Format the like/dislike text for a profile item
  668. // $cnt = number of people who like/dislike the item
  669. // $arr = array of pre-linked names of likers/dislikers
  670. // $type = one of 'like, 'dislike'
  671. // $id = item id
  672. // returns formatted text
  673. if(! function_exists('format_like')) {
  674. function format_like($cnt,$arr,$type,$id) {
  675. $o = '';
  676. if($cnt == 1)
  677. $o .= (($type === 'like') ? sprintf( t('%s likes this.'), $arr[0]) : sprintf( t('%s doesn\'t like this.'), $arr[0])) . EOL ;
  678. else {
  679. $spanatts = 'class="fakelink" onclick="openClose(\'' . $type . 'list-' . $id . '\');"';
  680. $o .= (($type === 'like') ?
  681. sprintf( t('<span %1$s>%2$d people</span> like this.'), $spanatts, $cnt)
  682. :
  683. sprintf( t('<span %1$s>%2$d people</span> don\'t like this.'), $spanatts, $cnt) );
  684. $o .= EOL ;
  685. $total = count($arr);
  686. if($total >= MAX_LIKERS)
  687. $arr = array_slice($arr, 0, MAX_LIKERS - 1);
  688. if($total < MAX_LIKERS)
  689. $arr[count($arr)-1] = t('and') . ' ' . $arr[count($arr)-1];
  690. $str = implode(', ', $arr);
  691. if($total >= MAX_LIKERS)
  692. $str .= sprintf( t(', and %d other people'), $total - MAX_LIKERS );
  693. $str = (($type === 'like') ? sprintf( t('%s like this.'), $str) : sprintf( t('%s don\'t like this.'), $str));
  694. $o .= "\t" . '<div id="' . $type . 'list-' . $id . '" style="display: none;" >' . $str . '</div>';
  695. }
  696. return $o;
  697. }}
  698. function status_editor($a,$x, $notes_cid = 0, $popup=false) {
  699. $o = '';
  700. $geotag = (($x['allow_location']) ? get_markup_template('jot_geotag.tpl') : '');
  701. $plaintext = false;
  702. if(local_user() && intval(get_pconfig(local_user(),'system','plaintext')))
  703. $plaintext = true;
  704. $tpl = get_markup_template('jot-header.tpl');
  705. $a->page['htmlhead'] .= replace_macros($tpl, array(
  706. '$newpost' => 'true',
  707. '$baseurl' => $a->get_baseurl(true),
  708. '$editselect' => (($plaintext) ? 'none' : '/(profile-jot-text|prvmail-text)/'),
  709. '$geotag' => $geotag,
  710. '$nickname' => $x['nickname'],
  711. '$ispublic' => t('Visible to <strong>everybody</strong>'),
  712. '$linkurl' => t('Please enter a link URL:'),
  713. '$vidurl' => t("Please enter a video link/URL:"),
  714. '$audurl' => t("Please enter an audio link/URL:"),
  715. '$term' => t('Tag term:'),
  716. '$fileas' => t('Save to Folder:'),
  717. '$whereareu' => t('Where are you right now?')
  718. ));
  719. $tpl = get_markup_template('jot-end.tpl');
  720. $a->page['end'] .= replace_macros($tpl, array(
  721. '$newpost' => 'true',
  722. '$baseurl' => $a->get_baseurl(true),
  723. '$editselect' => (($plaintext) ? 'none' : '/(profile-jot-text|prvmail-text)/'),
  724. '$geotag' => $geotag,
  725. '$nickname' => $x['nickname'],
  726. '$ispublic' => t('Visible to <strong>everybody</strong>'),
  727. '$linkurl' => t('Please enter a link URL:'),
  728. '$vidurl' => t("Please enter a video link/URL:"),
  729. '$audurl' => t("Please enter an audio link/URL:"),
  730. '$term' => t('Tag term:'),
  731. '$fileas' => t('Save to Folder:'),
  732. '$whereareu' => t('Where are you right now?')
  733. ));
  734. $tpl = get_markup_template("jot.tpl");
  735. $jotplugins = '';
  736. $jotnets = '';
  737. $mail_disabled = ((function_exists('imap_open') && (! get_config('system','imap_disabled'))) ? 0 : 1);
  738. $mail_enabled = false;
  739. $pubmail_enabled = false;
  740. if(($x['is_owner']) && (! $mail_disabled)) {
  741. $r = q("SELECT * FROM `mailacct` WHERE `uid` = %d AND `server` != '' LIMIT 1",
  742. intval(local_user())
  743. );
  744. if(count($r)) {
  745. $mail_enabled = true;
  746. if(intval($r[0]['pubmail']))
  747. $pubmail_enabled = true;
  748. }
  749. }
  750. if($mail_enabled) {
  751. $selected = (($pubmail_enabled) ? ' checked="checked" ' : '');
  752. $jotnets .= '<div class="profile-jot-net"><input type="checkbox" name="pubmail_enable"' . $selected . ' value="1" /> ' . t("Post to Email") . '</div>';
  753. }
  754. call_hooks('jot_tool', $jotplugins);
  755. call_hooks('jot_networks', $jotnets);
  756. if($notes_cid)
  757. $jotnets .= '<input type="hidden" name="contact_allow[]" value="' . $notes_cid .'" />';
  758. $tpl = replace_macros($tpl,array('$jotplugins' => $jotplugins));
  759. $o .= replace_macros($tpl,array(
  760. '$return_path' => $a->query_string,
  761. '$action' => $a->get_baseurl(true) . '/item',
  762. '$share' => (x($x,'button') ? $x['button'] : t('Share')),
  763. '$upload' => t('Upload photo'),
  764. '$shortupload' => t('upload photo'),
  765. '$attach' => t('Attach file'),
  766. '$shortattach' => t('attach file'),
  767. '$weblink' => t('Insert web link'),
  768. '$shortweblink' => t('web link'),
  769. '$video' => t('Insert video link'),
  770. '$shortvideo' => t('video link'),
  771. '$audio' => t('Insert audio link'),
  772. '$shortaudio' => t('audio link'),
  773. '$setloc' => t('Set your location'),
  774. '$shortsetloc' => t('set location'),
  775. '$noloc' => t('Clear browser location'),
  776. '$shortnoloc' => t('clear location'),
  777. '$title' => "",
  778. '$placeholdertitle' => t('Set title'),
  779. '$category' => "",
  780. '$placeholdercategory' => t('Categories (comma-separated list)'),
  781. '$wait' => t('Please wait'),
  782. '$permset' => t('Permission settings'),
  783. '$shortpermset' => t('permissions'),
  784. '$ptyp' => (($notes_cid) ? 'note' : 'wall'),
  785. '$content' => '',
  786. '$post_id' => '',
  787. '$baseurl' => $a->get_baseurl(true),
  788. '$defloc' => $x['default_location'],
  789. '$visitor' => $x['visitor'],
  790. '$pvisit' => (($notes_cid) ? 'none' : $x['visitor']),
  791. '$emailcc' => t('CC: email addresses'),
  792. '$public' => t('Public post'),
  793. '$jotnets' => $jotnets,
  794. '$emtitle' => t('Example: bob@example.com, mary@example.com'),
  795. '$lockstate' => $x['lockstate'],
  796. '$acl' => $x['acl'],
  797. '$bang' => $x['bang'],
  798. '$profile_uid' => $x['profile_uid'],
  799. '$preview' => t('Preview'),
  800. '$sourceapp' => t($a->sourcename),
  801. '$cancel' => t('Cancel')
  802. ));
  803. if ($popup==true){
  804. $o = '<div id="jot-popup" style="display: none;">'.$o.'</div>';
  805. }
  806. return $o;
  807. }
  808. function get_item_children($arr, $parent) {
  809. $children = array();
  810. foreach($arr as $item) {
  811. if($item['id'] != $item['parent']) {
  812. if(get_config('system','thread_allow')) {
  813. // Fallback to parent-uri if thr-parent is not set
  814. $thr_parent = $item['thr-parent'];
  815. if($thr_parent == '')
  816. $thr_parent = $item['parent-uri'];
  817. if($thr_parent == $parent['uri']) {
  818. $item['children'] = get_item_children($arr, $item);
  819. $children[] = $item;
  820. }
  821. }
  822. else if($item['parent'] == $parent['id']) {
  823. $children[] = $item;
  824. }
  825. }
  826. }
  827. return $children;
  828. }
  829. function sort_item_children($items) {
  830. $result = $items;
  831. usort($result,'sort_thr_created_rev');
  832. foreach($result as $k => $i) {
  833. if(count($result[$k]['children'])) {
  834. $result[$k]['children'] = sort_item_children($result[$k]['children']);
  835. }
  836. }
  837. return $result;
  838. }
  839. function add_children_to_list($children, &$arr) {
  840. foreach($children as $y) {
  841. $arr[] = $y;
  842. if(count($y['children']))
  843. add_children_to_list($y['children'], $arr);
  844. }
  845. }
  846. function conv_sort($arr,$order) {
  847. if((!(is_array($arr) && count($arr))))
  848. return array();
  849. $parents = array();
  850. $children = array();
  851. foreach($arr as $x)
  852. if($x['id'] == $x['parent'])
  853. $parents[] = $x;
  854. if(stristr($order,'created'))
  855. usort($parents,'sort_thr_created');
  856. elseif(stristr($order,'commented'))
  857. usort($parents,'sort_thr_commented');
  858. if(count($parents))
  859. foreach($parents as $i=>$_x)
  860. $parents[$i]['children'] = get_item_children($arr, $_x);
  861. /*foreach($arr as $x) {
  862. if($x['id'] != $x['parent']) {
  863. $p = find_thread_parent_index($parents,$x);
  864. if($p !== false)
  865. $parents[$p]['children'][] = $x;
  866. }
  867. }*/
  868. if(count($parents)) {
  869. foreach($parents as $k => $v) {
  870. if(count($parents[$k]['children'])) {
  871. $parents[$k]['children'] = sort_item_children($parents[$k]['children']);
  872. /*$y = $parents[$k]['children'];
  873. usort($y,'sort_thr_created_rev');
  874. $parents[$k]['children'] = $y;*/
  875. }
  876. }
  877. }
  878. $ret = array();
  879. if(count($parents)) {
  880. foreach($parents as $x) {
  881. $ret[] = $x;
  882. if(count($x['children']))
  883. add_children_to_list($x['children'], $ret);
  884. /*foreach($x['children'] as $y)
  885. $ret[] = $y;*/
  886. }
  887. }
  888. return $ret;
  889. }
  890. function sort_thr_created($a,$b) {
  891. return strcmp($b['created'],$a['created']);
  892. }
  893. function sort_thr_created_rev($a,$b) {
  894. return strcmp($a['created'],$b['created']);
  895. }
  896. function sort_thr_commented($a,$b) {
  897. return strcmp($b['commented'],$a['commented']);
  898. }
  899. function find_thread_parent_index($arr,$x) {
  900. foreach($arr as $k => $v)
  901. if($v['id'] == $x['parent'])
  902. return $k;
  903. return false;
  904. }
  905. function render_location_google($item) {
  906. $location = (($item['location']) ? '<a target="map" title="' . $item['location'] . '" href="http://maps.google.com/?q=' . urlencode($item['location']) . '">' . $item['location'] . '</a>' : '');
  907. $coord = (($item['coord']) ? '<a target="map" title="' . $item['coord'] . '" href="http://maps.google.com/?q=' . urlencode($item['coord']) . '">' . $item['coord'] . '</a>' : '');
  908. if($coord) {
  909. if($location)
  910. $location .= '<br /><span class="smalltext">(' . $coord . ')</span>';
  911. else
  912. $location = '<span class="smalltext">' . $coord . '</span>';
  913. }
  914. return $location;
  915. }