INI.class.php5 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. class INI {
  3. private $file;
  4. static private $instances;
  5. private $data;
  6. private function __construct($file) {
  7. $this->file = $file;
  8. $this->data = parse_ini_file($file, true);
  9. }
  10. static public function & getInstance($file) {
  11. if(! isset(self::$instances[$file])) {
  12. self::$instances[$file] = new INI($file);
  13. }
  14. return self::$instances[$file];
  15. }
  16. public function getValue($p_section,$p_key) {
  17. if ((is_array($this->data)) &&
  18. (array_key_exists($p_section, $this->data)) &&
  19. (array_key_exists($p_key, $this->data[$p_section]))) {
  20. return $this->data[$p_section][$p_key];
  21. }
  22. }
  23. public function getSections() {
  24. return array_keys($this->data);
  25. }
  26. public function getKeys($p_section) {
  27. if(array_key_exists($p_section,$this->data)) {
  28. return array_keys($this->data[$p_section]);
  29. }
  30. }
  31. public function setValue($sSection, $sKey, $sValue) {
  32. $sFileHandle = fopen($this->file, "r");
  33. if (!$sFileHandle) {
  34. Log::error(__FILE__,__LINE__,"Unable to open ".$this->file.".");
  35. }
  36. if (filesize($this->file) > 0) {
  37. $sFileContents = fread($sFileHandle, filesize($this->file));
  38. }
  39. if ($sFileContents === FALSE) {
  40. Log::error(__FILE__,__LINE__,"Unable to read data from ".$this->file.".");
  41. }
  42. $sFileData = preg_split("/\r?\n/", $sFileContents);
  43. $sectionStart = -1;
  44. $sectionEnd = -1;
  45. $returnLine = -1;
  46. for ($i = 0; $i < count($sFileData); $i++) {
  47. $x = strpos($sFileData[$i], "[".$sSection."]");
  48. $y = strpos($sFileData[$i], "[");
  49. if ($y == 0 && $y !== FALSE && $sectionStart >= 0 && $sectionEnd == -1) {
  50. $sectionEnd = ($i - 1);
  51. }
  52. $z = explode("=", $sFileData[$i]);
  53. if (count($z) >= 2) {
  54. if ($z[0] == $sKey && $sectionStart >= 0 && $sectionEnd == -1 && substr($sFileData[$i], 0, 1) != ";") {
  55. $returnLine = $i;
  56. }
  57. }
  58. if ($x == 0 && $x !== FALSE && $sectionStart == -1) {
  59. $sectionStart = ($i + 1);
  60. }
  61. }
  62. if ($sectionEnd == -1) {
  63. $sectionEnd = (count($sFileData)-1);
  64. }
  65. $newfile = "";
  66. if ($returnLine > -1) {
  67. $sFileData[$returnLine] = $sKey."=".$sValue;
  68. for ($i = 0; $i < count($sFileData); $i++) {
  69. $newfile .= $sFileData[$i]."\r\n";
  70. }
  71. }
  72. else {
  73. if ($sectionStart > -1) {
  74. for ($i = 0; $i < count($sFileData); $i++) {
  75. if ($i == $sectionEnd) {
  76. $newfile .= $sKey."=".$sValue."\r\n";
  77. }
  78. $newfile .= $sFileData[$i]."\r\n";
  79. }
  80. }
  81. else {
  82. for ($i = 0; $i < count($sFileData); $i++) {
  83. $newfile .= $sFileData[$i]."\r\n";
  84. }
  85. $newfile .= "[".$sSection."]\r\n".$sKey."=".$sValue."\r\n";
  86. }
  87. }
  88. fclose($sFileHandle);
  89. $sFileHandle = fopen($this->file, "w");
  90. if (!$sFileHandle) {
  91. Log::error(__FILE__,__LINE__,"Unable to open file ".$this->file." with write access.");
  92. }
  93. $sFileWrite = fwrite($sFileHandle, trim($newfile));
  94. if ($sFileWrite === FALSE) {
  95. Log::error(__FILE__,__LINE__,"Unable to write data to file ".$this->file.".");
  96. }
  97. fclose($sFileHandle);
  98. $this->data[$sSection][$sKey] = $sValue;
  99. }
  100. }
  101. ?>