]> git.mxchange.org Git - friendica.git/blobdiff - include/acl_selectors.php
Merge pull request #3343 from annando/1704-mastodon
[friendica.git] / include / acl_selectors.php
index cd68ffaa7d161b7be3a5ab02f9b7f2b4995344d7..f2d2999ba6175341ef254b4754e29f0f4b9c0083 100644 (file)
@@ -34,7 +34,7 @@ function group_select($selname,$selclass,$preselected = false,$size = 4) {
        call_hooks($a->module . '_pre_' . $selname, $arr);
 
        if (dbm::is_result($r)) {
-               foreach($r as $rr) {
+               foreach ($r as $rr) {
                        if((is_array($preselected)) && in_array($rr['id'], $preselected))
                                $selected = " selected=\"selected\" ";
                        else
@@ -65,20 +65,24 @@ function contact_selector($selname, $selclass, $preselected = false, $options) {
        $exclude = false;
        $size = 4;
 
-       if(is_array($options)) {
-               if(x($options,'size'))
+       if (is_array($options)) {
+               if (x($options,'size'))
                        $size = $options['size'];
 
-               if(x($options,'mutual_friends'))
+               if (x($options,'mutual_friends')) {
                        $mutual = true;
-               if(x($options,'single'))
+               }
+               if (x($options,'single')) {
                        $single = true;
-               if(x($options,'multiple'))
+               }
+               if (x($options,'multiple')) {
                        $single = false;
-               if(x($options,'exclude'))
+               }
+               if (x($options,'exclude')) {
                        $exclude = $options['exclude'];
+               }
 
-               if(x($options,'networks')) {
+               if (x($options,'networks')) {
                        switch($options['networks']) {
                                case 'DFRN_ONLY':
                                        $networks = array(NETWORK_DFRN);
@@ -131,7 +135,7 @@ function contact_selector($selname, $selclass, $preselected = false, $options) {
                $o .= "<select name=\"{$selname}[]\" id=\"$selclass\" class=\"$selclass\" multiple=\"multiple\" size=\"" . $x['size'] . "$\" $tabindex >\r\n";
 
        $r = q("SELECT `id`, `name`, `url`, `network` FROM `contact`
-               WHERE `uid` = %d AND `self` = 0 AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0 AND `notify` != ''
+               WHERE `uid` = %d AND NOT `self` AND NOT `blocked` AND NOT `pending` AND NOT `archive` AND `notify` != ''
                $sql_extra
                ORDER BY `name` ASC ",
                intval(local_user())
@@ -145,11 +149,12 @@ function contact_selector($selname, $selclass, $preselected = false, $options) {
        call_hooks($a->module . '_pre_' . $selname, $arr);
 
        if (dbm::is_result($r)) {
-               foreach($r as $rr) {
-                       if((is_array($preselected)) && in_array($rr['id'], $preselected))
+               foreach ($r as $rr) {
+                       if ((is_array($preselected)) && in_array($rr['id'], $preselected)) {
                                $selected = " selected=\"selected\" ";
-                       else
+                       } else {
                                $selected = '';
+                       }
 
                        $trimmed = mb_substr($rr['name'],0,20);
 
@@ -205,7 +210,7 @@ function contact_select($selname, $selclass, $preselected = false, $size = 4, $p
                $o .= "<select name=\"{$selname}[]\" id=\"$selclass\" class=\"$selclass\" multiple=\"multiple\" size=\"$size\" $tabindex >\r\n";
 
        $r = q("SELECT `id`, `name`, `url`, `network` FROM `contact`
-               WHERE `uid` = %d AND `self` = 0 AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0 AND `notify` != ''
+               WHERE `uid` = %d AND NOT `self` AND NOT `blocked` AND NOT `pending` AND NOT `archive` AND `notify` != ''
                $sql_extra
                ORDER BY `name` ASC ",
                intval(local_user())
@@ -221,16 +226,19 @@ function contact_select($selname, $selclass, $preselected = false, $size = 4, $p
        $receiverlist = array();
 
        if (dbm::is_result($r)) {
-               foreach($r as $rr) {
-                       if((is_array($preselected)) && in_array($rr['id'], $preselected))
+               foreach ($r as $rr) {
+                       if ((is_array($preselected)) && in_array($rr['id'], $preselected)) {
                                $selected = " selected=\"selected\" ";
-                       else
+                       }
+                       else {
                                $selected = '';
+                       }
 
-                       if($privmail)
+                       if ($privmail) {
                                $trimmed = GetProfileUsername($rr['url'], $rr['name'], false);
-                       else
+                       } else {
                                $trimmed = mb_substr($rr['name'],0,20);
+                       }
 
                        $receiverlist[] = $trimmed;
 
@@ -256,16 +264,22 @@ function fixacl(&$item) {
 
 function prune_deadguys($arr) {
 
-       if(! $arr)
+       if (! $arr) {
                return $arr;
+       }
+
        $str = dbesc(implode(',',$arr));
+
        $r = q("SELECT `id` FROM `contact` WHERE `id` IN ( " . $str . ") AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0 ");
-       if($r) {
+
+       if (dbm::is_result($r)) {
                $ret = array();
-               foreach($r as $rr)
+               foreach ($r as $rr) {
                        $ret[] = intval($rr['id']);
+               }
                return $ret;
        }
+
        return array();
 }
 
@@ -358,7 +372,7 @@ function populate_acl($user = null, $show_jotnets = false) {
 
 }
 
-function construct_acl_data(&$a, $user) {
+function construct_acl_data(App $a, $user) {
 
        // Get group and contact information for html ACL selector
        $acl_data = acl_lookup($a, 'html');
@@ -390,10 +404,11 @@ function construct_acl_data(&$a, $user) {
 
 }
 
-function acl_lookup(&$a, $out_type = 'json') {
+function acl_lookup(App $a, $out_type = 'json') {
 
-       if(!local_user())
-               return "";
+       if (!local_user()) {
+               return '';
+       }
 
        $start  =       (x($_REQUEST,'start')           ? $_REQUEST['start']            : 0);
        $count  =       (x($_REQUEST,'count')           ? $_REQUEST['count']            : 100);
@@ -434,8 +449,8 @@ function acl_lookup(&$a, $out_type = 'json') {
        // autocomplete for editor mentions
        if ($type=='' || $type=='c'){
                $r = q("SELECT COUNT(*) AS c FROM `contact`
-                               WHERE `uid` = %d AND `self` = 0
-                               AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0
+                               WHERE `uid` = %d AND NOT `self`
+                               AND NOT `blocked` AND NOT `pending` AND NOT `archive`
                                AND `notify` != '' $sql_extra2" ,
                        intval(local_user())
                );
@@ -446,8 +461,8 @@ function acl_lookup(&$a, $out_type = 'json') {
                // autocomplete for Private Messages
 
                $r = q("SELECT COUNT(*) AS c FROM `contact`
-                               WHERE `uid` = %d AND `self` = 0
-                               AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0
+                               WHERE `uid` = %d AND NOT `self`
+                               AND NOT `blocked` AND NOT `pending` AND NOT `archive`
                                AND `network` IN ('%s','%s','%s') $sql_extra2" ,
                        intval(local_user()),
                        dbesc(NETWORK_DFRN),
@@ -462,8 +477,8 @@ function acl_lookup(&$a, $out_type = 'json') {
                // autocomplete for Contacts
 
                $r = q("SELECT COUNT(*) AS c FROM `contact`
-                               WHERE `uid` = %d AND `self` = 0
-                               AND `pending` = 0 $sql_extra2" ,
+                               WHERE `uid` = %d AND NOT `self`
+                               AND NOT `pending` $sql_extra2" ,
                        intval(local_user())
                );
                $contact_count = (int)$r[0]['c'];
@@ -480,12 +495,14 @@ function acl_lookup(&$a, $out_type = 'json') {
 
        if ($type=='' || $type=='g'){
 
+               /// @todo We should cache this query.
+               // This can be done when we can delete cache entries via wildcard
                $r = q("SELECT `group`.`id`, `group`.`name`, GROUP_CONCAT(DISTINCT `group_member`.`contact-id` SEPARATOR ',') AS uids
                                FROM `group`
                                INNER JOIN `group_member` ON `group_member`.`gid`=`group`.`id` AND `group_member`.`uid` = `group`.`uid`
                                WHERE NOT `group`.`deleted` AND `group`.`uid` = %d
                                        $sql_extra
-                               GROUP BY `group`.`name`
+                               GROUP BY `group`.`name`, `group`.`id`
                                ORDER BY `group`.`name`
                                LIMIT %d,%d",
                        intval(local_user()),
@@ -510,7 +527,7 @@ function acl_lookup(&$a, $out_type = 'json') {
        if ($type==''){
 
                $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag`, `forum`, `prv` FROM `contact`
-                       WHERE `uid` = %d AND `self` = 0 AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0 AND `notify` != ''
+                       WHERE `uid` = %d AND NOT `self` AND NOT `blocked` AND NOT `pending` AND NOT `archive` AND `notify` != ''
                        AND NOT (`network` IN ('%s', '%s'))
                        $sql_extra2
                        ORDER BY `name` ASC ",
@@ -521,7 +538,7 @@ function acl_lookup(&$a, $out_type = 'json') {
        elseif ($type=='c'){
 
                $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag`, `forum`, `prv` FROM `contact`
-                       WHERE `uid` = %d AND `self` = 0 AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0 AND `notify` != ''
+                       WHERE `uid` = %d AND NOT `self` AND NOT `blocked` AND NOT `pending` AND NOT `archive` AND `notify` != ''
                        AND NOT (`network` IN ('%s'))
                        $sql_extra2
                        ORDER BY `name` ASC ",
@@ -531,7 +548,7 @@ function acl_lookup(&$a, $out_type = 'json') {
        }
        elseif($type == 'm') {
                $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag` FROM `contact`
-                       WHERE `uid` = %d AND `self` = 0 AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0
+                       WHERE `uid` = %d AND NOT `self` AND NOT `blocked` AND NOT `pending` AND NOT `archive`
                        AND `network` IN ('%s','%s','%s')
                        $sql_extra2
                        ORDER BY `name` ASC ",
@@ -540,54 +557,52 @@ function acl_lookup(&$a, $out_type = 'json') {
                        dbesc(NETWORK_ZOT),
                        dbesc(NETWORK_DIASPORA)
                );
-       }
-       elseif($type == 'a') {
+       } elseif ($type == 'a') {
                $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag`, `forum`, `prv` FROM `contact`
                        WHERE `uid` = %d AND `pending` = 0
                        $sql_extra2
                        ORDER BY `name` ASC ",
                        intval(local_user())
                );
-       }
-       elseif($type == 'x') {
+       } elseif ($type == 'x') {
                // autocomplete for global contact search (e.g. navbar search)
                $r = navbar_complete($a);
                $contacts = array();
-               if($r) {
-                       foreach($r as $g) {
+               if ($r) {
+                       foreach ($r as $g) {
                                $contacts[] = array(
-                                       "photo"    => proxy_url($g['photo'], false, PROXY_SIZE_MICRO),
-                                       "name"     => $g['name'],
-                                       "nick"     => (x($g['addr']) ? $g['addr'] : $g['url']),
-                                       "network" => $g['network'],
-                                       "link" => $g['url'],
-                                       "forum"    => (x($g['community']) ? 1 : 0),
+                                       'photo'   => proxy_url($g['photo'], false, PROXY_SIZE_MICRO),
+                                       'name'    => $g['name'],
+                                       'nick'    => (x($g['addr']) ? $g['addr'] : $g['url']),
+                                       'network' => $g['network'],
+                                       'link'    => $g['url'],
+                                       'forum'   => (x($g['community']) ? 1 : 0),
                                );
                        }
                }
                $o = array(
                        'start' => $start,
-                       'count' => $count,
-                       'items' => $contacts,
+                       'count' => $count,
+                       'items' => $contacts,
                );
                echo json_encode($o);
                killme();
-       }
-       else
+       } else {
                $r = array();
+       }
 
 
        if (dbm::is_result($r)) {
-               foreach($r as $g){
+               foreach ($r as $g){
                        $contacts[] = array(
-                               "type"  => "c",
-                               "photo" => proxy_url($g['micro'], false, PROXY_SIZE_MICRO),
-                               "name"  => htmlentities($g['name']),
-                               "id"    => intval($g['id']),
-                               "network" => $g['network'],
-                               "link" => $g['url'],
-                               "nick" => htmlentities(($g['attag']) ? $g['attag'] : $g['nick']),
-                               "forum" => ((x($g['forum']) || x($g['prv'])) ? 1 : 0),
+                               'type'    => 'c',
+                               'photo'   => proxy_url($g['micro'], false, PROXY_SIZE_MICRO),
+                               'name'    => htmlentities($g['name']),
+                               'id'      => intval($g['id']),
+                               'network' => $g['network'],
+                               'link'    => $g['url'],
+                               'nick'    => htmlentities(($g['attag']) ? $g['attag'] : $g['nick']),
+                               'forum'   => ((x($g['forum']) || x($g['prv'])) ? 1 : 0),
                        );
                }
        }
@@ -595,77 +610,81 @@ function acl_lookup(&$a, $out_type = 'json') {
        $items = array_merge($groups, $contacts);
 
        if ($conv_id) {
-               /* if $conv_id is set, get unknow contacts in thread */
-               /* but first get know contacts url to filter them out */
-               function _contact_link($i){ return dbesc($i['link']); }
-               $known_contacts = array_map(_contact_link, $contacts);
-               $unknow_contacts=array();
-               $r = q("SELECT `author-avatar`,`author-name`,`author-link`
+               /*
+                * if $conv_id is set, get unknown contacts in thread
+                * but first get known contacts url to filter them out
+                */
+               $known_contacts = array_map(
+                       function ($i) {
+                               return dbesc($i['link']);
+                       }
+               , $contacts);
+
+               $unknown_contacts = array();
+               $r = q("SELECT `author-link`
                                FROM `item` WHERE `parent` = %d
                                        AND (`author-name` LIKE '%%%s%%' OR `author-link` LIKE '%%%s%%')
                                        AND `author-link` NOT IN ('%s')
-                               GROUP BY `author-link`
+                               GROUP BY `author-link`, `author-avatar`, `author-name`
                                ORDER BY `author-name` ASC
                                ",
                                intval($conv_id),
                                dbesc($search),
                                dbesc($search),
-                               implode("','", $known_contacts)
+                               implode("', '", $known_contacts)
                );
-               if (dbm::is_result($r)){
-                       foreach($r as $row) {
-                               // nickname..
-                               $up = parse_url($row['author-link']);
-                               $nick = explode("/",$up['path']);
-                               $nick = $nick[count($nick)-1];
-                               $nick .= "@".$up['host'];
-                               // /nickname
-                               $unknow_contacts[] = array(
-                                       "type"  => "c",
-                                       "photo" => proxy_url($row['author-avatar'], false, PROXY_SIZE_MICRO),
-                                       "name"  => htmlentities($row['author-name']),
-                                       "id"    => '',
-                                       "network" => "unknown",
-                                       "link" => $row['author-link'],
-                                       "nick" => htmlentities($nick),
-                                       "forum" => false
-                               );
+               if (dbm::is_result($r)) {
+                       foreach ($r as $row) {
+                               $contact = get_contact_details_by_url($row['author-link']);
+
+                               if (count($contact) > 0) {
+                                       $unknown_contacts[] = array(
+                                               'type'    => 'c',
+                                               'photo'   => proxy_url($contact['micro'], false, PROXY_SIZE_MICRO),
+                                               'name'    => htmlentities($contact['name']),
+                                               'id'      => intval($contact['cid']),
+                                               'network' => $contact['network'],
+                                               'link'    => $contact['url'],
+                                               'nick'    => htmlentities($contact['nick'] ? : $contact['addr']),
+                                               'forum'   => $contact['forum']
+                                       );
+                               }
                        }
                }
 
-               $items = array_merge($items, $unknow_contacts);
-               $tot += count($unknow_contacts);
+               $items = array_merge($items, $unknown_contacts);
+               $tot += count($unknown_contacts);
        }
 
        $results = array(
-               "tot"   => $tot,
-               "start" => $start,
-               "count" => $count,
-               "groups" => $groups,
-               "contacts" => $contacts,
-               "items" => $items,
-               "type"  => $type,
-               "search" => $search,
+               'tot'      => $tot,
+               'start'    => $start,
+               'count'    => $count,
+               'groups'   => $groups,
+               'contacts' => $contacts,
+               'items'    => $items,
+               'type'     => $type,
+               'search'   => $search,
        );
 
        call_hooks('acl_lookup_end', $results);
 
        if($out_type === 'html') {
                $o = array(
-                       'tot'           => $results["tot"],
-                       'start'         => $results["start"],
-                       'count'         => $results["count"],
-                       'groups'        => $results["groups"],
-                       'contacts'      => $results["contacts"],
+                       'tot'      => $results['tot'],
+                       'start'    => $results['start'],
+                       'count'    => $results['count'],
+                       'groups'   => $results['groups'],
+                       'contacts' => $results['contacts'],
                );
                return $o;
        }
 
        $o = array(
-               'tot'   => $results["tot"],
-               'start' => $results["start"],
-               'count' => $results["count"],
-               'items' => $results["items"],
+               'tot'   => $results['tot'],
+               'start' => $results['start'],
+               'count' => $results['count'],
+               'items' => $results['items'],
        );
 
        echo json_encode($o);
@@ -674,15 +693,15 @@ function acl_lookup(&$a, $out_type = 'json') {
 }
 /**
  * @brief Searching for global contacts for autocompletion
- * 
+ *
  * @param App $a
  * @return array with the search results
  */
-function navbar_complete(App &$a) {
+function navbar_complete(App $a) {
 
 //     logger('navbar_complete');
 
-       if((get_config('system','block_public')) && (! local_user()) && (! remote_user())) {
+       if ((get_config('system','block_public')) && (! local_user()) && (! remote_user())) {
                return;
        }
 
@@ -693,28 +712,32 @@ function navbar_complete(App &$a) {
        $mode = $_REQUEST['smode'];
 
        // don't search if search term has less than 2 characters
-       if(! $search || mb_strlen($search) < 2)
+       if (! $search || mb_strlen($search) < 2) {
                return array();
+       }
 
-       if(substr($search,0,1) === '@')
+       if (substr($search,0,1) === '@') {
                $search = substr($search,1);
+       }
 
-       if($localsearch) {
+       if ($localsearch) {
                $x = DirSearch::global_search_by_name($search, $mode);
                return $x;
        }
 
-       if(! $localsearch) {
+       if (! $localsearch) {
                $p = (($a->pager['page'] != 1) ? '&p=' . $a->pager['page'] : '');
 
                $x = z_fetch_url(get_server().'/lsearch?f=' . $p .  '&search=' . urlencode($search));
-               if($x['success']) {
+               if ($x['success']) {
                        $t = 0;
                        $j = json_decode($x['body'],true);
-                       if($j && $j['results']) {
+                       if ($j && $j['results']) {
                                return $j['results'];
                        }
                }
        }
+
+       /// @TODO Not needed here?
        return;
 }