Пример #1
0
Файл: acl.php Проект: Mauru/red
function acl_init(&$a)
{
    //	logger('mod_acl: ' . print_r($_REQUEST,true));
    $start = x($_REQUEST, 'start') ? $_REQUEST['start'] : 0;
    $count = x($_REQUEST, 'count') ? $_REQUEST['count'] : 100;
    $search = x($_REQUEST, 'search') ? $_REQUEST['search'] : "";
    $type = x($_REQUEST, 'type') ? $_REQUEST['type'] : "";
    // For use with jquery.autocomplete for private mail completion
    if (x($_REQUEST, 'query') && strlen($_REQUEST['query'])) {
        if (!$type) {
            $type = 'm';
        }
        $search = $_REQUEST['query'];
    }
    if (!local_user()) {
        if ($type != 'x') {
            killme();
        }
    }
    if ($search != "") {
        $sql_extra = " AND `name` LIKE " . protect_sprintf("'%" . dbesc($search) . "%'") . " ";
        $sql_extra2 = "AND ( xchan_name LIKE " . protect_sprintf("'%" . dbesc($search) . "%'") . " OR xchan_addr LIKE " . protect_sprintf("'%" . dbesc($search) . "%'") . ") ";
        $col = strpos($search, '@') !== false ? 'xchan_addr' : 'xchan_name';
        $sql_extra3 = "AND {$col} like " . protect_sprintf("'%" . dbesc($search) . "%'") . " ";
    } else {
        $sql_extra = $sql_extra2 = $sql_extra3 = "";
    }
    // count groups and contacts
    if ($type == '' || $type == 'g') {
        $r = q("SELECT COUNT(`id`) AS g FROM `groups` WHERE `deleted` = 0 AND `uid` = %d {$sql_extra}", intval(local_user()));
        $group_count = (int) $r[0]['g'];
    } else {
        $group_count = 0;
    }
    if ($type == '' || $type == 'c') {
        $r = q("SELECT COUNT(abook_id) AS c FROM abook left join xchan on abook_xchan = xchan_hash \n\t\t\t\tWHERE abook_channel = %d AND not ( abook_flags & %d ) and not (xchan_flags & %d ) {$sql_extra2}", intval(local_user()), intval(ABOOK_FLAG_BLOCKED | ABOOK_FLAG_PENDING | ABOOK_FLAG_ARCHIVED), intval(XCHAN_FLAGS_DELETED));
        $contact_count = (int) $r[0]['c'];
        if (intval(get_config('system', 'taganyone')) || intval(get_pconfig(local_user(), 'system', 'taganyone'))) {
            if ((!$r || !$r[0]['total']) && $type == 'c') {
                $r = q("SELECT COUNT(xchan_hash) AS c FROM xchan \n\t\t\t\t\tWHERE not (xchan_flags & %d ) {$sql_extra2}", intval(XCHAN_FLAGS_DELETED));
                $contact_count = (int) $r[0]['c'];
            }
        }
    } elseif ($type == 'm') {
        // autocomplete for Private Messages
        $r = q("SELECT count(xchan_hash) as c\n\t\t\tFROM abook left join xchan on abook_xchan = xchan_hash\n\t\t\tWHERE abook_channel = %d and ( (abook_their_perms = null) or (abook_their_perms & %d ))\n\t\t\tand not ( xchan_flags & %d )\n\t\t\t{$sql_extra2} ", intval(local_user()), intval(PERMS_W_MAIL), intval(XCHAN_FLAGS_DELETED));
        if ($r) {
            $contact_count = (int) $r[0]['c'];
        }
    } elseif ($type == 'a') {
        // autocomplete for Contacts
        $r = q("SELECT COUNT(abook_id) AS c FROM abook left join xchan on abook_xchan = xchan_hash \n\t\t\t\tWHERE abook_channel = %d and not ( xchan_flags & %d ) {$sql_extra2}", intval(local_user()), intval(XCHAN_FLAGS_DELETED));
        $contact_count = (int) $r[0]['c'];
    } else {
        $contact_count = 0;
    }
    $tot = $group_count + $contact_count;
    $groups = array();
    $contacts = array();
    if ($type == '' || $type == 'g') {
        $r = q("SELECT `groups`.`id`, `groups`.`hash`, `groups`.`name`, \n\t\t\t\tGROUP_CONCAT(DISTINCT `group_member`.`xchan` SEPARATOR ',') as uids\n\t\t\t\tFROM `groups`,`group_member` \n\t\t\t\tWHERE `groups`.`deleted` = 0 AND `groups`.`uid` = %d \n\t\t\t\t\tAND `group_member`.`gid`=`groups`.`id`\n\t\t\t\t\t{$sql_extra}\n\t\t\t\tGROUP BY `groups`.`id`\n\t\t\t\tORDER BY `groups`.`name` \n\t\t\t\tLIMIT %d,%d", intval(local_user()), intval($start), intval($count));
        foreach ($r as $g) {
            //		logger('acl: group: ' . $g['name'] . ' members: ' . $g['uids']);
            $groups[] = array("type" => "g", "photo" => "images/twopeople.png", "name" => $g['name'], "id" => $g['id'], "xid" => $g['hash'], "uids" => explode(",", $g['uids']), "link" => '');
        }
    }
    if ($type == '' || $type == 'c') {
        $r = q("SELECT abook_id as id, xchan_hash as hash, xchan_name as name, xchan_photo_s as micro, xchan_url as url, xchan_addr as nick, abook_their_perms, abook_flags \n\t\t\t\tFROM abook left join xchan on abook_xchan = xchan_hash \n\t\t\t\tWHERE abook_channel = %d AND not ( abook_flags & %d ) and not (xchan_flags & %d ) {$sql_extra2} order by xchan_name asc", intval(local_user()), intval(ABOOK_FLAG_BLOCKED | ABOOK_FLAG_PENDING | ABOOK_FLAG_ARCHIVED), intval(XCHAN_FLAGS_DELETED));
        if (intval(get_config('system', 'taganyone')) || intval(get_pconfig(local_user(), 'system', 'taganyone'))) {
            if (!$r && $type == 'c') {
                $r = q("SELECT substr(xchan_hash,1,18) as id, xchan_hash as hash, xchan_name as name, xchan_photo_s as micro, xchan_url as url, xchan_addr as nick, 0 as abook_their_perms, 0 as abook_flags \n\t\t\t\t\tFROM xchan \n\t\t\t\t\tWHERE not (xchan_flags & %d ) {$sql_extra2} order by xchan_name asc", intval(XCHAN_FLAGS_DELETED));
            }
        }
    } elseif ($type == 'm') {
        $r = q("SELECT xchan_hash as id, xchan_name as name, xchan_addr as nick, xchan_photo_s as micro, xchan_url as url \n\t\t\tFROM abook left join xchan on abook_xchan = xchan_hash\n\t\t\tWHERE abook_channel = %d and ( (abook_their_perms = null) or (abook_their_perms & %d ))\n\t\t\tand not (xchan_flags & %d)\n\t\t\t{$sql_extra3}\n\t\t\tORDER BY `xchan_name` ASC ", intval(local_user()), intval(PERMS_W_MAIL), intval(XCHAN_FLAGS_DELETED));
    } elseif ($type == 'a') {
        $r = q("SELECT abook_id as id, xchan_name as name, xchan_hash as hash, xchan_addr as nick, xchan_photo_s as micro, xchan_network as network, xchan_url as url, xchan_addr as attag , abook_their_perms FROM abook left join xchan on abook_xchan = xchan_hash\n\t\t\tWHERE abook_channel = %d\n\t\t\tand not (xchan_flags & %d)\n\t\t\t{$sql_extra3}\n\t\t\tORDER BY xchan_name ASC ", intval(local_user()), intval(XCHAN_FLAGS_DELETED));
    } elseif ($type == 'x') {
        $r = navbar_complete($a);
        $x = array();
        $x['query'] = $search;
        $x['photos'] = array();
        $x['links'] = array();
        $x['suggestions'] = array();
        $x['data'] = array();
        if ($r) {
            foreach ($r as $g) {
                $x['photos'][] = $g['photo'];
                $x['links'][] = $g['url'];
                $x['suggestions'][] = '@' . $g['name'];
                $x['data'][] = $g['name'];
            }
        }
        echo json_encode($x);
        killme();
    } else {
        $r = array();
    }
    if ($type == 'm' || $type == 'a') {
        $x = array();
        $x['query'] = $search;
        $x['photos'] = array();
        $x['links'] = array();
        $x['suggestions'] = array();
        $x['data'] = array();
        if (count($r)) {
            foreach ($r as $g) {
                $x['photos'][] = $g['micro'];
                $x['links'][] = $g['url'];
                $x['suggestions'][] = $g['name'];
                $x['data'][] = $g['id'];
            }
        }
        echo json_encode($x);
        killme();
    }
    if (count($r)) {
        foreach ($r as $g) {
            // remove RSS feeds from ACLs - they are inaccessible
            if (strpos($g['hash'], '/')) {
                continue;
            }
            if ($g['abook_their_perms'] & PERMS_W_TAGWALL && $type == 'c') {
                $contacts[] = array("type" => "c", "photo" => "images/twopeople.png", "name" => $g['name'] . '+', "id" => $g['id'] . '+', "xid" => $g['hash'], "link" => $g['nick'], "nick" => substr($g['nick'], 0, strpos($g['nick'], '@')), "self" => $g['abook_flags'] & ABOOK_FLAG_SELF ? 'abook-self' : '', "taggable" => 'taggable', "label" => t('network'));
            }
            $contacts[] = array("type" => "c", "photo" => $g['micro'], "name" => $g['name'], "id" => $g['id'], "xid" => $g['hash'], "link" => $g['nick'], "nick" => substr($g['nick'], 0, strpos($g['nick'], '@')), "self" => $g['abook_flags'] & ABOOK_FLAG_SELF ? 'abook-self' : '', "taggable" => '', "label" => '');
        }
    }
    $items = array_merge($groups, $contacts);
    $o = array('tot' => $tot, 'start' => $start, 'count' => $count, 'items' => $items);
    echo json_encode($o);
    killme();
}
Пример #2
0
function acl_init(&$a)
{
    //	logger('mod_acl: ' . print_r($_REQUEST,true));
    $start = x($_REQUEST, 'start') ? $_REQUEST['start'] : 0;
    $count = x($_REQUEST, 'count') ? $_REQUEST['count'] : 100;
    $search = x($_REQUEST, 'search') ? $_REQUEST['search'] : "";
    $type = x($_REQUEST, 'type') ? $_REQUEST['type'] : "";
    $noforums = x($_REQUEST, 'n') ? $_REQUEST['n'] : false;
    // List of channels whose connections to also suggest, e.g. currently viewed channel or channels mentioned in a post
    $extra_channels = x($_REQUEST, 'extra_channels') ? $_REQUEST['extra_channels'] : array();
    // For use with jquery.autocomplete for private mail completion
    if (x($_REQUEST, 'query') && strlen($_REQUEST['query'])) {
        if (!$type) {
            $type = 'm';
        }
        $search = $_REQUEST['query'];
    }
    if (!local_channel()) {
        if (!($type == 'x' || $type == 'c')) {
            killme();
        }
    }
    if ($search != "") {
        $sql_extra = " AND `name` LIKE " . protect_sprintf("'%" . dbesc($search) . "%'") . " ";
        $sql_extra2 = "AND ( xchan_name LIKE " . protect_sprintf("'%" . dbesc($search) . "%'") . " OR xchan_addr LIKE " . protect_sprintf("'%" . dbesc($search) . (strpos($search, '@') === false ? "%@%'" : "%'")) . ") ";
        // This horrible mess is needed because position also returns 0 if nothing is found. W/ould be MUCH easier if it instead returned a very large value
        // Otherwise we could just order by LEAST(POSITION($search IN xchan_name),POSITION($search IN xchan_addr)).
        $order_extra2 = "CASE WHEN xchan_name LIKE " . protect_sprintf("'%" . dbesc($search) . "%'") . " then POSITION('" . dbesc($search) . "' IN xchan_name) else position('" . dbesc($search) . "' IN xchan_addr) end, ";
        $col = strpos($search, '@') !== false ? 'xchan_addr' : 'xchan_name';
        $sql_extra3 = "AND {$col} like " . protect_sprintf("'%" . dbesc($search) . "%'") . " ";
    } else {
        $sql_extra = $sql_extra2 = $sql_extra3 = "";
    }
    $groups = array();
    $contacts = array();
    if ($type == '' || $type == 'g') {
        $r = q("SELECT `groups`.`id`, `groups`.`hash`, `groups`.`name`, \n\t\t\t\t%s as uids\n\t\t\t\tFROM `groups`,`group_member` \n\t\t\t\tWHERE `groups`.`deleted` = 0 AND `groups`.`uid` = %d \n\t\t\t\t\tAND `group_member`.`gid`=`groups`.`id`\n\t\t\t\t\t{$sql_extra}\n\t\t\t\tGROUP BY `groups`.`id`\n\t\t\t\tORDER BY `groups`.`name` \n\t\t\t\tLIMIT %d OFFSET %d", db_concat('group_member.xchan', ','), intval(local_channel()), intval($count), intval($start));
        foreach ($r as $g) {
            //		logger('acl: group: ' . $g['name'] . ' members: ' . $g['uids']);
            $groups[] = array("type" => "g", "photo" => "images/twopeople.png", "name" => $g['name'], "id" => $g['id'], "xid" => $g['hash'], "uids" => explode(",", $g['uids']), "link" => '');
        }
    }
    if ($type == '' || $type == 'c') {
        $extra_channels_sql = '';
        // Only include channels who allow the observer to view their permissions
        foreach ($extra_channels as $channel) {
            if (perm_is_allowed(intval($channel), get_observer_hash(), 'view_contacts')) {
                $extra_channels_sql .= "," . intval($channel);
            }
        }
        $extra_channels_sql = substr($extra_channels_sql, 1);
        // Remove initial comma
        // Getting info from the abook is better for local users because it contains info about permissions
        if (local_channel()) {
            if ($extra_channels_sql != '') {
                $extra_channels_sql = " OR (abook_channel IN ({$extra_channels_sql})) and abook_hidden = 0 ";
            }
            $r = q("SELECT abook_id as id, xchan_hash as hash, xchan_name as name, xchan_photo_s as micro, xchan_url as url, xchan_addr as nick, abook_their_perms, abook_flags, abook_self \n\t\t\t\tFROM abook left join xchan on abook_xchan = xchan_hash \n\t\t\t\tWHERE (abook_channel = %d {$extra_channels_sql}) AND abook_blocked = 0 and abook_pending = 0 and abook_archived = 0 and xchan_deleted = 0 {$sql_extra2} order by {$order_extra2} xchan_name asc", intval(local_channel()));
        } else {
            // Visitors
            $r = q("SELECT xchan_hash as id, xchan_hash as hash, xchan_name as name, xchan_photo_s as micro, xchan_url as url, xchan_addr as nick, 0 as abook_their_perms, 0 as abook_flags, 0 as abook_self\n\t\t\t\tFROM xchan left join xlink on xlink_link = xchan_hash\n\t\t\t\tWHERE xlink_xchan  = '%s' AND xchan_deleted = 0 {$sql_extra2} order by {$order_extra2} xchan_name asc", dbesc(get_observer_hash()));
            // Find contacts of extra channels
            // This is probably more complicated than it needs to be
            if ($extra_channels_sql) {
                // Build a list of hashes that we got previously so we don't get them again
                $known_hashes = array("'" . get_observer_hash() . "'");
                if ($r) {
                    foreach ($r as $rr) {
                        $known_hashes[] = "'" . $rr['hash'] . "'";
                    }
                }
                $known_hashes_sql = 'AND xchan_hash not in (' . join(',', $known_hashes) . ')';
                $r2 = q("SELECT abook_id as id, xchan_hash as hash, xchan_name as name, xchan_photo_s as micro, xchan_url as url, xchan_addr as nick, abook_their_perms, abook_flags, abook_self \n\t\t\t\t\tFROM abook left join xchan on abook_xchan = xchan_hash \n\t\t\t\t\tWHERE abook_channel IN ({$extra_channels_sql}) {$known_hashes_sql} AND abook_blocked = 0 and abook_pending = 0 and abook_archived = 0 and abook_hidden = 0 and xchan_deleted = 0 {$sql_extra2} order by {$order_extra2} xchan_name asc");
                if ($r2) {
                    $r = array_merge($r, $r2);
                }
                // Sort accoring to match position, then alphabetically. This could be avoided if the above two SQL queries could be combined into one, and the sorting could be done on the SQl server (like in the case of a local user)
                $matchpos = function ($x) use($search) {
                    $namepos = strpos($x['name'], $search);
                    $nickpos = strpos($x['nick'], $search);
                    // Use a large position if not found
                    return min($namepos === false ? 9999 : $namepos, $nickpos === false ? 9999 : $nickpos);
                };
                // This could be made simpler if PHP supported stable sorting
                usort($r, function ($a, $b) use($matchpos) {
                    $pos1 = $matchpos($a);
                    $pos2 = $matchpos($b);
                    if ($pos1 == $pos2) {
                        // Order alphabetically if match position is the same
                        if ($a['name'] == $b['name']) {
                            return 0;
                        } else {
                            return $a['name'] < $b['name'] ? -1 : 1;
                        }
                    }
                    return $pos1 < $pos2 ? -1 : 1;
                });
            }
        }
        if (intval(get_config('system', 'taganyone')) || intval(get_pconfig(local_channel(), 'system', 'taganyone'))) {
            if (count($r) < 100 && $type == 'c') {
                $r2 = q("SELECT substr(xchan_hash,1,18) as id, xchan_hash as hash, xchan_name as name, xchan_photo_s as micro, xchan_url as url, xchan_addr as nick, 0 as abook_their_perms, 0 as abook_flags, 0 as abook_self \n\t\t\t\t\tFROM xchan \n\t\t\t\t\tWHERE xchan_deleted = 0 {$sql_extra2} order by {$order_extra2} xchan_name asc");
                if ($r2) {
                    $r = array_merge($r, $r2);
                }
            }
        }
    } elseif ($type == 'm') {
        $r = q("SELECT xchan_hash as id, xchan_name as name, xchan_addr as nick, xchan_photo_s as micro, xchan_url as url \n\t\t\tFROM abook left join xchan on abook_xchan = xchan_hash\n\t\t\tWHERE abook_channel = %d and ( (abook_their_perms = null) or (abook_their_perms & %d )>0)\n\t\t\tand xchan_deleted = 0\n\t\t\t{$sql_extra3}\n\t\t\tORDER BY `xchan_name` ASC ", intval(local_channel()), intval(PERMS_W_MAIL));
    } elseif ($type == 'a' || $type == 'p') {
        $r = q("SELECT abook_id as id, xchan_name as name, xchan_hash as hash, xchan_addr as nick, xchan_photo_s as micro, xchan_network as network, xchan_url as url, xchan_addr as attag , abook_their_perms FROM abook left join xchan on abook_xchan = xchan_hash\n\t\t\tWHERE abook_channel = %d\n\t\t\tand xchan_deleted = 0\n\t\t\t{$sql_extra3}\n\t\t\tORDER BY xchan_name ASC ", intval(local_channel()));
    } elseif ($type == 'x') {
        $r = navbar_complete($a);
        $contacts = array();
        if ($r) {
            foreach ($r as $g) {
                $contacts[] = array("photo" => $g['photo'], "name" => $g['name'], "nick" => $g['address']);
            }
        }
        $o = array('start' => $start, 'count' => $count, 'items' => $contacts);
        echo json_encode($o);
        killme();
    } else {
        $r = array();
    }
    if (count($r)) {
        foreach ($r as $g) {
            // remove RSS feeds from ACLs - they are inaccessible
            if (strpos($g['hash'], '/') && $type != 'a') {
                continue;
            }
            if ($g['abook_their_perms'] & PERMS_W_TAGWALL && $type == 'c' && !$noforums) {
                $contacts[] = array("type" => "c", "photo" => "images/twopeople.png", "name" => $g['name'] . '+', "id" => $g['id'] . '+', "xid" => $g['hash'], "link" => $g['nick'], "nick" => substr($g['nick'], 0, strpos($g['nick'], '@')), "self" => intval($g['abook_self']) ? 'abook-self' : '', "taggable" => 'taggable', "label" => t('network'));
            }
            $contacts[] = array("type" => "c", "photo" => $g['micro'], "name" => $g['name'], "id" => $g['id'], "xid" => $g['hash'], "link" => $g['nick'], "nick" => $g['nick'] ? substr($g['nick'], 0, strpos($g['nick'], '@')) : t('RSS'), "self" => intval($g['abook_self']) ? 'abook-self' : '', "taggable" => '', "label" => '');
        }
    }
    $items = array_merge($groups, $contacts);
    $o = array('start' => $start, 'count' => $count, 'items' => $items);
    echo json_encode($o);
    killme();
}