config_helper.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. <?php
  2. /**
  3. * searches for a config-file in the current and parent directories until found.
  4. * @return path to found config file, or FALSE otherwise.
  5. */
  6. function find_config($filename='config.php') {
  7. // Count the deph of the current directory, so we know how far we can go up.
  8. $path_length = substr_count(getcwd(), DIRECTORY_SEPARATOR)
  9. + 1; // also search the current directory
  10. $dir = '.'; // updated in each loop
  11. for ($i=0; $i<$path_length;$i++) {
  12. $config_filename = $dir . DIRECTORY_SEPARATOR . $filename;
  13. if (file_exists($config_filename)) {
  14. return $config_filename;
  15. } else {
  16. $dir = '../' . $dir;
  17. }
  18. }
  19. return false;
  20. }
  21. /**
  22. * searches and loads the config file. Prints an error if not found.
  23. */
  24. function load_config() {
  25. global $config;
  26. $file = find_config();
  27. if ($file !== false) {
  28. require_once($file);
  29. if (!isset($config) || !is_array($config)) {
  30. die('ERROR: Config file is invalid. Please see the installation instructions in the README.md');
  31. }
  32. } else {
  33. die('ERROR: Config file not found. Please see the installation instructions in the README.md');
  34. }
  35. }