Alternc  latest
Alternc logiel libre pour l'hébergement
variables.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  ----------------------------------------------------------------------
5  LICENSE
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License (GPL)
9  as published by the Free Software Foundation; either version 2
10  of the License, or (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  To read the license please visit http://www.gnu.org/copyleft/gpl.html
18  ----------------------------------------------------------------------
19 */
20 
21 /**
22  * Persistent variable table
23  *
24  * @author Drupal Developpement Team
25  * @link http://cvs.drupal.org/viewcvs/drupal/drupal/includes/bootstrap.inc?rev=1.38&view=auto
26  */
27 
28 
29 /**
30  * Load the persistent variable table.
31  *
32  * The variable table is composed of values that have been saved in the table
33  * with variable_set() as well as those explicitly specified in the configuration
34  * file.
35  */
36 function variable_init($conf = array()) {
37  global $db;
38  $result = $db->query('SELECT * FROM `variable`');
39  $variables=array();
40  while ($db->next_record($result)) {
41  /* maybe the data is *not* serialized, in that case, take it verbatim */
42  $variable = $db->Record;
43  if (($variables[$variable['name']] = @unserialize($variable['value'])) === FALSE) {
44  $variables[$variable['name']] = $variable['value'];
45  }
46  }
47 
48  foreach ($conf as $name => $value) {
49  $variables[$name] = $value;
50  }
51 
52  return $variables;
53 }
54 
55 
56 /**
57  * Initialize the global $conf array if necessary
58  *
59  * @global $conf the global conf array
60  * @uses variable_init()
61  */
62 function variable_init_maybe() {
63  global $conf;
64  if (!isset($conf)) {
65  $conf = variable_init();
66  }
67 }
68 
69 
70 /**
71  * Return a persistent variable.
72  *
73  * @param $name
74  * The name of the variable to return.
75  * @param $default
76  * The default value to use if this variable has never been set.
77  * @param $createit_comment
78  * If variable doesn't exist, create it with the default value
79  * and createit_comment value as comment
80  * @return
81  * The value of the variable.
82  * @global $conf
83  * A cache of the configuration.
84  */
85 function variable_get($name, $default = null, $createit_comment = null) {
86  global $conf;
87 
89 
90  if (isset($conf[$name])) {
91  return $conf[$name];
92  } elseif (!is_null($createit_comment)) {
93  variable_set($name, $default, $createit_comment);
94  }
95  return $default;
96 }
97 
98 
99 /**
100  * Set a persistent variable.
101  *
102  * @param $name
103  * The name of the variable to set.
104  * @param $value
105  * The value to set. This can be any PHP data type; these functions take care
106  * of serialization as necessary.
107  */
108 function variable_set($name, $value, $comment = null) {
109  global $conf, $db, $msg, $hooks;
110  $msg->log('variable', 'variable_set', '+' . serialize($value) . '+' . $comment . '+');
111 
113 
114  if (is_object($value) || is_array($value)) {
115  $value2 = serialize($value);
116  } else {
117  $value2 = $value;
118  }
119  if (array_key_exists($name, $conf)) {
120  $previous = $conf[$name];
121  } else {
122  $previous = null;
123  }
124  if (!array_key_exists($name, $conf) || $value != $conf[$name]) {
125  $conf[$name] = $value;
126  if (empty($comment)) {
127  $query = "INSERT INTO variable (name, value) values ( ?, ?) on duplicate key update name= ?, value= ? ;";
128  $query_args = array($name, $value2, $name, $value2);
129 
130  } else {
131  $query = "INSERT INTO variable (name, value, comment) values ( ?, ?, ?) on duplicate key update name= ?, value= ?, comment= ? ;";
132  $query_args = array($name, $value2, $comment, $name, $value2, $comment);
133  }
134  $db->query($query, $query_args);
135  $hooks->invoke("hook_variable_set", array("name" => $name, "old" => $previous, "new" => $value));
136  }
137 }
138 
139 
140 /**
141  * Unset a persistent variable.
142  *
143  * @param $name
144  * The name of the variable to undefine.
145  */
146 function variable_del($name) {
147  global $conf, $db;
148  $db->query("DELETE FROM `variable` WHERE name = ?;", array($name));
149  unset($conf[$name]);
150 }
151 
152 
153 /**
154  * List all variables
155  */
156 function variables_list() {
157  global $db;
158  $t = array();
159  $db->query("SELECT * FROM `variable` WHERE `comment` IS NOT NULL ORDER BY `name`");
160  while ($db->next_record()) {
161  $t[] = $db->Record;
162  }
163  return $t;
164 }
165 
$query
Definition: 3.0.0~3.php:37
if(! $admin->enabled) $conf
Manages global variables of AlternC.
$hooks
Definition: bootstrap.php:74
global $db
Definition: bootstrap.php:26
$msg
Definition: bootstrap.php:75
variable_set($name, $value, $comment=null)
Set a persistent variable.
Definition: variables.php:108
variable_init($conf=array())
Persistent variable table.
Definition: variables.php:36
variable_init_maybe()
Initialize the global $conf array if necessary.
Definition: variables.php:62
variable_get($name, $default=null, $createit_comment=null)
Return a persistent variable.
Definition: variables.php:85
variable_del($name)
Unset a persistent variable.
Definition: variables.php:146
variables_list()
List all variables.
Definition: variables.php:156
$value
if(empty($site_name)) elseif($piwik->site_add( $site_name, $site_urls))
foreach($domaines_user as $domaine) $t