Configuration.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. declare(strict_types=1);
  3. namespace netico\Bones;
  4. /**
  5. * The Configuration class retrieves configuration variables from an .ini file.
  6. *
  7. * It requires the path to the configuration file.
  8. * As for security, keeping the configuration file out of the web root is the first step.
  9. * Making it read-only and restricting access may make it more secure.
  10. * The .ini files have a trivial syntax so they are easy to edit and understand.
  11. * Also it is the syntax of PHP configuration files.
  12. *
  13. * [application]
  14. * name=foo
  15. *
  16. * [template]
  17. * name=bar
  18. *
  19. * [sqlite]
  20. * database=/path/to/file.sqlite
  21. *
  22. * And then:
  23. *
  24. * $c = new \netico\Bones\Configuration();
  25. * $name = $c->getValue('application', 'name');
  26. * print $name;
  27. *
  28. * @package bones
  29. * @link https://git.lattuga.net/netico/code-library/src/master/Framework
  30. * @copyright Copyright (c) 2016, 2022 netico <netico@riseup.net>
  31. * @license https://www.gnu.org/licenses/gpl-3.0.en.html GNU General Public License
  32. * @author netico <netico@riseup.net>
  33. *
  34. */
  35. class Configuration
  36. {
  37. /**
  38. * @var string[] $config Array of string objects. It represents the content of the INI file.
  39. */
  40. private $config;
  41. /**
  42. * This code works if you set the CONFIG constant.
  43. * @return void
  44. */
  45. public function __construct()
  46. {
  47. $this->config = $this->parser(CONFIG);
  48. }
  49. /**
  50. * parser
  51. *
  52. * @param string $filename path of INI config file.
  53. * @return string[] An array of string objects.
  54. */
  55. private function parser($filename)
  56. {
  57. if (is_readable($filename)) {
  58. $res = array();
  59. $section = "";
  60. $fdo = fopen($filename, "r");
  61. while (feof($fdo) != true) {
  62. $fc = fgets($fdo, 4096);
  63. if ($fc != false) {
  64. $line = trim($fc);
  65. $len = strlen($line);
  66. if ($len != 0 && $line[0] != ';' && $line[0] === '[' && $line[$len - 1] === ']') {
  67. $section = substr($line, 1, $len - 2);
  68. }
  69. $pos = strpos($line, '=');
  70. if ($pos > 0 && $section != "") {
  71. $name = trim(substr($line, 0, $pos));
  72. $value = trim(substr($line, $pos + 1, $len - $pos - 1));
  73. $value = str_replace('"', '\\"', $value);
  74. $value = str_replace("\"", '\\\'', $value);
  75. $res["$section"]["$name"] = $value;
  76. }
  77. }
  78. }
  79. fclose($fdo);
  80. }
  81. return $res;
  82. }
  83. /**
  84. * This method gets a configuration option.
  85. *
  86. * @param string $section Section in INI file (for example: '[section]').
  87. * @param string $name Key in file INI (for example: 'key=').
  88. * @return string Value (for example: '=value').
  89. */
  90. public function getValue($section, $name)
  91. {
  92. foreach ($this->config as $i => $a) {
  93. $b = array();
  94. $b = (array) $a;
  95. foreach ($b as $n => $v) {
  96. if (strcmp($i, $section) == 0 && strcmp($n, $name) == 0) {
  97. return (string) $v;
  98. }
  99. }
  100. }
  101. return (string) 'Undefined';
  102. }
  103. }