2014-11-27 14:30:49 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2017-10-06 21:42:39 +00:00
|
|
|
----------------------------------------------------------------------
|
|
|
|
LICENSE
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License (GPL)
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
To read the license please visit http://www.gnu.org/copyleft/gpl.html
|
|
|
|
----------------------------------------------------------------------
|
|
|
|
*/
|
2014-11-27 14:30:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Persistent variable table
|
|
|
|
*
|
|
|
|
* @author Drupal Developpement Team
|
|
|
|
* @link http://cvs.drupal.org/viewcvs/drupal/drupal/includes/bootstrap.inc?rev=1.38&view=auto
|
|
|
|
*/
|
|
|
|
|
2017-10-06 21:42:39 +00:00
|
|
|
|
2014-11-27 14:30:49 +00:00
|
|
|
/**
|
|
|
|
* Load the persistent variable table.
|
|
|
|
*
|
|
|
|
* The variable table is composed of values that have been saved in the table
|
|
|
|
* with variable_set() as well as those explicitly specified in the configuration
|
|
|
|
* file.
|
|
|
|
*/
|
|
|
|
function variable_init($conf = array()) {
|
2015-09-24 22:01:04 +00:00
|
|
|
global $db;
|
|
|
|
$result = $db->query('SELECT * FROM `variable`');
|
2016-03-13 12:23:11 +00:00
|
|
|
$variables=array();
|
2015-09-24 22:01:04 +00:00
|
|
|
while ($db->next_record($result)) {
|
|
|
|
/* maybe the data is *not* serialized, in that case, take it verbatim */
|
|
|
|
$variable = $db->Record;
|
|
|
|
if (($variables[$variable['name']] = @unserialize($variable['value'])) === FALSE) {
|
|
|
|
$variables[$variable['name']] = $variable['value'];
|
|
|
|
}
|
2014-11-27 14:30:49 +00:00
|
|
|
}
|
|
|
|
|
2015-09-24 22:01:04 +00:00
|
|
|
foreach ($conf as $name => $value) {
|
|
|
|
$variables[$name] = $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $variables;
|
2014-11-27 14:30:49 +00:00
|
|
|
}
|
|
|
|
|
2017-10-06 21:42:39 +00:00
|
|
|
|
2014-11-27 14:30:49 +00:00
|
|
|
/**
|
|
|
|
* Initialize the global $conf array if necessary
|
|
|
|
*
|
|
|
|
* @global $conf the global conf array
|
|
|
|
* @uses variable_init()
|
|
|
|
*/
|
|
|
|
function variable_init_maybe() {
|
2015-09-24 22:01:04 +00:00
|
|
|
global $conf;
|
|
|
|
if (!isset($conf)) {
|
|
|
|
$conf = variable_init();
|
|
|
|
}
|
2014-11-27 14:30:49 +00:00
|
|
|
}
|
|
|
|
|
2017-10-06 21:42:39 +00:00
|
|
|
|
2014-11-27 14:30:49 +00:00
|
|
|
/**
|
|
|
|
* Return a persistent variable.
|
|
|
|
*
|
|
|
|
* @param $name
|
|
|
|
* The name of the variable to return.
|
|
|
|
* @param $default
|
|
|
|
* The default value to use if this variable has never been set.
|
|
|
|
* @param $createit_comment
|
|
|
|
* If variable doesn't exist, create it with the default value
|
|
|
|
* and createit_comment value as comment
|
|
|
|
* @return
|
|
|
|
* The value of the variable.
|
|
|
|
* @global $conf
|
|
|
|
* A cache of the configuration.
|
|
|
|
*/
|
|
|
|
function variable_get($name, $default = null, $createit_comment = null) {
|
2015-09-24 22:01:04 +00:00
|
|
|
global $conf;
|
2014-11-27 14:30:49 +00:00
|
|
|
|
2015-09-24 22:01:04 +00:00
|
|
|
variable_init_maybe();
|
2014-11-27 14:30:49 +00:00
|
|
|
|
2015-09-24 22:01:04 +00:00
|
|
|
if (isset($conf[$name])) {
|
|
|
|
return $conf[$name];
|
|
|
|
} elseif (!is_null($createit_comment)) {
|
|
|
|
variable_set($name, $default, $createit_comment);
|
|
|
|
}
|
|
|
|
return $default;
|
2014-11-27 14:30:49 +00:00
|
|
|
}
|
|
|
|
|
2017-10-06 21:42:39 +00:00
|
|
|
|
2014-11-27 14:30:49 +00:00
|
|
|
/**
|
|
|
|
* Set a persistent variable.
|
|
|
|
*
|
|
|
|
* @param $name
|
|
|
|
* The name of the variable to set.
|
|
|
|
* @param $value
|
|
|
|
* The value to set. This can be any PHP data type; these functions take care
|
|
|
|
* of serialization as necessary.
|
|
|
|
*/
|
2015-09-24 22:01:04 +00:00
|
|
|
function variable_set($name, $value, $comment = null) {
|
2017-08-17 01:32:18 +00:00
|
|
|
global $conf, $db, $msg, $hooks;
|
|
|
|
$msg->log('variable', 'variable_set', '+' . serialize($value) . '+' . $comment . '+');
|
2015-09-24 22:01:04 +00:00
|
|
|
|
|
|
|
variable_init_maybe();
|
2015-11-05 17:07:38 +00:00
|
|
|
|
2015-09-24 22:01:04 +00:00
|
|
|
if (is_object($value) || is_array($value)) {
|
|
|
|
$value2 = serialize($value);
|
2015-11-05 17:07:38 +00:00
|
|
|
} else {
|
|
|
|
$value2 = $value;
|
2015-09-24 22:01:04 +00:00
|
|
|
}
|
|
|
|
if (array_key_exists($name, $conf)) {
|
|
|
|
$previous = $conf[$name];
|
2015-06-17 14:50:37 +00:00
|
|
|
} else {
|
2015-09-24 22:01:04 +00:00
|
|
|
$previous = null;
|
|
|
|
}
|
|
|
|
if (!array_key_exists($name, $conf) || $value != $conf[$name]) {
|
|
|
|
$conf[$name] = $value;
|
|
|
|
if (empty($comment)) {
|
2016-05-18 09:19:20 +00:00
|
|
|
$query = "INSERT INTO variable (name, value) values ( ?, ?) on duplicate key update name= ?, value= ? ;";
|
|
|
|
$query_args = array($name, $value2, $name, $value2);
|
|
|
|
|
2015-09-24 22:01:04 +00:00
|
|
|
} else {
|
2016-05-18 09:19:20 +00:00
|
|
|
$query = "INSERT INTO variable (name, value, comment) values ( ?, ?, ?) on duplicate key update name= ?, value= ?, comment= ? ;";
|
|
|
|
$query_args = array($name, $value2, $comment, $name, $value2, $comment);
|
2015-09-24 22:01:04 +00:00
|
|
|
}
|
2016-05-18 09:19:20 +00:00
|
|
|
$db->query($query, $query_args);
|
2015-09-24 22:01:04 +00:00
|
|
|
$hooks->invoke("hook_variable_set", array("name" => $name, "old" => $previous, "new" => $value));
|
2015-06-17 14:50:37 +00:00
|
|
|
}
|
2014-11-27 14:30:49 +00:00
|
|
|
}
|
|
|
|
|
2017-10-06 21:42:39 +00:00
|
|
|
|
2014-11-27 14:30:49 +00:00
|
|
|
/**
|
|
|
|
* Unset a persistent variable.
|
|
|
|
*
|
|
|
|
* @param $name
|
|
|
|
* The name of the variable to undefine.
|
|
|
|
*/
|
|
|
|
function variable_del($name) {
|
2015-09-24 22:01:04 +00:00
|
|
|
global $conf, $db;
|
2016-05-18 09:19:20 +00:00
|
|
|
$db->query("DELETE FROM `variable` WHERE name = ?;", array($name));
|
2015-09-24 22:01:04 +00:00
|
|
|
unset($conf[$name]);
|
2014-11-27 14:30:49 +00:00
|
|
|
}
|
|
|
|
|
2017-10-06 21:42:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* List all variables
|
|
|
|
*/
|
2014-11-27 14:30:49 +00:00
|
|
|
function variables_list() {
|
2015-09-24 22:01:04 +00:00
|
|
|
global $db;
|
|
|
|
$t = array();
|
|
|
|
$db->query("SELECT * FROM `variable` WHERE `comment` IS NOT NULL ORDER BY `name`");
|
|
|
|
while ($db->next_record()) {
|
|
|
|
$t[] = $db->Record;
|
|
|
|
}
|
|
|
|
return $t;
|
2014-11-27 14:30:49 +00:00
|
|
|
}
|
2017-10-06 21:42:39 +00:00
|
|
|
|