Alternc  3.2
Alternc logiel libre pour l'h├ębergement
 All Data Structures Namespaces Files Functions Variables Pages
m_variables.php
Go to the documentation of this file.
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 class m_variables {
36  var $strata_order = array('DEFAULT','GLOBAL','FQDN_CREATOR','FQDN','CREATOR','MEMBER','DOMAIN');
37  var $cache_variable_list = false;
38  var $replace_array = array();
39 
40  /**
41  *
42  * @global type $L_FQDN
43  */
44  function m_variables() {
45  global $L_FQDN;
46  $this->replace_array = array(
47  "%%FQDN%%"=> $L_FQDN,
48  );
49  }
50 
51 
52 
53  /**
54  * used by get_impersonated to merge array. Son value overwrite father's value
55  *
56  * @param array $father
57  * @param array $son
58  * @return array
59  */
60  private function variable_merge($father, $son) {
61  if (! is_array($son)) return $father;
62  foreach ($son as $k=>$v) {
63  $father[$k] = $v;
64  }
65  return $father;
66  }
67 
68  /**
69  * Load the persistent variable table.
70  *
71  * The variable table is composed of values that have been saved in the table
72  * with variable_set() as well as those explicitly specified in the configuration
73  * file.
74  *
75  * @global int $cuid
76  * @return array
77  */
78  function variable_init() {
79  global $cuid;
80  if ($cuid > 1999) {
81  $mid = $cuid;
82  } else {
83  $mid = null;
84  }
85 
86  // In case we launch it in a script, there is no $_SERVER
87  if (isset($_SERVER['HTTP_HOST'])) {
88  $host=$_SERVER['HTTP_HOST'];
89  } else {
90  $host=null;
91  }
92  return $this->get_impersonated($host, $mid);
93  }
94 
95  /**
96  * Return the var for a specific environnement :
97  * * logged via $fqdn url
98  * * the user is $uid
99  * * $var if we want only 1 var instead of all of them
100  *
101  * If $fqdn and $uid aren't specified, return the default value
102  *
103  * @global type $db
104  * @global type $err
105  * @param type $fqdn
106  * @param type $uid
107  * @param type $var
108  * @return array
109  */
110  function get_impersonated($fqdn=null, $uid=null, $var=null) {
111  global $db, $err;
112 
113  $arr_var=$this->variables_list();
114 
115  // Get some vars we are going to need.
116  if ($fqdn != NULL) {
117  $sub_infos=m_dom::get_sub_domain_id_and_member_by_name( strtolower($fqdn) );
118  } else {
119  $sub_infos=false;
120  }
121 
122  if ( $uid != NULL ) {
124  } else {
125  $creator=false;
126  }
127 
128  $variables = array();
129  // Browse the array in the specific order of the strata
130  foreach ( $this->strata_order as $strata) {
131  if (! isset($arr_var[$strata]) || !is_array($arr_var[$strata])) continue;
132  switch($strata) {
133  case 'DEFAULT':
134 
135 // $variables = $this->variable_merge(array(),$arr_var['DEFAULT'][NULL]);
136  $variablesList = current($arr_var["DEFAULT"]);
137  $variables = $this->variable_merge(array(),$variablesList);
138  break;
139  case 'GLOBAL':
140  $variables = $this->variable_merge($variables, $arr_var['GLOBAL'][NULL]);
141  break;
142  case 'FQDN_CREATOR':
143  if ( is_array($sub_infos) && isset($arr_var['FQDN_CREATOR'][$sub_infos['member_id']]) && is_array($arr_var['FQDN_CREATOR'][$sub_infos['member_id']])) {
144  $variables = $this->variable_merge($variables, $arr_var['FQDN_CREATOR'][$sub_infos['member_id']]);
145  }
146  break;
147  case 'FQDN':
148  if ( is_array($sub_infos) && isset($arr_var['FQDN'][$sub_infos['sub_id']]) && is_array($arr_var['FQDN'][$sub_infos['sub_id']])) {
149  $variables = $this->variable_merge($variables, $arr_var['FQDN'][$sub_infos['sub_id']]);
150  }
151  break;
152  case 'CREATOR':
153  if ( $creator && isset($arr_var['CREATOR'][$creator]) && is_array($arr_var['CREATOR'][$creator])) {
154  $variables = $this->variable_merge($variables, $arr_var['CREATOR'][$creator] );
155  }
156  break;
157  case 'MEMBER':
158  if ( $uid && isset($arr_var['MEMBER'][$uid]) && is_array($arr_var['MEMBER'][$uid])) {
159  $variables = $this->variable_merge($variables, $arr_var['MEMBER'][$uid] );
160  }
161  break;
162  case 'DOMAIN':
163  //FIXME TODO
164  break;
165  } //switch
166 
167  } //foreach
168 
169  // Replace needed vars
170  foreach ($variables as $vv => $hh) {
171  if (!isset($hh['value'])) {
172  continue;
173  }
174  $variables[$vv]['value'] = strtr($hh['value'], $this->replace_array );
175  }
176 
177  if ($var && isset($variables[$var])) {
178  return $variables[$var];
179  } else {
180  return $variables;
181  }
182  }
183 
184  /**
185  * Initialize the global $conf array if necessary
186  *
187  * @global $conf the global conf array
188  * @uses variable_init()
189  */
190  function variable_init_maybe($force=false) {
191  global $conf;
192  if ($force || !isset($conf)) {
193  $this->cache_variable_list = false;
194  $conf = $this->variable_init();
195  }
196  }
197 
198  /**
199  * Return a persistent variable.
200  *
201  * @param $name
202  * The name of the variable to return.
203  * @param $default
204  * The default value to use if this variable has never been set.
205  * @param $createit_comment
206  * If variable doesn't exist, create it with the default value
207  * and createit_comment value as comment
208  * @return
209  * The value of the variable.
210  * @global $conf
211  * A cache of the configuration.
212  */
213  function variable_get($name, $default = null, $createit_comment = null, $type=null) {
214  global $conf;
215 
216  $this->variable_init_maybe();
217 
218  if (isset($conf[$name])) {
219  return $conf[$name]['value'];
220  } elseif (!is_null($createit_comment)) {
221  $this->variable_update_or_create($name, $default, 'DEFAULT', 'null', 'null', $createit_comment, $type);
222  }
223  return $default;
224  }
225 
226  /**
227  * Create or update a variable
228  *
229  * @global type $db
230  * @global type $err
231  * @param type $var_name
232  * @param type $var_value
233  * @param type $strata
234  * @param null $strata_id
235  * @param null $var_id
236  * @param type $comment
237  * @param type $type
238  * @return boolean
239  */
240  function variable_update_or_create($var_name, $var_value, $strata=null, $strata_id=null, $var_id=null, $comment=null, $type=null) {
241  global $db, $err;
242  $err->log('variable', 'variable_update_or_create');
243  if ( strtolower($var_id) == 'null' ) $var_id = null;
244  if ( strtolower($strata_id) == 'null' ) $strata_id = null;
245 
246  if (is_object($type) || is_array($type)) {
247  $type = serialize($type);
248  }
249  if (is_object($var_value) || is_array($var_value)) {
250  $var_value = serialize($var_value);
251  }
252 
253  if ( ! is_null($var_id) ) {
254  $sql="UPDATE variable SET value='".mysql_real_escape_string($var_value)."' WHERE id = ".intval($var_id);
255  } else {
256  if ( empty($strata) ) {
257  $err->raise('variables', _("Err: Missing strata when creating var"));
258  return false;
259  }
260  $sql="INSERT INTO
261  variable (name, value, strata, strata_id, comment, type)
262  VALUES (
263  '".mysql_real_escape_string($var_name)."',
264  '".mysql_real_escape_string($var_value)."',
265  '".mysql_real_escape_string($strata)."',
266  ".( is_null($strata_id)?'NULL':"'".mysql_real_escape_string($strata_id)."'").",
267  '".mysql_real_escape_string($comment)."',
268  '".mysql_real_escape_string($type)."' );";
269  }
270 
271  $db->query("$sql");
272 
273  $this->variable_init_maybe(true);
274  return true;
275  }
276 
277  /**
278  * Unset a persistent variable.
279  *
280  * @param $name
281  * The name of the variable to undefine.
282  */
283  function del($id) {
284  global $db;
285  $result = $db->query("DELETE FROM `variable` WHERE id = '".intval($id)."'");
286  $this->variable_init_maybe(true);
287  return $result;
288  }
289 
290  /**
291  * echo HTML code to display a variable passed in parameters
292  *
293  * @param type $v
294  * @param type $varname
295  * @param type $echo
296  * @return type
297  */
298  function display_valueraw_html($v,$varname,$echo = true) {
299  $output = "";
300  if (is_array($v)) {
301  if (empty($v)) {
302  $output .= "<em>"._("Empty array")."</em>";
303  } else {
304  $output .= "<ul>";
305  foreach ( $v as $k=>$l) {
306  $output .= "<li>";
307  if (! is_numeric($k)) {
308  if (is_null($varname)) {
309  $output .= "$k";
310  } else {
311  if ( !isset($this->variables_list()['DEFAULT'][null][$varname]['type'][$k]) || is_array( $this->variables_list()['DEFAULT'][null][$varname]['type'][$k] ) ) {
312  if (isset($this->variables_list()['DEFAULT'][null][$varname]['type'][$k]['desc'])) {
313  $output .= $this->variables_list()['DEFAULT'][null][$varname]['type'][$k]['desc'];
314  } else {
315  $output .= $k;
316  }
317  } else {
318  $output .= $this->variables_list()['DEFAULT'][null][$varname]['type'][$k];
319  }
320  }
321  } else {
322  if (isset($this->variables_list()['DEFAULT'][null][$varname]['type'][$k]['desc'] )) {
323  $output .= $this->variables_list()['DEFAULT'][null][$varname]['type'][$k]['desc'];
324  }
325  }
326  if (is_array($l)) {
327  $output .= "<ul>";
328  foreach ($l as $m => $n ) {
329  $output .= "<li>";
330  if ( is_numeric($m)) {
331  $output .= "$m";
332  } else {
333  $output .= $this->variables_list()['DEFAULT'][null][$varname]['type'][$k][$m]['desc'];
334  }
335  $output .= " => $n";
336  $output .= "</li>";
337  }
338  $output .= "</ul>";
339  } else {
340  $output .= " => $l";
341  }
342  $output .= "</li>";
343  }
344  $output .= "</ul>";
345  } // empty $v
346  } else if (empty($v) && $v != '0') {
347  $output .= "<em>"._("Empty")."</em>";
348  } else {
349  $output .= $v;
350  }
351  if( $echo ){
352  echo $output;
353  }
354  return $output;
355  }
356 
357  /**
358  * Display a variable if is set
359  *
360  * @param type $tab
361  * @param type $strata
362  * @param type $id
363  * @param type $varname
364  * @param type $echo
365  * @return string
366  */
367  function display_value_html($tab, $strata, $id, $varname, $echo = TRUE) {
368  $output = "";
369  if (isset($tab[$strata][$id][$varname]['value'])) {
370  $v = $tab[$strata][$id][$varname]['value'];
371  $output .= $this->display_valueraw_html($v, $varname);
372  } else {
373  $output .= "<em>"._("None defined")."</em>";
374  }
375  if( $echo){
376  echo $output;
377  }
378  return $output;
379  }
380 
381  /**
382  * return hashtable with variable_name => comment for all the vars
383  *
384  * @global type $db
385  * @return type
386  */
387  function variables_list_name() {
388  global $db;
389 
390  $result = $db->query('SELECT name, comment FROM `variable` order by name');
391  $t=array();
392  while ($db->next_record($result)) {
393  $tname = $db->f('name');
394  // If not listed of if listed comment is shorter
395  if ( ! isset( $t[$tname] ) || strlen($t[$tname]) < $db->f('comment') ) {
396  $t[$db->f('name')] = $db->f('comment');
397  }
398  }
399  return $t;
400  }
401 
402  /**
403  * return a multidimensionnal array used to build vars
404  *
405  * @global type $db
406  * @return type
407  */
408  function variables_list() {
409  global $db;
410  if ( ! $this->cache_variable_list ) {
411 
412  $result = $db->query('SELECT * FROM `variable`');
413 
414  $arr_var=array();
415  while ($db->next_record($result)) {
416  // Unserialize value if needed
417  if ( ($value = @unserialize($db->f('value'))) === FALSE) {
418  $value=$db->f('value');
419  }
420  if ( ($type = @unserialize($db->f('type'))) === FALSE) {
421  $type=$db->f('type');
422  }
423  $arr_var[$db->f('strata')][$db->f('strata_id')][$db->f('name')] = array('id'=>$db->f('id') ,'name'=>$db->f('name'), 'value'=>$value, 'comment'=>$db->f('comment'), 'type'=>$type);
424  }
425  $this->cache_variable_list = $arr_var;
426  }
427 
429  }
430 
431 } /* Class m_variables */