root/alternc/trunk/bureau/class/variables.php

Revision 1534, 3.5 kB (checked in by anarcat, 2 years ago)

reverse part of the revision [1025] that erronously changed an API without documenting it in the revision log

Line 
1 <?php
2
3 /*
4  * $Id: variables.php,v 1.8 2005/04/02 00:26:36 anarcat Exp $
5  ----------------------------------------------------------------------
6  AlternC - Web Hosting System
7  Copyright (C) 2002 by the AlternC Development Team.
8  http://alternc.org/
9  ----------------------------------------------------------------------
10  Based on:
11  Valentin Lacambre's web hosting softwares: http://altern.org/
12  ----------------------------------------------------------------------
13  LICENSE
14
15  This program is free software; you can redistribute it and/or
16  modify it under the terms of the GNU General Public License (GPL)
17  as published by the Free Software Foundation; either version 2
18  of the License, or (at your option) any later version.
19
20  This program is distributed in the hope that it will be useful,
21  but WITHOUT ANY WARRANTY; without even the implied warranty of
22  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  GNU General Public License for more details.
24
25  To read the license please visit http://www.gnu.org/copyleft/gpl.html
26  ----------------------------------------------------------------------
27  */
28
29 /**
30  * Persistent variable table
31  *
32  * @author Drupal Developpement Team
33  * @link http://cvs.drupal.org/viewcvs/drupal/drupal/includes/bootstrap.inc?rev=1.38&view=auto
34  */
35
36 /**
37  * Load the persistent variable table.
38  *
39  * The variable table is composed of values that have been saved in the table
40  * with variable_set() as well as those explicitly specified in the configuration
41  * file.
42  */
43 function variable_init($conf = array()) {
44   global $db;
45   $result = $db->query('SELECT * FROM `variable`');
46   while ($db->next_record($result)) {
47     /* maybe the data is *not* serialized, in that case, take it verbatim */
48     $variable = $db->Record;
49     if (($variables[$variable['name']] = unserialize($variable['value'])) === FALSE) {
50       $variables[$variable['name']] = $variable['value'];
51     }
52   }
53  
54   foreach ($conf as $name => $value) {
55     $variables[$name] = $value;
56   }
57
58   return $variables;
59 }
60
61 /**
62  * Initialize the global $conf array if necessary
63  *
64  * @global $conf the global conf array
65  * @uses variable_init()
66  */
67 function variable_init_maybe() {
68   global $conf;
69   if (!isset($conf)) {
70     $conf = variable_init();
71   }
72 }
73
74 /**
75  * Return a persistent variable.
76  *
77  * @param $name
78  *   The name of the variable to return.
79  * @param $default
80  *   The default value to use if this variable has never been set.
81  * @return
82  *   The value of the variable.
83  * @global $conf
84  *   A cache of the configuration.
85  */
86 function variable_get($name, $default = null) {
87   global $conf;
88
89   variable_init_maybe();
90   return isset($conf[$name]) ? $conf[$name] : $default;
91 }
92
93 /**
94  * Set a persistent variable.
95  *
96  * @param $name
97  *   The name of the variable to set.
98  * @param $value
99  *   The value to set. This can be any PHP data type; these functions take care
100  *   of serialization as necessary.
101  */
102 function variable_set($name, $value) {
103   global $conf, $db;
104
105   $conf[$name] = $value;
106   if (is_object($value) || is_array($value)) {
107     $value = serialize($value);
108   }
109   @$db->query("INSERT IGNORE INTO `variable` (name, value) VALUES ('".$name."', '".$value."')");
110   if ($db->affected_rows() < 1) {
111     $db->query("UPDATE `variable` SET value = '".$value."' WHERE name = '".$name."'");
112   }
113
114   variable_init_maybe();
115 }
116
117 /**
118  * Unset a persistent variable.
119  *
120  * @param $name
121  *   The name of the variable to undefine.
122  */
123 function variable_del($name) {
124   global $conf, $db;
125
126   $db->query("DELETE FROM `variable` WHERE name = '".$name."'");
127
128   unset($conf[$name]);
129 }
130 ?>
131
Note: See TracBrowser for help on using the browser.