function api_oauth2_access_tokens_create_site_token($user = null)
{
    $site_key = api_keys_fetch_site_key();
    $id = dbtickets_create(64);
    $user_id = $user ? $user['id'] : 0;
    $token = api_oauth2_access_tokens_generate_token();
    $ttl = $user ? $GLOBALS['cfg']['api_site_tokens_user_ttl'] : $GLOBALS['cfg']['api_site_tokens_ttl'];
    $now = time();
    $expires = $now + $ttl;
    $perms_map = api_oauth2_access_tokens_permissions_map('string keys');
    $perms = $user_id ? $perms_map['write'] : $perms_map['login'];
    $row = array('id' => $id, 'perms' => $perms, 'api_key_id' => $site_key['id'], 'api_key_role_id' => $site_key['role_id'], 'user_id' => $user_id, 'access_token' => $token, 'created' => $now, 'last_modified' => $now, 'expires' => $expires);
    $insert = array();
    foreach ($row as $k => $v) {
        $insert[$k] = AddSlashes($v);
    }
    $rsp = db_insert('OAuth2AccessTokens', $insert);
    if ($rsp['ok']) {
        $rsp['token'] = $row;
    }
    return $rsp;
}
function api_config_init_blessings()
{
    # $GLOBALS['timing_keys']["api_blessings"] = "API blessings";
    # $GLOBALS['timings']['api_blessings_count'] = 0;
    # $GLOBALS['timings']['api_blessings_time'] = 0;
    foreach ($GLOBALS['cfg']['api']['blessings'] as $api_key => $key_details) {
        # $GLOBALS['timings']['api_blessings_count'] += 1;
        $start = microtime_ms();
        $whoami = $api_key;
        if ($api_key == 'site_key') {
            loadlib("api_keys");
            $blessed_site_keys = features_is_enabled(array("api_site_keys", "api_site_keys_blessed")) ? 1 : 0;
            if ($blessed_site_keys && ($site_key = api_keys_fetch_site_key())) {
                $api_key = $site_key['api_key'];
            }
        }
        $blessing_defaults = array();
        foreach (array('hosts', 'tokens', 'environments') as $prop) {
            if (isset($key_details[$prop])) {
                $blessing_defaults[$prop] = $key_details[$prop];
            }
        }
        if (is_array($key_details['method_classes'])) {
            foreach ($key_details['method_classes'] as $class_spec => $blessing_details) {
                foreach ($GLOBALS['cfg']['api']['methods'] as $method_name => $method_details) {
                    if (!$method_details['requires_blessing']) {
                        continue;
                    }
                    if (!preg_match("/^{$class_spec}/", $method_name)) {
                        continue;
                    }
                    $blessing = array_merge($blessing_defaults, $blessing_details);
                    _api_config_apply_blessing($method_name, $api_key, $blessing);
                }
            }
        }
        if (is_array($key_details['methods'])) {
            foreach ($key_details['methods'] as $method_name => $blessing_details) {
                $blessing = array_merge($blessing_defaults, $blessing_details);
                _api_config_apply_blessing($method_name, $api_key, $blessing);
            }
        }
        # _api_config_apply_blessing('api.test.isBlessed', $api_key, $blessing_defaults);
        $end = microtime_ms();
        $time = $end - $start;
        # $GLOBALS['timings']['api_blessings_time'] += $time;
    }
}