Page MenuHomeCode

No OneTemporary

diff --git a/Engines/Settings/Setting.php b/Engines/Settings/Setting.php
new file mode 100644
index 0000000..e18ded0
--- /dev/null
+++ b/Engines/Settings/Setting.php
@@ -0,0 +1,297 @@
+<?php
+
+/**
+ * Settings: an individual setting class
+ *
+ * Zed. The immensity of stars. The HyperShip. The people.
+ *
+ * (c) 2010, 2023, Dereckson, some rights reserved.
+ * Released under BSD license.
+ *
+ * @package Zed
+ * @subpackage Settings
+ * @author Sébastien Santoro aka Dereckson <dereckson@espace-win.org>
+ * @copyright 2010, 2023 Sébastien Santoro aka Dereckson
+ * @license http://www.opensource.org/licenses/bsd-license.php BSD
+ * @version 0.1
+ * @link http://scherzo.dereckson.be/doc/zed
+ * @link http://zed.dereckson.be/
+ * @filesource
+ */
+
+namespace Zed\Engines\Settings;
+
+use SimpleXMLElement;
+
+/**
+ * Setting class
+ *
+ * This class maps the <setting> XML block, from our settings XML format
+ */
+class Setting {
+
+ /**
+ * @var string The setting unique identifier
+ */
+ public string $id;
+
+ ///
+ /// Rendering settings
+ /// How the setting should be offered in presentation layer?
+ ///
+
+ /**
+ * @var string The setting unique key for rendering layer
+ *
+ * This identified is used to populate `id` and `name` attributes of
+ * the form control. They also allow to find language label to print
+ * the name of the setting in HTML form.
+ */
+ public string $key = "";
+
+ /**
+ * @var string The kind of field to use (e.g. "text", "validationText")
+ */
+ public string $field = "";
+
+ /**
+ * @var string If set, a regexp to validate the setting format
+ */
+ public string $regExp = "";
+
+ /**
+ * A list of choices. This list will be offered as a dropdown menu values.
+ *
+ * @var string[]
+ */
+ public array $choices = [];
+
+ ///
+ /// Mapping setting <> object property
+ /// What those settings control?
+ ///
+
+ /**
+ * @var string The name of the object this property belongs to.
+ *
+ * This is mainly used for get/set variables.
+ *
+ * @todo Create a targets property in Settings,
+ * then use $settings->targets[$this->object]
+ */
+ public string $object = "";
+
+ /**
+ * @var string The name of the property in the object
+ */
+ private string $property = "";
+
+ /**
+ * @var string The name of the method to call in the object
+ * when the setting is set.
+ *
+ * When left blank, it uses $handler, or if blank too, $property.
+ */
+ private string $method = "";
+
+ /**
+ * @var string[] The custom PHP code to run to get or set the property.
+ *
+ * Array valid keys are "get" and "set". It is acceptable to only use one,
+ * or to use both.
+ *
+ * This requires the use of `eval` and makes methods usage difficult to
+ * track. As such, it is now recommended to add a method to your model
+ * object, and write that method name in $method.
+ *
+ * @deprecated Create a method to your target object and use $this->method
+ */
+ private array $handler = [];
+
+ ///
+ /// Error management
+ ///
+
+ /**
+ * @var string If set, contains an error message
+ */
+ public string $lastError = "";
+
+ ///
+ /// Constructor
+ ///
+
+ public function __construct (string $id) {
+ $this->id = $id;
+ }
+
+ ///
+ /// Get and set property value
+ ///
+
+ function get () : string {
+ // Case 1 — Evaluates custom handler
+ if (array_key_exists('get', $this->handler)) {
+ return eval($this->handler['get']);
+ }
+
+ // Case 2 — Gets object property
+ if ($this->hasKnownObjectProperty()) {
+ return $this->getObjectProperty();
+ }
+
+ // Passwords can be left blank
+ if ($this->field === "password") {
+ //Okay not to have a value for password fields
+ return "";
+ }
+
+ // At this point, there is a configuration error in <setting> block.
+ throw new SettingException(<<<EOF
+Setting $this->key haven't any get indication.
+
+Please set <object> and <property> tags, and <method> as needed.
+EOF
+ );
+ }
+
+ function set(string $value) : bool {
+ // Validates data
+ if (!$this->hasValidFormat($value)) {
+ $this->lastError = "Invalid format for $this->key setting";
+ return false;
+ }
+
+ // Tries to set value
+
+ // Case 1 — uses custom handler code [deprecated]
+ if (array_key_exists('set', $this->handler)) {
+ return eval($this->handler['set']);
+ }
+
+ if ($this->object !== "") {
+ // Case 2 — calls a specific method
+ if ($this->method !== "") {
+ return $this->callSetMethod($value);
+ }
+
+ // Case 3 — sets directly the property
+ if ($this->property !== "") {
+ $this->setObjectProperty($value);
+ return true;
+ }
+ }
+
+ // At this point, there is a configuration error in <setting> block.
+ throw new SettingException(<<<EOF
+Setting $this->key haven't any set indication.
+
+Please set <object>, <property>, and, if needed, <method>.
+EOF
+);
+ }
+
+ private function hasValidFormat(string $value) : bool {
+ return $this->regExp === "" || preg_match("/^" . $this->regExp . '$/', $value);
+ }
+
+ private function hasKnownObjectProperty () : bool {
+ return $this->object !== "" && $this->property !== "";
+ }
+
+ ///
+ /// Helper methods to interact with underlying object
+ ///
+
+ /**
+ * @todo Consume $this->object from the engine targets, not from the globals
+ */
+ private function getUnderlyingObject() : object {
+ if ($this->object === "") {
+ throw new SettingException(
+ "Underlying object isn't specified. Set <object> tag."
+ );
+ }
+
+ return $GLOBALS[$this->object];
+ }
+
+ private function getObjectProperty () : string {
+ $object = $this->getUnderlyingObject();
+ $property = $this->property;
+
+ return $object->$property;
+ }
+
+ private function setObjectProperty (string $value) : void {
+ $object = $this->getUnderlyingObject();
+ $property = $this->property;
+
+ $object->$property = $value;
+ }
+
+ private function callSetMethod (string $value) : bool {
+ $object = $this->getUnderlyingObject();
+
+ return call_user_func([$object, $this->method], $value);
+ }
+
+ ///
+ /// XML deserialization
+ ///
+
+ /**
+ * Initializes a new instance of Setting class from a XML element
+ */
+ static function fromXml(SimpleXMLElement $xml) : self {
+ //Reads attributes
+ $id = '';
+ foreach ($xml->attributes() as $key => $value) {
+ switch ($key) {
+ case 'id':
+ $id = (string)$value;
+ break;
+
+ default:
+ message_die(GENERAL_ERROR, "Unknown attribute: $key = \"$value\"", "Settings error");
+ }
+ }
+
+ //id attribute is mandatory
+ if (!$id) {
+ message_die(GENERAL_ERROR, "Setting without id. Please add id='' in <setting> tag", "Settings error");
+ }
+
+ //Initializes new Setting instance
+ $setting = new Setting($id);
+
+ //Parses simple <tag>value</tag>
+ $properties = ['key', 'field', 'object', 'property', 'method', 'regExp'];
+ foreach ($properties as $property) {
+ if ($xml->$property) {
+ $setting->$property = (string)$xml->$property;
+ }
+ }
+
+ //Parses <handler>
+ $setting->handler = [];
+ if ($xml->handler) {
+ trigger_error("The setting $id still uses a handler. Move this code to the related object and switch here to method.", E_USER_DEPRECATED);
+ if ($xml->handler->get) {
+ $setting->handler['get'] = (string)$xml->handler->get;
+ }
+ if ($xml->handler->set) {
+ $setting->handler['set'] = (string)$xml->handler->set;
+ }
+ }
+
+ //Parses <choices>
+ if ($xml->choices) {
+ foreach ($xml->choices->choice as $choiceXml) {
+ $setting->choices[(string)$choiceXml->key] = (string)$choiceXml->value;
+ }
+ }
+
+ return $setting;
+ }
+}
diff --git a/Engines/Settings/SettingException.php b/Engines/Settings/SettingException.php
new file mode 100644
index 0000000..ce4fe56
--- /dev/null
+++ b/Engines/Settings/SettingException.php
@@ -0,0 +1,9 @@
+<?php
+
+namespace Zed\Engines\Settings;
+
+use LogicException;
+
+class SettingException extends LogicException {
+
+}
diff --git a/includes/settings/settings.php b/Engines/Settings/Settings.php
similarity index 60%
rename from includes/settings/settings.php
rename to Engines/Settings/Settings.php
index 5cede5a..be39f82 100644
--- a/includes/settings/settings.php
+++ b/Engines/Settings/Settings.php
@@ -1,83 +1,101 @@
<?php
/**
* Settings
*
* Zed. The immensity of stars. The HyperShip. The people.
*
* (c) 2010, Dereckson, some rights reserved.
* Released under BSD license.
*
* @package Zed
* @subpackage Settings
* @author Sébastien Santoro aka Dereckson <dereckson@espace-win.org>
* @copyright 2010 Sébastien Santoro aka Dereckson
* @license http://www.opensource.org/licenses/bsd-license.php BSD
* @version 0.1
* @link http://scherzo.dereckson.be/doc/zed
* @link http://zed.dereckson.be/
* @filesource
*/
-/**
- * The method to call in your objects, to save data.
- */
-define("SETTINGS_SAVE_METHOD", "save_to_database");
-
-require_once("page.php");
+namespace Zed\Engines\Settings;
/**
* Settings
*
* This class maps the Settings format (preferences.xml)
*
* It allows to generate settings web forms and handle replies, from a
* XML document.
*/
class Settings {
+ ///
+ /// Constants
+ ///
+
/**
- * The file path
+ * The method to call in your objects, to save data.
*
- * @var string
+ * @const string
*/
- public $file;
+ const SETTINGS_SAVE_METHOD = "save_to_database";
+
+ ///
+ /// Properties
+ ///
+
+ public string $filePath;
/**
* A collection of SettingsPage items
*
* @var SettingsPage[]
*/
- public $pages;
+ public array $pages = [];
/**
- * Initializes a new instance of Settings class
+ * The targets are underlying objects manipulated by the settings
*
- * @param string $xmlFile The XML document which defines the settings.
+ * @var object[]
*/
- function __construct ($xmlFile) {
+ public array $targets = [];
+
+ ///
+ /// Constructor
+ ///
+
+ /**
+ * Initializes a new instance of Settings class
+ */
+ function __construct (string $xmlFilePath) {
//Opens .xml
- if (!file_exists($xmlFile)) {
- message_die(GENERAL_ERROR, "$xmlFile not found.", "Settings load error");
+ if (!file_exists($xmlFilePath)) {
+ throw new SettingException("$xmlFilePath not found.");
}
- $this->file = $xmlFile;
+ $this->filePath = $xmlFilePath;
//Parses it
$this->parse();
}
+ ///
+ /// XML deserialization
+ ///
+
/**
* Parses XML file
*/
function parse () {
//Parses it
- $xml = simplexml_load_file($this->file);
+ $xml = simplexml_load_file($this->filePath);
foreach ($xml->page as $page) {
//Gets page
- $page = SettingsPage::from_xml($page);
+ $page = SettingsPage::fromXml($page);
//Adds to sections array
$this->pages[$page->id] = $page;
}
}
}
diff --git a/includes/settings/page.php b/Engines/Settings/SettingsPage.php
similarity index 65%
rename from includes/settings/page.php
rename to Engines/Settings/SettingsPage.php
index 8ca9d18..071f09c 100644
--- a/includes/settings/page.php
+++ b/Engines/Settings/SettingsPage.php
@@ -1,162 +1,169 @@
<?php
/**
* Settings: a settings page class
*
* Zed. The immensity of stars. The HyperShip. The people.
*
* (c) 2010, Dereckson, some rights reserved.
* Released under BSD license.
*
* @package Zed
* @subpackage Settings
* @author Sébastien Santoro aka Dereckson <dereckson@espace-win.org>
* @copyright 2010 Sébastien Santoro aka Dereckson
* @license http://www.opensource.org/licenses/bsd-license.php BSD
* @version 0.1
* @link http://scherzo.dereckson.be/doc/zed
* @link http://zed.dereckson.be/
* @filesource
*/
-require_once("setting.php");
+namespace Zed\Engines\Settings;
+
+use SimpleXMLElement;
/**
* This class maps the page XML element, from our Settings XML schema
*
* <page id="account" title="Account">
* <setting ...>
* ...
* </setting>
* <setting ...>
* ...
* </setting>
* <page>
*
* It provides method to print a form built from this page and to handle form.
*/
class SettingsPage {
+
/**
* The page ID
*
- * This property maps the id attribute from the page XML tag
- *
- * @var string the page ID
+ * This property maps the id attribute from the page XML tag.
*/
- public $id;
+ public string $id;
/**
* The page's title
*
- * This property maps the title attribute from the page XML tag
- *
- * @var string the page title
+ * This property maps the title attribute from the page XML tag.
*/
- public $title;
+ public string $title;
/**
* The settings
*
* This property is an array of Setting items and maps the <setting> tags
- * @var Array
+ * @var Setting[]
*/
- public $settings = [];
+ public array $settings = [];
/**
* Initializes a new instance of SettingsPage class
- *
- * @param string $id the page ID
*/
- function __construct ($id) {
+ function __construct(string $id) {
$this->id = $id;
}
/**
* Initializes a settings page from an SimpleXMLElement XML fragment
- *
- * @param SimpleXMLElement $xml the XML fragment
- * @return SettingsPage the section instance
*/
- static function from_xml ($xml) {
- //Reads attributes
+ static function fromXml(SimpleXMLElement $xml) : SettingsPage {
+ // Reads attributes
$id = '';
$title = '';
foreach ($xml->attributes() as $key => $value) {
switch ($key) {
case 'title':
+ $title = (string)$value;
+ break;
+
case 'id':
- $$key = (string)$value;
+ $id = (string)$value;
break;
default:
message_die(GENERAL_ERROR, "Unknown attribute: $key = \"$value\"", "Settings error");
}
}
- //id attribute is mandatory
- if (!$id) {
+ // Ensure id attribute is defined
+ if ($id === "") {
message_die(GENERAL_ERROR, "Section without id. Please add id='' in <section> tag", "Story error");
}
//Initializes new SettingsPage instance
$page = new SettingsPage($id);
$page->title = $title;
//Gets settings
if ($xml->setting) {
foreach ($xml->setting as $settingXml) {
- $setting = Setting::from_xml($settingXml);
+ $setting = Setting::fromXml($settingXml);
$page->settings[$setting->key] = $setting;
}
}
return $page;
}
/**
* Handles form reading $_POST array, set new settings values and saves.
*
- * @param array $errors an array where the errors will be filled
+ * @param string[] $errors an array where the errors will be filled
* @return boolean true if there isn't error ; otherwise, false.
*/
- function handle_form (array &$errors = []) : bool {
+ function handleForm(array &$errors = []) : bool {
$objects = [];
$result = true;
- //Sets new settings values
+ // Sets new settings values, and records objects to save
foreach ($this->settings as $setting) {
$value = $_POST[$setting->key] ?? "";
- if ($setting->field == "password" && !$value) {
- //We don't erase passwords if not set
+ if ($setting->field === "password" && $value === "") {
+ // We don't erase passwords if not set
continue;
}
- //If the setting value is different of current one, we update it
+ // If the setting value is different of current one, we update it
$currentValue = $setting->get();
- if ($setting->field == "checkbox" || $currentValue != $value) {
+ if ($setting->field === "checkbox" || $currentValue !== $value) {
if (!$setting->set($value)) {
$errors[] = $setting->lastError ?? "An error have occurred in $setting->key field.";
$result = false;
}
+
if ($setting->object) {
$objects[] = $setting->object;
}
}
}
- //Saves object (when the SETTINGS_SAVE_METHOD save method exists)
- if (count($objects)) {
- $objects = array_unique($objects);
- foreach ($objects as $object) {
- $object = $GLOBALS[$object];
- if (method_exists($object, SETTINGS_SAVE_METHOD)) {
- call_user_func([$object, SETTINGS_SAVE_METHOD]);
- }
+ $this->saveObjects($objects);
+
+ return $result;
+ }
+
+ /**
+ * @param string[] $objects
+ */
+ private function saveObjects (array $objects) : void {
+ $objects = array_unique($objects);
+
+ foreach ($objects as $objectName) {
+ $object = $this->getUnderlyingObject($objectName);
+ if (method_exists($object, Settings::SETTINGS_SAVE_METHOD)) {
+ call_user_func([$object, Settings::SETTINGS_SAVE_METHOD]);
}
}
+ }
- return $result;
+ private function getUnderlyingObject (string $object) : object {
+ return $GLOBALS[$object];
}
}
diff --git a/controllers/settings.php b/controllers/settings.php
index 7b96b1e..5a86acb 100644
--- a/controllers/settings.php
+++ b/controllers/settings.php
@@ -1,96 +1,110 @@
<?php
/**
* Settings
*
* Zed. The immensity of stars. The HyperShip. The people.
*
* (c) 2010, Dereckson, some rights reserved.
* Released under BSD license.
*
* This controller allows user to set its preferences, according the Settings
* classes and the preferences.xml document.
*
* It handles the /settings URL.
*
* @package Zed
* @subpackage Controllers
* @author Sébastien Santoro aka Dereckson <dereckson@espace-win.org>
* @copyright 2010 Sébastien Santoro aka Dereckson
* @license http://www.opensource.org/licenses/bsd-license.php BSD
* @version 0.1
* @link http://scherzo.dereckson.be/doc/zed
* @link http://zed.dereckson.be/
* @filesource
*
* @todo replace the on the fly preferences.xml code generation by a cached code generation
* @todo reduce the number of for loops in this controller
*/
+use Zed\Engines\Settings\SettingException;
+use Zed\Engines\Settings\Settings;
+
//
// Loads settings
//
lang_load('settings.conf');
-include('includes/settings/settings.php');
-$settings = new Settings('includes/settings/preferences.xml');
+try {
+ $settings = new Settings('data/settings/preferences.xml');
+ $settings->targets = [
+ "CurrentPerso" => $CurrentPerso,
+ "CurrentUser" => $CurrentUser,
+ ];
+} catch (SettingException $ex) {
+ message_die(GENERAL_ERROR, $ex->getMessage(), "Settings error");
+}
+
+///
+/// Handle request
+///
//Selects relevant settings page
$pages = $settings->pages;
if (count($url) > 1) {
//From url: /settings/account -> page account
if (array_key_exists($url[1], $settings->pages)) {
$page = $pages[$url[1]];
} else {
message_die(GENERAL_ERROR, "/settings/$url[1] isn't a valid setting page");
}
} else {
//Default page
$page = array_shift($pages);
}
//Pages links
foreach ($settings->pages as $tmppage) {
$pagesLinks[$tmppage->id] = $tmppage->title;
}
//
// Handles form
//
if (array_key_exists('settings_page', $_POST)) {
if ($_POST['settings_page'] == $page->id) {
//Updates settings
$errors = [];
- $page->handle_form($errors);
+ $page->handleForm($errors);
if (count($errors)) {
//Prints error message
$smarty->assign('WAP', implode('<br />', $errors));
}
} else {
//The field settings.page isn't the current page
//Prints an HACK_ERROR to avoid to save properties with the same names.
$id_current = $page->id;
$id_toSave = $_POST['settings_page'];
message_die(HACK_ERROR, "You're on /settings/$id_current but you want to update /settings/$id_toSave");
}
}
//
// HTML output
//
//Serves header
define('DIJIT', true);
$title = lang_get('Settings');
$smarty->assign('PAGE_TITLE', $title);
include('header.php');
//Serves settings page;
$smarty->assign('page', $page);
$smarty->assign('pages', $pagesLinks);
$smarty->display('settings_page.tpl');
//Servers footer
$smarty->assign('screen', "Settings > " . $page->title);
include('footer.php');
diff --git a/includes/settings/preferences.xml b/data/settings/preferences.xml
similarity index 100%
rename from includes/settings/preferences.xml
rename to data/settings/preferences.xml
diff --git a/includes/autoload.php b/includes/autoload.php
index 3aca728..73c392c 100644
--- a/includes/autoload.php
+++ b/includes/autoload.php
@@ -1,57 +1,53 @@
<?php
spl_autoload_register(function (string $className) {
//Classes
$classes['IAuthentication'] = './includes/auth/IAuthentication.php';
$classes['UserPasswordAuthentication'] = './includes/auth/UserPasswordAuthentication.php';
$classes['YubiCloudAuthentication'] = './includes/auth/YubiCloudAuthentication.php';
$classes['Cache'] = './includes/cache/cache.php';
$classes['CacheMemcached'] = './includes/cache/memcached.php';
$classes['CacheVoid'] = './includes/cache/void.php';
$classes['ContentFile'] = './includes/content/file.php';
$classes['ContentLocation'] = './includes/content/location.php';
$classes['ContentZone'] = './includes/content/zone.php';
$classes['GeoBody'] = './includes/geo/body.php';
$classes['GeoGalaxy'] = './includes/geo/galaxy.php';
$classes['GeoLocation'] = './includes/geo/location.php';
$classes['GeoPlace'] = './includes/geo/place.php';
$classes['GeoScene'] = './includes/geo/scene.php';
$classes['GeoSceneIndex'] = './includes/geo/sceneindex.php';
$classes['Application'] = './includes/objects/application.php';
$classes['Content'] = './includes/objects/content.php';
$classes['Invite'] = './includes/objects/invite.php';
$classes['Message'] = './includes/objects/message.php';
$classes['MOTD'] = './includes/objects/motd.php';
$classes['Perso'] = './includes/objects/perso.php';
$classes['Port'] = './includes/objects/port.php';
$classes['Profile'] = './includes/objects/profile.php';
$classes['ProfileComment'] = './includes/objects/profilecomment.php';
$classes['ProfilePhoto'] = './includes/objects/profilephoto.php';
$classes['Request'] = './includes/objects/request.php';
$classes['RequestReply'] = './includes/objects/requestreply.php';
$classes['Ship'] = './includes/objects/ship.php';
$classes['User'] = './includes/objects/user.php';
- $classes['SettingsPage'] = './includes/settings/page.php';
- $classes['Setting'] = './includes/settings/setting.php';
- $classes['Settings'] = './includes/settings/settings.php';
-
$classes['StoryChoice'] = './includes/story/choice.php';
$classes['StoryHook'] = './includes/story/hook.php';
$classes['DemoStoryHook'] = './includes/story/hook_demo.php';
$classes['SpatioportStoryHook'] = './includes/story/hook_spatioport.php';
$classes['StorySection'] = './includes/story/section.php';
$classes['Story'] = './includes/story/story.php';
$classes['TravelPlace'] = './includes/travel/place.php';
$classes['Travel'] = './includes/travel/travel.php';
//Loader
if (array_key_exists($className, $classes)) {
require_once($classes[$className]);
}
});
diff --git a/includes/settings/setting.php b/includes/settings/setting.php
deleted file mode 100644
index 76cf318..0000000
--- a/includes/settings/setting.php
+++ /dev/null
@@ -1,179 +0,0 @@
-<?php
-
-/**
- * Settings: an individual setting class
- *
- * Zed. The immensity of stars. The HyperShip. The people.
- *
- * (c) 2010, Dereckson, some rights reserved.
- * Released under BSD license.
- *
- * @package Zed
- * @subpackage Settings
- * @author Sébastien Santoro aka Dereckson <dereckson@espace-win.org>
- * @copyright 2010 Sébastien Santoro aka Dereckson
- * @license http://www.opensource.org/licenses/bsd-license.php BSD
- * @version 0.1
- * @link http://scherzo.dereckson.be/doc/zed
- * @link http://zed.dereckson.be/
- * @filesource
- */
-
-/**
- * Setting class
- *
- * This class map the <setting> XML block, from our settings XML format
- */
-class Setting {
-
- public $key;
-
- //Rendering variables
- public $field;
- public $regExp;
- public $choices;
-
- //get/set variables
- public $object;
- private $property;
- private $method;
- private $handler;
-
- //error variable
- public $lastError;
-
- /**
- * Gets the current setting value
- *
- * @return string the setting value
- */
- function get () {
- //1 - Evaluates custom handler
- if (array_key_exists('get', $this->handler)) {
- return eval($this->handler['get']);
- }
-
- //2 - Gets object property
- if ($this->object && $property = $this->property) {
- return $GLOBALS[$this->object]->$property;
- }
-
- if ($this->field == "password") {
- //Okay not to have a value for password fields
- return;
- }
-
- message_die(GENERAL_ERROR, "Setting $this->key haven't any get indication. Please set <object> and <property> / or a custom <handler><get></get></handler> block.", "Settings error");
- }
-
- /**
- * Sets a new value
- *
- * @param $value the setting new value
- * @return boolean true if the setting have been successfully set ; otherwise, false.
- */
- function set ($value) {
- //Validates data
- if ($this->regExp) {
- if (!preg_match('/^' . $this->regExp . '$/', $value)) {
- $this->lastError = "Invalid format for $this->key setting";
- return false;
- }
- }
-
- //Tries to set value
-
- //1 - Evaluates custom handler
- if (array_key_exists('set', $this->handler)) {
- return eval($this->handler['set']);
- }
-
- //2 - Calls object method
- //3 - Sets object property
- if ($this->object) {
- $object = $GLOBALS[$this->object];
- if ($this->method) {
- return call_user_func([$object, $this->method], $value);
- } elseif ($property = $this->property) {
- $object->$property = $value;
- return true;
- }
- }
-
- message_die(GENERAL_ERROR, "Setting $this->key haven't any set indication. Please set <object> (and whether <method>, whether <property>) or a custom <handler><set></set></handler> block.", "Settings error");
- }
-
- /**
- * Saves setting
- *
- * @return mixed the SETTINGS_SAVE_METHOD method value, or false if there's no method call;
- */
- function save () {
- if ($this->object) {
- $object = $GLOBALS[$this->object];
- if (method_exists($object, SETTINGS_SAVE_METHOD)) {
- return call_user_func([$object, SETTINGS_SAVE_METHOD]);
- }
- }
-
- return false;
- }
-
- /**
- * Initializes a new instance of Setting class from a XML element
- *
- * @param SimpleXMLElement the xml element to parse
- * @return Setting the setting class
- */
- static function from_xml ($xml) {
- //Reads attributes
- $id = '';
- foreach ($xml->attributes() as $key => $value) {
- switch ($key) {
- case 'id':
- $id = (string)$value;
- break;
-
- default:
- message_die(GENERAL_ERROR, "Unknown attribute: $key = \"$value\"", "Settings error");
- }
- }
-
- //id attribute is mandatory
- if (!$id) {
- message_die(GENERAL_ERROR, "Setting without id. Please add id='' in <setting> tag", "Settings error");
- }
-
- //Initializes new Setting instance
- $setting = new Setting($id);
-
- //Parses simple <tag>value</tag>
- $properties = ['key', 'field', 'object', 'property', 'method', 'regExp'];
- foreach ($properties as $property) {
- if ($xml->$property) {
- $setting->$property = (string)$xml->$property;
- }
- }
-
- //Parses <handler>
- $setting->handler = [];
- if ($xml->handler) {
- if ($xml->handler->get) {
- $setting->handler['get'] = (string)$xml->handler->get;
- }
- if ($xml->handler->set) {
- $setting->handler['set'] = (string)$xml->handler->set;
- }
- }
-
- //Parses <choices>
- if ($xml->choices) {
- foreach ($xml->choices->choice as $choiceXml) {
- $setting->choices[(string)$choiceXml->key] = (string)$choiceXml->value;
-
- }
- }
-
- return $setting;
- }
-}

File Metadata

Mime Type
text/x-diff
Expires
Sat, Nov 23, 09:00 (1 d, 16 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
20766
Default Alt Text
(30 KB)

Event Timeline