1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12:
13:
14: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
15:
16: define('CON_PREDICT_SUFFICIENT', 1);
17:
18: define('CON_PREDICT_NOTPREDICTABLE', 2);
19:
20: define('CON_PREDICT_CHANGEPERM_SAMEOWNER', 3);
21:
22: define('CON_PREDICT_CHANGEPERM_SAMEGROUP', 4);
23:
24: define('CON_PREDICT_CHANGEPERM_OTHERS', 5);
25:
26: define('CON_PREDICT_CHANGEUSER', 6);
27:
28: define('CON_PREDICT_CHANGEGROUP', 7);
29:
30: define('CON_PREDICT_WINDOWS', 8);
31:
32: define('CON_BASEDIR_NORESTRICTION', 1);
33:
34: define('CON_BASEDIR_DOTRESTRICTION', 2);
35:
36: define('CON_BASEDIR_RESTRICTIONSUFFICIENT', 3);
37:
38: define('CON_BASEDIR_INCOMPATIBLE', 4);
39:
40: function canWriteFile($filename) {
41: clearstatcache();
42: if (is_file($filename)) {
43: return is_writable($filename);
44: } else {
45: return is_writable(dirname($filename));
46: }
47: }
48:
49: function canWriteDir($dirname) {
50: clearstatcache();
51: return is_dir($dirname) && is_writable($dirname);
52: }
53:
54: function getFileInfo($sFilename) {
55: if (!cFileHandler::exists($sFilename)) {
56: return false;
57: }
58:
59: $oiFilePermissions = fileperms($sFilename);
60: if ($oiFilePermissions === false) {
61: return false;
62: }
63:
64: switch (true) {
65: case (($oiFilePermissions & 0xC000) == 0xC000):
66: $info = 's';
67: $type = "socket";
68: break;
69: case (($oiFilePermissions & 0xA000) == 0xA000):
70: $info = 'l';
71: $type = "symbolic link";
72: break;
73: case (($oiFilePermissions & 0x8000) == 0x8000):
74: $info = '-';
75: $type = "regular file";
76: break;
77: case (($oiFilePermissions & 0x6000) == 0x6000):
78: $info = 'b';
79: $type = "block special";
80: break;
81: case (($oiFilePermissions & 0x4000) == 0x4000):
82: $info = 'd';
83: $type = "directory";
84: break;
85: case (($oiFilePermissions & 0x2000) == 0x2000):
86: $info = 'c';
87: $type = "character special";
88: break;
89: case (($oiFilePermissions & 0x1000) == 0x1000):
90: $info = 'p';
91: $type = "FIFO pipe";
92: break;
93: default:
94: $info = "u";
95: $type = "Unknown";
96: break;
97: }
98:
99: $aFileinfo = array();
100: $aFileinfo["info"] = $info;
101: $aFileinfo["type"] = $type;
102: $aFileinfo["owner"]["read"] = ($oiFilePermissions & 0x0100) ? true : false;
103: $aFileinfo["owner"]["write"] = ($oiFilePermissions & 0x0080) ? true : false;
104: $aFileinfo["group"]["read"] = ($oiFilePermissions & 0x0020) ? true : false;
105: $aFileinfo["group"]["write"] = ($oiFilePermissions & 0x0010) ? true : false;
106: $aFileinfo["others"]["read"] = ($oiFilePermissions & 0x0004) ? true : false;
107: $aFileinfo["others"]["write"] = ($oiFilePermissions & 0x0002) ? true : false;
108: $aFileinfo["owner"]["id"] = fileowner($sFilename);
109: $aFileinfo["group"]["id"] = filegroup($sFilename);
110: return ($aFileinfo);
111: }
112:
113: function checkOpenBasedirCompatibility() {
114: $value = getPHPIniSetting("open_basedir");
115:
116: if (isWindows()) {
117: $aBasedirEntries = explode(";", $value);
118: } else {
119: $aBasedirEntries = explode(":", $value);
120: }
121:
122: if (count($aBasedirEntries) == 1 && $aBasedirEntries[0] == $value) {
123: return CON_BASEDIR_NORESTRICTION;
124: }
125:
126: if (in_array(".", $aBasedirEntries) && count($aBasedirEntries) == 1) {
127: return CON_BASEDIR_DOTRESTRICTION;
128: }
129:
130: $sCurrentDirectory = getcwd();
131:
132: foreach ($aBasedirEntries as $entry) {
133: if (cString::findFirstOccurrenceCI($sCurrentDirectory, $entry)) {
134: return CON_BASEDIR_RESTRICTIONSUFFICIENT;
135: }
136: }
137:
138: return CON_BASEDIR_INCOMPATIBLE;
139: }
140:
141: function predictCorrectFilepermissions($file) {
142:
143:
144: if (isWindows()) {
145: return CON_PREDICT_WINDOWS;
146: }
147:
148:
149:
150: if (is_writable($file) && is_readable($file)) {
151: return CON_PREDICT_SUFFICIENT;
152: }
153:
154:
155:
156: $iServerUID = getServerUID();
157: if ($iServerUID === false) {
158: return CON_PREDICT_NOTPREDICTABLE;
159: }
160:
161:
162:
163: $iServerGID = getServerGID();
164: if ($iServerGID === false) {
165: return CON_PREDICT_NOTPREDICTABLE;
166: }
167:
168: $aFilePermissions = getFileInfo($file);
169:
170: if (getSafeModeStatus()) {
171:
172: if ($iServerUID == $aFilePermissions["owner"]["id"]) {
173: return CON_PREDICT_CHANGEPERM_SAMEOWNER;
174: }
175:
176: if (getSafeModeGidStatus()) {
177:
178: if ($iServerGID == $aFilePermissions["group"]["id"]) {
179: return CON_PREDICT_CHANGEPERM_SAMEGROUP;
180: }
181:
182: return CON_PREDICT_CHANGEGROUP;
183: }
184: } else {
185:
186: if ($iServerUID == $aFilePermissions["owner"]["id"]) {
187: return CON_PREDICT_CHANGEPERM_SAMEOWNER;
188: }
189:
190: if ($iServerGID == $aFilePermissions["group"]["id"]) {
191: return CON_PREDICT_CHANGEPERM_SAMEGROUP;
192: }
193:
194: return CON_PREDICT_CHANGEPERM_OTHERS;
195: }
196:
197: return CON_PREDICT_NOTPREDICTABLE;
198: }