<?php
namespace Moto; use Moto; use Zend\Stdlib\ArrayUtils; class Features { const _SETTINGS_KEY = 'features'; protected static $_engineFeatures = array(); protected static $_features = array(); protected static $_defaults = array(); protected static $_initialized = false; public static function init() { if (static::$_initialized) { return; } static::$_initialized = true; static::_initDefault(); static::_loadFeatures(); if (method_exists('Moto\System\Settings', 'exportFeatures')) { static::$_engineFeatures = Moto\System\Settings::exportFeatures(); } } protected static function _initDefault() { $features = Moto\Config::get('__features__', array()); if (is_array($features)) { static::$_defaults = $features; } } protected static function _loadFeatures() { $features = Moto\Website\Settings::get(static::_SETTINGS_KEY); if (is_string($features)) { $features = json_decode($features, true); } if (is_array($features)) { static::$_features = $features; } } public static function getList() { $features = static::$_defaults; $features = array_merge($features, static::$_features); $features = array_merge($features, static::$_engineFeatures); return $features; } public static function _sanitizeName($name) { if ($name === 'allow_code_injection') { return 'engine_code_injection'; } return $name; } public static function isEnabled($name, $default = false) { $name = static::_sanitizeName($name); if (array_key_exists($name, static::$_engineFeatures)) { return static::$_engineFeatures[$name]; } if (array_key_exists($name, static::$_features)) { return static::$_features[$name]; } if (array_key_exists($name, static::$_defaults)) { return static::$_defaults[$name]; } return $default; } public static function disableByMask($mask) { $mask .= '_'; foreach (static::$_features as $name => $value) { if (strpos($name, $mask) === 0) { unset(static::$_features[$name]); } } static::_saveFeatures(); } protected static function _saveFeatures() { Moto\Website\Settings::set(static::_SETTINGS_KEY, static::$_features); } public static function enable($name, $value = true) { $name = static::_sanitizeName($name); if (is_string($name)) { static::$_features[$name] = $value; } elseif (is_array($name) || is_object($name)) { foreach ($name as $_name => $_value) { if (is_string($_name)) { static::$_features[$_name] = $_value; } elseif (is_int($_name)) { static::$_features[$_value] = $value; } } } static::_saveFeatures(); } public static function disable($name) { $name = static::_sanitizeName($name); if (is_string($name)) { unset(static::$_features[$name]); } elseif (is_array($name) || is_object($name)) { foreach ($name as $_name => $_value) { if (is_string($_name)) { unset(static::$_features[$_name]); } elseif (is_int($_name)) { unset(static::$_features[$_value]); } } } static::_saveFeatures(); } }