Back to index

php5  5.3.10
common.inc
Go to the documentation of this file.
00001 <?php
00002 error_reporting(E_ALL);
00003 define('PHPT_ACL_READ',  1 << 1);
00004 define('PHPT_ACL_WRITE', 1 << 2);
00005 define('PHPT_ACL_EXEC',  1 << 3);
00006 define('PHPT_ACL_NONE',  1 << 4);
00007 define('PHPT_ACL_FULL',  1 << 5);
00008 
00009 define('PHPT_ACL_GRANT',  1);
00010 define('PHPT_ACL_DENY',  2);
00011 
00012 function skipif() {
00013        if(substr(PHP_OS, 0, 3) != 'WIN' ) {
00014               die('skip windows only test');
00015        }
00016        if(stripos(php_uname(), 'XP') !== FALSE) {
00017               die('skip windows 2003 or newer only test');
00018        }
00019 }
00020 
00021 function get_username(){
00022        return getenv('USERNAME');
00023 }
00024 
00025 function get_domainname()
00026 {
00027        return getenv('USERDOMAIN');
00028 }
00029 
00030 function icacls_set($path, $mode, $perm) {
00031        $user = get_username();
00032        $path_escaped =  '"' . $path . '"';
00033        $perm_entry = array();
00034 
00035        if ($perm & PHPT_ACL_READ) $perm_entry[]  = 'R';
00036        if ($perm & PHPT_ACL_WRITE) $perm_entry[] = 'W';
00037        if ($perm & PHPT_ACL_EXEC) $perm_entry[]  = 'RX';
00038        if ($perm & PHPT_ACL_FULL) $perm_entry[]  = 'F';
00039 
00040        // Deny all
00041        $cmd = 'icacls ' . $path_escaped . ' /inheritance:r /deny ' . $user . ':(F,M,R,RX,W)';
00042        exec($cmd);
00043 
00044        if ($perm & PHPT_ACL_NONE) {
00045               /*
00046                This is required to remove all the previously denied
00047                permission for the USER. Just granting permission doesn't
00048                remove the previously denied permission.
00049               */
00050               $cmd = 'icacls ' . $path_escaped . ' /' . 'remove:d';
00051               $cmd .= ' ' . $user;
00052               exec($cmd);
00053               $cmd = 'icacls ' . $path_escaped . ' /' . 'remove:g';
00054               $cmd .= ' ' . $user;
00055               exec($cmd);
00056               return;
00057        }
00058 
00059        if ($mode == PHPT_ACL_GRANT) {
00060               $mode = 'grant';
00061        } else {
00062               $mode = 'deny';
00063        }
00064 
00065 
00066        // Deny all
00067        $cmd = 'icacls ' . $path_escaped . ' /deny ' . $user . ':(F,M,R,RX,W)';
00068        exec($cmd);
00069 
00070        /*
00071         This is required to remove all the previously denied
00072         permission for the USER. Just granting permission doesn't
00073         remove the previously denied permission.
00074        */
00075        $cmd = 'icacls ' . $path_escaped . ' /' . 'remove:d';
00076        $cmd .= ' ' . $user;
00077        exec($cmd);
00078        $cmd = 'icacls ' . $path_escaped . ' /' . 'remove:g';
00079        $cmd .= ' ' . $user;
00080        exec($cmd);
00081 
00082 
00083        /*
00084         Required to set no permission and check that is_readable()
00085         returns false. If the $perm_entry contains 'N' skip this step.
00086         This will make the file/dir with NO aceess.
00087        */
00088        if (!in_array('N', $perm_entry)) {
00089               /*
00090                This is required to remove all the previously denied
00091                permission for the USER. Just granting permission doesn't
00092                remove the previously denied permission.
00093               */
00094               $cmd = 'icacls ' . $path_escaped . ' /' . 'remove:d';
00095               $cmd .= ' ' . get_username();
00096               exec($cmd);
00097               $cmd = 'icacls ' . $path_escaped . ' /' . 'remove:g';
00098               $cmd .= ' ' . get_username();
00099               exec($cmd);
00100 
00101               $cmd = 'icacls ' . $path_escaped . ' /' . $mode;
00102               $cmd .= ' ' . get_username();
00103               $cmd .= ':' . '(' . implode($perm_entry, ',') . ')';
00104               exec($cmd);
00105        }
00106 }
00107 
00108 function create_dir($name, $perms) {
00109        if (empty($name)) {
00110               echo "create_dir: Empty name is not allowed\n";
00111               return;
00112        }
00113 
00114        mkdir($name);
00115        $dst = realpath($name);
00116        icacls_set($name, PHPT_ACL_GRANT, $perms);
00117 }
00118 
00119 function create_file($name, $perms) {
00120        if (empty($name)) {
00121               echo "create_file: Empty name is not allowed\n";
00122               return;
00123        }
00124 
00125        touch($name);
00126        icacls_set($name, PHPT_ACL_GRANT, $perms);
00127 }
00128 
00129 function delete_file($path) {
00130        icacls_set($path, PHPT_ACL_GRANT, PHPT_ACL_FULL);
00131        if (is_file($path)) {
00132               unlink($path);
00133        } else {
00134               echo "delete_file: '$path' is not a file\n";
00135               return;
00136        }
00137 }
00138 
00139 function delete_dir($path) {
00140        if (is_dir($path)) {
00141               icacls_set($path, PHPT_ACL_GRANT, PHPT_ACL_FULL);
00142               rmdir($path);
00143        } else {
00144               echo "delete_dir: '$path' is not a directory\n";
00145               return;
00146        }
00147 }
00148 if (0) {
00149 $path = __DIR__ . '/a.txt';
00150 create_file($path, PHPT_ACL_NONE);
00151 if (!is_writable($path)) {
00152        echo "PHPT_ACL_NONE success!!\n";
00153 } else {
00154        echo "PHPT_ACL_NONE failed!!\n";
00155 }
00156 delete_file($path);
00157 
00158 $path = __DIR__ . '/a.txt';
00159 create_file($path, PHPT_ACL_READ);
00160 if (!is_writable($path)) {
00161        echo "PHPT_ACL_READ success!!\n";
00162 } else {
00163        echo "PHPT_ACL_READ failed!!\n";
00164 }
00165 delete_file($path);
00166 
00167 $path = __DIR__ . '/adir';
00168 create_dir($path, PHPT_ACL_READ);
00169 if (!is_writable($path)) {
00170        echo "PHPT_ACL_READ dir success!!\n";
00171 } else {
00172        echo "PHPT_ACL_READ dir failed!!\n";
00173 }
00174 delete_dir($path);
00175 
00176 }