/** * / * @param [type] $app [description] * @return [type] [description] */ public function route($app) { /* * UPLOAD */ $app->get('/upload', function () use($app) { $app->render('upload'); }); $app->post('/upload', function () use($app) { $validator = new \FileUpload\Validator\Simple(1024 * 1024 * 2, $app->allowedFileTypes); $pathresolver = new \FileUpload\PathResolver\Simple('/var/www/html/files/'); $filesystem = new \FileUpload\FileSystem\Simple(); $fileupload = new \FileUpload\FileUpload($_FILES['files'], $_SERVER); $fileupload->setPathResolver($pathresolver); $fileupload->setFileSystem($filesystem); $fileupload->addValidator($validator); list($files, $headers) = $fileupload->processAll(); foreach ($headers as $header => $value) { $app->response->headers->set($header . ': ' . $value); } $body = json_encode(array('files' => $files)); $app->response->setBody($body); $app->stop(); }); return $app; }
/** * / * @param [type] $app [description] * @return [type] [description] */ public function route($app) { $app->view->setLayout('frontend'); $app->get('/login', function () use($app) { $app->render('login'); })->name('user_login_form'); $app->post('/login', function () use($app) { $credentials = $app->handler->handlePostRequest(); $app->doorman->login($credentials); if ($app->doorman->isUserAuthenticated()) { $app->redirect('admin_panel'); } else { $app->redirect('user_login_form'); } })->name('user_login'); $app->get('/signup', function () use($app) { $app->render('signup'); })->name('user_signup_form'); $app->post('/signup', function () use($app) { $record = new Record($app->request->post()); $token = User::query($app->db, 'signUp', $record); $sent = $app->handler->sendSignUpConfirmation($record, $token); $app->redirect('user_login_form'); })->name('user_signup'); $app->get('/signup/confirm/:token', function ($user_id, $token) use($app) { $result = User::query($app->db, 'validate', $token, 'activation'); if ((int) $result->isSuccessful()) { $app->redirect('user_login'); } $app->errorForbidden(); })->name('confirm_signup')->conditions(array('token' => '\\w+')); $app->get('/password/forgot', function () use($app) { $app->render('password.forgot'); })->name('forgot_password_form'); $app->post('/password/forgot', function () use($app) { $record = new Record($app->request->post()); $token = User::query($app->db, 'forgot', $record->get('user_id')); $sent = $app->handler->sendPasswordReset($record, $token); $app->redirect('user_login_form'); })->name('forgot_password'); $app->get('/password/new/:token', function ($token) use($app) { $result = User::query($app->db, 'validate', $token, 'forgot'); if ($result->isSuccessful()) { $app->render('password.new'); $app->stop(); } else { $app->handler->errorForbidden(); } })->name('new_password_form')->conditions(array('token' => '\\w+')); $app->post('/password/new', function () use($app) { $record = new Record($app->request->post()); $result = User::query($app->db, 'resetPassword', $record->get('new_password')); $app->redirect($app->urlFor('user_login_form')); })->name('new_password'); return $app; }
/** * / * @param [type] $app [description] * @return [type] [description] */ public function route($app) { $app->get('/admin/:appliance', function ($appliance) use($app) { $app->response->headers->set('Content-Type', 'text/html'); $info = array('' => ''); $app->handler->setViewInfo($info); $body = $app->handler->render($appliance); $app->response->setBody($body); $app->stop(); }); }
/** * / * @param [type] $status [description] * @param [type] $message [description] * @return [type] [description] */ public function error($status, $message) { $this->app->response->setStatus($status); $result = array('success' => false, 'message' => $message); if ($this->app->request->isAjax()) { $this->app->response->headers->set('Content-Type', 'application/json'); $this->response->setBody($result->toJson()); } else { $this->app->response->headers->set('Content-Type', 'text/html'); $this->setDefaultViewResources(); $this->app->view->setLayout('frontend'); $this->app->render('error', $result); } $this->app->stop(); }
/** * / * @param [type] $app [description] * @return [type] [description] */ public function route($app) { /* * MUSTACHE TEMPLATES */ $app->get('/template/:name', function ($name) use($app) { $file = '/public/assets/mustache/' . $name . '.mustache'; if (!is_file($file) || !is_readable($file)) { throw new \Exception("File not accessible", 1); } $body = file_get_contents($file); if (!$body) { throw new \Exception("File not accessible", 1); } $app->response->headers->set('Content-Type', $type); $app->response->setBody($body); $app->stop(); }); /* * ASSETS */ $app->get('/asset/:name/:extension', function ($name, $extension) use($app) { if (in_array($extension, array('css', 'js', 'gif', 'jpg', 'jpeg', 'png'))) { switch ($extension) { case 'css': $type = 'text/css'; break; case 'js': $type = 'application/javascript'; break; default: throw new \Exception("Unrecognized asset mimetype.", 01); break; } $file = '/public/assets/' . $extension . '/' . $name . '.' . $extension; if (!is_file($file) || !is_readable($file)) { throw new \Exception("File not accessible", 1); } $body = file_get_contents($file); if (!$body) { throw new \Exception("Empty file", 1); } $app->response->headers->set('Content-Type', $type); $app->response->setBody($body); } $app->stop(); }); /* * IMAGE FILES * (TODO: implement Imagine PHP lib) */ $app->get('/media/:name/:extension', function ($name, $extension) use($app) { if (in_array($extension, array('gif', 'jpg', 'jpeg', 'png'))) { switch ($extension) { case 'jpg': $type = 'image/jpeg'; break; case 'jpeg': $type = 'image/jpeg'; break; case 'gif': $type = 'image/gif'; break; case 'png': $type = 'image/png'; break; default: throw new \Exception("Unrecognized media mimetype.", 01); break; } $app->response->headers->set('Content-Type', $type); $file = '/public/media/' . $name . '.' . $extension; if (!is_file($file) || !is_readable($file)) { throw new \Exception("File not accessible", 1); } $body = file_get_contents($file); if (!$body) { throw new \Exception("Empty file", 1); } $app->response->setBody($body); } $app->stop(); }); /* * FORCE DOWNLOAD * (TODO: log downloaded files) */ $app->get('/download/:name/:extension', function ($name, $extension) use($app) { $file = '/public/media/' . $name . '.' . $extension; if (!is_file($file) || !is_readable($file)) { throw new \Exception("File not accessible", 1); } $body = file_get_contents('/public/media/' . $name . '.' . $extension); if (!$body) { throw new \Exception("Empty file", 1); } $mimes = $app->config('mimetypes.download'); if (isset($mimes[$extension])) { $app->response->headers->set('Content-Type', $mimes[$extension]); $app->response->setBody($body); } $app->stop(); }); return $app; }