From 4e128da70f3c2e5e0f97480eafd1fef1a92955ea Mon Sep 17 00:00:00 2001 From: Ad Schellevis Date: Sun, 4 Mar 2018 20:24:02 +0100 Subject: [PATCH] work in progress, use run_server.php to startup a local webserver and browse to /ui/ pages locally, when -d is specified xdebug is enabled (slow, but functional). --- .gitignore | 4 +- conf/.placeholder | 0 config/config.php.sample | 62 ++++++++++++++++++++++++++++++ public/.htrouter.php | 81 ++++++++++++++++++++++++++++++++++++++++ run_server.php | 75 +++++++++++++++++++++++++++++++++++++ stubs/api.php | 68 +++++++++++++++++++++++++++++++++ stubs/index.php | 59 +++++++++++++++++++++++++++++ stubs/loader.php | 43 +++++++++++++++++++++ 8 files changed, 391 insertions(+), 1 deletion(-) create mode 100644 conf/.placeholder create mode 100644 config/config.php.sample create mode 100644 public/.htrouter.php create mode 100644 run_server.php create mode 100644 stubs/api.php create mode 100644 stubs/index.php create mode 100644 stubs/loader.php diff --git a/.gitignore b/.gitignore index c55d1a9..d9de8ce 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,6 @@ *.pyc *.sass-cache *.volt.php -/.idea/ \ No newline at end of file +/.idea/ +/config/config.php +/conf/config.xml diff --git a/conf/.placeholder b/conf/.placeholder new file mode 100644 index 0000000..e69de29 diff --git a/config/config.php.sample b/config/config.php.sample new file mode 100644 index 0000000..34cade7 --- /dev/null +++ b/config/config.php.sample @@ -0,0 +1,62 @@ + array( + 'controllersDir' => '{plugin_directory}/src/opnsense/mvc/app/controllers/', + 'modelsDir' => '{plugin_directory}/src/opnsense/mvc/app/models/', + 'viewsDir' => '{plugin_directory}/src/opnsense/mvc/app/views/', + 'libraryDir' => '.', + 'cacheDir' => '{temp_directory}/cache', + 'baseUri' => '/opnsense_gui/', + ), + 'globals' => array( + 'config_path' => __DIR__ . '/../conf/', + 'temp_path' => '/tmp/', + 'debug' => false, + 'simulate_mode' => true + ), + 'environment' => array( + /* php includes to add */ + 'includes' => array( + '.' + ), + /* location of OPNsense core package */ + 'coreDir' => '{root_of_OPNsense_core_repo}', + ) +)); + +// Add core views +if (strpos($conf->application->viewsDir, $conf->environment->coreDir) === false) { + $conf->application->viewsDir = array($conf->application->viewsDir, + "{$conf->environment->coreDir}/src/opnsense/mvc/app/views/" + ); +} + +return $conf; \ No newline at end of file diff --git a/public/.htrouter.php b/public/.htrouter.php new file mode 100644 index 0000000..f37510d --- /dev/null +++ b/public/.htrouter.php @@ -0,0 +1,81 @@ + 'text/css', + 'js' => 'application/javascript', + 'jpg' => 'image/jpg', + 'png' => 'image/png', + 'map' => 'application/json' + ]; + if (isset($mimeTypes[$tmp_ext[count($tmp_ext)-1]])) { + header("Content-Type: {$mimeTypes[$tmp_ext[count($tmp_ext)-1]]}"); + } + readfile($path); + return true; + } + return false; + } + return false; + } +} + +// setup environment +global $DEV_WORKDIR; +$DEV_WORKDIR = getenv("DEV_WORKDIR"); // passed through from run_server + +// set user to root for local testing +session_start(); +$_SESSION["Username"]="root"; +session_write_close(); + +if (preg_match("/^\/ui\/.*/", $uri)) { + $_GET['_url'] = substr($_SERVER['REQUEST_URI'], 3); + require_once "{$DEV_WORKDIR}/stubs/index.php"; +} elseif (preg_match("/^\/api\/.*/", $uri)) { + $_GET['_url'] = substr($_SERVER['REQUEST_URI'], 4); + require_once "{$DEV_WORKDIR}/stubs/api.php"; +} else { + return false; +} diff --git a/run_server.php b/run_server.php new file mode 100644 index 0000000..98c6052 --- /dev/null +++ b/run_server.php @@ -0,0 +1,75 @@ +environment->coreDir}/src/opnsense/www/.htrouter.php"); + +// copy default config when there is no config found +if (!is_file("{$config->globals->config_path}/config.xml")) { + copy("{$config->environment->coreDir}/src/etc/config.xml.sample", "{$config->globals->config_path}/config.xml"); +} + +// gather php include paths and add to run command +$include_paths = array(); +foreach ($config->environment->includes as $include) { + $include_paths[] = trim($include); +} +$run_command[] = '-d include_path="'.implode(':', $include_paths).'"'; + +// listen to localhost +$run_command[] = "-S localhost:8000"; + +// set document root +$run_command[] = str_replace('//', '/', "-t {$config->environment->coreDir}/src/opnsense/www"); + +// .htaccess alternative routing +$run_command[] = ".htrouter.php"; + +// set our working directory in the php environment in which the server runs +putenv("DEV_WORKDIR=".__DIR__); + +// show executed command +$cmd_action = implode(' ', $run_command) ; +echo "{$cmd_action}\n"; + +exec($cmd_action); diff --git a/stubs/api.php b/stubs/api.php new file mode 100644 index 0000000..0654091 --- /dev/null +++ b/stubs/api.php @@ -0,0 +1,68 @@ +environment->coreDir . "/src/opnsense/mvc/app/config/services_api.php"; + + /** + * local webserver might have moved Authorization header, move it back + */ + if (!empty($_SERVER['PHP_AUTH_USER']) && !empty($_SERVER['PHP_AUTH_PW'])) { + $_SERVER['HTTP_AUTHORIZATION'] = "Basic " .base64_encode($_SERVER['PHP_AUTH_USER'].":".$_SERVER['PHP_AUTH_PW']); + } + + /** + * Handle the request + */ + $application = new \Phalcon\Mvc\Application($di); + echo $application->handle()->getContent(); + +} catch (\Exception $e) { + $response = array(); + $response['errorMessage'] = $e->getMessage(); + header('HTTP', true, 500); + header("Content-Type: application/json;charset=utf-8"); + echo htmlspecialchars(json_encode($response), ENT_NOQUOTES); + error_log($e); +} diff --git a/stubs/index.php b/stubs/index.php new file mode 100644 index 0000000..3aee614 --- /dev/null +++ b/stubs/index.php @@ -0,0 +1,59 @@ +environment->coreDir . "/src/opnsense/mvc/app/config/services.php"; + + /** + * Handle the request + */ + $application = new \Phalcon\Mvc\Application($di); + + // always flush caches for local testing + (new \OPNsense\Base\Menu\MenuSystem())->invalidateCache(); + (new \OPNsense\Core\ACL())->invalidateCache(); + echo $application->handle()->getContent(); + +} catch (\Exception $e) { + echo $e->getMessage(); +} diff --git a/stubs/loader.php b/stubs/loader.php new file mode 100644 index 0000000..02923a0 --- /dev/null +++ b/stubs/loader.php @@ -0,0 +1,43 @@ +registerDirs( + array( + $config->application->controllersDir, + $config->application->modelsDir, + $config->application->libraryDir, + "{$config->environment->coreDir}/src/opnsense/mvc/app/controllers", + "{$config->environment->coreDir}/src/opnsense/mvc/app/models", + "{$config->environment->coreDir}/src/opnsense/mvc/app/library", + ) +)->register();