PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/w3-total-cache/lib/W3/ConfigWriter.php

https://gitlab.com/juanito.abelo/nlmobile
PHP | 402 lines | 209 code | 53 blank | 140 comment | 73 complexity | 77588c57e310aadef78a64d1b5d4f696 MD5 | raw file
  1. <?php
  2. w3_require_once(W3TC_INC_DIR . '/functions/file.php');
  3. w3_require_once(W3TC_LIB_W3_DIR . '/ConfigData.php');
  4. /**
  5. * Class W3_dataWriter
  6. */
  7. class W3_ConfigWriter {
  8. /*
  9. * blog id of loaded config
  10. * @var integer
  11. */
  12. private $_blog_id;
  13. /*
  14. * Configuration
  15. * @var object
  16. */
  17. private $_data;
  18. /*
  19. * Is this preview config
  20. * @var boolean
  21. */
  22. private $_preview;
  23. /**
  24. * Sealing keys from master config which blocks key overrides
  25. * @var array
  26. */
  27. private $_sealing_keys_scope;
  28. /**
  29. * Constructor
  30. *
  31. * @param integer $blog_id
  32. * @param boolean $preview
  33. */
  34. function __construct($blog_id, $preview) {
  35. $this->_blog_id = $blog_id;
  36. /**
  37. * defines $keys with descriptors
  38. * @var array $keys config keys
  39. * @var array $sealing_keys_scope
  40. */
  41. include W3TC_LIB_W3_DIR . '/ConfigKeys.php';
  42. $this->_sealing_keys_scope = $sealing_keys_scope;
  43. $this->_data = new W3_ConfigData($keys);
  44. if (!$this->own_config_exists() && $blog_id <= 0)
  45. $this->_data->set_defaults();
  46. $this->_preview = $preview;
  47. $this->_data->read($this->_get_config_filename());
  48. }
  49. /*
  50. * Compiles active config from different files
  51. *
  52. * @param string $cache_filename
  53. * @return config values
  54. */
  55. function create_compiled_config($cache_filename, $write = true) {
  56. /**
  57. * defines $keys with descriptors
  58. * @var array $keys config keys
  59. */
  60. include W3TC_LIB_W3_DIR . '/ConfigKeys.php';
  61. $compiled_config = new W3_ConfigData($keys);
  62. $compiled_config->set_defaults();
  63. // collect config data from master config
  64. if (!$compiled_config->read($this->_get_config_filename(true))) {
  65. // try to read production master config
  66. $success = false;
  67. if ($this->_preview)
  68. $success = $compiled_config->read(
  69. $this->_get_config_filename(true, false));
  70. // try to read legacy master config
  71. if (!$success) {
  72. /**
  73. * @var W3_ConfigCompatibility $config_comp
  74. */
  75. $config_comp = w3_instance('W3_ConfigCompatibility');
  76. $data = $config_comp->get_imported_legacy_config_keys($this->_blog_id, true);
  77. if (!is_null($data)) {
  78. foreach ($data as $key => $value) {
  79. $compiled_config->set($key, $value);
  80. }
  81. }
  82. }
  83. }
  84. $this->_put_instance_value_in_config($compiled_config);
  85. // append data from blog config
  86. $config_admin = w3_instance('W3_ConfigAdmin');
  87. $data = W3_ConfigData::get_array_from_file($this->_get_config_filename());
  88. if (isset($data['version']) && version_compare($data['version'], W3TC_VERSION, '<')) {
  89. /**
  90. * @var W3_ConfigCompatibility $config_comp
  91. */
  92. $config_comp = w3_instance('W3_ConfigCompatibility');
  93. $config_comp->load_new_settings($data['version']);
  94. }
  95. if (is_null($data)) {
  96. /**
  97. * @var W3_ConfigCompatibility $config_comp
  98. */
  99. $config_comp = w3_instance('W3_ConfigCompatibility');
  100. $data = $config_comp->get_imported_legacy_config_keys($this->_blog_id);
  101. }
  102. if (!is_null($data)) {
  103. foreach ($data as $key => $value) {
  104. if (!$this->_key_sealed($key, $compiled_config->data, $config_admin, $value))
  105. $compiled_config->set($key, $value);
  106. }
  107. }
  108. // save the value for 'home' in the config
  109. $this->_put_home_value_in_config($compiled_config->data);
  110. // save the bad_behavior path in config if plugin exists
  111. $this->_put_bad_behavior_in_config($compiled_config->data);
  112. $this->_post_process_values($compiled_config);
  113. // write cache
  114. if ($write) {
  115. try {
  116. $compiled_config->write($cache_filename, true);
  117. } catch (Exception $ex) { // dont care here about file permissions
  118. }
  119. $this->flush_apc($cache_filename, true);
  120. }
  121. return $compiled_config->data;
  122. }
  123. /*
  124. * Converts configuration key returned in http _GET/_POST
  125. * to configuration key
  126. *
  127. * @param $http_key string
  128. * @return string
  129. */
  130. function resolve_http_key($http_key) {
  131. return $this->_data->resolve_http_key($http_key);
  132. }
  133. /**
  134. * Sets config value
  135. *
  136. * @param string $key
  137. * @param string $value
  138. * @return mixed set value
  139. */
  140. function set($key, $value) {
  141. return $this->_data->set($key, $value);
  142. }
  143. /**
  144. * Sets default values
  145. */
  146. function set_defaults() {
  147. $this->_data->clear();
  148. }
  149. /**
  150. * Saves modified config
  151. */
  152. function save() {
  153. $filename = $this->_get_config_filename();
  154. if (!is_dir(dirname($filename))) {
  155. w3_require_once(W3TC_INC_DIR . '/functions/file.php');
  156. w3_mkdir_from(dirname($filename), WP_CONTENT_DIR);
  157. }
  158. $this->_data->write($filename);
  159. $this->flush_apc($filename);
  160. }
  161. /**
  162. * Deploys the config file from a preview config file
  163. *
  164. * @param integer $direction +1: preview->production
  165. * -1: production->preview
  166. * @param boolean $remove_source remove source file
  167. */
  168. function preview_production_copy($direction, $remove_source) {
  169. $preview_filename = $this->_get_config_filename(false, true);
  170. $production_filename = $this->_get_config_filename(false, false);
  171. if ($direction > 0) {
  172. $src = $preview_filename;
  173. $dest = $production_filename;
  174. } else {
  175. $src = $production_filename;
  176. $dest = $preview_filename;
  177. }
  178. if (!@copy($src, $dest)) {
  179. w3_require_once(W3TC_INC_DIR . '/functions/activation.php');
  180. w3_throw_on_write_error($dest);
  181. }
  182. if ($remove_source)
  183. @unlink($src);
  184. }
  185. /**
  186. * Checks if own configuration file exists
  187. *
  188. * @return bool
  189. */
  190. function own_config_exists() {
  191. return @file_exists($this->_get_config_filename());
  192. }
  193. /**
  194. * Returns true is key is not modifiable anymore
  195. *
  196. * @param string $key
  197. * @param array $config_data
  198. * @param object $config_admin
  199. * @param mixed $value
  200. * @return bool
  201. */
  202. private function _key_sealed($key, &$config_data, $config_admin, &$value) {
  203. // skip finalized values by confif sealing options
  204. foreach ($this->_sealing_keys_scope as $i) {
  205. if (substr($key, 0, strlen($i['prefix'])) == $i['prefix']) {
  206. if (strpos($key, 'extensions') === false && $config_admin->get_boolean($i['key'])) {
  207. return true;
  208. } elseif (strpos($key, 'extensions') !== false) {
  209. $sealed = $config_admin->get_array($i['key']);
  210. $master_data = $config_data[$key];
  211. foreach ($sealed as $extension => $sealed_state) {
  212. if ($sealed_state && isset($master_data[$extension]))
  213. $value[$extension] = $master_data[$extension];
  214. }
  215. return false;
  216. }
  217. }
  218. }
  219. if ($key == 'minify.enabled' && !$config_data['minify.enabled'] && $value)
  220. return true;
  221. if ($key == 'pgcache.engine' && ($config_data['pgcache.engine'] != 'file_generic') && $value == 'file_generic')
  222. return true;
  223. if ($key == 'minify.engine')
  224. return true;
  225. if ($key == 'minify.rewrite' && !$config_data['minify.rewrite'] && $value)
  226. return true;
  227. //TODO: Add a hook
  228. return false;
  229. }
  230. /*
  231. * Returns config filename
  232. *
  233. * @param bool $master
  234. * @return string
  235. */
  236. private function _get_config_filename($force_master = false, $forced_preview = null) {
  237. if (w3_force_master())
  238. $force_master = true;
  239. $preview = (!is_null($forced_preview) ? $forced_preview : $this->_preview);
  240. $postfix = ($preview ? '-preview' : '') . '.php';
  241. if ($this->_blog_id <= 0 || $force_master)
  242. return W3TC_CONFIG_DIR . '/master' . $postfix;
  243. return W3TC_CONFIG_DIR . '/' .
  244. sprintf('%06d', $this->_blog_id) . $postfix;
  245. }
  246. public static function get_config_filename() {
  247. $force_master = false;
  248. if (w3_force_master())
  249. $force_master = true;
  250. $postfix = '.php';
  251. if (w3_get_blog_id() <= 0 || $force_master)
  252. return W3TC_CONFIG_DIR . '/master' . $postfix;
  253. return W3TC_CONFIG_DIR . '/' . sprintf('%06d', w3_get_blog_id()) . $postfix;
  254. }
  255. /**
  256. * Checks that config options are compatible
  257. *
  258. * @param object $config
  259. */
  260. private function _post_process_values($config) {
  261. $helper = new W3_ConfigBase($config->data);
  262. // caching pages with query string is not supported by Disk Basic
  263. if ($helper->get_boolean('pgcache.cache.query')) {
  264. if ($helper->get_string('pgcache.engine') == 'file_generic')
  265. $config->data['pgcache.cache.query'] = false;
  266. }
  267. // When feeds are cached - we have to switch on .xml files handling
  268. // by rules for nginx to return correct headers
  269. if ($helper->get_boolean('pgcache.cache.feed')) {
  270. if ($helper->get_string('pgcache.engine') == 'file_generic')
  271. $config->data['pgcache.cache.nginx_handle_xml'] = true;
  272. }
  273. }
  274. /**
  275. * Saves the value for 'wordpress.home' in the config cache.
  276. *
  277. * @param array
  278. * @return array
  279. **/
  280. private function _put_home_value_in_config(&$config_data) {
  281. // this also gets called during activation, when get_option is not yet available
  282. if (function_exists("get_option") && function_exists('wp_cache_get')) {
  283. global $wp_object_cache;
  284. if (isset($wp_object_cache) && !empty($wp_object_cache))
  285. $config_data['wordpress.home'] = get_option('home');
  286. }
  287. return $config_data;
  288. }
  289. /**
  290. * Saves the path to bad_behavior in config cache if it exists
  291. * @param $config_data
  292. */
  293. private function _put_bad_behavior_in_config(&$config_data) {
  294. if (function_exists('bb2_start')) {
  295. if (file_exists(WP_PLUGIN_DIR . '/bad-behavior/bad-behavior-generic.php')) {
  296. $bb_file = WP_PLUGIN_DIR . '/bad-behavior/bad-behavior-generic.php';
  297. } elseif (file_exists(WP_PLUGIN_DIR . '/Bad-Behavior/bad-behavior-generic.php')) {
  298. $bb_file = WP_PLUGIN_DIR . '/Bad-Behavior/bad-behavior-generic.php';
  299. } else {
  300. $bb_file = false;
  301. }
  302. if ($bb_file) {
  303. $config_data['pgcache.bad_behavior_path'] = $bb_file;
  304. }
  305. } else {
  306. $config_data['pgcache.bad_behavior_path'] = '';
  307. }
  308. }
  309. /**
  310. * Store instance in master config.
  311. * @param W3_ConfigData $compiled_config
  312. */
  313. private function _put_instance_value_in_config($compiled_config) {
  314. if (!isset($compiled_config->data['common.instance_id']) ||
  315. $compiled_config->data['common.instance_id'] == 0) {
  316. // read common.instance_id from master config
  317. /**
  318. * @var $keys
  319. */
  320. include W3TC_LIB_W3_DIR . '/ConfigKeys.php';
  321. $config2 = new W3_ConfigData($keys);
  322. $config2->read($this->_get_config_filename(true, false));
  323. if (isset($config2->data['common.instance_id']) &&
  324. $config2->data['common.instance_id'] != 0) {
  325. $compiled_config->data['common.instance_id'] =
  326. $config2->data['common.instance_id'];
  327. }
  328. }
  329. }
  330. /**
  331. * Flush the APC
  332. * @param string $filename file to reload
  333. * @param bool $local_flush default false. Flushes over SNS if false. Local if true
  334. */
  335. private function flush_apc($filename, $local_flush = false) {
  336. // If apc.stat is set to 0 (= no automatic detection of changes files), we explicitly flush the apc cache
  337. if(function_exists('apc_clear_cache') && ini_get('apc.stat') == '0' && !(defined('DONOTFLUSHAPC') && DONOTFLUSHAPC)) {
  338. if ($local_flush) {
  339. /** @var $w3_cacheflush W3_CacheFlushLocal */
  340. $w3_cacheflush = w3_instance('W3_CacheFlushLocal');
  341. $w3_cacheflush->apc_reload_file($filename);
  342. } else {
  343. /** @var $w3_cacheflush W3_CacheFlush */
  344. $w3_cacheflush = w3_instance('W3_CacheFlush');
  345. $w3_cacheflush->apc_reload_file($filename);
  346. }
  347. }
  348. }
  349. }