Test Failed
Push — master ( 8c47c2...3acf9f )
by Steve
12:37
created

engine/classes/Elgg/Database/ConfigTable.php (3 issues)

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 196
	public function __construct(\Elgg\Database $db, \Elgg\BootService $boot, \Elgg\Logger $logger) {
37 196
		$this->db = $db;
38 196
		$this->boot = $boot;
39 196
		$this->logger = $logger;
40 196
	}
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
	function remove($name) {
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
50
		$name = trim($name);
51
	
52
		$query = "
53
			DELETE FROM {$this->db->prefix}config
54
			WHERE name = :name
55
		";
56
57
		$params = [
58
			':name' => $name,
59
		];
60
		
61
		$this->boot->invalidateCache();
62
	
63
		return $this->db->deleteData($query, $params) !== false;
64
	}
65
	
66
	/**
67
	 * Add or update a config setting.
68
	 *
69
	 * Plugin authors should use elgg_save_config().
70
	 *
71
	 * If the config name already exists, it will be updated to the new value.
72
	 *
73
	 * @note Internal: These settings are stored in the dbprefix_config table and read
74
	 * during system boot into the config service.
75
	 *
76
	 * @note Internal: The value is serialized so we maintain type information.
77
	 *
78
	 * @param string $name  The name of the configuration value
79
	 * @param mixed  $value Its value
80
	 *
81
	 * @return bool
82
	 */
83
	function set($name, $value) {
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
84
		$name = trim($name);
85
	
86
		// cannot store anything longer than 255 characters in db, so catch before we set
87
		if (elgg_strlen($name) > 255) {
88
			$this->logger->error("The name length for configuration variables cannot be greater than 255");
89
			return false;
90
		}
91
	
92
		$sql = "
93
			INSERT INTO {$this->db->prefix}config
94
			SET name = :name,
95
				value = :value
96
			ON DUPLICATE KEY UPDATE value = :value
97
		";
98
		
99
		$params = [
100
			':name' => $name,
101
			':value' => serialize($value),
102
		];
103
		
104
		$version = (int) _elgg_config()->version;
105
		
106
		if (!empty($version) && $version < 2016102500) {
107
			// need to do this the old way as long as site_guid columns have not been dropped
108
			// need to check if we are not updating version after removing the site_guid
109
			if ($name !== 'version' || $value !== 2016102500) {
110
				$sql = "
111
					INSERT INTO {$this->db->prefix}config
112
					SET name = :name,
113
						value = :value,
114
						site_guid = :site_guid
115
					ON DUPLICATE KEY UPDATE value = :value
116
				";
117
				
118
				$params[':site_guid'] = 1;
119
			}
120
		}
121
				
122
		$result = $this->db->insertData($sql, $params);
123
124
		$this->boot->invalidateCache();
125
	
126
		return $result !== false;
127
	}
128
	
129
	/**
130
	 * Gets a configuration value
131
	 *
132
	 * Plugin authors should use elgg_get_config().
133
	 *
134
	 * @note Internal: These settings are stored in the dbprefix_config table and read
135
	 * during system boot into the config service.
136
	 *
137
	 * @param string $name The name of the config value
138
	 *
139
	 * @return mixed|null
140
	 */
141
	function get($name) {
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
142
		$name = trim($name);
143
	
144
		$sql = "
145
			SELECT value
146
			FROM {$this->db->prefix}config
147
			WHERE name = :name
148
		";
149
			
150
		$params[':name'] = $name;
151
		
152
		$result = $this->db->getDataRow($sql, null, $params);
153
		if ($result) {
154
			return unserialize($result->value);
155
		}
156
	
157
		return null;
158
	}
159
}
160