3 // Note: the code in 'item_extract_images' and 'item_redir_and_replace_images'
4 // is identical to the code in mod/message.php for 'item_extract_images' and
5 // 'item_redir_and_replace_images'
6 if(! function_exists('item_extract_images')) {
7 function item_extract_images($body) {
9 $saved_image = array();
14 $img_start = strpos($orig_body, '[img');
15 $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
16 $img_end = ($img_start !== false ? strpos(substr($orig_body, $img_start), '[/img]') : false);
17 while(($img_st_close !== false) && ($img_end !== false)) {
19 $img_st_close++; // make it point to AFTER the closing bracket
20 $img_end += $img_start;
22 if(! strcmp(substr($orig_body, $img_start + $img_st_close, 5), 'data:')) {
23 // This is an embedded image
25 $saved_image[$cnt] = substr($orig_body, $img_start + $img_st_close, $img_end - ($img_start + $img_st_close));
26 $new_body = $new_body . substr($orig_body, 0, $img_start) . '[!#saved_image' . $cnt . '#!]';
31 $new_body = $new_body . substr($orig_body, 0, $img_end + strlen('[/img]'));
33 $orig_body = substr($orig_body, $img_end + strlen('[/img]'));
35 if($orig_body === false) // in case the body ends on a closing image tag
38 $img_start = strpos($orig_body, '[img');
39 $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
40 $img_end = ($img_start !== false ? strpos(substr($orig_body, $img_start), '[/img]') : false);
43 $new_body = $new_body . $orig_body;
45 return array('body' => $new_body, 'images' => $saved_image);
48 if(! function_exists('item_redir_and_replace_images')) {
49 function item_redir_and_replace_images($body, $images, $cid) {
54 for($i = 0; $i < count($images); $i++) {
55 $search = '/\[url\=(.*?)\]\[!#saved_image' . $i . '#!\]\[\/url\]' . '/is';
56 $replace = '[url=' . z_path() . '/redir/' . $cid
57 . '?f=1&url=' . '$1' . '][!#saved_image' . $i . '#!][/url]' ;
59 $img_end = strpos($origbody, '[!#saved_image' . $i . '#!][/url]') + strlen('[!#saved_image' . $i . '#!][/url]');
60 $process_part = substr($origbody, 0, $img_end);
61 $origbody = substr($origbody, $img_end);
63 $process_part = preg_replace($search, $replace, $process_part);
64 $newbody = $newbody . $process_part;
66 $newbody = $newbody . $origbody;
69 foreach($images as $image) {
70 // We're depending on the property of 'foreach' (specified on the PHP website) that
71 // it loops over the array starting from the first element and going sequentially
72 // to the last element
73 $newbody = str_replace('[!#saved_image' . $cnt . '#!]', '[img]' . $image . '[/img]', $newbody);
82 * Render actions localized
84 function localize_item(&$item){
86 $extracted = item_extract_images($item['body']);
87 if($extracted['images'])
88 $item['body'] = item_redir_and_replace_images($extracted['body'], $extracted['images'], $item['contact-id']);
90 $xmlhead="<"."?xml version='1.0' encoding='UTF-8' ?".">";
91 if ($item['verb']=== ACTIVITY_LIKE || $item['verb']=== ACTIVITY_DISLIKE){
93 $r = q("SELECT * from `item`,`contact` WHERE
94 `item`.`contact-id`=`contact`.`id` AND `item`.`uri`='%s';",
95 dbesc($item['parent-uri']));
96 if(count($r)==0) return;
99 $author = '[url=' . $item['author-link'] . ']' . $item['author-name'] . '[/url]';
100 $objauthor = '[url=' . $obj['author-link'] . ']' . $obj['author-name'] . '[/url]';
102 switch($obj['verb']){
104 switch ($obj['object-type']){
105 case ACTIVITY_OBJ_EVENT:
106 $post_type = t('event');
109 $post_type = t('status');
113 if($obj['resource-id']){
114 $post_type = t('photo');
115 $m=array(); preg_match("/\[url=([^]]*)\]/", $obj['body'], $m);
116 $rr['plink'] = $m[1];
118 $post_type = t('status');
122 $plink = '[url=' . $obj['plink'] . ']' . $post_type . '[/url]';
124 switch($item['verb']){
126 $bodyverb = t('%1$s likes %2$s\'s %3$s');
128 case ACTIVITY_DISLIKE:
129 $bodyverb = t('%1$s doesn\'t like %2$s\'s %3$s');
132 $item['body'] = sprintf($bodyverb, $author, $objauthor, $plink);
135 if ($item['verb']=== ACTIVITY_FRIEND){
137 if ($item['object-type']=="" || $item['object-type']!== ACTIVITY_OBJ_PERSON) return;
139 $Aname = $item['author-name'];
140 $Alink = $item['author-link'];
142 $xmlhead="<"."?xml version='1.0' encoding='UTF-8' ?".">";
144 $obj = parse_xml_string($xmlhead.$item['object']);
145 $links = parse_xml_string($xmlhead."<links>".unxmlify($obj->link)."</links>");
147 $Bname = $obj->title;
148 $Blink = ""; $Bphoto = "";
149 foreach ($links->link as $l){
150 $atts = $l->attributes();
151 switch($atts['rel']){
152 case "alternate": $Blink = $atts['href'];
153 case "photo": $Bphoto = $atts['href'];
158 $A = '[url=' . zrl($Alink) . ']' . $Aname . '[/url]';
159 $B = '[url=' . zrl($Blink) . ']' . $Bname . '[/url]';
160 if ($Bphoto!="") $Bphoto = '[url=' . zrl($Blink) . '][img]' . $Bphoto . '[/img][/url]';
162 $item['body'] = sprintf( t('%1$s is now friends with %2$s'), $A, $B)."\n\n\n".$Bphoto;
165 if (stristr($item['verb'],ACTIVITY_POKE)) {
166 $verb = urldecode(substr($item['verb'],strpos($item['verb'],'#')+1));
169 if ($item['object-type']=="" || $item['object-type']!== ACTIVITY_OBJ_PERSON) return;
171 $Aname = $item['author-name'];
172 $Alink = $item['author-link'];
174 $xmlhead="<"."?xml version='1.0' encoding='UTF-8' ?".">";
176 $obj = parse_xml_string($xmlhead.$item['object']);
177 $links = parse_xml_string($xmlhead."<links>".unxmlify($obj->link)."</links>");
179 $Bname = $obj->title;
180 $Blink = ""; $Bphoto = "";
181 foreach ($links->link as $l){
182 $atts = $l->attributes();
183 switch($atts['rel']){
184 case "alternate": $Blink = $atts['href'];
185 case "photo": $Bphoto = $atts['href'];
190 $A = '[url=' . zrl($Alink) . ']' . $Aname . '[/url]';
191 $B = '[url=' . zrl($Blink) . ']' . $Bname . '[/url]';
192 if ($Bphoto!="") $Bphoto = '[url=' . zrl($Blink) . '][img=80x80]' . $Bphoto . '[/img][/url]';
194 // we can't have a translation string with three positions but no distinguishable text
195 // So here is the translate string.
197 $txt = t('%1$s poked %2$s');
199 // now translate the verb
201 $txt = str_replace( t('poked'), t($verb), $txt);
203 // then do the sprintf on the translation string
205 $item['body'] = sprintf($txt, $A, $B). "\n\n\n" . $Bphoto;
208 if ($item['verb']===ACTIVITY_TAG){
209 $r = q("SELECT * from `item`,`contact` WHERE
210 `item`.`contact-id`=`contact`.`id` AND `item`.`uri`='%s';",
211 dbesc($item['parent-uri']));
212 if(count($r)==0) return;
215 $author = '[url=' . zrl($item['author-link']) . ']' . $item['author-name'] . '[/url]';
216 $objauthor = '[url=' . zrl($obj['author-link']) . ']' . $obj['author-name'] . '[/url]';
218 switch($obj['verb']){
220 switch ($obj['object-type']){
221 case ACTIVITY_OBJ_EVENT:
222 $post_type = t('event');
225 $post_type = t('status');
229 if($obj['resource-id']){
230 $post_type = t('photo');
231 $m=array(); preg_match("/\[url=([^]]*)\]/", $obj['body'], $m);
232 $rr['plink'] = $m[1];
234 $post_type = t('status');
237 $plink = '[url=' . $obj['plink'] . ']' . $post_type . '[/url]';
239 $parsedobj = parse_xml_string($xmlhead.$item['object']);
241 $tag = sprintf('#[url=%s]%s[/url]', $parsedobj->id, $parsedobj->content);
242 $item['body'] = sprintf( t('%1$s tagged %2$s\'s %3$s with %4$s'), $author, $objauthor, $plink, $tag );
245 if ($item['verb']=== ACTIVITY_FAVORITE){
247 if ($item['object-type']== "")
250 $Aname = $item['author-name'];
251 $Alink = $item['author-link'];
253 $xmlhead="<"."?xml version='1.0' encoding='UTF-8' ?".">";
255 $obj = parse_xml_string($xmlhead.$item['object']);
256 if(strlen($obj->id)) {
257 $r = q("select * from item where uri = '%s' and uid = %d limit 1",
261 if(count($r) && $r[0]['plink']) {
263 $Bname = $target['author-name'];
264 $Blink = $target['author-link'];
265 $A = '[url=' . zrl($Alink) . ']' . $Aname . '[/url]';
266 $B = '[url=' . zrl($Blink) . ']' . $Bname . '[/url]';
267 $P = '[url=' . $target['plink'] . ']' . t('post/item') . '[/url]';
268 $item['body'] = sprintf( t('%1$s marked %2$s\'s %3$s as favorite'), $A, $B, $P)."\n";
274 if(preg_match_all('/@\[url=(.*?)\]/is',$item['body'],$matches,PREG_SET_ORDER)) {
275 foreach($matches as $mtch) {
276 if(! strpos($mtch[1],'zrl='))
277 $item['body'] = str_replace($mtch[0],'@[url=' . zrl($mtch[1]). ']',$item['body']);
281 // add zrl's to public images
282 if(preg_match_all('/\[url=(.*?)\/photos\/(.*?)\/image\/(.*?)\]\[img(.*?)\]h(.*?)\[\/img\]\[\/url\]/is',$item['body'],$matches,PREG_SET_ORDER)) {
283 foreach($matches as $mtch) {
284 $item['body'] = str_replace($mtch[0],'[url=' . zrl($mtch[1] . '/photos/' . $mtch[2] . '/image/' . $mtch[3] ,true) . '][img' . $mtch[4] . ']h' . $mtch[5] . '[/img][/url]',$item['body']);
288 // add sparkle links to appropriate permalinks
290 $x = stristr($item['plink'],'/display/');
293 $y = best_link_url($item,$sparkle,true);
294 if(strstr($y,'/redir/'))
295 $item['plink'] = $y . '?f=&url=' . $item['plink'];
303 * Count the total of comments on this item and its desendants
305 function count_descendants($item) {
306 $total = count($item['children']);
309 foreach($item['children'] as $child) {
310 if($child['verb'] === ACTIVITY_LIKE || $child['verb'] === ACTIVITY_DISLIKE) {
313 $total += count_descendants($child);
321 * Recursively prepare a thread for HTML
324 function prepare_threads_body($a, $items, $cmnt_tpl, $page_writeable, $mode, $profile_owner, $alike, $dlike, $thread_level=1) {
327 $wall_template = 'wall_thread.tpl';
328 $wallwall_template = 'wallwall_thread.tpl';
330 $nb_items = count($items);
332 $total_children = $nb_items;
334 foreach($items as $item) {
335 if($item['network'] === NETWORK_MAIL && local_user() != $item['uid']) {
336 // Don't count it as a visible item
340 if($item['verb'] === ACTIVITY_LIKE || $item['verb'] === ACTIVITY_DISLIKE) {
347 foreach($items as $item) {
348 // prevent private email reply to public conversation from leaking.
349 if($item['network'] === NETWORK_MAIL && local_user() != $item['uid']) {
353 if($item['verb'] === ACTIVITY_LIKE || $item['verb'] === ACTIVITY_DISLIKE) {
360 $template = $wall_template;
363 $owner_url = $owner_photo = $owner_name = '';
367 $isstarred = "unstarred";
368 $photo = $item['photo'];
369 $thumb = $item['thumb'];
372 $lastcollapsed = false;
373 $firstcollapsed = false;
374 $total_children += count_descendants($item);
376 $toplevelpost = (($item['id'] == $item['parent']) ? true : false);
377 $item_writeable = (($item['writable'] || $item['self']) ? true : false);
378 $show_comment_box = ((($page_writeable) && ($item_writeable)) ? true : false);
379 $lock = ((($item['private'] == 1) || (($item['uid'] == local_user()) && (strlen($item['allow_cid']) || strlen($item['allow_gid'])
380 || strlen($item['deny_cid']) || strlen($item['deny_gid']))))
381 ? t('Private Message')
383 $redirect_url = $a->get_baseurl($ssl_state) . '/redir/' . $item['cid'] ;
384 $shareable = ((($profile_owner == local_user()) && ($item['private'] != 1)) ? true : false);
385 if(local_user() && link_compare($a->contact['url'],$item['author-link']))
386 $edpost = array($a->get_baseurl($ssl_state)."/editpost/".$item['id'], t("Edit"));
389 if((intval($item['contact-id']) && $item['contact-id'] == remote_user()) || ($item['uid'] == local_user()))
393 'dropping' => $dropping,
394 'select' => t('Select'),
395 'delete' => t('Delete'),
398 $filer = (($profile_owner == local_user()) ? t("save to folder") : false);
400 $diff_author = ((link_compare($item['url'],$item['author-link'])) ? false : true);
401 $profile_name = (((strlen($item['author-name'])) && $diff_author) ? $item['author-name'] : $item['name']);
402 if($item['author-link'] && (! $item['author-name']))
403 $profile_name = $item['author-link'];
406 $profile_link = best_link_url($item,$sp);
407 if($profile_link === 'mailbox')
410 $sparkle = ' sparkle';
412 $profile_link = zrl($profile_link);
414 $normalised = normalise_link((strlen($item['author-link'])) ? $item['author-link'] : $item['url']);
415 if(($normalised != 'mailbox') && (x($a->contacts,$normalised)))
416 $profile_avatar = $a->contacts[$normalised]['thumb'];
418 $profile_avatar = (((strlen($item['author-avatar'])) && $diff_author) ? $item['author-avatar'] : $a->get_cached_avatar_image($thumb));
420 $locate = array('location' => $item['location'], 'coord' => $item['coord'], 'html' => '');
421 call_hooks('render_location',$locate);
422 $location = ((strlen($locate['html'])) ? $locate['html'] : render_location_google($locate));
425 foreach(explode(',',$item['tag']) as $tag){
427 if ($tag!="") $tags[] = bbcode($tag);
430 $like = ((x($alike,$item['uri'])) ? format_like($alike[$item['uri']],$alike[$item['uri'] . '-l'],'like',$item['uri']) : '');
431 $dislike = ((x($dlike,$item['uri'])) ? format_like($dlike[$item['uri']],$dlike[$item['uri'] . '-l'],'dislike',$item['uri']) : '');
434 if((! $item['self']) && ($mode !== 'profile')) {
437 // On the network page, I am the owner. On the display page it will be the profile owner.
438 // This will have been stored in $a->page_contact by our calling page.
439 // Put this person as the wall owner of the wall-to-wall notice.
441 $owner_url = zrl($a->page_contact['url']);
442 $owner_photo = $a->page_contact['thumb'];
443 $owner_name = $a->page_contact['name'];
444 $template = $wallwall_template;
448 else if($item['owner-link']) {
450 $owner_linkmatch = (($item['owner-link']) && link_compare($item['owner-link'],$item['author-link']));
451 $alias_linkmatch = (($item['alias']) && link_compare($item['alias'],$item['author-link']));
452 $owner_namematch = (($item['owner-name']) && $item['owner-name'] == $item['author-name']);
453 if((! $owner_linkmatch) && (! $alias_linkmatch) && (! $owner_namematch)) {
455 // The author url doesn't match the owner (typically the contact)
456 // and also doesn't match the contact alias.
457 // The name match is a hack to catch several weird cases where URLs are
458 // all over the park. It can be tricked, but this prevents you from
459 // seeing "Bob Smith to Bob Smith via Wall-to-wall" and you know darn
460 // well that it's the same Bob Smith.
462 // But it could be somebody else with the same name. It just isn't highly likely.
465 $owner_url = $item['owner-link'];
466 $owner_photo = $item['owner-avatar'];
467 $owner_name = $item['owner-name'];
468 $template = $wallwall_template;
470 // If it is our contact, use a friendly redirect link
471 if((link_compare($item['owner-link'],$item['url']))
472 && ($item['network'] === NETWORK_DFRN)) {
473 $owner_url = $redirect_url;
474 $osparkle = ' sparkle';
477 $owner_url = zrl($owner_url);
480 if($profile_owner == local_user()) {
481 $isstarred = (($item['starred']) ? "starred" : "unstarred");
484 'do' => t("add star"),
485 'undo' => t("remove star"),
486 'toggle' => t("toggle star status"),
487 'classdo' => (($item['starred']) ? "hidden" : ""),
488 'classundo' => (($item['starred']) ? "" : "hidden"),
489 'starred' => t('starred'),
490 'tagger' => t("add tag"),
497 if(($nb_items > 2) || ($thread_level > 2)) {
498 if($items_seen == 1) {
499 $firstcollapsed = true;
501 if($thread_level > 2) {
502 if($items_seen == $nb_items)
503 $lastcollapsed = true;
505 else if($items_seen == ($nb_items - 2)) {
506 $lastcollapsed = true;
511 if($page_writeable) {
513 'like' => array( t("I like this \x28toggle\x29"), t("like")),
514 'dislike' => array( t("I don't like this \x28toggle\x29"), t("dislike")),
516 if ($shareable) $buttons['share'] = array( t('Share this'), t('share'));
519 if($show_comment_box) {
520 $qc = $qcomment = null;
522 if(in_array('qcomment',$a->plugins)) {
523 $qc = ((local_user()) ? get_pconfig(local_user(),'qcomment','words') : null);
524 $qcomment = (($qc) ? explode("\n",$qc) : null);
526 $comment = replace_macros($cmnt_tpl,array(
527 '$return_path' => '',
528 '$jsreload' => (($mode === 'display') ? $_SESSION['return_url'] : ''),
529 '$type' => (($mode === 'profile') ? 'wall-comment' : 'net-comment'),
530 '$id' => $item['item_id'],
531 '$parent' => $item['item_id'],
532 '$qcomment' => $qcomment,
533 '$profile_uid' => $profile_owner,
534 '$mylink' => $a->contact['url'],
535 '$mytitle' => t('This is you'),
536 '$myphoto' => $a->contact['thumb'],
537 '$comment' => t('Comment'),
538 '$submit' => t('Submit'),
539 '$edbold' => t('Bold'),
540 '$editalic' => t('Italic'),
541 '$eduline' => t('Underline'),
542 '$edquote' => t('Quote'),
543 '$edcode' => t('Code'),
544 '$edimg' => t('Image'),
545 '$edurl' => t('Link'),
546 '$edvideo' => t('Video'),
547 '$preview' => t('Preview'),
548 '$sourceapp' => t($a->sourcename),
549 '$ww' => (($mode === 'network') ? $commentww : '')
554 if(strcmp(datetime_convert('UTC','UTC',$item['created']),datetime_convert('UTC','UTC','now - 12 hours')) > 0)
557 localize_item($item);
559 $body = prepare_body($item,true);
562 // collapse comments in template. I don't like this much...
563 'comment_firstcollapsed' => $firstcollapsed,
564 'comment_lastcollapsed' => $lastcollapsed,
565 // template to use to render item (wall, walltowall, search)
566 'template' => $template,
568 'type' => implode("",array_slice(explode("/",$item['verb']),-1)),
570 'body' => template_escape($body),
571 'text' => strip_tags(template_escape($body)),
572 'id' => $item['item_id'],
573 'linktitle' => sprintf( t('View %s\'s profile @ %s'), $profile_name, ((strlen($item['author-link'])) ? $item['author-link'] : $item['url'])),
574 'olinktitle' => sprintf( t('View %s\'s profile @ %s'), $owner_name, ((strlen($item['owner-link'])) ? $item['owner-link'] : $item['url'])),
576 'wall' => t('Wall-to-Wall'),
577 'vwall' => t('via Wall-To-Wall:'),
578 'profile_url' => $profile_link,
579 'item_photo_menu' => item_photo_menu($item),
580 'name' => template_escape($profile_name),
581 'thumb' => $profile_avatar,
582 'osparkle' => $osparkle,
583 'sparkle' => $sparkle,
584 'title' => template_escape($item['title']),
585 'ago' => (($item['app']) ? sprintf( t('%s from %s'),relative_date($item['created']),$item['app']) : relative_date($item['created'])),
587 'location' => template_escape($location),
589 'owner_url' => $owner_url,
590 'owner_photo' => $owner_photo,
591 'owner_name' => template_escape($owner_name),
592 'plink' => get_plink($item),
594 'isstarred' => $isstarred,
600 'dislike' => $dislike,
601 'comment' => $comment,
602 'previewing' => $previewing,
603 'wait' => t('Please wait'),
604 'thread_level' => $thread_level,
607 $arr = array('item' => $item, 'output' => $tmp_item);
608 call_hooks('display_item', $arr);
610 $item_result = $arr['output'];
611 if($firstcollapsed) {
612 $item_result['num_comments'] = sprintf( tt('%d comment','%d comments',$total_children),$total_children );
613 $item_result['hide_text'] = t('show more');
616 $item_result['children'] = array();
617 if(count($item['children'])) {
618 $item_result['children'] = prepare_threads_body($a, $item['children'], $cmnt_tpl, $page_writeable, $mode, $profile_owner, $alike, $dlike, ($thread_level + 1));
620 $item_result['private'] = $item['private'];
621 $item_result['toplevel'] = ($toplevelpost ? 'toplevel_item' : '');
624 * I don't like this very much...
626 if(get_config('system','thread_allow')) {
627 $item_result['flatten'] = false;
628 $item_result['threaded'] = true;
631 $item_result['flatten'] = true;
632 $item_result['threaded'] = false;
634 $item_result['comment'] = false;
638 $result[] = $item_result;
645 * "Render" a conversation or list of items for HTML display.
646 * There are two major forms of display:
647 * - Sequential or unthreaded ("New Item View" or search results)
648 * - conversation view
649 * The $mode parameter decides between the various renderings and also
650 * figures out how to determine page owner and other contextual items
651 * that are based on unique features of the calling module.
655 if(!function_exists('conversation')) {
656 function conversation(&$a, $items, $mode, $update, $preview = false) {
659 require_once('bbcode.php');
661 $ssl_state = ((local_user()) ? true : false);
664 $page_writeable = false;
666 $previewing = (($preview) ? ' preview ' : '');
668 if($mode === 'network') {
669 $profile_owner = local_user();
670 $page_writeable = true;
673 if($mode === 'profile') {
674 $profile_owner = $a->profile['profile_uid'];
675 $page_writeable = can_write_wall($a,$profile_owner);
678 if($mode === 'notes') {
679 $profile_owner = local_user();
680 $page_writeable = true;
683 if($mode === 'display') {
684 $profile_owner = $a->profile['uid'];
685 $page_writeable = can_write_wall($a,$profile_owner);
688 if($mode === 'community') {
690 $page_writeable = false;
693 $page_dropping = ((local_user() && local_user() == $profile_owner) ? true : false);
697 $return_url = $_SESSION['return_url'];
699 $return_url = $_SESSION['return_url'] = $a->query_string;
701 load_contact_links(local_user());
703 $cb = array('items' => $items, 'mode' => $mode, 'update' => $update, 'preview' => $preview);
704 call_hooks('conversation_start',$cb);
706 $items = $cb['items'];
708 $cmnt_tpl = get_markup_template('comment_item.tpl');
709 $tpl = 'wall_item.tpl';
710 $wallwall = 'wallwall_item.tpl';
711 $hide_comments_tpl = get_markup_template('hide_comments.tpl');
717 // array with html for each thread (parent+comments)
721 $page_template = get_markup_template("conversation.tpl");
723 if($items && count($items)) {
725 if($mode === 'network-new' || $mode === 'search' || $mode === 'community') {
727 // "New Item View" on network page or search page results
728 // - just loop through the items and format them minimally for display
730 //$tpl = get_markup_template('search_item.tpl');
731 $tpl = 'search_item.tpl';
733 foreach($items as $item) {
742 if($mode === 'search' || $mode === 'community') {
743 if(((activity_match($item['verb'],ACTIVITY_LIKE)) || (activity_match($item['verb'],ACTIVITY_DISLIKE)))
744 && ($item['id'] != $item['parent']))
746 $nickname = $item['nickname'];
749 $nickname = $a->user['nickname'];
751 // prevent private email from leaking.
752 if($item['network'] === NETWORK_MAIL && local_user() != $item['uid'])
755 $profile_name = ((strlen($item['author-name'])) ? $item['author-name'] : $item['name']);
756 if($item['author-link'] && (! $item['author-name']))
757 $profile_name = $item['author-link'];
762 $profile_link = best_link_url($item,$sp);
763 if($profile_link === 'mailbox')
766 $sparkle = ' sparkle';
768 $profile_link = zrl($profile_link);
770 $normalised = normalise_link((strlen($item['author-link'])) ? $item['author-link'] : $item['url']);
771 if(($normalised != 'mailbox') && (x($a->contacts[$normalised])))
772 $profile_avatar = $a->contacts[$normalised]['thumb'];
774 $profile_avatar = ((strlen($item['author-avatar'])) ? $a->get_cached_avatar_image($item['author-avatar']) : $item['thumb']);
776 $locate = array('location' => $item['location'], 'coord' => $item['coord'], 'html' => '');
777 call_hooks('render_location',$locate);
779 $location = ((strlen($locate['html'])) ? $locate['html'] : render_location_google($locate));
781 localize_item($item);
782 if($mode === 'network-new')
789 'dropping' => $dropping,
790 'select' => t('Select'),
791 'delete' => t('Delete'),
795 $isstarred = "unstarred";
798 $likebuttons = false;
801 $body = prepare_body($item,true);
803 //$tmp_item = replace_macros($tpl,array(
806 'id' => (($preview) ? 'P0' : $item['item_id']),
807 'linktitle' => sprintf( t('View %s\'s profile @ %s'), $profile_name, ((strlen($item['author-link'])) ? $item['author-link'] : $item['url'])),
808 'profile_url' => $profile_link,
809 'item_photo_menu' => item_photo_menu($item),
810 'name' => template_escape($profile_name),
811 'sparkle' => $sparkle,
813 'thumb' => $profile_avatar,
814 'title' => template_escape($item['title']),
815 'body' => template_escape($body),
816 'text' => strip_tags(template_escape($body)),
817 'ago' => (($item['app']) ? sprintf( t('%s from %s'),relative_date($item['created']),$item['app']) : relative_date($item['created'])),
818 'location' => template_escape($location),
820 'owner_name' => template_escape($owner_name),
821 'owner_url' => $owner_url,
822 'owner_photo' => $owner_photo,
823 'plink' => get_plink($item),
825 'isstarred' => $isstarred,
828 'vote' => $likebuttons,
832 'conv' => (($preview) ? '' : array('href'=> $a->get_baseurl($ssl_state) . '/display/' . $nickname . '/' . $item['id'], 'title'=> t('View in context'))),
833 'previewing' => $previewing,
834 'wait' => t('Please wait'),
838 $arr = array('item' => $item, 'output' => $tmp_item);
839 call_hooks('display_item', $arr);
841 $threads[$threadsid]['id'] = $item['item_id'];
842 $threads[$threadsid]['items'] = array($arr['output']);
850 $page_template = get_markup_template("threaded_conversation.tpl");
852 require_once('object/Conversation.php');
853 require_once('object/Item.php');
855 $conv = new Conversation($mode);
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) {
864 like_puller($a,$item,$alike,'like');
865 like_puller($a,$item,$dlike,'dislike');
867 if($item['id'] == $item['parent']) {
868 $item_object = new Item($item);
869 $conv->add_thread($item_object);
873 $threads = $conv->get_template_data($cmnt_tpl, $alike, $dlike);
875 logger('[ERROR] conversation : Failed to get template data.', LOGGER_DEBUG);
878 logger('[DEBUG] conversation : $threads = '. print_r($threads, true), LOGGER_DEBUG);
882 $o = replace_macros($page_template, array(
883 '$baseurl' => $a->get_baseurl($ssl_state),
886 '$threads' => $threads,
887 '$dropping' => ($page_dropping?t('Delete Selected Items'):False),
893 function best_link_url($item,&$sparkle,$ssl_state = false) {
900 $clean_url = normalise_link($item['author-link']);
902 if((local_user()) && (local_user() == $item['uid'])) {
903 if(isset($a->contacts) && x($a->contacts,$clean_url)) {
904 if($a->contacts[$clean_url]['network'] === NETWORK_DFRN) {
905 $best_url = $a->get_baseurl($ssl_state) . '/redir/' . $a->contacts[$clean_url]['id'];
909 $best_url = $a->contacts[$clean_url]['url'];
913 if(strlen($item['author-link']))
914 $best_url = $item['author-link'];
916 $best_url = $item['url'];
923 if(! function_exists('item_photo_menu')){
924 function item_photo_menu($item){
931 if(! count($a->contacts))
932 load_contact_links(local_user());
942 $profile_link = best_link_url($item,$sparkle,$ssl_state);
943 if($profile_link === 'mailbox')
947 $cid = intval(basename($profile_link));
948 $status_link = $profile_link . "?url=status";
949 $photos_link = $profile_link . "?url=photos";
950 $profile_link = $profile_link . "?url=profile";
951 $pm_url = $a->get_baseurl($ssl_state) . '/message/new/' . $cid;
955 $profile_link = zrl($profile_link);
956 if(local_user() && local_user() == $item['uid'] && link_compare($item['url'],$item['author-link'])) {
957 $cid = $item['contact-id'];
963 if(($cid) && (! $item['self'])) {
964 $poke_link = $a->get_baseurl($ssl_state) . '/poke/?f=&c=' . $cid;
965 $contact_url = $a->get_baseurl($ssl_state) . '/contacts/' . $cid;
966 $posts_link = $a->get_baseurl($ssl_state) . '/network/?cid=' . $cid;
968 $clean_url = normalise_link($item['author-link']);
970 if((local_user()) && (local_user() == $item['uid'])) {
971 if(isset($a->contacts) && x($a->contacts,$clean_url)) {
972 if($a->contacts[$clean_url]['network'] === NETWORK_DIASPORA) {
973 $pm_url = $a->get_baseurl($ssl_state) . '/message/new/' . $cid;
981 t("View Status") => $status_link,
982 t("View Profile") => $profile_link,
983 t("View Photos") => $photos_link,
984 t("Network Posts") => $posts_link,
985 t("Edit Contact") => $contact_url,
986 t("Send PM") => $pm_url,
987 t("Poke") => $poke_link
991 $args = array('item' => $item, 'menu' => $menu);
993 call_hooks('item_photo_menu', $args);
995 $menu = $args['menu'];
998 foreach($menu as $k=>$v){
999 if ($v!="") $o .= "<li><a href=\"$v\">$k</a></li>\n";
1004 if(! function_exists('like_puller')) {
1005 function like_puller($a,$item,&$arr,$mode) {
1009 $verb = (($mode === 'like') ? ACTIVITY_LIKE : ACTIVITY_DISLIKE);
1011 if((activity_match($item['verb'],$verb)) && ($item['id'] != $item['parent'])) {
1012 $url = $item['author-link'];
1013 if((local_user()) && (local_user() == $item['uid']) && ($item['network'] === 'dfrn') && (! $item['self']) && (link_compare($item['author-link'],$item['url']))) {
1014 $url = $a->get_baseurl(true) . '/redir/' . $item['contact-id'];
1015 $sparkle = ' class="sparkle" ';
1020 if(! $item['thr-parent'])
1021 $item['thr-parent'] = $item['parent-uri'];
1023 if(! ((isset($arr[$item['thr-parent'] . '-l'])) && (is_array($arr[$item['thr-parent'] . '-l']))))
1024 $arr[$item['thr-parent'] . '-l'] = array();
1025 if(! isset($arr[$item['thr-parent']]))
1026 $arr[$item['thr-parent']] = 1;
1028 $arr[$item['thr-parent']] ++;
1029 $arr[$item['thr-parent'] . '-l'][] = '<a href="'. $url . '"'. $sparkle .'>' . $item['author-name'] . '</a>';
1034 // Format the like/dislike text for a profile item
1035 // $cnt = number of people who like/dislike the item
1036 // $arr = array of pre-linked names of likers/dislikers
1037 // $type = one of 'like, 'dislike'
1039 // returns formatted text
1041 if(! function_exists('format_like')) {
1042 function format_like($cnt,$arr,$type,$id) {
1045 $o .= (($type === 'like') ? sprintf( t('%s likes this.'), $arr[0]) : sprintf( t('%s doesn\'t like this.'), $arr[0])) . EOL ;
1047 $spanatts = 'class="fakelink" onclick="openClose(\'' . $type . 'list-' . $id . '\');"';
1048 $o .= (($type === 'like') ?
1049 sprintf( t('<span %1$s>%2$d people</span> like this.'), $spanatts, $cnt)
1051 sprintf( t('<span %1$s>%2$d people</span> don\'t like this.'), $spanatts, $cnt) );
1053 $total = count($arr);
1054 if($total >= MAX_LIKERS)
1055 $arr = array_slice($arr, 0, MAX_LIKERS - 1);
1056 if($total < MAX_LIKERS)
1057 $arr[count($arr)-1] = t('and') . ' ' . $arr[count($arr)-1];
1058 $str = implode(', ', $arr);
1059 if($total >= MAX_LIKERS)
1060 $str .= sprintf( t(', and %d other people'), $total - MAX_LIKERS );
1061 $str = (($type === 'like') ? sprintf( t('%s like this.'), $str) : sprintf( t('%s don\'t like this.'), $str));
1062 $o .= "\t" . '<div id="' . $type . 'list-' . $id . '" style="display: none;" >' . $str . '</div>';
1068 function status_editor($a,$x, $notes_cid = 0, $popup=false) {
1072 $geotag = (($x['allow_location']) ? get_markup_template('jot_geotag.tpl') : '');
1075 if(local_user() && intval(get_pconfig(local_user(),'system','plaintext')))
1078 $tpl = get_markup_template('jot-header.tpl');
1079 $a->page['htmlhead'] .= replace_macros($tpl, array(
1080 '$newpost' => 'true',
1081 '$baseurl' => $a->get_baseurl(true),
1082 '$editselect' => (($plaintext) ? 'none' : '/(profile-jot-text|prvmail-text)/'),
1083 '$geotag' => $geotag,
1084 '$nickname' => $x['nickname'],
1085 '$ispublic' => t('Visible to <strong>everybody</strong>'),
1086 '$linkurl' => t('Please enter a link URL:'),
1087 '$vidurl' => t("Please enter a video link/URL:"),
1088 '$audurl' => t("Please enter an audio link/URL:"),
1089 '$term' => t('Tag term:'),
1090 '$fileas' => t('Save to Folder:'),
1091 '$whereareu' => t('Where are you right now?')
1095 $tpl = get_markup_template('jot-end.tpl');
1096 $a->page['end'] .= replace_macros($tpl, array(
1097 '$newpost' => 'true',
1098 '$baseurl' => $a->get_baseurl(true),
1099 '$editselect' => (($plaintext) ? 'none' : '/(profile-jot-text|prvmail-text)/'),
1100 '$geotag' => $geotag,
1101 '$nickname' => $x['nickname'],
1102 '$ispublic' => t('Visible to <strong>everybody</strong>'),
1103 '$linkurl' => t('Please enter a link URL:'),
1104 '$vidurl' => t("Please enter a video link/URL:"),
1105 '$audurl' => t("Please enter an audio link/URL:"),
1106 '$term' => t('Tag term:'),
1107 '$fileas' => t('Save to Folder:'),
1108 '$whereareu' => t('Where are you right now?')
1112 $tpl = get_markup_template("jot.tpl");
1117 $mail_disabled = ((function_exists('imap_open') && (! get_config('system','imap_disabled'))) ? 0 : 1);
1119 $mail_enabled = false;
1120 $pubmail_enabled = false;
1122 if(($x['is_owner']) && (! $mail_disabled)) {
1123 $r = q("SELECT * FROM `mailacct` WHERE `uid` = %d AND `server` != '' LIMIT 1",
1124 intval(local_user())
1127 $mail_enabled = true;
1128 if(intval($r[0]['pubmail']))
1129 $pubmail_enabled = true;
1134 $selected = (($pubmail_enabled) ? ' checked="checked" ' : '');
1135 $jotnets .= '<div class="profile-jot-net"><input type="checkbox" name="pubmail_enable"' . $selected . ' value="1" /> ' . t("Post to Email") . '</div>';
1138 call_hooks('jot_tool', $jotplugins);
1139 call_hooks('jot_networks', $jotnets);
1142 $jotnets .= '<input type="hidden" name="contact_allow[]" value="' . $notes_cid .'" />';
1144 $tpl = replace_macros($tpl,array('$jotplugins' => $jotplugins));
1146 $o .= replace_macros($tpl,array(
1147 '$return_path' => $a->query_string,
1148 '$action' => $a->get_baseurl(true) . '/item',
1149 '$share' => (x($x,'button') ? $x['button'] : t('Share')),
1150 '$upload' => t('Upload photo'),
1151 '$shortupload' => t('upload photo'),
1152 '$attach' => t('Attach file'),
1153 '$shortattach' => t('attach file'),
1154 '$weblink' => t('Insert web link'),
1155 '$shortweblink' => t('web link'),
1156 '$video' => t('Insert video link'),
1157 '$shortvideo' => t('video link'),
1158 '$audio' => t('Insert audio link'),
1159 '$shortaudio' => t('audio link'),
1160 '$setloc' => t('Set your location'),
1161 '$shortsetloc' => t('set location'),
1162 '$noloc' => t('Clear browser location'),
1163 '$shortnoloc' => t('clear location'),
1165 '$placeholdertitle' => t('Set title'),
1167 '$placeholdercategory' => t('Categories (comma-separated list)'),
1168 '$wait' => t('Please wait'),
1169 '$permset' => t('Permission settings'),
1170 '$shortpermset' => t('permissions'),
1171 '$ptyp' => (($notes_cid) ? 'note' : 'wall'),
1174 '$baseurl' => $a->get_baseurl(true),
1175 '$defloc' => $x['default_location'],
1176 '$visitor' => $x['visitor'],
1177 '$pvisit' => (($notes_cid) ? 'none' : $x['visitor']),
1178 '$emailcc' => t('CC: email addresses'),
1179 '$public' => t('Public post'),
1180 '$jotnets' => $jotnets,
1181 '$emtitle' => t('Example: bob@example.com, mary@example.com'),
1182 '$lockstate' => $x['lockstate'],
1183 '$acl' => $x['acl'],
1184 '$bang' => $x['bang'],
1185 '$profile_uid' => $x['profile_uid'],
1186 '$preview' => t('Preview'),
1187 '$sourceapp' => t($a->sourcename),
1192 $o = '<div id="jot-popup" style="display: none;">'.$o.'</div>';
1200 function get_item_children($arr, $parent) {
1201 $children = array();
1202 foreach($arr as $item) {
1203 if($item['id'] != $item['parent']) {
1204 if(get_config('system','thread_allow')) {
1205 // Fallback to parent-uri if thr-parent is not set
1206 $thr_parent = $item['thr-parent'];
1207 if($thr_parent == '')
1208 $thr_parent = $item['parent-uri'];
1210 if($thr_parent == $parent['uri']) {
1211 $item['children'] = get_item_children($arr, $item);
1212 $children[] = $item;
1215 else if($item['parent'] == $parent['id']) {
1216 $children[] = $item;
1223 function sort_item_children($items) {
1225 usort($result,'sort_thr_created_rev');
1226 foreach($result as $k => $i) {
1227 if(count($result[$k]['children'])) {
1228 $result[$k]['children'] = sort_item_children($result[$k]['children']);
1234 function add_children_to_list($children, &$arr) {
1235 foreach($children as $y) {
1237 if(count($y['children']))
1238 add_children_to_list($y['children'], $arr);
1242 function conv_sort($arr,$order) {
1244 if((!(is_array($arr) && count($arr))))
1248 $children = array();
1251 if($x['id'] == $x['parent'])
1254 if(stristr($order,'created'))
1255 usort($parents,'sort_thr_created');
1256 elseif(stristr($order,'commented'))
1257 usort($parents,'sort_thr_commented');
1260 foreach($parents as $i=>$_x)
1261 $parents[$i]['children'] = get_item_children($arr, $_x);
1263 /*foreach($arr as $x) {
1264 if($x['id'] != $x['parent']) {
1265 $p = find_thread_parent_index($parents,$x);
1267 $parents[$p]['children'][] = $x;
1270 if(count($parents)) {
1271 foreach($parents as $k => $v) {
1272 if(count($parents[$k]['children'])) {
1273 $parents[$k]['children'] = sort_item_children($parents[$k]['children']);
1274 /*$y = $parents[$k]['children'];
1275 usort($y,'sort_thr_created_rev');
1276 $parents[$k]['children'] = $y;*/
1282 if(count($parents)) {
1283 foreach($parents as $x) {
1285 if(count($x['children']))
1286 add_children_to_list($x['children'], $ret);
1287 /*foreach($x['children'] as $y)
1296 function sort_thr_created($a,$b) {
1297 return strcmp($b['created'],$a['created']);
1300 function sort_thr_created_rev($a,$b) {
1301 return strcmp($a['created'],$b['created']);
1304 function sort_thr_commented($a,$b) {
1305 return strcmp($b['commented'],$a['commented']);
1308 function find_thread_parent_index($arr,$x) {
1309 foreach($arr as $k => $v)
1310 if($v['id'] == $x['parent'])
1315 function render_location_google($item) {
1316 $location = (($item['location']) ? '<a target="map" title="' . $item['location'] . '" href="http://maps.google.com/?q=' . urlencode($item['location']) . '">' . $item['location'] . '</a>' : '');
1317 $coord = (($item['coord']) ? '<a target="map" title="' . $item['coord'] . '" href="http://maps.google.com/?q=' . urlencode($item['coord']) . '">' . $item['coord'] . '</a>' : '');
1320 $location .= '<br /><span class="smalltext">(' . $coord . ')</span>';
1322 $location = '<span class="smalltext">' . $coord . '</span>';