4 use Friendica\Core\System;
6 require_once "include/bbcode.php";
7 require_once "include/acl_selectors.php";
10 * Note: the code in 'item_extract_images' and 'item_redir_and_replace_images'
11 * is identical to the code in mod/message.php for 'item_extract_images' and
12 * 'item_redir_and_replace_images'
14 if (! function_exists('item_extract_images')) {
15 function item_extract_images($body) {
17 $saved_image = array();
22 $img_start = strpos($orig_body, '[img');
23 $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
24 $img_end = ($img_start !== false ? strpos(substr($orig_body, $img_start), '[/img]') : false);
25 while (($img_st_close !== false) && ($img_end !== false)) {
27 $img_st_close++; // make it point to AFTER the closing bracket
28 $img_end += $img_start;
30 if (! strcmp(substr($orig_body, $img_start + $img_st_close, 5), 'data:')) {
31 // This is an embedded image
33 $saved_image[$cnt] = substr($orig_body, $img_start + $img_st_close, $img_end - ($img_start + $img_st_close));
34 $new_body = $new_body . substr($orig_body, 0, $img_start) . '[!#saved_image' . $cnt . '#!]';
38 $new_body = $new_body . substr($orig_body, 0, $img_end + strlen('[/img]'));
41 $orig_body = substr($orig_body, $img_end + strlen('[/img]'));
43 if ($orig_body === false) {
44 // in case the body ends on a closing image tag
48 $img_start = strpos($orig_body, '[img');
49 $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
50 $img_end = ($img_start !== false ? strpos(substr($orig_body, $img_start), '[/img]') : false);
53 $new_body = $new_body . $orig_body;
55 return array('body' => $new_body, 'images' => $saved_image);
58 if (! function_exists('item_redir_and_replace_images')) {
59 function item_redir_and_replace_images($body, $images, $cid) {
65 $pos = get_bb_tag_pos($origbody, 'url', 1);
66 while ($pos !== false && $cnt < 1000) {
68 $search = '/\[url\=(.*?)\]\[!#saved_image([0-9]*)#!\]\[\/url\]' . '/is';
69 $replace = '[url=' . System::baseUrl() . '/redir/' . $cid
70 . '?f=1&url=' . '$1' . '][!#saved_image' . '$2' .'#!][/url]';
72 $newbody .= substr($origbody, 0, $pos['start']['open']);
73 $subject = substr($origbody, $pos['start']['open'], $pos['end']['close'] - $pos['start']['open']);
74 $origbody = substr($origbody, $pos['end']['close']);
75 if ($origbody === false) {
79 $subject = preg_replace($search, $replace, $subject);
83 $pos = get_bb_tag_pos($origbody, 'url', 1);
85 $newbody .= $origbody;
88 foreach ($images as $image) {
90 * We're depending on the property of 'foreach' (specified on the PHP website) that
91 * it loops over the array starting from the first element and going sequentially
92 * to the last element.
94 $newbody = str_replace('[!#saved_image' . $cnt . '#!]', '[img]' . $image . '[/img]', $newbody);
101 * Render actions localized
103 function localize_item(&$item) {
105 $extracted = item_extract_images($item['body']);
106 if ($extracted['images']) {
107 $item['body'] = item_redir_and_replace_images($extracted['body'], $extracted['images'], $item['contact-id']);
111 $xmlhead = "<" . "?xml version='1.0' encoding='UTF-8' ?" . ">";
112 if (activity_match($item['verb'], ACTIVITY_LIKE)
113 || activity_match($item['verb'], ACTIVITY_DISLIKE)
114 || activity_match($item['verb'], ACTIVITY_ATTEND)
115 || activity_match($item['verb'], ACTIVITY_ATTENDNO)
116 || activity_match($item['verb'], ACTIVITY_ATTENDMAYBE)) {
118 /// @TODO may hurt performance
119 $r = q("SELECT * FROM `item`, `contact`
120 WHERE `item`.`contact-id`=`contact`.`id`
121 AND `item`.`uri`='%s'",
122 dbesc($item['parent-uri']));
123 if (!dbm::is_result($r)) {
128 $author = '[url=' . $item['author-link'] . ']' . $item['author-name'] . '[/url]';
129 $objauthor = '[url=' . $obj['author-link'] . ']' . $obj['author-name'] . '[/url]';
131 switch ($obj['verb']) {
133 switch ($obj['object-type']) {
134 case ACTIVITY_OBJ_EVENT:
135 $post_type = t('event');
138 $post_type = t('status');
142 if ($obj['resource-id']) {
143 $post_type = t('photo');
145 preg_match("/\[url=([^]]*)\]/", $obj['body'], $m);
146 $rr['plink'] = $m[1];
148 $post_type = t('status');
152 $plink = '[url=' . $obj['plink'] . ']' . $post_type . '[/url]';
154 if (activity_match($item['verb'], ACTIVITY_LIKE)) {
155 $bodyverb = t('%1$s likes %2$s\'s %3$s');
157 elseif (activity_match($item['verb'], ACTIVITY_DISLIKE)) {
158 $bodyverb = t('%1$s doesn\'t like %2$s\'s %3$s');
160 elseif (activity_match($item['verb'], ACTIVITY_ATTEND)) {
161 $bodyverb = t('%1$s attends %2$s\'s %3$s');
163 elseif (activity_match($item['verb'], ACTIVITY_ATTENDNO)) {
164 $bodyverb = t('%1$s doesn\'t attend %2$s\'s %3$s');
166 elseif (activity_match($item['verb'], ACTIVITY_ATTENDMAYBE)) {
167 $bodyverb = t('%1$s attends maybe %2$s\'s %3$s');
169 $item['body'] = sprintf($bodyverb, $author, $objauthor, $plink);
172 if (activity_match($item['verb'], ACTIVITY_FRIEND)) {
174 if ($item['object-type']=="" || $item['object-type']!== ACTIVITY_OBJ_PERSON) return;
176 $Aname = $item['author-name'];
177 $Alink = $item['author-link'];
179 $xmlhead="<"."?xml version='1.0' encoding='UTF-8' ?".">";
181 $obj = parse_xml_string($xmlhead.$item['object']);
182 $links = parse_xml_string($xmlhead."<links>".unxmlify($obj->link)."</links>");
184 $Bname = $obj->title;
185 $Blink = ""; $Bphoto = "";
186 foreach ($links->link as $l) {
187 $atts = $l->attributes();
188 switch ($atts['rel']) {
189 case "alternate": $Blink = $atts['href'];
190 case "photo": $Bphoto = $atts['href'];
194 $A = '[url=' . zrl($Alink) . ']' . $Aname . '[/url]';
195 $B = '[url=' . zrl($Blink) . ']' . $Bname . '[/url]';
197 $Bphoto = '[url=' . zrl($Blink) . '][img]' . $Bphoto . '[/img][/url]';
200 $item['body'] = sprintf( t('%1$s is now friends with %2$s'), $A, $B)."\n\n\n".$Bphoto;
203 if (stristr($item['verb'], ACTIVITY_POKE)) {
204 $verb = urldecode(substr($item['verb'],strpos($item['verb'],'#')+1));
208 if ($item['object-type']=="" || $item['object-type']!== ACTIVITY_OBJ_PERSON) {
212 $Aname = $item['author-name'];
213 $Alink = $item['author-link'];
215 $xmlhead = "<" . "?xml version='1.0' encoding='UTF-8' ?" . ">";
217 $obj = parse_xml_string($xmlhead.$item['object']);
218 $links = parse_xml_string($xmlhead."<links>".unxmlify($obj->link)."</links>");
220 $Bname = $obj->title;
223 foreach ($links->link as $l) {
224 $atts = $l->attributes();
225 switch ($atts['rel']) {
226 case "alternate": $Blink = $atts['href'];
227 case "photo": $Bphoto = $atts['href'];
231 $A = '[url=' . zrl($Alink) . ']' . $Aname . '[/url]';
232 $B = '[url=' . zrl($Blink) . ']' . $Bname . '[/url]';
234 $Bphoto = '[url=' . zrl($Blink) . '][img=80x80]' . $Bphoto . '[/img][/url]';
238 * we can't have a translation string with three positions but no distinguishable text
239 * So here is the translate string.
241 $txt = t('%1$s poked %2$s');
243 // now translate the verb
244 $poked_t = trim(sprintf($txt, "", ""));
245 $txt = str_replace( $poked_t, t($verb), $txt);
247 // then do the sprintf on the translation string
249 $item['body'] = sprintf($txt, $A, $B). "\n\n\n" . $Bphoto;
252 if (stristr($item['verb'], ACTIVITY_MOOD)) {
253 $verb = urldecode(substr($item['verb'], strpos($item['verb'], '#') + 1));
258 $Aname = $item['author-name'];
259 $Alink = $item['author-link'];
260 $A = '[url=' . zrl($Alink) . ']' . $Aname . '[/url]';
262 $txt = t('%1$s is currently %2$s');
264 $item['body'] = sprintf($txt, $A, t($verb));
267 if (activity_match($item['verb'], ACTIVITY_TAG)) {
268 /// @TODO may hurt performance "joining" two tables + asterisk
269 $r = q("SELECT * FROM `item`, `contact`
270 WHERE `item`.`contact-id`=`contact`.`id`
271 AND `item`.`uri`='%s'",
272 dbesc($item['parent-uri']));
274 if (!dbm::is_result($r)) {
280 $author = '[url=' . zrl($item['author-link']) . ']' . $item['author-name'] . '[/url]';
281 $objauthor = '[url=' . zrl($obj['author-link']) . ']' . $obj['author-name'] . '[/url]';
283 switch ($obj['verb']) {
285 switch ($obj['object-type']) {
286 case ACTIVITY_OBJ_EVENT:
287 $post_type = t('event');
290 $post_type = t('status');
294 if ($obj['resource-id']) {
295 $post_type = t('photo');
296 $m=array(); preg_match("/\[url=([^]]*)\]/", $obj['body'], $m);
297 $rr['plink'] = $m[1];
299 $post_type = t('status');
301 // Let's break everthing ... ;-)
304 $plink = '[url=' . $obj['plink'] . ']' . $post_type . '[/url]';
306 $parsedobj = parse_xml_string($xmlhead.$item['object']);
308 $tag = sprintf('#[url=%s]%s[/url]', $parsedobj->id, $parsedobj->content);
309 $item['body'] = sprintf( t('%1$s tagged %2$s\'s %3$s with %4$s'), $author, $objauthor, $plink, $tag );
312 if (activity_match($item['verb'], ACTIVITY_FAVORITE)) {
314 if ($item['object-type'] == "") {
318 $Aname = $item['author-name'];
319 $Alink = $item['author-link'];
321 $xmlhead = "<" . "?xml version='1.0' encoding='UTF-8' ?" . ">";
323 $obj = parse_xml_string($xmlhead.$item['object']);
324 if (strlen($obj->id)) {
325 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
330 if (dbm::is_result($r) && $r[0]['plink']) {
332 $Bname = $target['author-name'];
333 $Blink = $target['author-link'];
334 $A = '[url=' . zrl($Alink) . ']' . $Aname . '[/url]';
335 $B = '[url=' . zrl($Blink) . ']' . $Bname . '[/url]';
336 $P = '[url=' . $target['plink'] . ']' . t('post/item') . '[/url]';
337 $item['body'] = sprintf( t('%1$s marked %2$s\'s %3$s as favorite'), $A, $B, $P)."\n";
342 if (preg_match_all('/@\[url=(.*?)\]/is', $item['body'], $matches, PREG_SET_ORDER)) {
343 foreach ($matches as $mtch) {
344 if (! strpos($mtch[1], 'zrl=')) {
345 $item['body'] = str_replace($mtch[0], '@[url=' . zrl($mtch[1]) . ']', $item['body']);
350 // add zrl's to public images
351 $photo_pattern = "/\[url=(.*?)\/photos\/(.*?)\/image\/(.*?)\]\[img(.*?)\]h(.*?)\[\/img\]\[\/url\]/is";
352 if (preg_match($photo_pattern, $item['body'])) {
353 $photo_replace = '[url=' . zrl('$1' . '/photos/' . '$2' . '/image/' . '$3' ,true) . '][img' . '$4' . ']h' . '$5' . '[/img][/url]';
354 $item['body'] = bb_tag_preg_replace($photo_pattern, $photo_replace, 'url', $item['body']);
357 // add sparkle links to appropriate permalinks
359 $x = stristr($item['plink'],'/display/');
362 $y = best_link_url($item, $sparkle);
364 if (strstr($y, '/redir/')) {
365 $item['plink'] = $y . '?f=&url=' . $item['plink'];
371 * Count the total of comments on this item and its desendants
372 * @TODO proper type-hint + doc-tag
374 function count_descendants($item) {
375 $total = count($item['children']);
378 foreach ($item['children'] as $child) {
379 if (! visible_activity($child)) {
382 $total += count_descendants($child);
389 function visible_activity($item) {
392 * likes (etc.) can apply to other things besides posts. Check if they are post children,
393 * in which case we handle them specially
395 $hidden_activities = array(ACTIVITY_LIKE, ACTIVITY_DISLIKE, ACTIVITY_ATTEND, ACTIVITY_ATTENDNO, ACTIVITY_ATTENDMAYBE);
396 foreach ($hidden_activities as $act) {
397 if (activity_match($item['verb'], $act)) {
402 if (activity_match($item['verb'], ACTIVITY_FOLLOW) && $item['object-type'] === ACTIVITY_OBJ_NOTE) {
403 if (! (($item['self']) && ($item['uid'] == local_user()))) {
412 * @brief SQL query for items
414 function item_query() {
415 return "SELECT " . item_fieldlists() . " FROM `item` " .
416 item_joins() . " WHERE " . item_condition();
420 * @brief List of all data fields that are needed for displaying items
422 function item_fieldlists() {
425 These Fields are not added below (yet). They are here to for bug search.
430 `item`.`target-type`,
432 `item`.`resource-id`,
446 `item`.`gcontact-id`,
450 return "`item`.`author-id`, `item`.`author-link`, `item`.`author-name`, `item`.`author-avatar`,
451 `item`.`owner-id`, `item`.`owner-link`, `item`.`owner-name`, `item`.`owner-avatar`,
452 `item`.`contact-id`, `item`.`uid`, `item`.`id`, `item`.`parent`,
453 `item`.`uri`, `item`.`thr-parent`, `item`.`parent-uri`,
454 `item`.`commented`, `item`.`created`, `item`.`edited`, `item`.`received`,
455 `item`.`verb`, `item`.`object-type`, `item`.`postopts`, `item`.`plink`,
456 `item`.`guid`, `item`.`wall`, `item`.`private`, `item`.`starred`,
457 `item`.`title`, `item`.`body`, `item`.`file`, `item`.`event-id`,
458 `item`.`location`, `item`.`coord`, `item`.`app`, `item`.`attach`,
459 `item`.`rendered-hash`, `item`.`rendered-html`, `item`.`object`,
460 `item`.`allow_cid`, `item`.`allow_gid`, `item`.`deny_cid`, `item`.`deny_gid`,
461 `item`.`id` AS `item_id`, `item`.`network` AS `item_network`,
463 `author`.`thumb` AS `author-thumb`, `owner`.`thumb` AS `owner-thumb`,
465 `contact`.`network`, `contact`.`url`, `contact`.`name`, `contact`.`writable`,
466 `contact`.`self`, `contact`.`id` AS `cid`, `contact`.`alias`";
470 * @brief SQL join for contacts that are needed for displaying items
472 function item_joins() {
473 return "STRAIGHT_JOIN `contact` ON `contact`.`id` = `item`.`contact-id` AND
474 (NOT `contact`.`blocked` OR `contact`.`pending`)
475 LEFT JOIN `contact` AS `author` ON `author`.`id`=`item`.`author-id`
476 LEFT JOIN `contact` AS `owner` ON `owner`.`id`=`item`.`owner-id`";
480 * @brief SQL condition for items that are needed for displaying items
482 function item_condition() {
483 return "`item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`";
486 if (!function_exists('conversation')) {
488 * "Render" a conversation or list of items for HTML display.
489 * There are two major forms of display:
490 * - Sequential or unthreaded ("New Item View" or search results)
491 * - conversation view
492 * The $mode parameter decides between the various renderings and also
493 * figures out how to determine page owner and other contextual items
494 * that are based on unique features of the calling module.
497 function conversation(App $a, $items, $mode, $update, $preview = false) {
499 require_once 'include/bbcode.php';
500 require_once 'include/Contact.php';
501 require_once 'mod/proxy.php';
503 $ssl_state = ((local_user()) ? true : false);
506 $page_writeable = false;
507 $live_update_div = '';
512 $str_blocked = get_pconfig(local_user(), 'system', 'blocked');
514 $arr_blocked = explode(',', $str_blocked);
515 for ($x = 0; $x < count($arr_blocked); $x ++) {
516 $arr_blocked[$x] = trim($arr_blocked[$x]);
522 $previewing = (($preview) ? ' preview ' : '');
524 if ($mode === 'network') {
525 $profile_owner = local_user();
526 $page_writeable = true;
529 * The special div is needed for liveUpdate to kick in for this page.
530 * We only launch liveUpdate if you aren't filtering in some incompatible
531 * way and also you aren't writing a comment (discovered in javascript).
533 $live_update_div = '<div id="live-network"></div>' . "\r\n"
534 . "<script> var profile_uid = " . $_SESSION['uid']
535 . "; var netargs = '" . substr($a->cmd, 8)
537 . ((x($_GET, 'cid')) ? '&cid=' . $_GET['cid'] : '')
538 . ((x($_GET, 'search')) ? '&search=' . $_GET['search'] : '')
539 . ((x($_GET, 'star')) ? '&star=' . $_GET['star'] : '')
540 . ((x($_GET, 'order')) ? '&order=' . $_GET['order'] : '')
541 . ((x($_GET, 'bmark')) ? '&bmark=' . $_GET['bmark'] : '')
542 . ((x($_GET, 'liked')) ? '&liked=' . $_GET['liked'] : '')
543 . ((x($_GET, 'conv')) ? '&conv=' . $_GET['conv'] : '')
544 . ((x($_GET, 'spam')) ? '&spam=' . $_GET['spam'] : '')
545 . ((x($_GET, 'nets')) ? '&nets=' . $_GET['nets'] : '')
546 . ((x($_GET, 'cmin')) ? '&cmin=' . $_GET['cmin'] : '')
547 . ((x($_GET, 'cmax')) ? '&cmax=' . $_GET['cmax'] : '')
548 . ((x($_GET, 'file')) ? '&file=' . $_GET['file'] : '')
550 . "'; var profile_page = " . $a->pager['page'] . "; </script>\r\n";
552 } elseif ($mode === 'profile') {
553 $profile_owner = $a->profile['profile_uid'];
554 $page_writeable = can_write_wall($a,$profile_owner);
557 $tab = notags(trim($_GET['tab']));
558 $tab = ( $tab ? $tab : 'posts' );
559 if ($tab === 'posts') {
561 * This is ugly, but we can't pass the profile_uid through the session to the ajax updater,
562 * because browser prefetching might change it on us. We have to deliver it with the page.
565 $live_update_div = '<div id="live-profile"></div>' . "\r\n"
566 . "<script> var profile_uid = " . $a->profile['profile_uid']
567 . "; var netargs = '?f='; var profile_page = " . $a->pager['page'] . "; </script>\r\n";
570 } elseif ($mode === 'notes') {
571 $profile_owner = local_user();
572 $page_writeable = true;
574 $live_update_div = '<div id="live-notes"></div>' . "\r\n"
575 . "<script> var profile_uid = " . local_user()
576 . "; var netargs = '/?f='; var profile_page = " . $a->pager['page'] . "; </script>\r\n";
578 } elseif ($mode === 'display') {
579 $profile_owner = $a->profile['uid'];
580 $page_writeable = can_write_wall($a,$profile_owner);
582 $live_update_div = '<div id="live-display"></div>' . "\r\n"
583 . "<script> var profile_uid = " . $_SESSION['uid'] . ";"
584 . " var profile_page = 1; </script>";
586 } elseif ($mode === 'community') {
588 $page_writeable = false;
590 $live_update_div = '<div id="live-community"></div>' . "\r\n"
591 . "<script> var profile_uid = -1; var netargs = '/?f='; var profile_page = " . $a->pager['page'] . "; </script>\r\n";
593 } elseif ($mode === 'search') {
594 $live_update_div = '<div id="live-search"></div>' . "\r\n";
597 $page_dropping = ((local_user() && local_user() == $profile_owner) ? true : false);
601 $return_url = $_SESSION['return_url'];
603 $return_url = $_SESSION['return_url'] = $a->query_string;
606 $cb = array('items' => $items, 'mode' => $mode, 'update' => $update, 'preview' => $preview);
607 call_hooks('conversation_start',$cb);
609 $items = $cb['items'];
611 $cmnt_tpl = get_markup_template('comment_item.tpl');
612 $hide_comments_tpl = get_markup_template('hide_comments.tpl');
614 $conv_responses = array(
615 'like' => array('title' => t('Likes','title')), 'dislike' => array('title' => t('Dislikes','title')),
616 'attendyes' => array('title' => t('Attending','title')), 'attendno' => array('title' => t('Not attending','title')), 'attendmaybe' => array('title' => t('Might attend','title'))
619 // array with html for each thread (parent+comments)
623 $page_template = get_markup_template("conversation.tpl");
625 if ($items && count($items)) {
627 if ($mode === 'network-new' || $mode === 'search' || $mode === 'community') {
630 * "New Item View" on network page or search page results
631 * - just loop through the items and format them minimally for display
634 /// @TODO old lost code?
635 // $tpl = get_markup_template('search_item.tpl');
636 $tpl = 'search_item.tpl';
638 foreach ($items as $item) {
642 foreach ($arr_blocked as $b) {
643 if ($b && link_compare($item['author-link'], $b)) {
661 if ($mode === 'search' || $mode === 'community') {
662 if (((activity_match($item['verb'], ACTIVITY_LIKE)) || (activity_match($item['verb'], ACTIVITY_DISLIKE)))
663 && ($item['id'] != $item['parent']))
665 $nickname = $item['nickname'];
667 $nickname = $a->user['nickname'];
670 // prevent private email from leaking.
671 if ($item['network'] === NETWORK_MAIL && local_user() != $item['uid']) {
675 $profile_name = ((strlen($item['author-name'])) ? $item['author-name'] : $item['name']);
676 if ($item['author-link'] && (! $item['author-name'])) {
677 $profile_name = $item['author-link'];
684 $searchpath = System::baseUrl()."/search?tag=";
686 $taglist = dba::select('term', array('type', 'term', 'url'),
687 array("`otype` = ? AND `oid` = ? AND `type` IN (?, ?)", TERM_OBJ_POST, $item['id'], TERM_HASHTAG, TERM_MENTION),
688 array('order' => array('tid')));
690 while ($tag = dba::fetch($taglist)) {
691 if ($tag["url"] == "") {
692 $tag["url"] = $searchpath . strtolower($tag["term"]);
695 $tag["url"] = best_link_url($item, $sp, $tag["url"]);
697 if ($tag["type"] == TERM_HASHTAG) {
698 $hashtags[] = "#<a href=\"" . $tag["url"] . "\" target=\"_blank\">" . $tag["term"] . "</a>";
700 } elseif ($tag["type"] == TERM_MENTION) {
701 $mentions[] = "@<a href=\"" . $tag["url"] . "\" target=\"_blank\">" . $tag["term"] . "</a>";
704 $tags[] = $prefix."<a href=\"" . $tag["url"] . "\" target=\"_blank\">" . $tag["term"] . "</a>";
706 dba::close($taglist);
709 $profile_link = best_link_url($item, $sp);
710 if ($profile_link === 'mailbox') {
715 $sparkle = ' sparkle';
717 $profile_link = zrl($profile_link);
720 if (!x($item, 'author-thumb') || ($item['author-thumb'] == "")) {
721 $author_contact = get_contact_details_by_url($item['author-link'], $profile_owner);
722 if ($author_contact["thumb"]) {
723 $item['author-thumb'] = $author_contact["thumb"];
725 $item['author-thumb'] = $item['author-avatar'];
729 if (!isset($item['owner-thumb']) || ($item['owner-thumb'] == "")) {
730 $owner_contact = get_contact_details_by_url($item['owner-link'], $profile_owner);
731 if ($owner_contact["thumb"]) {
732 $item['owner-thumb'] = $owner_contact["thumb"];
734 $item['owner-thumb'] = $item['owner-avatar'];
738 $locate = array('location' => $item['location'], 'coord' => $item['coord'], 'html' => '');
739 call_hooks('render_location',$locate);
741 $location = ((strlen($locate['html'])) ? $locate['html'] : render_location_dummy($locate));
743 localize_item($item);
744 if ($mode === 'network-new') {
751 'dropping' => $dropping,
752 'pagedrop' => $page_dropping,
753 'select' => t('Select'),
754 'delete' => t('Delete'),
758 $isstarred = "unstarred";
761 $likebuttons = false;
764 $body = prepare_body($item, true, $preview);
766 list($categories, $folders) = get_cats_and_terms($item);
768 if ($a->theme['template_engine'] === 'internal') {
769 $profile_name_e = template_escape($profile_name);
770 $item['title_e'] = template_escape($item['title']);
771 $body_e = template_escape($body);
772 $tags_e = template_escape($tags);
773 $hashtags_e = template_escape($hashtags);
774 $mentions_e = template_escape($mentions);
775 $location_e = template_escape($location);
776 $owner_name_e = template_escape($owner_name);
778 $profile_name_e = $profile_name;
779 $item['title_e'] = $item['title'];
782 $hashtags_e = $hashtags;
783 $mentions_e = $mentions;
784 $location_e = $location;
785 $owner_name_e = $owner_name;
788 if ($item['item_network'] == "") {
789 $item['item_network'] = $item['network'];
794 'id' => (($preview) ? 'P0' : $item['item_id']),
795 'network' => $item['item_network'],
796 'network_name' => network_to_name($item['item_network'], $profile_link),
797 'linktitle' => sprintf( t('View %s\'s profile @ %s'), $profile_name, ((strlen($item['author-link'])) ? $item['author-link'] : $item['url'])),
798 'profile_url' => $profile_link,
799 'item_photo_menu' => item_photo_menu($item),
800 'name' => $profile_name_e,
801 'sparkle' => $sparkle,
803 'thumb' => System::removedBaseUrl(proxy_url($item['author-thumb'], false, PROXY_SIZE_THUMB)),
804 'title' => $item['title_e'],
807 'hashtags' => $hashtags_e,
808 'mentions' => $mentions_e,
809 'txt_cats' => t('Categories:'),
810 'txt_folders' => t('Filed under:'),
811 'has_cats' => ((count($categories)) ? 'true' : ''),
812 'has_folders' => ((count($folders)) ? 'true' : ''),
813 'categories' => $categories,
814 'folders' => $folders,
815 'text' => strip_tags($body_e),
816 'localtime' => datetime_convert('UTC', date_default_timezone_get(), $item['created'], 'r'),
817 'ago' => (($item['app']) ? sprintf( t('%s from %s'),relative_date($item['created']),$item['app']) : relative_date($item['created'])),
818 'location' => $location_e,
820 'owner_name' => $owner_name_e,
821 'owner_url' => $owner_url,
822 'owner_photo' => System::removedBaseUrl(proxy_url($item['owner-thumb'], false, PROXY_SIZE_THUMB)),
823 'plink' => get_plink($item),
825 'isstarred' => $isstarred,
828 'vote' => $likebuttons,
832 //'conv' => (($preview) ? '' : array('href'=> 'display/' . $nickname . '/' . $item['id'], 'title'=> t('View in context'))),
833 'conv' => (($preview) ? '' : array('href'=> 'display/'.$item['guid'], 'title'=> t('View in context'))),
834 'previewing' => $previewing,
835 'wait' => t('Please wait'),
839 $arr = array('item' => $item, 'output' => $tmp_item);
840 call_hooks('display_item', $arr);
842 $threads[$threadsid]['id'] = $item['item_id'];
843 $threads[$threadsid]['network'] = $item['item_network'];
844 $threads[$threadsid]['items'] = array($arr['output']);
849 $page_template = get_markup_template("threaded_conversation.tpl");
851 require_once 'object/Conversation.php';
852 require_once 'object/Item.php';
854 $conv = new Conversation($mode, $preview);
857 * get all the topmost parents
858 * this shouldn't be needed, as we should have only them in our array
859 * But for now, this array respects the old style, just in case
862 foreach ($items as $item) {
866 foreach ($arr_blocked as $b) {
867 if ($b && link_compare($item['author-link'], $b)) {
877 // Can we put this after the visibility check?
878 builtin_activity_puller($item, $conv_responses);
880 // Only add what is visible
881 if ($item['network'] === NETWORK_MAIL && local_user() != $item['uid']) {
885 if (! visible_activity($item)) {
889 call_hooks('display_item', $arr);
891 $item['pagedrop'] = $page_dropping;
893 if ($item['id'] == $item['parent']) {
894 $item_object = new Item($item);
895 $conv->add_thread($item_object);
899 $threads = $conv->get_template_data($conv_responses);
902 logger('[ERROR] conversation : Failed to get template data.', LOGGER_DEBUG);
908 $o = replace_macros($page_template, array(
909 '$baseurl' => System::baseUrl($ssl_state),
910 '$return_path' => $a->query_string,
911 '$live_update' => $live_update_div,
912 '$remove' => t('remove'),
915 '$threads' => $threads,
916 '$dropping' => ($page_dropping && feature_enabled(local_user(), 'multi_delete') ? t('Delete Selected Items') : False),
922 function best_link_url($item, &$sparkle, $url = '') {
927 $clean_url = normalise_link($item['author-link']);
930 $r = dba::select('contact', array('id'),
931 array('network' => NETWORK_DFRN, 'uid' => local_user(), 'nurl' => normalise_link($clean_url), 'pending' => false),
932 array('limit' => 1));
933 if (dbm::is_result($r)) {
934 $best_url = 'redir/' . $r['id'];
937 $hostname = get_app()->get_hostname();
938 if (!strstr($url, $hostname)) {
939 $best_url .= "?url=".$url;
949 } elseif (strlen($item['author-link'])) {
950 $best_url = $item['author-link'];
952 $best_url = $item['url'];
960 function item_photo_menu($item) {
970 if ((local_user()) && local_user() == $item['uid'] && $item['parent'] == $item['id'] && (! $item['self'])) {
971 $sub_link = 'javascript:dosubthread(' . $item['id'] . '); return false;';
975 $profile_link = best_link_url($item, $sparkle);
976 if ($profile_link === 'mailbox') {
983 $r = dba::select('contact', array('id', 'network', 'rel'), array('uid' => local_user(), 'nurl' => normalise_link($item['author-link'])), array('limit' => 1));
984 if (dbm::is_result($r)) {
986 $network = $r['network'];
991 $status_link = $profile_link . '?url=status';
992 $photos_link = $profile_link . '?url=photos';
993 $profile_link = $profile_link . '?url=profile';
996 $profile_link = zrl($profile_link);
999 if ($cid && !$item['self']) {
1000 $poke_link = 'poke/?f=&c=' . $cid;
1001 $contact_url = 'contacts/' . $cid;
1002 $posts_link = 'contacts/' . $cid . '/posts';
1004 if (in_array($network, array(NETWORK_DFRN, NETWORK_DIASPORA))) {
1005 $pm_url = 'message/new/' . $cid;
1011 t('Follow Thread') => $sub_link,
1012 t('View Status') => $status_link,
1013 t('View Profile') => $profile_link,
1014 t('View Photos') => $photos_link,
1015 t('Network Posts') => $posts_link,
1016 t('View Contact') => $contact_url,
1017 t('Send PM') => $pm_url
1020 if ($network == NETWORK_DFRN) {
1021 $menu[t("Poke")] = $poke_link;
1024 if ((($cid == 0) || ($rel == CONTACT_IS_FOLLOWER)) &&
1025 in_array($item['network'], array(NETWORK_DFRN, NETWORK_OSTATUS, NETWORK_DIASPORA))) {
1026 $menu[t('Connect/Follow')] = 'follow?url=' . urlencode($item['author-link']);
1029 $menu = array(t('View Profile') => $item['author-link']);
1032 $args = array('item' => $item, 'menu' => $menu);
1034 call_hooks('item_photo_menu', $args);
1036 $menu = $args['menu'];
1039 foreach ($menu as $k => $v) {
1040 if (strpos($v, 'javascript:') === 0) {
1041 $v = substr($v, 11);
1042 $o .= '<li role="menuitem"><a onclick="' . $v . '">' . $k . '</a></li>' . PHP_EOL;
1044 $o .= '<li role="menuitem"><a href="' . $v . '">' . $k . '</a></li>' . PHP_EOL;
1050 if (! function_exists('builtin_activity_puller')) {
1052 * @brief Checks item to see if it is one of the builtin activities (like/dislike, event attendance, consensus items, etc.)
1053 * Increments the count of each matching activity and adds a link to the author as needed.
1055 * @param array $item
1056 * @param array &$conv_responses (already created with builtin activity structure)
1059 function builtin_activity_puller($item, &$conv_responses) {
1060 foreach ($conv_responses as $mode => $v) {
1066 $verb = ACTIVITY_LIKE;
1069 $verb = ACTIVITY_DISLIKE;
1072 $verb = ACTIVITY_ATTEND;
1075 $verb = ACTIVITY_ATTENDNO;
1078 $verb = ACTIVITY_ATTENDMAYBE;
1085 if ((activity_match($item['verb'], $verb)) && ($item['id'] != $item['parent'])) {
1086 $url = $item['author-link'];
1087 if ((local_user()) && (local_user() == $item['uid']) && ($item['network'] === NETWORK_DFRN) && (! $item['self']) && (link_compare($item['author-link'], $item['url']))) {
1088 $url = 'redir/' . $item['contact-id'];
1089 $sparkle = ' class="sparkle" ';
1094 $url = '<a href="'. $url . '"'. $sparkle .'>' . htmlentities($item['author-name']) . '</a>';
1096 if (! $item['thr-parent']) {
1097 $item['thr-parent'] = $item['parent-uri'];
1100 if (! ((isset($conv_responses[$mode][$item['thr-parent'] . '-l']))
1101 && (is_array($conv_responses[$mode][$item['thr-parent'] . '-l'])))) {
1102 $conv_responses[$mode][$item['thr-parent'] . '-l'] = array();
1105 // only list each unique author once
1106 if (in_array($url,$conv_responses[$mode][$item['thr-parent'] . '-l'])) {
1110 if (! isset($conv_responses[$mode][$item['thr-parent']])) {
1111 $conv_responses[$mode][$item['thr-parent']] = 1;
1113 $conv_responses[$mode][$item['thr-parent']] ++;
1116 if (public_contact() == $item['author-id']) {
1117 $conv_responses[$mode][$item['thr-parent'] . '-self'] = 1;
1120 $conv_responses[$mode][$item['thr-parent'] . '-l'][] = $url;
1122 // there can only be one activity verb per item so if we found anything, we can stop looking
1128 if (! function_exists('format_like')) {
1130 * Format the vote text for a profile item
1131 * @param int $cnt = number of people who vote the item
1132 * @param array $arr = array of pre-linked names of likers/dislikers
1133 * @param string $type = one of 'like, 'dislike', 'attendyes', 'attendno', 'attendmaybe'
1134 * @param int $id = item id
1135 * @return formatted text
1137 function format_like($cnt, array $arr, $type, $id) {
1144 // Phrase if there is only one liker. In other cases it will be uses for the expanded
1145 // list which show all likers
1148 $phrase = sprintf( t('%s likes this.'), $likers);
1151 $phrase = sprintf( t('%s doesn\'t like this.'), $likers);
1154 $phrase = sprintf( t('%s attends.'), $likers);
1157 $phrase = sprintf( t('%s doesn\'t attend.'), $likers);
1159 case 'attendmaybe' :
1160 $phrase = sprintf( t('%s attends maybe.'), $likers);
1166 $total = count($arr);
1167 if ($total >= MAX_LIKERS) {
1168 $arr = array_slice($arr, 0, MAX_LIKERS - 1);
1170 if ($total < MAX_LIKERS) {
1171 $last = t('and') . ' ' . $arr[count($arr)-1];
1172 $arr2 = array_slice($arr, 0, -1);
1173 $str = implode(', ', $arr2) . ' ' . $last;
1175 if ($total >= MAX_LIKERS) {
1176 $str = implode(', ', $arr);
1177 $str .= sprintf( t(', and %d other people'), $total - MAX_LIKERS );
1182 $spanatts = "class=\"fakelink\" onclick=\"openClose('{$type}list-$id');\"";
1186 $phrase = sprintf( t('<span %1$s>%2$d people</span> like this'), $spanatts, $cnt);
1187 $explikers = sprintf( t('%s like this.'), $likers);
1190 $phrase = sprintf( t('<span %1$s>%2$d people</span> don\'t like this'), $spanatts, $cnt);
1191 $explikers = sprintf( t('%s don\'t like this.'), $likers);
1194 $phrase = sprintf( t('<span %1$s>%2$d people</span> attend'), $spanatts, $cnt);
1195 $explikers = sprintf( t('%s attend.'), $likers);
1198 $phrase = sprintf( t('<span %1$s>%2$d people</span> don\'t attend'), $spanatts, $cnt);
1199 $explikers = sprintf( t('%s don\'t attend.'), $likers);
1202 $phrase = sprintf( t('<span %1$s>%2$d people</span> attend maybe'), $spanatts, $cnt);
1203 $explikers = sprintf( t('%s anttend maybe.'), $likers);
1207 $expanded .= "\t" . '<div class="wall-item-' . $type . '-expanded" id="' . $type . 'list-' . $id . '" style="display: none;" >' . $explikers . EOL . '</div>';
1211 $o .= replace_macros(get_markup_template('voting_fakelink.tpl'), array(
1212 '$phrase' => $phrase,
1221 function status_editor(App $a, $x, $notes_cid = 0, $popup = false) {
1224 $geotag = (x($x, 'allow_location') ? replace_macros(get_markup_template('jot_geotag.tpl'), array()) : '');
1226 $tpl = get_markup_template('jot-header.tpl');
1227 $a->page['htmlhead'] .= replace_macros($tpl, array(
1228 '$newpost' => 'true',
1229 '$baseurl' => System::baseUrl(true),
1230 '$geotag' => $geotag,
1231 '$nickname' => $x['nickname'],
1232 '$ispublic' => t('Visible to <strong>everybody</strong>'),
1233 '$linkurl' => t('Please enter a link URL:'),
1234 '$vidurl' => t("Please enter a video link/URL:"),
1235 '$audurl' => t("Please enter an audio link/URL:"),
1236 '$term' => t('Tag term:'),
1237 '$fileas' => t('Save to Folder:'),
1238 '$whereareu' => t('Where are you right now?'),
1239 '$delitems' => t('Delete item(s)?')
1242 $tpl = get_markup_template('jot-end.tpl');
1243 $a->page['end'] .= replace_macros($tpl, array(
1244 '$newpost' => 'true',
1245 '$baseurl' => System::baseUrl(true),
1246 '$geotag' => $geotag,
1247 '$nickname' => $x['nickname'],
1248 '$ispublic' => t('Visible to <strong>everybody</strong>'),
1249 '$linkurl' => t('Please enter a link URL:'),
1250 '$vidurl' => t("Please enter a video link/URL:"),
1251 '$audurl' => t("Please enter an audio link/URL:"),
1252 '$term' => t('Tag term:'),
1253 '$fileas' => t('Save to Folder:'),
1254 '$whereareu' => t('Where are you right now?')
1258 call_hooks('jot_tool', $jotplugins);
1260 // Private/public post links for the non-JS ACL form
1262 if (x($_REQUEST, 'public')) {
1266 $query_str = $a->query_string;
1267 if (strpos($query_str, 'public=1') !== false) {
1268 $query_str = str_replace(array('?public=1', '&public=1'), array('', ''), $query_str);
1272 * I think $a->query_string may never have ? in it, but I could be wrong
1273 * It looks like it's from the index.php?q=[etc] rewrite that the web
1274 * server does, which converts any ? to &, e.g. suggest&ignore=61 for suggest?ignore=61
1276 if (strpos($query_str, '?') === false) {
1277 $public_post_link = '?public=1';
1279 $public_post_link = '&public=1';
1282 // $tpl = replace_macros($tpl,array('$jotplugins' => $jotplugins));
1283 $tpl = get_markup_template("jot.tpl");
1285 $o .= replace_macros($tpl,array(
1286 '$return_path' => $query_str,
1287 '$action' => 'item',
1288 '$share' => (x($x,'button') ? $x['button'] : t('Share')),
1289 '$upload' => t('Upload photo'),
1290 '$shortupload' => t('upload photo'),
1291 '$attach' => t('Attach file'),
1292 '$shortattach' => t('attach file'),
1293 '$weblink' => t('Insert web link'),
1294 '$shortweblink' => t('web link'),
1295 '$video' => t('Insert video link'),
1296 '$shortvideo' => t('video link'),
1297 '$audio' => t('Insert audio link'),
1298 '$shortaudio' => t('audio link'),
1299 '$setloc' => t('Set your location'),
1300 '$shortsetloc' => t('set location'),
1301 '$noloc' => t('Clear browser location'),
1302 '$shortnoloc' => t('clear location'),
1303 '$title' => $x['title'],
1304 '$placeholdertitle' => t('Set title'),
1305 '$category' => $x['category'],
1306 '$placeholdercategory' => (feature_enabled(local_user(), 'categories') ? t('Categories (comma-separated list)') : ''),
1307 '$wait' => t('Please wait'),
1308 '$permset' => t('Permission settings'),
1309 '$shortpermset' => t('permissions'),
1310 '$ptyp' => (($notes_cid) ? 'note' : 'wall'),
1311 '$content' => $x['content'],
1312 '$post_id' => $x['post_id'],
1313 '$baseurl' => System::baseUrl(true),
1314 '$defloc' => $x['default_location'],
1315 '$visitor' => $x['visitor'],
1316 '$pvisit' => (($notes_cid) ? 'none' : $x['visitor']),
1317 '$public' => t('Public post'),
1318 '$jotnets' => $jotnets,
1319 '$lockstate' => $x['lockstate'],
1320 '$bang' => $x['bang'],
1321 '$profile_uid' => $x['profile_uid'],
1322 '$preview' => ((feature_enabled($x['profile_uid'],'preview')) ? t('Preview') : ''),
1323 '$jotplugins' => $jotplugins,
1324 '$notes_cid' => $notes_cid,
1325 '$sourceapp' => t($a->sourcename),
1326 '$cancel' => t('Cancel'),
1327 '$rand_num' => random_digits(12),
1329 // ACL permissions box
1330 '$acl' => $x['acl'],
1331 '$acl_data' => $x['acl_data'],
1332 '$group_perms' => t('Post to Groups'),
1333 '$contact_perms' => t('Post to Contacts'),
1334 '$private' => t('Private post'),
1335 '$is_private' => $private_post,
1336 '$public_link' => $public_post_link,
1338 //jot nav tab (used in some themes)
1339 '$message' => t('Message'),
1340 '$browser' => t('Browser'),
1344 if ($popup == true) {
1345 $o = '<div id="jot-popup" style="display: none;">'.$o.'</div>';
1352 function get_item_children($arr, $parent) {
1353 $children = array();
1355 foreach ($arr as $item) {
1356 if ($item['id'] != $item['parent']) {
1357 if (get_config('system', 'thread_allow') && $a->theme_thread_allow) {
1358 // Fallback to parent-uri if thr-parent is not set
1359 $thr_parent = $item['thr-parent'];
1360 if ($thr_parent == '') {
1361 $thr_parent = $item['parent-uri'];
1364 if ($thr_parent == $parent['uri']) {
1365 $item['children'] = get_item_children($arr, $item);
1366 $children[] = $item;
1368 } elseif ($item['parent'] == $parent['id']) {
1369 $children[] = $item;
1376 /// @TODO Add type-hint
1377 function sort_item_children($items) {
1379 usort($result, 'sort_thr_created_rev');
1380 foreach ($result as $k => $i) {
1381 if (count($result[$k]['children'])) {
1382 $result[$k]['children'] = sort_item_children($result[$k]['children']);
1388 /// @TODO Add type-hint
1389 function add_children_to_list($children, &$arr) {
1390 foreach ($children as $y) {
1392 if (count($y['children'])) {
1393 add_children_to_list($y['children'], $arr);
1398 /// @TODO Add type-hint
1399 function conv_sort($arr, $order) {
1401 if ((!(is_array($arr) && count($arr)))) {
1406 $children = array();
1410 * This is a preparation for having two different items with the same uri in one thread
1411 * This will otherwise lead to an endless loop.
1413 foreach ($arr as $x) {
1414 if (!isset($newarr[$x['uri']])) {
1415 $newarr[$x['uri']] = $x;
1421 foreach ($arr as $x) {
1422 if ($x['id'] == $x['parent']) {
1427 if (stristr($order, 'created')) {
1428 usort($parents, 'sort_thr_created');
1429 } elseif (stristr($order, 'commented')) {
1430 usort($parents, 'sort_thr_commented');
1433 if (count($parents)) {
1434 foreach ($parents as $i => $_x) {
1435 $parents[$i]['children'] = get_item_children($arr, $_x);
1439 /// @TODO Old-lost code?
1440 /*foreach ($arr as $x) {
1441 if ($x['id'] != $x['parent']) {
1442 $p = find_thread_parent_index($parents,$x);
1444 $parents[$p]['children'][] = $x;
1447 if (count($parents)) {
1448 foreach ($parents as $k => $v) {
1449 if (count($parents[$k]['children'])) {
1450 $parents[$k]['children'] = sort_item_children($parents[$k]['children']);
1451 /// @TODO Old-lost code?
1452 /*$y = $parents[$k]['children'];
1453 usort($y,'sort_thr_created_rev');
1454 $parents[$k]['children'] = $y;*/
1460 if (count($parents)) {
1461 foreach ($parents as $x) {
1463 if (count($x['children'])) {
1464 add_children_to_list($x['children'], $ret);
1465 /// @TODO Old-lost code?
1466 /*foreach ($x['children'] as $y)
1475 /// @TODO Add type-hint
1476 function sort_thr_created($a, $b) {
1477 return strcmp($b['created'], $a['created']);
1480 /// @TODO Add type-hint
1481 function sort_thr_created_rev($a, $b) {
1482 return strcmp($a['created'], $b['created']);
1485 /// @TODO Add type-hint
1486 function sort_thr_commented($a, $b) {
1487 return strcmp($b['commented'], $a['commented']);
1490 /// @TODO Add type-hint
1491 function find_thread_parent_index($arr, $x) {
1492 foreach ($arr as $k => $v) {
1493 if ($v['id'] == $x['parent']) {
1500 /// @TODO Add type-hint
1501 function render_location_dummy($item) {
1502 if ($item['location'] != "") {
1503 return $item['location'];
1506 if ($item['coord'] != "") {
1507 return $item['coord'];
1511 /// @TODO Add type-hint
1512 function get_responses($conv_responses, $response_verbs, $ob, $item) {
1514 foreach ($response_verbs as $v) {
1516 $ret[$v]['count'] = ((x($conv_responses[$v], $item['uri'])) ? $conv_responses[$v][$item['uri']] : '');
1517 $ret[$v]['list'] = ((x($conv_responses[$v], $item['uri'])) ? $conv_responses[$v][$item['uri'] . '-l'] : '');
1518 $ret[$v]['self'] = ((x($conv_responses[$v], $item['uri'])) ? $conv_responses[$v][$item['uri'] . '-self'] : '0');
1519 if (count($ret[$v]['list']) > MAX_LIKERS) {
1520 $ret[$v]['list_part'] = array_slice($ret[$v]['list'], 0, MAX_LIKERS);
1521 array_push($ret[$v]['list_part'], '<a href="#" data-toggle="modal" data-target="#' . $v . 'Modal-'
1522 . (($ob) ? $ob->get_id() : $item['id']) . '"><b>' . t('View all') . '</b></a>');
1524 $ret[$v]['list_part'] = '';
1526 $ret[$v]['button'] = get_response_button_text($v, $ret[$v]['count']);
1527 $ret[$v]['title'] = $conv_responses[$v]['title'];
1531 foreach ($ret as $key) {
1532 if ($key['count'] == true) {
1536 $ret['count'] = $count;
1541 function get_response_button_text($v, $count) {
1544 return tt('Like', 'Likes', $count, 'noun');
1547 return tt('Dislike', 'Dislikes', $count, 'noun');
1550 return tt('Attending', 'Attending', $count, 'noun');
1553 return tt('Not Attending', 'Not Attending', $count, 'noun');
1556 return tt('Undecided', 'Undecided', $count, 'noun');