Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Writer.php « Config « core - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b605ecdeb26c239d00261c56412b97989e094a15 (plain)
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<?php
/**
 * Piwik - Open source web analytics
 *
 * @link http://piwik.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 * @version $Id$
 *
 * @category Piwik
 * @package Piwik
 */

/**
 * This class extends the base Piwik_Config class to save in-memory changes to the local
 * configuration file.
 *
 * Example reading a value from the configuration:
 *
 *    $minValue = Piwik_Config_Writer::getInstance()->General['minimum_memory_limit'];
 *
 * will read the value minimum_memory_limit under the [General] section of the config file.
 *
 * Example setting a section in the configuration:
 *
 *    $brandingConfig = array(
 *        'use_custom_logo' => 1,
 *    );
 *    Piwik_Config_Writer::getInstance()->setConfigSection('branding', $brandingConfig);
 *
 * Example setting an option within a section in the configuration:
 *
 *    Piwik_Config_Writer::getInstance()->setConfigOption('branding', 'use_custom_logo', '1');
 *
 * @package Piwik
 * @subpackage Piwik_Config
 */
class Piwik_Config_Writer extends Piwik_Config
{
	static private $instance = null;

        /**
         * Returns the singleton Piwik_Config
         *
         * @return Piwik_Config
         */
        static public function getInstance()
        {
                if (self::$instance == null)
                {
                        self::$instance = new self;
                }
                return self::$instance;
        }

	/**
	 * Read configuration files into memory
	 *
	 * @throws Exception if file is not read/writable or contains no configuration
	 */
	public function init()
	{
		Piwik::checkDirectoriesWritableOrDie( array('/config/') );

		if(file_exists($this->pathLocal)
			&& !is_writable($this->pathLocal))
		{
			throw new Exception(Piwik_TranslateException('General_ExceptionUnwritableFileDisabledMethod', array($this->pathLocal)));
		}

		parent::init();
	}

	/**
	 * Comparison function
	 *
	 * @param mixed $elem1
	 * @param mixed $elem2
	 * @return int;
	 */
	static function compareElements($elem1, $elem2)
	{
		if (is_array($elem1)) {
			if (is_array($elem2))
			{
				return strcmp(serialize($elem1), serialize($elem2));
			}
			return 1;
		}
		if (is_array($elem2))
			return -1;

		if ((string)$elem1 === (string)$elem2)
			return 0;

		return ((string)$elem1 > (string)$elem2) ? 1 : -1;
	}

	/**
	 * Compare arrays and return difference, such that:
	 *
	 *     $modified = array_merge($original, $difference);
	 *
	 * @param array $original original array
	 * @param array $modified modified array
	 * @return array differences between original and modified
	 */
	public function array_unmerge($original, $modified)
	{
		// return key/value pairs for keys in $modified but not in $original
		// return key/value pairs for keys in both $modified and $original, but values differ
		// ignore keys that are in $original but not in $modified

		return array_udiff_assoc($modified, $original, array(__CLASS__, 'compareElements'));
	}

	/**
	 * Encode HTML entities
	 *
	 * @param mixed $values
	 * @return mixed
	 */
	protected function encodeValues($values)
	{
		if(is_array($values))
		{
			foreach($values as &$value)
			{
				$value = $this->encodeValues($value);
			}
		}
		else
		{
			$values = htmlentities($values, ENT_COMPAT);
		}
		return $values;
	}

	/**
	 * Write user configuration file
	 *
	 * @param array $configLocal
	 * @param array $configGlobal
	 * @param array $configCache
	 * @param string $pathLocal
	 */
	public function writeConfig($configLocal, $configGlobal, $configCache, $pathLocal)
	{
		$dirty = false;

		$output = "; <?php exit; ?> DO NOT REMOVE THIS LINE\n";
		$output .= "; file automatically generated or modified by Piwik; you can manually override the default values in global.ini.php by redefining them in this file.\n";

		if ($configCache)
		{
			foreach($configLocal as $name => $section)
			{
				if (!isset($configCache[$name]))
				{
					$configCache[$name] = $this->decodeValues($section);
				}
			}

			foreach($configCache as $name => $section)
			{
				$configLocal = $this->array_unmerge($configGlobal[$name], $configCache[$name]);
				if (count($configLocal) == 0)
				{
					continue;
				}

				$dirty = true;

				$output .= "[$name]\n";

				foreach($configLocal as $name => $value)
				{
					$value = $this->encodeValues($value);

					if(is_numeric($name))
					{
						$name = $section;
						$value = array($value);
					}

					if(is_array($value))
					{
						foreach($value as $currentValue)
						{
							$output .= $name."[] = \"$currentValue\"\n";
						}
					}
					else
					{
						if(!is_numeric($value))
						{
							$value = "\"$value\"";
						}
						$output .= $name.' = '.$value."\n";
					}
				}
				$output .= "\n";
			}

			if ($dirty)
			{
				@file_put_contents($pathLocal, $output );
			}
		}

		$this->clear();
	}

	/**
	 * Force save
	 */
	public function forceSave()
	{
		$this->writeConfig($this->configLocal, $this->configGlobal, $this->configCache, $this->pathLocal);
	}

	/**
	 * At the script shutdown, we save the new configuration file, if the user has set some values
	 */
	public function __destruct()
	{
		$this->forceSave();
	}
}