Esempio n. 1
0
function unl_site_variable_get($db_prefix, $name, $default = NULL)
{
    $shared_prefix = unl_get_shared_db_prefix();
    $data = db_query("SELECT * " . "FROM {$db_prefix}_{$shared_prefix}variable " . "WHERE name = :name", array(':name' => $name))->fetchAll();
    if (count($data) == 0) {
        return $default;
    }
    return unserialize($data[0]->value);
}
Esempio n. 2
0
/**
 * Returns an array of lists of either roles a user belongs to or users belonging to a role.
 * Each key is the URI of a site and the value is the list.
 * If $list_empty_sites is set to TRUE, all sites will be listed, even if they have empty lists.
 *
 * @param string $search_by (Either 'username' or 'role')
 * @param mixed $username_or_role
 * @param bool $list_empty_sites
 * @throws Exception
 */
function unl_get_site_user_map($search_by, $username_or_role, $list_empty_sites = FALSE)
{
    if (!in_array($search_by, array('username', 'role'))) {
        throw new Exception('Invalid argument for $search_by');
    }
    $sites = db_select('unl_sites', 's')->fields('s', array('site_id', 'db_prefix', 'installed', 'site_path', 'uri'))->execute()->fetchAll();
    $audit_map = array();
    foreach ($sites as $site) {
        $shared_prefix = unl_get_shared_db_prefix();
        $prefix = $site->db_prefix;
        try {
            $site_settings = unl_get_site_settings($site->uri);
            $site_db_config = $site_settings['databases']['default']['default'];
            $roles_are_shared = is_array($site_db_config['prefix']) && array_key_exists('role', $site_db_config['prefix']);
            // If the site uses shared roles, ignore it if the user wants us to.
            /*if ($roles_are_shared && $form_state['values']['ignore_shared_roles']) {
                continue;
              }*/
            $bound_params = array();
            $where = array();
            if ($search_by == 'username') {
                $return_label = 'roles';
                $select = 'r.name, u.name';
                $where[] = 'u.name LIKE :name';
                $bound_params[':name'] = "%" . $username_or_role . "%";
            } else {
                $return_label = 'users';
                $select = 'u.name';
                $where[] = 'r.name = :role';
                $bound_params[':role'] = $username_or_role;
            }
            $query = "SELECT {$select} " . "FROM {$prefix}_{$shared_prefix}users AS u " . "JOIN {$prefix}_{$shared_prefix}users_roles AS m " . "  ON u.uid = m.uid " . 'JOIN ' . ($roles_are_shared ? '' : $prefix . '_') . $shared_prefix . 'role AS r ' . "  ON m.rid = r.rid ";
            if (count($where) > 0) {
                $query .= 'WHERE ' . implode(' AND ', $where) . ' ';
            }
            $role_user = db_query($query, $bound_params)->fetchAllKeyed();
            if (count($role_user) == 0 && !$list_empty_sites) {
                continue;
            }
            $primary_base_url = unl_site_variable_get($prefix, 'unl_primary_base_url');
            if ($primary_base_url) {
                $uri = $primary_base_url;
            } else {
                $uri = $site->uri;
            }
            // Get timestamp of last node edit to signify last time the site was updated.
            $query = "SELECT n.changed " . "FROM {$prefix}_{$shared_prefix}node AS n " . "ORDER BY n.changed DESC " . "LIMIT 1";
            $last_update = db_query($query)->fetchCol();
            $audit_map[$site->site_id] = array('uri' => $uri, $return_label => $role_user, 'last_update' => isset($last_update[0]) ? $last_update[0] : null);
        } catch (Exception $e) {
            // Either the site has no settings.php or the db_prefix is wrong.
            watchdog('unl_multisite', 'Error querying database for site %uri', array('%uri' => $site->uri), WATCHDOG_WARNING);
        }
    }
    return $audit_map;
}