Passed
Push — master ( c0a3a7...3b84a4 )
by Jeroen
58:51
created

engine/classes/Elgg/Database/ConfigTable.php (1 issue)

implicit conversion of array to boolean.

Best Practice Bug Minor
1
<?php
2
namespace Elgg\Database;
3
4
/**
5
 * Manipulates values in the dbprefix_config table. Do not use to read/write $CONFIG.
6
 *
7
 * WARNING: API IN FLUX. DO NOT USE DIRECTLY.
8
 *
9
 * @access private
10
 * @since  1.10.0
11
 */
12
class ConfigTable {
13
		
14
	/**
15
	 * @var \Elgg\Database
16
	 */
17
	protected $db;
18
	
19
	/**
20
	 * @var \Elgg\BootService
21
	 */
22
	protected $boot;
23
	
24
	/**
25
	 * @var \Elgg\Logger
26
	 */
27
	protected $logger;
28
29
	/**
30
	 * Constructor
31
	 *
32
	 * @param \Elgg\Database    $db     Database
33
	 * @param \Elgg\BootService $boot   BootService
34
	 * @param \Elgg\Logger      $logger Logger
35
	 */
36 4417
	public function __construct(\Elgg\Database $db, \Elgg\BootService $boot, \Elgg\Logger $logger) {
37 4417
		$this->db = $db;
38 4417
		$this->boot = $boot;
39 4417
		$this->logger = $logger;
40 4417
	}
41
42
	/**
43
	 * Removes a config setting.
44
	 *
45
	 * @param string $name The name of the field.
46
	 *
47
	 * @return bool Success or failure
48
	 */
49 7
	function remove($name) {
50
		$query = "
51 7
			DELETE FROM {$this->db->prefix}config
52
			WHERE name = :name
53
		";
54
55
		$params = [
56 7
			':name' => $name,
57
		];
58
		
59 7
		$this->boot->invalidateCache();
60
	
61 7
		return $this->db->deleteData($query, $params) !== false;
62
	}
63
	
64
	/**
65
	 * Add or update a config setting.
66
	 *
67
	 * Plugin authors should use elgg_save_config().
68
	 *
69
	 * If the config name already exists, it will be updated to the new value.
70
	 *
71
	 * @note Internal: These settings are stored in the dbprefix_config table and read
72
	 * during system boot into the config service.
73
	 *
74
	 * @note Internal: The value is serialized so we maintain type information.
75
	 *
76
	 * @param string $name  The name of the configuration value
77
	 * @param mixed  $value Its value
78
	 *
79
	 * @return bool
80
	 */
81 6
	function set($name, $value) {
82
		// cannot store anything longer than 255 characters in db, so catch before we set
83 6
		if (elgg_strlen($name) > 255) {
84
			$this->logger->error("The name length for configuration variables cannot be greater than 255");
85
			return false;
86
		}
87
	
88
		$sql = "
89 6
			INSERT INTO {$this->db->prefix}config
90
			SET name = :name,
91
				value = :value
92
			ON DUPLICATE KEY UPDATE value = :value
93
		";
94
		
95
		$params = [
96 6
			':name' => $name,
97 6
			':value' => serialize($value),
98
		];
99
				
100 6
		$result = $this->db->insertData($sql, $params);
101
102 6
		$this->boot->invalidateCache();
103
	
104 6
		return $result !== false;
105
	}
106
	
107
	/**
108
	 * Gets a configuration value
109
	 *
110
	 * Plugin authors should use elgg_get_config().
111
	 *
112
	 * @note Internal: These settings are stored in the dbprefix_config table and read
113
	 * during system boot into the config service.
114
	 *
115
	 * @param string $name The name of the config value
116
	 *
117
	 * @return mixed|null
118
	 */
119
	function get($name) {
120
		$sql = "
121
			SELECT value
122
			FROM {$this->db->prefix}config
123
			WHERE name = :name
124
		";
125
			
126
		$params[':name'] = $name;
127
		
128
		$result = $this->db->getDataRow($sql, null, $params);
129
		if ($result) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $result of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
130
			return unserialize($result->value);
131
		}
132
	
133
		return null;
134
	}
135
136
	/**
137
	 * Load all config values from the config table (and datalists if not yet
138
	 * upgraded to 3.0)
139
	 *
140
	 * @todo move into BootData once we no longer have to support the 2.x schema
141
	 *
142
	 * @return array
143
	 */
144 13
	public function getAll() {
145 13
		$values = [];
146
147
		$sql = "
148
			SELECT *
149 13
			FROM {$this->db->prefix}config
150
		";
151 13
		foreach ($this->db->getData($sql) as $row) {
152 13
			$values[$row->name] = unserialize($row->value);
153
		}
154
155 13
		if (!array_key_exists('installed', $values)) {
156
			// try to fetch from old pre 3.0 datalists table
157
			// need to do this to be able to perform an upgrade from 2.x to 3.0
158
			try {
159
				$sql = "
160
					SELECT *
161
					FROM {$this->db->prefix}datalists
162
				";
163
				foreach ($this->db->getData($sql) as $row) {
164
					$value = $row->value;
165
					if ($row->name == 'processed_upgrades') {
166
						// config table already serializes data so no need to double serialize
167
						$value = unserialize($value);
168
					}
169
					$values[$row->name] = $value;
170
				}
171
			} catch (\Exception $e) {
172
			}
173
		}
174
175
		// don't pull in old config values
176
		/**
177
		 * @see \Elgg\Config::__construct sets this
178
		 */
179 13
		unset($values['path']);
180 13
		unset($values['dataroot']);
181 13
		unset($values['default_site']);
182
183 13
		return $values;
184
	}
185
}
186