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

https://gitlab.com/juanito.abelo/nlmobile · PHP · 176 lines · 80 code · 23 blank · 73 comment · 15 complexity · 22aa08f10ec5dcc2403e73b9cc634802 MD5 · raw file

  1. <?php
  2. /**
  3. * Class W3_ConfigData
  4. */
  5. class W3_ConfigData {
  6. /*
  7. * Normalized data
  8. * @var array
  9. */
  10. public $data = array();
  11. /*
  12. * Array of config keys descriptors. In a format of
  13. * <key> => array('type' => <key type>, 'default' => <default value>)
  14. *
  15. * @var array
  16. */
  17. private $_keys;
  18. /*
  19. * Maps http key to options key.
  20. * Fixes problem when php replaces 'my.super_option' to 'my_super_option'
  21. * <http name> => <config name>
  22. *
  23. * @var array
  24. */
  25. private $_http_keys_map;
  26. /**
  27. * Constructor
  28. */
  29. function __construct($keys) {
  30. $this->data = array('version' => W3TC_VERSION);
  31. $this->_keys = $keys;
  32. $this->_http_keys_map = array();
  33. foreach (array_keys($keys) as $key) {
  34. $http_key = str_replace('.', '_', $key);
  35. $this->_http_keys_map[$http_key] = $key;
  36. // add also non-escaped key
  37. $this->_http_keys_map[$key] = $key;
  38. }
  39. }
  40. /*
  41. * Converts configuration key returned in http _GET/_POST
  42. * to configuration key
  43. *
  44. * @param $http_key string
  45. * @return string
  46. */
  47. function resolve_http_key($http_key) {
  48. if (!isset($this->_http_keys_map[$http_key]))
  49. return null;
  50. return $this->_http_keys_map[$http_key];
  51. }
  52. /*
  53. * Removes data
  54. */
  55. function clear() {
  56. $this->data = array('version' => W3TC_VERSION);
  57. }
  58. /**
  59. * Sets config value
  60. *
  61. * @param string $key
  62. * @param string $value
  63. * @return mixed value set
  64. */
  65. function set($key, $value) {
  66. if (!array_key_exists($key, $this->_keys))
  67. return null;
  68. $type = $this->_keys[$key]['type'];
  69. if (!($type == 'array' && is_string($value)))
  70. settype($value, $type);
  71. else {
  72. $value = str_replace("\r\n", "\n", $value);
  73. $value = explode("\n", $value);
  74. }
  75. $this->data[$key] = $value;
  76. return $value;
  77. }
  78. /**
  79. * Sets default values
  80. */
  81. function set_defaults() {
  82. foreach ($this->_keys as $key => $value)
  83. $this->data[$key] = $value['default'];
  84. }
  85. /**
  86. * Sets group of keys
  87. *
  88. * @param $data array
  89. */
  90. function set_group($data) {
  91. foreach ($data as $key => $value)
  92. $this->set($key, $value);
  93. }
  94. /**
  95. * Reads config from file and returns it's content as array (or null)
  96. *
  97. * @param string $filename
  98. * @param bool $unserialize
  99. * @return array or null
  100. */
  101. static function get_array_from_file($filename, $unserialize = false) {
  102. if (file_exists($filename) && is_readable($filename)) {
  103. // include errors not hidden by @ since they still terminate
  104. // process (code not functional), but hides reason why
  105. if ($unserialize) {
  106. $content = file_get_contents($filename);
  107. $content = substr($content, 13);
  108. $config = @unserialize($content);
  109. if (!$config)
  110. return null;
  111. } else {
  112. /** @var $filename array */
  113. // including file directly instead of read+eval causes constant
  114. // problems with APC, ZendCache, and WSOD in a case of
  115. // broken config file, still doesnt affect runtime since
  116. // config cache is used
  117. $content = @file_get_contents($filename);
  118. $config = @eval(substr($content, 5));
  119. }
  120. if (is_array($config)) {
  121. return $config;
  122. }
  123. }
  124. return null;
  125. }
  126. /**
  127. * Reads config from file using "set" method to fill object with data.
  128. *
  129. * @param string $filename
  130. * @param bool $unserialize
  131. * @return boolean
  132. */
  133. function read($filename, $unserialize = false) {
  134. $config = W3_ConfigData::get_array_from_file($filename, $unserialize);
  135. if (is_null($config))
  136. return false;
  137. foreach ($config as $key => $value)
  138. $this->set($key, $value);
  139. return true;
  140. }
  141. /**
  142. * Saves modified config
  143. */
  144. function write($filename, $serialize = false) {
  145. w3_require_once(W3TC_INC_DIR . '/functions/file.php');
  146. if ($serialize) {
  147. $config = '<?php exit;?>' . serialize($this->data);
  148. } else {
  149. $config = w3tc_format_data_as_settings_file($this->data);
  150. }
  151. w3_file_put_contents_atomic($filename, $config);
  152. }
  153. }