class.ini.file.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. # ***** BEGIN LICENSE BLOCK *****
  3. # This file is part of DotClear.
  4. # Copyright (c) 2004 Olivier Meunier and contributors. All rights
  5. # reserved.
  6. #
  7. # DotClear is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # DotClear is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with DotClear; if not, write to the Free Software
  19. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. #
  21. # ***** END LICENSE BLOCK *****
  22. class iniFile
  23. {
  24. var $content;
  25. var $var_reg = '/^[\s]*(%s)[\s]*?=[\s*](.*)$/m';
  26. function iniFile($file)
  27. {
  28. if (file_exists($file)) {
  29. $this->file = $file;
  30. $this->content = implode('',file($file));
  31. } else {
  32. $this->file = false;
  33. }
  34. }
  35. /* édition d'une variable */
  36. function editVar($name,$value)
  37. {
  38. if ($this->file !== false)
  39. {
  40. $match = sprintf($this->var_reg,preg_quote($name));
  41. if (preg_match($match,$this->content))
  42. {
  43. $replace = '$1 = '.$value;
  44. $this->content = preg_replace($match,$replace,$this->content);
  45. }
  46. else
  47. {
  48. $this->createVar($name,$value);
  49. }
  50. }
  51. }
  52. /* création d'un variable */
  53. function createVar($name,$value,$comment='')
  54. {
  55. $match = sprintf($this->var_reg,preg_quote($name));
  56. if ($comment != '') {
  57. $comment = '; '.str_replace("\n","\n; ",$comment)."\n";
  58. }
  59. if (!preg_match($match,$this->content)) {
  60. $this->content .= "\n\n".$comment.$name.' = '.$value;
  61. }
  62. }
  63. /* sauvegarde du fichier */
  64. function saveFile()
  65. {
  66. if (($fp = @fopen($this->file,'w')) !== false) {
  67. if (@fwrite($fp,$this->content,strlen($this->content)) !== false) {
  68. $res = true;
  69. } else {
  70. $res = false;
  71. }
  72. fclose($fp);
  73. return $res;
  74. } else {
  75. return false;
  76. }
  77. }
  78. /*
  79. Static method that puts ini vars in constants or returns array
  80. */
  81. function read($file,$return=false)
  82. {
  83. if (!file_exists($file)) {
  84. trigger_error('No config file',E_USER_ERROR);
  85. exit;
  86. }
  87. $f = file($file);
  88. if ($return) {
  89. $res = array();
  90. }
  91. foreach ($f as $v)
  92. {
  93. $v = trim ($v);
  94. if (substr($v,0,1) != ';' && $v != '') {
  95. $p = strpos($v,'=');
  96. $K = (string) trim(substr($v,0,$p));
  97. $V = (string) trim(substr($v,($p+1)));
  98. if (substr($V,0,1) == '"' && substr($V,-1) == '"') {
  99. $V = substr(substr($V,1),0,-1);
  100. }
  101. if ($V === 'yes' || $V === 'true' || $V === '1') {
  102. $V = true;
  103. }
  104. if ($V === 'no' || $V === 'false' || $V === '0') {
  105. $V = false;
  106. }
  107. if ($return) {
  108. $res[$K] = $V;
  109. } elseif (!defined($K)) {
  110. define($K,$V);
  111. }
  112. }
  113. }
  114. if ($return) {
  115. return $res;
  116. }
  117. }
  118. }
  119. ?>