]> git.mxchange.org Git - friendica.git/blobdiff - include/conversation.php
Mostly some checks in order to avoid Notices; 1 real bugfix in /mod/network.php
[friendica.git] / include / conversation.php
old mode 100644 (file)
new mode 100755 (executable)
index fae57c5..bd9b11b
@@ -4,6 +4,33 @@
  * Render actions localized
  */
 function localize_item(&$item){
+
+       $Text = $item['body'];
+
+
+       // find private image (w/data url) if present and convert image 
+       // link to a magic-auth redirect.
+
+       $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);
+       }
+
        $xmlhead="<"."?xml version='1.0' encoding='UTF-8' ?".">";
        if ($item['verb']=== ACTIVITY_LIKE || $item['verb']=== ACTIVITY_DISLIKE){
 
@@ -116,6 +143,34 @@ function localize_item(&$item){
                $item['body'] = sprintf( t('%1$s tagged %2$s\'s %3$s with %4$s'), $author, $objauthor, $plink, $tag );
                
        }
+       if ($item['verb']=== ACTIVITY_FAVORITE){
+
+               if ($item['object-type']== "")
+                       return;
+
+               $Aname = $item['author-name'];
+               $Alink = $item['author-link'];
+               
+               $xmlhead="<"."?xml version='1.0' encoding='UTF-8' ?".">";
+               
+               $obj = parse_xml_string($xmlhead.$item['object']);
+               if(strlen($obj->id)) {
+                       $r = q("select * from item where uri = '%s' and uid = %d limit 1",
+                                       dbesc($obj->id),
+                                       intval($item['uid'])
+                       );
+                       if(count($r) && $r[0]['plink']) {
+                               $target = $r[0];
+                               $Bname = $target['author-name'];
+                               $Blink = $target['author-link'];
+                               $A = '[url=' . $Alink . ']' . $Aname . '[/url]';
+                               $B = '[url=' . $Blink . ']' . $Bname . '[/url]';
+                               $P = '[url=' . $target['plink'] . ']' . t('post/item') . '[/url]';
+                               $item['body'] = sprintf( t('%1$s marked %2$s\'s %3$s as favorite'), $A, $B, $P)."\n";
+
+                       }
+               }
+       }
 
 }
 
@@ -129,13 +184,15 @@ function localize_item(&$item){
  * that are based on unique features of the calling module.
  *
  */
-function conversation(&$a, $items, $mode, $update) {
+function conversation(&$a, $items, $mode, $update, $preview = false) {
 
        require_once('bbcode.php');
 
        $profile_owner = 0;
        $page_writeable      = false;
 
+       $previewing = (($preview) ? ' preview ' : '');
+
        if($mode === 'network') {
                $profile_owner = local_user();
                $page_writeable = true;
@@ -168,6 +225,10 @@ function conversation(&$a, $items, $mode, $update) {
 
        load_contact_links(local_user());
 
+       $cb = array('items' => $items, 'mode' => $mode, 'update' => $update, 'preview' => $preview);
+       call_hooks('conversation_start',$cb);
+
+       $items = $cb['items'];
 
        $cmnt_tpl    = get_markup_template('comment_item.tpl');
        $tpl         = get_markup_template('wall_item.tpl');
@@ -176,7 +237,7 @@ function conversation(&$a, $items, $mode, $update) {
 
        $alike = array();
        $dlike = array();
-       
+       $o = "";
        
        // array with html for each thread (parent+comments)
        $threads = array();
@@ -228,15 +289,10 @@ function conversation(&$a, $items, $mode, $update) {
                                else
                                        $profile_avatar = ((strlen($item['author-avatar'])) ? $item['author-avatar'] : $item['thumb']);
 
-                               $location = (($item['location']) ? '<a target="map" title="' . $item['location'] . '" href="http://maps.google.com/?q=' . urlencode($item['location']) . '">' . $item['location'] . '</a>' : '');
-                               $coord = (($item['coord']) ? '<a target="map" title="' . $item['coord'] . '" href="http://maps.google.com/?q=' . urlencode($item['coord']) . '">' . $item['coord'] . '</a>' : '');
-                               if($coord) {
-                                       if($location)
-                                               $location .= '<br /><span class="smalltext">(' . $coord . ')</span>';
-                                       else
-                                               $location = '<span class="smalltext">' . $coord . '</span>';
-                               }
+                               $locate = array('location' => $item['location'], 'coord' => $item['coord'], 'html' => '');
+                               call_hooks('render_location',$locate);
 
+                               $location = ((strlen($locate['html'])) ? $locate['html'] : render_location_google($locate));
 
                                localize_item($item);
                                if($mode === 'network-new')
@@ -261,7 +317,7 @@ function conversation(&$a, $items, $mode, $update) {
                                $body = prepare_body($item,true);
                                
                                $tmp_item = replace_macros($tpl,array(
-                                       '$id' => $item['item_id'],
+                                       '$id' => (($preview) ? 'P0' : $item['item_id']),
                                        '$linktitle' => sprintf( t('View %s\'s profile @ %s'), $profile_name, ((strlen($item['author-link'])) ? $item['author-link'] : $item['url'])),
                                        '$profile_url' => $profile_link,
                                        '$item_photo_menu' => item_photo_menu($item),
@@ -287,7 +343,8 @@ function conversation(&$a, $items, $mode, $update) {
                                        '$like' => '',
                                        '$dislike' => '',
                                        '$comment' => '',
-                                       '$conv' => array('href'=> $a->get_baseurl() . '/display/' . $nickname . '/' . $item['id'], 'title'=> t('View in context')),
+                                       '$conv' => (($preview) ? '' : array('href'=> $a->get_baseurl() . '/display/' . $nickname . '/' . $item['id'], 'title'=> t('View in context'))),
+                                       '$previewing' => $previewing,
                                        '$wait' => t('Please wait'),
                                ));
 
@@ -316,7 +373,8 @@ function conversation(&$a, $items, $mode, $update) {
                                                $comments[$item['parent']] = 1;
                                        else
                                                $comments[$item['parent']] += 1;
-                               }
+                               } elseif(! x($comments,$item['parent'])) 
+                                       $comments[$item['parent']] = 0; // avoid notices later on
                        }
 
                        // map all the like/dislike activities for each parent item 
@@ -351,6 +409,7 @@ function conversation(&$a, $items, $mode, $update) {
                                $toplevelprivate = false;
 
                                // Take care of author collapsing and comment collapsing
+                               // (author collapsing is currently disabled)
                                // If a single author has more than 3 consecutive top-level posts, squash the remaining ones.
                                // If there are more than two comments, squash all but the last 2.
                        
@@ -358,7 +417,9 @@ function conversation(&$a, $items, $mode, $update) {
                                        $toplevelprivate = (($toplevelpost && $item['private']) ? true : false);
                                        $item_writeable = (($item['writable'] || $item['self']) ? true : false);
 
-                                       /*if($blowhard == $item['cid'] && (! $item['self']) && ($mode != 'profile') && ($mode != 'notes')) {
+                                       // DISABLED
+                                       /*
+                                       if($blowhard == $item['cid'] && (! $item['self']) && ($mode != 'profile') && ($mode != 'notes')) {
                                                $blowhard_count ++;
                                                if($blowhard_count == 3) {
                                                        $o .= '<div class="icollapse-wrapper fakelink" id="icollapse-wrapper-' . $item['parent'] 
@@ -372,7 +433,9 @@ function conversation(&$a, $items, $mode, $update) {
                                                if($blowhard_count >= 3)
                                                        $o .= '</div>';
                                                $blowhard_count = 0;
-                                       }*/
+                                       }
+                                       // END DISABLED
+                                       */
 
                                        $comments_seen = 0;
                                        $comments_collapsed = false;
@@ -384,7 +447,7 @@ function conversation(&$a, $items, $mode, $update) {
                                }
                                else {
                                        // prevent private email from leaking into public conversation
-                                       if((! $toplevelpost) && (! toplevelprivate) && ($item['private']) && ($profile_owner != local_user()))
+                                       if((! $toplevelpost) && (! $toplevelprivate) && ($item['private']) && ($profile_owner != local_user()))
                                                continue;
                                        $comments_seen ++;
                                }       
@@ -459,7 +522,7 @@ function conversation(&$a, $items, $mode, $update) {
                                }
 
                                $likebuttons = '';
-                               $shareable = ((($profile_owner == local_user()) && ($mode != 'display') && (! $item['private'])) ? true : false);
+                               $shareable = ((($profile_owner == local_user()) &&  (! $item['private'])) ? true : false); //($mode != 'display') &&
 
                                if($page_writeable) {
                                        if($toplevelpost) {
@@ -470,6 +533,10 @@ function conversation(&$a, $items, $mode, $update) {
                                                if ($shareable) $likebuttons['share'] = array( t('Share this'), t('share'));
                                        }
 
+
+                                       $qc = ((local_user()) ? get_pconfig(local_user(),'qcomment','words') : null);
+                                       $qcomment = (($qc) ? explode("\n",$qc) : null);
+
                                        if(($show_comment_box) || (($show_comment_box == false) && ($override_comment_box == false) && ($item['last-child']))) {
                                                $comment = replace_macros($cmnt_tpl,array(
                                                        '$return_path' => '', 
@@ -477,12 +544,14 @@ function conversation(&$a, $items, $mode, $update) {
                                                        '$type' => (($mode === 'profile') ? 'wall-comment' : 'net-comment'),
                                                        '$id' => $item['item_id'],
                                                        '$parent' => $item['parent'],
+                                                       '$qcomment' => $qcomment,
                                                        '$profile_uid' =>  $profile_owner,
                                                        '$mylink' => $a->contact['url'],
                                                        '$mytitle' => t('This is you'),
                                                        '$myphoto' => $a->contact['thumb'],
                                                        '$comment' => t('Comment'),
                                                        '$submit' => t('Submit'),
+                                                       '$preview' => t('Preview'),
                                                        '$ww' => (($mode === 'network') ? $commentww : '')
                                                ));
                                        }
@@ -558,16 +627,10 @@ function conversation(&$a, $items, $mode, $update) {
                                $like    = ((x($alike,$item['id'])) ? format_like($alike[$item['id']],$alike[$item['id'] . '-l'],'like',$item['id']) : '');
                                $dislike = ((x($dlike,$item['id'])) ? format_like($dlike[$item['id']],$dlike[$item['id'] . '-l'],'dislike',$item['id']) : '');
 
-                               $location = (($item['location']) ? '<a target="map" title="' . $item['location'] 
-                                       . '" href="http://maps.google.com/?q=' . urlencode($item['location']) . '">' . $item['location'] . '</a>' : '');
-                               $coord = (($item['coord']) ? '<a target="map" title="' . $item['coord'] 
-                                       . '" href="http://maps.google.com/?q=' . urlencode($item['coord']) . '">' . $item['coord'] . '</a>' : '');
-                               if($coord) {
-                                       if($location)
-                                               $location .= '<br /><span class="smalltext">(' . $coord . ')</span>';
-                                       else
-                                               $location = '<span class="smalltext">' . $coord . '</span>';
-                               }
+                               $locate = array('location' => $item['location'], 'coord' => $item['coord'], 'html' => '');
+                               call_hooks('render_location',$locate);
+
+                               $location = ((strlen($locate['html'])) ? $locate['html'] : render_location_google($locate));
 
                                $indent = (($toplevelpost) ? '' : ' comment');
 
@@ -584,14 +647,12 @@ function conversation(&$a, $items, $mode, $update) {
                                        if ($tag!="") $tags[] = bbcode($tag);
                                }
 
-
                                // Build the HTML
 
                                $body = prepare_body($item,true);
-                               
 
                                $tmp_item = replace_macros($template,array(
-                                       '$type' => implode("",array_slice(split("/",$item['verb']),-1)),
+                                       '$type' => implode("",array_slice(explode("/",$item['verb']),-1)),
                                        '$tags' => $tags,
                                        '$body' => template_escape($body),
                                        '$id' => $item['item_id'],
@@ -623,6 +684,7 @@ function conversation(&$a, $items, $mode, $update) {
                                        '$like' => $like,
                                        '$dislike' => $dislike,
                                        '$comment' => $comment,
+                                       '$previewing' => $previewing,
                                        '$wait' => t('Please wait'),
 
                                ));
@@ -724,10 +786,12 @@ function item_photo_menu($item){
        );
        
        
-       $args = array($item, &$menu);
+       $args = array('item' => $item, 'menu' => $menu);
        
        call_hooks('item_photo_menu', $args);
-       
+
+       $menu = $args['menu'];  
+
        $o = "";
        foreach($menu as $k=>$v){
                if ($v!="") $o .= "<li><a href='$v'>$k</a></li>\n";
@@ -793,94 +857,199 @@ function format_like($cnt,$arr,$type,$id) {
 }}
 
 
-function status_editor($a,$x, $notes_cid = 0) {
+function status_editor($a,$x, $notes_cid = 0, $popup=false) {
 
        $o = '';
                
        $geotag = (($x['allow_location']) ? get_markup_template('jot_geotag.tpl') : '');
 
-               $tpl = get_markup_template('jot-header.tpl');
+       $plaintext = false;
+       if(local_user() && intval(get_pconfig(local_user(),'system','plaintext')))
+               $plaintext = true;
+
+       $tpl = get_markup_template('jot-header.tpl');
        
-               $a->page['htmlhead'] .= replace_macros($tpl, array(
-                       '$newpost' => 'true',
-                       '$baseurl' => $a->get_baseurl(),
-                       '$geotag' => $geotag,
-                       '$nickname' => $x['nickname'],
-                       '$ispublic' => t('Visible to <strong>everybody</strong>'),
-                       '$linkurl' => t('Please enter a link URL:'),
-                       '$vidurl' => t("Please enter a video link/URL:"),
-                       '$audurl' => t("Please enter an audio link/URL:"),
-                       '$term' => t('Tag term:'),
-                       '$whereareu' => t('Where are you right now?'),
-                       '$title' => t('Enter a title for this item') 
-               ));
-
-
-               $tpl = get_markup_template("jot.tpl");
+       $a->page['htmlhead'] .= replace_macros($tpl, array(
+               '$newpost' => 'true',
+               '$baseurl' => $a->get_baseurl(),
+               '$editselect' => (($plaintext) ? 'none' : '/(profile-jot-text|prvmail-text)/'),
+               '$geotag' => $geotag,
+               '$nickname' => $x['nickname'],
+               '$ispublic' => t('Visible to <strong>everybody</strong>'),
+               '$linkurl' => t('Please enter a link URL:'),
+               '$vidurl' => t("Please enter a video link/URL:"),
+               '$audurl' => t("Please enter an audio link/URL:"),
+               '$term' => t('Tag term:'),
+               '$whereareu' => t('Where are you right now?'),
+               '$title' => t('Enter a title for this item') 
+       ));
+
+
+       $tpl = get_markup_template("jot.tpl");
                
-               $jotplugins = '';
-               $jotnets = '';
+       $jotplugins = '';
+       $jotnets = '';
+
+       $mail_disabled = ((function_exists('imap_open') && (! get_config('system','imap_disabled'))) ? 0 : 1);
+
+       $mail_enabled = false;
+       $pubmail_enabled = false;
+
+       if(($x['is_owner']) && (! $mail_disabled)) {
+               $r = q("SELECT * FROM `mailacct` WHERE `uid` = %d AND `server` != '' LIMIT 1",
+                       intval(local_user())
+               );
+               if(count($r)) {
+                       $mail_enabled = true;
+                       if(intval($r[0]['pubmail']))
+                               $pubmail_enabled = true;
+               }
+       }
 
-               $mail_disabled = ((function_exists('imap_open') && (! get_config('system','imap_disabled'))) ? 0 : 1);
+       if($mail_enabled) {
+               $selected = (($pubmail_enabled) ? ' checked="checked" ' : '');
+               $jotnets .= '<div class="profile-jot-net"><input type="checkbox" name="pubmail_enable"' . $selected . ' value="1" /> ' . t("Post to Email") . '</div>';
+       }
 
-               $mail_enabled = false;
-               $pubmail_enabled = false;
+       call_hooks('jot_tool', $jotplugins);
+       call_hooks('jot_networks', $jotnets);
+
+       if($notes_cid)
+               $jotnets .= '<input type="hidden" name="contact_allow[]" value="' . $notes_cid .'" />';
+
+       $tpl = replace_macros($tpl,array('$jotplugins' => $jotplugins));        
+
+       $o .= replace_macros($tpl,array(
+               '$return_path' => $a->cmd,
+               '$action' =>  $a->get_baseurl().'/item',
+               '$share' => (x($x,'button') ? $x['button'] : t('Share')),
+               '$upload' => t('Upload photo'),
+               '$shortupload' => t('upload photo'),
+               '$attach' => t('Attach file'),
+               '$shortattach' => t('attach file'),
+               '$weblink' => t('Insert web link'),
+               '$shortweblink' => t('web link'),
+               '$video' => t('Insert video link'),
+               '$shortvideo' => t('video link'),
+               '$audio' => t('Insert audio link'),
+               '$shortaudio' => t('audio link'),
+               '$setloc' => t('Set your location'),
+               '$shortsetloc' => t('set location'),
+               '$noloc' => t('Clear browser location'),
+               '$shortnoloc' => t('clear location'),
+               '$title' => "",
+               '$placeholdertitle' => t('Set title'),
+               '$wait' => t('Please wait'),
+               '$permset' => t('Permission settings'),
+               '$shortpermset' => t('permissions'),
+               '$ptyp' => (($notes_cid) ? 'note' : 'wall'),
+               '$content' => '',
+               '$post_id' => '',
+               '$baseurl' => $a->get_baseurl(),
+               '$defloc' => $x['default_location'],
+               '$visitor' => $x['visitor'],
+               '$pvisit' => (($notes_cid) ? 'none' : $x['visitor']),
+               '$emailcc' => t('CC: email addresses'),
+               '$public' => t('Public post'),
+               '$jotnets' => $jotnets,
+               '$emtitle' => t('Example: bob@example.com, mary@example.com'),
+               '$lockstate' => $x['lockstate'],
+               '$acl' => $x['acl'],
+               '$bang' => $x['bang'],
+               '$profile_uid' => $x['profile_uid'],
+               '$preview' => t('Preview'),
+       ));
 
-               if(($x['is_owner']) && (! $mail_disabled)) {
-                       $r = q("SELECT * FROM `mailacct` WHERE `uid` = %d AND `server` != '' LIMIT 1",
-                               intval(local_user())
-                       );
-                       if(count($r)) {
-                               $mail_enabled = true;
-                               if(intval($r[0]['pubmail']))
-                                       $pubmail_enabled = true;
-                       }
+
+       if ($popup==true){
+               $o = '<div id="jot-popup" style="display: none;">'.$o.'</div>';
+               
+       }
+
+       return $o;
+}
+
+
+function conv_sort($arr,$order) {
+
+       if((!(is_array($arr) && count($arr))))
+               return array();
+
+       $parents = array();
+
+       foreach($arr as $x)
+               if($x['id'] == $x['parent'])
+                               $parents[] = $x;
+
+       if(stristr($order,'created'))
+               usort($parents,'sort_thr_created');
+       elseif(stristr($order,'commented'))
+               usort($parents,'sort_thr_commented');
+
+       if(count($parents))
+               foreach($parents as $i=>$_x) 
+                       $parents[$i]['children'] = array();
+
+       foreach($arr as $x) {
+               if($x['id'] != $x['parent']) {
+                       $p = find_thread_parent_index($parents,$x);
+                       if($p !== false)
+                               $parents[$p]['children'][] = $x;
                }
+       }
+       if(count($parents)) {
+               foreach($parents as $k => $v) {
+                       if(count($parents[$k]['children'])) {
+                               $y = $parents[$k]['children'];
+                               usort($y,'sort_thr_created_rev');
+                               $parents[$k]['children'] = $y;
+                       }
+               }       
+       }
 
-               if($mail_enabled) {
-              $selected = (($pubmail_enabled) ? ' checked="checked" ' : '');
-                       $jotnets .= '<div class="profile-jot-net"><input type="checkbox" name="pubmail_enable"' . $selected . ' value="1" /> '
-               . t("Post to Email") . '</div>';
+       $ret = array();
+       if(count($parents)) {
+               foreach($parents as $x) {
+                       $ret[] = $x;
+                       if(count($x['children']))
+                               foreach($x['children'] as $y)
+                                       $ret[] = $y;
                }
+       }
 
-               call_hooks('jot_tool', $jotplugins);
-               call_hooks('jot_networks', $jotnets);
-
-               if($notes_cid)
-                       $jotnets .= '<input type="hidden" name="contact_allow[]" value="' . $notes_cid .'" />';
-
-               $tpl = replace_macros($tpl,array('$jotplugins' => $jotplugins));        
-
-               $o .= replace_macros($tpl,array(
-                       '$return_path' => $a->cmd,
-                       '$action' => 'item',
-                       '$share' => (($x['button']) ? $x['button'] : t('Share')),
-                       '$upload' => t('Upload photo'),
-                       '$attach' => t('Attach file'),
-                       '$weblink' => t('Insert web link'),
-                       '$video' => t('Insert video link'),
-                       '$audio' => t('Insert audio link'),
-                       '$setloc' => t('Set your location'),
-                       '$noloc' => t('Clear browser location'),
-                       '$title' => t('Set title'),
-                       '$wait' => t('Please wait'),
-                       '$permset' => t('Permission settings'),
-                       '$ptyp' => (($notes_cid) ? 'note' : 'wall'),
-                       '$content' => '',
-                       '$post_id' => '',
-                       '$baseurl' => $a->get_baseurl(),
-                       '$defloc' => $x['default_location'],
-                       '$visitor' => $x['visitor'],
-                       '$pvisit' => (($notes_cid) ? 'none' : $x['visitor']),
-                       '$emailcc' => t('CC: email addresses'),
-                       '$public' => t('Public post'),
-                       '$jotnets' => $jotnets,
-                       '$emtitle' => t('Example: bob@example.com, mary@example.com'),
-                       '$lockstate' => $x['lockstate'],
-                       '$acl' => $x['acl'],
-                       '$bang' => $x['bang'],
-                       '$profile_uid' => $x['profile_uid'],
-               ));
+       return $ret;
+}
 
-       return $o;
+
+function sort_thr_created($a,$b) {
+       return strcmp($b['created'],$a['created']);
+}
+
+function sort_thr_created_rev($a,$b) {
+       return strcmp($a['created'],$b['created']);
+}
+
+function sort_thr_commented($a,$b) {
+       return strcmp($b['commented'],$a['commented']);
+}
+
+function find_thread_parent_index($arr,$x) {
+       foreach($arr as $k => $v)
+               if($v['id'] == $x['parent'])
+                       return $k;
+       return false;
 }
+
+function render_location_google($item) {
+       $location = '';
+       $location = (($item['location']) ? '<a target="map" title="' . $item['location'] . '" href="http://maps.google.com/?q=' . urlencode($item['location']) . '">' . $item['location'] . '</a>' : '');
+       $coord = (($item['coord']) ? '<a target="map" title="' . $item['coord'] . '" href="http://maps.google.com/?q=' . urlencode($item['coord']) . '">' . $item['coord'] . '</a>' : '');
+       if($coord) {
+               if($location)
+                       $location .= '<br /><span class="smalltext">(' . $coord . ')</span>';
+               else
+                       $location = '<span class="smalltext">' . $coord . '</span>';
+       }
+       return $location;
+}
+