Browse Source

Use a constant to mark requests coming from frontend

merumelu 9 years ago
parent
commit
4a2ad38cc4
7 changed files with 15 additions and 11 deletions
  1. 4 0
      api/index.php
  2. 1 1
      index.php
  3. 1 1
      src/middleware.php
  4. 3 3
      src/routes/asset.php
  5. 1 1
      src/routes/asset_edit.php
  6. 3 3
      src/routes/auth.php
  7. 2 2
      src/routes/user.php

+ 4 - 0
api/index.php

@@ -14,6 +14,10 @@ if (PHP_SAPI == 'cli-server') {
 
 
 require __DIR__ . '/../vendor/autoload.php';
 require __DIR__ . '/../vendor/autoload.php';
 
 
+if (!defined('FRONTEND')) {
+    define('FRONTEND', false);
+}
+
 // Instantiate the app
 // Instantiate the app
 $settings = require __DIR__ . '/../src/settings.php';
 $settings = require __DIR__ . '/../src/settings.php';
 $local_settings = require __DIR__ . '/../src/settings-local.php';
 $local_settings = require __DIR__ . '/../src/settings-local.php';

+ 1 - 1
index.php

@@ -1,4 +1,4 @@
 <?php
 <?php
-$frontend = true;
+define('FRONTEND', true);
 include(__DIR__ . '/api/index.php');
 include(__DIR__ . '/api/index.php');
 
 

+ 1 - 1
src/middleware.php

@@ -1,6 +1,6 @@
 <?php
 <?php
 
 
-if(isset($frontend) && $frontend) {
+if(FRONTEND) {
   $container = $app->getContainer();
   $container = $app->getContainer();
 
 
   $app->get('/', function ($request, $response) {
   $app->get('/', function ($request, $response) {

+ 3 - 3
src/routes/asset.php

@@ -2,11 +2,11 @@
 // Asset routes
 // Asset routes
 
 
 // Searches through the list of assets
 // Searches through the list of assets
-$app->get('/asset', function ($request, $response, $args) { global $frontend;
+$app->get('/asset', function ($request, $response, $args) {
   $params = $request->getQueryParams();
   $params = $request->getQueryParams();
 
 
   $category = '%';
   $category = '%';
-  if(isset($frontend) && $frontend) {
+  if(FRONTEND) {
     $category_type = $this->constants['category_type']['any'];
     $category_type = $this->constants['category_type']['any'];
   } else {
   } else {
     $category_type = $this->constants['category_type']['addon'];
     $category_type = $this->constants['category_type']['addon'];
@@ -186,7 +186,7 @@ $get_asset = function ($request, $response, $args) {
 };
 };
 // Binding to multiple routes
 // Binding to multiple routes
 $app->get('/asset/{id:[0-9]+}', $get_asset);
 $app->get('/asset/{id:[0-9]+}', $get_asset);
-if(isset($frontend) && $frontend) {
+if(FRONTEND) {
   $app->get('/asset/{id:[0-9]+}/edit', $get_asset);
   $app->get('/asset/{id:[0-9]+}/edit', $get_asset);
 }
 }
 
 

+ 1 - 1
src/routes/asset_edit.php

@@ -405,7 +405,7 @@ $get_edit = function ($request, $response, $args) {
 
 
 // Binding to multiple routes
 // Binding to multiple routes
 $app->get('/asset/edit/{id:[0-9]+}', $get_edit);
 $app->get('/asset/edit/{id:[0-9]+}', $get_edit);
-if(isset($frontend) && $frontend) {
+if(FRONTEND) {
   $app->get('/asset/edit/{id:[0-9]+}/edit', $get_edit);
   $app->get('/asset/edit/{id:[0-9]+}/edit', $get_edit);
 }
 }
 
 

+ 3 - 3
src/routes/auth.php

@@ -3,7 +3,7 @@
 
 
 
 
 // Initializes the connection by sending all categories available
 // Initializes the connection by sending all categories available
-$app->get('/configure', function ($request, $response, $args) { global $frontend;
+$app->get('/configure', function ($request, $response, $args) {
   $params = $request->getQueryParams();
   $params = $request->getQueryParams();
 
 
   $category_type = $this->constants['category_type']['addon'];
   $category_type = $this->constants['category_type']['addon'];
@@ -29,7 +29,7 @@ $app->get('/configure', function ($request, $response, $args) { global $frontend
       'categories' => $query->fetchAll(),
       'categories' => $query->fetchAll(),
       'token' => $token,
       'token' => $token,
       'login_url' => $_SERVER['HTTP_HOST'] .
       'login_url' => $_SERVER['HTTP_HOST'] .
-        (isset($frontend) && $frontend ? dirname($request->getUri()->getBasePath()) : $request->getUri()->getBasePath()) .
+        (FRONTEND ? dirname($request->getUri()->getBasePath()) : $request->getUri()->getBasePath()) .
         '/login#' . urlencode($token),
         '/login#' . urlencode($token),
       // ^ TODO: Make those routes actually work
       // ^ TODO: Make those routes actually work
     ], 200);
     ], 200);
@@ -138,7 +138,7 @@ $app->post('/login', function ($request, $response, $args) {
   }
   }
 });
 });
 
 
-if(isset($frontend) && $frontend) { // Doesn't work for non-frontend, since we can't unissue tokens -- to logout from api/ just drop the token.
+if(FRONTEND) { // Doesn't work for non-frontend, since we can't unissue tokens -- to logout from api/ just drop the token.
   $app->get('/logout', function ($request, $response, $args) {
   $app->get('/logout', function ($request, $response, $args) {
     return $response->withJson([
     return $response->withJson([
       'authenticated' => false,
       'authenticated' => false,

+ 2 - 2
src/routes/user.php

@@ -43,6 +43,6 @@ $get_feed = function ($request, $response, $args) {
 
 
 // Binding to multiple routes
 // Binding to multiple routes
 $app->post('/user/feed', $get_feed);
 $app->post('/user/feed', $get_feed);
-if(isset($frontend) && $frontend) {
-	$app->get('/user/feed', $get_feed);
+if(FRONTEND) {
+  $app->get('/user/feed', $get_feed);
 }
 }