Back to index

php5  5.3.10
open_basedir.inc
Go to the documentation of this file.
00001 <?php
00002 
00003 // This file contains helper functions for testing open_basedir configuration
00004 // Care must be taken with where the directories are created because different
00005 // SAPIs set the working directory differently. So simply creating a directory
00006 // relative to the current working directory like this: mkdir("blah") might 
00007 // actually create it in several different places depending on the SAPI..!
00008 //
00009 // Note also depending on the version of php being tested, so the open_basedir
00010 // configuration may or may not be changeable from a script (PHP_INI_SYSTEM).
00011 //
00012 // For this reason we set the open_basedir to . (current directory) and then
00013 // move around to various directories for testing using chdir(). This is NOT
00014 // recommended for production use as . bypasses all semblence of security..!
00015 //
00016 // Although safe mode has been removed in php 6.0, open_basedir is still valid.
00017 //      See http://www.php.net/features.safe-mode for more information
00018 
00019 function recursive_delete_directory($directory) {
00020 
00021     // Remove any trailing slash first
00022     if (substr($directory, -1) == '/') {
00023         $directory = substr($directory, 0, -1);
00024     }
00025 
00026     // Make sure the directory is valid
00027     if (is_dir($directory) == FALSE) {
00028         return FALSE;
00029     } 
00030 
00031     // Check we can access the directory
00032     if (is_readable($directory) == FALSE) {
00033         return FALSE;
00034     }
00035 
00036     $handle = opendir($directory);
00037 
00038     // Scan through the directory contents
00039     while (FALSE !== ($item = readdir($handle))) {
00040         if ($item != '.') {
00041              if ($item != '..') {
00042                 $path = ($directory.'/'.$item);
00043                 if (is_dir($path) == TRUE) {
00044                     recursive_delete_directory($path);
00045                 } else {
00046                                    @chmod($path, 0777);
00047                     unlink($path);
00048                 }
00049             }
00050         }
00051     }
00052 
00053     closedir($handle);
00054        @chmod($directory, 0777);
00055     rmdir($directory);
00056 
00057     return TRUE;
00058 }
00059 
00060 function create_directories() {
00061     delete_directories();
00062     $directory = getcwd();
00063 
00064     var_dump(mkdir($directory."/test"));
00065     var_dump(mkdir($directory."/test/ok"));
00066     var_dump(mkdir($directory."/test/bad"));
00067     file_put_contents($directory."/test/ok/ok.txt", "Hello World!");
00068     file_put_contents($directory."/test/bad/bad.txt", "Hello World!");
00069 }
00070 
00071 function delete_directories() {
00072     $directory = (getcwd()."/test");
00073     recursive_delete_directory($directory);
00074 }
00075 
00076 function test_open_basedir_error($function) {
00077     global $savedDirectory;
00078     var_dump($function("../bad"));
00079     var_dump($function("../bad/bad.txt"));
00080     var_dump($function(".."));
00081     var_dump($function("../"));
00082     var_dump($function("/"));
00083     var_dump($function("../bad/."));
00084     $directory = $savedDirectory;
00085     var_dump($function($directory."/test/bad/bad.txt"));
00086     var_dump($function($directory."/test/bad/../bad/bad.txt"));
00087 }
00088 
00089 function test_open_basedir_before($function, $change = TRUE) {
00090     global $savedDirectory;
00091     echo "*** Testing open_basedir configuration [$function] ***\n";
00092     $directory = getcwd();
00093     $savedDirectory = $directory;
00094     var_dump(chdir($directory));
00095     create_directories();
00096 
00097     // Optionally change directory
00098     if ($change == TRUE) {
00099         var_dump(chdir($directory."/test/ok"));
00100     }
00101 }
00102 
00103 // Delete directories using a --CLEAN-- section!
00104 function test_open_basedir_after($function) {
00105     echo "*** Finished testing open_basedir configuration [$function] ***\n";
00106 }
00107 
00108 // This is used by functions that return an array on success
00109 function test_open_basedir_array($function) {
00110     global $savedDirectory;
00111 
00112     test_open_basedir_before($function);
00113     test_open_basedir_error($function); 
00114     var_dump(is_array($function("./../.")));
00115     var_dump(is_array($function("../ok")));
00116     var_dump(is_array($function("ok.txt")));
00117     var_dump(is_array($function("../ok/ok.txt")));
00118     $directory = $savedDirectory;
00119     var_dump(is_array($function($directory."/test/ok/ok.txt")));
00120     var_dump(is_array($function($directory."/test/ok/../ok/ok.txt")));
00121     test_open_basedir_after($function);
00122 }
00123 
00124 function test_open_basedir($function) {
00125     global $savedDirectory;
00126     test_open_basedir_before($function);
00127     test_open_basedir_error($function);     
00128     var_dump($function("./../."));
00129     var_dump($function("../ok"));
00130     var_dump($function("ok.txt"));
00131     var_dump($function("../ok/ok.txt"));
00132     $directory = $savedDirectory;
00133     var_dump($function($directory."/test/ok/ok.txt"));
00134     var_dump($function($directory."/test/ok/../ok/ok.txt"));
00135     test_open_basedir_after($function);
00136 }
00137 
00138 ?>
00139