PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 4.3.0
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v4.3.0
5.11.1 5.11.0 5.10.2 5.10.1 trunk 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.3.0 1.3.1 1.3.2 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.1.0 4.1.1 4.1.2 4.1.3 4.10.0 4.11.0 4.12.0 4.13.0 4.13.2 4.13.3 4.13.4 4.13.5 4.14.0 4.14.1 4.14.2 4.15.0 4.15.1 4.15.2 4.15.3 4.2.0 4.3.0 4.3.1 4.4.1 4.4.2 4.5.0 4.6.0 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.10.0 5.2.0 5.2.1 5.2.2 5.3.0 5.3.1 5.3.2 5.3.3 5.6.0 5.6.1 5.7.0 5.7.1 5.8.0 5.8.1 5.8.2
matomo / app / core / Filechecks.php
matomo / app / core Last commit date
API 5 years ago Access 5 years ago Application 5 years ago Archive 5 years ago ArchiveProcessor 5 years ago Archiver 5 years ago AssetManager 5 years ago Auth 5 years ago Category 5 years ago CliMulti 5 years ago Columns 5 years ago Composer 5 years ago Concurrency 5 years ago Config 5 years ago Container 5 years ago CronArchive 5 years ago DataAccess 5 years ago DataFiles 5 years ago DataTable 5 years ago Db 5 years ago DeviceDetector 5 years ago Email 5 years ago Exception 5 years ago Http 5 years ago Intl 5 years ago Mail 5 years ago Measurable 5 years ago Menu 5 years ago Metrics 5 years ago Notification 5 years ago Period 5 years ago Plugin 5 years ago ProfessionalServices 5 years ago Report 5 years ago ReportRenderer 5 years ago Scheduler 5 years ago Segment 5 years ago Session 5 years ago Settings 5 years ago Tracker 5 years ago Translation 5 years ago UpdateCheck 5 years ago Updater 5 years ago Updates 5 years ago Validators 5 years ago View 5 years ago ViewDataTable 5 years ago Visualization 5 years ago Widget 5 years ago .htaccess 6 years ago Access.php 5 years ago Archive.php 5 years ago ArchiveProcessor.php 5 years ago AssetManager.php 5 years ago Auth.php 5 years ago AuthResult.php 5 years ago BaseFactory.php 5 years ago Cache.php 5 years ago CacheId.php 5 years ago CliMulti.php 5 years ago Common.php 5 years ago Config.php 5 years ago Console.php 5 years ago Context.php 5 years ago Cookie.php 5 years ago CronArchive.php 5 years ago DataArray.php 5 years ago DataTable.php 5 years ago Date.php 5 years ago Db.php 5 years ago DbHelper.php 5 years ago Development.php 5 years ago ErrorHandler.php 5 years ago EventDispatcher.php 5 years ago ExceptionHandler.php 5 years ago FileIntegrity.php 5 years ago Filechecks.php 5 years ago Filesystem.php 5 years ago FrontController.php 5 years ago Http.php 5 years ago IP.php 5 years ago Log.php 5 years ago LogDeleter.php 5 years ago Mail.php 5 years ago Metrics.php 5 years ago NoAccessException.php 5 years ago Nonce.php 5 years ago Notification.php 5 years ago NumberFormatter.php 5 years ago Option.php 5 years ago Period.php 5 years ago Piwik.php 5 years ago Plugin.php 5 years ago Profiler.php 5 years ago ProxyHeaders.php 5 years ago ProxyHttp.php 5 years ago QuickForm2.php 5 years ago RankingQuery.php 5 years ago ReportRenderer.php 5 years ago Segment.php 5 years ago Sequence.php 5 years ago Session.php 5 years ago SettingsPiwik.php 5 years ago SettingsServer.php 5 years ago Singleton.php 5 years ago Site.php 5 years ago SupportedBrowser.php 5 years ago TCPDF.php 5 years ago Theme.php 5 years ago Timer.php 5 years ago Tracker.php 5 years ago Twig.php 5 years ago Unzip.php 5 years ago UpdateCheck.php 5 years ago Updater.php 5 years ago UpdaterErrorException.php 5 years ago Updates.php 5 years ago Url.php 5 years ago UrlHelper.php 5 years ago Version.php 5 years ago View.php 5 years ago bootstrap.php 5 years ago dispatch.php 5 years ago testMinimumPhpVersion.php 5 years ago
Filechecks.php
253 lines
1 <?php
2 /**
3 * Matomo - free/libre analytics platform
4 *
5 * @link https://matomo.org
6 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
7 *
8 */
9 namespace Piwik;
10
11 use Piwik\Exception\MissingFilePermissionException;
12
13 class Filechecks
14 {
15 /**
16 * Check if this installation can be auto-updated.
17 * For performance, we look for clues rather than an exhaustive test.
18 *
19 * @return bool
20 */
21 public static function canAutoUpdate()
22 {
23 if (!is_writable(PIWIK_INCLUDE_PATH . '/') ||
24 !is_writable(PIWIK_DOCUMENT_ROOT . '/index.php') ||
25 !is_writable(PIWIK_INCLUDE_PATH . '/core') ||
26 !is_writable(PIWIK_DOCUMENT_ROOT . '/config/global.ini.php')
27 ) {
28 return false;
29 }
30 return true;
31 }
32
33 /**
34 * Checks if directories are writable and create them if they do not exist.
35 *
36 * @param array $directoriesToCheck array of directories to check - if not given default Piwik directories that needs write permission are checked
37 * @return array directory name => true|false (is writable)
38 */
39 public static function checkDirectoriesWritable($directoriesToCheck)
40 {
41 $resultCheck = array();
42 foreach ($directoriesToCheck as $directoryToCheck) {
43 Filesystem::mkdir($directoryToCheck);
44
45 $directory = Filesystem::realpath($directoryToCheck);
46 if ($directory !== false) {
47 $resultCheck[$directory] = is_writable($directoryToCheck);
48 }
49 }
50 return $resultCheck;
51 }
52
53 /**
54 * Checks that the directories Piwik needs write access are actually writable
55 * Displays a nice error page if permissions are missing on some directories
56 *
57 * @param array $directoriesToCheck Array of directory names to check
58 */
59 public static function dieIfDirectoriesNotWritable($directoriesToCheck = null)
60 {
61 $resultCheck = self::checkDirectoriesWritable($directoriesToCheck);
62 if (array_search(false, $resultCheck) === false) {
63 return;
64 }
65
66 $directoryList = '';
67 foreach ($resultCheck as $dir => $bool) {
68 $realpath = Filesystem::realpath($dir);
69 if (!empty($realpath) && $bool === false) {
70 $directoryList .= self::getMakeWritableCommand($realpath);
71 }
72 }
73
74 // Also give the chown since the chmod is only 755
75 if (!SettingsServer::isWindows()) {
76 $realpath = Filesystem::realpath(PIWIK_INCLUDE_PATH . '/');
77 $directoryList = "<code>chown -R ". self::getUserAndGroup() ." " . $realpath . "</code><br />" . $directoryList;
78 }
79
80 if (function_exists('shell_exec')) {
81 $currentUser = self::getUser();
82 if (!empty($currentUser)) {
83 $optionalUserInfo = " (running as user '" . $currentUser . "')";
84 }
85 }
86
87 $directoryMessage = "<p><b>Matomo couldn't write to some directories $optionalUserInfo</b>.</p>";
88 $directoryMessage .= "<p>Try to Execute the following commands on your server, to allow Write access on these directories"
89 . ":</p>"
90 . "<blockquote>$directoryList</blockquote>"
91 . "<p>If this doesn't work, you can try to create the directories with your FTP software, and set the CHMOD to 0755 (or 0777 if 0755 is not enough). To do so with your FTP software, right click on the directories then click permissions.</p>"
92 . "<p>After applying the modifications, you can <a href='index.php'>refresh the page</a>.</p>"
93 . "<p>If you need more help, try <a target='_blank' rel='noreferrer noopener' href='https://matomo.org'>Matomo.org</a>.</p>";
94
95 $ex = new MissingFilePermissionException($directoryMessage);
96 $ex->setIsHtmlMessage();
97
98 throw $ex;
99 }
100
101 /**
102 * Returns the help message when the auto update can't run because of missing permissions
103 *
104 * @return string
105 */
106 public static function getAutoUpdateMakeWritableMessage()
107 {
108 $realpath = Filesystem::realpath(PIWIK_INCLUDE_PATH . '/');
109 $message = '';
110 $message .= "<br /><code>" . self::getCommandToChangeOwnerOfPiwikFiles() . "</code><br />";
111 $message .= self::getMakeWritableCommand($realpath);
112 $message .= '<code>chmod 755 '.$realpath.'/console</code><br />';
113 $message .= 'After you execute these commands (or change permissions via your FTP software), refresh the page and you should be able to use the "Automatic Update" feature.';
114 return $message;
115 }
116
117 /**
118 * Returns friendly error message explaining how to fix permissions
119 *
120 * @param string $path to the directory missing permissions
121 * @return string Error message
122 */
123 public static function getErrorMessageMissingPermissions($path)
124 {
125 $message = "Please check that the web server has enough permission to write to these files/directories:<br />";
126
127 if (SettingsServer::isWindows()) {
128 $message .= "On Windows, check that the folder is not read only and is writable.\n
129 You can try to execute:<br />";
130 } else {
131 $message .= "For example, on a GNU/Linux server if your Apache httpd user is "
132 . Common::sanitizeInputValue(self::getUser())
133 . ", you can try to execute:<br />\n"
134 . "<code>chown -R ". Common::sanitizeInputValue(self::getUserAndGroup()) ." " . Common::sanitizeInputValue($path) . "</code><br />";
135 }
136
137 $message .= self::getMakeWritableCommand($path);
138
139 return $message;
140 }
141
142 public static function getUserAndGroup()
143 {
144 $user = self::getUser();
145 if (!function_exists('shell_exec')) {
146 return $user . ':' . $user;
147 }
148
149 $group = trim(shell_exec('groups '. $user .' | cut -f3 -d" "'));
150
151 if (empty($group) && function_exists('posix_getegid') && function_exists('posix_getgrgid')) {
152 $currentGroupId = posix_getegid();
153
154 $group = posix_getpwuid($currentGroupId);
155 if (!empty($group['name'])) {
156 $group = $group['name'];
157 } else {
158 $group = $currentGroupId;
159 }
160 }
161
162 if (empty($group)) {
163 $group = 'www-data';
164 }
165
166 return $user . ':' . $group;
167 }
168
169 public static function getUser()
170 {
171 if (function_exists('shell_exec')) {
172 return trim(shell_exec('whoami'));
173 }
174
175 $currentUser = get_current_user();
176
177 if (empty($currentUser) && function_exists('posix_geteuid') && function_exists('posix_getpwuid')) {
178 $currentUserId = posix_geteuid();
179
180 $user = posix_getpwuid($currentUserId);
181 if (!empty($user['name'])) {
182 $currentUser = $user['name'];
183 } else {
184 $currentUser = $currentUserId;
185 }
186 }
187
188 if (empty($currentUser)) {
189 $currentUser = 'www-data';
190 }
191
192 return $currentUser;
193 }
194
195 /**
196 * Returns the help text displayed to suggest which command to run to give writable access to a file or directory
197 *
198 * @param string $realpath
199 * @return string
200 */
201 private static function getMakeWritableCommand($realpath)
202 {
203 $realpath = Common::sanitizeInputValue($realpath);
204 if (SettingsServer::isWindows()) {
205 return "<code>cacls $realpath /t /g " . Common::sanitizeInputValue(self::getUser()) . ":f</code><br />\n";
206 }
207 return "<code>find $realpath -type f -exec chmod 644 {} \;</code><br /><code>find $realpath -type d -exec chmod 755 {} \;</code><br />";
208 }
209
210 /**
211 * @return string
212 */
213 public static function getCommandToChangeOwnerOfPiwikFiles()
214 {
215 $realpath = Filesystem::realpath(PIWIK_INCLUDE_PATH . '/');
216 return "chown -R " . self::getUserAndGroup() . " " . $realpath;
217 }
218
219 public static function getOwnerOfPiwikFiles()
220 {
221 $index = Filesystem::realpath(PIWIK_INCLUDE_PATH . '/index.php');
222 $stat = stat($index);
223 if (!$stat) {
224 return '';
225 }
226
227 if (function_exists('posix_getgrgid')) {
228 $group = posix_getgrgid($stat[5]);
229
230 if (!empty($group['name'])) {
231 $group = $group['name'];
232 } else {
233 $group = $stat[5];
234 }
235 } else {
236 return '';
237 }
238
239 if (function_exists('posix_getpwuid')) {
240 $user = posix_getpwuid($stat[4]);
241 if (!empty($user['name'])) {
242 $user = $user['name'];
243 } else {
244 $user = $stat[4];
245 }
246 } else {
247 return '';
248 }
249
250 return "$user:$group";
251 }
252 }
253