forked from shaarli/Shaarli
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
install: check file/directory permissions for Shaarli resources
Relates to shaarli#40 Relates to shaarli#372 Additions: - FileUtils: IOException - ApplicationUtils: - check if Shaarli resources are accessible with sufficient permissions - basic test coverage - index.php: - check access permissions and redirect to an error page if needed: - before running the first installation Modifications: - LinkDB: - factorize datastore write code - check if the datastore (exists AND is writeable) OR (doesn't exist AND its parent dir is writable) - raise an IOException if needed Signed-off-by: VirtualTam <virtualtam@flibidi.net>
- Loading branch information
1 parent
c580024
commit 2e28269
Showing
6 changed files
with
213 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
/** | ||
* Shaarli (application) utilities | ||
*/ | ||
class ApplicationUtils | ||
{ | ||
|
||
/** | ||
* Checks Shaarli has the proper access permissions to its resources | ||
* | ||
* @param array $globalConfig The $GLOBALS['config'] array | ||
* | ||
* @return array A list of the detected configuration issues | ||
*/ | ||
public static function checkResourcePermissions($globalConfig) | ||
{ | ||
$errors = array(); | ||
|
||
// Check script and template directories are readable | ||
foreach (array( | ||
'application', | ||
'inc', | ||
'plugins', | ||
$globalConfig['RAINTPL_TPL'] | ||
) as $path) { | ||
if (! is_readable(realpath($path))) { | ||
$errors[] = '"'.$path.'" directory is not readable'; | ||
} | ||
} | ||
|
||
// Check cache and data directories are readable and writeable | ||
foreach (array( | ||
$globalConfig['CACHEDIR'], | ||
$globalConfig['DATADIR'], | ||
$globalConfig['PAGECACHE'], | ||
$globalConfig['RAINTPL_TMP'] | ||
) as $path) { | ||
if (! is_readable(realpath($path))) { | ||
$errors[] = '"'.$path.'" directory is not readable'; | ||
} | ||
if (! is_writable(realpath($path))) { | ||
$errors[] = '"'.$path.'" directory is not writable'; | ||
} | ||
} | ||
|
||
// Check configuration files are readable and writeable | ||
foreach (array( | ||
$globalConfig['CONFIG_FILE'], | ||
$globalConfig['DATASTORE'], | ||
$globalConfig['IPBANS_FILENAME'], | ||
$globalConfig['LOG_FILE'], | ||
$globalConfig['UPDATECHECK_FILENAME'] | ||
) as $path) { | ||
if (! is_file(realpath($path))) { | ||
# the file may not exist yet | ||
continue; | ||
} | ||
|
||
if (! is_readable(realpath($path))) { | ||
$errors[] = '"'.$path.'" file is not readable'; | ||
} | ||
if (! is_writable(realpath($path))) { | ||
$errors[] = '"'.$path.'" file is not writable'; | ||
} | ||
} | ||
|
||
return $errors; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
/** | ||
* Exception class thrown when a filesystem access failure happens | ||
*/ | ||
class IOException extends Exception | ||
{ | ||
private $path; | ||
|
||
/** | ||
* Construct a new IOException | ||
* | ||
* @param string $path path to the ressource that cannot be accessed | ||
*/ | ||
public function __construct($path) | ||
{ | ||
$this->path = $path; | ||
$this->message = 'Error accessing '.$this->path; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
/** | ||
* ApplicationUtils' tests | ||
*/ | ||
|
||
require_once 'application/ApplicationUtils.php'; | ||
|
||
|
||
/** | ||
* Unitary tests for Shaarli utilities | ||
*/ | ||
class ApplicationUtilsTest extends PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* Checks resource permissions for the current Shaarli installation | ||
*/ | ||
public function testCheckCurrentResourcePermissions() | ||
{ | ||
$config = array( | ||
'CACHEDIR' => 'cache', | ||
'CONFIG_FILE' => 'data/config.php', | ||
'DATADIR' => 'data', | ||
'DATASTORE' => 'data/datastore.php', | ||
'IPBANS_FILENAME' => 'data/ipbans.php', | ||
'LOG_FILE' => 'data/log.txt', | ||
'PAGECACHE' => 'pagecache', | ||
'RAINTPL_TMP' => 'tmp', | ||
'RAINTPL_TPL' => 'tpl', | ||
'UPDATECHECK_FILENAME' => 'data/lastupdatecheck.txt' | ||
); | ||
$this->assertEquals( | ||
array(), | ||
ApplicationUtils::checkResourcePermissions($config) | ||
); | ||
} | ||
|
||
/** | ||
* Checks resource permissions for a non-existent Shaarli installation | ||
*/ | ||
public function testCheckCurrentResourcePermissionsErrors() | ||
{ | ||
$config = array( | ||
'CACHEDIR' => 'null/cache', | ||
'CONFIG_FILE' => 'null/data/config.php', | ||
'DATADIR' => 'null/data', | ||
'DATASTORE' => 'null/data/store.php', | ||
'IPBANS_FILENAME' => 'null/data/ipbans.php', | ||
'LOG_FILE' => 'null/data/log.txt', | ||
'PAGECACHE' => 'null/pagecache', | ||
'RAINTPL_TMP' => 'null/tmp', | ||
'RAINTPL_TPL' => 'null/tpl', | ||
'UPDATECHECK_FILENAME' => 'null/data/lastupdatecheck.txt' | ||
); | ||
$this->assertEquals( | ||
array( | ||
'"null/tpl" directory is not readable', | ||
'"null/cache" directory is not readable', | ||
'"null/cache" directory is not writable', | ||
'"null/data" directory is not readable', | ||
'"null/data" directory is not writable', | ||
'"null/pagecache" directory is not readable', | ||
'"null/pagecache" directory is not writable', | ||
'"null/tmp" directory is not readable', | ||
'"null/tmp" directory is not writable' | ||
), | ||
ApplicationUtils::checkResourcePermissions($config) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters