2018-02-16 14:18:04 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace PHPCensor;
|
|
|
|
|
2018-03-04 08:30:34 +01:00
|
|
|
use PHPCensor\Store\Factory;
|
2018-02-16 14:18:04 +01:00
|
|
|
use PHPCensor\Model\User;
|
2018-02-17 05:59:02 +01:00
|
|
|
use PHPCensor\Store\UserStore;
|
2018-02-16 14:18:04 +01:00
|
|
|
|
|
|
|
class View
|
|
|
|
{
|
|
|
|
protected $vars = [];
|
|
|
|
protected $isContent = false;
|
|
|
|
|
|
|
|
protected static $extension = 'phtml';
|
|
|
|
|
|
|
|
public function __construct($file, $path = null)
|
|
|
|
{
|
|
|
|
if ('{@content}' === $file) {
|
|
|
|
$this->isContent = true;
|
|
|
|
} else {
|
|
|
|
if (!self::exists($file, $path)) {
|
|
|
|
throw new \RuntimeException('View file does not exist: ' . $file);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->viewFile = self::getViewFile($file, $path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected static function getViewFile($file, $path = null)
|
|
|
|
{
|
2018-03-04 11:50:08 +01:00
|
|
|
$viewPath = is_null($path) ? (SRC_DIR . 'View/') : $path;
|
2018-02-16 14:18:04 +01:00
|
|
|
$fullPath = $viewPath . $file . '.' . static::$extension;
|
|
|
|
|
|
|
|
return $fullPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function exists($file, $path = null)
|
|
|
|
{
|
|
|
|
if (!file_exists(self::getViewFile($file, $path))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function __isset($var)
|
|
|
|
{
|
|
|
|
return isset($this->vars[$var]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function __get($var)
|
|
|
|
{
|
|
|
|
return $this->vars[$var];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function __set($var, $val)
|
|
|
|
{
|
|
|
|
$this->vars[$var] = $val;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function render()
|
|
|
|
{
|
|
|
|
if ($this->isContent) {
|
|
|
|
return $this->vars['content'];
|
|
|
|
} else {
|
|
|
|
extract($this->vars);
|
|
|
|
|
|
|
|
ob_start();
|
|
|
|
|
|
|
|
require($this->viewFile);
|
|
|
|
|
|
|
|
$html = ob_get_contents();
|
|
|
|
ob_end_clean();
|
|
|
|
|
|
|
|
return $html;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2018-02-23 17:05:06 +01:00
|
|
|
protected function loginIsDisabled()
|
2018-02-16 14:18:04 +01:00
|
|
|
{
|
|
|
|
$config = Config::getInstance();
|
|
|
|
$disableAuth = (boolean)$config->get('php-censor.security.disable_auth', false);
|
|
|
|
|
|
|
|
return $disableAuth;
|
|
|
|
}
|
2018-02-17 05:59:02 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return User|null
|
|
|
|
*/
|
|
|
|
protected function getUser()
|
|
|
|
{
|
|
|
|
if (empty($_SESSION['php-censor-user-id'])) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @var UserStore $userStore */
|
|
|
|
$userStore = Factory::getStore('User');
|
|
|
|
|
|
|
|
return $userStore->getById($_SESSION['php-censor-user-id']);
|
|
|
|
}
|
2018-02-16 14:18:04 +01:00
|
|
|
}
|