1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Check that the given file is writable. |
5
|
|
|
*/ |
6
|
|
|
class FileWriteableCheck implements EnvironmentCheck |
|
|
|
|
7
|
|
|
{ |
8
|
|
|
/** |
9
|
|
|
* @var string |
10
|
|
|
*/ |
11
|
|
|
protected $path; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @param string $path The full path. If a relative path, it will relative to the BASE_PATH. |
15
|
|
|
*/ |
16
|
|
|
public function __construct($path) |
17
|
|
|
{ |
18
|
|
|
$this->path = $path; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @inheritdoc |
23
|
|
|
* |
24
|
|
|
* @return array |
25
|
|
|
*/ |
26
|
|
|
public function check() |
27
|
|
|
{ |
28
|
|
|
if ($this->path[0] == '/') { |
29
|
|
|
$filename = $this->path; |
30
|
|
|
} else { |
31
|
|
|
$filename = BASE_PATH . DIRECTORY_SEPARATOR . str_replace('/', DIRECTORY_SEPARATOR, $this->path); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
if (file_exists($filename)) { |
35
|
|
|
$isWriteable = is_writeable($filename); |
36
|
|
|
} else { |
37
|
|
|
$isWriteable = is_writeable(dirname($filename)); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
if (!$isWriteable) { |
41
|
|
|
if (function_exists('posix_getgroups')) { |
42
|
|
|
$userID = posix_geteuid(); |
43
|
|
|
$user = posix_getpwuid($userID); |
44
|
|
|
|
45
|
|
|
$currentOwnerID = fileowner(file_exists($filename) ? $filename : dirname($filename)); |
46
|
|
|
$currentOwner = posix_getpwuid($currentOwnerID); |
47
|
|
|
|
48
|
|
|
$message = "User '$user[name]' needs to be able to write to this file:\n$filename\n\nThe file is currently owned by '$currentOwner[name]'. "; |
49
|
|
|
|
50
|
|
|
if ($user['name'] == $currentOwner['name']) { |
51
|
|
|
$message .= "We recommend that you make the file writeable."; |
52
|
|
|
} else { |
53
|
|
|
$groups = posix_getgroups(); |
54
|
|
|
$groupList = array(); |
55
|
|
|
foreach ($groups as $group) { |
56
|
|
|
$groupInfo = posix_getgrgid($group); |
57
|
|
|
if (in_array($currentOwner['name'], $groupInfo['members'])) { |
58
|
|
|
$groupList[] = $groupInfo['name']; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
if ($groupList) { |
|
|
|
|
62
|
|
|
$message .= " We recommend that you make the file group-writeable and change the group to one of these groups:\n - ". implode("\n - ", $groupList) |
63
|
|
|
. "\n\nFor example:\nchmod g+w $filename\nchgrp " . $groupList[0] . " $filename"; |
64
|
|
|
} else { |
65
|
|
|
$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."; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
} else { |
69
|
|
|
$message = "The webserver user needs to be able to write to this file:\n$filename"; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return array(EnvironmentCheck::ERROR, $message); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return array(EnvironmentCheck::OK,''); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.