public static function handleRequestRequest()
 {
     $GLOBALS['Session']->requireAuthentication();
     // get key
     if (empty($_REQUEST['endpoint'])) {
         return static::throwInvalidRequestError('endpoint required');
     }
     if (!($Endpoint = Endpoint::getByHandle($_REQUEST['endpoint']))) {
         return static::throwNotFoundError('Endpoint not found');
     }
     if (!$Endpoint->KeySelfRegistration) {
         return static::throwUnauthorizedError('key registration not available');
     }
     if ($_SERVER['REQUEST_METHOD'] == 'POST') {
         if (empty($_POST['OwnerName'])) {
             return static::throwInvalidRequestError('OwnerName required');
         }
         // create Key
         $Key = Key::create(['OwnerName' => $_POST['OwnerName'], 'ContactName' => $GLOBALS['Session']->Person->FullName, 'ContactEmail' => $GLOBALS['Session']->Person->Email], true);
         $KeyEndpoint = KeyEndpoint::create(['Key' => $Key, 'Endpoint' => $Endpoint], true);
         $KeyUser = KeyUser::create(['Key' => $Key, 'Person' => $GLOBALS['Session']->Person, 'Role' => 'owner'], true);
         return static::respond('keyIssued', ['data' => $Key]);
     }
     return static::respond('request', ['Endpoint' => $Endpoint]);
 }
 public static function handleBrowseRequest($options = [], $conditions = [], $responseID = null, $responseData = [])
 {
     // apply status filter
     if (empty($_GET['status'])) {
         $status = 'open';
     } elseif ($_GET['status'] == 'any') {
         $status = null;
     } elseif (in_array($_GET['status'], AbstractAlert::getFieldOptions('Status', 'values'))) {
         $status = $_GET['status'];
     } else {
         $status = 'open';
     }
     if ($status) {
         $responseData['status'] = $conditions['Status'] = $status;
     }
     // apply endpoint filter
     if (!empty($_GET['endpoint'])) {
         if (!($Endpoint = Endpoint::getByHandle($_GET['endpoint']))) {
             return static::throwNotFoundError('Endpoint not found');
         }
     }
     if (isset($Endpoint)) {
         $conditions['EndpointID'] = $Endpoint->ID;
         $responseData['Endpoint'] = $Endpoint;
     }
     return parent::handleBrowseRequest($options, $conditions, $responseID, $responseData);
 }
 public static function handleBrowseRequest($options = [], $conditions = [], $responseID = null, $responseData = [])
 {
     // apply endpoint filter
     if (!empty($_REQUEST['endpoint'])) {
         if (!($Endpoint = Endpoint::getByHandle($_REQUEST['endpoint']))) {
             return static::throwNotFoundError('Endpoint not found');
         }
         $conditions['EndpointID'] = $Endpoint->ID;
         $responseData['Endpoint'] = $Endpoint;
     }
     // apply method filter
     if (!empty($_REQUEST['method'])) {
         $conditions['Method'] = $_REQUEST['method'];
     }
     // apply path filter
     if (!empty($_REQUEST['path-substring'])) {
         $conditions[] = 'Path LIKE "%' . DB::escape($_REQUEST['path-substring']) . '%"';
     }
     // apply path filter
     if (!empty($_REQUEST['query-substring'])) {
         $conditions[] = 'Query LIKE "%' . DB::escape($_REQUEST['query-substring']) . '%"';
     }
     // apply IP filter
     if (!empty($_REQUEST['ip'])) {
         if (!filter_var($_REQUEST['ip'], FILTER_VALIDATE_IP)) {
             return static::throwError('IP is invalid');
         }
         $conditions['ClientIP'] = ip2long($_REQUEST['ip']);
     }
     // apply key filter
     if (!empty($_REQUEST['key'])) {
         if (!($Key = Key::getByKey($_REQUEST['key']))) {
             return static::throwError('key is invalid');
         }
         $conditions['KeyID'] = $Key->ID;
     }
     // apply time filter
     if (!empty($_REQUEST['time-max']) && ($timeMax = strtotime($_REQUEST['time-max']))) {
         $conditions[] = 'Created <= "' . date('Y-m-d H:i:s', $timeMax) . '"';
     }
     if (!empty($_REQUEST['time-min']) && ($timeMin = strtotime($_REQUEST['time-min']))) {
         $conditions[] = 'Created >= "' . date('Y-m-d H:i:s', $timeMin) . '"';
     }
     // apply type filter
     if (!empty($_REQUEST['type'])) {
         if ($_REQUEST['type'] == 'ping') {
             $conditions['Class'] = PingTransaction::class;
         } elseif ($_REQUEST['type'] == 'consumer') {
             $conditions['Class'] = Transaction::class;
         }
     }
     return parent::handleBrowseRequest($options, $conditions, $responseID, $responseData);
 }
 public static function handleRequest()
 {
     $GLOBALS['Session']->requireAccountLevel('Staff');
     if (empty($_REQUEST['endpoint'])) {
         return static::throwInvalidRequestError('endpoint required');
     } elseif (!($Endpoint = Endpoint::getByHandle($_REQUEST['endpoint']))) {
         return static::throwNotFoundError('Endpoint not found');
     }
     $cachedResponses = $Endpoint->getCachedResponses();
     $limit = isset($_GET['limit']) && ctype_digit($_GET['limit']) ? (int) $_GET['limit'] : static::$defaultLimit;
     $offset = isset($_GET['offset']) && ctype_digit($_GET['offset']) ? (int) $_GET['offset'] : 0;
     return static::respond('cachedResponses', ['success' => true, 'data' => $limit ? array_slice($cachedResponses, $offset, $limit) : $cachedResponses, 'total' => count($cachedResponses), 'limit' => $limit, 'offset' => $offset, 'Endpoint' => $Endpoint]);
 }
 public static function handleRequest()
 {
     $GLOBALS['Session']->requireAccountLevel('Staff');
     if (empty($_GET['time-max']) || !($timeMax = strtotime($_GET['time-max']))) {
         $timeMax = time();
     }
     if (empty($_GET['time-min']) || !($timeMin = strtotime($_GET['time-min']))) {
         $timeMin = $timeMax - 3600 * 24 * 7;
         // 1 week
     }
     if (!empty($_GET['endpoint'])) {
         if (!($Endpoint = Endpoint::getByHandle($_GET['endpoint']))) {
             return static::throwNotFoundError('endpoint not found');
         }
     }
     $topUsers = DB::allRecords('SELECT' . '  @user := SUBSTRING_INDEX(SUBSTRING_INDEX(`Key`, "/", -2), "/", 1) AS User,' . '  SUBSTRING_INDEX(@user, ":", 1) AS UserType,' . '  SUBSTRING_INDEX(@user, ":", -1) AS UserIdentifier,' . '  SUM(Value) AS TotalRequests,' . '  MIN(Timestamp) AS EarliestRequest,' . '  MAX(Timestamp) AS LatestRequest' . ' FROM `%s`' . ' WHERE' . '  `Timestamp` BETWEEN "%s" AND "%s" AND ' . '  `Key` LIKE "endpoints/%s/users/%%/requests"' . ' GROUP BY User' . ' ORDER BY TotalRequests DESC' . ' LIMIT %u', [MetricSample::$tableName, date('Y-m-d H:i:s', $timeMin), date('Y-m-d H:i:s', $timeMax), $Endpoint ? $Endpoint->ID : '%', !empty($_GET['limit']) && ctype_digit($_GET['limit']) ? $_GET['limit'] : 20]);
     return static::respond('topUsers', ['data' => $topUsers, 'Endpoint' => $Endpoint]);
 }