]> git.mxchange.org Git - friendica.git/blobdiff - include/conversation.php
Fix autolink regular expression to include accents
[friendica.git] / include / conversation.php
index 020eca18418ffaf8cda8f0110f4b793044e2fa3b..a6edf0d577305ecc7471dac77baa493be2996be6 100644 (file)
@@ -3,12 +3,14 @@
  * @file include/conversation.php
  */
 use Friendica\App;
+use Friendica\Content\ContactSelector;
 use Friendica\Content\Feature;
 use Friendica\Core\Config;
 use Friendica\Core\PConfig;
 use Friendica\Core\System;
 use Friendica\Database\DBM;
 use Friendica\Model\Contact;
+use Friendica\Model\Profile;
 use Friendica\Object\Thread;
 use Friendica\Object\Post;
 
@@ -17,7 +19,7 @@ require_once "include/acl_selectors.php";
 
 function item_extract_images($body) {
 
-       $saved_image = array();
+       $saved_image = [];
        $orig_body = $body;
        $new_body = '';
 
@@ -55,7 +57,7 @@ function item_extract_images($body) {
 
        $new_body = $new_body . $orig_body;
 
-       return array('body' => $new_body, 'images' => $saved_image);
+       return ['body' => $new_body, 'images' => $saved_image];
 }
 
 function item_redir_and_replace_images($body, $images, $cid) {
@@ -143,7 +145,7 @@ function localize_item(&$item) {
                        default:
                                if ($obj['resource-id']) {
                                        $post_type = t('photo');
-                                       $m = array();
+                                       $m = [];
                                        preg_match("/\[url=([^]]*)\]/", $obj['body'], $m);
                                        $rr['plink'] = $m[1];
                                } else {
@@ -193,10 +195,10 @@ function localize_item(&$item) {
                        }
                }
 
-               $A = '[url=' . zrl($Alink) . ']' . $Aname . '[/url]';
-               $B = '[url=' . zrl($Blink) . ']' . $Bname . '[/url]';
+               $A = '[url=' . Profile::zrl($Alink) . ']' . $Aname . '[/url]';
+               $B = '[url=' . Profile::zrl($Blink) . ']' . $Bname . '[/url]';
                if ($Bphoto != "") {
-                       $Bphoto = '[url=' . zrl($Blink) . '][img]' . $Bphoto . '[/img][/url]';
+                       $Bphoto = '[url=' . Profile::zrl($Blink) . '][img]' . $Bphoto . '[/img][/url]';
                }
 
                $item['body'] = sprintf( t('%1$s is now friends with %2$s'), $A, $B)."\n\n\n".$Bphoto;
@@ -230,10 +232,10 @@ function localize_item(&$item) {
                        }
                }
 
-               $A = '[url=' . zrl($Alink) . ']' . $Aname . '[/url]';
-               $B = '[url=' . zrl($Blink) . ']' . $Bname . '[/url]';
+               $A = '[url=' . Profile::zrl($Alink) . ']' . $Aname . '[/url]';
+               $B = '[url=' . Profile::zrl($Blink) . ']' . $Bname . '[/url]';
                if ($Bphoto != "") {
-                       $Bphoto = '[url=' . zrl($Blink) . '][img=80x80]' . $Bphoto . '[/img][/url]';
+                       $Bphoto = '[url=' . Profile::zrl($Blink) . '][img=80x80]' . $Bphoto . '[/img][/url]';
                }
 
                /*
@@ -265,8 +267,8 @@ function localize_item(&$item) {
 
                $obj = $r[0];
 
-               $author  = '[url=' . zrl($item['author-link']) . ']' . $item['author-name'] . '[/url]';
-               $objauthor =  '[url=' . zrl($obj['author-link']) . ']' . $obj['author-name'] . '[/url]';
+               $author  = '[url=' . Profile::zrl($item['author-link']) . ']' . $item['author-name'] . '[/url]';
+               $objauthor =  '[url=' . Profile::zrl($obj['author-link']) . ']' . $obj['author-name'] . '[/url]';
 
                switch ($obj['verb']) {
                        case ACTIVITY_POST:
@@ -281,7 +283,7 @@ function localize_item(&$item) {
                        default:
                                if ($obj['resource-id']) {
                                        $post_type = t('photo');
-                                       $m=array(); preg_match("/\[url=([^]]*)\]/", $obj['body'], $m);
+                                       $m=[]; preg_match("/\[url=([^]]*)\]/", $obj['body'], $m);
                                        $rr['plink'] = $m[1];
                                } else {
                                        $post_type = t('status');
@@ -319,8 +321,8 @@ function localize_item(&$item) {
                                $target = $r[0];
                                $Bname = $target['author-name'];
                                $Blink = $target['author-link'];
-                               $A = '[url=' . zrl($Alink) . ']' . $Aname . '[/url]';
-                               $B = '[url=' . zrl($Blink) . ']' . $Bname . '[/url]';
+                               $A = '[url=' . Profile::zrl($Alink) . ']' . $Aname . '[/url]';
+                               $B = '[url=' . Profile::zrl($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";
                        }
@@ -330,7 +332,7 @@ function localize_item(&$item) {
        if (preg_match_all('/@\[url=(.*?)\]/is', $item['body'], $matches, PREG_SET_ORDER)) {
                foreach ($matches as $mtch) {
                        if (! strpos($mtch[1], 'zrl=')) {
-                               $item['body'] = str_replace($mtch[0], '@[url=' . zrl($mtch[1]) . ']', $item['body']);
+                               $item['body'] = str_replace($mtch[0], '@[url=' . Profile::zrl($mtch[1]) . ']', $item['body']);
                        }
                }
        }
@@ -338,7 +340,7 @@ function localize_item(&$item) {
        // add zrl's to public images
        $photo_pattern = "/\[url=(.*?)\/photos\/(.*?)\/image\/(.*?)\]\[img(.*?)\]h(.*?)\[\/img\]\[\/url\]/is";
        if (preg_match($photo_pattern, $item['body'])) {
-               $photo_replace = '[url=' . zrl('$1' . '/photos/' . '$2' . '/image/' . '$3' ,true) . '][img' . '$4' . ']h' . '$5'  . '[/img][/url]';
+               $photo_replace = '[url=' . Profile::zrl('$1' . '/photos/' . '$2' . '/image/' . '$3' ,true) . '][img' . '$4' . ']h' . '$5'  . '[/img][/url]';
                $item['body'] = bb_tag_preg_replace($photo_pattern, $photo_replace, 'url', $item['body']);
        }
 
@@ -380,7 +382,7 @@ function visible_activity($item) {
         * likes (etc.) can apply to other things besides posts. Check if they are post children,
         * in which case we handle them specially
         */
-       $hidden_activities = array(ACTIVITY_LIKE, ACTIVITY_DISLIKE, ACTIVITY_ATTEND, ACTIVITY_ATTENDNO, ACTIVITY_ATTENDMAYBE);
+       $hidden_activities = [ACTIVITY_LIKE, ACTIVITY_DISLIKE, ACTIVITY_ATTEND, ACTIVITY_ATTENDNO, ACTIVITY_ATTENDMAYBE];
        foreach ($hidden_activities as $act) {
                if (activity_match($item['verb'], $act)) {
                        return false;
@@ -428,7 +430,6 @@ These Fields are not added below (yet). They are here to for bug search.
 `item`.`deleted`,
 `item`.`origin`,
 `item`.`forum_mode`,
-`item`.`last-child`,
 `item`.`mention`,
 `item`.`global`,
 `item`.`gcontact-id`,
@@ -593,18 +594,18 @@ function conversation(App $a, $items, $mode, $update, $preview = false) {
                $_SESSION['return_url'] = $a->query_string;
        }
 
-       $cb = array('items' => $items, 'mode' => $mode, 'update' => $update, 'preview' => $preview);
+       $cb = ['items' => $items, 'mode' => $mode, 'update' => $update, 'preview' => $preview];
        call_hooks('conversation_start',$cb);
 
        $items = $cb['items'];
 
-       $conv_responses = array(
-               'like' => array('title' => t('Likes','title')), 'dislike' => array('title' => t('Dislikes','title')),
-               'attendyes' => array('title' => t('Attending','title')), 'attendno' => array('title' => t('Not attending','title')), 'attendmaybe' => array('title' => t('Might attend','title'))
-       );
+       $conv_responses = [
+               'like' => ['title' => t('Likes','title')], 'dislike' => ['title' => t('Dislikes','title')],
+               'attendyes' => ['title' => t('Attending','title')], 'attendno' => ['title' => t('Not attending','title')], 'attendmaybe' => ['title' => t('Might attend','title')]
+       ];
 
        // array with html for each thread (parent+comments)
-       $threads = array();
+       $threads = [];
        $threadsid = -1;
 
        $page_template = get_markup_template("conversation.tpl");
@@ -618,7 +619,7 @@ function conversation(App $a, $items, $mode, $update, $preview = false) {
                                $community_readonly = false;
                                $writable = true;
                        } else {
-                               $writable = ($items[0]['uid'] == 0) && in_array($items[0]['network'], array(NETWORK_OSTATUS, NETWORK_DIASPORA, NETWORK_DFRN));
+                               $writable = ($items[0]['uid'] == 0) && in_array($items[0]['network'], [NETWORK_OSTATUS, NETWORK_DIASPORA, NETWORK_DFRN]);
                        }
                } else {
                        $writable = false;
@@ -628,7 +629,7 @@ function conversation(App $a, $items, $mode, $update, $preview = false) {
                        $writable = false;
                }
 
-               if ($mode === 'network-new' || $mode === 'search' || $community_readonly) {
+               if (in_array($mode, ['network-new', 'search', 'contact-posts']) || $community_readonly) {
 
                        /*
                         * "New Item View" on network page or search page results
@@ -671,15 +672,15 @@ function conversation(App $a, $items, $mode, $update, $preview = false) {
                                        $profile_name = $item['author-link'];
                                }
 
-                               $tags = array();
-                               $hashtags = array();
-                               $mentions = array();
+                               $tags = [];
+                               $hashtags = [];
+                               $mentions = [];
 
                                $searchpath = System::baseUrl()."/search?tag=";
 
-                               $taglist = dba::select('term', array('type', 'term', 'url'),
-                                                       array("`otype` = ? AND `oid` = ? AND `type` IN (?, ?)", TERM_OBJ_POST, $item['id'], TERM_HASHTAG, TERM_MENTION),
-                                                       array('order' => array('tid')));
+                               $taglist = dba::select('term', ['type', 'term', 'url'],
+                                                       ["`otype` = ? AND `oid` = ? AND `type` IN (?, ?)", TERM_OBJ_POST, $item['id'], TERM_HASHTAG, TERM_MENTION],
+                                                       ['order' => ['tid']]);
 
                                while ($tag = dba::fetch($taglist)) {
                                        if ($tag["url"] == "") {
@@ -708,7 +709,7 @@ function conversation(App $a, $items, $mode, $update, $preview = false) {
                                if ($sp) {
                                        $sparkle = ' sparkle';
                                } else {
-                                       $profile_link = zrl($profile_link);
+                                       $profile_link = Profile::zrl($profile_link);
                                }
 
                                if (!x($item, 'author-thumb') || ($item['author-thumb'] == "")) {
@@ -729,7 +730,7 @@ function conversation(App $a, $items, $mode, $update, $preview = false) {
                                        }
                                }
 
-                               $locate = array('location' => $item['location'], 'coord' => $item['coord'], 'html' => '');
+                               $locate = ['location' => $item['location'], 'coord' => $item['coord'], 'html' => ''];
                                call_hooks('render_location',$locate);
 
                                $location = ((strlen($locate['html'])) ? $locate['html'] : render_location_dummy($locate));
@@ -741,12 +742,12 @@ function conversation(App $a, $items, $mode, $update, $preview = false) {
                                        $dropping = false;
                                }
 
-                               $drop = array(
+                               $drop = [
                                        'dropping' => $dropping,
                                        'pagedrop' => $page_dropping,
                                        'select' => t('Select'),
                                        'delete' => t('Delete'),
-                               );
+                               ];
 
                                $star = false;
                                $isstarred = "unstarred";
@@ -771,12 +772,12 @@ function conversation(App $a, $items, $mode, $update, $preview = false) {
                                        $item['item_network'] = $item['network'];
                                }
 
-                               $tmp_item = array(
+                               $tmp_item = [
                                        'template' => $tpl,
                                        'id' => (($preview) ? 'P0' : $item['item_id']),
                                        'guid' => (($preview) ? 'Q0' : $item['guid']),
                                        'network' => $item['item_network'],
-                                       'network_name' => network_to_name($item['item_network'], $profile_link),
+                                       'network_name' => ContactSelector::networkToName($item['item_network'], $profile_link),
                                        '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),
@@ -812,18 +813,18 @@ function conversation(App $a, $items, $mode, $update, $preview = false) {
                                        'like' => '',
                                        'dislike' => '',
                                        'comment' => '',
-                                       'conv' => (($preview) ? '' : array('href'=> 'display/'.$item['guid'], 'title'=> t('View in context'))),
+                                       'conv' => (($preview) ? '' : ['href'=> 'display/'.$item['guid'], 'title'=> t('View in context')]),
                                        'previewing' => $previewing,
                                        'wait' => t('Please wait'),
                                        'thread_level' => 1,
-                               );
+                               ];
 
-                               $arr = array('item' => $item, 'output' => $tmp_item);
+                               $arr = ['item' => $item, 'output' => $tmp_item];
                                call_hooks('display_item', $arr);
 
                                $threads[$threadsid]['id'] = $item['item_id'];
                                $threads[$threadsid]['network'] = $item['item_network'];
-                               $threads[$threadsid]['items'] = array($arr['output']);
+                               $threads[$threadsid]['items'] = [$arr['output']];
 
                        }
                } else {
@@ -876,12 +877,12 @@ function conversation(App $a, $items, $mode, $update, $preview = false) {
                        $threads = $conv->getTemplateData($conv_responses);
                        if (!$threads) {
                                logger('[ERROR] conversation : Failed to get template data.', LOGGER_DEBUG);
-                               $threads = array();
+                               $threads = [];
                        }
                }
        }
 
-       $o = replace_macros($page_template, array(
+       $o = replace_macros($page_template, [
                '$baseurl' => System::baseUrl($ssl_state),
                '$return_path' => $a->query_string,
                '$live_update' => $live_update_div,
@@ -890,7 +891,7 @@ function conversation(App $a, $items, $mode, $update, $preview = false) {
                '$user' => $a->user,
                '$threads' => $threads,
                '$dropping' => ($page_dropping && Feature::isEnabled(local_user(), 'multi_delete') ? t('Delete Selected Items') : False),
-       ));
+       ]);
 
        return $o;
 }
@@ -908,7 +909,7 @@ function conversation(App $a, $items, $mode, $update, $preview = false) {
 function community_add_items($parents) {
        $max_comments = Config::get("system", "max_comments", 100);
 
-       $items = array();
+       $items = [];
 
        foreach ($parents AS $parent) {
                $thread_items = dba::p(item_query()." AND `item`.`uid` = ?
@@ -930,7 +931,7 @@ function community_add_items($parents) {
                                }
                        }
                        if (!$parent_found) {
-                               $comments = array();
+                               $comments = [];
                        }
                }
 
@@ -967,11 +968,15 @@ function best_link_url($item, &$sparkle, $url = '') {
        $clean_url = normalise_link($item['author-link']);
 
        if (local_user()) {
-               $r = dba::select('contact', array('id'),
-                       array('network' => NETWORK_DFRN, 'uid' => local_user(), 'nurl' => normalise_link($clean_url), 'pending' => false),
-                       array('limit' => 1));
-               if (DBM::is_result($r)) {
-                       $best_url = 'redir/' . $r['id'];
+               $condition = [
+                       'network' => NETWORK_DFRN,
+                       'uid' => local_user(),
+                       'nurl' => normalise_link($clean_url),
+                       'pending' => false
+               ];
+               $contact = dba::selectFirst('contact', ['id'], $condition);
+               if (DBM::is_result($contact)) {
+                       $best_url = 'redir/' . $contact['id'];
                        $sparkle = true;
                        if ($url != '') {
                                $hostname = get_app()->get_hostname();
@@ -1019,11 +1024,12 @@ function item_photo_menu($item) {
        $cid = 0;
        $network = '';
        $rel = 0;
-       $r = dba::select('contact', array('id', 'network', 'rel'), array('uid' => local_user(), 'nurl' => normalise_link($item['author-link'])), array('limit' => 1));
-       if (DBM::is_result($r)) {
-               $cid = $r['id'];
-               $network = $r['network'];
-               $rel = $r['rel'];
+       $condition = ['uid' => local_user(), 'nurl' => normalise_link($item['author-link'])];
+       $contact = dba::selectFirst('contact', ['id', 'network', 'rel'], $condition);
+       if (DBM::is_result($contact)) {
+               $cid = $contact['id'];
+               $network = $contact['network'];
+               $rel = $contact['rel'];
        }
 
        if ($sparkle) {
@@ -1031,7 +1037,7 @@ function item_photo_menu($item) {
                $photos_link = $profile_link . '?url=photos';
                $profile_link = $profile_link . '?url=profile';
        } else {
-               $profile_link = zrl($profile_link);
+               $profile_link = Profile::zrl($profile_link);
        }
 
        if ($cid && !$item['self']) {
@@ -1039,13 +1045,13 @@ function item_photo_menu($item) {
                $contact_url = 'contacts/' . $cid;
                $posts_link = 'contacts/' . $cid . '/posts';
 
-               if (in_array($network, array(NETWORK_DFRN, NETWORK_DIASPORA))) {
+               if (in_array($network, [NETWORK_DFRN, NETWORK_DIASPORA])) {
                        $pm_url = 'message/new/' . $cid;
                }
        }
 
        if (local_user()) {
-               $menu = array(
+               $menu = [
                        t('Follow Thread') => $sub_link,
                        t('View Status') => $status_link,
                        t('View Profile') => $profile_link,
@@ -1053,21 +1059,21 @@ function item_photo_menu($item) {
                        t('Network Posts') => $posts_link,
                        t('View Contact') => $contact_url,
                        t('Send PM') => $pm_url
-               );
+               ];
 
                if ($network == NETWORK_DFRN) {
                        $menu[t("Poke")] = $poke_link;
                }
 
                if ((($cid == 0) || ($rel == CONTACT_IS_FOLLOWER)) &&
-                       in_array($item['network'], array(NETWORK_DFRN, NETWORK_OSTATUS, NETWORK_DIASPORA))) {
+                       in_array($item['network'], [NETWORK_DFRN, NETWORK_OSTATUS, NETWORK_DIASPORA])) {
                        $menu[t('Connect/Follow')] = 'follow?url=' . urlencode($item['author-link']);
                }
        } else {
-               $menu = array(t('View Profile') => $item['author-link']);
+               $menu = [t('View Profile') => $item['author-link']];
        }
 
-       $args = array('item' => $item, 'menu' => $menu);
+       $args = ['item' => $item, 'menu' => $menu];
 
        call_hooks('item_photo_menu', $args);
 
@@ -1124,7 +1130,7 @@ function builtin_activity_puller($item, &$conv_responses) {
                                $url = 'redir/' . $item['contact-id'];
                                $sparkle = ' class="sparkle" ';
                        } else {
-                               $url = zrl($url);
+                               $url = Profile::zrl($url);
                        }
 
                        $url = '<a href="'. $url . '"'. $sparkle .'>' . htmlentities($item['author-name']) . '</a>';
@@ -1135,7 +1141,7 @@ function builtin_activity_puller($item, &$conv_responses) {
 
                        if (! ((isset($conv_responses[$mode][$item['thr-parent'] . '-l']))
                                && (is_array($conv_responses[$mode][$item['thr-parent'] . '-l'])))) {
-                               $conv_responses[$mode][$item['thr-parent'] . '-l'] = array();
+                               $conv_responses[$mode][$item['thr-parent'] . '-l'] = [];
                        }
 
                        // only list each unique author once
@@ -1243,11 +1249,11 @@ function format_like($cnt, array $arr, $type, $id) {
        }
 
        $phrase .= EOL ;
-       $o .= replace_macros(get_markup_template('voting_fakelink.tpl'), array(
+       $o .= replace_macros(get_markup_template('voting_fakelink.tpl'), [
                '$phrase' => $phrase,
                '$type' => $type,
                '$id' => $id
-       ));
+       ]);
        $o .= $expanded;
 
        return $o;
@@ -1257,10 +1263,10 @@ function status_editor(App $a, $x, $notes_cid = 0, $popup = false)
 {
        $o = '';
 
-       $geotag = x($x, 'allow_location') ? replace_macros(get_markup_template('jot_geotag.tpl'), array()) : '';
+       $geotag = x($x, 'allow_location') ? replace_macros(get_markup_template('jot_geotag.tpl'), []) : '';
 
        $tpl = get_markup_template('jot-header.tpl');
-       $a->page['htmlhead'] .= replace_macros($tpl, array(
+       $a->page['htmlhead'] .= replace_macros($tpl, [
                '$newpost'   => 'true',
                '$baseurl'   => System::baseUrl(true),
                '$geotag'    => $geotag,
@@ -1273,10 +1279,10 @@ function status_editor(App $a, $x, $notes_cid = 0, $popup = false)
                '$fileas'    => t('Save to Folder:'),
                '$whereareu' => t('Where are you right now?'),
                '$delitems'  => t('Delete item(s)?')
-       ));
+       ]);
 
        $tpl = get_markup_template('jot-end.tpl');
-       $a->page['end'] .= replace_macros($tpl, array(
+       $a->page['end'] .= replace_macros($tpl, [
                '$newpost'   => 'true',
                '$baseurl'   => System::baseUrl(true),
                '$geotag'    => $geotag,
@@ -1288,7 +1294,7 @@ function status_editor(App $a, $x, $notes_cid = 0, $popup = false)
                '$term'      => t('Tag term:'),
                '$fileas'    => t('Save to Folder:'),
                '$whereareu' => t('Where are you right now?')
-       ));
+       ]);
 
        $jotplugins = '';
        call_hooks('jot_tool', $jotplugins);
@@ -1301,7 +1307,7 @@ function status_editor(App $a, $x, $notes_cid = 0, $popup = false)
 
        $query_str = $a->query_string;
        if (strpos($query_str, 'public=1') !== false) {
-               $query_str = str_replace(array('?public=1', '&public=1'), array('', ''), $query_str);
+               $query_str = str_replace(['?public=1', '&public=1'], ['', ''], $query_str);
        }
 
        /*
@@ -1318,7 +1324,7 @@ function status_editor(App $a, $x, $notes_cid = 0, $popup = false)
        // $tpl = replace_macros($tpl,array('$jotplugins' => $jotplugins));
        $tpl = get_markup_template("jot.tpl");
 
-       $o .= replace_macros($tpl,array(
+       $o .= replace_macros($tpl,[
                '$return_path'  => $query_str,
                '$action'       => 'item',
                '$share'        => defaults($x, 'button', t('Share')),
@@ -1372,7 +1378,7 @@ function status_editor(App $a, $x, $notes_cid = 0, $popup = false)
                //jot nav tab (used in some themes)
                '$message' => t('Message'),
                '$browser' => t('Browser'),
-       ));
+       ]);
 
 
        if ($popup == true) {
@@ -1629,9 +1635,9 @@ function render_location_dummy($item) {
 
 /// @TODO Add type-hint
 function get_responses($conv_responses, $response_verbs, $ob, $item) {
-       $ret = array();
+       $ret = [];
        foreach ($response_verbs as $v) {
-               $ret[$v] = array();
+               $ret[$v] = [];
                $ret[$v]['count'] = defaults($conv_responses[$v], $item['uri'], '');
                $ret[$v]['list']  = defaults($conv_responses[$v], $item['uri'] . '-l', '');
                $ret[$v]['self']  = defaults($conv_responses[$v], $item['uri'] . '-self', '0');