Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.27 KB | None | 0 0
  1. <?php
  2. class confRead {
  3. private $_handler = null;
  4. private $_file = null;
  5. private $_contents = null;
  6. function __construct($file) {
  7. if($file == null && $this->_file != null) {
  8. print "Error: No File Was Defined to load.";
  9. }
  10. else {
  11. $this->_file = $file;
  12. $file = explode(".",$this->_file);
  13. if($file[1] == "conf") {
  14. if(!file_exists($this->_file)) {
  15. print "Error: File Specified Was not found.";
  16. }
  17. else {
  18. $this->openFile();
  19. }
  20. }
  21. else {
  22. print "Error: Wrong File Extension!";
  23. }
  24. }
  25. }
  26. private function openFile() {
  27. if($this->_handler == null) {
  28. $this->_handler = fopen($this->_file,"r");
  29. if(!$this->_handler) {
  30. print "Error: Couldn't Open File {$this->_file}";
  31. }
  32. else {
  33. return 5;
  34. }
  35. }
  36. else {
  37. print "Error: Please Close existing File.";
  38. }
  39. }
  40. function getValue($item,$defval=false) {
  41. if(!$this->_handler || $this->_handler == null) {
  42. print "Error: No File is Open, Please Open one.";
  43. }
  44. else {
  45. do {
  46. $this->_contents .= fgets($this->_handler,2056);
  47.  
  48. }
  49. while(!feof($this->_handler));
  50. $item2 = explode($item." = ",$this->_contents);
  51. $item2 = explode(";",$item2[1]);
  52. $item2 = $item2[0];
  53. $this->closeFile();
  54. $this->_handler = null;
  55. if($defval != false && $item2 == "") {
  56. return $defval;
  57. }
  58. else {
  59. return $item2;
  60. }
  61. }
  62. }
  63. function setValue($item,$value) {
  64. if($this->_handler == null && $this->_contents != null) {
  65. if(preg_match('/'.$item.' = (.+);/',$this->_contents)) {
  66. $this->_contents = preg_replace('/'.$item.' = (.+);/',$item.' = '.$value.';',$this->_contents);
  67. $this->_handler = fopen($this->_file,"w");
  68. if($this->_handler) {
  69. $upd = fwrite($this->_handler,$this->_contents);
  70. if(!$upd) {
  71. print "Error: File Failed to Update";
  72. } else {
  73. print "File Writen";
  74. }
  75. }
  76. }
  77. }
  78. else {
  79. print "Error: File Not Open or Contents Was Null";
  80. }
  81. }
  82. function closeFile() {
  83. if($this->_handler) {
  84. fclose($this->_handler);
  85. }
  86. }
  87. }
  88. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement