/** * Require a user to be logged in. Redirects to /login if a session is not found. * @return int|bool */ protected function _requireLogin() { $id = $this->_getUser(); if (!$id) { $fw = \App::fw(); if (empty($_GET)) { $fw->reroute('/login?to=' . urlencode($fw->get('PATH'))); } else { $fw->reroute('/login?to=' . urlencode($fw->get('PATH')) . urlencode('?' . http_build_query($_GET))); } $fw->unload(); } return $id; }
/** * Call the API * @param string $path * @param string $method * @param array|null $data * @param array|null $headers * @return mixed */ static function call($path, $method = 'GET', array $data = null, array $headers = null) { // Build curl request $url = self::baseUrl() . ltrim($path, '/'); $token = \App::fw()->get('COOKIE.session_token'); if (strtoupper($method) == 'GET') { if ($token) { $data['_token'] = $token; } if ($data !== null) { $url .= '?' . http_build_query($data); } $curl = curl_init($url); } else { if ($token) { $url .= '?_token=' . urlencode($token); } $curl = curl_init($url); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); } // Set curl options curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // Add any custom headers if ($headers) { curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); } // Disable SSL checks during development if (\App::fw()->get('DEBUG') >= 2) { curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); } // Perform request $data = curl_exec($curl); curl_close($curl); // Decode object $obj = json_decode($data); if ($obj !== null) { return $obj; } return $data; }
<?php $fw = App::fw(); // Index (public pages) $fw->route('GET /', 'Controller\\Index->index'); $fw->route('GET /style', 'Controller\\Index->style'); $fw->route('GET /manifest.json', 'Controller\\Index->manifest'); $fw->route('GET /register', 'Controller\\Index->register'); $fw->route('POST /register', 'Controller\\Index->registerPost'); $fw->route('GET /auth', 'Controller\\Index->auth'); $fw->route('POST /cspreport', 'Controller\\Index->cspreport'); // Users $fw->route('GET /stream', 'Controller\\User->stream'); $fw->route('GET /u/@username', 'Controller\\User->base'); $fw->route('GET|POST /logout', 'Controller\\User->logout'); // Posts $fw->route('GET /post', 'Controller\\Post->post'); $fw->route('GET /post/@id', 'Controller\\Post->single'); // Handle errors $fw->set('ONERROR', function (Base $fw) { $controller = new Controller\Index(); switch ($fw->get('ERROR.code')) { case 404: $fw->set('title', '404 Not Found'); echo Template::instance()->render("error/404.html"); break; case 401: $fw->set('title', '404 Not Authorized'); echo Template::instance()->render("error/401.html"); break; default:
/** * Get the stream posts * @return object */ static function getStream() { // TODO: Display buddy posts $user = \App::fw()->get('user'); return self::call('u/' . $user->username . '/posts.json'); }