/** * Adds a backend routes * @param $appInstance * @return void */ public static function addRouteDefinitions(Slim $appInstance) { $appInstance->group('/admin', function () use($appInstance) { $appInstance->get('/', function () { print '<h1>A Simple Backend</h1>'; }); $appInstance->map("/chpass", function () use($appInstance) { if (EMA_ADMIN_CHPASS) { AdminPasswordChange_controller::process(); } else { $appInstance->pass(); } })->via('GET', 'POST'); $appInstance->map("/update", function () use($appInstance) { ClassAndMethodsDispatcher::updateGPMethods(); })->via('GET', 'POST'); $appInstance->post("/login", function () use($appInstance) { $appInstance->response->headers->set('Cache-Control', 'no-store'); if (isset($_POST['username']) && is_string($_POST['username']) && (isset($_POST['password']) && is_string($_POST['password']))) { try { try { $user = new UserAuth(); } catch (SessionExpired $e) { $user = new UserAuth(); } $user->userLogin($_POST['username'], $_POST['password']); if (!$user->isAdmin()) { $user->logout(); throw new LoginIncorrect('You are not allowed to login here'); } $appInstance->response->headers->set('Content-Type', 'application/json'); print json_encode($user->getSessionAuthData()); } catch (LoginIncorrect $e) { $appInstance->response->headers->set('Content-Type', 'text/plain'); $appInstance->response->setStatus(400); print $e->getMessage(); } } else { $appInstance->response->headers->set('Content-Type', 'text/plain'); $appInstance->response->setStatus(400); print 'Bad request'; } }); $appInstance->map('/logout', function () use($appInstance) { try { $user = new UserAuth(); if ($user->isUserLoggedInSimple()) { $user->logout(); } } catch (SessionExpired $e) { } })->via('GET', 'POST'); }); }
/** * This methods will be called at application startup * @param $appInstance * @return void */ public static function addRouteDefinitions(Slim $appInstance) { $appInstance->group('/nats', function () use($appInstance) { $appInstance->map('/users', function () use($appInstance) { try { $inst = new cNatsMembersPostback(); print 'OK|' . $inst->run(); } catch (InputError $e) { print 'NOTOK|' . $e->getMessage(); } catch (FatalError $e) { print 'ERROR|' . $e->getMessage(); } })->via('GET', 'POST'); }); }
/** * @param string $routeName * @param string $tableName * @param callable $customCRUDFunction * @param string $displayName */ public function add($routeName, $customCRUDFunction = null, $tableName = null, $displayName = null) { if ($tableName == null) { $tableName = $routeName; } $this->tableList[$routeName] = $tableName; $this->tableDisplayName[$routeName] = $displayName; $this->routeNameList[] = $routeName; /* * Page Group (ListView, CreateView, EditView) */ $this->slim->group("/" . $this->groupName . "/" . $routeName, function () use($routeName, $customCRUDFunction, $tableName) { $this->slim->get("/", function () use($routeName) { $this->slim->redirectTo("_louisCRUD_" . $routeName); }); /* * ListView */ $this->slim->get("/list(/:p1(/:p2(/:p3(/:p4(/:p5)))))", function ($p1 = null, $p2 = null, $p3 = null, $p4 = null, $p5 = null) use($routeName, $customCRUDFunction, $tableName) { // MUST INIT FIRST $this->init($tableName, $routeName, $p1, $p2, $p3, $p4, $p5); if ($this->configFunction != null) { $function = $this->configFunction; $result = $function(); if ($result === false) { return; } } if ($customCRUDFunction != null) { $result = $customCRUDFunction($p1, $p2, $p3, $p4, $p5); if ($result === false) { return; } } if ($this->listviewFunction != null) { $listviewFunction = $this->listviewFunction; $result = $listviewFunction($p1, $p2, $p3, $p4, $p5); if ($result === false) { return; } } if ($this->isEnabledListView()) { $this->renderListView(); } })->name("_louisCRUD_" . $routeName); /* * Create */ $this->slim->get("/create(/:p1(/:p2(/:p3(/:p4(/:p5)))))", function ($p1 = null, $p2 = null, $p3 = null, $p4 = null, $p5 = null) use($routeName, $customCRUDFunction, $tableName) { // MUST INIT FIRST $this->init($tableName, $routeName, $p1, $p2, $p3, $p4, $p5); if ($this->configFunction != null) { $function = $this->configFunction; $result = $function(); if ($result === false) { return; } } if ($customCRUDFunction != null) { $result = $customCRUDFunction($p1, $p2, $p3, $p4, $p5); if ($result === false) { return; } } if ($this->createFunction != null) { $createFunction = $this->createFunction; $result = $createFunction($p1, $p2, $p3, $p4, $p5); if ($result === false) { return; } } // Force Hide ID field $this->field("id")->hide(); if ($this->isEnabledCreate()) { $this->renderCreateView(); } }); /* * Edit */ $this->slim->get("/edit/:id(/:p1(/:p2(/:p3(/:p4(/:p5)))))", function ($id, $p1 = null, $p2 = null, $p3 = null, $p4 = null, $p5 = null) use($routeName, $customCRUDFunction, $tableName) { // MUST INIT FIRST $this->init($tableName, $routeName, $p1, $p2, $p3, $p4, $p5); // Load Bean first $this->loadBean($id); // ID must be hidden $this->field("id")->hide(); if ($this->configFunction != null) { $function = $this->configFunction; $result = $function(); if ($result === false) { return; } } if ($customCRUDFunction != null) { $result = $customCRUDFunction($p1, $p2, $p3, $p4, $p5); if ($result === false) { return; } } if ($this->editFunction != null) { $editFunction = $this->editFunction; $result = $editFunction($id, $p1, $p2, $p3, $p4, $p5); if ($result === false) { return; } } // If user show the ID field, force set it to readonly $this->field("id")->setReadOnly(true); if ($this->isEnabledEdit()) { $this->renderEditView(); } }); /* * Export Excel */ $this->slim->map("/export(/:p1(/:p2(/:p3(/:p4(/:p5)))))", function ($p1 = null, $p2 = null, $p3 = null, $p4 = null, $p5 = null) use($routeName, $customCRUDFunction, $tableName) { // MUST INIT FIRST $this->init($tableName, $routeName, $p1, $p2, $p3, $p4, $p5); if ($this->configFunction != null) { $function = $this->configFunction; $result = $function(); if ($result === false) { return; } } if ($customCRUDFunction != null) { $result = $customCRUDFunction($p1, $p2, $p3, $p4, $p5); if ($result === false) { return; } } if ($this->listviewFunction != null) { $listviewFunction = $this->listviewFunction; $result = $listviewFunction($p1, $p2, $p3, $p4, $p5); if ($result === false) { return; } } if ($this->exportFunction != null) { $exportFunction = $this->exportFunction; $result = $exportFunction($p1, $p2, $p3, $p4, $p5); if ($result === false) { return; } } // TODO: isEnabledExport(); $this->renderExcel(); })->via('GET', 'POST'); }); /* * API Group, RESTful style. */ $this->slim->group("/" . $this->apiGroupName . "/" . $routeName, function () use($routeName, $customCRUDFunction, $tableName) { /* * JSON for Listview */ $this->slim->map("/list(/:p1(/:p2(/:p3(/:p4(/:p5)))))", function ($p1 = null, $p2 = null, $p3 = null, $p4 = null, $p5 = null) use($routeName, $customCRUDFunction, $tableName) { $this->enableJSONResponse(); // MUST INIT FIRST $this->init($tableName, $routeName, $p1, $p2, $p3, $p4, $p5); if ($this->configFunction != null) { $function = $this->configFunction; $result = $function(); if ($result === false) { return; } } if ($customCRUDFunction != null) { $result = $customCRUDFunction($p1, $p2, $p3, $p4, $p5); if ($result === false) { return; } } if ($this->listviewFunction != null) { $listviewFunction = $this->listviewFunction; $result = $listviewFunction($p1, $p2, $p3, $p4, $p5); if ($result === false) { return; } } if ($this->isEnabledListView()) { $this->getJSONList(); } return; })->via('GET', 'POST'); /* * For Datatables */ $this->slim->map("/datatables(/:p1(/:p2(/:p3(/:p4(/:p5)))))", function ($p1 = null, $p2 = null, $p3 = null, $p4 = null, $p5 = null) use($routeName, $customCRUDFunction, $tableName) { $this->enableJSONResponse(); // MUST INIT FIRST $this->init($tableName, $routeName, $p1, $p2, $p3, $p4, $p5); if ($this->configFunction != null) { $function = $this->configFunction; $result = $function(); if ($result === false) { return; } } if ($customCRUDFunction != null) { $result = $customCRUDFunction($p1, $p2, $p3, $p4, $p5); if ($result === false) { return; } } if ($this->listviewFunction != null) { $listviewFunction = $this->listviewFunction; $result = $listviewFunction($p1, $p2, $p3, $p4, $p5); if ($result === false) { return; } } if ($this->isEnabledListView()) { $this->getListViewJSONString(); } return; })->via('GET', 'POST'); /* * View a bean * PUT /api/{tableName}/{id} */ $this->slim->get("/:id(/:p1(/:p2(/:p3(/:p4(/:p5)))))", function ($id, $p1 = null, $p2 = null, $p3 = null, $p4 = null, $p5 = null) use($routeName, $customCRUDFunction, $tableName) { // MUST INIT FIRST $this->init($tableName, $routeName, $p1, $p2, $p3, $p4, $p5); // Load Bean $this->loadBean($id); if ($this->configFunction != null) { $function = $this->configFunction; $result = $function(); if ($result === false) { return; } } // Custom Global Function if ($customCRUDFunction != null) { $result = $customCRUDFunction($p1, $p2, $p3, $p4, $p5); if ($result === false) { return; } } // Custom Edit Function if ($this->editFunction != null) { $editFunction = $this->editFunction; $result = $editFunction($id, $p1, $p2, $p3, $p4, $p5); if ($result === false) { return; } } // Force hide ID $this->field("id")->hide(); // Insert into database if ($this->isEnabledEdit()) { $json = $this->getJSON(false); $this->enableJSONResponse(); echo $json; } }); /* * Insert a bean * POST /api/{tableName} */ $this->slim->post("(/:p1(/:p2(/:p3(/:p4(/:p5)))))", function ($p1 = null, $p2 = null, $p3 = null, $p4 = null, $p5 = null) use($routeName, $customCRUDFunction, $tableName) { // MUST INIT FIRST $this->init($tableName, $routeName, $p1, $p2, $p3, $p4, $p5); if ($this->configFunction != null) { $function = $this->configFunction; $result = $function(); if ($result === false) { return; } } if ($customCRUDFunction != null) { $result = $customCRUDFunction($p1, $p2, $p3, $p4, $p5); if ($result === false) { return; } } // Custom Global Function $result = $customCRUDFunction($p1, $p2, $p3, $p4, $p5); if ($result === false) { return; } // Custom Create Function if ($this->createFunction != null) { $createFunction = $this->createFunction; $result = $createFunction($p1, $p2, $p3, $p4, $p5); } if ($result === false) { return; } // Force hide ID $this->field("id")->hide(); // Insert into database if ($this->isEnabledCreate()) { $jsonObject = $this->insertBean($_POST); $this->enableJSONResponse(); echo json_encode($jsonObject); } else { // TODO: Should be json object echo "No permission"; } }); /* * Update a bean * PUT /crud/{tableName}/{id} */ $this->slim->put("/:id(/:p1(/:p2(/:p3(/:p4(/:p5)))))", function ($id, $p1 = null, $p2 = null, $p3 = null, $p4 = null, $p5 = null) use($routeName, $customCRUDFunction, $tableName) { // MUST INIT FIRST $this->init($tableName, $routeName, $p1, $p2, $p3, $p4, $p5); // Load Bean $this->loadBean($id); if ($this->configFunction != null) { $function = $this->configFunction; $result = $function(); if ($result === false) { return; } } // Custom Global Function if ($customCRUDFunction != null) { $result = $customCRUDFunction($p1, $p2, $p3, $p4, $p5); if ($result === false) { return; } } // Custom Create Function if ($this->editFunction != null) { $editFunction = $this->editFunction; $result = $editFunction($id, $p1, $p2, $p3, $p4, $p5); if ($result === false) { return; } } // Force hide ID $this->field("id")->hide(); // Insert into database if ($this->isEnabledEdit()) { $jsonObject = $this->updateBean($this->slim->request()->params()); $this->enableJSONResponse(); echo json_encode($jsonObject); } }); /* * Delete a bean * DELETE /crud/{tableName}/{id} */ $this->slim->delete("/:id(/:p1(/:p2(/:p3(/:p4(/:p5)))))", function ($id, $p1 = null, $p2 = null, $p3 = null, $p4 = null, $p5 = null) use($routeName, $customCRUDFunction, $tableName) { // MUST INIT FIRST $this->init($tableName, $routeName, $p1, $p2, $p3, $p4, $p5); $this->enableJSONResponse(); $this->loadBean($id); if ($this->configFunction != null) { $function = $this->configFunction; $result = $function(); if ($result === false) { return; } } // Custom Global Function if ($customCRUDFunction != null) { $result = $customCRUDFunction($p1, $p2, $p3, $p4, $p5); if ($result === false) { return; } } // Custom Delete Function if ($this->deleteFunction != null) { $deleteFunction = $this->deleteFunction; $result = $deleteFunction($id, $p1, $p2, $p3, $p4, $p5); if ($result === false) { return; } } if ($this->isEnabledDelete()) { $this->deleteBean(); $result = new \stdClass(); $result->status = "succ"; echo json_encode($result); } }); }); }
/** * Configure routes to be handled by application. * * @param \Slim\Slim $slim Application */ protected function configureRoutes(\Slim\Slim $slim) { $slim->group('/', function () use($slim) { App::template($slim, 'about'); $slim->get('projects', function () use($slim) { $page = new Pages\Projects($slim); $page->setI18nContext($slim->i18nContext); $page->setLogs($slim->logs); $page(); })->name('projects'); $slim->get('log/:id', function ($id) use($slim) { $page = new Pages\Log($slim); $page->setI18nContext($slim->i18nContext); $page->setLogs($slim->logs); $page($id); })->name('log'); $slim->get('(:project)', function ($project = 'production') use($slim) { $page = new Pages\Sal($slim); $page->setI18nContext($slim->i18nContext); $page->setLogs($slim->logs); $page($project); })->name('SAL'); }); // end group '/' $slim->notFound(function () use($slim) { $slim->render('404.html'); }); }
/** * Configure routes to be handled by application. * * @param \Slim\Slim $slim Application */ protected function configureRoutes(\Slim\Slim $slim) { $middleware = array('must-revalidate' => function () use($slim) { $slim->response->headers->set('Cache-Control', 'private, must-revalidate, max-age=0'); $slim->response->headers->set('Expires', 'Thu, 01 Jan 1970 00:00:00 GMT'); }, 'inject-user' => function () use($slim) { $user = $slim->authManager->getUserData(); $slim->view->set('user', $user); }, 'require-user' => function () use($slim) { if ($slim->authManager->isAnonymous()) { if ($slim->request->isGet()) { $uri = $slim->request->getUrl() . $slim->request->getPath(); $qs = Form::qsMerge(); if ($qs) { $uri = "{$uri}?{$qs}"; } $_SESSION[AuthManager::NEXTPAGE_SESSION_KEY] = $uri; } // FIXME: use i18n $slim->flash('error', 'Login required'); $slim->flashKeep(); $slim->redirect($slim->urlFor('login')); } }); $slim->group('/', $middleware['inject-user'], function () use($slim, $middleware) { App::redirect($slim, '', 'random', 'home'); App::redirect($slim, 'index', 'random'); $slim->get('random', function () use($slim) { $page = new Pages\Random($slim); $page->setI18nContext($slim->i18nContext); $page->setQuips($slim->quips); $page(); })->name('random'); $slim->get('random.json', function () use($slim) { $slim->response->headers->set('Content-Type', 'application/json'); $page = new Pages\Random($slim); $page->setI18nContext($slim->i18nContext); $page->setQuips($slim->quips); $page->setTemplate('random.json'); $page(); })->name('random.json'); $slim->get('search', function () use($slim) { $page = new Pages\Search($slim); $page->setI18nContext($slim->i18nContext); $page->setQuips($slim->quips); $page(); })->name('search'); $slim->get('top', function () use($slim) { $page = new Pages\Top($slim); $page->setI18nContext($slim->i18nContext); $page->setQuips($slim->quips); $page(); })->name('top'); App::template($slim, 'login'); $slim->get('logout', $middleware['must-revalidate'], function () use($slim) { $slim->authManager->logout(); $slim->redirect($slim->urlFor('home')); })->name('logout'); App::template($slim, 'about'); App::template($slim, 'help'); }); // end group '/' $slim->group('/quip/', $middleware['inject-user'], function () use($slim, $middleware) { $slim->get(':id', function ($id) use($slim) { $page = new Pages\Quip($slim); $page->setI18nContext($slim->i18nContext); $page->setQuips($slim->quips); $page($id); })->name('quip'); $slim->get(':id/edit', $middleware['require-user'], function ($id) use($slim) { $page = new Pages\Edit($slim); $page->setI18nContext($slim->i18nContext); $page->setQuips($slim->quips); $page($id); })->name('edit'); $slim->post(':id/post', $middleware['require-user'], function ($id) use($slim) { $page = new Pages\Edit($slim); $page->setI18nContext($slim->i18nContext); $page->setQuips($slim->quips); $page($id); })->name('edit_post'); $slim->post(':id/delete', $middleware['require-user'], function ($id) use($slim) { $page = new Pages\Delete($slim); $page->setI18nContext($slim->i18nContext); $page->setQuips($slim->quips); $page($id); })->name('delete_post'); $slim->post(':id/vote', $middleware['require-user'], function ($id) use($slim) { $page = new Pages\Vote($slim); $page->setI18nContext($slim->i18nContext); $page->setQuips($slim->quips); $page($id); })->name('vote_post'); }); $slim->group('/oauth/', function () use($slim) { $slim->get('', function () use($slim) { $page = new Pages\OAuth($slim); $page->setOAuth($slim->oauthClient); $page('init'); })->name('oauth_init'); $slim->get('callback', function () use($slim) { $page = new Pages\OAuth($slim); $page->setI18nContext($slim->i18nContext); $page->setOAuth($slim->oauthClient); $page->setUserManager($slim->userManager); $page('callback'); })->name('oauth_callback'); }); $slim->notFound(function () use($slim) { $slim->render('404.html'); }); }
$app->group('/emojis', function () use($app) { /** * View all emojis */ $app->get('/', function () use($app) { return EmojiController::getAll($app); }); /** * Find emoji by id */ $app->get('/:id', function ($id) use($app) { return EmojiController::find($app, $id); }); /** * Create new emoji */ $app->post('/', function () use($app) { return EmojiController::create($app); }); /** * Update an emoji */ $app->put('/:id', function ($id) use($app) { return EmojiController::update($app, $id); }); $app->patch('/:id', function ($id) use($app) { return EmojiController::update($app, $id); }); /** * delete an emoji */ $app->delete('/:id', function ($id) use($app) { return EmojiController::delete($app, $id); }); /** * extra (fetch emoji by any criteria) */ $app->get('/:field/:criteria', function ($field, $criteria) use($app) { return EmojiController::findBy($app, $field, $criteria); }); });
if (in_array($lang_uri, $data['app_langs'])) { if ($lang_uri != $data['default_lang']) { $data['lang'] = $lang_uri; $route = '/' . $lang_uri; $home_link = $route; } } $data['home_link'] = $home_link; $data['langs'] = (require '../app/langs/' . $data['lang'] . '.php'); $pages = (require '../app/config/pages.php'); $lang = $data['lang']; $data['route'] = function ($routeName) use($pages, $lang, $route) { return $route . $pages[$routeName][$lang]['route']; }; $app->group($route, function () use($app, $data, $pages) { require '../app/routes/site.php'; }); // ================================================================== // // Errors 404 and 500 // // ------------------------------------------------------------------ $app->notFound(function () use($app) { $data['metas']['title'] = '404 Page not Found'; $app->render('404', $data); }); $app->error(function () use($app) { $data['metas']['title'] = 'Internal server error'; $app->render('500', $data); }); // ==================================================================
} if (!$b) { $app = \Slim\Slim::getInstance(); $app->flash('msgError', 'Login requerido'); $app->redirect(URL . 'login'); } } else { $app = \Slim\Slim::getInstance(); $app->flash('msgError', 'Login requerido'); $app->redirect(URL . 'login'); } }; } $app->config(array('templates.path' => 'app/view/')); $app->group('/', function () use($app) { $app->get('/', function () use($app) { }); }); $app->group('/login', function () use($app) { $app->get('/', function () use($app) { $app->render('login.php'); }); $app->post('/', function () use($app) { $data = $app->request()->post(); $login = new LoginController(); $result = $login->login($data); if ($result) { $app->flash('msgInfo', 'Bienvenido ' . $_SESSION['user']); if ($_SESSION['tipo'] == 1) { $app->redirect(URL . 'pacientes'); } if ($_SESSION['tipo'] == 2) {