PageRenderTime 27ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

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

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