Просмотр файла system/inc/classes/ini.class.php

Размер файла: 4.09Kb
  1. <?php
  2. class INI {
  3. /**
  4. * WRITE
  5. */
  6. static function write($filename, $ini) {
  7. $string = '';
  8. foreach(array_keys($ini) as $key) {
  9. $string .= '['.$key."]\n";
  10. $string .= INI::write_get_string($ini[$key], '')."\n";
  11. }
  12. file_put_contents($filename, $string);
  13. }
  14. /**
  15. * write get string
  16. */
  17. static function write_get_string(& $ini, $prefix) {
  18. $string = '';
  19. ksort($ini);
  20. foreach($ini as $key => $val) {
  21. if (is_array($val)) {
  22. $string .= INI::write_get_string($ini[$key], $prefix.$key.'.');
  23. } else {
  24. $string .= $prefix.$key.' = '.str_replace("\n", "\\\n", INI::set_value($val))."\n";
  25. }
  26. }
  27. return $string;
  28. }
  29. /**
  30. * manage keys
  31. */
  32. static function set_value($val) {
  33. if ($val === true) { return 'true'; }
  34. else if ($val === false) { return 'false'; }
  35. return $val;
  36. }
  37. /**
  38. * READ
  39. */
  40. static function read($filename) {
  41. $ini = array();
  42. $lines = file($filename);
  43. $section = 'default';
  44. $multi = '';
  45. foreach($lines as $line) {
  46. if (substr($line, 0, 1) !== ';') {
  47. $line = str_replace("\r", "", str_replace("\n", "", $line));
  48. if (preg_match('/^\[(.*)\]/', $line, $m)) {
  49. $section = $m[1];
  50. } else if ($multi === '' && preg_match('/^([a-z0-9_.\[\]-]+)\s*=\s*(.*)$/i', $line, $m)) {
  51. $key = $m[1];
  52. $val = $m[2];
  53. if (substr($val, -1) !== "\\") {
  54. $val = trim($val);
  55. INI::manage_keys($ini[$section], $key, $val);
  56. $multi = '';
  57. } else {
  58. $multi = substr($val, 0, -1)."\n";
  59. }
  60. } else if ($multi !== '') {
  61. if (substr($line, -1) === "\\") {
  62. $multi .= substr($line, 0, -1)."\n";
  63. } else {
  64. INI::manage_keys($ini[$section], $key, $multi.$line);
  65. $multi = '';
  66. }
  67. }
  68. }
  69. }
  70. $buf = get_defined_constants(true);
  71. $consts = array();
  72. foreach($buf['user'] as $key => $val) {
  73. $consts['{'.$key.'}'] = $val;
  74. }
  75. array_walk_recursive($ini, array('INI', 'replace_consts'), $consts);
  76. return $ini;
  77. }
  78. /**
  79. * manage keys
  80. */
  81. static function get_value($val) {
  82. if (preg_match('/^-?[0-9]$/i', $val)) { return intval($val); }
  83. else if (strtolower($val) === 'true') { return true; }
  84. else if (strtolower($val) === 'false') { return false; }
  85. else if (preg_match('/^"(.*)"$/i', $val, $m)) { return $m[1]; }
  86. else if (preg_match('/^\'(.*)\'$/i', $val, $m)) { return $m[1]; }
  87. return $val;
  88. }
  89. /**
  90. * manage keys
  91. */
  92. static function get_key($val) {
  93. if (preg_match('/^[0-9]$/i', $val)) { return intval($val); }
  94. return $val;
  95. }
  96. /**
  97. * manage keys
  98. */
  99. static function manage_keys(& $ini, $key, $val) {
  100. if (preg_match('/^([a-z0-9_-]+)\.(.*)$/i', $key, $m)) {
  101. INI::manage_keys($ini[$m[1]], $m[2], $val);
  102. } else if (preg_match('/^([a-z0-9_-]+)\[(.*)\]$/i', $key, $m)) {
  103. if ($m[2] !== '') {
  104. $ini[$m[1]][INI::get_key($m[2])] = INI::get_value($val);
  105. } else {
  106. $ini[$m[1]][] = INI::get_value($val);
  107. }
  108. } else {
  109. $ini[INI::get_key($key)] = INI::get_value($val);
  110. }
  111. }
  112. /**
  113. * replace utility
  114. */
  115. static function replace_consts(& $item, $key, $consts) {
  116. if (is_string($item)) {
  117. $item = strtr($item, $consts);
  118. }
  119. }
  120. }
  121. ?>