open_basedir.inc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. // This file contains helper functions for testing open_basedir configuration
  3. // Care must be taken with where the directories are created because different
  4. // SAPIs set the working directory differently. So simply creating a directory
  5. // relative to the current working directory like this: mkdir("blah") might
  6. // actually create it in several different places depending on the SAPI..!
  7. //
  8. // Note also depending on the version of php being tested, so the open_basedir
  9. // configuration may or may not be changeable from a script (PHP_INI_SYSTEM).
  10. //
  11. // For this reason we set the open_basedir to . (current directory) and then
  12. // move around to various directories for testing using chdir(). This is NOT
  13. // recommended for production use as . bypasses all semblances of security..!
  14. //
  15. // Although safe mode has been removed in php 6.0, open_basedir is still valid.
  16. // See http://www.php.net/features.safe-mode for more information
  17. function recursive_delete_directory($directory) {
  18. // Remove any trailing slash first
  19. if (substr($directory, -1) == '/') {
  20. $directory = substr($directory, 0, -1);
  21. }
  22. // Make sure the directory is valid
  23. if (is_dir($directory) == FALSE) {
  24. return FALSE;
  25. }
  26. // Check we can access the directory
  27. if (is_readable($directory) == FALSE) {
  28. return FALSE;
  29. }
  30. $handle = opendir($directory);
  31. // Scan through the directory contents
  32. while (FALSE !== ($item = readdir($handle))) {
  33. if ($item != '.') {
  34. if ($item != '..') {
  35. $path = ($directory.'/'.$item);
  36. if (is_dir($path) == TRUE) {
  37. recursive_delete_directory($path);
  38. } else {
  39. @chmod($path, 0777);
  40. unlink($path);
  41. }
  42. }
  43. }
  44. }
  45. closedir($handle);
  46. @chmod($directory, 0777);
  47. rmdir($directory);
  48. return TRUE;
  49. }
  50. function create_directories() {
  51. delete_directories();
  52. $directory = getcwd();
  53. var_dump(mkdir($directory."/test"));
  54. var_dump(mkdir($directory."/test/ok"));
  55. var_dump(mkdir($directory."/test/bad"));
  56. file_put_contents($directory."/test/ok/ok.txt", "Hello World!");
  57. file_put_contents($directory."/test/bad/bad.txt", "Hello World!");
  58. }
  59. function delete_directories() {
  60. $directory = (getcwd()."/test");
  61. recursive_delete_directory($directory);
  62. }
  63. function test_open_basedir_error($function) {
  64. global $savedDirectory;
  65. var_dump($function("../bad"));
  66. var_dump($function("../bad/bad.txt"));
  67. var_dump($function(".."));
  68. var_dump($function("../"));
  69. var_dump($function("/"));
  70. var_dump($function("../bad/."));
  71. $directory = $savedDirectory;
  72. var_dump($function($directory."/test/bad/bad.txt"));
  73. var_dump($function($directory."/test/bad/../bad/bad.txt"));
  74. }
  75. function test_open_basedir_before($function, $change = TRUE) {
  76. global $savedDirectory;
  77. echo "*** Testing open_basedir configuration [$function] ***\n";
  78. $directory = getcwd();
  79. $savedDirectory = $directory;
  80. var_dump(chdir($directory));
  81. create_directories();
  82. // Optionally change directory
  83. if ($change == TRUE) {
  84. var_dump(chdir($directory."/test/ok"));
  85. }
  86. }
  87. // Delete directories using a --CLEAN-- section!
  88. function test_open_basedir_after($function) {
  89. echo "*** Finished testing open_basedir configuration [$function] ***\n";
  90. }
  91. // This is used by functions that return an array on success
  92. function test_open_basedir_array($function) {
  93. global $savedDirectory;
  94. test_open_basedir_before($function);
  95. test_open_basedir_error($function);
  96. var_dump(is_array($function("./../.")));
  97. var_dump(is_array($function("../ok")));
  98. var_dump(is_array($function("ok.txt")));
  99. var_dump(is_array($function("../ok/ok.txt")));
  100. $directory = $savedDirectory;
  101. var_dump(is_array($function($directory."/test/ok/ok.txt")));
  102. var_dump(is_array($function($directory."/test/ok/../ok/ok.txt")));
  103. test_open_basedir_after($function);
  104. }
  105. function test_open_basedir($function) {
  106. global $savedDirectory;
  107. test_open_basedir_before($function);
  108. test_open_basedir_error($function);
  109. var_dump($function("./../."));
  110. var_dump($function("../ok"));
  111. var_dump($function("ok.txt"));
  112. var_dump($function("../ok/ok.txt"));
  113. $directory = $savedDirectory;
  114. var_dump($function($directory."/test/ok/ok.txt"));
  115. var_dump($function($directory."/test/ok/../ok/ok.txt"));
  116. test_open_basedir_after($function);
  117. }
  118. ?>