PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/reareaf/wp-re
PHP | 419 lines | 220 code | 114 blank | 85 comment | 50 complexity | 2a68bfd0adc48182a269a09b0e9048e7 MD5 | raw file
Possible License(s): BSD-3-Clause, MIT, Apache-2.0
  1. <?php
  2. namespace W3TC;
  3. /**
  4. * class Config
  5. * Provides configuration data using cache
  6. */
  7. class Config {
  8. /*
  9. * blog id of loaded config
  10. * @var integer
  11. */
  12. private $_blog_id;
  13. private $_is_master;
  14. /*
  15. * Is this preview config
  16. * @var boolean
  17. */
  18. private $_preview;
  19. private $_md5;
  20. private $_data;
  21. private $_compiled;
  22. /**
  23. * Reads config from file and returns it's content as array (or null)
  24. * Stored in this class to limit class loading
  25. */
  26. static public function util_array_from_storage( $blog_id, $preview ) {
  27. if ( !defined( 'W3TC_CONFIG_CACHE_ENGINE' ) ) {
  28. return self::_util_array_from_storage( $blog_id, $preview );
  29. }
  30. // config cache enabled
  31. $config = ConfigCache::util_array_from_storage( $blog_id, $preview );
  32. if ( !is_null( $config ) ) {
  33. return $config;
  34. }
  35. $config = self::_util_array_from_storage( $blog_id, $preview );
  36. ConfigCache::save_item( $blog_id, $preview, $config );
  37. return $config;
  38. }
  39. static private function _util_array_from_storage( $blog_id, $preview ) {
  40. if ( defined( 'W3TC_CONFIG_DATABASE' ) && W3TC_CONFIG_DATABASE ) {
  41. return ConfigDbStorage::util_array_from_storage( $blog_id, $preview );
  42. }
  43. $filename = self::util_config_filename( $blog_id, $preview );
  44. if ( file_exists( $filename ) && is_readable( $filename ) ) {
  45. // including file directly instead of read+eval causes constant
  46. // problems with APC, ZendCache, and WSOD in a case of
  47. // broken config file
  48. $content = @file_get_contents( $filename );
  49. $config = @json_decode( substr( $content, 14 ), true );
  50. if ( is_array( $config ) ) {
  51. return $config;
  52. }
  53. }
  54. return null;
  55. }
  56. /*
  57. * Returns config filename
  58. * Stored in this class to limit class loading
  59. */
  60. static public function util_config_filename( $blog_id, $preview ) {
  61. $postfix = ( $preview ? '-preview' : '' ) . '.php';
  62. if ( $blog_id <= 0 )
  63. return W3TC_CONFIG_DIR . '/master' . $postfix;
  64. else
  65. return W3TC_CONFIG_DIR . '/' . sprintf( '%06d', $blog_id ) . $postfix;
  66. }
  67. /*
  68. * Returns config filename
  69. * Stored in this class to limit class loading
  70. * v = 0.9.5 - 0.9.5.1
  71. */
  72. static public function util_config_filename_legacy_v2( $blog_id, $preview ) {
  73. $postfix = ( $preview ? '-preview' : '' ) . '.json';
  74. if ( $blog_id <= 0 )
  75. return W3TC_CONFIG_DIR . '/master' . $postfix;
  76. else
  77. return W3TC_CONFIG_DIR . '/' . sprintf( '%06d', $blog_id ) . $postfix;
  78. }
  79. public function __construct( $blog_id = null ) {
  80. if ( !is_null( $blog_id ) ) {
  81. $this->_blog_id = $blog_id;
  82. $this->_is_master = ( $this->_blog_id == 0 );
  83. } else {
  84. if ( Util_Environment::is_using_master_config() )
  85. $this->_blog_id = 0;
  86. else
  87. $this->_blog_id = Util_Environment::blog_id();
  88. $this->_is_master = ( Util_Environment::blog_id() == 0 );
  89. }
  90. $this->_preview = Util_Environment::is_preview_mode();
  91. $this->load();
  92. }
  93. /**
  94. * Returns config value. Implementation for overriding
  95. */
  96. public function get( $key, $default = null ) {
  97. $v = $this->_get( $this->_data, $key );
  98. if ( !is_null( $v ) )
  99. return $v;
  100. // take default value
  101. if ( !empty( $default ) || !function_exists( 'apply_filters' ) )
  102. return $default;
  103. // try cached default values
  104. static $default_values = null;
  105. if ( is_null( $default_values ) )
  106. $default_values = apply_filters( 'w3tc_config_default_values',
  107. array() );
  108. $v = $this->_get( $default_values, $key );
  109. if ( !is_null( $v ) )
  110. return $v;
  111. // update default values
  112. $default_values = apply_filters( 'w3tc_config_default_values',
  113. array() );
  114. $v = $this->_get( $default_values, $key );
  115. if ( !is_null( $v ) )
  116. return $v;
  117. return $default;
  118. }
  119. private function _get( &$a, $key ) {
  120. if ( is_array( $key ) ) {
  121. $key0 = $key[0];
  122. if ( isset( $a[$key0] ) ) {
  123. $key1 = $key[1];
  124. if ( isset( $a[$key0][$key1] ) )
  125. return $a[$key0][$key1];
  126. }
  127. } else if ( isset( $a[$key] ) ) {
  128. return $a[$key];
  129. }
  130. return null;
  131. }
  132. /**
  133. * Returns string value
  134. */
  135. public function get_string( $key, $default = '', $trim = true ) {
  136. $value = (string)$this->get( $key, $default );
  137. return $trim ? trim( $value ) : $value;
  138. }
  139. /**
  140. * Returns integer value
  141. */
  142. public function get_integer( $key, $default = 0 ) {
  143. return (integer)$this->get( $key, $default );
  144. }
  145. /**
  146. * Returns boolean value
  147. */
  148. public function get_boolean( $key, $default = false ) {
  149. return (boolean)$this->get( $key, $default );
  150. }
  151. /**
  152. * Returns array value
  153. */
  154. public function get_array( $key, $default = array() ) {
  155. return (array)$this->get( $key, $default );
  156. }
  157. /**
  158. * Check if an extension is active
  159. */
  160. public function is_extension_active( $extension ) {
  161. $extensions = $this->get_array( 'extensions.active' );
  162. return isset( $extensions[$extension] );
  163. }
  164. public function is_extension_active_frontend( $extension ) {
  165. $extensions = $this->get_array( 'extensions.active_frontend' );
  166. return isset( $extensions[$extension] );
  167. }
  168. public function set_extension_active_frontend( $extension,
  169. $is_active_frontend ) {
  170. $a = $this->get_array( 'extensions.active_frontend' );
  171. if ( !$is_active_frontend )
  172. unset( $a[$extension] );
  173. else
  174. $a[$extension] = '*';
  175. $this->set( 'extensions.active_frontend', $a );
  176. }
  177. /**
  178. * Sets config value.
  179. * Method to override
  180. */
  181. public function set( $key, $value ) {
  182. if ( !is_array( $key ) ) {
  183. $this->_data[$key] = $value;
  184. } else {
  185. // set extension's key
  186. $key0 = $key[0];
  187. $key1 = $key[1];
  188. if ( !isset( $this->_data[$key0] ) || !is_array( $this->_data[$key0] ) )
  189. $this->_data[$key0] = array();
  190. $this->_data[$key0][$key1] = $value;
  191. }
  192. return $value;
  193. }
  194. /**
  195. * Check if we are in preview mode
  196. */
  197. public function is_preview() {
  198. return $this->_preview;
  199. }
  200. /**
  201. * Returns true if we edit master config
  202. */
  203. public function is_master() {
  204. return $this->_is_master;
  205. }
  206. public function is_compiled() {
  207. return $this->_compiled;
  208. }
  209. /**
  210. * Sets default values
  211. */
  212. public function set_defaults() {
  213. $c = new ConfigCompiler( $this->_blog_id, $this->_preview );
  214. $this->_data = $c->get_data();
  215. }
  216. /**
  217. * Saves modified config
  218. */
  219. public function save() {
  220. if ( function_exists( 'do_action' ) )
  221. do_action( 'w3tc_config_save', $this );
  222. $c = new ConfigCompiler( $this->_blog_id, $this->_preview );
  223. $c->apply_data( $this->_data );
  224. $c->save();
  225. }
  226. public function is_sealed( $key ) {
  227. if ( $this->is_master() )
  228. return false;
  229. // better to use master config data here, but
  230. // its faster and preciese enough for UI
  231. return ConfigCompiler::child_key_sealed( $key, $this->_data,
  232. $this->_data );
  233. }
  234. /**
  235. * Exports config content
  236. */
  237. public function export() {
  238. if ( defined( 'JSON_PRETTY_PRINT' ) )
  239. $content = json_encode( $this->_data, JSON_PRETTY_PRINT );
  240. else
  241. $content = json_encode( $this->_data );
  242. return $content;
  243. }
  244. /**
  245. * Imports config content
  246. */
  247. public function import( $filename ) {
  248. if ( file_exists( $filename ) && is_readable( $filename ) ) {
  249. $content = file_get_contents( $filename );
  250. if ( substr( $content, 0, 14 ) == '<?php exit; ?>' ) {
  251. $content = substr( $content, 14 );
  252. }
  253. $data = @json_decode( $content, true );
  254. if ( is_array( $data ) ) {
  255. if ( !isset( $data['version'] ) || $data['version'] != W3TC_VERSION ) {
  256. $c = new ConfigCompiler( $this->_blog_id, false );
  257. $c->load( $data );
  258. $data = $c->get_data();
  259. }
  260. foreach ( $data as $key => $value )
  261. $this->set( $key, $value );
  262. return true;
  263. }
  264. }
  265. return false;
  266. }
  267. public function get_md5() {
  268. if ( is_null( $this->_md5 ) )
  269. $this->_md5 = substr( md5( serialize( $this->_data ) ), 20 );
  270. return $this->_md5;
  271. }
  272. /**
  273. * Loads config.
  274. * In a case it finds out config files are of older version - uses slower
  275. * loader which takes all possible bloglevel-overloads into account
  276. * correctly
  277. */
  278. public function load() {
  279. $data = Config::util_array_from_storage( 0, $this->_preview );
  280. // config file assumed is not up to date, use slow version
  281. if ( !isset( $data['version'] ) || $data['version'] != W3TC_VERSION )
  282. return $this->load_full();
  283. if ( !$this->is_master() ) {
  284. $child_data = Config::util_array_from_storage( $this->_blog_id,
  285. $this->_preview );
  286. if ( !is_null( $child_data ) ) {
  287. if ( !isset( $data['version'] ) || $data['version'] != W3TC_VERSION )
  288. return $this->load_full();
  289. foreach ( $child_data as $key => $value )
  290. $data[$key] = $value;
  291. }
  292. }
  293. $this->_data = $data;
  294. $this->_compiled = false;
  295. }
  296. /**
  297. * Slower version of loader, used when configs belong to older w3tc version
  298. */
  299. private function load_full() {
  300. $c = new ConfigCompiler( $this->_blog_id, $this->_preview );
  301. $c->load();
  302. $this->_data = $c->get_data();
  303. $this->_compiled = true;
  304. }
  305. }