private function setAccessHeaders(\Neuron\Net\Response $response) { $response->setHeader('Access-Control-Allow-Origin', '*'); $response->setHeader('Access-Control-Allow-Methods', 'POST, GET, PUT, DELETE, PATCH, OPTIONS'); $response->setHeader('Access-Control-Allow-Headers', 'origin, x-requested-with, content-type, access_token, authorization'); return $response; }
public function outputContent(Response $response) { if ($response->getBody()) { echo $response->getBody(); } else { echo $response->getData(); } }
public function outputContent(Response $response) { if (!is_string($response->getData())) { $this->printTable($response->getData()); } else { echo $response->getData(); } }
public function outputContent(Response $response) { if (!is_string($response->getData())) { echo '<pre>' . print_r($response->getData()) . '<pre>'; } else { echo $response->getData(); } }
public function outputContent(Response $response) { header('Content-type: application/json'); $out = json_encode($response->getData()); if ($out) { echo $out; } else { if ($error = json_last_error()) { http_response_code(500); echo 'json_encode failed with error code ' . $error; } } }
public function templates() { Template::addPath('lowpriority', '', -5); Template::addPath('regularpriority', '', 0); Template::addPath('highpriority', '', 5); return Response::table(Template::getPaths()); }
/** * @param $token * @return Response */ public function authenticator($token) { $authenticator = $this->module->getAuthenticators()->getFromToken($token); if (!$authenticator) { return Response::error('Authenticator not found', Response::STATUS_NOTFOUND); } $authenticator->setRequest($this->request); return $authenticator->register(); }
public function outputContent(Response $response) { if ($response->getBody()) { echo $response->getBody(); } else { if (!is_string($response->getData())) { print_r($response->getData()); } else { echo $response->getData(); } } }
public function register() { // Must be logged in if (!($user = $this->request->getUser())) { //echo '<p>' . ('This page is only available for registered users.') . '</p>'; $login = URLBuilder::getURL('account/login', array('return' => $this->module->getURL('register', $_GET))); return Response::redirect($login); } if ($this->request->isPost()) { $template = new Template('CatLab/OAuth2/registerdone.phpt'); $clientid = uniqid('oauth2', true); $password = md5(uniqid('secret')); $redirect_url = $this->request->input('redirecturi'); $layout = $this->request->input('layout'); MapperFactory::getApplicationMapper()->create($clientid, $password, $redirect_url, $layout, $this->request->getUser()->getId()); $template->set('clientid', $clientid); $template->set('clientsecret', $password); $template->set('redirecturi', $redirect_url); return Response::template($template); } $template = new Template('CatLab/OAuth2/register.phpt'); $template->set('action', $this->module->getURL('register')); return Response::template($template); }
<?php // Initialize router $router = new \Neuron\Router(); // Accounts module $signinmodule = new \CatLab\Accounts\Module(); $signinmodule->requireEmailValidation(); //$signinmodule->setLayout ('index-account.phpt'); $password = new \CatLab\Accounts\Authenticators\Password(); $signinmodule->addAuthenticator($password); $facebook = new \CatLab\Accounts\Authenticators\Facebook(); $signinmodule->addAuthenticator($facebook); $steam = new \CatLab\Accounts\Authenticators\Steam(); $signinmodule->addAuthenticator($steam); $mailer = new \CatLab\Mailer\Module(); $router->module('/mailer', $mailer); // Make the module available on /account $router->module('/account', $signinmodule); $router->get('/thirdparty', function () { $request = \Neuron\Application::getInstance()->getRouter()->getRequest(); $deligatedAccounts = $request->getUser()->getDeligatedAccounts(); return \Neuron\Net\Response::template('thirdparty.phpt', array('accounts' => $deligatedAccounts)); })->filter('authenticated'); // Catch the default route $router->get('/', function () { return \Neuron\Net\Response::template('home.phpt'); }); return $router;
<?php // Initialize router $router = new \Neuron\Router(); $router->get('/', '\\Example\\Controllers\\HomeController@main'); $router->get('/templates', '\\Example\\Controllers\\HomeController@templates'); $router->get('/test/{something?}', function ($a) { return \Neuron\Net\Response::json($a); }); return $router;
public function cancel() { $cancel = $this->request->getSession()->get('cancel-login-redirect'); if ($cancel) { $this->request->getSession()->set('post-login-redirect', null); $this->request->getSession()->set('cancel-login-redirect', null); return Response::redirect($cancel); } else { return Response::redirect(URLBuilder::getURL('/')); } }
public function login() { $this->initialize(); $helper = new FacebookRedirectLoginHelper(URLBuilder::getAbsoluteURL($this->module->getRoutePath() . '/login/' . $this->getToken(), array('next' => 1))); if (!$this->request->input('next')) { $loginUrl = $helper->getLoginUrl($this->scopes); return Response::redirect($loginUrl); } else { try { $session = $helper->getSessionFromRedirect(); } catch (FacebookRequestException $ex) { // When Facebook returns an error return Response::error($ex->getMessage()); } catch (\Exception $ex) { // When validation fails or other local issues return Response::error($ex->getMessage()); } if ($session) { // Check if this user is already registered. $request = new FacebookRequest($session, 'GET', '/me', array('fields' => 'id,name,gender,verified,locale,timezone,email,birthday,first_name,last_name')); $response = $request->execute(); $graphObject = $response->getGraphObject(); $data = $graphObject->asArray(); // Create an object. $user = new DeligatedUser(); $user->setType('facebook'); $user->setUniqueId($data['id']); $user->setAccessToken((string) $session->getAccessToken()); if (isset($data['name'])) { $user->setName($data['name']); } if (isset($data['gender'])) { switch (strtoupper($data['gender'])) { case DeligatedUser::GENDER_FEMALE: case DeligatedUser::GENDER_MALE: $user->setGender(strtoupper($data['gender'])); break; } } if (isset($data['locale'])) { $user->setLocale($data['locale']); } if (isset($data['email'])) { $user->setEmail($data['email']); } if (isset($data['birthday'])) { if (strlen($data['birthday']) == 10) { $parts = explode('/', $data['birthday']); $user->setBirthday(Carbon::createFromDate($parts[2], $parts[0], $parts[1])); } } if (isset($data['first_name'])) { $user->setFirstname($data['first_name']); } if (isset($data['last_name'])) { $user->setLastname($data['last_name']); } $user->setAvatar('https://graph.facebook.com/' . $user->getUniqueId() . '/picture?type=large'); // Touchy touchy! return $this->setDeligatedUser($user); } } }
/** * Execute the router: Loop all defined before middlewares and routes, and execute the handling function if a mactch was found * * @param Request $request * @return Response */ public function run(Request $request) { // Define which method we need to handle $this->method = $request->getMethod(); // Set request $this->request = $request; // Handle all routes $numHandled = 0; if (isset($this->routes[$this->method])) { $numHandled = $this->handle($this->routes[$this->method], true); } // If no route was handled, trigger the 404 (if any) if ($numHandled == 0) { if ($this->notFound) { //call_user_func($this->notFound); $this->handleMatch($this->notFound, array()); } else { $request = Response::error('Page not found.', Response::STATUS_NOTFOUND); $request->output(); } } // If it originally was a HEAD request, clean up after ourselves by emptying the output buffer if ($_SERVER['REQUEST_METHOD'] == 'HEAD') { ob_end_clean(); } }
private function api(Request $request, $method) { $ch = curl_init(); $post = $request->getBody(); $parsedUrl = $request->getUrl(); if ($request->getParameters()) { if (strpos($parsedUrl, '?')) { $parsedUrl .= '&'; } else { $parsedUrl .= '?'; } $parsedUrl .= http_build_query($request->getParameters()); } curl_setopt($ch, CURLOPT_URL, $parsedUrl); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HEADER, 1); if ($request->getHeaders()) { $headers = $request->getHeaders(); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); } switch ($method) { case 'GET': break; case 'POST': curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $post); break; case 'DELETE': throw new NotImplemented("Not implemented."); break; case 'PUT': curl_setopt($ch, CURLOPT_PUT, 1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $post); break; } $output = curl_exec($ch); // Response $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE); $header = substr($output, 0, $header_size); $body = substr($output, $header_size); $response = Response::fromRaw($body, self::http_parse_headers($header)); curl_close($ch); return $response; }
public function getError($message) { return Response::json(array('error' => array('message' => $message)))->setStatus(401); }
public function getError($message) { return Response::error($message, 401); }
/** * Called after a redirect * @param Request $request * @return Response */ public function postLogout(Request $request) { return Response::redirect(URLBuilder::getURL('/')); }
/** * @return bool|Response|string */ public function register() { $template = new Template('CatLab/Accounts/authenticators/password/register.phpt'); if ($this->request->isPost()) { $email = $this->request->input('email', 'email'); $username = $this->request->input('username', 'username'); $password = $this->request->input('password'); $response = $this->processRegister($email, $username, $password); if ($response instanceof Response) { return $response; } else { if (is_string($response)) { $template->set('error', $response); } } } $template->set('layout', $this->module->getLayout()); $template->set('action', URLBuilder::getURL($this->module->getRoutePath() . '/register/' . $this->getToken())); $template->set('email', $this->request->input('email', 'string')); $template->set('username', $this->request->input('username', 'string')); return Response::template($template); }
/** * Return an error response. * @return Response */ public function getErrorResponse() { return Response::error($this->getError(), self::STATUS_INVALID_INPUT); }
public function routerVerifier(\Neuron\Models\Router\Filter $filter) { if ($filter->getRequest()->getUser()) { return true; } return Response::error('You must be authenticated', Response::STATUS_UNAUTHORIZED); }
public function outputContent(Response $response) { header('Content-type: application/xml'); if (!is_string($response->getData())) { echo self::output_xml($response->getData(), $this->version, $this->root, $this->parameters, $this->itemName); } else { echo $response->getData(); } }
/** * @return Response */ public function register() { return Response::error('Authenticator does not have register method.', Response::STATUS_NOTFOUND); }
private function runFinish() { $consumer = $this->getConsumer(); // Complete the authentication process using the server's // response. $return_to = $this->getReturnTo(); $response = $consumer->complete($return_to); // Check the response status. if ($response->status == Auth_OpenID_CANCEL) { // This means the authentication was cancelled. $msg = 'Verification cancelled.'; } else { if ($response->status == Auth_OpenID_FAILURE) { // Authentication failed; display the error message. $msg = "OpenID authentication failed: " . $response->message; } else { if ($response->status == Auth_OpenID_SUCCESS) { // This means the authentication succeeded; extract the // identity URL and Simple Registration data (if it was // returned). $openid = $response->getDisplayIdentifier(); return $this->afterLogin($openid); } } } return Response::error($msg); }
private function showAuthorizationDialog($clientdata) { $template = new Template('CatLab/OAuth2/authorize.phpt'); $template->set('clientdata', $clientdata); $template->set('action', URLBuilder::getURL('oauth2/authorize', $_GET)); return \Neuron\Net\Response::template($template); }
private function linkExitingAccount(DeligatedUser $deligatedUser) { $page = new Template('CatLab/Accounts/authenticators/deligated/link.phpt'); if ($this->request->isPost()) { $email = $this->request->input('email'); $password = $this->request->input('password'); $response = $this->processLogin($deligatedUser, $email, $password); if ($response instanceof Response) { return $response; } else { if (is_string($response)) { $page->set('error', $response); } } } $page->set('layout', $this->module->getLayout()); $page->set('action', URLBuilder::getURL($this->module->getRoutePath() . '/register/' . $this->getToken(), array('link' => 1))); $page->set('return', URLBuilder::getURL($this->module->getRoutePath() . '/register/' . $this->getToken())); // Name if ($name = $deligatedUser->getWelcomeName()) { $page->set('name', $name); } // Email. if ($email = $this->request->input('email')) { $page->set('email', $email); } else { if ($email = $deligatedUser->getEmail()) { $page->set('email', $email); } else { $page->set('email', ''); } } return Response::template($page); }