/** * equivalent for windows filesystem for PHP's is_writable() * @param string file Full path to the file/dir * @return bool true if writable */ function is_writable_windows($file) { if ($file[strlen($file) - 1] == '/') { return is_writable_windows($file . uniqid(mt_rand()) . '.tmp'); } // the assumption here is that Windows has an inherited permissions scheme // any file that is a descendant of an unwritable directory will inherit // that property and will trigger a failure below. if (is_dir($file)) { return true; } $file = str_replace("/", '\\', $file); if (file_exists($file)) { if (!($f = @sugar_fopen($file, 'r+'))) { return false; } fclose($f); return true; } if (!($f = @sugar_fopen($file, 'w'))) { return false; } fclose($f); unlink($file); return true; }
/** * checks files for permissions * @param array files Array of files with absolute paths * @return string result of check */ function checkFiles($files, $echo = false) { global $mod_strings; $filesNotWritable = array(); $i = 0; $filesOut = "\n\t\t<a href='javascript:void(0); toggleNwFiles(\"filesNw\");'>{$mod_strings['LBL_UW_SHOW_NW_FILES']}</a>\n\t\t<div id='filesNw' style='display:none;'>\n\t\t<table cellpadding='3' cellspacing='0' border='0'>\n\t\t<tr>\n\t\t\t<th align='left'>{$mod_strings['LBL_UW_FILE']}</th>\n\t\t\t<th align='left'>{$mod_strings['LBL_UW_FILE_PERMS']}</th>\n\t\t\t<th align='left'>{$mod_strings['LBL_UW_FILE_OWNER']}</th>\n\t\t\t<th align='left'>{$mod_strings['LBL_UW_FILE_GROUP']}</th>\n\t\t</tr>"; $isWindows = is_windows(); foreach ($files as $file) { if ($isWindows) { if (!is_writable_windows($file)) { logThis('WINDOWS: File [' . $file . '] not readable - saving for display'); // don't warn yet - we're going to use this to check against replacement files // aw: commented out; it's a hack to allow upgrade wizard to continue on windows... will fix later /*$filesNotWritable[$i] = $file; $filesNWPerms[$i] = substr(sprintf('%o',fileperms($file)), -4); $filesOut .= "<tr>". "<td><span class='error'>{$file}</span></td>". "<td>{$filesNWPerms[$i]}</td>". "<td>".$mod_strings['ERR_UW_CANNOT_DETERMINE_USER']."</td>". "<td>".$mod_strings['ERR_UW_CANNOT_DETERMINE_GROUP']."</td>". "</tr>";*/ } } else { if (!is_writable($file)) { logThis('File [' . $file . '] not writable - saving for display'); // don't warn yet - we're going to use this to check against replacement files $filesNotWritable[$i] = $file; $filesNWPerms[$i] = substr(sprintf('%o', fileperms($file)), -4); $owner = posix_getpwuid(fileowner($file)); $group = posix_getgrgid(filegroup($file)); $filesOut .= "<tr>" . "<td><span class='error'>{$file}</span></td>" . "<td>{$filesNWPerms[$i]}</td>" . "<td>" . $owner['name'] . "</td>" . "<td>" . $group['name'] . "</td>" . "</tr>"; } } $i++; } $filesOut .= '</table></div>'; // not a stop error $errors['files']['filesNotWritable'] = count($filesNotWritable) > 0 ? true : false; if (count($filesNotWritable) < 1) { $filesOut = "{$mod_strings['LBL_UW_FILE_NO_ERRORS']}"; } return $filesOut; }
/** * checks files for permissions * @param array files Array of files with absolute paths * @return string result of check */ function systemCheckJsonCheckFiles($persistence) { global $mod_strings; global $persistence; $filesNotWritable = array(); $i = 0; $filesOut = "\n\t\t<a href='javascript:void(0); toggleNwFiles(\"filesNw\");'>{$mod_strings['LBL_UW_SHOW_NW_FILES']}</a>\n\t\t<div id='filesNw' style='display:none;'>\n\t\t<table cellpadding='3' cellspacing='0' border='0'>\n\t\t<tr>\n\t\t\t<th align='left'>{$mod_strings['LBL_UW_FILE']}</th>\n\t\t\t<th align='left'>{$mod_strings['LBL_UW_FILE_PERMS']}</th>\n\t\t\t<th align='left'>{$mod_strings['LBL_UW_FILE_OWNER']}</th>\n\t\t\t<th align='left'>{$mod_strings['LBL_UW_FILE_GROUP']}</th>\n\t\t</tr>"; $isWindows = is_windows(); foreach ($persistence['files_to_check'] as $file) { // while($file = array_pop($persistence['files_to_check'])) { // admin deletes a bad file mid-check: if (!file_exists($file)) { continue; } if ($isWindows) { if (!is_writable_windows($file)) { logThis('WINDOWS: File [' . $file . '] not readable - saving for display'); // don't warn yet - we're going to use this to check against replacement files $filesNotWritable[$i] = $file; $filesNWPerms[$i] = substr(sprintf('%o', fileperms($file)), -4); $filesOut .= "<tr>" . "<td valign='top'><span class='error'>{$file}</span></td>" . "<td valign='top'>{$filesNWPerms[$i]}</td>" . "<td valign='top'>" . $mod_strings['ERR_UW_CANNOT_DETERMINE_USER'] . "</td>" . "<td valign='top'>" . $mod_strings['ERR_UW_CANNOT_DETERMINE_GROUP'] . "</td>" . "</tr>"; } } else { if (!is_writable($file)) { logThis('File [' . $file . '] not writable - saving for display'); // don't warn yet - we're going to use this to check against replacement files $filesNotWritable[$i] = $file; $filesNWPerms[$i] = substr(sprintf('%o', fileperms($file)), -4); $owner = posix_getpwuid(fileowner($file)); $group = posix_getgrgid(filegroup($file)); $filesOut .= "<tr>" . "<td valign='top'><span class='error'>{$file}</span></td>" . "<td valign='top'>{$filesNWPerms[$i]}</td>" . "<td valign='top'>" . $owner['name'] . "</td>" . "<td valign='top'>" . $group['name'] . "</td>" . "</tr>"; } } $i++; } $filesOut .= '</table></div>'; // not a stop error $persistence['filesNotWritable'] = count($filesNotWritable) > 0 ? true : false; if (count($filesNotWritable) < 1) { $filesOut = "{$mod_strings['LBL_UW_FILE_NO_ERRORS']}"; $persistence['step']['systemCheck'] = 'success'; } echo $filesOut; return $persistence; }
$stop = false; // flag to prevent going to next step /////////////////////////////////////////////////////////////////////////////// //// FILE CHECKS logThis('Starting file permission check...'); $filesNotWritable = array(); $filesNWPerms = array(); // add directories here that should be skipped when doing file permissions checks (cache/upload is the nasty one) $skipDirs = array($sugar_config['upload_dir'], '.svn'); $files = uwFindAllFiles(getcwd(), array(), true, $skipDirs); $i = 0; $filesOut = "\n\t<a href='javascript:void(0); toggleNwFiles(\"filesNw\");'>{$mod_strings['LBL_UW_SHOW_NW_FILES']}</a>\n\t<div id='filesNw' style='display:none;'>\n\t<table cellpadding='3' cellspacing='0' border='0'>\n\t<tr>\n\t\t<th align='left'>{$mod_strings['LBL_UW_FILE']}</th>\n\t\t<th align='left'>{$mod_strings['LBL_UW_FILE_PERMS']}</th>\n\t\t<th align='left'>{$mod_strings['LBL_UW_FILE_OWNER']}</th>\n\t\t<th align='left'>{$mod_strings['LBL_UW_FILE_GROUP']}</th>\n\t</tr>"; $isWindows = is_windows(); foreach ($files as $file) { if ($isWindows) { if (!is_writable_windows($file)) { logThis('WINDOWS: File [' . $file . '] not readable - saving for display'); // don't warn yet - we're going to use this to check against replacement files $filesNotWritable[$i] = $file; $filesNWPerms[$i] = substr(sprintf('%o', fileperms($file)), -4); $filesOut .= "<tr>" . "<td><span class='error'>{$file}</span></td>" . "<td>{$filesNWPerms[$i]}</td>" . "<td>" . $mod_strings['ERR_UW_CANNOT_DETERMINE_USER'] . "</td>" . "<td>" . $mod_strings['ERR_UW_CANNOT_DETERMINE_GROUP'] . "</td>" . "</tr>"; } } else { if (!is_writable($file)) { logThis('File [' . $file . '] not writable - saving for display'); // don't warn yet - we're going to use this to check against replacement files $filesNotWritable[$i] = $file; $filesNWPerms[$i] = substr(sprintf('%o', fileperms($file)), -4); $owner = posix_getpwuid(fileowner($file)); $group = posix_getgrgid(filegroup($file)); $filesOut .= "<tr>" . "<td><span class='error'>{$file}</span></td>" . "<td>{$filesNWPerms[$i]}</td>" . "<td>" . $owner['name'] . "</td>" . "<td>" . $group['name'] . "</td>" . "</tr>";