Annotation of embedaddon/php/ext/standard/tests/file/windows_acls/common.inc, revision 1.1.1.1
1.1 misho 1: <?php
2: error_reporting(E_ALL);
3: define('PHPT_ACL_READ', 1 << 1);
4: define('PHPT_ACL_WRITE', 1 << 2);
5: define('PHPT_ACL_EXEC', 1 << 3);
6: define('PHPT_ACL_NONE', 1 << 4);
7: define('PHPT_ACL_FULL', 1 << 5);
8:
9: define('PHPT_ACL_GRANT', 1);
10: define('PHPT_ACL_DENY', 2);
11:
12: function skipif() {
13: if(substr(PHP_OS, 0, 3) != 'WIN' ) {
14: die('skip windows only test');
15: }
16: if(stripos(php_uname(), 'XP') !== FALSE) {
17: die('skip windows 2003 or newer only test');
18: }
19: }
20:
21: function get_username(){
22: return getenv('USERNAME');
23: }
24:
25: function get_domainname()
26: {
27: return getenv('USERDOMAIN');
28: }
29:
30: function icacls_set($path, $mode, $perm) {
31: $user = get_username();
32: $path_escaped = '"' . $path . '"';
33: $perm_entry = array();
34:
35: if ($perm & PHPT_ACL_READ) $perm_entry[] = 'R';
36: if ($perm & PHPT_ACL_WRITE) $perm_entry[] = 'W';
37: if ($perm & PHPT_ACL_EXEC) $perm_entry[] = 'RX';
38: if ($perm & PHPT_ACL_FULL) $perm_entry[] = 'F';
39:
40: // Deny all
41: $cmd = 'icacls ' . $path_escaped . ' /inheritance:r /deny ' . $user . ':(F,M,R,RX,W)';
42: exec($cmd);
43:
44: if ($perm & PHPT_ACL_NONE) {
45: /*
46: This is required to remove all the previously denied
47: permission for the USER. Just granting permission doesn't
48: remove the previously denied permission.
49: */
50: $cmd = 'icacls ' . $path_escaped . ' /' . 'remove:d';
51: $cmd .= ' ' . $user;
52: exec($cmd);
53: $cmd = 'icacls ' . $path_escaped . ' /' . 'remove:g';
54: $cmd .= ' ' . $user;
55: exec($cmd);
56: return;
57: }
58:
59: if ($mode == PHPT_ACL_GRANT) {
60: $mode = 'grant';
61: } else {
62: $mode = 'deny';
63: }
64:
65:
66: // Deny all
67: $cmd = 'icacls ' . $path_escaped . ' /deny ' . $user . ':(F,M,R,RX,W)';
68: exec($cmd);
69:
70: /*
71: This is required to remove all the previously denied
72: permission for the USER. Just granting permission doesn't
73: remove the previously denied permission.
74: */
75: $cmd = 'icacls ' . $path_escaped . ' /' . 'remove:d';
76: $cmd .= ' ' . $user;
77: exec($cmd);
78: $cmd = 'icacls ' . $path_escaped . ' /' . 'remove:g';
79: $cmd .= ' ' . $user;
80: exec($cmd);
81:
82:
83: /*
84: Required to set no permission and check that is_readable()
85: returns false. If the $perm_entry contains 'N' skip this step.
86: This will make the file/dir with NO aceess.
87: */
88: if (!in_array('N', $perm_entry)) {
89: /*
90: This is required to remove all the previously denied
91: permission for the USER. Just granting permission doesn't
92: remove the previously denied permission.
93: */
94: $cmd = 'icacls ' . $path_escaped . ' /' . 'remove:d';
95: $cmd .= ' ' . get_username();
96: exec($cmd);
97: $cmd = 'icacls ' . $path_escaped . ' /' . 'remove:g';
98: $cmd .= ' ' . get_username();
99: exec($cmd);
100:
101: $cmd = 'icacls ' . $path_escaped . ' /' . $mode;
102: $cmd .= ' ' . get_username();
103: $cmd .= ':' . '(' . implode($perm_entry, ',') . ')';
104: exec($cmd);
105: }
106: }
107:
108: function create_dir($name, $perms) {
109: if (empty($name)) {
110: echo "create_dir: Empty name is not allowed\n";
111: return;
112: }
113:
114: mkdir($name);
115: $dst = realpath($name);
116: icacls_set($name, PHPT_ACL_GRANT, $perms);
117: }
118:
119: function create_file($name, $perms) {
120: if (empty($name)) {
121: echo "create_file: Empty name is not allowed\n";
122: return;
123: }
124:
125: touch($name);
126: icacls_set($name, PHPT_ACL_GRANT, $perms);
127: }
128:
129: function delete_file($path) {
130: icacls_set($path, PHPT_ACL_GRANT, PHPT_ACL_FULL);
131: if (is_file($path)) {
132: unlink($path);
133: } else {
134: echo "delete_file: '$path' is not a file\n";
135: return;
136: }
137: }
138:
139: function delete_dir($path) {
140: if (is_dir($path)) {
141: icacls_set($path, PHPT_ACL_GRANT, PHPT_ACL_FULL);
142: rmdir($path);
143: } else {
144: echo "delete_dir: '$path' is not a directory\n";
145: return;
146: }
147: }
148: if (0) {
149: $path = __DIR__ . '/a.txt';
150: create_file($path, PHPT_ACL_NONE);
151: if (!is_writable($path)) {
152: echo "PHPT_ACL_NONE success!!\n";
153: } else {
154: echo "PHPT_ACL_NONE failed!!\n";
155: }
156: delete_file($path);
157:
158: $path = __DIR__ . '/a.txt';
159: create_file($path, PHPT_ACL_READ);
160: if (!is_writable($path)) {
161: echo "PHPT_ACL_READ success!!\n";
162: } else {
163: echo "PHPT_ACL_READ failed!!\n";
164: }
165: delete_file($path);
166:
167: $path = __DIR__ . '/adir';
168: create_dir($path, PHPT_ACL_READ);
169: if (!is_writable($path)) {
170: echo "PHPT_ACL_READ dir success!!\n";
171: } else {
172: echo "PHPT_ACL_READ dir failed!!\n";
173: }
174: delete_dir($path);
175:
176: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>