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

Размер файла: 1.92Kb
  1. <?php
  2. if (!defined('_BR_'))
  3. define('_BR_',chr(13).chr(10));
  4. class ini {
  5. public $filename;
  6. public $arr;
  7. function __construct($file = false){
  8. if ($file)
  9. {
  10. if (!file_exists($file))
  11. file_put_contents($file, '');
  12. $this->loadFromFile($file);
  13. }
  14. }
  15. function initArray(){
  16. $this->arr = parse_ini_file($this->filename);
  17. }
  18. function loadFromFile($file){
  19. $result = true;
  20. $this->filename = $file;
  21. if (file_exists($file) && is_readable($file)){
  22. $this->initArray();
  23. }
  24. else
  25. $result = false;
  26. return $result;
  27. }
  28. function read($section, $key, $def = ''){
  29. if (isset($this->arr[$section][$key])){
  30. return $this->arr[$section][$key];
  31. } else
  32. return $def;
  33. }
  34. function write($key, $value){
  35. if (is_bool($value))
  36. $value = $value ? 1 : 0;
  37. $this->arr[$key] = $value;
  38. }
  39. function eraseSection($section){
  40. if (isset($this->arr[$section]))
  41. unset($this->arr[$section]);
  42. }
  43. function deleteKey($section, $key){
  44. if (isset($this->arr[$section][$key]))
  45. unset($this->arr[$section][$key]);
  46. }
  47. function readSections(&$array){
  48. $array = array_keys($this->arr);
  49. return $array;
  50. }
  51. function readKeys($section, &$array){
  52. if (isset($this->arr[$section])){
  53. $array = array_keys($this->arr[$section]);
  54. return $array;
  55. }
  56. return array();
  57. }
  58. function updateFile(){
  59. $result = '';
  60. foreach ($this->arr as $key=>$value){
  61. $result .= $key .'='.$value . PHP_EOL;
  62. }
  63. $result .= PHP_EOL;
  64. file_put_contents($this->filename, $result);
  65. return true;
  66. }
  67. function __destruct(){
  68. $this->updateFile();
  69. }
  70. }