nsITobin 3 years ago
parent
commit
317ace163b
3 changed files with 94 additions and 76 deletions
  1. 14 14
      base/special.php
  2. 38 62
      fundamentals.php
  3. 42 0
      index.php

+ 14 - 14
base/special.php

@@ -66,8 +66,6 @@ function gfGenContent($aTitle, $aContent, $aTextBox = null, $aList = null, $aErr
   exit();
 }
 
-// --------------------------------------------------------------------------------------------------------------------
-
 /**********************************************************************************************************************
 * Checks the exploded count against the number of path parts in an exploded path and 404s it if it is greater
 ***********************************************************************************************************************/
@@ -87,14 +85,14 @@ gfLocalAuth();
 
 // The Special Component doesn't intend on having more than one level on metropolis
 gfCheckPathCount(1);
+$gvSpecialFunction = $gaRuntime['explodedPath'][0];
 
-switch ($gaRuntime['explodedPath'][0]) {
-  case 'phpinfo':
-    phpinfo(INFO_GENERAL | INFO_CONFIGURATION | INFO_ENVIRONMENT | INFO_VARIABLES);
-    break;
-  case 'software-state':
-    gfGenContent('Software State', $gaRuntime);
-    break;
+switch ($gvSpecialFunction) {
+  case 'root':
+    $rootHTML = '<a href="/test/">Test Cases</a></li><li>' .
+                '<a href="/phpinfo/">PHP Info</a></li><li>' .
+                '<a href="/software-state/">Software State</a>';
+    gfGenContent('Functions', $rootHTML, null, true);
   case 'test':
     $gaRuntime['requestTestCase'] = gfSuperVar('get', 'case');
     $arrayTestsGlob = glob('./base/tests/*.php');
@@ -124,11 +122,13 @@ switch ($gaRuntime['explodedPath'][0]) {
 
     gfGenContent('Test Cases', $testsHTML);
     break;
-  case 'root':
-    $rootHTML = '<a href="/test/">Test Cases</a></li><li>' .
-                '<a href="/phpinfo/">PHP Info</a></li><li>' .
-                '<a href="/software-state/">Software State</a>';
-    gfGenContent('Functions', $rootHTML, null, true);
+  case 'phpinfo':
+    gfHeader('html');
+    phpinfo(INFO_GENERAL | INFO_CONFIGURATION | INFO_ENVIRONMENT | INFO_VARIABLES);
+    break;
+  case 'software-state':
+    gfGenContent('Software State', $gaRuntime);
+    break;
   default:
     gfHeader(404);
 }

+ 38 - 62
fundamentals.php

@@ -1,4 +1,8 @@
 <?php
+// This Source Code Form is subject to the terms of the Mozilla Public
+// License, v. 2.0. If a copy of the MPL was not distributed with this
+// file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
 // == | Setup | =======================================================================================================
 
 // Check if the basic defines have been defined in the including script
@@ -18,7 +22,7 @@ define('BINOC_FUNCTIONS', 1);
 
 // ====================================================================================================================
 
-// == | Global Constants | ======================================================================================
+// == | Global Constants | ============================================================================================
 
 const PHP_ERROR_CODES       = array(
   E_ERROR                   => 'Fatal Error',
@@ -69,8 +73,12 @@ const SCHEME_SUFFIX         = "://";
 
 const PHP_EXTENSION         = DOT . 'php';
 const INI_EXTENSION         = DOT . 'ini';
+const HTML_EXTENSION        = DOT . 'html';
 const XML_EXTENSION         = DOT . 'xml';
+const RDF_EXTENSION         = DOT . 'rdf';
 const JSON_EXTENSION        = DOT . 'json';
+const CONTENT_EXTENSION     = DOT . 'content';
+const XPINSTALL_EXTENSION   = DOT . 'xpi';
 const TEMP_EXTENSION        = DOT . 'temp';
 
 // --------------------------------------------------------------------------------------------------------------------
@@ -79,11 +87,6 @@ const XML_TAG               = '<?xml version="1.0" encoding="utf-8" ?>';
 
 // --------------------------------------------------------------------------------------------------------------------
 
-const XPINSTALL_EXTENSION   = DOT . 'xpi';
-const RDF_EXTENSION         = DOT . 'rdf';
-
-// --------------------------------------------------------------------------------------------------------------------
-
 const RDF_INSTALL_MANIFEST  = 'install' . DOT . RDF_EXTENSION;
 const JSON_INSTALL_MANIFEST = 'install' . DOT . JSON_EXTENSION;
 
@@ -95,6 +98,7 @@ const FILE_WRITE_FLAGS      = "w+";
 // --------------------------------------------------------------------------------------------------------------------
 
 const REGEX_GET_FILTER      = "/[^-a-zA-Z0-9_\-\/\{\}\@\.\%\s\,]/";
+const REGEX_YAML_FILTER     = "/\A---(.|\n)*?---/";
 const REGEX_GUID            = "/^\{[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\}$/i";
 const REGEX_HOST            = "/[a-z0-9-\._]+\@[a-z0-9-\._]+/i";
 
@@ -180,6 +184,7 @@ function gfError($aValue, $phpError = false) {
 
   $externalOutput = function_exists('gfGenContent');
   $isCLI = (php_sapi_name() == "cli");
+  $isOutput = false;
 
   if (is_string($aValue) || is_int($aValue)) {
     $eContentType = 'text/xml';
@@ -193,6 +198,7 @@ function gfError($aValue, $phpError = false) {
     }
   }
   else {
+    $isOutput = true;
     $eContentType = 'application/json';
     $ePrefix = $pageHeader['output'];
     $eMessage = json_encode($aValue, JSON_ENCODE_FLAGS);
@@ -203,6 +209,10 @@ function gfError($aValue, $phpError = false) {
       gfGenContent($ePrefix, $eMessage, null, true, true);
     }
 
+    if ($isOutput) {
+      gfGenContent($ePrefix, $eMessage, true, false, true);
+    }
+    
     gfGenContent($ePrefix, $eMessage);
   }
   elseif ($isCLI) {
@@ -347,7 +357,7 @@ function gfHeader($aHeader) {
 **********************************************************************************************************************/
 // This function sends a redirect header
 function gfRedirect($aURL) {
-	header('Location: ' . $aURL , true, 302);
+  header('Location: ' . $aURL , true, 302);
   
   // We are done here
   exit();
@@ -545,7 +555,7 @@ function gfReadFile($aFile) {
   }
 
   // If it is a mozilla install manifest and the module has been included then parse it
-  if (str_ends_with($aFile, 'install.rdf') && array_key_exists('gmMozillaRDF', $GLOBALS)) {
+  if (str_ends_with($aFile, RDF_INSTALL_MANIFEST) && array_key_exists('gmMozillaRDF', $GLOBALS)) {
     global $gmMozillaRDF;
     $file = $gmMozillaRDF->parseInstallManifest($file);
 
@@ -625,6 +635,8 @@ function gfHexString($aLength = 40) {
 /**********************************************************************************************************************
 * Basic Filter Substitution of a string
 *
+* @dep EMPTY_STRING
+* @dep SLASH
 * @dep SPACE
 * @dep gfError()
 * @param $aSubsts               multi-dimensional array of keys and values to be replaced
@@ -632,38 +644,40 @@ function gfHexString($aLength = 40) {
 * @param $aRegEx                set to true if pcre
 * @returns                      bitwise int value representing applications
 ***********************************************************************************************************************/
-function gfSubst($aSubsts, $aString, $aRegEx = null) {
+function gfSubst($aMode, $aSubsts, $aString) {
+  $ePrefix = __FUNCTION__ . DASH_SEPARATOR;
   if (!is_array($aSubsts)) {
-    gfError('$aSubsts must be an array');
+    gfError($ePrefix . '$aSubsts must be an array');
   }
 
   if (!is_string($aString)) {
-    gfError('$aString must be a string');
+    gfError($ePrefix . '$aString must be a string');
   }
 
-  $string = $aString;
+  $rv = $aString;
 
-  if ($aRegEx) {
-    foreach ($aSubsts as $_key => $_value) {
-      $string = preg_replace($_key, $_value, $string);
-    }
-  }
-  else {
-    foreach ($aSubsts as $_key => $_value) {
-      $string = str_replace('{%' . $_key . '}', $_value, $string);
-    }
+  switch ($aMode) {
+    case 'simple':
+      foreach ($aSubsts as $_key => $_value) { $rv = str_replace($_key, $_value, $rv); }
+      break;
+    case 'regex':
+      foreach ($aSubsts as $_key => $_value) { $rv = preg_replace(SLASH . $_key . SLASH . 'iU', $_value, $rv); }
+      break;
+    default:
+      gfError($ePrefix . 'Unknown mode');
   }
 
-  if (!$string) {
-    gfError('Something has gone wrong with' . SPACE . __FUNCTION__);
+  if (!$rv) {
+    gfError($ePrefix . 'Something has gone wrong...');
   }
 
-  return $string;
+  return $rv;
 }
 
 /**********************************************************************************************************************
 * Request HTTP Basic Authentication
 *
+* @dep SOFTWARE_NAME
 * @dep gfError()
 ***********************************************************************************************************************/
 function gfBasicAuthPrompt() {
@@ -672,43 +686,5 @@ function gfBasicAuthPrompt() {
   gfError('You need to enter a valid username and password.');
 }
 
-/**********************************************************************************************************************
-* Local Authentication from a pre-defined json file with user and hashed passwords
-*
-* @dep ROOT_PATH
-* @dep DOTDOT
-* @dep JSON_EXTENSION
-* @dep gfError()
-* @dep gfSuperVar()
-* @dep gfBuildPath()
-* @dep gfBasicAuthPrompt()
-* @param $aTobinOnly   Only Tobin's username is valid
-***********************************************************************************************************************/
-function gfLocalAuth($aTobinOnly = null) {
-  global $gaRuntime;
-
-  $username = gfSuperVar('server', 'PHP_AUTH_USER');
-  $password = gfSuperVar('server', 'PHP_AUTH_PW');
-
-  if ((!$username || !$password) || ($aTobinOnly && $username != 'mattatobin')) {
-    gfBasicAuthPrompt();
-  }
-
-  $configPath = gfBuildPath(ROOT_PATH, DOTDOT, 'storage', 'config' . JSON_EXTENSION);
-  $userdb = gfReadFile($configPath);
-  
-  if (!$userdb) {
-    gfError('Could not read configuration.');
-  }
-
-  $userdb = $userdb['userdb'];
-  
-  if (!array_key_exists($username, $userdb) || !password_verify($password, $userdb[$username])) {
-    gfBasicAuthPrompt();
-  }
-
-  $gaRuntime['authentication']['username'] = $username;
-}
-
 // ====================================================================================================================
 ?>

+ 42 - 0
index.php

@@ -25,6 +25,48 @@ require_once('./fundamentals.php');
 
 // ====================================================================================================================
 
+// == | Global Functions | ============================================================================================
+
+/**********************************************************************************************************************
+* Local Authentication from a pre-defined json file with user and hashed passwords
+*
+* @dep ROOT_PATH
+* @dep DOTDOT
+* @dep JSON_EXTENSION
+* @dep gfError()
+* @dep gfSuperVar()
+* @dep gfBuildPath()
+* @dep gfBasicAuthPrompt()
+* @param $aTobinOnly   Only Tobin's username is valid
+***********************************************************************************************************************/
+function gfLocalAuth($aTobinOnly = null) {
+  global $gaRuntime;
+
+  $username = gfSuperVar('server', 'PHP_AUTH_USER');
+  $password = gfSuperVar('server', 'PHP_AUTH_PW');
+
+  if ((!$username || !$password) || ($aTobinOnly && $username != 'mattatobin')) {
+    gfBasicAuthPrompt();
+  }
+
+  $configPath = gfBuildPath(ROOT_PATH, DOTDOT, 'storage', 'config' . JSON_EXTENSION);
+  $userdb = gfReadFile($configPath);
+  
+  if (!$userdb) {
+    gfError('Could not read configuration.');
+  }
+
+  $userdb = $userdb['userdb'];
+  
+  if (!array_key_exists($username, $userdb) || !password_verify($password, $userdb[$username])) {
+    gfBasicAuthPrompt();
+  }
+
+  $gaRuntime['authentication']['username'] = $username;
+}
+
+// ====================================================================================================================
+
 // == | Main | ========================================================================================================
 
 // Define an array that will hold the current application state