3 require_once("include/bbcode.php");
6 // Note: the code in 'item_extract_images' and 'item_redir_and_replace_images'
7 // is identical to the code in mod/message.php for 'item_extract_images' and
8 // 'item_redir_and_replace_images'
9 if(! function_exists('item_extract_images')) {
10 function item_extract_images($body) {
12 $saved_image = array();
17 $img_start = strpos($orig_body, '[img');
18 $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
19 $img_end = ($img_start !== false ? strpos(substr($orig_body, $img_start), '[/img]') : false);
20 while(($img_st_close !== false) && ($img_end !== false)) {
22 $img_st_close++; // make it point to AFTER the closing bracket
23 $img_end += $img_start;
25 if(! strcmp(substr($orig_body, $img_start + $img_st_close, 5), 'data:')) {
26 // This is an embedded image
28 $saved_image[$cnt] = substr($orig_body, $img_start + $img_st_close, $img_end - ($img_start + $img_st_close));
29 $new_body = $new_body . substr($orig_body, 0, $img_start) . '[!#saved_image' . $cnt . '#!]';
34 $new_body = $new_body . substr($orig_body, 0, $img_end + strlen('[/img]'));
36 $orig_body = substr($orig_body, $img_end + strlen('[/img]'));
38 if($orig_body === false) // in case the body ends on a closing image tag
41 $img_start = strpos($orig_body, '[img');
42 $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
43 $img_end = ($img_start !== false ? strpos(substr($orig_body, $img_start), '[/img]') : false);
46 $new_body = $new_body . $orig_body;
48 return array('body' => $new_body, 'images' => $saved_image);
51 if(! function_exists('item_redir_and_replace_images')) {
52 function item_redir_and_replace_images($body, $images, $cid) {
58 $pos = get_bb_tag_pos($origbody, 'url', 1);
59 while($pos !== false && $cnt < 1000) {
61 $search = '/\[url\=(.*?)\]\[!#saved_image([0-9]*)#!\]\[\/url\]' . '/is';
62 $replace = '[url=' . z_path() . '/redir/' . $cid
63 . '?f=1&url=' . '$1' . '][!#saved_image' . '$2' .'#!][/url]';
65 $newbody .= substr($origbody, 0, $pos['start']['open']);
66 $subject = substr($origbody, $pos['start']['open'], $pos['end']['close'] - $pos['start']['open']);
67 $origbody = substr($origbody, $pos['end']['close']);
68 if($origbody === false)
71 $subject = preg_replace($search, $replace, $subject);
75 $pos = get_bb_tag_pos($origbody, 'url', 1);
77 $newbody .= $origbody;
80 foreach($images as $image) {
81 // We're depending on the property of 'foreach' (specified on the PHP website) that
82 // it loops over the array starting from the first element and going sequentially
83 // to the last element
84 $newbody = str_replace('[!#saved_image' . $cnt . '#!]', '[img]' . $image . '[/img]', $newbody);
93 * Render actions localized
95 function localize_item(&$item){
97 $extracted = item_extract_images($item['body']);
98 if($extracted['images'])
99 $item['body'] = item_redir_and_replace_images($extracted['body'], $extracted['images'], $item['contact-id']);
101 $xmlhead="<"."?xml version='1.0' encoding='UTF-8' ?".">";
102 if (activity_match($item['verb'],ACTIVITY_LIKE) || activity_match($item['verb'],ACTIVITY_DISLIKE)){
104 $r = q("SELECT * from `item`,`contact` WHERE
105 `item`.`contact-id`=`contact`.`id` AND `item`.`uri`='%s';",
106 dbesc($item['parent-uri']));
107 if(count($r)==0) return;
110 $author = '[url=' . $item['author-link'] . ']' . $item['author-name'] . '[/url]';
111 $objauthor = '[url=' . $obj['author-link'] . ']' . $obj['author-name'] . '[/url]';
113 switch($obj['verb']){
115 switch ($obj['object-type']){
116 case ACTIVITY_OBJ_EVENT:
117 $post_type = t('event');
120 $post_type = t('status');
124 if($obj['resource-id']){
125 $post_type = t('photo');
126 $m=array(); preg_match("/\[url=([^]]*)\]/", $obj['body'], $m);
127 $rr['plink'] = $m[1];
129 $post_type = t('status');
133 $plink = '[url=' . $obj['plink'] . ']' . $post_type . '[/url]';
135 if(activity_match($item['verb'],ACTIVITY_LIKE)) {
136 $bodyverb = t('%1$s likes %2$s\'s %3$s');
138 elseif(activity_match($item['verb'],ACTIVITY_DISLIKE)) {
139 $bodyverb = t('%1$s doesn\'t like %2$s\'s %3$s');
141 $item['body'] = sprintf($bodyverb, $author, $objauthor, $plink);
144 if (activity_match($item['verb'],ACTIVITY_FRIEND)) {
146 if ($item['object-type']=="" || $item['object-type']!== ACTIVITY_OBJ_PERSON) return;
148 $Aname = $item['author-name'];
149 $Alink = $item['author-link'];
151 $xmlhead="<"."?xml version='1.0' encoding='UTF-8' ?".">";
153 $obj = parse_xml_string($xmlhead.$item['object']);
154 $links = parse_xml_string($xmlhead."<links>".unxmlify($obj->link)."</links>");
156 $Bname = $obj->title;
157 $Blink = ""; $Bphoto = "";
158 foreach ($links->link as $l){
159 $atts = $l->attributes();
160 switch($atts['rel']){
161 case "alternate": $Blink = $atts['href'];
162 case "photo": $Bphoto = $atts['href'];
167 $A = '[url=' . zrl($Alink) . ']' . $Aname . '[/url]';
168 $B = '[url=' . zrl($Blink) . ']' . $Bname . '[/url]';
169 if ($Bphoto!="") $Bphoto = '[url=' . zrl($Blink) . '][img]' . $Bphoto . '[/img][/url]';
171 $item['body'] = sprintf( t('%1$s is now friends with %2$s'), $A, $B)."\n\n\n".$Bphoto;
174 if (stristr($item['verb'],ACTIVITY_POKE)) {
175 $verb = urldecode(substr($item['verb'],strpos($item['verb'],'#')+1));
178 if ($item['object-type']=="" || $item['object-type']!== ACTIVITY_OBJ_PERSON) return;
180 $Aname = $item['author-name'];
181 $Alink = $item['author-link'];
183 $xmlhead="<"."?xml version='1.0' encoding='UTF-8' ?".">";
185 $obj = parse_xml_string($xmlhead.$item['object']);
186 $links = parse_xml_string($xmlhead."<links>".unxmlify($obj->link)."</links>");
188 $Bname = $obj->title;
189 $Blink = ""; $Bphoto = "";
190 foreach ($links->link as $l){
191 $atts = $l->attributes();
192 switch($atts['rel']){
193 case "alternate": $Blink = $atts['href'];
194 case "photo": $Bphoto = $atts['href'];
199 $A = '[url=' . zrl($Alink) . ']' . $Aname . '[/url]';
200 $B = '[url=' . zrl($Blink) . ']' . $Bname . '[/url]';
201 if ($Bphoto!="") $Bphoto = '[url=' . zrl($Blink) . '][img=80x80]' . $Bphoto . '[/img][/url]';
203 // we can't have a translation string with three positions but no distinguishable text
204 // So here is the translate string.
206 $txt = t('%1$s poked %2$s');
208 // now translate the verb
210 $txt = str_replace( t('poked'), t($verb), $txt);
212 // then do the sprintf on the translation string
214 $item['body'] = sprintf($txt, $A, $B). "\n\n\n" . $Bphoto;
217 if (stristr($item['verb'],ACTIVITY_MOOD)) {
218 $verb = urldecode(substr($item['verb'],strpos($item['verb'],'#')+1));
222 $Aname = $item['author-name'];
223 $Alink = $item['author-link'];
224 $A = '[url=' . zrl($Alink) . ']' . $Aname . '[/url]';
226 $txt = t('%1$s is currently %2$s');
228 $item['body'] = sprintf($txt, $A, t($verb));
231 if (activity_match($item['verb'],ACTIVITY_TAG)) {
232 $r = q("SELECT * from `item`,`contact` WHERE
233 `item`.`contact-id`=`contact`.`id` AND `item`.`uri`='%s';",
234 dbesc($item['parent-uri']));
235 if(count($r)==0) return;
238 $author = '[url=' . zrl($item['author-link']) . ']' . $item['author-name'] . '[/url]';
239 $objauthor = '[url=' . zrl($obj['author-link']) . ']' . $obj['author-name'] . '[/url]';
241 switch($obj['verb']){
243 switch ($obj['object-type']){
244 case ACTIVITY_OBJ_EVENT:
245 $post_type = t('event');
248 $post_type = t('status');
252 if($obj['resource-id']){
253 $post_type = t('photo');
254 $m=array(); preg_match("/\[url=([^]]*)\]/", $obj['body'], $m);
255 $rr['plink'] = $m[1];
257 $post_type = t('status');
260 $plink = '[url=' . $obj['plink'] . ']' . $post_type . '[/url]';
262 $parsedobj = parse_xml_string($xmlhead.$item['object']);
264 $tag = sprintf('#[url=%s]%s[/url]', $parsedobj->id, $parsedobj->content);
265 $item['body'] = sprintf( t('%1$s tagged %2$s\'s %3$s with %4$s'), $author, $objauthor, $plink, $tag );
268 if (activity_match($item['verb'],ACTIVITY_FAVORITE)){
270 if ($item['object-type']== "")
273 $Aname = $item['author-name'];
274 $Alink = $item['author-link'];
276 $xmlhead="<"."?xml version='1.0' encoding='UTF-8' ?".">";
278 $obj = parse_xml_string($xmlhead.$item['object']);
279 if(strlen($obj->id)) {
280 $r = q("select * from item where uri = '%s' and uid = %d limit 1",
284 if(count($r) && $r[0]['plink']) {
286 $Bname = $target['author-name'];
287 $Blink = $target['author-link'];
288 $A = '[url=' . zrl($Alink) . ']' . $Aname . '[/url]';
289 $B = '[url=' . zrl($Blink) . ']' . $Bname . '[/url]';
290 $P = '[url=' . $target['plink'] . ']' . t('post/item') . '[/url]';
291 $item['body'] = sprintf( t('%1$s marked %2$s\'s %3$s as favorite'), $A, $B, $P)."\n";
297 if(preg_match_all('/@\[url=(.*?)\]/is',$item['body'],$matches,PREG_SET_ORDER)) {
298 foreach($matches as $mtch) {
299 if(! strpos($mtch[1],'zrl='))
300 $item['body'] = str_replace($mtch[0],'@[url=' . zrl($mtch[1]). ']',$item['body']);
304 // add zrl's to public images
305 $photo_pattern = "/\[url=(.*?)\/photos\/(.*?)\/image\/(.*?)\]\[img(.*?)\]h(.*?)\[\/img\]\[\/url\]/is";
306 if(preg_match($photo_pattern,$item['body'])) {
307 $photo_replace = '[url=' . zrl('$1' . '/photos/' . '$2' . '/image/' . '$3' ,true) . '][img' . '$4' . ']h' . '$5' . '[/img][/url]';
308 $item['body'] = bb_tag_preg_replace($photo_pattern, $photo_replace, 'url', $item['body']);
311 // add sparkle links to appropriate permalinks
313 $x = stristr($item['plink'],'/display/');
316 $y = best_link_url($item,$sparkle,true);
317 if(strstr($y,'/redir/'))
318 $item['plink'] = $y . '?f=&url=' . $item['plink'];
326 * Count the total of comments on this item and its desendants
328 function count_descendants($item) {
329 $total = count($item['children']);
332 foreach($item['children'] as $child) {
333 if(! visible_activity($child))
335 $total += count_descendants($child);
342 function visible_activity($item) {
344 if(activity_match($child['verb'],ACTIVITY_LIKE) || activity_match($child['verb'],ACTIVITY_DISLIKE))
347 if(activity_match($item['verb'],ACTIVITY_FOLLOW) && $item['object-type'] === ACTIVITY_OBJ_NOTE && $item['uid'] != local_user())
355 * Recursively prepare a thread for HTML
358 function prepare_threads_body($a, $items, $cmnt_tpl, $page_writeable, $mode, $profile_owner, $alike, $dlike, $previewing, $thread_level=1) {
361 $wall_template = 'wall_thread.tpl';
362 $wallwall_template = 'wallwall_thread.tpl';
364 $nb_items = count($items);
366 $total_children = $nb_items;
368 foreach($items as $item) {
369 if($item['network'] === NETWORK_MAIL && local_user() != $item['uid']) {
370 // Don't count it as a visible item
374 if(! visible_activity($item)) {
380 foreach($items as $item) {
381 // prevent private email reply to public conversation from leaking.
382 if($item['network'] === NETWORK_MAIL && local_user() != $item['uid']) {
386 if(! visible_activity($item)) {
393 $template = $wall_template;
396 $owner_url = $owner_photo = $owner_name = '';
400 $isstarred = "unstarred";
401 $photo = $item['photo'];
402 $thumb = $item['thumb'];
406 $lastcollapsed = false;
407 $firstcollapsed = false;
408 $total_children += count_descendants($item);
410 $toplevelpost = (($item['id'] == $item['parent']) ? true : false);
413 if($item['uid'] == local_user())
415 elseif(is_array($_SESSION['remote'])) {
416 foreach($_SESSION['remote'] as $visitor) {
417 if($visitor['cid'] == $item['contact-id']) {
425 $item_writeable = (($item['writable'] || $item['self']) ? true : false);
427 // This will allow us to comment on wall-to-wall items owned by our friends
428 // and community forums even if somebody else wrote the post.
430 if($visiting && $mode == 'profile')
431 $item_writeable = true;
433 $show_comment_box = ((($page_writeable) && ($item_writeable)) ? true : false);
434 $lock = ((($item['private'] == 1) || (($item['uid'] == local_user()) && (strlen($item['allow_cid']) || strlen($item['allow_gid'])
435 || strlen($item['deny_cid']) || strlen($item['deny_gid']))))
436 ? t('Private Message')
438 $redirect_url = $a->get_baseurl($ssl_state) . '/redir/' . $item['cid'] ;
439 $shareable = ((($profile_owner == local_user()) && ($item['private'] != 1)) ? true : false);
440 if(local_user() && link_compare($a->contact['url'],$item['author-link']))
441 $edpost = array($a->get_baseurl($ssl_state)."/editpost/".$item['id'], t("Edit"));
446 'dropping' => $dropping,
447 'select' => t('Select'),
448 'delete' => t('Delete'),
451 $filer = (($profile_owner == local_user()) ? t("save to folder") : false);
453 $diff_author = ((link_compare($item['url'],$item['author-link'])) ? false : true);
454 $profile_name = (((strlen($item['author-name'])) && $diff_author) ? $item['author-name'] : $item['name']);
455 if($item['author-link'] && (! $item['author-name']))
456 $profile_name = $item['author-link'];
459 $profile_link = best_link_url($item,$sp);
460 if($profile_link === 'mailbox')
463 $sparkle = ' sparkle';
465 $profile_link = zrl($profile_link);
467 $normalised = normalise_link((strlen($item['author-link'])) ? $item['author-link'] : $item['url']);
468 if(($normalised != 'mailbox') && (x($a->contacts,$normalised)))
469 $profile_avatar = $a->contacts[$normalised]['thumb'];
471 $profile_avatar = (((strlen($item['author-avatar'])) && $diff_author) ? $item['author-avatar'] : $a->get_cached_avatar_image($thumb));
473 $locate = array('location' => $item['location'], 'coord' => $item['coord'], 'html' => '');
474 call_hooks('render_location',$locate);
475 $location = ((strlen($locate['html'])) ? $locate['html'] : render_location_google($locate));
480 foreach(explode(',',$item['tag']) as $tag){
493 $like = ((x($alike,$item['uri'])) ? format_like($alike[$item['uri']],$alike[$item['uri'] . '-l'],'like',$item['uri']) : '');
494 $dislike = ((x($dlike,$item['uri'])) ? format_like($dlike[$item['uri']],$dlike[$item['uri'] . '-l'],'dislike',$item['uri']) : '');
497 if((! $item['self']) && ($mode !== 'profile')) {
500 // On the network page, I am the owner. On the display page it will be the profile owner.
501 // This will have been stored in $a->page_contact by our calling page.
502 // Put this person as the wall owner of the wall-to-wall notice.
504 $owner_url = zrl($a->page_contact['url']);
505 $owner_photo = $a->page_contact['thumb'];
506 $owner_name = $a->page_contact['name'];
507 $template = $wallwall_template;
510 else if($item['owner-link']) {
512 $owner_linkmatch = (($item['owner-link']) && link_compare($item['owner-link'],$item['author-link']));
513 $alias_linkmatch = (($item['alias']) && link_compare($item['alias'],$item['author-link']));
514 $owner_namematch = (($item['owner-name']) && $item['owner-name'] == $item['author-name']);
515 if((! $owner_linkmatch) && (! $alias_linkmatch) && (! $owner_namematch)) {
517 // The author url doesn't match the owner (typically the contact)
518 // and also doesn't match the contact alias.
519 // The name match is a hack to catch several weird cases where URLs are
520 // all over the park. It can be tricked, but this prevents you from
521 // seeing "Bob Smith to Bob Smith via Wall-to-wall" and you know darn
522 // well that it's the same Bob Smith.
524 // But it could be somebody else with the same name. It just isn't highly likely.
527 $owner_url = $item['owner-link'];
528 $owner_photo = $item['owner-avatar'];
529 $owner_name = $item['owner-name'];
530 $template = $wallwall_template;
532 // If it is our contact, use a friendly redirect link
533 if((link_compare($item['owner-link'],$item['url']))
534 && ($item['network'] === NETWORK_DFRN)) {
535 $owner_url = $redirect_url;
536 $osparkle = ' sparkle';
539 $owner_url = zrl($owner_url);
543 if($profile_owner == local_user()) {
544 $isstarred = (($item['starred']) ? "starred" : "unstarred");
547 'do' => t("add star"),
548 'undo' => t("remove star"),
549 'toggle' => t("toggle star status"),
550 'classdo' => (($item['starred']) ? "hidden" : ""),
551 'classundo' => (($item['starred']) ? "" : "hidden"),
552 'starred' => t('starred'),
553 'tagger' => t("add tag"),
560 if(($nb_items > 2) || ($thread_level > 2)) {
561 if($items_seen == 1) {
562 $firstcollapsed = true;
564 if($thread_level > 2) {
565 if($items_seen == $nb_items)
566 $lastcollapsed = true;
568 else if($items_seen == ($nb_items - 2)) {
569 $lastcollapsed = true;
574 if(intval(get_config('system','thread_allow')) && $a->theme_thread_allow) {
575 $comments_threaded = true;
578 $comments_threaded = false;
581 if($page_writeable) {
583 'like' => array( t("I like this \x28toggle\x29"), t("like")),
584 'dislike' => array( t("I don't like this \x28toggle\x29"), t("dislike")),
586 if ($shareable) $buttons['share'] = array( t('Share this'), t('share'));
589 if($show_comment_box) {
590 $qc = $qcomment = null;
592 if(in_array('qcomment',$a->plugins)) {
593 $qc = ((local_user()) ? get_pconfig(local_user(),'qcomment','words') : null);
594 $qcomment = (($qc) ? explode("\n",$qc) : null);
596 $comment = replace_macros($cmnt_tpl,array(
597 '$return_path' => '',
598 '$threaded' => $comments_threaded,
599 '$jsreload' => (($mode === 'display') ? $_SESSION['return_url'] : ''),
600 '$type' => (($mode === 'profile') ? 'wall-comment' : 'net-comment'),
601 '$id' => $item['item_id'],
602 '$parent' => $item['item_id'],
603 '$qcomment' => $qcomment,
604 '$profile_uid' => $profile_owner,
605 '$mylink' => $a->contact['url'],
606 '$mytitle' => t('This is you'),
607 '$myphoto' => $a->contact['thumb'],
608 '$comment' => t('Comment'),
609 '$submit' => t('Submit'),
610 '$edbold' => t('Bold'),
611 '$editalic' => t('Italic'),
612 '$eduline' => t('Underline'),
613 '$edquote' => t('Quote'),
614 '$edcode' => t('Code'),
615 '$edimg' => t('Image'),
616 '$edurl' => t('Link'),
617 '$edvideo' => t('Video'),
618 '$preview' => t('Preview'),
619 '$indent' => $indent,
620 '$sourceapp' => t($a->sourcename),
621 '$ww' => (($mode === 'network') ? $commentww : '')
626 if(strcmp(datetime_convert('UTC','UTC',$item['created']),datetime_convert('UTC','UTC','now - 12 hours')) > 0)
629 localize_item($item);
631 $body = prepare_body($item,true);
633 list($categories, $folders) = get_cats_and_terms($item);
638 // collapse comments in template. I don't like this much...
639 'comment_firstcollapsed' => $firstcollapsed,
640 'comment_lastcollapsed' => $lastcollapsed,
641 // template to use to render item (wall, walltowall, search)
642 'template' => $template,
644 'type' => implode("",array_slice(explode("/",$item['verb']),-1)),
645 'tags' => template_escape($tags),
646 'hashtags' => template_escape($hashtags),
647 'mentions' => template_escape($mentions),
648 'categories' => $categories,
649 'folders' => $folders,
650 'body' => template_escape($body),
651 'text' => strip_tags(template_escape($body)),
652 'id' => $item['item_id'],
653 'linktitle' => sprintf( t('View %s\'s profile @ %s'), $profile_name, ((strlen($item['author-link'])) ? $item['author-link'] : $item['url'])),
654 'olinktitle' => sprintf( t('View %s\'s profile @ %s'), $owner_name, ((strlen($item['owner-link'])) ? $item['owner-link'] : $item['url'])),
656 'wall' => t('Wall-to-Wall'),
657 'vwall' => t('via Wall-To-Wall:'),
658 'profile_url' => $profile_link,
659 'item_photo_menu' => item_photo_menu($item),
660 'name' => template_escape($profile_name),
661 'thumb' => $profile_avatar,
662 'osparkle' => $osparkle,
663 'sparkle' => $sparkle,
664 'title' => template_escape($item['title']),
665 'localtime' => datetime_convert('UTC', date_default_timezone_get(), $item['created'], 'r'),
667 'ago' => (($item['app']) ? sprintf( t('%s from %s'),relative_date($item['created']),$item['app']) : relative_date($item['created'])),
669 'location' => template_escape($location),
671 'owner_url' => $owner_url,
672 'owner_photo' => $owner_photo,
673 'owner_name' => template_escape($owner_name),
674 'plink' => get_plink($item),
676 'isstarred' => $isstarred,
682 'dislike' => $dislike,
683 'comment' => $comment,
684 'previewing' => $previewing,
685 'wait' => t('Please wait'),
686 'thread_level' => $thread_level,
689 $arr = array('item' => $item, 'output' => $tmp_item);
690 call_hooks('display_item', $arr);
692 $item_result = $arr['output'];
693 if($firstcollapsed) {
694 $item_result['num_comments'] = sprintf( tt('%d comment','%d comments',$total_children),$total_children );
695 $item_result['hidden_comments_num'] = $total_children;
696 $item_result['hidden_comments_text'] = tt('comment', 'comments', $total_children);
697 $item_result['hide_text'] = t('show more');
700 $item_result['children'] = array();
701 if(count($item['children'])) {
702 $item_result['children'] = prepare_threads_body($a, $item['children'], $cmnt_tpl, $page_writeable, $mode, $profile_owner, $alike, $dlike, $previewing, ($thread_level + 1));
704 $item_result['private'] = $item['private'];
705 $item_result['toplevel'] = ($toplevelpost ? 'toplevel_item' : '');
708 * I don't like this very much...
710 if(get_config('system','thread_allow') && $a->theme_thread_allow) {
711 $item_result['flatten'] = false;
712 $item_result['threaded'] = true;
715 $item_result['flatten'] = true;
716 $item_result['threaded'] = false;
718 $item_result['comment'] = false;
722 $result[] = $item_result;
729 * "Render" a conversation or list of items for HTML display.
730 * There are two major forms of display:
731 * - Sequential or unthreaded ("New Item View" or search results)
732 * - conversation view
733 * The $mode parameter decides between the various renderings and also
734 * figures out how to determine page owner and other contextual items
735 * that are based on unique features of the calling module.
739 if(!function_exists('conversation')) {
740 function conversation(&$a, $items, $mode, $update, $preview = false) {
743 require_once('bbcode.php');
745 $ssl_state = ((local_user()) ? true : false);
748 $page_writeable = false;
750 $previewing = (($preview) ? ' preview ' : '');
752 if($mode === 'network') {
753 $profile_owner = local_user();
754 $page_writeable = true;
757 if($mode === 'profile') {
758 $profile_owner = $a->profile['profile_uid'];
759 $page_writeable = can_write_wall($a,$profile_owner);
762 if($mode === 'notes') {
763 $profile_owner = local_user();
764 $page_writeable = true;
767 if($mode === 'display') {
768 $profile_owner = $a->profile['uid'];
769 $page_writeable = can_write_wall($a,$profile_owner);
772 if($mode === 'community') {
774 $page_writeable = false;
777 $page_dropping = ((local_user() && local_user() == $profile_owner) ? true : false);
781 $return_url = $_SESSION['return_url'];
783 $return_url = $_SESSION['return_url'] = $a->query_string;
785 load_contact_links(local_user());
787 $cb = array('items' => $items, 'mode' => $mode, 'update' => $update, 'preview' => $preview);
788 call_hooks('conversation_start',$cb);
790 $items = $cb['items'];
792 $cmnt_tpl = get_markup_template('comment_item.tpl');
793 $tpl = 'wall_item.tpl';
794 $wallwall = 'wallwall_item.tpl';
795 $hide_comments_tpl = get_markup_template('hide_comments.tpl');
801 // array with html for each thread (parent+comments)
805 $page_template = get_markup_template("conversation.tpl");
807 if($items && count($items)) {
809 if($mode === 'network-new' || $mode === 'search' || $mode === 'community') {
811 // "New Item View" on network page or search page results
812 // - just loop through the items and format them minimally for display
814 //$tpl = get_markup_template('search_item.tpl');
815 $tpl = 'search_item.tpl';
817 foreach($items as $item) {
826 if($mode === 'search' || $mode === 'community') {
827 if(((activity_match($item['verb'],ACTIVITY_LIKE)) || (activity_match($item['verb'],ACTIVITY_DISLIKE)))
828 && ($item['id'] != $item['parent']))
830 $nickname = $item['nickname'];
833 $nickname = $a->user['nickname'];
835 // prevent private email from leaking.
836 if($item['network'] === NETWORK_MAIL && local_user() != $item['uid'])
839 $profile_name = ((strlen($item['author-name'])) ? $item['author-name'] : $item['name']);
840 if($item['author-link'] && (! $item['author-name']))
841 $profile_name = $item['author-link'];
848 foreach(explode(',',$item['tag']) as $tag){
861 $profile_link = best_link_url($item,$sp);
862 if($profile_link === 'mailbox')
865 $sparkle = ' sparkle';
867 $profile_link = zrl($profile_link);
869 $normalised = normalise_link((strlen($item['author-link'])) ? $item['author-link'] : $item['url']);
870 if(($normalised != 'mailbox') && (x($a->contacts[$normalised])))
871 $profile_avatar = $a->contacts[$normalised]['thumb'];
873 $profile_avatar = ((strlen($item['author-avatar'])) ? $a->get_cached_avatar_image($item['author-avatar']) : $item['thumb']);
875 $locate = array('location' => $item['location'], 'coord' => $item['coord'], 'html' => '');
876 call_hooks('render_location',$locate);
878 $location = ((strlen($locate['html'])) ? $locate['html'] : render_location_google($locate));
880 localize_item($item);
881 if($mode === 'network-new')
888 'dropping' => $dropping,
889 'select' => t('Select'),
890 'delete' => t('Delete'),
894 $isstarred = "unstarred";
897 $likebuttons = false;
900 $body = prepare_body($item,true);
902 list($categories, $folders) = get_cats_and_terms($item);
903 //$tmp_item = replace_macros($tpl,array(
906 'id' => (($preview) ? 'P0' : $item['item_id']),
907 'linktitle' => sprintf( t('View %s\'s profile @ %s'), $profile_name, ((strlen($item['author-link'])) ? $item['author-link'] : $item['url'])),
908 'profile_url' => $profile_link,
909 'item_photo_menu' => item_photo_menu($item),
910 'name' => template_escape($profile_name),
911 'sparkle' => $sparkle,
913 'thumb' => $profile_avatar,
914 'title' => template_escape($item['title']),
915 'body' => template_escape($body),
916 'tags' => template_escape($tags),
917 'hashtags' => template_escape($hashtags),
918 'mentions' => template_escape($mentions),
919 'categories' => $categories,
920 'folders' => $folders,
921 'text' => strip_tags(template_escape($body)),
922 'localtime' => datetime_convert('UTC', date_default_timezone_get(), $item['created'], 'r'),
923 'ago' => (($item['app']) ? sprintf( t('%s from %s'),relative_date($item['created']),$item['app']) : relative_date($item['created'])),
924 'location' => template_escape($location),
926 'owner_name' => template_escape($owner_name),
927 'owner_url' => $owner_url,
928 'owner_photo' => $owner_photo,
929 'plink' => get_plink($item),
931 'isstarred' => $isstarred,
934 'vote' => $likebuttons,
938 'conv' => (($preview) ? '' : array('href'=> $a->get_baseurl($ssl_state) . '/display/' . $nickname . '/' . $item['id'], 'title'=> t('View in context'))),
939 'previewing' => $previewing,
940 'wait' => t('Please wait'),
944 $arr = array('item' => $item, 'output' => $tmp_item);
945 call_hooks('display_item', $arr);
947 $threads[$threadsid]['id'] = $item['item_id'];
948 $threads[$threadsid]['items'] = array($arr['output']);
956 $page_template = get_markup_template("threaded_conversation.tpl");
958 require_once('object/Conversation.php');
959 require_once('object/Item.php');
961 $conv = new Conversation($mode, $preview);
963 // get all the topmost parents
964 // this shouldn't be needed, as we should have only them in our array
965 // But for now, this array respects the old style, just in case
968 foreach($items as $item) {
970 // Can we put this after the visibility check?
971 like_puller($a,$item,$alike,'like');
972 like_puller($a,$item,$dlike,'dislike');
974 // Only add what is visible
975 if($item['network'] === NETWORK_MAIL && local_user() != $item['uid']) {
978 if(! visible_activity($item)) {
982 if($item['id'] == $item['parent']) {
983 $item_object = new Item($item);
984 $conv->add_thread($item_object);
988 $threads = $conv->get_template_data($alike, $dlike);
990 logger('[ERROR] conversation : Failed to get template data.', LOGGER_DEBUG);
996 $o = replace_macros($page_template, array(
997 '$baseurl' => $a->get_baseurl($ssl_state),
998 '$remove' => t('remove'),
1000 '$user' => $a->user,
1001 '$threads' => $threads,
1002 '$dropping' => ($page_dropping?t('Delete Selected Items'):False),
1008 function best_link_url($item,&$sparkle,$ssl_state = false) {
1015 $clean_url = normalise_link($item['author-link']);
1017 if((local_user()) && (local_user() == $item['uid'])) {
1018 if(isset($a->contacts) && x($a->contacts,$clean_url)) {
1019 if($a->contacts[$clean_url]['network'] === NETWORK_DFRN) {
1020 $best_url = $a->get_baseurl($ssl_state) . '/redir/' . $a->contacts[$clean_url]['id'];
1024 $best_url = $a->contacts[$clean_url]['url'];
1028 if(strlen($item['author-link']))
1029 $best_url = $item['author-link'];
1031 $best_url = $item['url'];
1038 if(! function_exists('item_photo_menu')){
1039 function item_photo_menu($item){
1046 if(! count($a->contacts))
1047 load_contact_links(local_user());
1057 $profile_link = best_link_url($item,$sparkle,$ssl_state);
1058 if($profile_link === 'mailbox')
1062 $cid = intval(basename($profile_link));
1063 $status_link = $profile_link . "?url=status";
1064 $photos_link = $profile_link . "?url=photos";
1065 $profile_link = $profile_link . "?url=profile";
1066 $pm_url = $a->get_baseurl($ssl_state) . '/message/new/' . $cid;
1070 $profile_link = zrl($profile_link);
1071 if(local_user() && local_user() == $item['uid'] && link_compare($item['url'],$item['author-link'])) {
1072 $cid = $item['contact-id'];
1078 if(($cid) && (! $item['self'])) {
1079 $poke_link = $a->get_baseurl($ssl_state) . '/poke/?f=&c=' . $cid;
1080 $contact_url = $a->get_baseurl($ssl_state) . '/contacts/' . $cid;
1081 $posts_link = $a->get_baseurl($ssl_state) . '/network/?cid=' . $cid;
1083 $clean_url = normalise_link($item['author-link']);
1085 if((local_user()) && (local_user() == $item['uid'])) {
1086 if(isset($a->contacts) && x($a->contacts,$clean_url)) {
1087 if($a->contacts[$clean_url]['network'] === NETWORK_DIASPORA) {
1088 $pm_url = $a->get_baseurl($ssl_state) . '/message/new/' . $cid;
1096 t("View Status") => $status_link,
1097 t("View Profile") => $profile_link,
1098 t("View Photos") => $photos_link,
1099 t("Network Posts") => $posts_link,
1100 t("Edit Contact") => $contact_url,
1101 t("Send PM") => $pm_url,
1102 t("Poke") => $poke_link
1106 $args = array('item' => $item, 'menu' => $menu);
1108 call_hooks('item_photo_menu', $args);
1110 $menu = $args['menu'];
1113 foreach($menu as $k=>$v){
1114 if ($v!="") $o .= "<li><a href=\"$v\">$k</a></li>\n";
1119 if(! function_exists('like_puller')) {
1120 function like_puller($a,$item,&$arr,$mode) {
1124 $verb = (($mode === 'like') ? ACTIVITY_LIKE : ACTIVITY_DISLIKE);
1126 if((activity_match($item['verb'],$verb)) && ($item['id'] != $item['parent'])) {
1127 $url = $item['author-link'];
1128 if((local_user()) && (local_user() == $item['uid']) && ($item['network'] === 'dfrn') && (! $item['self']) && (link_compare($item['author-link'],$item['url']))) {
1129 $url = $a->get_baseurl(true) . '/redir/' . $item['contact-id'];
1130 $sparkle = ' class="sparkle" ';
1135 if(! $item['thr-parent'])
1136 $item['thr-parent'] = $item['parent-uri'];
1138 if(! ((isset($arr[$item['thr-parent'] . '-l'])) && (is_array($arr[$item['thr-parent'] . '-l']))))
1139 $arr[$item['thr-parent'] . '-l'] = array();
1140 if(! isset($arr[$item['thr-parent']]))
1141 $arr[$item['thr-parent']] = 1;
1143 $arr[$item['thr-parent']] ++;
1144 $arr[$item['thr-parent'] . '-l'][] = '<a href="'. $url . '"'. $sparkle .'>' . $item['author-name'] . '</a>';
1149 // Format the like/dislike text for a profile item
1150 // $cnt = number of people who like/dislike the item
1151 // $arr = array of pre-linked names of likers/dislikers
1152 // $type = one of 'like, 'dislike'
1154 // returns formatted text
1156 if(! function_exists('format_like')) {
1157 function format_like($cnt,$arr,$type,$id) {
1160 $o .= (($type === 'like') ? sprintf( t('%s likes this.'), $arr[0]) : sprintf( t('%s doesn\'t like this.'), $arr[0])) . EOL ;
1162 $spanatts = 'class="fakelink" onclick="openClose(\'' . $type . 'list-' . $id . '\');"';
1163 $o .= (($type === 'like') ?
1164 sprintf( t('<span %1$s>%2$d people</span> like this.'), $spanatts, $cnt)
1166 sprintf( t('<span %1$s>%2$d people</span> don\'t like this.'), $spanatts, $cnt) );
1168 $total = count($arr);
1169 if($total >= MAX_LIKERS)
1170 $arr = array_slice($arr, 0, MAX_LIKERS - 1);
1171 if($total < MAX_LIKERS)
1172 $arr[count($arr)-1] = t('and') . ' ' . $arr[count($arr)-1];
1173 $str = implode(', ', $arr);
1174 if($total >= MAX_LIKERS)
1175 $str .= sprintf( t(', and %d other people'), $total - MAX_LIKERS );
1176 $str = (($type === 'like') ? sprintf( t('%s like this.'), $str) : sprintf( t('%s don\'t like this.'), $str));
1177 $o .= "\t" . '<div id="' . $type . 'list-' . $id . '" style="display: none;" >' . $str . '</div>';
1183 function status_editor($a,$x, $notes_cid = 0, $popup=false) {
1187 $geotag = (($x['allow_location']) ? get_markup_template('jot_geotag.tpl') : '');
1190 if(local_user() && intval(get_pconfig(local_user(),'system','plaintext')))
1193 $tpl = get_markup_template('jot-header.tpl');
1194 $a->page['htmlhead'] .= replace_macros($tpl, array(
1195 '$newpost' => 'true',
1196 '$baseurl' => $a->get_baseurl(true),
1197 '$editselect' => (($plaintext) ? 'none' : '/(profile-jot-text|prvmail-text)/'),
1198 '$geotag' => $geotag,
1199 '$nickname' => $x['nickname'],
1200 '$ispublic' => t('Visible to <strong>everybody</strong>'),
1201 '$linkurl' => t('Please enter a link URL:'),
1202 '$vidurl' => t("Please enter a video link/URL:"),
1203 '$audurl' => t("Please enter an audio link/URL:"),
1204 '$term' => t('Tag term:'),
1205 '$fileas' => t('Save to Folder:'),
1206 '$whereareu' => t('Where are you right now?')
1210 $tpl = get_markup_template('jot-end.tpl');
1211 $a->page['end'] .= replace_macros($tpl, array(
1212 '$newpost' => 'true',
1213 '$baseurl' => $a->get_baseurl(true),
1214 '$editselect' => (($plaintext) ? 'none' : '/(profile-jot-text|prvmail-text)/'),
1215 '$geotag' => $geotag,
1216 '$nickname' => $x['nickname'],
1217 '$ispublic' => t('Visible to <strong>everybody</strong>'),
1218 '$linkurl' => t('Please enter a link URL:'),
1219 '$vidurl' => t("Please enter a video link/URL:"),
1220 '$audurl' => t("Please enter an audio link/URL:"),
1221 '$term' => t('Tag term:'),
1222 '$fileas' => t('Save to Folder:'),
1223 '$whereareu' => t('Where are you right now?')
1227 $tpl = get_markup_template("jot.tpl");
1232 $mail_disabled = ((function_exists('imap_open') && (! get_config('system','imap_disabled'))) ? 0 : 1);
1234 $mail_enabled = false;
1235 $pubmail_enabled = false;
1237 if(($x['is_owner']) && (! $mail_disabled)) {
1238 $r = q("SELECT * FROM `mailacct` WHERE `uid` = %d AND `server` != '' LIMIT 1",
1239 intval(local_user())
1242 $mail_enabled = true;
1243 if(intval($r[0]['pubmail']))
1244 $pubmail_enabled = true;
1249 $selected = (($pubmail_enabled) ? ' checked="checked" ' : '');
1250 $jotnets .= '<div class="profile-jot-net"><input type="checkbox" name="pubmail_enable"' . $selected . ' value="1" /> ' . t("Post to Email") . '</div>';
1253 call_hooks('jot_tool', $jotplugins);
1254 call_hooks('jot_networks', $jotnets);
1257 $jotnets .= '<input type="hidden" name="contact_allow[]" value="' . $notes_cid .'" />';
1259 $tpl = replace_macros($tpl,array('$jotplugins' => $jotplugins));
1261 $o .= replace_macros($tpl,array(
1262 '$return_path' => $a->query_string,
1263 '$action' => $a->get_baseurl(true) . '/item',
1264 '$share' => (x($x,'button') ? $x['button'] : t('Share')),
1265 '$upload' => t('Upload photo'),
1266 '$shortupload' => t('upload photo'),
1267 '$attach' => t('Attach file'),
1268 '$shortattach' => t('attach file'),
1269 '$weblink' => t('Insert web link'),
1270 '$shortweblink' => t('web link'),
1271 '$video' => t('Insert video link'),
1272 '$shortvideo' => t('video link'),
1273 '$audio' => t('Insert audio link'),
1274 '$shortaudio' => t('audio link'),
1275 '$setloc' => t('Set your location'),
1276 '$shortsetloc' => t('set location'),
1277 '$noloc' => t('Clear browser location'),
1278 '$shortnoloc' => t('clear location'),
1280 '$placeholdertitle' => t('Set title'),
1282 '$placeholdercategory' => t('Categories (comma-separated list)'),
1283 '$wait' => t('Please wait'),
1284 '$permset' => t('Permission settings'),
1285 '$shortpermset' => t('permissions'),
1286 '$ptyp' => (($notes_cid) ? 'note' : 'wall'),
1289 '$baseurl' => $a->get_baseurl(true),
1290 '$defloc' => $x['default_location'],
1291 '$visitor' => $x['visitor'],
1292 '$pvisit' => (($notes_cid) ? 'none' : $x['visitor']),
1293 '$emailcc' => t('CC: email addresses'),
1294 '$public' => t('Public post'),
1295 '$jotnets' => $jotnets,
1296 '$emtitle' => t('Example: bob@example.com, mary@example.com'),
1297 '$lockstate' => $x['lockstate'],
1298 '$acl' => $x['acl'],
1299 '$bang' => $x['bang'],
1300 '$profile_uid' => $x['profile_uid'],
1301 '$preview' => t('Preview'),
1302 '$sourceapp' => t($a->sourcename),
1303 '$cancel' => t('Cancel')
1308 $o = '<div id="jot-popup" style="display: none;">'.$o.'</div>';
1316 function get_item_children($arr, $parent) {
1317 $children = array();
1318 foreach($arr as $item) {
1319 if($item['id'] != $item['parent']) {
1320 if(get_config('system','thread_allow')) {
1321 // Fallback to parent-uri if thr-parent is not set
1322 $thr_parent = $item['thr-parent'];
1323 if($thr_parent == '')
1324 $thr_parent = $item['parent-uri'];
1326 if($thr_parent == $parent['uri']) {
1327 $item['children'] = get_item_children($arr, $item);
1328 $children[] = $item;
1331 else if($item['parent'] == $parent['id']) {
1332 $children[] = $item;
1339 function sort_item_children($items) {
1341 usort($result,'sort_thr_created_rev');
1342 foreach($result as $k => $i) {
1343 if(count($result[$k]['children'])) {
1344 $result[$k]['children'] = sort_item_children($result[$k]['children']);
1350 function add_children_to_list($children, &$arr) {
1351 foreach($children as $y) {
1353 if(count($y['children']))
1354 add_children_to_list($y['children'], $arr);
1358 function conv_sort($arr,$order) {
1360 if((!(is_array($arr) && count($arr))))
1364 $children = array();
1367 if($x['id'] == $x['parent'])
1370 if(stristr($order,'created'))
1371 usort($parents,'sort_thr_created');
1372 elseif(stristr($order,'commented'))
1373 usort($parents,'sort_thr_commented');
1376 foreach($parents as $i=>$_x)
1377 $parents[$i]['children'] = get_item_children($arr, $_x);
1379 /*foreach($arr as $x) {
1380 if($x['id'] != $x['parent']) {
1381 $p = find_thread_parent_index($parents,$x);
1383 $parents[$p]['children'][] = $x;
1386 if(count($parents)) {
1387 foreach($parents as $k => $v) {
1388 if(count($parents[$k]['children'])) {
1389 $parents[$k]['children'] = sort_item_children($parents[$k]['children']);
1390 /*$y = $parents[$k]['children'];
1391 usort($y,'sort_thr_created_rev');
1392 $parents[$k]['children'] = $y;*/
1398 if(count($parents)) {
1399 foreach($parents as $x) {
1401 if(count($x['children']))
1402 add_children_to_list($x['children'], $ret);
1403 /*foreach($x['children'] as $y)
1412 function sort_thr_created($a,$b) {
1413 return strcmp($b['created'],$a['created']);
1416 function sort_thr_created_rev($a,$b) {
1417 return strcmp($a['created'],$b['created']);
1420 function sort_thr_commented($a,$b) {
1421 return strcmp($b['commented'],$a['commented']);
1424 function find_thread_parent_index($arr,$x) {
1425 foreach($arr as $k => $v)
1426 if($v['id'] == $x['parent'])
1431 function render_location_google($item) {
1432 $location = (($item['location']) ? '<a target="map" title="' . $item['location'] . '" href="http://maps.google.com/?q=' . urlencode($item['location']) . '">' . $item['location'] . '</a>' : '');
1433 $coord = (($item['coord']) ? '<a target="map" title="' . $item['coord'] . '" href="http://maps.google.com/?q=' . urlencode($item['coord']) . '">' . $item['coord'] . '</a>' : '');
1436 $location .= '<br /><span class="smalltext">(' . $coord . ')</span>';
1438 $location = '<span class="smalltext">' . $coord . '</span>';