PageRenderTime 51ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://gitlab.com/juanito.abelo/nlmobile
PHP | 319 lines | 172 code | 41 blank | 106 comment | 36 complexity | 3d139dbcb5f5e9e9da98b81694013710 MD5 | raw file
  1. <?php
  2. w3_require_once(W3TC_LIB_W3_DIR . '/ConfigBase.php');
  3. /**
  4. * Class W3_Config
  5. * Provides configuration data using cache
  6. */
  7. class W3_Config extends W3_ConfigBase {
  8. /*
  9. * blog id of loaded config
  10. * @var integer
  11. */
  12. private $_blog_id;
  13. /*
  14. * Is this preview config
  15. * @var boolean
  16. */
  17. private $_preview;
  18. private $_md5 = null;
  19. /**
  20. * Constructor
  21. */
  22. function __construct($master = false, $blog_id = null) {
  23. $preview = w3_is_preview_mode();
  24. if (defined('WP_ADMIN')) {
  25. $config_admin = w3_instance('W3_ConfigAdmin');
  26. $preview = $config_admin->get_boolean('previewmode.enabled');
  27. }
  28. if ($master)
  29. $this->_blog_id = 0;
  30. elseif ($blog_id == null)
  31. $this->_blog_id = w3_get_blog_id();
  32. else
  33. $this->_blog_id = $blog_id;
  34. $this->_preview = $preview;
  35. $this->load();
  36. }
  37. /**
  38. * Check if we are in preview mode
  39. */
  40. function is_preview() {
  41. return $this->_preview;
  42. }
  43. /**
  44. * Sets config value
  45. *
  46. * @param string $key
  47. * @param string $value
  48. * @return mixed value set
  49. */
  50. function set($key, $value) {
  51. $key = $this->_get_writer()->resolve_http_key($key);
  52. $value = $this->_get_writer()->set($key, $value);
  53. return parent::set($key, $value);
  54. }
  55. /**
  56. * Sets default values
  57. */
  58. function set_defaults() {
  59. $this->_get_writer()->set_defaults();
  60. $this->_flush_cache();
  61. }
  62. /**
  63. * Saves modified config
  64. */
  65. function save($deprecated = false) {
  66. $this->_get_writer()->save();
  67. $this->_flush_cache();
  68. }
  69. /**
  70. * Deploys the config file from a preview config file
  71. *
  72. * @param integer $direction +1: preview->production
  73. * -1: production->preview
  74. * @param boolean $remove_source remove source file
  75. */
  76. function preview_production_copy($direction = 1, $remove_source = false) {
  77. $this->_get_writer()->preview_production_copy($direction, $remove_source);
  78. $this->_flush_cache(
  79. ($direction > 0 ? false /* del production */: true /* del preview */));
  80. }
  81. /**
  82. * Checks if own configuration file exists
  83. *
  84. * @return bool
  85. */
  86. function own_config_exists() {
  87. return $this->_get_writer()->own_config_exists();
  88. }
  89. /**
  90. * Loads config
  91. * @param bool $generate_admin_cache generates a cached config even in admin
  92. */
  93. function load($generate_admin_cache = false) {
  94. $filename = $this->_get_config_filename();
  95. // dont use cache in admin - may load some old cache when real config
  96. // contains different state (even manually edited)
  97. if (!defined('WP_ADMIN')) {
  98. $data = $this->_read($filename);
  99. if (!is_null($data)) {
  100. $this->_data = $data;
  101. return;
  102. }
  103. }
  104. $this->_data = $this->_get_writer()->create_compiled_config($filename, $generate_admin_cache || !defined('WP_ADMIN'));
  105. }
  106. /**
  107. * Exports config content
  108. *
  109. * @return string
  110. */
  111. function export() {
  112. $this->refresh_cache(true);
  113. w3_require_once(W3TC_INC_DIR . '/functions/file.php');
  114. $content = file_get_contents($this->_get_config_filename());
  115. $content = substr($content,13);
  116. $data = unserialize($content);
  117. $settings = w3tc_format_data_as_settings_file($data);
  118. return $settings;
  119. }
  120. /**
  121. * Imports config content
  122. *
  123. * @param string $filename
  124. * @return boolean
  125. */
  126. function import($filename) {
  127. if (file_exists($filename) && is_readable($filename)) {
  128. $data = file_get_contents($filename);
  129. if (substr($data, 0, 5) == '<?php')
  130. $data = substr($data, 5);
  131. $config = eval($data);
  132. if (is_array($config)) {
  133. foreach ($config as $key => $value)
  134. $this->set($key, $value);
  135. return true;
  136. }
  137. }
  138. return false;
  139. }
  140. /**
  141. * Returns true if we edit master config
  142. * @return boolean
  143. */
  144. function is_master() {
  145. return ($this->_blog_id <= 0);
  146. }
  147. /**
  148. * Checks if cache file is actual
  149. * @throws Exception
  150. */
  151. function validate_cache_actual() {
  152. $filename = $this->_get_config_filename();
  153. if (!file_exists($filename))
  154. return;
  155. $data = $this->_read($filename);
  156. if (is_null($data))
  157. throw new Exception('Can\'t read file <strong>' .
  158. $filename . '</strong>. Remove it manually.');
  159. $stale_data = array();
  160. if (false !== $data) {
  161. foreach ($this->_data as $key => $value) {
  162. if (!isset($data[$key]) || $value != $data[$key]) {
  163. $stale_data[$key] = $value;
  164. }
  165. }
  166. }
  167. if ($stale_data) {
  168. set_transient('w3tc_new_settings', $stale_data);
  169. throw new SelfTestExceptions('Cache file <strong>' .
  170. $filename . '</strong> can\'t be actualized. ' .
  171. 'Remove it manually.');
  172. }
  173. }
  174. /**
  175. * Flushes the cache and rebuilds it from scratch
  176. *
  177. * @param bool $force_refresh
  178. * @return void
  179. */
  180. function refresh_cache($force_refresh = false) {
  181. $this->_flush_cache();
  182. $this->load($force_refresh);
  183. }
  184. /**
  185. * Tries to get cache options which are not filled yet
  186. * and saves cache
  187. */
  188. function fill_missing_cache_options_and_save() {
  189. if (isset($this->_data['wordpress.home']) || w3_force_master())
  190. return false;
  191. $this->refresh_cache();
  192. return true;
  193. }
  194. /**
  195. * Will get a value from the config cache.
  196. * Will rebuild the cache in case the option doesn't exist.
  197. *
  198. * @param string $option
  199. * @return mixed value of the option if it can be found in the (regenerated) cache, null if otherwise
  200. */
  201. function get_cache_option($option) {
  202. $value = null;
  203. // Attempt to get the value
  204. if (isset($this->_data[$option])) {
  205. $value = $this->_data[$option];
  206. } else {
  207. // Rebuild the cache
  208. $this->refresh_cache();
  209. // Try again
  210. $value = $this->_data[$option];
  211. if (!isset($this->_data[$option])) {
  212. // true value is a sign to just generate config cache
  213. $GLOBALS['w3tc_blogmap_register_new_item'] = true;
  214. }
  215. }
  216. return $value;
  217. }
  218. function get_md5() {
  219. if (is_null($this->_md5))
  220. $this->_md5 = substr(md5(serialize($this->_data)), 20);
  221. return $this->_md5;
  222. }
  223. /**
  224. * Reads config from file
  225. *
  226. * @param string $filename
  227. * @return boolean
  228. */
  229. private function _read($filename) {
  230. if (file_exists($filename) && is_readable($filename)) {
  231. $content = file_get_contents($filename);
  232. $content = substr($content,13);
  233. $config = @unserialize($content);
  234. if (is_array($config)) {
  235. if (isset($config['version'])
  236. && $config['version'] == W3TC_VERSION) {
  237. return $config;
  238. }
  239. }
  240. }
  241. return null;
  242. }
  243. private function _flush_cache($forced_preview = null) {
  244. $this->_md5 = null;
  245. if ($this->_blog_id > 0)
  246. @unlink($this->_get_config_filename($forced_preview));
  247. else {
  248. // clear whole cache if we change master config
  249. w3_require_once(W3TC_INC_DIR . '/functions/file.php');
  250. w3_emptydir(W3TC_CACHE_CONFIG_DIR);
  251. }
  252. }
  253. /*
  254. * Returns config filename
  255. *
  256. * @return string
  257. */
  258. private function _get_config_filename($forced_preview = null, $master = false) {
  259. $preview = (is_null($forced_preview) ? $this->_preview : $forced_preview);
  260. $postfix = ($preview ? '-preview' : '') . '.php';
  261. if ($this->_blog_id <= 0 || w3_force_master() || $master)
  262. return W3TC_CACHE_CONFIG_DIR . '/master' . $postfix;
  263. return W3TC_CACHE_CONFIG_DIR . '/' .
  264. sprintf('%06d', $this->_blog_id) . $postfix;
  265. }
  266. /*
  267. * Returns object able to write config files
  268. *
  269. * @return string
  270. */
  271. private function _get_writer() {
  272. if (!isset($this->_writer)) {
  273. w3_require_once(W3TC_LIB_W3_DIR . '/ConfigWriter.php');
  274. $this->_writer = new W3_ConfigWriter($this->_blog_id, $this->_preview);
  275. }
  276. return $this->_writer;
  277. }
  278. }