Mini Shell
<?php
/**
* Copyright © Cloud Linux GmbH & Cloud Linux Software, Inc 2010-2019 All Rights Reserved
*
* Licensed under CLOUD LINUX LICENSE AGREEMENT
* http://cloudlinux.com/docs/LICENSE.TXT
*/
namespace App\Base;
/**
* Class Base
*
* @package selector\base
* @author: Ruslan Rakhmanberdiev
*/
class Base extends Component {
/**
* SPA billing name.
*/
const BILLING_SPA = 'Spa';
/**
* Alias of admin.
*/
const OWNER_ADMIN = 'admin';
/**
* Alias of user.
*/
const OWNER_USER = 'user';
/**
* Alias of reseller.
*/
const OWNER_RESELLER = 'reseller';
/**
* Default app mode
*/
const APP_MODE = 'PRODUCTION_MODE';
/**
* Billing system manager object
*
* @var SpaManager
*/
public $billingManager;
/**
* Billing system text name
*
* @var string
*/
private $billing;
/**
* Corresponds if it was called ajax request from web-server.
*
* @var bool
*/
public $isAJAX = false;
/**
* User type: admin or user.
*
* @var
*/
private $owner = 'user';
/**
* @param string $billing
*/
public function __construct($billing = self::BILLING_SPA)
{
$this->setBilling($billing);
}
/**
* Getter
*
* @param $name
* @return mixed
* @throws \Exception
*/
public function __get($name)
{
if(method_exists($this, 'get'.ucfirst($name))) {
return $this->{'get'.ucfirst($name)};
} elseif(isset($this->{$name})) {
return $this->{$name};
} else {
throw new Exception('Undefined property');
}
}
/**
* Run application engine.
*
* @param bool $isAJAX
* @param str $owner
*/
public function run($isAJAX = false, $owner = self::OWNER_USER)
{
try {
$this->getBillingManager();
$this->checkPermission($owner);
$this->billingManager->run();
} catch (Exception $e) {
if ($isAJAX) {
echo Response::asJson(
$e->data, 503, 'Service Unavailable', $e->getMessage()
);
} else {
echo '<div style="color: red">'.$e->getMessage().'</div>';
}
if(SELECTOR_DEBUG && !$isAJAX) {
echo '<pre>'.$e->getTraceAsString().'</pre>';
}
}
}
/**
* Checks permission of executing script.
*
* @param string $owner
*
* @throws \Exception
*/
public function checkPermission($owner)
{
if ($owner == self::OWNER_ADMIN && !$this->billingManager->isAdmin()) {
include 'warning-message.html';
throw new Exception('');
}
$this->owner = $owner;
}
/**
* @param $billing
* @throws \Exception
*/
private function setBilling($billing)
{
$availableBilling = array(
self::BILLING_SPA,
);
if(!in_array($billing, $availableBilling)) {
throw new Exception('Undefined billing system '.$billing);
}
$this->billing = $billing;
}
/**
* @return mixed
*/
public function getBilling()
{
return $this->billing;
}
/**
* Get param from $_GET variable
*
* @param $key
* @param null $default
* @return null
*/
public function getParam($key, $default = null)
{
return isset($_GET[$key]) ? $_GET[$key] : $default;
}
/**
* Get param from $_POST variable
*
* @param $key
* @param null $default
* @return null
*/
public function getPost($key, $default = null)
{
return isset($_POST[$key]) ? $_POST[$key] : $default;
}
public function hasParam($key)
{
return isset($_REQUEST[$key]);
}
/**
* Get billing system manager
*/
private function getBillingManager()
{
switch($this->getBilling()) {
case self::BILLING_SPA:
$this->billingManager = new SpaManager();
break;
}
}
/**
* Class loader
*
* @param string $className
* @return Base
*/
public static function load($className = __CLASS__)
{
if(isset(self::$_models[$className])) {
return self::$_models[$className];
} else {
self::$_models[$className] = new $className;
return self::$_models[$className];
}
}
/**
* @return string
*/
public function getOwner()
{
return $this->owner;
}
/**
* @return string
*/
public function getAppMode()
{
$modeFile = '/usr/share/l.v.e-manager/spa/app_mode.status';
if (file_exists($modeFile)) {
return trim(file_get_contents($modeFile));
}
return self::APP_MODE;
}
}
Zerion Mini Shell 1.0