Updating modules/cms/classes

This commit is contained in:
Stefan Talen 2014-10-11 01:22:03 +02:00
parent b64a744498
commit 202e8869b1
27 changed files with 637 additions and 354 deletions

View File

@ -41,8 +41,9 @@ class Asset extends CmsObject
$defaultTypes = ['css','js','less','sass','scss']; $defaultTypes = ['css','js','less','sass','scss'];
$configTypes = Config::get('cms.editableAssetTypes'); $configTypes = Config::get('cms.editableAssetTypes');
if (!$configTypes) if (!$configTypes) {
return $defaultTypes; return $defaultTypes;
}
return $configTypes; return $configTypes;
} }
@ -64,5 +65,4 @@ class Asset extends CmsObject
{ {
return null; return null;
} }
} }

View File

@ -67,8 +67,9 @@ class CmsCompoundObject extends CmsObject
*/ */
public static function load($theme, $fileName) public static function load($theme, $fileName)
{ {
if (($obj = parent::load($theme, $fileName)) === null) if (($obj = parent::load($theme, $fileName)) === null) {
return null; return null;
}
CmsException::mask($obj, 200); CmsException::mask($obj, 200);
$parsedData = SectionParser::parse($obj->content); $parsedData = SectionParser::parse($obj->content);
@ -89,8 +90,9 @@ class CmsCompoundObject extends CmsObject
*/ */
public function __get($name) public function __get($name)
{ {
if (is_array($this->settings) && array_key_exists($name, $this->settings)) if (is_array($this->settings) && array_key_exists($name, $this->settings)) {
return $this->settings[$name]; return $this->settings[$name];
}
return parent::__get($name); return parent::__get($name);
} }
@ -103,8 +105,9 @@ class CmsCompoundObject extends CmsObject
*/ */
public function __isset($key) public function __isset($key)
{ {
if (parent::__isset($key) === true) if (parent::__isset($key) === true) {
return true; return true;
}
return isset($this->settings[$key]); return isset($this->settings[$key]);
} }
@ -124,8 +127,9 @@ class CmsCompoundObject extends CmsObject
public function runComponents() public function runComponents()
{ {
foreach ($this->components as $component) { foreach ($this->components as $component) {
if ($result = $component->onRun()) if ($result = $component->onRun()) {
return $result; return $result;
}
} }
} }
@ -139,13 +143,15 @@ class CmsCompoundObject extends CmsObject
$manager = ComponentManager::instance(); $manager = ComponentManager::instance();
$components = []; $components = [];
foreach ($this->settings as $setting => $value) { foreach ($this->settings as $setting => $value) {
if (!is_array($value)) if (!is_array($value)) {
continue; continue;
}
$settingParts = explode(' ', $setting); $settingParts = explode(' ', $setting);
$settingName = $settingParts[0]; $settingName = $settingParts[0];
// if (!$manager->hasComponent($settingName)) // if (!$manager->hasComponent($settingName)) {
// continue; // continue;
// }
$components[$setting] = $value; $components[$setting] = $value;
unset($this->settings[$setting]); unset($this->settings[$setting]);
@ -182,25 +188,29 @@ class CmsCompoundObject extends CmsObject
$this->code = trim($this->code); $this->code = trim($this->code);
$this->markup = trim($this->markup); $this->markup = trim($this->markup);
$trim = function(&$values) use (&$trim) { $trim = function (&$values) use (&$trim) {
foreach ($values as &$value) { foreach ($values as &$value) {
if (!is_array($value)) if (!is_array($value)) {
$value = trim($value); $value = trim($value);
else $trim($value); } else {
$trim($value);
}
} }
}; };
$trim($this->settings); $trim($this->settings);
if (array_key_exists('components', $this->settings) && count($this->settings['components']) == 0) if (array_key_exists('components', $this->settings) && count($this->settings['components']) == 0) {
unset($this->settings['components']); unset($this->settings['components']);
}
$this->validate(); $this->validate();
$content = []; $content = [];
if ($this->settings) if ($this->settings) {
$content[] = FileHelper::formatIniString($this->settings); $content[] = FileHelper::formatIniString($this->settings);
}
if ($this->code) { if ($this->code) {
$code = preg_replace('/^\<\?php/', '', $this->code); $code = preg_replace('/^\<\?php/', '', $this->code);
@ -224,8 +234,9 @@ class CmsCompoundObject extends CmsObject
*/ */
public function getViewBag() public function getViewBag()
{ {
if ($this->viewBagCache !== false) if ($this->viewBagCache !== false) {
return $this->viewBagCache; return $this->viewBagCache;
}
$componentName = 'viewBag'; $componentName = 'viewBag';
@ -248,13 +259,15 @@ class CmsCompoundObject extends CmsObject
*/ */
public function getComponent($componentName) public function getComponent($componentName)
{ {
if (!$this->hasComponent($componentName)) if (!$this->hasComponent($componentName)) {
return null; return null;
}
return ComponentManager::instance()->makeComponent( return ComponentManager::instance()->makeComponent(
$componentName, $componentName,
null, null,
$this->settings['components'][$componentName]); $this->settings['components'][$componentName]
);
} }
/** /**
@ -283,31 +296,35 @@ class CmsCompoundObject extends CmsObject
$cached = Cache::get($key, false); $cached = Cache::get($key, false);
$unserialized = $cached ? @unserialize($cached) : false; $unserialized = $cached ? @unserialize($cached) : false;
$objectComponentMap = $unserialized ? $unserialized : []; $objectComponentMap = $unserialized ? $unserialized : [];
if ($objectComponentMap) if ($objectComponentMap) {
self::$objectComponentPropertyMap = $objectComponentMap; self::$objectComponentPropertyMap = $objectComponentMap;
}
} }
$objectCode = $this->getBaseFileName(); $objectCode = $this->getBaseFileName();
if (array_key_exists($objectCode, $objectComponentMap)) { if (array_key_exists($objectCode, $objectComponentMap)) {
if (array_key_exists($componentName, $objectComponentMap[$objectCode])) if (array_key_exists($componentName, $objectComponentMap[$objectCode])) {
return $objectComponentMap[$objectCode][$componentName]; return $objectComponentMap[$objectCode][$componentName];
}
return []; return [];
} }
if (!isset($this->settings['components'])) if (!isset($this->settings['components'])) {
$objectComponentMap[$objectCode] = []; $objectComponentMap[$objectCode] = [];
else { } else {
foreach ($this->settings['components'] as $componentName=>$componentSettings) { foreach ($this->settings['components'] as $componentName => $componentSettings) {
$component = $this->getComponent($componentName); $component = $this->getComponent($componentName);
if (!$component) if (!$component) {
continue; continue;
}
$componentProperties = []; $componentProperties = [];
$propertyDefinitions = $component->defineProperties(); $propertyDefinitions = $component->defineProperties();
foreach ($propertyDefinitions as $propertyName=>$propertyInfo) foreach ($propertyDefinitions as $propertyName => $propertyInfo) {
$componentProperties[$propertyName] = $component->property($propertyName); $componentProperties[$propertyName] = $component->property($propertyName);
}
$objectComponentMap[$objectCode][$componentName] = $componentProperties; $objectComponentMap[$objectCode][$componentName] = $componentProperties;
} }
@ -317,8 +334,9 @@ class CmsCompoundObject extends CmsObject
Cache::put($key, serialize($objectComponentMap), Config::get('cms.parsedPageCacheTTL', 10)); Cache::put($key, serialize($objectComponentMap), Config::get('cms.parsedPageCacheTTL', 10));
if (array_key_exists($componentName, $objectComponentMap[$objectCode])) if (array_key_exists($componentName, $objectComponentMap[$objectCode])) {
return $objectComponentMap[$objectCode][$componentName]; return $objectComponentMap[$objectCode][$componentName];
}
return []; return [];
} }
@ -338,7 +356,9 @@ class CmsCompoundObject extends CmsObject
* the content of the $settings property after the object * the content of the $settings property after the object
* is loaded from a file. * is loaded from a file.
*/ */
protected function parseSettings() {} protected function parseSettings()
{
}
/** /**
* Initializes the object properties from the cached data. * Initializes the object properties from the cached data.
@ -368,14 +388,24 @@ class CmsCompoundObject extends CmsObject
*/ */
protected function validate() protected function validate()
{ {
$validation = Validator::make($this->settings, $this->settingsValidationRules, $this->settingsValidationMessages); $validation = Validator::make(
if ($validation->fails()) $this->settings,
$this->settingsValidationRules,
$this->settingsValidationMessages
);
if ($validation->fails()) {
throw new ValidationException($validation); throw new ValidationException($validation);
}
if ($this->viewBagValidationRules && isset($this->settings['viewBag'])) { if ($this->viewBagValidationRules && isset($this->settings['viewBag'])) {
$validation = Validator::make($this->settings['viewBag'], $this->viewBagValidationRules, $this->viewBagValidationMessages); $validation = Validator::make(
if ($validation->fails()) $this->settings['viewBag'],
$this->viewBagValidationRules,
$this->viewBagValidationMessages
);
if ($validation->fails()) {
throw new ValidationException($validation); throw new ValidationException($validation);
}
} }
} }
} }

View File

@ -50,8 +50,9 @@ class CmsException extends ApplicationException
$message = ''; $message = '';
} }
if (isset(static::$errorCodes[$code])) if (isset(static::$errorCodes[$code])) {
$this->errorType = static::$errorCodes[$code]; $this->errorType = static::$errorCodes[$code];
}
parent::__construct($message, $code, $previous); parent::__construct($message, $code, $previous);
} }
@ -104,9 +105,15 @@ class CmsException extends ApplicationException
/* /*
* Expecting: syntax error, unexpected '!' in Unknown on line 4 * Expecting: syntax error, unexpected '!' in Unknown on line 4
*/ */
if (!starts_with($message, 'syntax error')) return false; if (!starts_with($message, 'syntax error')) {
if (strpos($message, 'Unknown') === false) return false; return false;
if (strpos($exception->getFile(), 'SectionParser.php') === false) return false; }
if (strpos($message, 'Unknown') === false) {
return false;
}
if (strpos($exception->getFile(), 'SectionParser.php') === false) {
return false;
}
/* /*
* Line number from parse_ini_string() error. * Line number from parse_ini_string() error.
@ -143,23 +150,28 @@ class CmsException extends ApplicationException
$check = false; $check = false;
// Expected: */modules/cms/classes/CodeParser.php(165) : eval()'d code line 7 // Expected: */modules/cms/classes/CodeParser.php(165) : eval()'d code line 7
if (strpos($exception->getFile(), 'CodeParser.php')) $check = true; if (strpos($exception->getFile(), 'CodeParser.php')) {
$check = true;
}
// Expected: */app/storage/cache/39/05/home.htm.php // Expected: */app/storage/cache/39/05/home.htm.php
if (strpos($exception->getFile(), $this->compoundObject->getFileName() . '.php')) $check = true; if (strpos($exception->getFile(), $this->compoundObject->getFileName() . '.php')) {
$check = true;
}
if (!$check) if (!$check) {
return false; return false;
} }
/* /*
* Errors occurring the PHP code base class (Cms\Classes\CodeBase) * Errors occurring the PHP code base class (Cms\Classes\CodeBase)
*/ */
else { } else {
$trace = $exception->getTrace(); $trace = $exception->getTrace();
if (isset($trace[1]['class'])) { if (isset($trace[1]['class'])) {
$class = $trace[1]['class']; $class = $trace[1]['class'];
if (!is_subclass_of($class, 'Cms\Classes\CodeBase')) if (!is_subclass_of($class, 'Cms\Classes\CodeBase')) {
return false; return false;
}
} }
} }
@ -187,8 +199,9 @@ class CmsException extends ApplicationException
protected function processTwig(Exception $exception) protected function processTwig(Exception $exception)
{ {
// Must be a Twig related exception // Must be a Twig related exception
if (!$exception instanceof Twig_Error) if (!$exception instanceof Twig_Error) {
return false; return false;
}
$this->message = $exception->getRawMessage(); $this->message = $exception->getRawMessage();
$this->line = $exception->getTemplateLine(); $this->line = $exception->getTemplateLine();
@ -220,5 +233,4 @@ class CmsException extends ApplicationException
return; return;
} }
} }
}
}

View File

@ -80,27 +80,32 @@ class CmsObject implements ArrayAccess
*/ */
public static function loadCached($theme, $fileName) public static function loadCached($theme, $fileName)
{ {
if (!FileHelper::validatePath($fileName, static::getMaxAllowedPathNesting())) if (!FileHelper::validatePath($fileName, static::getMaxAllowedPathNesting())) {
throw new SystemException(Lang::get('cms::lang.cms_object.invalid_file', ['name'=>$fileName])); throw new SystemException(Lang::get('cms::lang.cms_object.invalid_file', ['name'=>$fileName]));
}
if (!strlen(File::extension($fileName))) if (!strlen(File::extension($fileName))) {
$fileName .= '.'.static::$defaultExtension; $fileName .= '.'.static::$defaultExtension;
}
$filePath = static::getFilePath($theme, $fileName); $filePath = static::getFilePath($theme, $fileName);
if (array_key_exists($filePath, ObjectMemoryCache::$cache)) if (array_key_exists($filePath, ObjectMemoryCache::$cache)) {
return ObjectMemoryCache::$cache[$filePath]; return ObjectMemoryCache::$cache[$filePath];
}
$key = self::getObjectTypeDirName().crc32($filePath); $key = self::getObjectTypeDirName().crc32($filePath);
clearstatcache($filePath); clearstatcache($filePath);
$cached = Cache::get($key, false); $cached = Cache::get($key, false);
if ($cached !== false && ($cached = @unserialize($cached)) !== false) { if ($cached !== false && ($cached = @unserialize($cached)) !== false) {
if ($cached['mtime'] != @File::lastModified($filePath)) if ($cached['mtime'] != @File::lastModified($filePath)) {
$cached = false; $cached = false;
}
} }
if ($cached && !File::isFile($filePath)) if ($cached && !File::isFile($filePath)) {
$cached = false; $cached = false;
}
if ($cached !== false) { if ($cached !== false) {
/* /*
@ -151,19 +156,23 @@ class CmsObject implements ArrayAccess
*/ */
public static function load($theme, $fileName) public static function load($theme, $fileName)
{ {
if (!FileHelper::validatePath($fileName, static::getMaxAllowedPathNesting())) if (!FileHelper::validatePath($fileName, static::getMaxAllowedPathNesting())) {
throw new SystemException(Lang::get('cms::lang.cms_object.invalid_file', ['name'=>$fileName])); throw new SystemException(Lang::get('cms::lang.cms_object.invalid_file', ['name'=>$fileName]));
}
if (!strlen(File::extension($fileName))) if (!strlen(File::extension($fileName))) {
$fileName .= '.'.static::$defaultExtension; $fileName .= '.'.static::$defaultExtension;
}
$fullPath = static::getFilePath($theme, $fileName); $fullPath = static::getFilePath($theme, $fileName);
if (!File::isFile($fullPath)) if (!File::isFile($fullPath)) {
return null; return null;
}
if (($content = @File::get($fullPath)) === false) if (($content = @File::get($fullPath)) === false) {
return null; return null;
}
$obj = new static($theme); $obj = new static($theme);
$obj->fileName = $fileName; $obj->fileName = $fileName;
@ -209,8 +218,9 @@ class CmsObject implements ArrayAccess
public function getBaseFileName() public function getBaseFileName()
{ {
$pos = strrpos($this->fileName, '.'); $pos = strrpos($this->fileName, '.');
if ($pos === false) if ($pos === false) {
return $this->fileName; return $this->fileName;
}
return substr($this->fileName, 0, $pos); return substr($this->fileName, 0, $pos);
} }
@ -258,8 +268,9 @@ class CmsObject implements ArrayAccess
]); ]);
} }
if (!strlen(File::extension($fileName))) if (!strlen(File::extension($fileName))) {
$fileName .= '.htm'; $fileName .= '.htm';
}
$this->fileName = $fileName; $this->fileName = $fileName;
return $this; return $this;
@ -298,15 +309,20 @@ class CmsObject implements ArrayAccess
*/ */
public function fill(array $attributes) public function fill(array $attributes)
{ {
foreach ($attributes as $key=>$value) { foreach ($attributes as $key => $value) {
if (!in_array($key, static::$fillable)) if (!in_array($key, static::$fillable)) {
throw new ApplicationException(Lang::get('cms::lang.cms_object.invalid_property', ['name'=>$key])); throw new ApplicationException(Lang::get(
'cms::lang.cms_object.invalid_property',
['name' => $key]
));
}
$methodName = 'set'.ucfirst($key); $methodName = 'set'.ucfirst($key);
if (method_exists($this, $methodName)) if (method_exists($this, $methodName)) {
$this->$methodName($value); $this->$methodName($value);
else } else {
$this->$key = $value; $this->$key = $value;
}
} }
} }
@ -317,31 +333,48 @@ class CmsObject implements ArrayAccess
{ {
$fullPath = static::getFilePath($this->theme, $this->fileName); $fullPath = static::getFilePath($this->theme, $this->fileName);
if (File::isFile($fullPath) && $this->originalFileName !== $this->fileName) if (File::isFile($fullPath) && $this->originalFileName !== $this->fileName) {
throw new ApplicationException(Lang::get('cms::lang.cms_object.file_already_exists', ['name'=>$this->fileName])); throw new ApplicationException(Lang::get(
'cms::lang.cms_object.file_already_exists',
['name'=>$this->fileName]
));
}
$dirPath = rtrim(static::getFilePath($this->theme, ''), '/'); $dirPath = rtrim(static::getFilePath($this->theme, ''), '/');
if (!file_exists($dirPath) || !is_dir($dirPath)) { if (!file_exists($dirPath) || !is_dir($dirPath)) {
if (!File::makeDirectory($dirPath, 0777, true, true)) if (!File::makeDirectory($dirPath, 0777, true, true)) {
throw new ApplicationException(Lang::get('cms::lang.cms_object.error_creating_directory', ['name'=>$dirPath])); throw new ApplicationException(Lang::get(
'cms::lang.cms_object.error_creating_directory',
['name'=>$dirPath]
));
}
} }
if (($pos = strpos($this->fileName, '/')) !== false) { if (($pos = strpos($this->fileName, '/')) !== false) {
$dirPath = static::getFilePath($this->theme, dirname($this->fileName)); $dirPath = static::getFilePath($this->theme, dirname($this->fileName));
if (!is_dir($dirPath) && !File::makeDirectory($dirPath, 0777, true, true)) if (!is_dir($dirPath) && !File::makeDirectory($dirPath, 0777, true, true)) {
throw new ApplicationException(Lang::get('cms::lang.cms_object.error_creating_directory', ['name'=>$dirPath])); throw new ApplicationException(Lang::get(
'cms::lang.cms_object.error_creating_directory',
['name'=>$dirPath]
));
}
} }
$newFullPath = $fullPath; $newFullPath = $fullPath;
if (@File::put($fullPath, $this->content) === false) if (@File::put($fullPath, $this->content) === false) {
throw new ApplicationException(Lang::get('cms::lang.cms_object.error_saving', ['name'=>$this->fileName])); throw new ApplicationException(Lang::get(
'cms::lang.cms_object.error_saving',
['name'=>$this->fileName]
));
}
if (strlen($this->originalFileName) && $this->originalFileName !== $this->fileName) { if (strlen($this->originalFileName) && $this->originalFileName !== $this->fileName) {
$fullPath = static::getFilePath($this->theme, $this->originalFileName); $fullPath = static::getFilePath($this->theme, $this->originalFileName);
if (File::isFile($fullPath)) if (File::isFile($fullPath)) {
@unlink($fullPath); @unlink($fullPath);
}
} }
clearstatcache(); clearstatcache();
@ -378,24 +411,27 @@ class CmsObject implements ArrayAccess
*/ */
public static function listInTheme($theme, $skipCache = false) public static function listInTheme($theme, $skipCache = false)
{ {
if (!$theme) if (!$theme) {
throw new ApplicationException(Lang::get('cms::lang.theme.active.not_set')); throw new ApplicationException(Lang::get('cms::lang.theme.active.not_set'));
}
$dirPath = $theme->getPath().'/'.static::getObjectTypeDirName(); $dirPath = $theme->getPath().'/'.static::getObjectTypeDirName();
$result = []; $result = [];
if (!File::isDirectory($dirPath)) if (!File::isDirectory($dirPath)) {
return $result; return $result;
}
$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dirPath)); $it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dirPath));
$it->setMaxDepth(1); // Support only a single level of subdirectories $it->setMaxDepth(1); // Support only a single level of subdirectories
$it->rewind(); $it->rewind();
while($it->valid()) { while ($it->valid()) {
if ($it->isFile() && in_array($it->getExtension(), static::$allowedExtensions)) { if ($it->isFile() && in_array($it->getExtension(), static::$allowedExtensions)) {
$filePath = $it->getBasename(); $filePath = $it->getBasename();
if ($it->getDepth() > 0) if ($it->getDepth() > 0) {
$filePath = basename($it->getPath()).'/'.$filePath; $filePath = basename($it->getPath()).'/'.$filePath;
}
$page = $skipCache ? static::load($theme, $filePath) : static::loadCached($theme, $filePath); $page = $skipCache ? static::load($theme, $filePath) : static::loadCached($theme, $filePath);
$result[] = $page; $result[] = $page;
@ -426,8 +462,9 @@ class CmsObject implements ArrayAccess
public function __get($name) public function __get($name)
{ {
$methodName = 'get'.ucfirst($name); $methodName = 'get'.ucfirst($name);
if (method_exists($this, $methodName)) if (method_exists($this, $methodName)) {
return $this->$methodName(); return $this->$methodName();
}
return null; return null;
} }
@ -440,8 +477,9 @@ class CmsObject implements ArrayAccess
public function __isset($key) public function __isset($key)
{ {
$methodName = 'get'.ucfirst($key); $methodName = 'get'.ucfirst($key);
if (method_exists($this, $methodName)) if (method_exists($this, $methodName)) {
return true; return true;
}
return false; return false;
} }
@ -540,18 +578,24 @@ class CmsObject implements ArrayAccess
* Initializes the object properties from the cached data. * Initializes the object properties from the cached data.
* @param array $cached The cached data array. * @param array $cached The cached data array.
*/ */
protected function initFromCache($cached) {} protected function initFromCache($cached)
{
}
/** /**
* Initializes a cache item. * Initializes a cache item.
* @param array &$item The cached item array. * @param array &$item The cached item array.
*/ */
protected function initCacheItem(&$item) {} protected function initCacheItem(&$item)
{
}
/** /**
* Returns the directory name corresponding to the object type. * Returns the directory name corresponding to the object type.
* For pages the directory name is "pages", for layouts - "layouts", etc. * For pages the directory name is "pages", for layouts - "layouts", etc.
* @return string * @return string
*/ */
public static function getObjectTypeDirName() {} public static function getObjectTypeDirName()
} {
}
}

View File

@ -11,4 +11,4 @@ use System\Classes\ApplicationException;
*/ */
class CmsObjectCollection extends CollectionBase class CmsObjectCollection extends CollectionBase
{ {
} }

View File

@ -69,13 +69,15 @@ class CmsObjectQuery
*/ */
public function find($fileName) public function find($fileName)
{ {
if (!$this->theme) if (!$this->theme) {
$this->inEditTheme(); $this->inEditTheme();
}
if ($this->useCache) if ($this->useCache) {
return forward_static_call([$this->cmsObject, 'loadCached'], $this->theme, $fileName); return forward_static_call([$this->cmsObject, 'loadCached'], $this->theme, $fileName);
else } else {
return forward_static_call([$this->cmsObject, 'load'], $this->theme, $fileName); return forward_static_call([$this->cmsObject, 'load'], $this->theme, $fileName);
}
} }
/** /**
@ -84,8 +86,9 @@ class CmsObjectQuery
*/ */
public function all() public function all()
{ {
if (!$this->theme) if (!$this->theme) {
$this->inEditTheme(); $this->inEditTheme();
}
$collection = forward_static_call([$this->cmsObject, 'listInTheme'], $this->theme, !$this->useCache); $collection = forward_static_call([$this->cmsObject, 'listInTheme'], $this->theme, !$this->useCache);
$collection = new CmsObjectCollection($collection); $collection = new CmsObjectCollection($collection);
@ -108,5 +111,4 @@ class CmsObjectQuery
$className = get_class($this); $className = get_class($this);
throw new \BadMethodCallException("Call to undefined method {$className}::{$method}()"); throw new \BadMethodCallException("Call to undefined method {$className}::{$method}()");
} }
}
}

View File

@ -27,7 +27,10 @@ class CmsPropertyHelper
*/ */
public static function listPages() public static function listPages()
{ {
Flash::warning("CmsPropertyHelper::listPages() is deprecated, use Page::sortBy('baseFileName')->lists('baseFileName', 'baseFileName') instead."); Flash::warning(
"CmsPropertyHelper::listPages() is deprecated, use Page::sortBy('baseFileName')->lists('baseFileName',
'baseFileName') instead."
);
return Page::sortBy('baseFileName')->lists('baseFileName', 'baseFileName'); return Page::sortBy('baseFileName')->lists('baseFileName', 'baseFileName');
} }
} }

View File

@ -45,19 +45,25 @@ class CodeBase extends Extendable implements ArrayAccess
* This event is triggered when all components are initialized and before AJAX is handled. * This event is triggered when all components are initialized and before AJAX is handled.
* The layout's onInit method triggers before the page's onInit method. * The layout's onInit method triggers before the page's onInit method.
*/ */
public function onInit() {} public function onInit()
{
}
/** /**
* This event is triggered in the beginning of the execution cycle. * This event is triggered in the beginning of the execution cycle.
* The layout's onStart method triggers before the page's onStart method. * The layout's onStart method triggers before the page's onStart method.
*/ */
public function onStart() {} public function onStart()
{
}
/** /**
* This event is triggered in the end of the execution cycle, but before the page is displayed. * This event is triggered in the end of the execution cycle, but before the page is displayed.
* The layout's onEnd method triggers after the page's onEnd method. * The layout's onEnd method triggers after the page's onEnd method.
*/ */
public function onEnd() {} public function onEnd()
{
}
/** /**
* ArrayAccess implementation * ArrayAccess implementation
@ -99,8 +105,9 @@ class CodeBase extends Extendable implements ArrayAccess
*/ */
public function __call($method, $parameters) public function __call($method, $parameters)
{ {
if (method_exists($this, $method)) if (method_exists($this, $method)) {
return call_user_func_array([$this, $method], $parameters); return call_user_func_array([$this, $method], $parameters);
}
return call_user_func_array([$this->controller, $method], $parameters); return call_user_func_array([$this->controller, $method], $parameters);
} }
@ -115,11 +122,13 @@ class CodeBase extends Extendable implements ArrayAccess
*/ */
public function __get($name) public function __get($name)
{ {
if (($value = $this->page->{$name}) !== null) if (($value = $this->page->{$name}) !== null) {
return $value; return $value;
}
if (array_key_exists($name, $this->controller->vars)) if (array_key_exists($name, $this->controller->vars)) {
return $this[$name]; return $this[$name];
}
return null; return null;
} }

View File

@ -99,13 +99,15 @@ class CodeParser
$body = preg_replace($pattern, '', $body); $body = preg_replace($pattern, '', $body);
$parentClass = $this->object->getCodeClassParent(); $parentClass = $this->object->getCodeClassParent();
if ($parentClass !== null) if ($parentClass !== null) {
$parentClass = ' extends '.$parentClass; $parentClass = ' extends '.$parentClass;
}
$fileContents = '<?php '.PHP_EOL; $fileContents = '<?php '.PHP_EOL;
foreach ($namespaces[0] as $namespace) foreach ($namespaces[0] as $namespace) {
$fileContents .= $namespace; $fileContents .= $namespace;
}
$fileContents .= 'class '.$className.$parentClass.PHP_EOL; $fileContents .= 'class '.$className.$parentClass.PHP_EOL;
$fileContents .= '{'.PHP_EOL; $fileContents .= '{'.PHP_EOL;
@ -115,15 +117,18 @@ class CodeParser
$this->validate($fileContents); $this->validate($fileContents);
$dir = dirname($path); $dir = dirname($path);
if (!File::isDirectory($dir) && !@File::makeDirectory($dir, 0777, true)) if (!File::isDirectory($dir) && !@File::makeDirectory($dir, 0777, true)) {
throw new SystemException(Lang::get('system::lang.directory.create_fail', ['name'=>$dir])); throw new SystemException(Lang::get('system::lang.directory.create_fail', ['name'=>$dir]));
}
if (!@File::put($path, $fileContents)) if (!@File::put($path, $fileContents)) {
throw new SystemException(Lang::get('system::lang.file.create_fail', ['name'=>$dir])); throw new SystemException(Lang::get('system::lang.file.create_fail', ['name'=>$dir]));
}
$cached = $this->getCachedInfo(); $cached = $this->getCachedInfo();
if (!$cached) if (!$cached) {
$cached = []; $cached = [];
}
$result['className'] = $className; $result['className'] = $className;
$result['source'] = 'parser'; $result['source'] = 'parser';
@ -148,8 +153,9 @@ class CodeParser
{ {
$data = $this->parse(); $data = $this->parse();
if (!class_exists($data['className'])) if (!class_exists($data['className'])) {
require_once $data['filePath']; require_once $data['filePath'];
}
$className = $data['className']; $className = $data['className'];
return new $className($page, $layout, $controller); return new $className($page, $layout, $controller);
@ -186,8 +192,9 @@ class CodeParser
protected function getCachedInfo() protected function getCachedInfo()
{ {
$cached = Cache::get($this->dataCacheKey, false); $cached = Cache::get($this->dataCacheKey, false);
if ($cached !== false && ($cached = @unserialize($cached)) !== false) if ($cached !== false && ($cached = @unserialize($cached)) !== false) {
return $cached; return $cached;
}
return null; return null;
} }
@ -200,10 +207,11 @@ class CodeParser
{ {
$cached = $this->getCachedInfo(); $cached = $this->getCachedInfo();
if ($cached !== null) { if ($cached !== null) {
if (array_key_exists($this->filePath, $cached)) if (array_key_exists($this->filePath, $cached)) {
return $cached[$this->filePath]; return $cached[$this->filePath];
}
} }
return null; return null;
} }
} }

View File

@ -115,8 +115,9 @@ class CombineAssets
*/ */
public static function combine($assets = [], $path = null) public static function combine($assets = [], $path = null)
{ {
if (static::$instance === null) if (static::$instance === null) {
static::$instance = new self(); static::$instance = new self();
}
return static::$instance->prepareRequest($assets, $path); return static::$instance->prepareRequest($assets, $path);
} }
@ -128,8 +129,9 @@ class CombineAssets
public function getContents($cacheId) public function getContents($cacheId)
{ {
$cacheInfo = $this->getCache($cacheId); $cacheInfo = $this->getCache($cacheId);
if (!$cacheInfo) if (!$cacheInfo) {
throw new CmsException(Lang::get('cms::lang.combiner.not_found', ['name'=>$cacheId])); throw new CmsException(Lang::get('cms::lang.combiner.not_found', ['name'=>$cacheId]));
}
$this->path = $cacheInfo['path']; $this->path = $cacheInfo['path'];
$this->storagePath = storage_path().'/combiner/cms'; $this->storagePath = storage_path().'/combiner/cms';
@ -157,8 +159,9 @@ class CombineAssets
{ {
$extension = strtolower($extension); $extension = strtolower($extension);
if (!isset($this->aliases[$extension])) if (!isset($this->aliases[$extension])) {
$this->aliases[$extension] = []; $this->aliases[$extension] = [];
}
$this->aliases[$extension][$alias] = $file; $this->aliases[$extension][$alias] = $file;
@ -172,10 +175,11 @@ class CombineAssets
*/ */
public function resetAliases($extension = null) public function resetAliases($extension = null)
{ {
if ($extension === null) if ($extension === null) {
$this->aliases = []; $this->aliases = [];
else } else {
$this->aliases[$extension] = []; $this->aliases[$extension] = [];
}
return $this; return $this;
} }
@ -187,12 +191,13 @@ class CombineAssets
*/ */
public function getAliases($extension = null) public function getAliases($extension = null)
{ {
if ($extension === null) if ($extension === null) {
return $this->aliases; return $this->aliases;
elseif (isset($this->aliases[$extension])) } elseif (isset($this->aliases[$extension])) {
return $this->aliases[$extension]; return $this->aliases[$extension];
else } else {
return null; return null;
}
} }
/** /**
@ -212,11 +217,13 @@ class CombineAssets
$extension = strtolower($extension); $extension = strtolower($extension);
if (!isset($this->filters[$extension])) if (!isset($this->filters[$extension])) {
$this->filters[$extension] = []; $this->filters[$extension] = [];
}
if ($filter !== null) if ($filter !== null) {
$this->filters[$extension][] = $filter; $this->filters[$extension][] = $filter;
}
return $this; return $this;
} }
@ -228,10 +235,11 @@ class CombineAssets
*/ */
public function resetFilters($extension = null) public function resetFilters($extension = null)
{ {
if ($extension === null) if ($extension === null) {
$this->filters = []; $this->filters = [];
else } else {
$this->filters[$extension] = []; $this->filters[$extension] = [];
}
return $this; return $this;
} }
@ -243,12 +251,13 @@ class CombineAssets
*/ */
public function getFilters($extension = null) public function getFilters($extension = null)
{ {
if ($extension === null) if ($extension === null) {
return $this->filters; return $this->filters;
elseif (isset($this->filters[$extension])) } elseif (isset($this->filters[$extension])) {
return $this->filters[$extension]; return $this->filters[$extension];
else } else {
return null; return null;
}
} }
/** /**
@ -260,14 +269,16 @@ class CombineAssets
*/ */
protected function prepareRequest(array $assets, $path = null) protected function prepareRequest(array $assets, $path = null)
{ {
if (substr($path, -1) != '/') if (substr($path, -1) != '/') {
$path = $path.'/'; $path = $path.'/';
}
$this->path = public_path().$path; $this->path = public_path().$path;
$this->storagePath = storage_path().'/combiner/cms'; $this->storagePath = storage_path().'/combiner/cms';
if (!is_array($assets)) if (!is_array($assets)) {
$assets = [$assets]; $assets = [$assets];
}
/* /*
* Split assets in to groups. * Split assets in to groups.
@ -304,8 +315,7 @@ class CombineAssets
if (count($combineCss) > count($combineJs)) { if (count($combineCss) > count($combineJs)) {
$extension = 'css'; $extension = 'css';
$assets = $combineCss; $assets = $combineCss;
} } else {
else {
$extension = 'js'; $extension = 'js';
$assets = $combineJs; $assets = $combineJs;
} }
@ -315,12 +325,14 @@ class CombineAssets
*/ */
if ($aliasMap = $this->getAliases($extension)) { if ($aliasMap = $this->getAliases($extension)) {
foreach ($assets as $key => $asset) { foreach ($assets as $key => $asset) {
if (substr($asset, 0, 1) !== '@') if (substr($asset, 0, 1) !== '@') {
continue; continue;
}
$_asset = substr($asset, 1); $_asset = substr($asset, 1);
if (isset($aliasMap[$_asset])) if (isset($aliasMap[$_asset])) {
$assets[$key] = $aliasMap[$_asset]; $assets[$key] = $aliasMap[$_asset];
}
} }
} }
@ -358,10 +370,11 @@ class CombineAssets
$combineAction = 'Cms\Classes\Controller@combine'; $combineAction = 'Cms\Classes\Controller@combine';
$actionExists = Route::getRoutes()->getByAction($combineAction) !== null; $actionExists = Route::getRoutes()->getByAction($combineAction) !== null;
if ($actionExists) if ($actionExists) {
return URL::action($combineAction, [$outputFilename], false); return URL::action($combineAction, [$outputFilename], false);
else } else {
return Request::getBasePath().'/combine/'.$outputFilename; return Request::getBasePath().'/combine/'.$outputFilename;
}
} }
/** /**
@ -408,8 +421,9 @@ class CombineAssets
$path = $baseUri.'/combine'; $path = $baseUri.'/combine';
} }
if (strpos($path, '/') === 0) if (strpos($path, '/') === 0) {
$path = substr($path, 1); $path = substr($path, 1);
}
$path = str_replace('.', '-', $path).'/'; $path = str_replace('.', '-', $path).'/';
return $path; return $path;
@ -426,8 +440,9 @@ class CombineAssets
{ {
$cacheId = 'combiner.'.$cacheId; $cacheId = 'combiner.'.$cacheId;
if (Cache::has($cacheId)) if (Cache::has($cacheId)) {
return false; return false;
}
$this->putCacheIndex($cacheId); $this->putCacheIndex($cacheId);
Cache::forever($cacheId, serialize($cacheInfo)); Cache::forever($cacheId, serialize($cacheInfo));
@ -443,8 +458,9 @@ class CombineAssets
{ {
$cacheId = 'combiner.'.$cacheId; $cacheId = 'combiner.'.$cacheId;
if (!Cache::has($cacheId)) if (!Cache::has($cacheId)) {
return false; return false;
}
return unserialize(Cache::get($cacheId)); return unserialize(Cache::get($cacheId));
} }
@ -465,8 +481,9 @@ class CombineAssets
*/ */
public static function resetCache() public static function resetCache()
{ {
if (!Cache::has('combiner.index')) if (!Cache::has('combiner.index')) {
return; return;
}
$index = unserialize(Cache::get('combiner.index')); $index = unserialize(Cache::get('combiner.index'));
foreach ($index as $cacheId) { foreach ($index as $cacheId) {
@ -486,16 +503,17 @@ class CombineAssets
{ {
$index = []; $index = [];
if (Cache::has('combiner.index')) if (Cache::has('combiner.index')) {
$index = unserialize(Cache::get('combiner.index')); $index = unserialize(Cache::get('combiner.index'));
}
if (in_array($cacheId, $index)) if (in_array($cacheId, $index)) {
return false; return false;
}
$index[] = $cacheId; $index[] = $cacheId;
Cache::forever('combiner.index', serialize($index)); Cache::forever('combiner.index', serialize($index));
return true; return true;
} }
}
}

View File

@ -113,19 +113,29 @@ abstract class ComponentBase extends Extendable
/** /**
* Executed when this component is first initialized, before AJAX requests. * Executed when this component is first initialized, before AJAX requests.
*/ */
public function init() {} public function init()
public function onInit() {} // Deprecated: Remove ithis line if year >= 2015 {
}
// @deprecated: Remove this line if year >= 2015
public function onInit()
{
}
/** /**
* Executed when this component is bound to a page or layout, part of * Executed when this component is bound to a page or layout, part of
* the page life cycle. * the page life cycle.
*/ */
public function onRun() {} public function onRun()
{
}
/** /**
* Executed when this component is rendered on a page or layout. * Executed when this component is rendered on a page or layout.
*/ */
public function onRender() {} public function onRender()
{
}
/** /**
* Dynamically handle calls into the controller instance. * Dynamically handle calls into the controller instance.
@ -135,11 +145,13 @@ abstract class ComponentBase extends Extendable
*/ */
public function __call($method, $parameters) public function __call($method, $parameters)
{ {
if (method_exists($this, $method)) if (method_exists($this, $method)) {
return call_user_func_array([$this, $method], $parameters); return call_user_func_array([$this, $method], $parameters);
}
if (method_exists($this->controller, $method)) if (method_exists($this->controller, $method)) {
return call_user_func_array([$this->controller, $method], $parameters); return call_user_func_array([$this->controller, $method], $parameters);
}
throw new CmsException(Lang::get('cms::lang.component.method_not_found', [ throw new CmsException(Lang::get('cms::lang.component.method_not_found', [
'name' => get_class($this), 'name' => get_class($this),
@ -161,12 +173,13 @@ abstract class ComponentBase extends Extendable
* @param $default A default value to return if no value is found. * @param $default A default value to return if no value is found.
* @return string * @return string
*/ */
public function propertyOrParam($name, $default = null) public function propertyOrParam($name, $default = null)
{ {
$value = $this->property($name, $default); $value = $this->property($name, $default);
if (substr($value, 0, 1) == ':') if (substr($value, 0, 1) == ':') {
return $this->param(substr($value, 1), $default); return $this->param(substr($value, 1), $default);
}
return $value; return $value;
} }
@ -202,5 +215,4 @@ abstract class ComponentBase extends Extendable
// return $this->pageUrl($page, $params); // return $this->pageUrl($page, $params);
// } // }
}
}

View File

@ -38,7 +38,9 @@ class ComponentHelpers
]; ];
foreach ($params as $name => $value) { foreach ($params as $name => $value) {
if (isset($property[$name])) continue; if (isset($property[$name])) {
continue;
}
$property[$name] = $value; $property[$name] = $value;
} }
@ -47,7 +49,9 @@ class ComponentHelpers
*/ */
$translate = ['title', 'description']; $translate = ['title', 'description'];
foreach ($property as $name => $value) { foreach ($property as $name => $value) {
if (!in_array($name, $translate)) continue; if (!in_array($name, $translate)) {
continue;
}
$property[$name] = Lang::get($value); $property[$name] = Lang::get($value);
} }
@ -69,8 +73,9 @@ class ComponentHelpers
$result['oc.alias'] = $component->alias; $result['oc.alias'] = $component->alias;
$properties = $component->defineProperties(); $properties = $component->defineProperties();
foreach ($properties as $name => $params) foreach ($properties as $name => $params) {
$result[$name] = $component->property($name); $result[$name] = $component->property($name);
}
return json_encode($result); return json_encode($result);
} }
@ -104,4 +109,4 @@ class ComponentHelpers
return Lang::get($name); return Lang::get($name);
} }
} }

View File

@ -61,8 +61,9 @@ class ComponentManager
foreach ($plugins as $plugin) { foreach ($plugins as $plugin) {
$components = $plugin->registerComponents(); $components = $plugin->registerComponents();
if (!is_array($components)) if (!is_array($components)) {
continue; continue;
}
foreach ($components as $className => $code) { foreach ($components as $className => $code) {
$this->registerComponent($className, $code, $plugin); $this->registerComponent($className, $code, $plugin);
@ -91,23 +92,31 @@ class ComponentManager
*/ */
public function registerComponent($className, $code = null, $plugin = null) public function registerComponent($className, $code = null, $plugin = null)
{ {
if (!$this->classMap) if (!$this->classMap) {
$this->classMap = []; $this->classMap = [];
}
if (!$this->codeMap) if (!$this->codeMap) {
$this->codeMap = []; $this->codeMap = [];
}
if (!$code) if (!$code) {
$code = Str::getClassId($className); $code = Str::getClassId($className);
}
if ($code == 'viewBag' && $className != 'Cms\Classes\ViewBag') if ($code == 'viewBag' && $className != 'Cms\Classes\ViewBag') {
throw new SystemException(sprintf('The component code viewBag is reserved. Please use another code for the component class %s.', $className)); throw new SystemException(sprintf(
'The component code viewBag is reserved. Please use another code for the component class %s.',
$className
));
}
$className = Str::normalizeClassName($className); $className = Str::normalizeClassName($className);
$this->codeMap[$code] = $className; $this->codeMap[$code] = $className;
$this->classMap[$className] = $code; $this->classMap[$className] = $code;
if ($plugin !== null) if ($plugin !== null) {
$this->pluginMap[$className] = $plugin; $this->pluginMap[$className] = $plugin;
}
} }
/** /**
@ -116,8 +125,9 @@ class ComponentManager
*/ */
public function listComponents() public function listComponents()
{ {
if ($this->codeMap === null) if ($this->codeMap === null) {
$this->loadComponents(); $this->loadComponents();
}
return $this->codeMap; return $this->codeMap;
} }
@ -128,8 +138,9 @@ class ComponentManager
*/ */
public function listComponentDetails() public function listComponentDetails()
{ {
if ($this->detailsCache !== null) if ($this->detailsCache !== null) {
return $this->detailsCache; return $this->detailsCache;
}
$details = []; $details = [];
foreach ($this->listComponents() as $componentAlias => $componentClass) { foreach ($this->listComponents() as $componentAlias => $componentClass) {
@ -148,12 +159,14 @@ class ComponentManager
{ {
$codes = $this->listComponents(); $codes = $this->listComponents();
if (isset($codes[$name])) if (isset($codes[$name])) {
return $codes[$name]; return $codes[$name];
}
$name = Str::normalizeClassName($name); $name = Str::normalizeClassName($name);
if (isset($this->classMap[$name])) if (isset($this->classMap[$name])) {
return $name; return $name;
}
return null; return null;
} }
@ -166,8 +179,9 @@ class ComponentManager
public function hasComponent($name) public function hasComponent($name)
{ {
$className = $this->resolve($name); $className = $this->resolve($name);
if (!$className) if (!$className) {
return false; return false;
}
return isset($this->classMap[$className]); return isset($this->classMap[$className]);
} }
@ -182,11 +196,19 @@ class ComponentManager
public function makeComponent($name, $cmsObject = null, $properties = []) public function makeComponent($name, $cmsObject = null, $properties = [])
{ {
$className = $this->resolve($name); $className = $this->resolve($name);
if (!$className) if (!$className) {
throw new SystemException(sprintf('Class name is not registered for the component %s. Check the component plugin.', $name)); throw new SystemException(sprintf(
'Class name is not registered for the component %s. Check the component plugin.',
$name
));
}
if (!class_exists($className)) if (!class_exists($className)) {
throw new SystemException(sprintf('Component class not found %s.Check the component plugin.', $className)); throw new SystemException(sprintf(
'Component class not found %s.Check the component plugin.',
$className
));
}
$component = new $className($cmsObject, $properties); $component = new $className($cmsObject, $properties);
$component->name = $name; $component->name = $name;
@ -202,9 +224,10 @@ class ComponentManager
public function findComponentPlugin($component) public function findComponentPlugin($component)
{ {
$className = Str::normalizeClassName(get_class($component)); $className = Str::normalizeClassName(get_class($component));
if (isset($this->pluginMap[$className])) if (isset($this->pluginMap[$className])) {
return $this->pluginMap[$className]; return $this->pluginMap[$className];
}
return null; return null;
} }
} }

View File

@ -59,8 +59,9 @@ class ComponentPartial extends CmsObject
if (!File::isFile($path)) { if (!File::isFile($path)) {
$sharedDir = dirname($component->getPath()).'/partials'; $sharedDir = dirname($component->getPath()).'/partials';
$sharedPath = $sharedDir.'/'.$fileName; $sharedPath = $sharedDir.'/'.$fileName;
if (File::isFile($sharedPath)) if (File::isFile($sharedPath)) {
return $sharedPath; return $sharedPath;
}
} }
return $path; return $path;

View File

@ -37,8 +37,9 @@ class Content extends CmsCompoundObject
*/ */
public static function load($theme, $fileName) public static function load($theme, $fileName)
{ {
if ($obj = parent::load($theme, $fileName)) if ($obj = parent::load($theme, $fileName)) {
$obj->parsedMarkup = $obj->parseMarkup(); $obj->parsedMarkup = $obj->parseMarkup();
}
return $obj; return $obj;
} }
@ -70,8 +71,9 @@ class Content extends CmsCompoundObject
{ {
$result = $this->markup; $result = $this->markup;
if (strtolower(File::extension($this->fileName)) == 'md') if (strtolower(File::extension($this->fileName)) == 'md') {
$result = Markdown::parse($this->markup); $result = Markdown::parse($this->markup);
}
return $result; return $result;
} }

View File

@ -107,8 +107,9 @@ class Controller extends BaseController
public function __construct($theme = null) public function __construct($theme = null)
{ {
$this->theme = $theme ? $theme : Theme::getActiveTheme(); $this->theme = $theme ? $theme : Theme::getActiveTheme();
if (!$this->theme) if (!$this->theme) {
throw new CmsException(Lang::get('cms::lang.theme.active.not_found')); throw new CmsException(Lang::get('cms::lang.theme.active.not_found'));
}
$this->assetPath = Config::get('cms.themesDir').'/'.$this->theme->getDirName(); $this->assetPath = Config::get('cms.themesDir').'/'.$this->theme->getDirName();
$this->router = new Router($this->theme); $this->router = new Router($this->theme);
@ -137,29 +138,34 @@ class Controller extends BaseController
*/ */
public function run($url = '/') public function run($url = '/')
{ {
if ($url === null) if ($url === null) {
$url = Request::path(); $url = Request::path();
}
if (!strlen($url)) if (!strlen($url)) {
$url = '/'; $url = '/';
}
/* /*
* Handle hidden pages * Handle hidden pages
*/ */
$page = $this->router->findByUrl($url); $page = $this->router->findByUrl($url);
if ($page && $page->hidden) { if ($page && $page->hidden) {
if (!BackendAuth::getUser()) if (!BackendAuth::getUser()) {
$page = null; $page = null;
}
} }
/* /*
* Extensibility * Extensibility
*/ */
if ($event = $this->fireEvent('page.beforeDisplay', [$url, $page], true)) if ($event = $this->fireEvent('page.beforeDisplay', [$url, $page], true)) {
return $event; return $event;
}
if ($event = Event::fire('cms.page.beforeDisplay', [$this, $url, $page], true)) if ($event = Event::fire('cms.page.beforeDisplay', [$this, $url, $page], true)) {
return $event; return $event;
}
/* /*
* If the page was not found, render the 404 page - either provided by the theme or the built-in one. * If the page was not found, render the 404 page - either provided by the theme or the built-in one.
@ -168,11 +174,13 @@ class Controller extends BaseController
$this->setStatusCode(404); $this->setStatusCode(404);
// Log the 404 request // Log the 404 request
if (!App::runningUnitTests()) if (!App::runningUnitTests()) {
RequestLog::add(); RequestLog::add();
}
if (!$page = $this->router->findByUrl('/404')) if (!$page = $this->router->findByUrl('/404')) {
return Response::make(View::make('cms::404'), $this->statusCode); return Response::make(View::make('cms::404'), $this->statusCode);
}
} }
$this->page = $page; $this->page = $page;
@ -181,10 +189,11 @@ class Controller extends BaseController
* If the page doesn't refer any layout, create the fallback layout. * If the page doesn't refer any layout, create the fallback layout.
* Otherwise load the layout specified in the page. * Otherwise load the layout specified in the page.
*/ */
if (!$page->layout) if (!$page->layout) {
$layout = Layout::initFallback($this->theme); $layout = Layout::initFallback($this->theme);
elseif (($layout = Layout::loadCached($this->theme, $page->layout)) === null) } elseif (($layout = Layout::loadCached($this->theme, $page->layout)) === null) {
throw new CmsException(Lang::get('cms::lang.layout.not_found', ['name'=>$page->layout])); throw new CmsException(Lang::get('cms::lang.layout.not_found', ['name'=>$page->layout]));
}
$this->layout = $layout; $this->layout = $layout;
@ -223,29 +232,38 @@ class Controller extends BaseController
/* /*
* Extensibility * Extensibility
*/ */
if ($event = $this->fireEvent('page.init', [$url, $page], true)) if ($event = $this->fireEvent('page.init', [$url, $page], true)) {
return $event; return $event;
}
if ($event = Event::fire('cms.page.init', [$this, $url, $page], true)) if ($event = Event::fire('cms.page.init', [$this, $url, $page], true)) {
return $event; return $event;
}
/* /*
* Execute AJAX event * Execute AJAX event
*/ */
if ($ajaxResponse = $this->execAjaxHandlers()) if ($ajaxResponse = $this->execAjaxHandlers()) {
return $ajaxResponse; return $ajaxResponse;
}
/* /*
* Execute postback handler * Execute postback handler
*/ */
if (($handler = post('_handler')) && ($handlerResponse = $this->runAjaxHandler($handler)) && $handlerResponse !== true) if (
($handler = post('_handler')) &&
($handlerResponse = $this->runAjaxHandler($handler)) &&
$handlerResponse !== true
) {
return $handlerResponse; return $handlerResponse;
}
/* /*
* Execute page lifecycle * Execute page lifecycle
*/ */
if ($cycleResponse = $this->execPageCycle()) if ($cycleResponse = $this->execPageCycle()) {
return $cycleResponse; return $cycleResponse;
}
/* /*
* Render the page * Render the page
@ -268,14 +286,17 @@ class Controller extends BaseController
/* /*
* Extensibility * Extensibility
*/ */
if ($event = $this->fireEvent('page.display', [$url, $page], true)) if ($event = $this->fireEvent('page.display', [$url, $page], true)) {
return $event; return $event;
}
if ($event = Event::fire('cms.page.display', [$this, $url, $page], true)) if ($event = Event::fire('cms.page.display', [$this, $url, $page], true)) {
return $event; return $event;
}
if (!is_string($result)) if (!is_string($result)) {
return $result; return $result;
}
return Response::make($result, $this->statusCode); return Response::make($result, $this->statusCode);
} }
@ -295,15 +316,17 @@ class Controller extends BaseController
'auto_reload' => true, 'auto_reload' => true,
'debug' => $isDebugMode, 'debug' => $isDebugMode,
]; ];
if (!Config::get('cms.twigNoCache')) if (!Config::get('cms.twigNoCache')) {
$options['cache'] = storage_path().'/twig'; $options['cache'] = storage_path().'/twig';
}
$this->twig = new Twig_Environment($this->loader, $options); $this->twig = new Twig_Environment($this->loader, $options);
$this->twig->addExtension(new CmsTwigExtension($this)); $this->twig->addExtension(new CmsTwigExtension($this));
$this->twig->addExtension(new SystemTwigExtension); $this->twig->addExtension(new SystemTwigExtension);
if ($isDebugMode) if ($isDebugMode) {
$this->twig->addExtension(new DebugExtension($this)); $this->twig->addExtension(new DebugExtension($this));
}
} }
/** /**
@ -335,13 +358,15 @@ class Controller extends BaseController
{ {
if (!$this->layout->isFallBack()) { if (!$this->layout->isFallBack()) {
foreach ($this->layout->settings['components'] as $component => $properties) { foreach ($this->layout->settings['components'] as $component => $properties) {
list($name, $alias) = strpos($component, ' ') ? explode(' ', $component) : array($component, $component); list($name, $alias) = strpos($component, ' ') ?
explode(' ', $component) : array($component, $component);
$this->addComponent($name, $alias, $properties, true); $this->addComponent($name, $alias, $properties, true);
} }
} }
foreach ($this->page->settings['components'] as $component => $properties) { foreach ($this->page->settings['components'] as $component => $properties) {
list($name, $alias) = strpos($component, ' ') ? explode(' ', $component) : array($component, $component); list($name, $alias) = strpos($component, ' ') ?
explode(' ', $component) : array($component, $component);
$this->addComponent($name, $alias, $properties); $this->addComponent($name, $alias, $properties);
} }
} }
@ -359,15 +384,16 @@ class Controller extends BaseController
$manager = ComponentManager::instance(); $manager = ComponentManager::instance();
if ($addToLayout) { if ($addToLayout) {
if (!$componentObj = $manager->makeComponent($name, $this->layoutObj, $properties)) if (!$componentObj = $manager->makeComponent($name, $this->layoutObj, $properties)) {
throw new CmsException(Lang::get('cms::lang.component.not_found', ['name'=>$name])); throw new CmsException(Lang::get('cms::lang.component.not_found', ['name'=>$name]));
}
$componentObj->alias = $alias; $componentObj->alias = $alias;
$this->vars[$alias] = $this->layout->components[$alias] = $componentObj; $this->vars[$alias] = $this->layout->components[$alias] = $componentObj;
} } else {
else { if (!$componentObj = $manager->makeComponent($name, $this->pageObj, $properties)) {
if (!$componentObj = $manager->makeComponent($name, $this->pageObj, $properties))
throw new CmsException(Lang::get('cms::lang.component.not_found', ['name'=>$name])); throw new CmsException(Lang::get('cms::lang.component.not_found', ['name'=>$name]));
}
$componentObj->alias = $alias; $componentObj->alias = $alias;
$this->vars[$alias] = $this->page->components[$alias] = $componentObj; $this->vars[$alias] = $this->page->components[$alias] = $componentObj;
@ -389,8 +415,9 @@ class Controller extends BaseController
/* /*
* Validate the handler name * Validate the handler name
*/ */
if (!preg_match('/^(?:\w+\:{2})?on[A-Z]{1}[\w+]*$/', $handler)) if (!preg_match('/^(?:\w+\:{2})?on[A-Z]{1}[\w+]*$/', $handler)) {
throw new CmsException(Lang::get('cms::lang.ajax_handler.invalid_name', ['name'=>$handler])); throw new CmsException(Lang::get('cms::lang.ajax_handler.invalid_name', ['name'=>$handler]));
}
/* /*
* Validate the handler partial list * Validate the handler partial list
@ -399,11 +426,11 @@ class Controller extends BaseController
$partialList = explode('&', $partialList); $partialList = explode('&', $partialList);
foreach ($partialList as $partial) { foreach ($partialList as $partial) {
if (!preg_match('/^(?:\w+\:{2}|@)?[a-z0-9\_\-\.\/]+$/i', $partial)) if (!preg_match('/^(?:\w+\:{2}|@)?[a-z0-9\_\-\.\/]+$/i', $partial)) {
throw new CmsException(Lang::get('cms::lang.partial.invalid_name', ['name'=>$partial])); throw new CmsException(Lang::get('cms::lang.partial.invalid_name', ['name'=>$partial]));
}
} }
} } else {
else {
$partialList = []; $partialList = [];
} }
@ -412,23 +439,26 @@ class Controller extends BaseController
/* /*
* Execute the handler * Execute the handler
*/ */
if (!$result = $this->runAjaxHandler($handler)) if (!$result = $this->runAjaxHandler($handler)) {
throw new CmsException(Lang::get('cms::lang.ajax_handler.not_found', ['name'=>$handler])); throw new CmsException(Lang::get('cms::lang.ajax_handler.not_found', ['name'=>$handler]));
}
/* /*
* If the handler returned an array, we should add it to output for rendering. * If the handler returned an array, we should add it to output for rendering.
* If it is a string, add it to the array with the key "result". * If it is a string, add it to the array with the key "result".
*/ */
if (is_array($result)) if (is_array($result)) {
$responseContents = array_merge($responseContents, $result); $responseContents = array_merge($responseContents, $result);
elseif (is_string($result)) } elseif (is_string($result)) {
$responseContents['result'] = $result; $responseContents['result'] = $result;
}
/* /*
* Render partials and return the response as array that will be converted to JSON automatically. * Render partials and return the response as array that will be converted to JSON automatically.
*/ */
foreach ($partialList as $partial) foreach ($partialList as $partial) {
$responseContents[$partial] = $this->renderPartial($partial); $responseContents[$partial] = $this->renderPartial($partial);
}
/* /*
* If the handler returned a redirect, process it so framework.js knows to redirect * If the handler returned a redirect, process it so framework.js knows to redirect
@ -439,27 +469,28 @@ class Controller extends BaseController
} }
return Response::make()->setContent($responseContents); return Response::make()->setContent($responseContents);
} } catch (ValidationException $ex) {
catch (ValidationException $ex) {
/* /*
* Handle validation errors * Handle validation errors
*/ */
$responseContents['X_OCTOBER_ERROR_FIELDS'] = $ex->getFields(); $responseContents['X_OCTOBER_ERROR_FIELDS'] = $ex->getFields();
$responseContents['X_OCTOBER_ERROR_MESSAGE'] = $ex->getMessage(); $responseContents['X_OCTOBER_ERROR_MESSAGE'] = $ex->getMessage();
return Response::make($responseContents, 406); return Response::make($responseContents, 406);
} } catch (ApplicationException $ex) {
catch (ApplicationException $ex) {
return Response::make($ex->getMessage(), 500); return Response::make($ex->getMessage(), 500);
} } catch (Exception $ex) {
catch (Exception $ex) {
/* /*
* Display a "dumbed down" error if custom page is activated * Display a "dumbed down" error if custom page is activated
* otherwise display a more detailed error. * otherwise display a more detailed error.
*/ */
if (Config::get('cms.customErrorPage', false)) if (Config::get('cms.customErrorPage', false)) {
return Response::make($ex->getMessage(), 500); return Response::make($ex->getMessage(), 500);
}
return Response::make(sprintf('"%s" on line %s of %s', $ex->getMessage(), $ex->getLine(), $ex->getFile()), 500); return Response::make(
sprintf('"%s" on line %s of %s', $ex->getMessage(), $ex->getLine(), $ex->getFile()),
500
);
} }
} }
@ -486,11 +517,10 @@ class Controller extends BaseController
$result = $componentObj->$handlerName(); $result = $componentObj->$handlerName();
return ($result) ?: true; return ($result) ?: true;
} }
}
/* /*
* Process code section handler * Process code section handler
*/ */
else { } else {
if (method_exists($this->pageObj, $handler)) { if (method_exists($this->pageObj, $handler)) {
$result = $this->pageObj->$handler(); $result = $this->pageObj->$handler();
return ($result) ?: true; return ($result) ?: true;
@ -533,11 +563,13 @@ class Controller extends BaseController
/* /*
* Extensibility * Extensibility
*/ */
if ($event = $this->fireEvent('page.start', [], true)) if ($event = $this->fireEvent('page.start', [], true)) {
return $event; return $event;
}
if ($event = Event::fire('cms.page.start', [$this], true)) if ($event = Event::fire('cms.page.start', [$this], true)) {
return $event; return $event;
}
/* /*
* Run layout functions * Run layout functions
@ -549,7 +581,9 @@ class Controller extends BaseController
|| ($result = $this->layoutObj->onBeforePageStart())) ? $result: null; || ($result = $this->layoutObj->onBeforePageStart())) ? $result: null;
CmsException::unmask(); CmsException::unmask();
if ($response) return $response; if ($response) {
return $response;
}
} }
/* /*
@ -561,7 +595,9 @@ class Controller extends BaseController
|| ($result = $this->pageObj->onEnd())) ? $result : null; || ($result = $this->pageObj->onEnd())) ? $result : null;
CmsException::unmask(); CmsException::unmask();
if ($response) return $response; if ($response) {
return $response;
}
/* /*
* Run remaining layout functions * Run remaining layout functions
@ -575,11 +611,13 @@ class Controller extends BaseController
/* /*
* Extensibility * Extensibility
*/ */
if ($event = $this->fireEvent('page.end', [], true)) if ($event = $this->fireEvent('page.end', [], true)) {
return $event; return $event;
}
if ($event = Event::fire('cms.page.end', [$this], true)) if ($event = Event::fire('cms.page.end', [$this], true)) {
return $event; return $event;
}
return $response; return $response;
} }
@ -595,11 +633,13 @@ class Controller extends BaseController
/* /*
* Extensibility * Extensibility
*/ */
if ($event = $this->fireEvent('page.render', [$contents], true)) if ($event = $this->fireEvent('page.render', [$contents], true)) {
return $event; return $event;
}
if ($event = Event::fire('cms.page.render', [$this, $contents], true)) if ($event = Event::fire('cms.page.render', [$this, $contents], true)) {
return $event; return $event;
}
return $contents; return $contents;
} }
@ -617,8 +657,9 @@ class Controller extends BaseController
/* /*
* Alias @ symbol for :: * Alias @ symbol for ::
*/ */
if (substr($name, 0, 1) == '@') if (substr($name, 0, 1) == '@') {
$name = '::' . substr($name, 1); $name = '::' . substr($name, 1);
}
/* /*
* Process Component partial * Process Component partial
@ -633,23 +674,23 @@ class Controller extends BaseController
if (!strlen($componentAlias)) { if (!strlen($componentAlias)) {
if ($this->componentContext !== null) { if ($this->componentContext !== null) {
$componentObj = $this->componentContext; $componentObj = $this->componentContext;
} } elseif (($componentObj = $this->findComponentByPartial($partialName)) === null) {
elseif (($componentObj = $this->findComponentByPartial($partialName)) === null) { if ($throwException) {
if ($throwException)
throw new CmsException(Lang::get('cms::lang.partial.not_found', ['name'=>$name])); throw new CmsException(Lang::get('cms::lang.partial.not_found', ['name'=>$name]));
else } else {
return false; return false;
}
} }
}
/* /*
* Component alias is supplied * Component alias is supplied
*/ */
else { } else {
if (($componentObj = $this->findComponentByName($componentAlias)) === null) { if (($componentObj = $this->findComponentByName($componentAlias)) === null) {
if ($throwException) if ($throwException) {
throw new CmsException(Lang::get('cms::lang.component.not_found', ['name'=>$componentAlias])); throw new CmsException(Lang::get('cms::lang.component.not_found', ['name'=>$componentAlias]));
else } else {
return false; return false;
}
} }
} }
@ -667,31 +708,33 @@ class Controller extends BaseController
/* /*
* Check the component partial * Check the component partial
*/ */
if ($partial === null) if ($partial === null) {
$partial = ComponentPartial::loadCached($componentObj, $partialName); $partial = ComponentPartial::loadCached($componentObj, $partialName);
}
if ($partial === null) { if ($partial === null) {
if ($throwException) if ($throwException) {
throw new CmsException(Lang::get('cms::lang.partial.not_found', ['name'=>$name])); throw new CmsException(Lang::get('cms::lang.partial.not_found', ['name'=>$name]));
else } else {
return false; return false;
}
} }
/* /*
* Set context for self access * Set context for self access
*/ */
$this->vars['__SELF__'] = $componentObj; $this->vars['__SELF__'] = $componentObj;
} } else {
else {
/* /*
* Process theme partial * Process theme partial
*/ */
if (($partial = Partial::loadCached($this->theme, $name)) === null) { if (($partial = Partial::loadCached($this->theme, $name)) === null) {
if ($throwException) if ($throwException) {
throw new CmsException(Lang::get('cms::lang.partial.not_found', ['name'=>$name])); throw new CmsException(Lang::get('cms::lang.partial.not_found', ['name'=>$name]));
else } else {
return false; return false;
}
} }
} }
@ -714,28 +757,29 @@ class Controller extends BaseController
/* /*
* Extensibility * Extensibility
*/ */
if ($event = $this->fireEvent('page.beforeRenderContent', [$name], true)) if ($event = $this->fireEvent('page.beforeRenderContent', [$name], true)) {
$content = $event; $content = $event;
} elseif ($event = Event::fire('cms.page.beforeRenderContent', [$this, $name], true)) {
elseif ($event = Event::fire('cms.page.beforeRenderContent', [$this, $name], true))
$content = $event; $content = $event;
/* /*
* Load content from theme * Load content from theme
*/ */
elseif (($content = Content::loadCached($this->theme, $name)) === null) } elseif (($content = Content::loadCached($this->theme, $name)) === null) {
throw new CmsException(Lang::get('cms::lang.content.not_found', ['name'=>$name])); throw new CmsException(Lang::get('cms::lang.content.not_found', ['name'=>$name]));
}
$fileContent = $content->parsedMarkup; $fileContent = $content->parsedMarkup;
/* /*
* Extensibility * Extensibility
*/ */
if ($event = $this->fireEvent('page.renderContent', [$name, $fileContent], true)) if ($event = $this->fireEvent('page.renderContent', [$name, $fileContent], true)) {
return $event; return $event;
}
if ($event = Event::fire('cms.page.renderContent', [$this, $name, $fileContent], true)) if ($event = Event::fire('cms.page.renderContent', [$this, $name, $fileContent], true)) {
return $event; return $event;
}
return $fileContent; return $fileContent;
} }
@ -749,8 +793,9 @@ class Controller extends BaseController
if ($componentObj = $this->findComponentByName($name)) { if ($componentObj = $this->findComponentByName($name)) {
$componentObj->id = uniqid($name); $componentObj->id = uniqid($name);
$componentObj->setProperties(array_merge($componentObj->getProperties(), $parameters)); $componentObj->setProperties(array_merge($componentObj->getProperties(), $parameters));
if ($result = $componentObj->onRender()) if ($result = $componentObj->onRender()) {
return $result; return $result;
}
} }
return $this->renderPartial($name.'::default', [], false); return $this->renderPartial($name.'::default', [], false);
@ -823,8 +868,9 @@ class Controller extends BaseController
*/ */
public function pageUrl($name, $parameters = [], $routePersistence = true, $absolute = true) public function pageUrl($name, $parameters = [], $routePersistence = true, $absolute = true)
{ {
if (!$name) if (!$name) {
return null; return null;
}
/* /*
* Second parameter can act as third * Second parameter can act as third
@ -834,14 +880,17 @@ class Controller extends BaseController
$parameters = []; $parameters = [];
} }
if ($routePersistence) if ($routePersistence) {
$parameters = array_merge($this->router->getParameters(), $parameters); $parameters = array_merge($this->router->getParameters(), $parameters);
}
if (!$url = $this->router->findByFile($name, $parameters)) if (!$url = $this->router->findByFile($name, $parameters)) {
return null; return null;
}
if (substr($url, 0, 1) == '/') if (substr($url, 0, 1) == '/') {
$url = substr($url, 1); $url = substr($url, 1);
}
return URL::action('Cms\Classes\Controller@run', ['slug' => $url], $absolute); return URL::action('Cms\Classes\Controller@run', ['slug' => $url], $absolute);
} }
@ -867,10 +916,11 @@ class Controller extends BaseController
if (is_array($url)) { if (is_array($url)) {
$_url = Request::getBaseUrl(); $_url = Request::getBaseUrl();
$_url .= CombineAssets::combine($url, $themePath); $_url .= CombineAssets::combine($url, $themePath);
} } else {
else {
$_url = Request::getBasePath().$themePath; $_url = Request::getBasePath().$themePath;
if ($url !== null) $_url .= '/'.$url; if ($url !== null) {
$_url .= '/'.$url;
}
} }
return $_url; return $_url;
@ -894,19 +944,17 @@ class Controller extends BaseController
*/ */
public function combine($name) public function combine($name)
{ {
try { try {
if (!strpos($name, '-')) if (!strpos($name, '-')) {
throw new CmsException(Lang::get('cms::lang.combiner.not_found', ['name'=>$name])); throw new CmsException(Lang::get('cms::lang.combiner.not_found', ['name'=>$name]));
}
$parts = explode('-', $name); $parts = explode('-', $name);
$cacheId = $parts[0]; $cacheId = $parts[0];
$combiner = new CombineAssets;
$combiner = new CombineAssets; return $combiner->getContents($cacheId);
return $combiner->getContents($cacheId); } catch (Exception $ex) {
} return '/* '.$ex->getMessage().' */';
catch (Exception $ex) { }
return '/* '.$ex->getMessage().' */';
}
} }
/** /**
@ -915,11 +963,13 @@ class Controller extends BaseController
*/ */
protected function findComponentByName($name) protected function findComponentByName($name)
{ {
if (isset($this->page->components[$name])) if (isset($this->page->components[$name])) {
return $this->page->components[$name]; return $this->page->components[$name];
}
if (isset($this->layout->components[$name])) if (isset($this->layout->components[$name])) {
return $this->layout->components[$name]; return $this->layout->components[$name];
}
return null; return null;
} }
@ -931,13 +981,15 @@ class Controller extends BaseController
protected function findComponentByHandler($handler) protected function findComponentByHandler($handler)
{ {
foreach ($this->page->components as $component) { foreach ($this->page->components as $component) {
if (method_exists($component, $handler)) if (method_exists($component, $handler)) {
return $component; return $component;
}
} }
foreach ($this->layout->components as $component) { foreach ($this->layout->components as $component) {
if (method_exists($component, $handler)) if (method_exists($component, $handler)) {
return $component; return $component;
}
} }
return null; return null;
@ -951,20 +1003,24 @@ class Controller extends BaseController
{ {
foreach ($this->page->components as $component) { foreach ($this->page->components as $component) {
$fileName = ComponentPartial::getFilePath($component, $partial); $fileName = ComponentPartial::getFilePath($component, $partial);
if (!strlen(File::extension($fileName))) if (!strlen(File::extension($fileName))) {
$fileName .= '.htm'; $fileName .= '.htm';
}
if (File::isFile($fileName)) if (File::isFile($fileName)) {
return $component; return $component;
}
} }
foreach ($this->layout->components as $component) { foreach ($this->layout->components as $component) {
$fileName = ComponentPartial::getFilePath($component, $partial); $fileName = ComponentPartial::getFilePath($component, $partial);
if (!strlen(File::extension($fileName))) if (!strlen(File::extension($fileName))) {
$fileName .= '.htm'; $fileName .= '.htm';
}
if (File::isFile($fileName)) if (File::isFile($fileName)) {
return $component; return $component;
}
} }
return null; return null;
@ -985,7 +1041,10 @@ class Controller extends BaseController
// if (($page = Page::loadCached($theme, $page)) && isset($page->settings['components'])) { // if (($page = Page::loadCached($theme, $page)) && isset($page->settings['components'])) {
// foreach ($page->settings['components'] as $component => $properties) { // foreach ($page->settings['components'] as $component => $properties) {
// list($name, $alias) = strpos($component, ' ') ? explode(' ', $component) : array($component, $component); // list($name, $alias) = strpos($component, ' ') ?
// explode(' ', $component) :
// array($component, $component)
// ;
// if ($manager->resolve($name) == $class) { // if ($manager->resolve($name) == $class) {
// $componentObj->setProperties($properties); // $componentObj->setProperties($properties);
// $componentObj->alias = $alias; // $componentObj->alias = $alias;
@ -999,7 +1058,10 @@ class Controller extends BaseController
// $layout = $page->settings['layout']; // $layout = $page->settings['layout'];
// if (($layout = Layout::loadCached($theme, $layout)) && isset($layout->settings['components'])) { // if (($layout = Layout::loadCached($theme, $layout)) && isset($layout->settings['components'])) {
// foreach ($layout->settings['components'] as $component => $properties) { // foreach ($layout->settings['components'] as $component => $properties) {
// list($name, $alias) = strpos($component, ' ') ? explode(' ', $component) : array($component, $component); // list($name, $alias) = strpos($component, ' ') ?
// explode(' ', $component) :
// array($component, $component)
// ;
// if ($manager->resolve($name) == $class) { // if ($manager->resolve($name) == $class) {
// $componentObj->setProperties($properties); // $componentObj->setProperties($properties);
// $componentObj->alias = $alias; // $componentObj->alias = $alias;
@ -1011,5 +1073,4 @@ class Controller extends BaseController
// return null; // return null;
// } // }
} }

View File

@ -30,8 +30,9 @@ class FileHelper
public static function validateExtension($fileName, $allowedExtensions, $allowEmpty = true) public static function validateExtension($fileName, $allowedExtensions, $allowEmpty = true)
{ {
$extension = strtolower(pathinfo($fileName, PATHINFO_EXTENSION)); $extension = strtolower(pathinfo($fileName, PATHINFO_EXTENSION));
if (!strlen($extension)) if (!strlen($extension)) {
return $allowEmpty; return $allowEmpty;
}
return in_array($extension, $allowedExtensions); return in_array($extension, $allowedExtensions);
} }
@ -46,19 +47,24 @@ class FileHelper
*/ */
public static function validatePath($filePath, $maxNesting = 2) public static function validatePath($filePath, $maxNesting = 2)
{ {
if (strpos($filePath, '..') !== false) if (strpos($filePath, '..') !== false) {
return false; return false;
}
if (strpos($filePath, './') !== false || strpos($filePath, '//') !== false) if (strpos($filePath, './') !== false || strpos($filePath, '//') !== false) {
return false; return false;
}
$segments = explode('/', $filePath); $segments = explode('/', $filePath);
if ($maxNesting !== null && count($segments) > $maxNesting) if ($maxNesting !== null && count($segments) > $maxNesting) {
return false; return false;
}
foreach ($segments as $segment) foreach ($segments as $segment) {
if (!self::validateName($segment)) if (!self::validateName($segment)) {
return false; return false;
}
}
return true; return true;
} }
@ -74,20 +80,23 @@ class FileHelper
$content = null; $content = null;
$sections = []; $sections = [];
foreach ($data as $key=>$value) { foreach ($data as $key => $value) {
if (is_array($value)) { if (is_array($value)) {
if ($level == 1) if ($level == 1) {
$sections[$key] = self::formatIniString($value, $level+1); $sections[$key] = self::formatIniString($value, $level+1);
else { } else {
foreach ($value as $val) foreach ($value as $val) {
$content .= $key.'[] = "'.self::escapeIniString($val).'"'.PHP_EOL; $content .= $key.'[] = "'.self::escapeIniString($val).'"'.PHP_EOL;
}
} }
} elseif (strlen($value)) } elseif (strlen($value)) {
$content .= $key.' = "'.self::escapeIniString($value).'"'.PHP_EOL; $content .= $key.' = "'.self::escapeIniString($value).'"'.PHP_EOL;
}
} }
foreach ($sections as $key=>$section) foreach ($sections as $key => $section) {
$content .= PHP_EOL.'['.$key.']'.PHP_EOL.$section.PHP_EOL; $content .= PHP_EOL.'['.$key.']'.PHP_EOL.$section.PHP_EOL;
}
return trim($content); return trim($content);
} }

View File

@ -10,7 +10,9 @@ class Layout extends CmsCompoundObject
{ {
const FALLBACK_FILE_NAME = 'fallback'; const FALLBACK_FILE_NAME = 'fallback';
protected function parseSettings() {} protected function parseSettings()
{
}
/** /**
* Returns the directory name corresponding to the object type. * Returns the directory name corresponding to the object type.

View File

@ -12,5 +12,7 @@ class LayoutCode extends CodeBase
* This event is triggered after the layout components are executed, * This event is triggered after the layout components are executed,
* but before the page's onStart event. * but before the page's onStart event.
*/ */
public function onBeforePageStart() {} public function onBeforePageStart()
} {
}
}

View File

@ -9,4 +9,4 @@
class ObjectMemoryCache class ObjectMemoryCache
{ {
public static $cache = []; public static $cache = [];
} }

View File

@ -31,7 +31,9 @@ class Page extends CmsCompoundObject
]; ];
} }
protected function parseSettings() {} protected function parseSettings()
{
}
/** /**
* Returns the directory name corresponding to the object type. * Returns the directory name corresponding to the object type.
@ -59,8 +61,9 @@ class Page extends CmsCompoundObject
*/ */
public function getLayoutOptions() public function getLayoutOptions()
{ {
if (!($theme = Theme::getEditTheme())) if (!($theme = Theme::getEditTheme())) {
throw new ApplicationException(Lang::get('cms::lang.theme.edit.not_found')); throw new ApplicationException(Lang::get('cms::lang.theme.edit.not_found'));
}
$layouts = Layout::listInTheme($theme, true); $layouts = Layout::listInTheme($theme, true);
$result = []; $result = [];
@ -101,8 +104,9 @@ class Page extends CmsCompoundObject
* request processing. * request processing.
*/ */
$controller = Controller::getController(); $controller = Controller::getController();
if (!$controller) if (!$controller) {
$controller = new Controller; $controller = new Controller;
}
return $controller->pageUrl($page, $params, true, $absolute); return $controller->pageUrl($page, $params, true, $absolute);
} }
@ -119,8 +123,8 @@ class Page extends CmsCompoundObject
* false if omitted. * false if omitted.
* - dynamicItems - Boolean value indicating whether the item type could generate new menu items. * - dynamicItems - Boolean value indicating whether the item type could generate new menu items.
* Optional, false if omitted. * Optional, false if omitted.
* - cmsPages - a list of CMS pages (objects of the Cms\Classes\Page class), if the item type requires a CMS page reference to * - cmsPages - a list of CMS pages (objects of the Cms\Classes\Page class), if the item type requires
* resolve the item URL. * a CMS page reference to resolve the item URL.
* @param string $type Specifies the menu item type * @param string $type Specifies the menu item type
* @return array Returns an array * @return array Returns an array
*/ */
@ -132,8 +136,9 @@ class Page extends CmsCompoundObject
$theme = Theme::getActiveTheme(); $theme = Theme::getActiveTheme();
$pages = self::listInTheme($theme, true); $pages = self::listInTheme($theme, true);
foreach ($pages as $page) foreach ($pages as $page) {
$references[$page->getBaseFileName()] = $page->title; $references[$page->getBaseFileName()] = $page->title;
}
$result = [ $result = [
'references' => $references, 'references' => $references,
@ -167,8 +172,9 @@ class Page extends CmsCompoundObject
$result = null; $result = null;
if ($item->type == 'cms-page') { if ($item->type == 'cms-page') {
if (!$item->reference) if (!$item->reference) {
return; return;
}
$pageUrl = self::url($item->reference); $pageUrl = self::url($item->reference);

View File

@ -8,4 +8,4 @@
*/ */
class PageCode extends CodeBase class PageCode extends CodeBase
{ {
} }

View File

@ -18,7 +18,8 @@ use October\Rain\Router\Helper as RouterHelper;
* add the question mark after its name: * add the question mark after its name:
* <pre>/blog/post/:post_id?</pre> * <pre>/blog/post/:post_id?</pre>
* By default parameters in the middle of the URL are required, for example: * By default parameters in the middle of the URL are required, for example:
* <pre>/blog/:post_id?/comments - although the :post_id parameter is marked as optional, it will be processed as required.</pre> * <pre>/blog/:post_id?/comments - although the :post_id parameter is marked as optional,
* it will be processed as required.</pre>
* Optional parameters can have default values which are used as fallback values in case if the real * Optional parameters can have default values which are used as fallback values in case if the real
* parameter value is not presented in the URL. Default values cannot contain the pipe symbols and question marks. * parameter value is not presented in the URL. Default values cannot contain the pipe symbols and question marks.
* Specify the default value after the question mark: * Specify the default value after the question mark:
@ -74,16 +75,21 @@ class Router
$url = RouterHelper::normalizeUrl($url); $url = RouterHelper::normalizeUrl($url);
$apiResult = Event::fire('cms.router.beforeRoute', [$url], true); $apiResult = Event::fire('cms.router.beforeRoute', [$url], true);
if ($apiResult !== null) if ($apiResult !== null) {
return $apiResult; return $apiResult;
}
for ($pass = 1; $pass <= 2; $pass++) { for ($pass = 1; $pass <= 2; $pass++) {
$fileName = null; $fileName = null;
$urlList = []; $urlList = [];
$cacheable = Config::get('cms.enableRoutesCache') && in_array(Config::get('cache.driver'), ['apc', 'memcached', 'redis', 'array']); $cacheable = Config::get('cms.enableRoutesCache') && in_array(
if ($cacheable) Config::get('cache.driver'),
['apc', 'memcached', 'redis', 'array']
);
if ($cacheable) {
$fileName = $this->getCachedUrlFileName($url, $urlList); $fileName = $this->getCachedUrlFileName($url, $urlList);
}
/* /*
* Find the page by URL and cache the route * Find the page by URL and cache the route
@ -96,8 +102,9 @@ class Router
$fileName = $router->matchedRoute(); $fileName = $router->matchedRoute();
if ($cacheable) { if ($cacheable) {
if (!$urlList || !is_array($urlList)) if (!$urlList || !is_array($urlList)) {
$urlList = []; $urlList = [];
}
$urlList[$url] = $fileName; $urlList[$url] = $fileName;
@ -140,8 +147,9 @@ class Router
*/ */
public function findByFile($fileName, $parameters = []) public function findByFile($fileName, $parameters = [])
{ {
if (!strlen(File::extension($fileName))) if (!strlen(File::extension($fileName))) {
$fileName .= '.htm'; $fileName .= '.htm';
}
$router = $this->getRouterObject(); $router = $this->getRouterObject();
return $router->url($fileName, $parameters); return $router->url($fileName, $parameters);
@ -153,8 +161,9 @@ class Router
*/ */
protected function getRouterObject() protected function getRouterObject()
{ {
if (self::$routerObj !== null) if (self::$routerObj !== null) {
return self::$routerObj; return self::$routerObj;
}
/* /*
* Load up each route rule * Load up each route rule
@ -178,8 +187,9 @@ class Router
*/ */
protected function getUrlMap() protected function getUrlMap()
{ {
if (!count(self::$urlMap)) if (!count(self::$urlMap)) {
$this->loadUrlMap(); $this->loadUrlMap();
}
return self::$urlMap; return self::$urlMap;
} }
@ -196,10 +206,11 @@ class Router
$key = $this->getCacheKey('page-url-map'); $key = $this->getCacheKey('page-url-map');
$cacheable = Config::get('cms.enableRoutesCache'); $cacheable = Config::get('cms.enableRoutesCache');
if ($cacheable) if ($cacheable) {
$cached = Cache::get($key, false); $cached = Cache::get($key, false);
else } else {
$cached = false; $cached = false;
}
if (!$cached || ($unserialized = @unserialize($cached)) === false) { if (!$cached || ($unserialized = @unserialize($cached)) === false) {
/* /*
@ -208,15 +219,17 @@ class Router
$pages = $this->theme->listPages(); $pages = $this->theme->listPages();
$map = []; $map = [];
foreach ($pages as $page) { foreach ($pages as $page) {
if (!$page->url) if (!$page->url) {
continue; continue;
}
$map[] = ['file' => $page->getFileName(), 'pattern' => $page->url]; $map[] = ['file' => $page->getFileName(), 'pattern' => $page->url];
} }
self::$urlMap = $map; self::$urlMap = $map;
if ($cacheable) if ($cacheable) {
Cache::put($key, serialize($map), Config::get('cms.urlCacheTtl', 1)); Cache::put($key, serialize($map), Config::get('cms.urlCacheTtl', 1));
}
return false; return false;
} }
@ -259,8 +272,9 @@ class Router
*/ */
public function getParameter($name, $default = null) public function getParameter($name, $default = null)
{ {
if (isset($this->parameters[$name]) && !empty($this->parameters[$name])) if (isset($this->parameters[$name]) && !empty($this->parameters[$name])) {
return $this->parameters[$name]; return $this->parameters[$name];
}
return $default; return $default;
} }
@ -296,10 +310,11 @@ class Router
$urlList = Cache::get($key, false); $urlList = Cache::get($key, false);
if ($urlList && ($urlList = @unserialize($urlList)) && is_array($urlList)) { if ($urlList && ($urlList = @unserialize($urlList)) && is_array($urlList)) {
if (array_key_exists($url, $urlList)) if (array_key_exists($url, $urlList)) {
return $urlList[$url]; return $urlList[$url];
}
} }
return null; return null;
} }
} }

View File

@ -33,8 +33,9 @@ class SectionParser
{ {
$sections = preg_split('/^={2,}\s*/m', $content, -1); $sections = preg_split('/^={2,}\s*/m', $content, -1);
$count = count($sections); $count = count($sections);
foreach ($sections as &$section) foreach ($sections as &$section) {
$section = trim($section); $section = trim($section);
}
$result = [ $result = [
'settings' => [], 'settings' => [],
@ -51,13 +52,12 @@ class SectionParser
$result['code'] = preg_replace('/\?\>\s*$/', '', $result['code']); $result['code'] = preg_replace('/\?\>\s*$/', '', $result['code']);
$result['markup'] = $sections[2]; $result['markup'] = $sections[2];
} } elseif ($count == 2) {
elseif ($count == 2) {
$result['settings'] = parse_ini_string($sections[0], true); $result['settings'] = parse_ini_string($sections[0], true);
$result['markup'] = $sections[1]; $result['markup'] = $sections[1];
} } elseif ($count == 1) {
elseif ($count == 1)
$result['markup'] = $sections[0]; $result['markup'] = $sections[0];
}
return $result; return $result;
} }
@ -83,12 +83,10 @@ class SectionParser
$result['settings'] = self::adjustLinePosition($content); $result['settings'] = self::adjustLinePosition($content);
$result['code'] = self::calculateLinePosition($content); $result['code'] = self::calculateLinePosition($content);
$result['markup'] = self::calculateLinePosition($content, 2); $result['markup'] = self::calculateLinePosition($content, 2);
} } elseif ($count == 2) {
elseif ($count == 2) {
$result['settings'] = self::adjustLinePosition($content); $result['settings'] = self::adjustLinePosition($content);
$result['markup'] = self::calculateLinePosition($content); $result['markup'] = self::calculateLinePosition($content);
} } elseif ($count == 1) {
elseif ($count == 1) {
$result['markup'] = 1; $result['markup'] = 1;
} }
@ -106,11 +104,13 @@ class SectionParser
$count = 0; $count = 0;
$lines = explode(PHP_EOL, $content); $lines = explode(PHP_EOL, $content);
foreach ($lines as $number => $line) { foreach ($lines as $number => $line) {
if (trim($line) == self::SECTION_SEPARATOR) if (trim($line) == self::SECTION_SEPARATOR) {
$count++; $count++;
}
if ($count == $instance) if ($count == $instance) {
return static::adjustLinePosition($content, $number); return static::adjustLinePosition($content, $number);
}
} }
return null; return null;
@ -150,10 +150,11 @@ class SectionParser
} }
/* /*
* PHP namespaced line (use x;) * PHP namespaced line (use x;) {
* Don't increase the line count, it will be rewritten by Cms\Classes\CodeParser * Don't increase the line count, it will be rewritten by Cms\Classes\CodeParser
*/ */
if (preg_match_all('/(use\s+[a-z0-9_\\\\]+;\n?)/mi', $line) == 1) { if (preg_match_all('/(use\s+[a-z0-9_\\\\]+;
}\n?)/mi', $line) == 1) {
continue; continue;
} }
@ -163,4 +164,4 @@ class SectionParser
// Line 0 does not exist. // Line 0 does not exist.
return ++$startLine; return ++$startLine;
} }
} }

View File

@ -60,8 +60,9 @@ class Theme
*/ */
public function getPath($dirName = null) public function getPath($dirName = null)
{ {
if (!$dirName) if (!$dirName) {
$dirName = $this->getDirName(); $dirName = $this->getDirName();
}
return base_path().Config::get('cms.themesDir').'/'.$dirName; return base_path().Config::get('cms.themesDir').'/'.$dirName;
} }
@ -108,8 +109,9 @@ class Theme
*/ */
public static function getActiveTheme() public static function getActiveTheme()
{ {
if (self::$activeThemeCache !== false) if (self::$activeThemeCache !== false) {
return self::$activeThemeCache; return self::$activeThemeCache;
}
$activeTheme = Config::get('cms.activeTheme'); $activeTheme = Config::get('cms.activeTheme');
@ -119,21 +121,25 @@ class Theme
->pluck('value') ->pluck('value')
; ;
if ($dbResult !== null) if ($dbResult !== null) {
$activeTheme = $dbResult; $activeTheme = $dbResult;
}
} }
$apiResult = Event::fire('cms.activeTheme', [], true); $apiResult = Event::fire('cms.activeTheme', [], true);
if ($apiResult !== null) if ($apiResult !== null) {
$activeTheme = $apiResult; $activeTheme = $apiResult;
}
if (!strlen($activeTheme)) if (!strlen($activeTheme)) {
throw new SystemException(Lang::get('cms::lang.theme.active.not_set')); throw new SystemException(Lang::get('cms::lang.theme.active.not_set'));
}
$theme = new static; $theme = new static;
$theme->load($activeTheme); $theme->load($activeTheme);
if (!File::isDirectory($theme->getPath())) if (!File::isDirectory($theme->getPath())) {
return self::$activeThemeCache = null; return self::$activeThemeCache = null;
}
return self::$activeThemeCache = $theme; return self::$activeThemeCache = $theme;
} }
@ -160,24 +166,29 @@ class Theme
*/ */
public static function getEditTheme() public static function getEditTheme()
{ {
if (self::$editThemeCache !== false) if (self::$editThemeCache !== false) {
return self::$editThemeCache; return self::$editThemeCache;
}
$editTheme = Config::get('cms.editTheme'); $editTheme = Config::get('cms.editTheme');
if (!$editTheme) if (!$editTheme) {
$editTheme = static::getActiveTheme()->getDirName(); $editTheme = static::getActiveTheme()->getDirName();
}
$apiResult = Event::fire('cms.editTheme', [], true); $apiResult = Event::fire('cms.editTheme', [], true);
if ($apiResult !== null) if ($apiResult !== null) {
$editTheme = $apiResult; $editTheme = $apiResult;
}
if (!strlen($editTheme)) if (!strlen($editTheme)) {
throw new SystemException(Lang::get('cms::lang.theme.edit.not_set')); throw new SystemException(Lang::get('cms::lang.theme.edit.not_set'));
}
$theme = new static; $theme = new static;
$theme->load($editTheme); $theme->load($editTheme);
if (!File::isDirectory($theme->getPath())) if (!File::isDirectory($theme->getPath())) {
return self::$editThemeCache = null; return self::$editThemeCache = null;
}
return self::$editThemeCache = $theme; return self::$editThemeCache = $theme;
} }
@ -195,8 +206,9 @@ class Theme
$result = []; $result = [];
foreach ($it as $fileinfo) { foreach ($it as $fileinfo) {
if (!$fileinfo->isDir() || $fileinfo->isDot()) if (!$fileinfo->isDir() || $fileinfo->isDot()) {
continue; continue;
}
$theme = new static; $theme = new static;
$theme->load($fileinfo->getFilename()); $theme->load($fileinfo->getFilename());
@ -212,12 +224,14 @@ class Theme
*/ */
public function getConfig() public function getConfig()
{ {
if ($this->configCache !== null) if ($this->configCache !== null) {
return $this->configCache; return $this->configCache;
}
$path = $this->getPath().'/theme.yaml'; $path = $this->getPath().'/theme.yaml';
if (!File::exists($path)) if (!File::exists($path)) {
return $this->configCache = []; return $this->configCache = [];
}
return $this->configCache = Yaml::parseFile($path); return $this->configCache = Yaml::parseFile($path);
} }
@ -225,14 +239,16 @@ class Theme
/** /**
* Returns a value from the theme configuration file by its name. * Returns a value from the theme configuration file by its name.
* @param string $name Specifies the configuration parameter name. * @param string $name Specifies the configuration parameter name.
* @param mixed $default Specifies the default value to return in case if the parameter doesn't exist in the configuration file. * @param mixed $default Specifies the default value to return in case if the parameter
* doesn't exist in the configuration file.
* @return mixed Returns the parameter value or a default value * @return mixed Returns the parameter value or a default value
*/ */
public function getConfigValue($name, $default = null) public function getConfigValue($name, $default = null)
{ {
$config = $this->getConfig(); $config = $this->getConfig();
if (isset($config[$name])) if (isset($config[$name])) {
return $config[$name]; return $config[$name];
}
return $default; return $default;
} }
@ -246,8 +262,9 @@ class Theme
{ {
$previewPath = '/assets/images/theme-preview.png'; $previewPath = '/assets/images/theme-preview.png';
$path = $this->getPath().$previewPath; $path = $this->getPath().$previewPath;
if (!File::exists($path)) if (!File::exists($path)) {
return URL::asset('modules/cms/assets/images/default-theme-preview.png'); return URL::asset('modules/cms/assets/images/default-theme-preview.png');
}
return URL::asset('themes/'.$this->getDirName().$previewPath); return URL::asset('themes/'.$this->getDirName().$previewPath);
} }

View File

@ -26,4 +26,4 @@ class UnknownComponent extends ComponentBase
'description' => $this->errorMessage 'description' => $this->errorMessage
]; ];
} }
} }

View File

@ -28,8 +28,9 @@ class ViewBag extends ComponentBase
public function __call($method, $parameters) public function __call($method, $parameters)
{ {
if (array_key_exists($method, $this->properties) && !method_exists($this, $method)) if (array_key_exists($method, $this->properties) && !method_exists($this, $method)) {
return $this->properties[$method]; return $this->properties[$method];
}
return parent::__call($method, $parameters); return parent::__call($method, $parameters);
} }
@ -38,7 +39,7 @@ class ViewBag extends ComponentBase
{ {
$result = []; $result = [];
foreach ($this->properties as $name=>$value) { foreach ($this->properties as $name => $value) {
$result[$name] = [ $result[$name] = [
'title' => $name, 'title' => $name,
'type' => 'string' 'type' => 'string'
@ -47,4 +48,4 @@ class ViewBag extends ComponentBase
return $result; return $result;
} }
} }