PageRenderTime 209ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/bsnowman/classyblog
PHP | 262 lines | 127 code | 35 blank | 100 comment | 24 complexity | c7aa2186ad02bdaec3e88af802257d59 MD5 | raw file
Possible License(s): BSD-3-Clause
  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. /**
  19. * Constructor
  20. */
  21. function __construct($master = false) {
  22. $preview = w3_is_preview_mode();
  23. if (defined('WP_ADMIN')) {
  24. $config_admin = w3_instance('W3_ConfigAdmin');
  25. $preview = $config_admin->get_boolean('previewmode.enabled');
  26. }
  27. if ($master)
  28. $this->_blog_id = 0;
  29. else
  30. $this->_blog_id = w3_get_blog_id();
  31. $this->_preview = $preview;
  32. $this->load();
  33. }
  34. /**
  35. * Check if we are in preview mode
  36. */
  37. function is_preview() {
  38. return $this->_preview;
  39. }
  40. /**
  41. * Sets config value
  42. *
  43. * @param string $key
  44. * @param string $value
  45. * @return value set
  46. */
  47. function set($key, $value) {
  48. $key = $this->_get_writer()->resolve_http_key($key);
  49. $value = $this->_get_writer()->set($key, $value);
  50. return parent::set($key, $value);
  51. }
  52. /**
  53. * Sets default values
  54. */
  55. function set_defaults() {
  56. $this->_get_writer()->set_defaults();
  57. $this->_flush_cache();
  58. }
  59. /**
  60. * Saves modified config
  61. */
  62. function save($deprecated = false) {
  63. $this->_get_writer()->save();
  64. $this->_flush_cache();
  65. }
  66. /**
  67. * Deploys the config file from a preview config file
  68. *
  69. * @param integer $direction +1: preview->production
  70. * -1: production->preview
  71. * @param boolean $remove_source remove source file
  72. */
  73. function preview_production_copy($direction = 1, $remove_source = false) {
  74. $this->_get_writer()->preview_production_copy($direction, $remove_source);
  75. $this->_flush_cache(
  76. ($direction > 0 ? false /* del production */: true /* del preview */));
  77. }
  78. /**
  79. * Checks if own configuration file exists
  80. *
  81. * @return bool
  82. */
  83. function own_config_exists() {
  84. return $this->_get_writer()->own_config_exists();
  85. }
  86. /**
  87. * Loads config
  88. */
  89. function load() {
  90. $filename = $this->_get_config_filename();
  91. if (!$this->_read($filename))
  92. $this->_data = $this->_get_writer()->create_compiled_config($filename);
  93. }
  94. /**
  95. * Exports config content
  96. *
  97. * @return string
  98. */
  99. function export() {
  100. return file_get_contents($this->_get_config_filename());
  101. }
  102. /**
  103. * Imports config content
  104. *
  105. * @param string $filename
  106. * @return boolean
  107. */
  108. function import($filename) {
  109. if (file_exists($filename) && is_readable($filename)) {
  110. $data = file_get_contents($filename);
  111. if (substr($data, 0, 5) == '<?php')
  112. $data = substr($data, 5);
  113. $config = eval($data);
  114. if (is_array($config)) {
  115. foreach ($config as $key => $value)
  116. $this->set($key, $value);
  117. return true;
  118. }
  119. }
  120. return false;
  121. }
  122. /**
  123. * Returns true if we edit master config
  124. * @return boolean
  125. */
  126. function is_master() {
  127. return ($this->_blog_id <= 0);
  128. }
  129. /**
  130. * Flushes the cache and rebuilds it from scratch
  131. *
  132. * @return void
  133. */
  134. function refresh_cache() {
  135. $this->_flush_cache();
  136. $this->load();
  137. }
  138. /**
  139. * Tries to get cache options which are not filled yet
  140. * and saves cache
  141. */
  142. function fill_missing_cache_options_and_save() {
  143. if (isset($this->_data['wordpress.home']) || w3_force_master())
  144. return false;
  145. $this->refresh_cache();
  146. return true;
  147. }
  148. /**
  149. * Will get a value from the config cache.
  150. * Will rebuild the cache in case the option doesn't exist.
  151. *
  152. * @param string $option
  153. * @return mixed value of the option if it can be found in the (regenerated) cache, null if otherwise
  154. */
  155. function get_cache_option($option) {
  156. $value = null;
  157. // Attempt to get the value
  158. if (isset($this->_data[$option])) {
  159. $value = $this->_data[$option];
  160. } else {
  161. // Rebuild the cache
  162. $this->refresh_cache();
  163. // Try again
  164. $value = $this->_data[$option];
  165. if (!isset($this->_data[$option])) {
  166. // true value is a sign to just generate config cache
  167. $GLOBALS['w3tc_blogmap_register_new_item'] = true;
  168. }
  169. }
  170. return $value;
  171. }
  172. /**
  173. * Reads config from file
  174. *
  175. * @param string $filename
  176. * @return boolean
  177. */
  178. private function _read($filename) {
  179. if (file_exists($filename) && is_readable($filename)) {
  180. // include errors not hidden by @ since they still terminate
  181. // process (code not functonal), but hides reason why
  182. $config = include $filename;
  183. if (is_array($config)) {
  184. if (isset($config['version'])
  185. && $config['version'] == W3TC_VERSION) {
  186. $this->_data = $config;
  187. return true;
  188. }
  189. }
  190. }
  191. return false;
  192. }
  193. private function _flush_cache($forced_preview = null) {
  194. if ($this->_blog_id > 0)
  195. @unlink($this->_get_config_filename($forced_preview));
  196. else {
  197. // clear whole cache if we change master config
  198. w3_require_once(W3TC_INC_DIR . '/functions/file.php');
  199. w3_emptydir(W3TC_CACHE_CONFIG_DIR);
  200. }
  201. }
  202. /*
  203. * Returns config filename
  204. *
  205. * @return string
  206. */
  207. private function _get_config_filename($forced_preview = null) {
  208. $preview = (is_null($forced_preview) ? $this->_preview : $forced_preview);
  209. $postfix = ($preview ? '-preview' : '') . '.php';
  210. if ($this->_blog_id <= 0 || w3_force_master())
  211. return W3TC_CACHE_CONFIG_DIR . '/master' . $postfix;
  212. return W3TC_CACHE_CONFIG_DIR . '/' .
  213. sprintf('%06d', $this->_blog_id) . $postfix;
  214. }
  215. /*
  216. * Returns object able to write config files
  217. *
  218. * @return string
  219. */
  220. private function _get_writer() {
  221. if (!isset($this->_writer)) {
  222. w3_require_once(W3TC_LIB_W3_DIR . '/ConfigWriter.php');
  223. $this->_writer = new W3_ConfigWriter($this->_blog_id, $this->_preview);
  224. }
  225. return $this->_writer;
  226. }
  227. }