]> git.mxchange.org Git - friendica.git/blobdiff - include/conversation.php
Merge pull request #404 from CyberDomovoy/hook_priority
[friendica.git] / include / conversation.php
index c2113dead4b2a59d813d0a01c1fc36cf2977e70d..4a9142bb202285c6553a2a2dd2e2317ea9c914fd 100644 (file)
@@ -1,30 +1,91 @@
 <?php
 
+// Note: the code in 'item_extract_images' and 'item_redir_and_replace_images'
+// is identical to the code in mod/message.php for 'item_extract_images' and
+// 'item_redir_and_replace_images'
+if(! function_exists('item_extract_images')) {
+function item_extract_images($body) {
+
+       $saved_image = array();
+       $orig_body = $body;
+       $new_body = '';
+
+       $cnt = 0;
+       $img_start = strpos($orig_body, '[img');
+       $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
+       $img_end = ($img_start !== false ? strpos(substr($orig_body, $img_start), '[/img]') : false);
+       while(($img_st_close !== false) && ($img_end !== false)) {
+
+               $img_st_close++; // make it point to AFTER the closing bracket
+               $img_end += $img_start;
+
+               if(! strcmp(substr($orig_body, $img_start + $img_st_close, 5), 'data:')) {
+                       // This is an embedded image
+
+                       $saved_image[$cnt] = substr($orig_body, $img_start + $img_st_close, $img_end - ($img_start + $img_st_close));
+                       $new_body = $new_body . substr($orig_body, 0, $img_start) . '[!#saved_image' . $cnt . '#!]';
+
+                       $cnt++;
+               }
+               else
+                       $new_body = $new_body . substr($orig_body, 0, $img_end + strlen('[/img]'));
+
+               $orig_body = substr($orig_body, $img_end + strlen('[/img]'));
+
+               if($orig_body === false) // in case the body ends on a closing image tag
+                       $orig_body = '';
+
+               $img_start = strpos($orig_body, '[img');
+               $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
+               $img_end = ($img_start !== false ? strpos(substr($orig_body, $img_start), '[/img]') : false);
+       }
+
+       $new_body = $new_body . $orig_body;
+
+       return array('body' => $new_body, 'images' => $saved_image);
+}}
+
+if(! function_exists('item_redir_and_replace_images')) {
+function item_redir_and_replace_images($body, $images, $cid) {
+
+       $origbody = $body;
+       $newbody = '';
+
+       for($i = 0; $i < count($images); $i++) {
+               $search = '/\[url\=(.*?)\]\[!#saved_image' . $i . '#!\]\[\/url\]' . '/is';
+               $replace = '[url=' . z_path() . '/redir/' . $cid 
+                          . '?f=1&url=' . '$1' . '][!#saved_image' . $i . '#!][/url]' ;
+
+               $img_end = strpos($origbody, '[!#saved_image' . $i . '#!][/url]') + strlen('[!#saved_image' . $i . '#!][/url]');
+               $process_part = substr($origbody, 0, $img_end);
+               $origbody = substr($origbody, $img_end);
+
+               $process_part = preg_replace($search, $replace, $process_part);
+               $newbody = $newbody . $process_part;
+       }
+       $newbody = $newbody . $origbody;
+
+       $cnt = 0;
+       foreach($images as $image) {
+               // We're depending on the property of 'foreach' (specified on the PHP website) that
+               // it loops over the array starting from the first element and going sequentially
+               // to the last element
+               $newbody = str_replace('[!#saved_image' . $cnt . '#!]', '[img]' . $image . '[/img]', $newbody);
+               $cnt++;
+       }
+       return $newbody;
+}}
+
+
+
 /**
  * Render actions localized
  */
 function localize_item(&$item){
 
-       $Text = $item['body'];
-       $saved_image = '';
-       $img_start = strpos($Text,'[img]data:');
-       $img_end = strpos($Text,'[/img]');
-
-       if($img_start !== false && $img_end !== false && $img_end > $img_start) {
-               $start_fragment = substr($Text,0,$img_start);
-               $img_start += strlen('[img]');
-               $saved_image = substr($Text,$img_start,$img_end - $img_start);
-               $end_fragment = substr($Text,$img_end + strlen('[/img]'));              
-               $Text = $start_fragment . '[!#saved_image#!]' . $end_fragment;
-               $search = '/\[url\=(.*?)\]\[!#saved_image#!\]\[\/url\]' . '/is';
-               $replace = '[url=' . z_path() . '/redir/' . $item['contact-id'] 
-                       . '?f=1&url=' . '$1' . '][!#saved_image#!][/url]' ;
-
-               $Text = preg_replace($search,$replace,$Text);
-
-               if(strlen($saved_image))
-                       $item['body'] = str_replace('[!#saved_image#!]', '[img]' . $saved_image . '[/img]',$Text);
-       }
+       $extracted = item_extract_images($item['body']);
+       if($extracted['images'])
+               $item['body'] = item_redir_and_replace_images($extracted['body'], $extracted['images'], $item['contact-id']);
 
        $xmlhead="<"."?xml version='1.0' encoding='UTF-8' ?".">";
        if ($item['verb']=== ACTIVITY_LIKE || $item['verb']=== ACTIVITY_DISLIKE){
@@ -101,6 +162,49 @@ function localize_item(&$item){
                $item['body'] = sprintf( t('%1$s is now friends with %2$s'), $A, $B)."\n\n\n".$Bphoto;
 
        }
+       if (stristr($item['verb'],ACTIVITY_POKE)) {
+               $verb = urldecode(substr($item['verb'],strpos($item['verb'],'#')+1));
+               if(! $verb)
+                       return;
+               if ($item['object-type']=="" || $item['object-type']!== ACTIVITY_OBJ_PERSON) return;
+
+               $Aname = $item['author-name'];
+               $Alink = $item['author-link'];
+               
+               $xmlhead="<"."?xml version='1.0' encoding='UTF-8' ?".">";
+               
+               $obj = parse_xml_string($xmlhead.$item['object']);
+               $links = parse_xml_string($xmlhead."<links>".unxmlify($obj->link)."</links>");
+               
+               $Bname = $obj->title;
+               $Blink = ""; $Bphoto = "";
+               foreach ($links->link as $l){
+                       $atts = $l->attributes();
+                       switch($atts['rel']){
+                               case "alternate": $Blink = $atts['href'];
+                               case "photo": $Bphoto = $atts['href'];
+                       }
+                       
+               }
+               
+               $A = '[url=' . zrl($Alink) . ']' . $Aname . '[/url]';
+               $B = '[url=' . zrl($Blink) . ']' . $Bname . '[/url]';
+               if ($Bphoto!="") $Bphoto = '[url=' . zrl($Blink) . '][img=80x80]' . $Bphoto . '[/img][/url]';
+
+               // we can't have a translation string with three positions but no distinguishable text
+               // So here is the translate string.
+
+               $txt = t('%1$s poked %2$s');
+
+               // now translate the verb
+
+               $txt = str_replace( t('poked'), t($verb), $txt);
+
+               // then do the sprintf on the translation string
+
+               $item['body'] = sprintf($txt, $A, $B). "\n\n\n" . $Bphoto;
+
+       }
     if ($item['verb']===ACTIVITY_TAG){
                $r = q("SELECT * from `item`,`contact` WHERE 
                `item`.`contact-id`=`contact`.`id` AND `item`.`uri`='%s';",
@@ -173,6 +277,7 @@ function localize_item(&$item){
                                $item['body'] = str_replace($mtch[0],'@[url=' . zrl($mtch[1]). ']',$item['body']);
                }
        }
+
        // add zrl's to public images
        if(preg_match_all('/\[url=(.*?)\/photos\/(.*?)\/image\/(.*?)\]\[img(.*?)\]h(.*?)\[\/img\]\[\/url\]/is',$item['body'],$matches,PREG_SET_ORDER)) {
                foreach($matches as $mtch) {
@@ -180,6 +285,17 @@ function localize_item(&$item){
                }
        }
 
+       // add sparkle links to appropriate permalinks
+
+       $x = stristr($item['plink'],'/display/');
+       if($x) {
+               $sparkle = false;
+               $y = best_link_url($item,$sparkle,true);
+               if(strstr($y,'/redir/'))
+                       $item['plink'] = $y . '?f=&url=' . $item['plink'];
+       } 
+
+
 
 }
 
@@ -427,12 +543,12 @@ function conversation(&$a, $items, $mode, $update, $preview = false) {
                                // We've already parsed out like/dislike for special treatment. We can ignore them now
 
                                if(((activity_match($item['verb'],ACTIVITY_LIKE)) 
-                                       || (activity_match($item['verb'],ACTIVITY_DISLIKE)))) 
-//                                     && ($item['id'] != $item['parent']))
+                                       || (activity_match($item['verb'],ACTIVITY_DISLIKE))) 
+                                       && ($item['id'] != $item['parent']))
                                        continue;
 
                                $toplevelpost = (($item['id'] == $item['parent']) ? true : false);
-                               $toplevelprivate = false;
+
 
                                // Take care of author collapsing and comment collapsing
                                // (author collapsing is currently disabled)
@@ -440,7 +556,7 @@ function conversation(&$a, $items, $mode, $update, $preview = false) {
                                // If there are more than two comments, squash all but the last 2.
                        
                                if($toplevelpost) {
-                                       $toplevelprivate = (($toplevelpost && $item['private']) ? true : false);
+
                                        $item_writeable = (($item['writable'] || $item['self']) ? true : false);
 
                                        $comments_seen = 0;
@@ -485,7 +601,7 @@ function conversation(&$a, $items, $mode, $update, $preview = false) {
 
                                $redirect_url = $a->get_baseurl($ssl_state) . '/redir/' . $item['cid'] ;
 
-                               $lock = ((($item['private']) || (($item['uid'] == local_user()) && (strlen($item['allow_cid']) || strlen($item['allow_gid']) 
+                               $lock = ((($item['private'] == 1) || (($item['uid'] == local_user()) && (strlen($item['allow_cid']) || strlen($item['allow_gid']) 
                                        || strlen($item['deny_cid']) || strlen($item['deny_gid']))))
                                        ? t('Private Message')
                                        : false);
@@ -546,7 +662,7 @@ function conversation(&$a, $items, $mode, $update, $preview = false) {
                                }
 
                                $likebuttons = '';
-                               $shareable = ((($profile_owner == local_user()) &&  ((! $item['private']) || $item['network'] === NETWORK_FEED)) ? true : false); 
+                               $shareable = ((($profile_owner == local_user()) && ($item['private'] != 1)) ? true : false); 
 
                                if($page_writeable) {
 /*                                     if($toplevelpost) {  */
@@ -699,7 +815,7 @@ function conversation(&$a, $items, $mode, $update, $preview = false) {
                                        'text' => strip_tags(template_escape($body)),
                                        'id' => $item['item_id'],
                                        'linktitle' => sprintf( t('View %s\'s profile @ %s'), $profile_name, ((strlen($item['author-link'])) ? $item['author-link'] : $item['url'])),
-                                       'olinktitle' => sprintf( t('View %s\'s profile @ %s'), $profile_name, ((strlen($item['owner-link'])) ? $item['owner-link'] : $item['url'])),
+                                       'olinktitle' => sprintf( t('View %s\'s profile @ %s'), $owner-name, ((strlen($item['owner-link'])) ? $item['owner-link'] : $item['url'])),
                                        'to' => t('to'),
                                        'wall' => t('Wall-to-Wall'),
                                        'vwall' => t('via Wall-To-Wall:'),
@@ -794,6 +910,7 @@ function item_photo_menu($item){
                 if(! count($a->contacts))
                        load_contact_links(local_user());
        }
+       $poke_link="";
        $contact_url="";
        $pm_url="";
        $status_link="";
@@ -823,6 +940,7 @@ function item_photo_menu($item){
                }
        }
        if(($cid) && (! $item['self'])) {
+               $poke_link = $a->get_baseurl($ssl_state) . '/poke/?f=&c=' . $cid;
                $contact_url = $a->get_baseurl($ssl_state) . '/contacts/' . $cid;
                $posts_link = $a->get_baseurl($ssl_state) . '/network/?cid=' . $cid;
 
@@ -845,6 +963,7 @@ function item_photo_menu($item){
                t("Network Posts") => $posts_link, 
                t("Edit Contact") => $contact_url,
                t("Send PM") => $pm_url,
+               t("Poke") => $poke_link
        );
        
        
@@ -856,7 +975,7 @@ function item_photo_menu($item){
 
        $o = "";
        foreach($menu as $k=>$v){
-               if ($v!="") $o .= "<li><a href='$v'>$k</a></li>\n";
+               if ($v!="") $o .= "<li><a href=\"$v\">$k</a></li>\n";
        }
        return $o;
 }}