function __construct() { global $amp_conf, $db; $this->amp_conf =& $amp_conf; $this->auth = new RestAuth($this); $this->db =& $db; //import our db object $this->hash_algo = 'sha256'; $this->opts = restapi_opts_get(); if ($this->opts['logging'] == 'enabled') { $this->log = new RestLogger($this); } else { $this->log = NULL; } $this->mods = modulelist::create($this->db); //matched routes, if any $this->_register_routes(); $this->res = new stdClass(); $this->res->nonce = restapi_tokens_generate(); //setup $this->req $this->_get_req(); if ($this->log) { $this->log->init(); } }
$vars[$k] = isset($_REQUEST[$k]) ? $_REQUEST[$k] : $v; } if (isset($vars['action'])) { switch ($vars['action']) { case 'save_general': restapi_opts_put($vars); break; default: break; } } $vars = array_merge($vars, restapi_opts_get()); //if we dont have tokens, create new ones if (!$vars['token'] && !$vars['tokenkey']) { $vars['token'] = restapi_tokens_generate(); $vars['tokenkey'] = restapi_tokens_generate(); } ?> <div class="container-fluid"> <div class="row"> <div class="col-sm-12"> <h1><?php echo _('Rest API'); ?> </h1> <div class="panel panel-info"> <div class="panel-heading"> <div class="panel-title"> <a href="#" data-toggle="collapse" data-target="#moreinfo" class="collapsed" aria-expanded="false"><i class="glyphicon glyphicon-info-sign"></i></a> <?php echo _('What is Rest API'); ?>
public function usermanShowPage() { if (isset($_REQUEST['action'])) { switch ($_REQUEST['action']) { case 'showgroup': case 'addgroup': case 'adduser': case 'showuser': $enabled = null; if ($_REQUEST['action'] == "showuser") { $enabled = $this->userman->getModuleSettingByID($_REQUEST['user'], 'restapi', 'restapi_token_status', true); $tokens = restapi_user_get_user_tokens($_REQUEST['user']); } else { $tokens = array(); } $displayvars = array("mode" => in_array($_REQUEST['action'], array("showgroup", "addgroup")) ? "group" : "user", "enabled" => $enabled); $tokens = !empty($tokens) ? $tokens : array(); $displayvars['user_list_all'] = array(); if (in_array($_REQUEST['action'], array("showgroup", "addgroup"))) { $displayvars['user_list_all']['self'] = _("User Primary Extension"); } $cul = array(); foreach (core_users_list() as $list) { $cul[$list[0]] = array("name" => $list[1], "vmcontext" => $list[2]); $displayvars['user_list_all'][$list[0]] = $list[1] . " <" . $list[0] . ">"; } // Get list of modules that have been API enabled. $api = new \Api(); $api_mods = array(); foreach ($api->maps as $verb => $urls) { foreach ($urls as $url => $maps) { foreach ($maps as $map => $details) { $api_mods[$details["module"]] = 1; } } } unset($api); //modules global $db; $mods = \modulelist::create($db); $displayvars['module_list'] = array(); foreach ($mods->module_array as $mod) { if (isset($mod['rawname']) && isset($api_mods[$mod['rawname']])) { $displayvars['module_list'][$mod['rawname']] = $mod['name']; } } asort($displayvars['module_list']); $displayvars['module_list'] = array('*' => _('All')) + $displayvars['module_list']; //everything else $rest_template = $displayvars; if (!empty($tokens)) { foreach ($tokens as $token) { $displayvars['tokens'][] = array_merge($rest_template, restapi_tokens_get($token)); } } else { $displayvars['tokens'][0] = array_merge($rest_template, restapi_tokens_get()); $displayvars['tokens'][0]['token'] = \restapi_tokens_generate(); $displayvars['tokens'][0]['tokenkey'] = \restapi_tokens_generate(); $displayvars['tokens'][0]['id'] = 0; $displayvars['tokens'][0]['users'] = array("self"); $displayvars['tokens'][0]['rate'] = 1000; } if ($displayvars['mode'] == "user") { } else { //group mode $enabled = $this->userman->getModuleSettingByGID($_REQUEST['group'], 'restapi', 'restapi_token_status'); $users = $this->userman->getModuleSettingByGID($_REQUEST['group'], 'restapi', 'restapi_users'); $modules = $this->userman->getModuleSettingByGID($_REQUEST['group'], 'restapi', 'restapi_modules'); $rate = $this->userman->getModuleSettingByGID($_REQUEST['group'], 'restapi', 'restapi_rate'); $displayvars['tokens'][0] = array_merge($rest_template, restapi_tokens_get()); $displayvars['tokens'][0]['token'] = 1; $displayvars['tokens'][0]['tokenkey'] = 1; $displayvars['tokens'][0]['id'] = 0; if (!$enabled) { $displayvars['tokens'][0]['users'] = is_array($users) ? $users : array("self"); $displayvars['enabled'] = $enabled; } else { $displayvars['tokens'][0]['users'] = is_array($users) ? $users : array("self"); $displayvars['tokens'][0]['rate'] = !empty($rate) ? $rate : "1000"; $displayvars['tokens'][0]['modules'] = is_array($modules) ? $modules : array(); $displayvars['enabled'] = $enabled; } } return array(array("title" => _("Rest API"), "rawname" => "restapi", "content" => load_view(__DIR__ . '/views/hook_userman.php', $displayvars))); break; } } }
$bootstrap_settings['freepbx_auth'] = false; $restrict_mods = array('restapi' => true); if (!@(include_once getenv('FREEPBX_CONF') ? getenv('FREEPBX_CONF') : '/etc/freepbx.conf')) { include_once '/etc/asterisk/freepbx.conf'; } $api = new Api(); $get = array('action:', 'url:', 'verb:', 'nonce:', 'body:', 'token:', 'tokenkey:'); if (php_sapi_name() == 'cli') { $vars = getopt('', $get); foreach ($get as $key) { $k = trim($key, ':'); $vars[$k] = isset($vars[$k]) ? $vars[$k] : ''; } } else { foreach ($get as $key) { $k = trim($key, ':'); $vars[$k] = isset($_GET[$k]) ? $_GET[$k] : ''; } } //print_r($vars); switch ($vars['action']) { case 'nonce': echo 'Nonce: ' . restapi_tokens_generate() . PHP_EOL; break; default: $data = $api->auth->get_data_hash($vars['token'], $vars['url'], $vars['verb'], $vars['nonce'], $vars['body']); $sig = $api->auth->get_signature($data, $vars['tokenkey']); echo 'Body: ' . $data . PHP_EOL; echo 'Signature: ' . $sig . PHP_EOL; break; }