2012-01-20 18:10:48 +01:00
|
|
|
<?php
|
2015-09-10 23:13:48 +02:00
|
|
|
|
2017-01-04 23:16:12 +01:00
|
|
|
namespace SilverStripe\EnvironmentCheck\Checks;
|
|
|
|
|
|
|
|
use SilverStripe\EnvironmentCheck\EnvironmentCheck;
|
|
|
|
|
2012-01-20 18:10:48 +01:00
|
|
|
/**
|
2015-09-10 23:13:48 +02:00
|
|
|
* Check that the given file is writable.
|
2017-01-04 23:16:12 +01:00
|
|
|
*
|
|
|
|
* @package environmentcheck
|
2012-01-20 18:10:48 +01:00
|
|
|
*/
|
2015-11-21 07:18:35 +01:00
|
|
|
class FileWriteableCheck implements EnvironmentCheck
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $path;
|
2015-09-10 23:13:48 +02:00
|
|
|
|
2015-11-21 07:18:35 +01:00
|
|
|
/**
|
|
|
|
* @param string $path The full path. If a relative path, it will relative to the BASE_PATH.
|
|
|
|
*/
|
|
|
|
public function __construct($path)
|
|
|
|
{
|
|
|
|
$this->path = $path;
|
|
|
|
}
|
2015-09-10 23:13:48 +02:00
|
|
|
|
2015-11-21 07:18:35 +01:00
|
|
|
/**
|
2017-01-04 23:16:12 +01:00
|
|
|
* {@inheritDoc}
|
2015-11-21 07:18:35 +01:00
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function check()
|
|
|
|
{
|
|
|
|
if ($this->path[0] == '/') {
|
|
|
|
$filename = $this->path;
|
|
|
|
} else {
|
|
|
|
$filename = BASE_PATH . DIRECTORY_SEPARATOR . str_replace('/', DIRECTORY_SEPARATOR, $this->path);
|
|
|
|
}
|
2017-01-04 23:16:12 +01:00
|
|
|
|
2015-11-21 07:18:35 +01:00
|
|
|
if (file_exists($filename)) {
|
|
|
|
$isWriteable = is_writeable($filename);
|
|
|
|
} else {
|
|
|
|
$isWriteable = is_writeable(dirname($filename));
|
|
|
|
}
|
2017-01-04 23:16:12 +01:00
|
|
|
|
2015-11-21 07:18:35 +01:00
|
|
|
if (!$isWriteable) {
|
|
|
|
if (function_exists('posix_getgroups')) {
|
|
|
|
$userID = posix_geteuid();
|
|
|
|
$user = posix_getpwuid($userID);
|
2012-01-20 18:10:48 +01:00
|
|
|
|
2015-11-21 07:18:35 +01:00
|
|
|
$currentOwnerID = fileowner(file_exists($filename) ? $filename : dirname($filename));
|
|
|
|
$currentOwner = posix_getpwuid($currentOwnerID);
|
2012-01-20 18:10:48 +01:00
|
|
|
|
2017-01-04 23:16:12 +01:00
|
|
|
$message = "User '$user[name]' needs to be able to write to this file:\n$filename\n\nThe file is "
|
|
|
|
. "currently owned by '$currentOwner[name]'. ";
|
2012-01-20 18:10:48 +01:00
|
|
|
|
2015-11-21 07:18:35 +01:00
|
|
|
if ($user['name'] == $currentOwner['name']) {
|
2017-01-04 23:16:12 +01:00
|
|
|
$message .= 'We recommend that you make the file writeable.';
|
2015-11-21 07:18:35 +01:00
|
|
|
} else {
|
|
|
|
$groups = posix_getgroups();
|
2017-03-21 11:57:07 +01:00
|
|
|
$groupList = [];
|
2015-11-21 07:18:35 +01:00
|
|
|
foreach ($groups as $group) {
|
|
|
|
$groupInfo = posix_getgrgid($group);
|
|
|
|
if (in_array($currentOwner['name'], $groupInfo['members'])) {
|
|
|
|
$groupList[] = $groupInfo['name'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($groupList) {
|
2017-01-04 23:16:12 +01:00
|
|
|
$message .= " We recommend that you make the file group-writeable and change the group to "
|
|
|
|
. "one of these groups:\n - " . implode("\n - ", $groupList)
|
2015-11-21 07:18:35 +01:00
|
|
|
. "\n\nFor example:\nchmod g+w $filename\nchgrp " . $groupList[0] . " $filename";
|
|
|
|
} else {
|
2017-01-04 23:16:12 +01:00
|
|
|
$message .= " There is no user-group that contains both the web-server user and the owner "
|
|
|
|
. "of this file. Change the ownership of the file, create a new group, or temporarily "
|
|
|
|
. "make the file writeable by everyone during the install process.";
|
2015-11-21 07:18:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$message = "The webserver user needs to be able to write to this file:\n$filename";
|
|
|
|
}
|
2017-01-04 23:16:12 +01:00
|
|
|
|
2017-03-21 11:57:07 +01:00
|
|
|
return [EnvironmentCheck::ERROR, $message];
|
2015-11-21 07:18:35 +01:00
|
|
|
}
|
2012-01-20 18:10:48 +01:00
|
|
|
|
2017-03-21 11:57:07 +01:00
|
|
|
return [EnvironmentCheck::OK, ''];
|
2015-11-21 07:18:35 +01:00
|
|
|
}
|
2015-09-10 23:13:48 +02:00
|
|
|
}
|