6 * @copyright (c) 2008 phpBB Group
7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
14 if (!defined('IN_PHPBB'))
20 * System-specific methods. For example chmod(), unlink()...
24 class phpbb_system
extends phpbb_plugin_support
27 * @var array required phpBB objects
29 public $phpbb_required = array();
32 * @var array Optional phpBB objects
34 public $phpbb_optional = array();
37 * @var array Holding some information for chmod()
39 private $chmod_info = array();
42 * Method for chmodding directories and files for internal use.
44 * This function determines owner and group whom the file belongs to and user and group of PHP and then set safest possible file permissions.
45 * The function determines owner and group from common.php file and sets the same to the provided file.
46 * The function uses bit fields to build the permissions.
47 * The function sets the appropiate execute bit on directories.
49 * Supported constants representing bit fields are:
51 * phpbb::CHMOD_ALL - all permissions (7)
52 * phpbb::CHMOD_READ - read permission (4)
53 * phpbb::CHMOD_WRITE - write permission (2)
54 * phpbb::CHMOD_EXECUTE - execute permission (1)
56 * NOTE: The function uses POSIX extension and fileowner()/filegroup() functions. If any of them is disabled, this function tries to build proper permissions, by calling is_readable() and is_writable() functions.
58 * @param string $filename The file/directory to be chmodded
59 * @param int $perms Permissions to set
61 * @return bool true on success, otherwise false
62 * @author faw, phpBB Group
65 public function chmod($filename, $perms = phpbb
::CHMOD_READ
)
67 // Return if the file no longer exists.
68 if (!file_exists($filename))
73 // Determine some common vars
74 if (empty($this->chmod_info
))
76 if (!function_exists('fileowner') ||
!function_exists('filegroup'))
78 // No need to further determine owner/group - it is unknown
79 $this->chmod_info
['process'] = false;
83 // Determine owner/group of common.php file and the filename we want to change here
84 $common_php_owner = fileowner(PHPBB_ROOT_PATH
. 'common.' . PHP_EXT
);
85 $common_php_group = filegroup(PHPBB_ROOT_PATH
. 'common.' . PHP_EXT
);
87 // And the owner and the groups PHP is running under.
88 $php_uid = (function_exists('posix_getuid')) ? @posix_getuid
() : false;
89 $php_gids = (function_exists('posix_getgroups')) ? @posix_getgroups
() : false;
91 if (!$php_uid ||
empty($php_gids) ||
!$common_php_owner ||
!$common_php_group)
93 $this->chmod_info
['process'] = false;
97 $this->chmod_info
= array(
99 'common_owner' => $common_php_owner,
100 'common_group' => $common_php_group,
101 'php_uid' => $php_uid,
102 'php_gids' => $php_gids,
108 if ($this->chmod_info
['process'])
111 if (@chown
($filename, $this->chmod_info
['common_owner']))
114 $file_uid = fileowner($filename);
118 if (@chgrp
($filename, $this->chmod_info
['common_group']))
121 $file_gid = filegroup($filename);
124 // If the file_uid/gid now match the one from common.php we can process further, else we are not able to change something
125 if ($file_uid != $this->chmod_info
['common_owner'] ||
$file_gid != $this->chmod_info
['common_group'])
127 $this->chmod_info
['process'] = false;
131 // Still able to process?
132 if ($this->chmod_info
['process'])
134 if ($file_uid == $this->chmod_info
['php_uid'])
138 else if (in_array($file_gid, $this->chmod_info
['php_gids']))
144 // Since we are setting the everyone bit anyway, no need to do expensive operations
145 $this->chmod_info
['process'] = false;
149 // We are not able to determine or change something
150 if (!$this->chmod_info
['process'])
155 // Owner always has read/write permission
156 $owner = phpbb
::CHMOD_READ | phpbb
::CHMOD_WRITE
;
157 if (is_dir($filename))
159 $owner |
= phpbb
::CHMOD_EXECUTE
;
161 // Only add execute bit to the permission if the dir needs to be readable
162 if ($perms & phpbb
::CHMOD_READ
)
164 $perms |
= phpbb
::CHMOD_EXECUTE
;
171 $result = @chmod
($filename, ($owner << 6) +
(0 << 3) +
(0 << 0));
175 if (!is_null($php) ||
(is_readable($filename) && is_writable($filename)))
181 $result = @chmod
($filename, ($owner << 6) +
($perms << 3) +
(0 << 0));
185 if (!is_null($php) ||
((!($perms & phpbb
::CHMOD_READ
) ||
is_readable($filename)) && (!($perms & phpbb
::CHMOD_WRITE
) ||
is_writable($filename))))
191 $result = @chmod
($filename, ($owner << 6) +
($perms << 3) +
($perms << 0));
195 if (!is_null($php) ||
((!($perms & phpbb
::CHMOD_READ
) ||
is_readable($filename)) && (!($perms & phpbb
::CHMOD_WRITE
) ||
is_writable($filename))))