move isDirectoryUsable to System

This commit is contained in:
Philipp Holzer 2019-02-05 22:30:18 +01:00
parent 6a9d73f7d9
commit b79bd63231
No known key found for this signature in database
GPG Key ID: 517BE60E2CE5C8A5
4 changed files with 47 additions and 49 deletions

View File

@ -22,6 +22,7 @@ use Friendica\BaseObject;
use Friendica\Core\Config; use Friendica\Core\Config;
use Friendica\Core\PConfig; use Friendica\Core\PConfig;
use Friendica\Core\Protocol; use Friendica\Core\Protocol;
use Friendica\Core\System;
use Friendica\Database\DBA; use Friendica\Database\DBA;
use Friendica\Model\Contact; use Friendica\Model\Contact;
use Friendica\Util\BasePath; use Friendica\Util\BasePath;
@ -634,7 +635,7 @@ function get_temppath()
$temppath = Config::get("system", "temppath"); $temppath = Config::get("system", "temppath");
if (($temppath != "") && BasePath::isDirectoryUsable($temppath)) { if (($temppath != "") && System::isDirectoryUsable($temppath)) {
// We have a temp path and it is usable // We have a temp path and it is usable
return BasePath::getRealPath($temppath); return BasePath::getRealPath($temppath);
} }
@ -643,7 +644,7 @@ function get_temppath()
$temppath = sys_get_temp_dir(); $temppath = sys_get_temp_dir();
// Check if it is usable // Check if it is usable
if (($temppath != "") && BasePath::isDirectoryUsable($temppath)) { if (($temppath != "") && System::isDirectoryUsable($temppath)) {
// Always store the real path, not the path through symlinks // Always store the real path, not the path through symlinks
$temppath = BasePath::getRealPath($temppath); $temppath = BasePath::getRealPath($temppath);
@ -654,7 +655,7 @@ function get_temppath()
mkdir($new_temppath); mkdir($new_temppath);
} }
if (BasePath::isDirectoryUsable($new_temppath)) { if (System::isDirectoryUsable($new_temppath)) {
// The new path is usable, we are happy // The new path is usable, we are happy
Config::set("system", "temppath", $new_temppath); Config::set("system", "temppath", $new_temppath);
return $new_temppath; return $new_temppath;
@ -736,7 +737,7 @@ function get_itemcachepath()
} }
$itemcache = Config::get('system', 'itemcache'); $itemcache = Config::get('system', 'itemcache');
if (($itemcache != "") && BasePath::isDirectoryUsable($itemcache)) { if (($itemcache != "") && System::isDirectoryUsable($itemcache)) {
return BasePath::getRealPath($itemcache); return BasePath::getRealPath($itemcache);
} }
@ -748,7 +749,7 @@ function get_itemcachepath()
mkdir($itemcache); mkdir($itemcache);
} }
if (BasePath::isDirectoryUsable($itemcache)) { if (System::isDirectoryUsable($itemcache)) {
Config::set("system", "itemcache", $itemcache); Config::set("system", "itemcache", $itemcache);
return $itemcache; return $itemcache;
} }
@ -764,7 +765,7 @@ function get_itemcachepath()
function get_spoolpath() function get_spoolpath()
{ {
$spoolpath = Config::get('system', 'spoolpath'); $spoolpath = Config::get('system', 'spoolpath');
if (($spoolpath != "") && BasePath::isDirectoryUsable($spoolpath)) { if (($spoolpath != "") && System::isDirectoryUsable($spoolpath)) {
// We have a spool path and it is usable // We have a spool path and it is usable
return $spoolpath; return $spoolpath;
} }
@ -779,7 +780,7 @@ function get_spoolpath()
mkdir($spoolpath); mkdir($spoolpath);
} }
if (BasePath::isDirectoryUsable($spoolpath)) { if (System::isDirectoryUsable($spoolpath)) {
// The new path is usable, we are happy // The new path is usable, we are happy
Config::set("system", "spoolpath", $spoolpath); Config::set("system", "spoolpath", $spoolpath);
return $spoolpath; return $spoolpath;

View File

@ -13,7 +13,6 @@ use Friendica\Core\Config\ConfigCacheLoader;
use Friendica\Database\DBA; use Friendica\Database\DBA;
use Friendica\Factory\ConfigFactory; use Friendica\Factory\ConfigFactory;
use Friendica\Network\HTTPException\InternalServerErrorException; use Friendica\Network\HTTPException\InternalServerErrorException;
use Friendica\Util\BasePath;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
/** /**
@ -194,7 +193,7 @@ class App
$this->logger = $logger; $this->logger = $logger;
$this->basePath = $this->config->get('system', 'basepath'); $this->basePath = $this->config->get('system', 'basepath');
if (!BasePath::isDirectoryUsable($this->basePath, false)) { if (!Core\System::isDirectoryUsable($this->basePath, false)) {
throw new Exception('Basepath ' . $this->basePath . ' isn\'t usable.'); throw new Exception('Basepath ' . $this->basePath . ' isn\'t usable.');
} }
$this->basePath = rtrim($this->basePath, DIRECTORY_SEPARATOR); $this->basePath = rtrim($this->basePath, DIRECTORY_SEPARATOR);

View File

@ -303,6 +303,44 @@ class System extends BaseObject
return $processUser['name']; return $processUser['name'];
} }
/**
* @brief Checks if a given directory is usable for the system
*
* @param $directory
* @param bool $check_writable
*
* @return boolean the directory is usable
*/
public static function isDirectoryUsable($directory, $check_writable = true)
{
if ($directory == '') {
Logger::log('Directory is empty. This shouldn\'t happen.', Logger::DEBUG);
return false;
}
if (!file_exists($directory)) {
Logger::log('Path "' . $directory . '" does not exist for user ' . static::getUser(), Logger::DEBUG);
return false;
}
if (is_file($directory)) {
Logger::log('Path "' . $directory . '" is a file for user ' . static::getUser(), Logger::DEBUG);
return false;
}
if (!is_dir($directory)) {
Logger::log('Path "' . $directory . '" is not a directory for user ' . static::getUser(), Logger::DEBUG);
return false;
}
if ($check_writable && !is_writable($directory)) {
Logger::log('Path "' . $directory . '" is not writable for user ' . static::getUser(), Logger::DEBUG);
return false;
}
return true;
}
/// @todo Move the following functions from boot.php /// @todo Move the following functions from boot.php
/* /*
function killme() function killme()

View File

@ -2,8 +2,6 @@
namespace Friendica\Util; namespace Friendica\Util;
use Friendica\Core;
class BasePath class BasePath
{ {
/** /**
@ -52,42 +50,4 @@ class BasePath
return $path; return $path;
} }
} }
/**
* @brief Checks if a given directory is usable for the system
*
* @param $directory
* @param bool $check_writable
*
* @return boolean the directory is usable
*/
public static function isDirectoryUsable($directory, $check_writable = true)
{
if ($directory == '') {
Core\Logger::log('Directory is empty. This shouldn\'t happen.', Core\Logger::DEBUG);
return false;
}
if (!file_exists($directory)) {
Core\Logger::log('Path "' . $directory . '" does not exist for user ' . Core\System::getUser(), Core\Logger::DEBUG);
return false;
}
if (is_file($directory)) {
Core\Logger::log('Path "' . $directory . '" is a file for user ' . Core\System::getUser(), Core\Logger::DEBUG);
return false;
}
if (!is_dir($directory)) {
Core\Logger::log('Path "' . $directory . '" is not a directory for user ' . Core\System::getUser(), Core\Logger::DEBUG);
return false;
}
if ($check_writable && !is_writable($directory)) {
Core\Logger::log('Path "' . $directory . '" is not writable for user ' . Core\System::getUser(), Core\Logger::DEBUG);
return false;
}
return true;
}
} }