File: _class_settings.php

package info (click to toggle)
b2evolution 0.9.2-3
  • links: PTS
  • area: main
  • in suites: etch-m68k
  • size: 12,976 kB
  • ctags: 5,460
  • sloc: php: 58,989; sh: 298; makefile: 36
file content (149 lines) | stat: -rw-r--r-- 3,473 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
/**
 * Class to handle the global settings
 *
 * b2evolution - {@link http://b2evolution.net/}
 * Released under GNU GPL License - {@link http://b2evolution.net/about/license.html}
 * @copyright (c)2003-2005 by Francois PLANQUE - {@link http://fplanque.net/}
 *
 * @package evocore
 * @author blueyed
 */
if( !defined('DB_USER') ) die( 'Please, do not access this page directly.' );

/**
 * Class to handle the global settings
 *
 * @package evocore
 */
class Settings
{
	/**
	 * Constructor
	 *
	 * loads settings, checks db_version
	 */
	function Settings()
	{ // constructor
		global $new_db_version, $DB, $tablesettings;

		$result = $DB->get_results( "SELECT * FROM $tablesettings" );

		if( $DB->get_col_info('name', 0) == 'set_name' )
		{ // read new format only
			foreach( $result as $loop_row )
			{
				$this->{$loop_row->set_name}->value = $loop_row->set_value;
				$this->{$loop_row->set_name}->dbstatus = 'uptodate';
				$this->{$loop_row->set_name}->dbescape = false;
			}
		}
		else
		{	// Get old version number in order to display it in error message:
				$this->db_version->value = $DB->get_var( "SELECT db_version FROM $tablesettings" );
				// This is a very incomplete load, but we'll die right below anyway. 
		}

		if( $new_db_version != $this->db_version->value )
		{	// Database is not up to date:
			$error_message = 'Database schema is not up to date. You have schema version '.$this->db_version->value.', but we would need '.$new_db_version.'.';
			require dirname(__FILE__).'/_conf_error.page.php';	// error & exit
		}
	}


	/**
	 * get a setting from the DB settings table
	 * @param string name of setting
	 */
	function get( $setting )
	{
		// echo 'get: '.$setting.'<br />';

		if( isset($this->$setting) )
		{
			return $this->$setting->value;
		}
		else
		{
			debug_log("Setting '$setting' not defined.");
			return false;
		}
	}


	/**
	 * temporarily sets a setting (updateDB(-) writes it to DB)
	 *
	 * @param string name of setting
	 * @param mixed new value
	 * @param boolean should the value be escaped in DB?
	 */
	function set( $setting, $value, $escape = true )
	{
		// echo 'set '.$setting;
		if( isset($this->$setting->value) )
		{
			if( $this->$setting->value == $value )
			{ // already set
				return false;
			}

			if( $this->$setting->dbstatus == 'uptodate' )
			{
				$this->$setting->dbstatus = 'update';
			}
			else
			{
				$this->$setting->dbstatus = 'insert';
			}
		}
		else
		{
			$this->$setting->dbstatus = 'insert';
		}

		$this->$setting->value = $value;
		$this->$setting->dbescape = $escape;

		// echo ' to '.$value.' <br />';
		return true;
	}


	/**
	 * commits changed settings to DB
	 */
	function updateDB()
	{
		global $tablesettings, $DB;

		$queries_update = array();
		$query_insert = array();

		foreach( $this as $key => $setting )
		{
			if( $setting->dbstatus != 'uptodate' )
			{
				// NOTE: we could split this to use UPDATE for dbstatus=='update'. Dunno what's better for performance.
				$query_insert[] = "('$key', '"
					.( $setting->dbescape ? $DB->escape($setting->value) : $setting->value )
					."')";
			}
		}

		$q = false;

		if( count($query_insert) )
		{
			$query = "REPLACE INTO $tablesettings (set_name, set_value) VALUES ".implode(', ', $query_insert);
			$q = $DB->query( $query );
		}

		return $q;
	}

}


?>