示例#1
0
 function __invoke($req, $res, $next)
 {
     $apip = apip::getInstance();
     $captchaType = $req->getAttribute('route')->getArgument('AliceSPA_CaptchaType');
     $body = $req->getParsedBody();
     if (!empty($captchaType) && !empty($body) && !empty($body['AliceSPA_Captcha'])) {
         $captcha = $body['AliceSPA_Captcha'];
         $validTime = null;
         if ($captchaType === 'image') {
             $validTime = configHelper::getCoreConfig()['imageCaptchaValidTime'];
         }
         if ($captchaType === 'SMS') {
             $validTime = configHelper::getCoreConfig()['SMSCaptchaValidTime'];
         }
         $r = VCManager::getInstance()->check($captcha['id'], $captcha['code'], $captchaType, $validTime);
         if ($r === false) {
             $apip->pushError(6);
             return $res;
         }
     } else {
         $apip->pushError(6);
         return $res;
     }
     return $next($req, $res);
 }
示例#2
0
 function __invoke($req, $res, $next)
 {
     $apip = apip::getInstance();
     $userId = utils::getRequestHeader($req, 'AliceSPA-UserID');
     $webToken = utils::getRequestHeader($req, 'AliceSPA-WebToken');
     $userId = empty($userId) ? null : $userId[0];
     $webToken = empty($webToken) ? null : $webToken[0];
     if ($userId === null || $webToken === null) {
         $apip->pushError(3);
         return $res;
     }
     $r = utils::disposeAPIException(function () use($userId, $webToken) {
         return authService::getInstance()->authenticateByWebToken($userId, $webToken);
     }, [1 => ['dispel' => 3, 'dispelPushError' => false]]);
     if ($r === false) {
         $apip->pushError(3);
         return $res;
     }
     $roles = $req->getAttribute('route')->getArgument('AliceSPA_Roles');
     $r = authService::getInstance()->checkRoles($roles);
     if ($r === false) {
         $apip->pushError(5);
         return $res;
     }
     return $next($req, $res);
 }
示例#3
0
 function __invoke($req, $res, $next)
 {
     $apip = apip::getInstance();
     $ddbRule = $req->getAttribute('route')->getArgument('AliceSPA_DirectDatabase');
     if (!$req->isPost()) {
         $apip->pushError(7);
         return $res;
     }
     $body = $req->getParsedBody();
     $ddbr = ddb::getInstance()->do($ddbRule, $body);
     if ($ddbr !== false) {
         $apip->setData($ddbr);
     }
     return $res;
 }
示例#4
0
 function __invoke($req, $res, $next)
 {
     if ($req->isOptions()) {
         return $res;
     }
     $sessionId = utils::getRequestHeader($req, 'AliceSPA-SessionID');
     if (!empty($sessionId)) {
         $sessionId = $sessionId[0];
     }
     $sessionId = sessionServ::getInstance()->loadSession($sessionId);
     apip::getInstance()->setSessionId($sessionId);
     $res = $next($req, $res);
     sessionServ::getInstance()->storeSession($sessionId);
     return $res;
 }
示例#5
0
 function __invoke($req, $res, $next)
 {
     $apip = apip::getInstance();
     try {
         // Cache APIExceptoin instance, api protocol should deal with it and fill the response body.
         $res = $next($req, $res);
     } catch (APIException $e) {
         $apip->pushError($e->getCode());
         $apip->setAPIException($e);
     }
     if ($apip->isEnabled()) {
         $res = $apip->flush($res);
     }
     return $res;
 }
示例#6
0
 public function getErrors($req, $res, $args)
 {
     $errors = configHelper::getErrors();
     apip::getInstance()->setData($errors);
 }
示例#7
0
 public static function disposeAPIException($callable, $map, $successCallback = null, $dispelCallback = null)
 {
     $r = null;
     try {
         $r = $callable();
         $successCallback && $successCallback($r);
     } catch (APIException $e) {
         $oCode = $e->getCode();
         if (!empty($map[$oCode])) {
             if (!empty($map[$oCode]['change'])) {
                 $e->setCode($map[$oCode]['change']);
                 throw $e;
             }
             if (!empty($map[$oCode]['dispel'])) {
                 $r = $map[$oCode]['dispel'];
                 if (!($map[$oCode]['dispelPushError'] === false)) {
                     apip::getInstance()->pushError($r);
                 }
                 $dispelCallback && $dispelCallback($r);
                 return false;
             }
         }
     }
     return $r;
 }
示例#8
0
        $this->isEnabled = false;
    }
    function isEnabled()
    {
        return $this->isEnabled;
    }
    function setSessionId($id)
    {
        $this->data['sessionID'] = $id;
    }
    function pushError($err)
    {
        $this->data['errors'][] = $err;
        $this->setFailure();
    }
    function setAPIException($e)
    {
        if (configHelper::getCoreConfig()['showAPIExceptoin']) {
            $edata = [];
            $edata['code'] = $e->getCode();
            $edata['message'] = $e->getMessage();
            $edata['file'] = $e->getFile();
            $edata['line'] = $e->getLine();
            $edata['trace'] = $e->getTrace();
            $this->data['APIException'] = $edata;
        }
    }
}
$container['apip'] = function () {
    return \AliceSPA\Service\APIProtocol::getInstance();
};