PageRenderTime 74ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/millien/illien.ch-wordpress
PHP | 311 lines | 162 code | 43 blank | 106 comment | 32 complexity | d602761823a9d740f008a4ae0cc6395b 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 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. */
  92. function load() {
  93. $filename = $this->_get_config_filename();
  94. // dont use cache in admin - may load some old cache when real config
  95. // contains different state (even manually edited)
  96. if (!defined('WP_ADMIN')) {
  97. $data = $this->_read($filename);
  98. if (!is_null($data)) {
  99. $this->_data = $data;
  100. return;
  101. }
  102. }
  103. $this->_data = $this->_get_writer()->create_compiled_config($filename);
  104. }
  105. /**
  106. * Exports config content
  107. *
  108. * @return string
  109. */
  110. function export() {
  111. return file_get_contents($this->_get_config_filename());
  112. }
  113. /**
  114. * Imports config content
  115. *
  116. * @param string $filename
  117. * @return boolean
  118. */
  119. function import($filename) {
  120. if (file_exists($filename) && is_readable($filename)) {
  121. $data = file_get_contents($filename);
  122. if (substr($data, 0, 5) == '<?php')
  123. $data = substr($data, 5);
  124. $config = eval($data);
  125. if (is_array($config)) {
  126. foreach ($config as $key => $value)
  127. $this->set($key, $value);
  128. return true;
  129. }
  130. }
  131. return false;
  132. }
  133. function import_legacy_config() {
  134. $data = $this->_get_writer()->get_imported_legacy_config_keys();
  135. if (!is_null($data)) {
  136. foreach ($data as $key => $value) {
  137. $this->set($key, $value);
  138. }
  139. }
  140. }
  141. /**
  142. * Returns true if we edit master config
  143. * @return boolean
  144. */
  145. function is_master() {
  146. return ($this->_blog_id <= 0);
  147. }
  148. /**
  149. * Checks if cache file is actual
  150. * @throws Exception
  151. */
  152. function validate_cache_actual() {
  153. $filename = $this->_get_config_filename();
  154. $data = $this->_read($filename);
  155. if (is_null($data))
  156. throw new Exception('Can\'t read file <strong>' .
  157. $filename . '</strong>. Remove it manually.');
  158. foreach ($this->_data as $key => $value) {
  159. if (!isset($data[$key]) || $value != $data[$key])
  160. throw new Exception('Cache file <strong>' .
  161. $filename . '</strong> can\'t be actualized. ' .
  162. 'Remove it manually.' . $key);
  163. }
  164. }
  165. /**
  166. * Flushes the cache and rebuilds it from scratch
  167. *
  168. * @return void
  169. */
  170. function refresh_cache() {
  171. $this->_flush_cache();
  172. $this->load();
  173. }
  174. /**
  175. * Tries to get cache options which are not filled yet
  176. * and saves cache
  177. */
  178. function fill_missing_cache_options_and_save() {
  179. if (isset($this->_data['wordpress.home']) || w3_force_master())
  180. return false;
  181. $this->refresh_cache();
  182. return true;
  183. }
  184. /**
  185. * Will get a value from the config cache.
  186. * Will rebuild the cache in case the option doesn't exist.
  187. *
  188. * @param string $option
  189. * @return mixed value of the option if it can be found in the (regenerated) cache, null if otherwise
  190. */
  191. function get_cache_option($option) {
  192. $value = null;
  193. // Attempt to get the value
  194. if (isset($this->_data[$option])) {
  195. $value = $this->_data[$option];
  196. } else {
  197. // Rebuild the cache
  198. $this->refresh_cache();
  199. // Try again
  200. $value = $this->_data[$option];
  201. if (!isset($this->_data[$option])) {
  202. // true value is a sign to just generate config cache
  203. $GLOBALS['w3tc_blogmap_register_new_item'] = true;
  204. }
  205. }
  206. return $value;
  207. }
  208. function get_md5() {
  209. if (is_null($this->_md5))
  210. $this->_md5 = substr(md5(serialize($this->_data)), 20);
  211. return $this->_md5;
  212. }
  213. /**
  214. * Reads config from file
  215. *
  216. * @param string $filename
  217. * @return boolean
  218. */
  219. private function _read($filename) {
  220. if (file_exists($filename) && is_readable($filename)) {
  221. // include errors not hidden by @ since they still terminate
  222. // process (code not functonal), but hides reason why
  223. $config = include $filename;
  224. if (is_array($config)) {
  225. if (isset($config['version'])
  226. && $config['version'] == W3TC_VERSION) {
  227. return $config;
  228. }
  229. }
  230. }
  231. return null;
  232. }
  233. private function _flush_cache($forced_preview = null) {
  234. $this->_md5 = null;
  235. if ($this->_blog_id > 0)
  236. @unlink($this->_get_config_filename($forced_preview));
  237. else {
  238. // clear whole cache if we change master config
  239. w3_require_once(W3TC_INC_DIR . '/functions/file.php');
  240. w3_emptydir(W3TC_CACHE_CONFIG_DIR);
  241. }
  242. }
  243. /*
  244. * Returns config filename
  245. *
  246. * @return string
  247. */
  248. private function _get_config_filename($forced_preview = null) {
  249. $preview = (is_null($forced_preview) ? $this->_preview : $forced_preview);
  250. $postfix = ($preview ? '-preview' : '') . '.php';
  251. if ($this->_blog_id <= 0 || w3_force_master())
  252. return W3TC_CACHE_CONFIG_DIR . '/master' . $postfix;
  253. return W3TC_CACHE_CONFIG_DIR . '/' .
  254. sprintf('%06d', $this->_blog_id) . $postfix;
  255. }
  256. /*
  257. * Returns object able to write config files
  258. *
  259. * @return string
  260. */
  261. private function _get_writer() {
  262. if (!isset($this->_writer)) {
  263. w3_require_once(W3TC_LIB_W3_DIR . '/ConfigWriter.php');
  264. $this->_writer = new W3_ConfigWriter($this->_blog_id, $this->_preview);
  265. }
  266. return $this->_writer;
  267. }
  268. }