hedera-web/web/app.php

77 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
**/
class App extends \Vn\Lib\App
2016-07-22 20:00:27 +00:00
{
2016-08-30 07:43:47 +00:00
protected $conn = NULL;
private $allowedServices =
[
'html',
'rest',
'json'
];
function run ()
2016-07-22 20:00:27 +00:00
{
$this->init ();
2016-07-22 20:00:27 +00:00
$srv = empty ($_REQUEST['srv']) ? '' : $_REQUEST['srv'];
$explode = explode (':', $srv, 2);
2016-07-22 20:00:27 +00:00
if (count ($explode) > 0)
$_REQUEST['service'] = $explode[0];
if (count ($explode) > 1)
$_REQUEST['method'] = $explode[1];
2016-07-22 20:00:27 +00:00
$service = empty ($_REQUEST['service']) ? 'html' : $_REQUEST['service'];
2016-07-22 20:00:27 +00:00
if (in_array ($service, $this->allowedServices, TRUE))
2016-07-22 20:00:27 +00:00
{
$includeFile = __DIR__."/$service-service.php";
require_once ($includeFile);
2016-07-22 20:00:27 +00:00
$className = __NAMESPACE__ .'\\'. hyphenToCamelCase ($service, TRUE) .'Service';
$service = new $className ($this);
$service->run ();
2016-07-22 20:00:27 +00:00
}
else
http_response_code (400);
2016-07-22 20:00:27 +00:00
}
2016-09-23 22:47:34 +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
**/
function getConfigFile ()
{
if (!empty ($_SERVER['SERVER_NAME'])
&& preg_match ('/^[\w\-\.]+$/', $_SERVER['SERVER_NAME']))
{
$hostSplit = explode ('.', $_SERVER['SERVER_NAME']);
2016-09-23 22:47:34 +00:00
array_splice ($hostSplit, -2);
$subdomain = implode ('.', $hostSplit);
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
}
if (isset ($hostFile) && file_exists ($hostFile))
return $hostFile;
else
return parent::getConfigFile ();
}
}
?>