if (!empty($_SERVER['DOCUMENT_ROOT'])) {
if (!@file_exists($_SERVER['DOCUMENT_ROOT'].'/tmp/'.$tmpDirName)) {
@mkdir($_SERVER['DOCUMENT_ROOT'].'/tmp/'.$tmpDirName, 0777, true);
- @chmod($_SERVER['DOCUMENT_ROOT'].'/tmp/'.$tmpDirName, 0777);
+ self::makeWritable($_SERVER['DOCUMENT_ROOT'].'/tmp/'.$tmpDirName);
}
if (@file_exists($_SERVER['DOCUMENT_ROOT'].'/tmp/'.$tmpDirName) && @is_writable($_SERVER['DOCUMENT_ROOT'].'/tmp/'.$tmpDirName)) {
if (isset($_ENV[$tmpDir]) && @is_writable($_ENV[$tmpDir])) {
$dir = $_ENV[$tmpDir] . '/' . $tmpDirName;
@mkdir($dir, 0777);
- @chmod($dir, 0777);
+ self::makeWritable($dir);
if (@file_exists($dir) && @is_writable($dir)) {
return $dir;
$dir = INSTALL_SCRIPT_DIR . 'tmp/' . $tmpDirName;
@mkdir($dir, 0777);
- @chmod($dir, 0777);
+ self::makeWritable($dir);
if (!@file_exists($dir) || !@is_writable($dir)) {
$tmpDir = explode('/', $dir);
return $dir;
}
+
+ /**
+ * Tries to make a file or directory writable. It starts of with the least
+ * permissions and goes up until 0777.
+ *
+ * @param string $filename
+ */
+ public static function makeWritable($filename) {
+ if (is_writable($filename)) {
+ return;
+ }
+
+ $chmods = array('0644', '0755', '0775', '0777');
+
+ $startIndex = 0;
+ if (is_dir($filename)) {
+ $startIndex = 1;
+ }
+
+ for ($i = $startIndex; $i < 4; $i++) {
+ @chmod($filename, octdec($chmods[$i]));
+
+ if (is_writable($filename)) {
+ break;
+ }
+ else if ($i == 3) {
+ // does not work with 0777
+ throw new SystemException("Unable to make '".$filename."' writable. This is a misconfiguration of your server, please contact your system administrator or hosting provider.");
+ }
+ }
+ }
}
/**
}
$targetFile->close();
- if (function_exists('apache_get_version') || !@$targetFile->is_writable()) {
- @$targetFile->chmod(0777);
- }
- else {
- @$targetFile->chmod(0755);
- }
+ BasicFileUtil::makeWritable($destination);
if ($header['mtime']) {
@$targetFile->touch($header['mtime']);
$dir = TMP_DIR . dirname($file['filename']);
if (!@is_dir($dir)) {
@mkdir($dir, 0777, true);
- @chmod($dir, 0777);
+ BasicFileUtil::makeWritable($dir);
}
$tar->extract($file['index'], TMP_DIR . $file['filename']);
// create cache folders
@mkdir(TMP_DIR . 'setup/lang/cache/', 0777);
- @chmod(TMP_DIR . 'setup/lang/cache/', 0777);
+ BasicFileUtil::makeWritable(TMP_DIR . 'setup/lang/cache/');
@mkdir(TMP_DIR . 'setup/template/compiled/', 0777);
- @chmod(TMP_DIR . 'setup/template/compiled/', 0777);
+ BasicFileUtil::makeWritable(TMP_DIR . 'setup/template/compiled/');
}
if (!class_exists('wcf\system\WCFSetup')) {
use wcf\system\io\File;
use wcf\system\Regex;
use wcf\system\WCF;
+use wcf\util\FileUtil;
use wcf\util\PasswordUtil;
use wcf\util\StringUtil;
define('MASTER_PASSWORD', '".PasswordUtil::getDoubleSaltedHash($this->masterPassword)."');
?>");
$file->close();
- @chmod(WCF_DIR.'acp/masterPassword.inc.php', 0777);
+ FileUtil::makeWritable(WCF_DIR.'acp/masterPassword.inc.php');
parent::save();
}
$tmpFile = FileUtil::downloadFileFromHttp($gravatarURL, 'gravatar');
copy($tmpFile, WCF_DIR.$cachedFilename);
@unlink($tmpFile);
- @chmod(WCF_DIR.$cachedFilename, 0777);
+ FileUtil::makeWritable(WCF_DIR.$cachedFilename);
@header('Content-Type: image/png');
@readfile(WCF_DIR.$cachedFilename);
<?php
namespace wcf\data\language;
+use wcf\util\FileUtil;
+
use wcf\data\language\category\LanguageCategory;
use wcf\data\language\category\LanguageCategoryEditor;
use wcf\data\language\item\LanguageItemEditor;
}
}
- $file = new File(WCF_DIR.'language/'.$this->languageID.'_'.$category->languageCategory.'.php');
- @$file->chmod(0777);
+ $filename = WCF_DIR.'language/'.$this->languageID.'_'.$category->languageCategory.'.php';
+ $file = new File($filename);
+ FileUtil::makeWritable($filename);
$file->write($content . '?>');
$file->close();
}
use wcf\system\cache\CacheHandler;
use wcf\system\io\File;
use wcf\system\WCF;
+use wcf\util\FileUtil;
/**
* Provides functions to edit options.
// close file
$file->close();
- @$file->chmod(0777);
+ FileUtil::makeWritable(WCF_DIR.'options.inc.php');
}
}
foreach ($contentList as $key => $val) {
if ($val['type'] == 'file') {
$imagesTar->extract($key, $imagesLocation.basename($val['filename']));
- @chmod($imagesLocation.basename($val['filename']), 0666);
+ FileUtil::makeWritable($imagesLocation.basename($val['filename']));
}
}
// create template path
if (!file_exists($templatesDir)) {
@mkdir($templatesDir, 0777);
- @chmod($templatesDir, 0777);
+ FileUtil::makeWritable($templatesDir);
}
// copy templates
if ($index !== false) {
$filename = WCF_DIR.'images/stylePreview-'.$style->styleID.$fileExtension;
$tar->extract($index, $filename);
- @chmod($filename, 0777);
+ FileUtil::makeWritable($filename);
if (file_exists($filename)) {
$style->update(array('image' => 'stylePreview-'.$style->styleID.$fileExtension));
$directory = $location . ($index === null ? '' : $index);
if (!is_dir($directory)) {
@mkdir($directory, 0777, true);
- @chmod($directory, 0777);
+ FileUtil::makeWritable($directory);
return FileUtil::addTrailingSlash($directory);
}
// create templates tar
$templatesTarName = FileUtil::getTemporaryFilename('templates', '.tar');
$templatesTar = new TarWriter($templatesTarName);
- @chmod($templatesTarName, 0777);
+ FileUtil::makeWritable($templatesTarName);
// append templates to tar
// get templates
// create images tar
$imagesTarName = FileUtil::getTemporaryFilename('images_', '.tar');
$imagesTar = new TarWriter($imagesTarName);
- @chmod($imagesTarName, 0777);
+ FileUtil::makeWritable($imagesTarName);
// append images to tar
$path = FileUtil::addTrailingSlash(WCF_DIR.$this->imagePath);
<?php
namespace wcf\data\template;
+use wcf\util\FileUtil;
+
use wcf\data\DatabaseObjectEditor;
use wcf\system\io\File;
use wcf\system\Regex;
$file = new File($path);
$file->write($source);
$file->close();
- @$file->chmod(0777);
+ FileUtil::makeWritable($path);
}
/**
$targetFile->write($buffer);
$targetFile->close();
- if (FileUtil::isApacheModule() || !@$targetFile->is_writable()) {
- @$targetFile->chmod(0777);
- }
- else {
- @$targetFile->chmod(0755);
- }
+ FileUtil::makeWritable($destination);
if ($header['mtime']) {
@$targetFile->touch($header['mtime']);
/**
* Reads zip files.
*
- * @author Tim Düsterhus
- * @copyright 2012 Tim Düsterhus
+ * @author Tim Duesterhus
+ * @copyright 2001-2013 WoltLab GmbH
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
* @package com.woltlab.wcf
* @subpackage system.io
$targetFile->write($file['content'], strlen($file['content']));
$targetFile->close();
- if (FileUtil::isApacheModule() || !@$targetFile->is_writable()) {
- @$targetFile->chmod(0777);
- }
- else {
- @$targetFile->chmod(0755);
- }
+ FileUtil::makeWritable($destination);
if ($file['header']['mtime']) {
@$targetFile->touch($file['header']['mtime']);
// create directory and set permissions
@mkdir($packageDir, 0777, true);
- @chmod($packageDir, 0777);
+ FileUtil::makeWritable($packageDir);
}
return null;
* Extracts files and directories from a tar archive.
*
* @author Marcel Werk
- * @copyright 2001-2012 WoltLab GmbH
+ * @copyright 2001-2013 WoltLab GmbH
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
* @package com.woltlab.wcf
* @subpackage system.setup
* @param string $target
*/
protected function makeWriteable($target) {
- if (!preg_match('/^WIN/i', PHP_OS)) {
- if (!@chmod($target, 0777)) {
- // todo: what to do in this case?
- //throw new SystemException("Could not chmod file '".$target."'");
- }
- }
+ FileUtil::makeWritable($target);
}
}
// create tmp folder in document root automatically
if (!@file_exists($_SERVER['DOCUMENT_ROOT'].'/tmp')) {
@mkdir($_SERVER['DOCUMENT_ROOT'].'/tmp/', 0777);
- @chmod($_SERVER['DOCUMENT_ROOT'].'/tmp/', 0777);
+ self::makeWritable($_SERVER['DOCUMENT_ROOT'].'/tmp/');
}
}
if (@file_exists($_SERVER['DOCUMENT_ROOT'].'/tmp') && @is_writable($_SERVER['DOCUMENT_ROOT'].'/tmp')) {
* necessary.
*
* @param string $path
- * @param integer $chmod
* @return boolean
*/
- public static function makePath($path, $chmod = 0777) {
+ public static function makePath($path) {
// directory already exists, abort
if (file_exists($path)) {
return false;
$parent = self::addTrailingSlash($parent);
if (!@file_exists($parent)) {
// could not create parent directory either => abort
- if (!self::makePath($parent, $chmod)) {
+ if (!self::makePath($parent)) {
return false;
}
}
// well, the parent directory exists or has been created
// lets create this path
- $oldumask = @umask(0);
- if (!@mkdir($path, $chmod)) {
+ if (!@mkdir($path)) {
return false;
}
- @umask($oldumask);
- /*if (!@chmod($path, $chmod)) {
- return false;
- }*/
- if (self::isApacheModule() || !@is_writable($path)) {
- @chmod($path, 0777);
- }
+
+ self::makeWritable($path);
return true;
}
}
$targetFile->close();
$sourceFile->close();
- @$targetFile->chmod(0777);
- /*if ($filesize != filesize($destination)) {
- @unlink($destination);
- return false;
- }*/
+ self::makeWritable($destination);
return true;
}
return self::$finfo->file($filename);
}
+ /**
+ * Tries to make a file or directory writable. It starts of with the least
+ * permissions and goes up until 0777.
+ *
+ * @param string $filename
+ */
+ public static function makeWritable($filename) {
+ if (is_writable($filename)) {
+ return;
+ }
+
+ $chmods = array('0644', '0755', '0775', '0777');
+
+ $startIndex = 0;
+ if (is_dir($filename)) {
+ $startIndex = 1;
+ }
+
+ for ($i = $startIndex; $i < 4; $i++) {
+ @chmod($filename, octdec($chmods[$i]));
+
+ if (is_writable($filename)) {
+ break;
+ }
+ else if ($i == 3) {
+ // does not work with 0777
+ throw new SystemException("Unable to make '".$filename."' writable. This is a misconfiguration of your server, please contact your system administrator or hosting provider.");
+ }
+ }
+ }
+
private function __construct() { }
}