1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\EnvironmentCheck\Checks; |
4
|
|
|
|
5
|
|
|
use SilverStripe\EnvironmentCheck\EnvironmentCheck; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Check that the given file is writable. |
9
|
|
|
* |
10
|
|
|
* @package environmentcheck |
11
|
|
|
*/ |
12
|
|
|
class FileWriteableCheck implements EnvironmentCheck |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
protected $path; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param string $path The full path. If a relative path, it will relative to the BASE_PATH. |
21
|
|
|
*/ |
22
|
|
|
public function __construct($path) |
23
|
|
|
{ |
24
|
|
|
$this->path = $path; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* {@inheritDoc} |
29
|
|
|
* |
30
|
|
|
* @return array |
31
|
|
|
*/ |
32
|
|
|
public function check() |
33
|
|
|
{ |
34
|
|
|
if ($this->path[0] == '/') { |
35
|
|
|
$filename = $this->path; |
36
|
|
|
} else { |
37
|
|
|
$filename = BASE_PATH . DIRECTORY_SEPARATOR . str_replace('/', DIRECTORY_SEPARATOR, $this->path); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
if (file_exists($filename)) { |
41
|
|
|
$isWriteable = is_writeable($filename); |
42
|
|
|
} else { |
43
|
|
|
$isWriteable = is_writeable(dirname($filename)); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
if (!$isWriteable) { |
47
|
|
|
if (function_exists('posix_getgroups')) { |
48
|
|
|
$userID = posix_geteuid(); |
49
|
|
|
$user = posix_getpwuid($userID); |
50
|
|
|
|
51
|
|
|
$currentOwnerID = fileowner(file_exists($filename) ? $filename : dirname($filename)); |
52
|
|
|
$currentOwner = posix_getpwuid($currentOwnerID); |
53
|
|
|
|
54
|
|
|
$message = "User '$user[name]' needs to be able to write to this file:\n$filename\n\nThe file is " |
55
|
|
|
. "currently owned by '$currentOwner[name]'. "; |
56
|
|
|
|
57
|
|
|
if ($user['name'] == $currentOwner['name']) { |
58
|
|
|
$message .= 'We recommend that you make the file writeable.'; |
59
|
|
|
} else { |
60
|
|
|
$groups = posix_getgroups(); |
61
|
|
|
$groupList = []; |
62
|
|
|
foreach ($groups as $group) { |
63
|
|
|
$groupInfo = posix_getgrgid($group); |
64
|
|
|
if (in_array($currentOwner['name'], $groupInfo['members'])) { |
65
|
|
|
$groupList[] = $groupInfo['name']; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
if ($groupList) { |
69
|
|
|
$message .= " We recommend that you make the file group-writeable and change the group to " |
70
|
|
|
. "one of these groups:\n - " . implode("\n - ", $groupList) |
71
|
|
|
. "\n\nFor example:\nchmod g+w $filename\nchgrp " . $groupList[0] . " $filename"; |
72
|
|
|
} else { |
73
|
|
|
$message .= " There is no user-group that contains both the web-server user and the owner " |
74
|
|
|
. "of this file. Change the ownership of the file, create a new group, or temporarily " |
75
|
|
|
. "make the file writeable by everyone during the install process."; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
} else { |
79
|
|
|
$message = "The webserver user needs to be able to write to this file:\n$filename"; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return [EnvironmentCheck::ERROR, $message]; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return [EnvironmentCheck::OK, '']; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|