3 require_once "include/bbcode.php";
4 require_once "include/acl_selectors.php";
8 * Note: the code in 'item_extract_images' and 'item_redir_and_replace_images'
9 * is identical to the code in mod/message.php for 'item_extract_images' and
10 * 'item_redir_and_replace_images'
12 if (! function_exists('item_extract_images')) {
13 function item_extract_images($body) {
15 $saved_image = array();
20 $img_start = strpos($orig_body, '[img');
21 $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
22 $img_end = ($img_start !== false ? strpos(substr($orig_body, $img_start), '[/img]') : false);
23 while (($img_st_close !== false) && ($img_end !== false)) {
25 $img_st_close++; // make it point to AFTER the closing bracket
26 $img_end += $img_start;
28 if (! strcmp(substr($orig_body, $img_start + $img_st_close, 5), 'data:')) {
29 // This is an embedded image
31 $saved_image[$cnt] = substr($orig_body, $img_start + $img_st_close, $img_end - ($img_start + $img_st_close));
32 $new_body = $new_body . substr($orig_body, 0, $img_start) . '[!#saved_image' . $cnt . '#!]';
36 $new_body = $new_body . substr($orig_body, 0, $img_end + strlen('[/img]'));
39 $orig_body = substr($orig_body, $img_end + strlen('[/img]'));
41 if ($orig_body === false) {
42 // in case the body ends on a closing image tag
46 $img_start = strpos($orig_body, '[img');
47 $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
48 $img_end = ($img_start !== false ? strpos(substr($orig_body, $img_start), '[/img]') : false);
51 $new_body = $new_body . $orig_body;
53 return array('body' => $new_body, 'images' => $saved_image);
56 if (! function_exists('item_redir_and_replace_images')) {
57 function item_redir_and_replace_images($body, $images, $cid) {
63 $pos = get_bb_tag_pos($origbody, 'url', 1);
64 while ($pos !== false && $cnt < 1000) {
66 $search = '/\[url\=(.*?)\]\[!#saved_image([0-9]*)#!\]\[\/url\]' . '/is';
67 $replace = '[url=' . z_path() . '/redir/' . $cid
68 . '?f=1&url=' . '$1' . '][!#saved_image' . '$2' .'#!][/url]';
70 $newbody .= substr($origbody, 0, $pos['start']['open']);
71 $subject = substr($origbody, $pos['start']['open'], $pos['end']['close'] - $pos['start']['open']);
72 $origbody = substr($origbody, $pos['end']['close']);
73 if ($origbody === false) {
77 $subject = preg_replace($search, $replace, $subject);
81 $pos = get_bb_tag_pos($origbody, 'url', 1);
83 $newbody .= $origbody;
86 foreach ($images as $image) {
88 * We're depending on the property of 'foreach' (specified on the PHP website) that
89 * it loops over the array starting from the first element and going sequentially
90 * to the last element.
92 $newbody = str_replace('[!#saved_image' . $cnt . '#!]', '[img]' . $image . '[/img]', $newbody);
99 * Render actions localized
101 function localize_item(&$item) {
103 $extracted = item_extract_images($item['body']);
104 if ($extracted['images']) {
105 $item['body'] = item_redir_and_replace_images($extracted['body'], $extracted['images'], $item['contact-id']);
109 $xmlhead = "<" . "?xml version='1.0' encoding='UTF-8' ?" . ">";
110 if (activity_match($item['verb'], ACTIVITY_LIKE)
111 || activity_match($item['verb'], ACTIVITY_DISLIKE)
112 || activity_match($item['verb'], ACTIVITY_ATTEND)
113 || activity_match($item['verb'], ACTIVITY_ATTENDNO)
114 || activity_match($item['verb'], ACTIVITY_ATTENDMAYBE)) {
116 /// @TODO may hurt performance
117 $r = q("SELECT * FROM `item`, `contact`
118 WHERE `item`.`contact-id`=`contact`.`id`
119 AND `item`.`uri`='%s'",
120 dbesc($item['parent-uri']));
121 if (!dbm::is_result($r)) {
126 $author = '[url=' . $item['author-link'] . ']' . $item['author-name'] . '[/url]';
127 $objauthor = '[url=' . $obj['author-link'] . ']' . $obj['author-name'] . '[/url]';
129 switch ($obj['verb']) {
131 switch ($obj['object-type']) {
132 case ACTIVITY_OBJ_EVENT:
133 $post_type = t('event');
136 $post_type = t('status');
140 if ($obj['resource-id']) {
141 $post_type = t('photo');
143 preg_match("/\[url=([^]]*)\]/", $obj['body'], $m);
144 $rr['plink'] = $m[1];
146 $post_type = t('status');
150 $plink = '[url=' . $obj['plink'] . ']' . $post_type . '[/url]';
152 if (activity_match($item['verb'], ACTIVITY_LIKE)) {
153 $bodyverb = t('%1$s likes %2$s\'s %3$s');
155 elseif (activity_match($item['verb'], ACTIVITY_DISLIKE)) {
156 $bodyverb = t('%1$s doesn\'t like %2$s\'s %3$s');
158 elseif (activity_match($item['verb'], ACTIVITY_ATTEND)) {
159 $bodyverb = t('%1$s attends %2$s\'s %3$s');
161 elseif (activity_match($item['verb'], ACTIVITY_ATTENDNO)) {
162 $bodyverb = t('%1$s doesn\'t attend %2$s\'s %3$s');
164 elseif (activity_match($item['verb'], ACTIVITY_ATTENDMAYBE)) {
165 $bodyverb = t('%1$s attends maybe %2$s\'s %3$s');
167 $item['body'] = sprintf($bodyverb, $author, $objauthor, $plink);
170 if (activity_match($item['verb'], ACTIVITY_FRIEND)) {
172 if ($item['object-type']=="" || $item['object-type']!== ACTIVITY_OBJ_PERSON) return;
174 $Aname = $item['author-name'];
175 $Alink = $item['author-link'];
177 $xmlhead="<"."?xml version='1.0' encoding='UTF-8' ?".">";
179 $obj = parse_xml_string($xmlhead.$item['object']);
180 $links = parse_xml_string($xmlhead."<links>".unxmlify($obj->link)."</links>");
182 $Bname = $obj->title;
183 $Blink = ""; $Bphoto = "";
184 foreach ($links->link as $l) {
185 $atts = $l->attributes();
186 switch ($atts['rel']) {
187 case "alternate": $Blink = $atts['href'];
188 case "photo": $Bphoto = $atts['href'];
192 $A = '[url=' . zrl($Alink) . ']' . $Aname . '[/url]';
193 $B = '[url=' . zrl($Blink) . ']' . $Bname . '[/url]';
195 $Bphoto = '[url=' . zrl($Blink) . '][img]' . $Bphoto . '[/img][/url]';
198 $item['body'] = sprintf( t('%1$s is now friends with %2$s'), $A, $B)."\n\n\n".$Bphoto;
201 if (stristr($item['verb'], ACTIVITY_POKE)) {
202 $verb = urldecode(substr($item['verb'],strpos($item['verb'],'#')+1));
206 if ($item['object-type']=="" || $item['object-type']!== ACTIVITY_OBJ_PERSON) {
210 $Aname = $item['author-name'];
211 $Alink = $item['author-link'];
213 $xmlhead = "<" . "?xml version='1.0' encoding='UTF-8' ?" . ">";
215 $obj = parse_xml_string($xmlhead.$item['object']);
216 $links = parse_xml_string($xmlhead."<links>".unxmlify($obj->link)."</links>");
218 $Bname = $obj->title;
221 foreach ($links->link as $l) {
222 $atts = $l->attributes();
223 switch ($atts['rel']) {
224 case "alternate": $Blink = $atts['href'];
225 case "photo": $Bphoto = $atts['href'];
229 $A = '[url=' . zrl($Alink) . ']' . $Aname . '[/url]';
230 $B = '[url=' . zrl($Blink) . ']' . $Bname . '[/url]';
232 $Bphoto = '[url=' . zrl($Blink) . '][img=80x80]' . $Bphoto . '[/img][/url]';
236 * we can't have a translation string with three positions but no distinguishable text
237 * So here is the translate string.
239 $txt = t('%1$s poked %2$s');
241 // now translate the verb
242 $poked_t = trim(sprintf($txt, "", ""));
243 $txt = str_replace( $poked_t, t($verb), $txt);
245 // then do the sprintf on the translation string
247 $item['body'] = sprintf($txt, $A, $B). "\n\n\n" . $Bphoto;
250 if (stristr($item['verb'], ACTIVITY_MOOD)) {
251 $verb = urldecode(substr($item['verb'], strpos($item['verb'], '#') + 1));
256 $Aname = $item['author-name'];
257 $Alink = $item['author-link'];
258 $A = '[url=' . zrl($Alink) . ']' . $Aname . '[/url]';
260 $txt = t('%1$s is currently %2$s');
262 $item['body'] = sprintf($txt, $A, t($verb));
265 if (activity_match($item['verb'], ACTIVITY_TAG)) {
266 /// @TODO may hurt performance "joining" two tables + asterisk
267 $r = q("SELECT * FROM `item`, `contact`
268 WHERE `item`.`contact-id`=`contact`.`id`
269 AND `item`.`uri`='%s'",
270 dbesc($item['parent-uri']));
272 if (!dbm::is_result($r)) {
278 $author = '[url=' . zrl($item['author-link']) . ']' . $item['author-name'] . '[/url]';
279 $objauthor = '[url=' . zrl($obj['author-link']) . ']' . $obj['author-name'] . '[/url]';
281 switch ($obj['verb']) {
283 switch ($obj['object-type']) {
284 case ACTIVITY_OBJ_EVENT:
285 $post_type = t('event');
288 $post_type = t('status');
292 if ($obj['resource-id']) {
293 $post_type = t('photo');
294 $m=array(); preg_match("/\[url=([^]]*)\]/", $obj['body'], $m);
295 $rr['plink'] = $m[1];
297 $post_type = t('status');
299 // Let's break everthing ... ;-)
302 $plink = '[url=' . $obj['plink'] . ']' . $post_type . '[/url]';
304 $parsedobj = parse_xml_string($xmlhead.$item['object']);
306 $tag = sprintf('#[url=%s]%s[/url]', $parsedobj->id, $parsedobj->content);
307 $item['body'] = sprintf( t('%1$s tagged %2$s\'s %3$s with %4$s'), $author, $objauthor, $plink, $tag );
310 if (activity_match($item['verb'], ACTIVITY_FAVORITE)) {
312 if ($item['object-type'] == "") {
316 $Aname = $item['author-name'];
317 $Alink = $item['author-link'];
319 $xmlhead = "<" . "?xml version='1.0' encoding='UTF-8' ?" . ">";
321 $obj = parse_xml_string($xmlhead.$item['object']);
322 if (strlen($obj->id)) {
323 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
328 if (dbm::is_result($r) && $r[0]['plink']) {
330 $Bname = $target['author-name'];
331 $Blink = $target['author-link'];
332 $A = '[url=' . zrl($Alink) . ']' . $Aname . '[/url]';
333 $B = '[url=' . zrl($Blink) . ']' . $Bname . '[/url]';
334 $P = '[url=' . $target['plink'] . ']' . t('post/item') . '[/url]';
335 $item['body'] = sprintf( t('%1$s marked %2$s\'s %3$s as favorite'), $A, $B, $P)."\n";
340 if (preg_match_all('/@\[url=(.*?)\]/is', $item['body'], $matches, PREG_SET_ORDER)) {
341 foreach ($matches as $mtch) {
342 if (! strpos($mtch[1], 'zrl=')) {
343 $item['body'] = str_replace($mtch[0], '@[url=' . zrl($mtch[1]) . ']', $item['body']);
348 // add zrl's to public images
349 $photo_pattern = "/\[url=(.*?)\/photos\/(.*?)\/image\/(.*?)\]\[img(.*?)\]h(.*?)\[\/img\]\[\/url\]/is";
350 if (preg_match($photo_pattern, $item['body'])) {
351 $photo_replace = '[url=' . zrl('$1' . '/photos/' . '$2' . '/image/' . '$3' ,true) . '][img' . '$4' . ']h' . '$5' . '[/img][/url]';
352 $item['body'] = bb_tag_preg_replace($photo_pattern, $photo_replace, 'url', $item['body']);
355 // add sparkle links to appropriate permalinks
357 $x = stristr($item['plink'],'/display/');
360 $y = best_link_url($item, $sparkle, true);
362 if (strstr($y, '/redir/')) {
363 $item['plink'] = $y . '?f=&url=' . $item['plink'];
369 * Count the total of comments on this item and its desendants
370 * @TODO proper type-hint + doc-tag
372 function count_descendants($item) {
373 $total = count($item['children']);
376 foreach ($item['children'] as $child) {
377 if (! visible_activity($child)) {
380 $total += count_descendants($child);
387 function visible_activity($item) {
390 * likes (etc.) can apply to other things besides posts. Check if they are post children,
391 * in which case we handle them specially
393 $hidden_activities = array(ACTIVITY_LIKE, ACTIVITY_DISLIKE, ACTIVITY_ATTEND, ACTIVITY_ATTENDNO, ACTIVITY_ATTENDMAYBE);
394 foreach ($hidden_activities as $act) {
395 if (activity_match($item['verb'], $act)) {
400 if (activity_match($item['verb'], ACTIVITY_FOLLOW) && $item['object-type'] === ACTIVITY_OBJ_NOTE) {
401 if (! (($item['self']) && ($item['uid'] == local_user()))) {
410 * @brief SQL query for items
412 function item_query() {
413 return "SELECT " . item_fieldlists() . " FROM `item` " .
414 item_joins() . " WHERE " . item_condition();
418 * @brief List of all data fields that are needed for displaying items
420 function item_fieldlists() {
423 These Fields are not added below (yet). They are here to for bug search.
429 `item`.`target-type`,
431 `item`.`resource-id`,
445 `item`.`gcontact-id`,
449 return "`item`.`author-id`, `item`.`author-link`, `item`.`author-name`, `item`.`author-avatar`,
450 `item`.`owner-id`, `item`.`owner-link`, `item`.`owner-name`, `item`.`owner-avatar`,
451 `item`.`contact-id`, `item`.`uid`, `item`.`id`, `item`.`parent`,
452 `item`.`uri`, `item`.`thr-parent`, `item`.`parent-uri`,
453 `item`.`commented`, `item`.`created`, `item`.`edited`,
454 `item`.`verb`, `item`.`object-type`, `item`.`postopts`, `item`.`plink`,
455 `item`.`guid`, `item`.`wall`, `item`.`private`, `item`.`starred`,
456 `item`.`title`, `item`.`body`, `item`.`file`, `item`.`event-id`,
457 `item`.`location`, `item`.`coord`, `item`.`app`, `item`.`attach`,
458 `item`.`rendered-hash`, `item`.`rendered-html`, `item`.`object`,
459 `item`.`allow_cid`, `item`.`allow_gid`, `item`.`deny_cid`, `item`.`deny_gid`,
460 `item`.`id` AS `item_id`, `item`.`network` AS `item_network`,
462 `author`.`thumb` AS `author-thumb`, `owner`.`thumb` AS `owner-thumb`,
464 `contact`.`network`, `contact`.`url`, `contact`.`name`, `contact`.`writable`,
465 `contact`.`self`, `contact`.`id` AS `cid`, `contact`.`alias`";
469 * @brief SQL join for contacts that are needed for displaying items
471 function item_joins() {
472 return "STRAIGHT_JOIN `contact` ON `contact`.`id` = `item`.`contact-id` AND
473 (NOT `contact`.`blocked` OR `contact`.`pending`)
474 LEFT JOIN `contact` AS `author` ON `author`.`id`=`item`.`author-id`
475 LEFT JOIN `contact` AS `owner` ON `owner`.`id`=`item`.`owner-id`";
479 * @brief SQL condition for items that are needed for displaying items
481 function item_condition() {
482 return "`item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`";
485 if (!function_exists('conversation')) {
487 * "Render" a conversation or list of items for HTML display.
488 * There are two major forms of display:
489 * - Sequential or unthreaded ("New Item View" or search results)
490 * - conversation view
491 * The $mode parameter decides between the various renderings and also
492 * figures out how to determine page owner and other contextual items
493 * that are based on unique features of the calling module.
496 function conversation(App $a, $items, $mode, $update, $preview = false) {
498 require_once 'include/bbcode.php';
499 require_once 'include/Contact.php';
500 require_once 'mod/proxy.php';
502 $ssl_state = ((local_user()) ? true : false);
505 $page_writeable = false;
506 $live_update_div = '';
511 $str_blocked = get_pconfig(local_user(), 'system', 'blocked');
513 $arr_blocked = explode(',', $str_blocked);
514 for ($x = 0; $x < count($arr_blocked); $x ++) {
515 $arr_blocked[$x] = trim($arr_blocked[$x]);
521 $previewing = (($preview) ? ' preview ' : '');
523 if ($mode === 'network') {
524 $profile_owner = local_user();
525 $page_writeable = true;
528 * The special div is needed for liveUpdate to kick in for this page.
529 * We only launch liveUpdate if you aren't filtering in some incompatible
530 * way and also you aren't writing a comment (discovered in javascript).
532 $live_update_div = '<div id="live-network"></div>' . "\r\n"
533 . "<script> var profile_uid = " . $_SESSION['uid']
534 . "; var netargs = '" . substr($a->cmd, 8)
536 . ((x($_GET, 'cid')) ? '&cid=' . $_GET['cid'] : '')
537 . ((x($_GET, 'search')) ? '&search=' . $_GET['search'] : '')
538 . ((x($_GET, 'star')) ? '&star=' . $_GET['star'] : '')
539 . ((x($_GET, 'order')) ? '&order=' . $_GET['order'] : '')
540 . ((x($_GET, 'bmark')) ? '&bmark=' . $_GET['bmark'] : '')
541 . ((x($_GET, 'liked')) ? '&liked=' . $_GET['liked'] : '')
542 . ((x($_GET, 'conv')) ? '&conv=' . $_GET['conv'] : '')
543 . ((x($_GET, 'spam')) ? '&spam=' . $_GET['spam'] : '')
544 . ((x($_GET, 'nets')) ? '&nets=' . $_GET['nets'] : '')
545 . ((x($_GET, 'cmin')) ? '&cmin=' . $_GET['cmin'] : '')
546 . ((x($_GET, 'cmax')) ? '&cmax=' . $_GET['cmax'] : '')
547 . ((x($_GET, 'file')) ? '&file=' . $_GET['file'] : '')
549 . "'; var profile_page = " . $a->pager['page'] . "; </script>\r\n";
551 } elseif ($mode === 'profile') {
552 $profile_owner = $a->profile['profile_uid'];
553 $page_writeable = can_write_wall($a,$profile_owner);
556 $tab = notags(trim($_GET['tab']));
557 $tab = ( $tab ? $tab : 'posts' );
558 if ($tab === 'posts') {
560 * This is ugly, but we can't pass the profile_uid through the session to the ajax updater,
561 * because browser prefetching might change it on us. We have to deliver it with the page.
564 $live_update_div = '<div id="live-profile"></div>' . "\r\n"
565 . "<script> var profile_uid = " . $a->profile['profile_uid']
566 . "; var netargs = '?f='; var profile_page = " . $a->pager['page'] . "; </script>\r\n";
569 } elseif ($mode === 'notes') {
570 $profile_owner = local_user();
571 $page_writeable = true;
573 $live_update_div = '<div id="live-notes"></div>' . "\r\n"
574 . "<script> var profile_uid = " . local_user()
575 . "; var netargs = '/?f='; var profile_page = " . $a->pager['page'] . "; </script>\r\n";
577 } elseif ($mode === 'display') {
578 $profile_owner = $a->profile['uid'];
579 $page_writeable = can_write_wall($a,$profile_owner);
581 $live_update_div = '<div id="live-display"></div>' . "\r\n"
582 . "<script> var profile_uid = " . $_SESSION['uid'] . ";"
583 . " var profile_page = 1; </script>";
585 } elseif ($mode === 'community') {
587 $page_writeable = false;
589 $live_update_div = '<div id="live-community"></div>' . "\r\n"
590 . "<script> var profile_uid = -1; var netargs = '/?f='; var profile_page = " . $a->pager['page'] . "; </script>\r\n";
592 } elseif ($mode === 'search') {
593 $live_update_div = '<div id="live-search"></div>' . "\r\n";
596 $page_dropping = ((local_user() && local_user() == $profile_owner) ? true : false);
600 $return_url = $_SESSION['return_url'];
602 $return_url = $_SESSION['return_url'] = $a->query_string;
605 $cb = array('items' => $items, 'mode' => $mode, 'update' => $update, 'preview' => $preview);
606 call_hooks('conversation_start',$cb);
608 $items = $cb['items'];
610 $cmnt_tpl = get_markup_template('comment_item.tpl');
611 $hide_comments_tpl = get_markup_template('hide_comments.tpl');
613 $conv_responses = array(
614 'like' => array('title' => t('Likes','title')), 'dislike' => array('title' => t('Dislikes','title')),
615 'attendyes' => array('title' => t('Attending','title')), 'attendno' => array('title' => t('Not attending','title')), 'attendmaybe' => array('title' => t('Might attend','title'))
618 // array with html for each thread (parent+comments)
622 $page_template = get_markup_template("conversation.tpl");
624 if ($items && count($items)) {
626 if ($mode === 'network-new' || $mode === 'search' || $mode === 'community') {
629 * "New Item View" on network page or search page results
630 * - just loop through the items and format them minimally for display
633 /// @TODO old lost code?
634 // $tpl = get_markup_template('search_item.tpl');
635 $tpl = 'search_item.tpl';
637 foreach ($items as $item) {
641 foreach ($arr_blocked as $b) {
642 if ($b && link_compare($item['author-link'], $b)) {
660 if ($mode === 'search' || $mode === 'community') {
661 if (((activity_match($item['verb'], ACTIVITY_LIKE)) || (activity_match($item['verb'], ACTIVITY_DISLIKE)))
662 && ($item['id'] != $item['parent']))
664 $nickname = $item['nickname'];
666 $nickname = $a->user['nickname'];
669 // prevent private email from leaking.
670 if ($item['network'] === NETWORK_MAIL && local_user() != $item['uid']) {
674 $profile_name = ((strlen($item['author-name'])) ? $item['author-name'] : $item['name']);
675 if ($item['author-link'] && (! $item['author-name'])) {
676 $profile_name = $item['author-link'];
683 $taglist = q("SELECT `type`, `term`, `url` FROM `term` WHERE `otype` = %d AND `oid` = %d AND `type` IN (%d, %d) ORDER BY `tid`",
684 intval(TERM_OBJ_POST), intval($item['id']), intval(TERM_HASHTAG), intval(TERM_MENTION));
686 foreach ($taglist as $tag) {
688 if ($tag["url"] == "") {
689 $tag["url"] = $searchpath . strtolower($tag["term"]);
692 if ($tag["type"] == TERM_HASHTAG) {
693 $hashtags[] = "#<a href=\"" . $tag["url"] . "\" target=\"_blank\">" . $tag["term"] . "</a>";
695 } elseif ($tag["type"] == TERM_MENTION) {
696 $mentions[] = "@<a href=\"" . $tag["url"] . "\" target=\"_blank\">" . $tag["term"] . "</a>";
699 $tags[] = $prefix."<a href=\"" . $tag["url"] . "\" target=\"_blank\">" . $tag["term"] . "</a>";
703 $profile_link = best_link_url($item,$sp);
704 if ($profile_link === 'mailbox') {
709 $sparkle = ' sparkle';
711 $profile_link = zrl($profile_link);
714 if (!x($item, 'author-thumb') OR ($item['author-thumb'] == "")) {
715 $author_contact = get_contact_details_by_url($item['author-link'], $profile_owner);
716 if ($author_contact["thumb"]) {
717 $item['author-thumb'] = $author_contact["thumb"];
719 $item['author-thumb'] = $item['author-avatar'];
723 if (!isset($item['owner-thumb']) OR ($item['owner-thumb'] == "")) {
724 $owner_contact = get_contact_details_by_url($item['owner-link'], $profile_owner);
725 if ($owner_contact["thumb"]) {
726 $item['owner-thumb'] = $owner_contact["thumb"];
728 $item['owner-thumb'] = $item['owner-avatar'];
732 $locate = array('location' => $item['location'], 'coord' => $item['coord'], 'html' => '');
733 call_hooks('render_location',$locate);
735 $location = ((strlen($locate['html'])) ? $locate['html'] : render_location_dummy($locate));
737 localize_item($item);
738 if ($mode === 'network-new') {
745 'dropping' => $dropping,
746 'pagedrop' => $page_dropping,
747 'select' => t('Select'),
748 'delete' => t('Delete'),
752 $isstarred = "unstarred";
755 $likebuttons = false;
758 $body = prepare_body($item, true, $preview);
760 list($categories, $folders) = get_cats_and_terms($item);
762 if ($a->theme['template_engine'] === 'internal') {
763 $profile_name_e = template_escape($profile_name);
764 $item['title_e'] = template_escape($item['title']);
765 $body_e = template_escape($body);
766 $tags_e = template_escape($tags);
767 $hashtags_e = template_escape($hashtags);
768 $mentions_e = template_escape($mentions);
769 $location_e = template_escape($location);
770 $owner_name_e = template_escape($owner_name);
772 $profile_name_e = $profile_name;
773 $item['title_e'] = $item['title'];
776 $hashtags_e = $hashtags;
777 $mentions_e = $mentions;
778 $location_e = $location;
779 $owner_name_e = $owner_name;
782 if ($item['item_network'] == "") {
783 $item['item_network'] = $item['network'];
788 'id' => (($preview) ? 'P0' : $item['item_id']),
789 'network' => $item['item_network'],
790 'network_name' => network_to_name($item['item_network'], $profile_link),
791 'linktitle' => sprintf( t('View %s\'s profile @ %s'), $profile_name, ((strlen($item['author-link'])) ? $item['author-link'] : $item['url'])),
792 'profile_url' => $profile_link,
793 'item_photo_menu' => item_photo_menu($item),
794 'name' => $profile_name_e,
795 'sparkle' => $sparkle,
797 'thumb' => App::remove_baseurl(proxy_url($item['author-thumb'], false, PROXY_SIZE_THUMB)),
798 'title' => $item['title_e'],
801 'hashtags' => $hashtags_e,
802 'mentions' => $mentions_e,
803 'txt_cats' => t('Categories:'),
804 'txt_folders' => t('Filed under:'),
805 'has_cats' => ((count($categories)) ? 'true' : ''),
806 'has_folders' => ((count($folders)) ? 'true' : ''),
807 'categories' => $categories,
808 'folders' => $folders,
809 'text' => strip_tags($body_e),
810 'localtime' => datetime_convert('UTC', date_default_timezone_get(), $item['created'], 'r'),
811 'ago' => (($item['app']) ? sprintf( t('%s from %s'),relative_date($item['created']),$item['app']) : relative_date($item['created'])),
812 'location' => $location_e,
814 'owner_name' => $owner_name_e,
815 'owner_url' => $owner_url,
816 'owner_photo' => App::remove_baseurl(proxy_url($item['owner-thumb'], false, PROXY_SIZE_THUMB)),
817 'plink' => get_plink($item),
819 'isstarred' => $isstarred,
822 'vote' => $likebuttons,
826 //'conv' => (($preview) ? '' : array('href'=> 'display/' . $nickname . '/' . $item['id'], 'title'=> t('View in context'))),
827 'conv' => (($preview) ? '' : array('href'=> 'display/'.$item['guid'], 'title'=> t('View in context'))),
828 'previewing' => $previewing,
829 'wait' => t('Please wait'),
833 $arr = array('item' => $item, 'output' => $tmp_item);
834 call_hooks('display_item', $arr);
836 $threads[$threadsid]['id'] = $item['item_id'];
837 $threads[$threadsid]['network'] = $item['item_network'];
838 $threads[$threadsid]['items'] = array($arr['output']);
843 $page_template = get_markup_template("threaded_conversation.tpl");
845 require_once 'object/Conversation.php';
846 require_once 'object/Item.php';
848 $conv = new Conversation($mode, $preview);
851 * get all the topmost parents
852 * this shouldn't be needed, as we should have only them in our array
853 * But for now, this array respects the old style, just in case
856 foreach ($items as $item) {
860 foreach ($arr_blocked as $b) {
861 if ($b && link_compare($item['author-link'], $b)) {
871 // Can we put this after the visibility check?
872 builtin_activity_puller($item, $conv_responses);
874 // Only add what is visible
875 if ($item['network'] === NETWORK_MAIL && local_user() != $item['uid']) {
879 if (! visible_activity($item)) {
883 call_hooks('display_item', $arr);
885 $item['pagedrop'] = $page_dropping;
887 if ($item['id'] == $item['parent']) {
888 $item_object = new Item($item);
889 $conv->add_thread($item_object);
893 $threads = $conv->get_template_data($conv_responses);
896 logger('[ERROR] conversation : Failed to get template data.', LOGGER_DEBUG);
902 $o = replace_macros($page_template, array(
903 '$baseurl' => App::get_baseurl($ssl_state),
904 '$return_path' => $a->query_string,
905 '$live_update' => $live_update_div,
906 '$remove' => t('remove'),
909 '$threads' => $threads,
910 '$dropping' => ($page_dropping && feature_enabled(local_user(), 'multi_delete') ? t('Delete Selected Items') : False),
916 function best_link_url($item, &$sparkle, $ssl_state = false) {
921 $clean_url = normalise_link($item['author-link']);
924 $r = q("SELECT `id` FROM `contact` WHERE `network` = '%s' AND `uid` = %d AND `nurl` = '%s' AND NOT `pending` LIMIT 1",
925 dbesc(NETWORK_DFRN), intval(local_user()), dbesc(normalise_link($clean_url)));
926 if (dbm::is_result($r)) {
927 $best_url = 'redir/' . $r[0]['id'];
932 if (strlen($item['author-link'])) {
933 $best_url = $item['author-link'];
935 $best_url = $item['url'];
943 if (! function_exists('item_photo_menu')) {
944 function item_photo_menu($item) {
960 if ((local_user()) && local_user() == $item['uid'] && $item['parent'] == $item['id'] && (! $item['self'])) {
961 $sub_link = 'javascript:dosubthread(' . $item['id'] . '); return false;';
965 $profile_link = best_link_url($item, $sparkle, $ssl_state);
966 if ($profile_link === 'mailbox') {
973 $r = q("SELECT `id`, `network`, `rel` FROM `contact` WHERE `uid` = %d AND `nurl` = '%s' LIMIT 1",
974 intval(local_user()), dbesc(normalise_link($item['author-link'])));
975 if (dbm::is_result($r)) {
977 $network = $r[0]['network'];
982 $status_link = $profile_link . '?url=status';
983 $photos_link = $profile_link . '?url=photos';
984 $profile_link = $profile_link . '?url=profile';
987 $profile_link = zrl($profile_link);
990 if ($cid && !$item['self']) {
991 $poke_link = 'poke/?f=&c=' . $cid;
992 $contact_url = 'contacts/' . $cid;
993 $posts_link = 'contacts/' . $cid . '/posts';
995 if (in_array($network, array(NETWORK_DFRN, NETWORK_DIASPORA))) {
996 $pm_url = 'message/new/' . $cid;
1002 t('Follow Thread') => $sub_link,
1003 t('View Status') => $status_link,
1004 t('View Profile') => $profile_link,
1005 t('View Photos') => $photos_link,
1006 t('Network Posts') => $posts_link,
1007 t('View Contact') => $contact_url,
1008 t('Send PM') => $pm_url
1011 if ($network == NETWORK_DFRN) {
1012 $menu[t("Poke")] = $poke_link;
1015 if ((($cid == 0) OR ($rel == CONTACT_IS_FOLLOWER)) AND
1016 in_array($item['network'], array(NETWORK_DFRN, NETWORK_OSTATUS, NETWORK_DIASPORA))) {
1017 $menu[t('Connect/Follow')] = 'follow?url=' . urlencode($item['author-link']);
1020 $menu = array(t('View Profile') => $item['author-link']);
1023 $args = array('item' => $item, 'menu' => $menu);
1025 call_hooks('item_photo_menu', $args);
1027 $menu = $args['menu'];
1030 foreach ($menu as $k => $v) {
1031 if (strpos($v, 'javascript:') === 0) {
1032 $v = substr($v, 11);
1033 $o .= '<li role="menuitem"><a onclick="' . $v . '">' . $k . '</a></li>' . PHP_EOL;
1035 $o .= '<li role="menuitem"><a href="' . $v . '">' . $k . '</a></li>' . PHP_EOL;
1041 if (! function_exists('builtin_activity_puller')) {
1043 * @brief Checks item to see if it is one of the builtin activities (like/dislike, event attendance, consensus items, etc.)
1044 * Increments the count of each matching activity and adds a link to the author as needed.
1046 * @param array $item
1047 * @param array &$conv_responses (already created with builtin activity structure)
1050 function builtin_activity_puller($item, &$conv_responses) {
1051 foreach ($conv_responses as $mode => $v) {
1057 $verb = ACTIVITY_LIKE;
1060 $verb = ACTIVITY_DISLIKE;
1063 $verb = ACTIVITY_ATTEND;
1066 $verb = ACTIVITY_ATTENDNO;
1069 $verb = ACTIVITY_ATTENDMAYBE;
1076 if ((activity_match($item['verb'], $verb)) && ($item['id'] != $item['parent'])) {
1077 $url = $item['author-link'];
1078 if ((local_user()) && (local_user() == $item['uid']) && ($item['network'] === NETWORK_DFRN) && (! $item['self']) && (link_compare($item['author-link'], $item['url']))) {
1079 $url = 'redir/' . $item['contact-id'];
1080 $sparkle = ' class="sparkle" ';
1085 $url = '<a href="'. $url . '"'. $sparkle .'>' . htmlentities($item['author-name']) . '</a>';
1087 if (! $item['thr-parent']) {
1088 $item['thr-parent'] = $item['parent-uri'];
1091 if (! ((isset($conv_responses[$mode][$item['thr-parent'] . '-l']))
1092 && (is_array($conv_responses[$mode][$item['thr-parent'] . '-l'])))) {
1093 $conv_responses[$mode][$item['thr-parent'] . '-l'] = array();
1096 // only list each unique author once
1097 if (in_array($url,$conv_responses[$mode][$item['thr-parent'] . '-l'])) {
1101 if (! isset($conv_responses[$mode][$item['thr-parent']])) {
1102 $conv_responses[$mode][$item['thr-parent']] = 1;
1104 $conv_responses[$mode][$item['thr-parent']] ++;
1107 if (public_contact() == $item['author-id']) {
1108 $conv_responses[$mode][$item['thr-parent'] . '-self'] = 1;
1111 $conv_responses[$mode][$item['thr-parent'] . '-l'][] = $url;
1113 // there can only be one activity verb per item so if we found anything, we can stop looking
1119 if (! function_exists('format_like')) {
1121 * Format the vote text for a profile item
1122 * @param int $cnt = number of people who vote the item
1123 * @param array $arr = array of pre-linked names of likers/dislikers
1124 * @param string $type = one of 'like, 'dislike', 'attendyes', 'attendno', 'attendmaybe'
1125 * @param int $id = item id
1126 * @return formatted text
1128 function format_like($cnt, array $arr, $type, $id) {
1135 // Phrase if there is only one liker. In other cases it will be uses for the expanded
1136 // list which show all likers
1139 $phrase = sprintf( t('%s likes this.'), $likers);
1142 $phrase = sprintf( t('%s doesn\'t like this.'), $likers);
1145 $phrase = sprintf( t('%s attends.'), $likers);
1148 $phrase = sprintf( t('%s doesn\'t attend.'), $likers);
1150 case 'attendmaybe' :
1151 $phrase = sprintf( t('%s attends maybe.'), $likers);
1157 $total = count($arr);
1158 if ($total >= MAX_LIKERS) {
1159 $arr = array_slice($arr, 0, MAX_LIKERS - 1);
1161 if ($total < MAX_LIKERS) {
1162 $last = t('and') . ' ' . $arr[count($arr)-1];
1163 $arr2 = array_slice($arr, 0, -1);
1164 $str = implode(', ', $arr2) . ' ' . $last;
1166 if ($total >= MAX_LIKERS) {
1167 $str = implode(', ', $arr);
1168 $str .= sprintf( t(', and %d other people'), $total - MAX_LIKERS );
1173 $spanatts = "class=\"fakelink\" onclick=\"openClose('{$type}list-$id');\"";
1177 $phrase = sprintf( t('<span %1$s>%2$d people</span> like this'), $spanatts, $cnt);
1178 $explikers = sprintf( t('%s like this.'), $likers);
1181 $phrase = sprintf( t('<span %1$s>%2$d people</span> don\'t like this'), $spanatts, $cnt);
1182 $explikers = sprintf( t('%s don\'t like this.'), $likers);
1185 $phrase = sprintf( t('<span %1$s>%2$d people</span> attend'), $spanatts, $cnt);
1186 $explikers = sprintf( t('%s attend.'), $likers);
1189 $phrase = sprintf( t('<span %1$s>%2$d people</span> don\'t attend'), $spanatts, $cnt);
1190 $explikers = sprintf( t('%s don\'t attend.'), $likers);
1193 $phrase = sprintf( t('<span %1$s>%2$d people</span> attend maybe'), $spanatts, $cnt);
1194 $explikers = sprintf( t('%s anttend maybe.'), $likers);
1198 $expanded .= "\t" . '<div class="wall-item-' . $type . '-expanded" id="' . $type . 'list-' . $id . '" style="display: none;" >' . $explikers . EOL . '</div>';
1202 $o .= replace_macros(get_markup_template('voting_fakelink.tpl'), array(
1203 '$phrase' => $phrase,
1212 function status_editor(App $a, $x, $notes_cid = 0, $popup = false) {
1215 $geotag = (x($x, 'allow_location') ? replace_macros(get_markup_template('jot_geotag.tpl'), array()) : '');
1217 $tpl = get_markup_template('jot-header.tpl');
1218 $a->page['htmlhead'] .= replace_macros($tpl, array(
1219 '$newpost' => 'true',
1220 '$baseurl' => App::get_baseurl(true),
1221 '$geotag' => $geotag,
1222 '$nickname' => $x['nickname'],
1223 '$ispublic' => t('Visible to <strong>everybody</strong>'),
1224 '$linkurl' => t('Please enter a link URL:'),
1225 '$vidurl' => t("Please enter a video link/URL:"),
1226 '$audurl' => t("Please enter an audio link/URL:"),
1227 '$term' => t('Tag term:'),
1228 '$fileas' => t('Save to Folder:'),
1229 '$whereareu' => t('Where are you right now?'),
1230 '$delitems' => t('Delete item(s)?')
1233 $tpl = get_markup_template('jot-end.tpl');
1234 $a->page['end'] .= replace_macros($tpl, array(
1235 '$newpost' => 'true',
1236 '$baseurl' => App::get_baseurl(true),
1237 '$geotag' => $geotag,
1238 '$nickname' => $x['nickname'],
1239 '$ispublic' => t('Visible to <strong>everybody</strong>'),
1240 '$linkurl' => t('Please enter a link URL:'),
1241 '$vidurl' => t("Please enter a video link/URL:"),
1242 '$audurl' => t("Please enter an audio link/URL:"),
1243 '$term' => t('Tag term:'),
1244 '$fileas' => t('Save to Folder:'),
1245 '$whereareu' => t('Where are you right now?')
1249 call_hooks('jot_tool', $jotplugins);
1251 // Private/public post links for the non-JS ACL form
1253 if (x($_REQUEST, 'public')) {
1257 $query_str = $a->query_string;
1258 if (strpos($query_str, 'public=1') !== false) {
1259 $query_str = str_replace(array('?public=1', '&public=1'), array('', ''), $query_str);
1263 * I think $a->query_string may never have ? in it, but I could be wrong
1264 * It looks like it's from the index.php?q=[etc] rewrite that the web
1265 * server does, which converts any ? to &, e.g. suggest&ignore=61 for suggest?ignore=61
1267 if (strpos($query_str, '?') === false) {
1268 $public_post_link = '?public=1';
1270 $public_post_link = '&public=1';
1273 // $tpl = replace_macros($tpl,array('$jotplugins' => $jotplugins));
1274 $tpl = get_markup_template("jot.tpl");
1276 $o .= replace_macros($tpl,array(
1277 '$return_path' => $query_str,
1278 '$action' => 'item',
1279 '$share' => (x($x,'button') ? $x['button'] : t('Share')),
1280 '$upload' => t('Upload photo'),
1281 '$shortupload' => t('upload photo'),
1282 '$attach' => t('Attach file'),
1283 '$shortattach' => t('attach file'),
1284 '$weblink' => t('Insert web link'),
1285 '$shortweblink' => t('web link'),
1286 '$video' => t('Insert video link'),
1287 '$shortvideo' => t('video link'),
1288 '$audio' => t('Insert audio link'),
1289 '$shortaudio' => t('audio link'),
1290 '$setloc' => t('Set your location'),
1291 '$shortsetloc' => t('set location'),
1292 '$noloc' => t('Clear browser location'),
1293 '$shortnoloc' => t('clear location'),
1294 '$title' => $x['title'],
1295 '$placeholdertitle' => t('Set title'),
1296 '$category' => $x['category'],
1297 '$placeholdercategory' => (feature_enabled(local_user(), 'categories') ? t('Categories (comma-separated list)') : ''),
1298 '$wait' => t('Please wait'),
1299 '$permset' => t('Permission settings'),
1300 '$shortpermset' => t('permissions'),
1301 '$ptyp' => (($notes_cid) ? 'note' : 'wall'),
1302 '$content' => $x['content'],
1303 '$post_id' => $x['post_id'],
1304 '$baseurl' => App::get_baseurl(true),
1305 '$defloc' => $x['default_location'],
1306 '$visitor' => $x['visitor'],
1307 '$pvisit' => (($notes_cid) ? 'none' : $x['visitor']),
1308 '$public' => t('Public post'),
1309 '$jotnets' => $jotnets,
1310 '$lockstate' => $x['lockstate'],
1311 '$bang' => $x['bang'],
1312 '$profile_uid' => $x['profile_uid'],
1313 '$preview' => ((feature_enabled($x['profile_uid'],'preview')) ? t('Preview') : ''),
1314 '$jotplugins' => $jotplugins,
1315 '$notes_cid' => $notes_cid,
1316 '$sourceapp' => t($a->sourcename),
1317 '$cancel' => t('Cancel'),
1318 '$rand_num' => random_digits(12),
1320 // ACL permissions box
1321 '$acl' => $x['acl'],
1322 '$acl_data' => $x['acl_data'],
1323 '$group_perms' => t('Post to Groups'),
1324 '$contact_perms' => t('Post to Contacts'),
1325 '$private' => t('Private post'),
1326 '$is_private' => $private_post,
1327 '$public_link' => $public_post_link,
1329 //jot nav tab (used in some themes)
1330 '$message' => t('Message'),
1331 '$browser' => t('Browser'),
1335 if ($popup == true) {
1336 $o = '<div id="jot-popup" style="display: none;">'.$o.'</div>';
1343 function get_item_children($arr, $parent) {
1344 $children = array();
1346 foreach ($arr as $item) {
1347 if ($item['id'] != $item['parent']) {
1348 if (get_config('system', 'thread_allow') && $a->theme_thread_allow) {
1349 // Fallback to parent-uri if thr-parent is not set
1350 $thr_parent = $item['thr-parent'];
1351 if ($thr_parent == '') {
1352 $thr_parent = $item['parent-uri'];
1355 if ($thr_parent == $parent['uri']) {
1356 $item['children'] = get_item_children($arr, $item);
1357 $children[] = $item;
1359 } elseif ($item['parent'] == $parent['id']) {
1360 $children[] = $item;
1367 /// @TODO Add type-hint
1368 function sort_item_children($items) {
1370 usort($result, 'sort_thr_created_rev');
1371 foreach ($result as $k => $i) {
1372 if (count($result[$k]['children'])) {
1373 $result[$k]['children'] = sort_item_children($result[$k]['children']);
1379 /// @TODO Add type-hint
1380 function add_children_to_list($children, &$arr) {
1381 foreach ($children as $y) {
1383 if (count($y['children'])) {
1384 add_children_to_list($y['children'], $arr);
1389 /// @TODO Add type-hint
1390 function conv_sort($arr, $order) {
1392 if ((!(is_array($arr) && count($arr)))) {
1397 $children = array();
1401 * This is a preparation for having two different items with the same uri in one thread
1402 * This will otherwise lead to an endless loop.
1404 foreach ($arr as $x) {
1405 if (!isset($newarr[$x['uri']])) {
1406 $newarr[$x['uri']] = $x;
1412 foreach ($arr as $x) {
1413 if ($x['id'] == $x['parent']) {
1418 if (stristr($order, 'created')) {
1419 usort($parents, 'sort_thr_created');
1420 } elseif (stristr($order, 'commented')) {
1421 usort($parents, 'sort_thr_commented');
1424 if (count($parents)) {
1425 foreach ($parents as $i => $_x) {
1426 $parents[$i]['children'] = get_item_children($arr, $_x);
1430 /// @TODO Old-lost code?
1431 /*foreach ($arr as $x) {
1432 if ($x['id'] != $x['parent']) {
1433 $p = find_thread_parent_index($parents,$x);
1435 $parents[$p]['children'][] = $x;
1438 if (count($parents)) {
1439 foreach ($parents as $k => $v) {
1440 if (count($parents[$k]['children'])) {
1441 $parents[$k]['children'] = sort_item_children($parents[$k]['children']);
1442 /// @TODO Old-lost code?
1443 /*$y = $parents[$k]['children'];
1444 usort($y,'sort_thr_created_rev');
1445 $parents[$k]['children'] = $y;*/
1451 if (count($parents)) {
1452 foreach ($parents as $x) {
1454 if (count($x['children'])) {
1455 add_children_to_list($x['children'], $ret);
1456 /// @TODO Old-lost code?
1457 /*foreach ($x['children'] as $y)
1466 /// @TODO Add type-hint
1467 function sort_thr_created($a, $b) {
1468 return strcmp($b['created'], $a['created']);
1471 /// @TODO Add type-hint
1472 function sort_thr_created_rev($a, $b) {
1473 return strcmp($a['created'], $b['created']);
1476 /// @TODO Add type-hint
1477 function sort_thr_commented($a, $b) {
1478 return strcmp($b['commented'], $a['commented']);
1481 /// @TODO Add type-hint
1482 function find_thread_parent_index($arr, $x) {
1483 foreach ($arr as $k => $v) {
1484 if ($v['id'] == $x['parent']) {
1491 /// @TODO Add type-hint
1492 function render_location_dummy($item) {
1493 if ($item['location'] != "") {
1494 return $item['location'];
1497 if ($item['coord'] != "") {
1498 return $item['coord'];
1502 /// @TODO Add type-hint
1503 function get_responses($conv_responses, $response_verbs, $ob, $item) {
1505 foreach ($response_verbs as $v) {
1507 $ret[$v]['count'] = ((x($conv_responses[$v], $item['uri'])) ? $conv_responses[$v][$item['uri']] : '');
1508 $ret[$v]['list'] = ((x($conv_responses[$v], $item['uri'])) ? $conv_responses[$v][$item['uri'] . '-l'] : '');
1509 $ret[$v]['self'] = ((x($conv_responses[$v], $item['uri'])) ? $conv_responses[$v][$item['uri'] . '-self'] : '0');
1510 if (count($ret[$v]['list']) > MAX_LIKERS) {
1511 $ret[$v]['list_part'] = array_slice($ret[$v]['list'], 0, MAX_LIKERS);
1512 array_push($ret[$v]['list_part'], '<a href="#" data-toggle="modal" data-target="#' . $v . 'Modal-'
1513 . (($ob) ? $ob->get_id() : $item['id']) . '"><b>' . t('View all') . '</b></a>');
1515 $ret[$v]['list_part'] = '';
1517 $ret[$v]['button'] = get_response_button_text($v, $ret[$v]['count']);
1518 $ret[$v]['title'] = $conv_responses[$v]['title'];
1522 foreach ($ret as $key) {
1523 if ($key['count'] == true) {
1527 $ret['count'] = $count;
1532 function get_response_button_text($v, $count) {
1535 return tt('Like', 'Likes', $count, 'noun');
1538 return tt('Dislike', 'Dislikes', $count, 'noun');
1541 return tt('Attending', 'Attending', $count, 'noun');
1544 return tt('Not Attending', 'Not Attending', $count, 'noun');
1547 return tt('Undecided', 'Undecided', $count, 'noun');