\API\Core\DB::initCapsule(); use API\Core\Tool; $app = new \Slim\Slim(); // Loading all REST modules // with their endpoints like that: // inside 'src/endoints' $dir_endpoints = opendir('src/endpoints'); while ($ent = readdir($dir_endpoints)) { // For each .php file if (preg_match('/^(.*)\\.php$/', $ent, $m)) { $endpoint = $m[0]; // Read the file with PHP require 'src/endpoints/' . $endpoint; } } closedir($dir_endpoints); // Logs to error_log specified in virtualhost $app->error(function (\Exception $e) { Tool::endWithJson(["error" => "Server error"], 500); Tool::log($e->getMessage()); }); // JSON 404 response $app->notFound(function () { Tool::endWithJson(["error" => "invalid endpoint"], 404); }); // Welcoming browsers when they reach /api $app->get('/', function () use($app) { echo file_get_contents(__DIR__ . '/welcome.html'); }); // Ready to serve with Slim $app->run();
public function log() { global $resourceServer; global $app; $accessToken = null; $userId = null; $url = null; try { $url = $app->request->getResourceUri(); } catch (\Exception $e) { } try { $resourceServer->isValidRequest(); $_accessToken = $resourceServer->getAccessToken(); $accessToken = $_accessToken->getId(); $userId = $_accessToken->getSession()->getOwnerId(); } catch (\Exception $e) { } Tool::log(($accessToken && !$userId ? '[anonymous]' : '') . ($accessToken ? '[' . $accessToken . '] ' : '') . ($userId ? '(' . $userId . ') ' : '') . ($url ? '[' . $app->request->getMethod() . ' ' . $url . '] ' : '') . $this->getRepresentation() . ($this->parent ? ' because of ' . get_class($this->parent) . ' thrown at ' . $this->parent->getFile() . ' line ' . $this->parent->getLine() : '')); }
// this is OUR access token, not the provider's one $accessToken = AccessToken::where('token', '=', $_COOKIE['access_token'])->first(); setcookie('access_token', '', 1, '/'); if (!$accessToken) { Tool::endWithJson(["error" => "You provided a wrong access_token via cookie"]); } else { $user_id = $accessToken->session->user->id; } } else { $alreadyAuthed = false; } $external_account_infos = $oAuth->getInfos($token); if ($alreadyAuthed) { $user = User::where('id', '=', $user_id)->first(); if (!$user) { Tool::log('warning: session has unexisting user_id ' . $user_id); Tool::endWithJson(["error" => "Service error"], 400); } $externalAccount = $user->externalAccounts()->where('external_user_id', '=', $external_account_infos['id'])->where('service', '=', $service)->first(); if (!$externalAccount) { if ($_externalAccount = UserExternalAccount::where('external_user_id', '=', $external_account_infos['id'])->first()) { $data['error'] = 'EXTERNAL_ACCOUNT_ALREADY_PAIRED'; } else { $externalAccount = new UserExternalAccount(); $externalAccount->external_user_id = $external_account_infos['id']; $externalAccount->token = $token; $externalAccount->service = $service; $user->externalAccounts()->save($externalAccount); $data['external_account_linked'] = true; } } else {