add some properties
[phpbb.git] / phpBB / includes / core / system.php
blob55dca1d3111c7093491839d7f5d628415eb8641b
1 <?php
2 /**
4 * @package core
5 * @version $Id$
6 * @copyright (c) 2008 phpBB Group
7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
9 */
11 /**
12 * @ignore
14 if (!defined('IN_PHPBB'))
16 exit();
19 /**
20 * System-specific methods. For example chmod(), unlink()...
22 * @package core
24 class phpbb_system extends phpbb_plugin_support
26 /**
27 * @var array required phpBB objects
29 public $phpbb_required = array();
31 /**
32 * @var array Optional phpBB objects
34 public $phpbb_optional = array();
36 /**
37 * @var array Holding some information for chmod()
39 private $chmod_info = array();
41 /**
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
63 * @access public
65 public function chmod($filename, $perms = phpbb::CHMOD_READ)
67 // Return if the file no longer exists.
68 if (!file_exists($filename))
70 return false;
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;
81 else
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;
95 else
97 $this->chmod_info = array(
98 'process' => true,
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'])
110 // Change owner
111 if (@chown($filename, $this->chmod_info['common_owner']))
113 clearstatcache();
114 $file_uid = fileowner($filename);
117 // Change group
118 if (@chgrp($filename, $this->chmod_info['common_group']))
120 clearstatcache();
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'])
136 $php = 'owner';
138 else if (in_array($file_gid, $this->chmod_info['php_gids']))
140 $php = 'group';
142 else
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'])
152 $php = 'other';
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;
168 switch ($php)
170 case 'owner':
171 $result = @chmod($filename, ($owner << 6) + (0 << 3) + (0 << 0));
173 clearstatcache();
175 if (!is_null($php) || (is_readable($filename) && is_writable($filename)))
177 break;
180 case 'group':
181 $result = @chmod($filename, ($owner << 6) + ($perms << 3) + (0 << 0));
183 clearstatcache();
185 if (!is_null($php) || ((!($perms & phpbb::CHMOD_READ) || is_readable($filename)) && (!($perms & phpbb::CHMOD_WRITE) || is_writable($filename))))
187 break;
190 case 'other':
191 $result = @chmod($filename, ($owner << 6) + ($perms << 3) + ($perms << 0));
193 clearstatcache();
195 if (!is_null($php) || ((!($perms & phpbb::CHMOD_READ) || is_readable($filename)) && (!($perms & phpbb::CHMOD_WRITE) || is_writable($filename))))
197 break;
200 default:
201 return false;
202 break;
205 return $result;