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

/Quản lý website mạng xã hội giống twitter PHP/upload/libraries/SConfig.php

https://gitlab.com/phamngsinh/baitaplon_sinhvien
PHP | 195 lines | 175 code | 20 blank | 0 comment | 36 complexity | 6167f132dce527043d0677ce2ce0f833 MD5 | raw file
  1. <?php
  2. define ("CONF_COMMENT", "#");
  3. class SConfig {
  4. var $_def_file_name;
  5. var $_filename;
  6. var $_SConfig;
  7. var $_errors;
  8. function SConfig() {
  9. global $SConfig;
  10. if (!isset($SConfig)) $SConfig = $this;
  11. $SConfig->_init();
  12. }
  13. function create() {
  14. return new SConfig();
  15. }
  16. function _init () {
  17. global $config;
  18. $this->_def_file_name = $config[CONF_FILE];
  19. $filename = $this->_def_file_name;
  20. $this->_filename = $filename;
  21. if (!$file = $this->_read_file()) return false;
  22. $ar = explode("\n", $file);
  23. foreach ($ar as $item) {
  24. if (eregi("\[.*\]", $item)) {
  25. $section = trim(eregi_replace("\[|\]", "", $item));
  26. } else {
  27. $key = trim(substr($item, 0, strpos($item, "=")));
  28. $value = trim(substr($item, strpos($item, "=")+1));
  29. if ($key and !eregi("[[:space:]]*".CONF_COMMENT, $item)) {
  30. $res[$section][$key] = $value;
  31. }
  32. }
  33. }
  34. $this->_SConfig = $res;
  35. }
  36. function _check_SConfig_loaded () {
  37. if (isset($this->_SConfig)) {
  38. return true;
  39. } else {
  40. SError::add("Configuration file has not been loaded");
  41. return false;
  42. }
  43. }
  44. function get ($section, $key) {
  45. global $SConfig;
  46. if (!isset($SConfig)) {
  47. SConfig::create();
  48. }
  49. if (!$SConfig->_check_SConfig_loaded()) {
  50. return false;
  51. }
  52. return $SConfig->_SConfig[$section][$key];
  53. }
  54. function getSection ($section) {
  55. global $SConfig;
  56. if (!isset($SConfig)) {
  57. SConfig::create();
  58. }
  59. if (!$SConfig->_check_SConfig_loaded()) {
  60. return false;
  61. }
  62. if (!isset($SConfig->_SConfig[$section])) {
  63. SError::add("Section $section does not exist");
  64. return false;
  65. }
  66. return $SConfig->_SConfig[$section];
  67. }
  68. function getAll () {
  69. global $SConfig;
  70. if (!isset($SConfig)) {
  71. SConfig::create();
  72. }
  73. if (!$SConfig->_check_SConfig_loaded()) {
  74. return false;
  75. }
  76. return $SConfig->_SConfig;
  77. }
  78. function checkSection ($section) {
  79. global $SConfig;
  80. if (!isset($SConfig)) {
  81. SConfig::create();
  82. }
  83. if (!$SConfig->_check_SConfig_loaded()) {
  84. return false;
  85. }
  86. return (isset($SConfig->_SConfig[$section]));
  87. }
  88. function set ($section, $key, $value, $create = true, $is_write = true) {
  89. global $SConfig;
  90. if (!isset($SConfig)) {
  91. SConfig::create();
  92. }
  93. if (!$SConfig->_check_SConfig_loaded()) {
  94. return false;
  95. }
  96. if (!$SConfig->checkSection($section)) {
  97. SError::add("Invalid section name \"$section\"");
  98. return false;
  99. }
  100. if (isset($this->_SConfig[$section][$key])) {
  101. $SConfig->_SConfig[$section][$key] = $value;
  102. } else {
  103. if ($create) {
  104. $SConfig->_SConfig[$section][$key] = $value;
  105. } else {
  106. SError::add("Unable to create new key");
  107. return false;
  108. }
  109. }
  110. if ($is_write) {
  111. $SConfig->_write();
  112. }
  113. return true;
  114. }
  115. function write ($filename = null) {
  116. global $SConfig;
  117. if (!isset($SConfig)) {
  118. SConfig::create();
  119. }
  120. if (!$SConfig->_check_SConfig_loaded()) {
  121. return false;
  122. }
  123. if (!$filename) {
  124. $filename = $SConfig->_filename;
  125. }
  126. $out = "";
  127. foreach ($this->_SConfig as $section => $values) {
  128. $out .= "[$section]\n";
  129. foreach ($values as $key => $value) {
  130. $out .= "$key = $value\n";
  131. }
  132. }
  133. if (is_writable($filename)) {
  134. if (!$fp = fopen($filename, 'w')) {
  135. SError::add("Cannot open file ($filename)");
  136. return false;
  137. }
  138. if (!fwrite($fp, $out)) {
  139. SError::add("Cannot write to file ($filename).");
  140. return false;
  141. }
  142. fclose($fp);
  143. } else {
  144. SError::add("The file $filename is not writable.");
  145. return false;
  146. }
  147. return true;
  148. }
  149. function _read_file () {
  150. global $SConfig;
  151. if (!isset($SConfig)) {
  152. SConfig::create();
  153. }
  154. if (!file_exists($SConfig->_filename)) {
  155. SError::add("file ".$SConfig->_filename." does not exists.");
  156. return false;
  157. }
  158. $fd = fopen ($SConfig->_filename, "r");
  159. if (!$fd) {
  160. SError::add("error opening file for reading");
  161. return false;
  162. }
  163. $buffer = "";
  164. while (!feof ($fd)) {
  165. $buffer .= fgets($fd, 4096);
  166. }
  167. fclose ($fd);
  168. $buffer = eregi_replace("\n\n", "\n", $buffer);
  169. return $buffer;
  170. }
  171. function getErrors () {
  172. return SError::getErrors();
  173. }
  174. }
  175. ?>