Completed
Push — master ( e090e4...18d79e )
by Damian
02:26
created

code/checks/FileWriteableCheck.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/**
4
 * Check that the given file is writable.
5
 */
6
class FileWriteableCheck implements EnvironmentCheck {
0 ignored issues
show
As per PSR2, the opening brace for this class should be on a new line.
Loading history...
7
	/**
8
	 * @var string
9
	 */
10
	protected $path;
11
12
	/**
13
	 * @param string $path The full path. If a relative path, it will relative to the BASE_PATH.
14
	 */
15
	function __construct($path) {
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
16
		$this->path = $path;
17
	}
18
19
	/**
20
	 * @inheritdoc
21
	 *
22
	 * @return array
23
	 */
24
	function check() {
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
25
		if($this->path[0] == '/') $filename = $this->path;
26
		else $filename = BASE_PATH . DIRECTORY_SEPARATOR . str_replace('/', DIRECTORY_SEPARATOR, $this->path);
27
		
28
		if(file_exists($filename)) $isWriteable = is_writeable($filename);
29
		else $isWriteable = is_writeable(dirname($filename));
30
		
31
		if(!$isWriteable) {
32
			if(function_exists('posix_getgroups')) {
33
				$userID = posix_geteuid();
34
				$user = posix_getpwuid($userID);
35
36
				$currentOwnerID = fileowner(file_exists($filename) ? $filename : dirname($filename) );
37
				$currentOwner = posix_getpwuid($currentOwnerID);
38
39
				$message = "User '$user[name]' needs to be able to write to this file:\n$filename\n\nThe file is currently owned by '$currentOwner[name]'.  ";
40
41
				if($user['name'] == $currentOwner['name']) {
42
					$message .= "We recommend that you make the file writeable.";
43
				} else {
44
					
45
					$groups = posix_getgroups();
46
					$groupList = array();
47
					foreach($groups as $group) {
48
						$groupInfo = posix_getgrgid($group);
49
						if(in_array($currentOwner['name'], $groupInfo['members'])) $groupList[] = $groupInfo['name'];
50
					}
51
					if($groupList) {
52
						$message .= "	We recommend that you make the file group-writeable and change the group to one of these groups:\n - ". implode("\n - ", $groupList)
53
							. "\n\nFor example:\nchmod g+w $filename\nchgrp " . $groupList[0] . " $filename";  		
54
					} else {
55
						$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.";
56
					}
57
				}
58
59
			} else {
60
				$message = "The webserver user needs to be able to write to this file:\n$filename";
61
			}
62
			
63
			return array(EnvironmentCheck::ERROR, $message);
64
		}
65
66
		return array(EnvironmentCheck::OK,'');
67
	}
68
}
69