getSession(); } catch (DatabaseOperationException $e) { throw new StandardRpcException('An error occurred while trying to get the session', StandardError::INTERNAL_SERVER_ERROR, $e); } // Check for session conditions if(!$session->flagExists(SessionFlags::VER_IMAGE_CAPTCHA)) { return $rpcRequest->produceError(StandardError::METHOD_NOT_ALLOWED, 'Solving an image captcha is not required at this time'); } try { $peer = $request->getPeer(); if(CaptchaManager::captchaExists($peer)) { $captchaRecord = CaptchaManager::getCaptcha($peer); if($captchaRecord->isExpired()) { $answer = CaptchaManager::createCaptcha($peer); $captchaRecord = CaptchaManager::getCaptcha($peer); } else { $answer = $captchaRecord->getAnswer(); } } else { $answer = CaptchaManager::createCaptcha($peer); $captchaRecord = CaptchaManager::getCaptcha($peer); } } catch (DatabaseOperationException $e) { throw new StandardRpcException("There was an unexpected error while trying create the captcha", StandardError::INTERNAL_SERVER_ERROR, $e); } // Build the captcha // Returns HTML base64 encoded image of the captcha // Important note: Must always be HTML-BASE64 which means it must be prefixed with `data:image/jpeg;base64,` return $rpcRequest->produceResponse(new ImageCaptchaVerification([ 'expires' => $captchaRecord->getExpires(), 'content' => (new CaptchaBuilder($answer))->build()->inline() ])); } }