0
1
Fork 0
hedera-web-mindshore/web/app.php

70 lines
1.6 KiB
PHP
Raw Normal View History

2016-07-22 20:00:27 +00:00
<?php
namespace Vn\Web;
/**
* Main class for web applications.
*
* Format for $_REQUEST['srv'] variable:
* - [serviceName]:[requestDir]/[requestFile]
2016-07-22 20:00:27 +00:00
**/
2018-05-23 10:14:20 +00:00
class App extends \Vn\Lib\App {
2016-08-30 07:43:47 +00:00
protected $conn = NULL;
private $allowedServices =
[
'html',
'rest',
'json'
];
2018-05-23 10:14:20 +00:00
function run() {
$this->init();
2016-07-22 20:00:27 +00:00
2018-05-23 10:14:20 +00:00
$srv = empty($_REQUEST['srv']) ? '' : $_REQUEST['srv'];
$explode = explode(':', $srv, 2);
2016-07-22 20:00:27 +00:00
2018-05-23 10:14:20 +00:00
if (count($explode) > 0)
$_REQUEST['service'] = $explode[0];
2018-05-23 10:14:20 +00:00
if (count($explode) > 1)
$_REQUEST['method'] = $explode[1];
2016-07-22 20:00:27 +00:00
2018-05-23 10:14:20 +00:00
$service = empty($_REQUEST['service']) ? 'html' : $_REQUEST['service'];
2016-07-22 20:00:27 +00:00
2018-05-23 10:14:20 +00:00
if (in_array($service, $this->allowedServices, TRUE)) {
$includeFile = __DIR__."/$service-service.php";
2018-05-23 10:14:20 +00:00
require_once($includeFile);
2016-07-22 20:00:27 +00:00
2018-05-23 10:14:20 +00:00
$className = __NAMESPACE__ .'\\'. hyphenToCamelCase($service, TRUE) .'Service';
$service = new $className($this);
$service->run();
2018-05-23 11:09:55 +00:00
} else
2018-05-23 10:14:20 +00:00
http_response_code(400);
2016-07-22 20:00:27 +00:00
}
2016-09-23 22:47:34 +00:00
2018-05-23 10:14:20 +00:00
function deinit() {}
2016-07-22 20:00:27 +00:00
/**
* Gets the configuration file name associated to the current vhost
* or the default config file if isn't defined a file for the vhost.
2016-09-24 14:32:31 +00:00
*
* @return string The config file name
2016-07-22 20:00:27 +00:00
**/
2018-05-23 10:14:20 +00:00
function getConfigFile() {
if (!empty($_SERVER['SERVER_NAME'])
&& preg_match('/^[\w\-\.]+$/', $_SERVER['SERVER_NAME'])) {
$hostSplit = explode('.', $_SERVER['SERVER_NAME']);
array_splice($hostSplit, -2);
$subdomain = implode('.', $hostSplit);
2016-09-23 22:47:34 +00:00
2016-07-22 20:00:27 +00:00
$configDir = _CONFIG_DIR .'/'. $this->name;
2016-09-23 22:47:34 +00:00
$hostFile = "$configDir/config.$subdomain.php";
2016-07-22 20:00:27 +00:00
}
2018-05-23 10:14:20 +00:00
if (isset($hostFile) && file_exists($hostFile))
2016-07-22 20:00:27 +00:00
return $hostFile;
else
2018-05-23 10:14:20 +00:00
return parent::getConfigFile();
2016-07-22 20:00:27 +00:00
}
}