PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 4.13.2
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v4.13.2
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 3 years ago Access 3 years ago Application 4 years ago Archive 3 years ago ArchiveProcessor 3 years ago Archiver 5 years ago AssetManager 3 years ago Auth 3 years ago Category 5 years ago Changes 4 years ago CliMulti 4 years ago Columns 3 years ago Concurrency 3 years ago Config 4 years ago Container 4 years ago CronArchive 3 years ago DataAccess 3 years ago DataFiles 5 years ago DataTable 3 years ago Db 3 years ago DeviceDetector 3 years ago Email 5 years ago Exception 3 years ago Http 4 years ago Intl 4 years ago Mail 3 years ago Measurable 5 years ago Menu 3 years ago Metrics 4 years ago Notification 4 years ago Period 4 years ago Plugin 3 years ago ProfessionalServices 4 years ago Report 5 years ago ReportRenderer 3 years ago Scheduler 4 years ago Segment 3 years ago Session 4 years ago Settings 3 years ago Tracker 3 years ago Translation 4 years ago UpdateCheck 5 years ago Updater 3 years ago Updates 3 years ago Validators 4 years ago View 4 years ago ViewDataTable 3 years ago Visualization 4 years ago Widget 5 years ago .htaccess 6 years ago Access.php 4 years ago Archive.php 4 years ago ArchiveProcessor.php 4 years ago AssetManager.php 4 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 4 years ago Common.php 3 years ago Config.php 3 years ago Console.php 3 years ago Context.php 5 years ago Cookie.php 3 years ago CronArchive.php 3 years ago DataArray.php 4 years ago DataTable.php 3 years ago Date.php 4 years ago Db.php 4 years ago DbHelper.php 3 years ago Development.php 5 years ago ErrorHandler.php 5 years ago EventDispatcher.php 3 years ago ExceptionHandler.php 3 years ago FileIntegrity.php 5 years ago Filechecks.php 4 years ago Filesystem.php 3 years ago FrontController.php 3 years ago Http.php 3 years ago IP.php 4 years ago Log.php 4 years ago LogDeleter.php 4 years ago Mail.php 4 years ago Metrics.php 3 years ago NoAccessException.php 5 years ago Nonce.php 3 years ago Notification.php 5 years ago NumberFormatter.php 4 years ago Option.php 4 years ago Period.php 5 years ago Piwik.php 3 years ago Plugin.php 4 years ago Profiler.php 4 years ago ProxyHeaders.php 5 years ago ProxyHttp.php 3 years ago QuickForm2.php 5 years ago RankingQuery.php 3 years ago ReportRenderer.php 4 years ago Segment.php 3 years ago Sequence.php 5 years ago Session.php 3 years ago SettingsPiwik.php 3 years ago SettingsServer.php 5 years ago Singleton.php 5 years ago Site.php 3 years ago SiteContentDetector.php 3 years ago SupportedBrowser.php 3 years ago TCPDF.php 5 years ago Theme.php 5 years ago Timer.php 5 years ago Tracker.php 4 years ago Twig.php 4 years ago Unzip.php 5 years ago UpdateCheck.php 5 years ago Updater.php 4 years ago UpdaterErrorException.php 5 years ago Updates.php 5 years ago Url.php 3 years ago UrlHelper.php 3 years ago Version.php 3 years ago View.php 3 years ago bootstrap.php 3 years ago dispatch.php 5 years ago testMinimumPhpVersion.php 3 years ago
Filechecks.php
257 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 if (!SettingsServer::isWindows()) {
111 $message .= "<br /><code>" . self::getCommandToChangeOwnerOfPiwikFiles() . "</code><br />";
112 }
113 $message .= self::getMakeWritableCommand($realpath);
114 if (!SettingsServer::isWindows()) {
115 $message .= '<code>chmod 755 ' . $realpath . '/console</code><br />';
116 }
117 $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.';
118 return $message;
119 }
120
121 /**
122 * Returns friendly error message explaining how to fix permissions
123 *
124 * @param string $path to the directory missing permissions
125 * @return string Error message
126 */
127 public static function getErrorMessageMissingPermissions($path)
128 {
129 $message = "Please check that the web server has enough permission to write to these files/directories:<br />";
130
131 if (SettingsServer::isWindows()) {
132 $message .= "On Windows, check that the folder is not read only and is writable.\n
133 You can try to execute:<br />";
134 } else {
135 $message .= "For example, on a GNU/Linux server if your Apache httpd user is "
136 . Common::sanitizeInputValue(self::getUser())
137 . ", you can try to execute:<br />\n"
138 . "<code>chown -R ". Common::sanitizeInputValue(self::getUserAndGroup()) ." " . Common::sanitizeInputValue($path) . "</code><br />";
139 }
140
141 $message .= self::getMakeWritableCommand($path);
142
143 return $message;
144 }
145
146 public static function getUserAndGroup()
147 {
148 $user = self::getUser();
149 if (!function_exists('shell_exec')) {
150 return $user . ':' . $user;
151 }
152
153 $group = trim(shell_exec('groups '. $user .' | cut -f3 -d" "'));
154
155 if (empty($group) && function_exists('posix_getegid') && function_exists('posix_getgrgid')) {
156 $currentGroupId = posix_getegid();
157
158 $group = posix_getpwuid($currentGroupId);
159 if (!empty($group['name'])) {
160 $group = $group['name'];
161 } else {
162 $group = $currentGroupId;
163 }
164 }
165
166 if (empty($group)) {
167 $group = 'www-data';
168 }
169
170 return $user . ':' . $group;
171 }
172
173 public static function getUser()
174 {
175 if (function_exists('shell_exec')) {
176 return trim(shell_exec('whoami'));
177 }
178
179 $currentUser = get_current_user();
180
181 if (empty($currentUser) && function_exists('posix_geteuid') && function_exists('posix_getpwuid')) {
182 $currentUserId = posix_geteuid();
183
184 $user = posix_getpwuid($currentUserId);
185 if (!empty($user['name'])) {
186 $currentUser = $user['name'];
187 } else {
188 $currentUser = $currentUserId;
189 }
190 }
191
192 if (empty($currentUser)) {
193 $currentUser = 'www-data';
194 }
195
196 return $currentUser;
197 }
198
199 /**
200 * Returns the help text displayed to suggest which command to run to give writable access to a file or directory
201 *
202 * @param string $realpath
203 * @return string
204 */
205 private static function getMakeWritableCommand($realpath)
206 {
207 $realpath = Common::sanitizeInputValue($realpath);
208 if (SettingsServer::isWindows()) {
209 return "<code>cacls $realpath /t /g " . Common::sanitizeInputValue(self::getUser()) . ":f</code><br />\n";
210 }
211 return "<code>find $realpath -type f -exec chmod 644 {} \;</code><br /><code>find $realpath -type d -exec chmod 755 {} \;</code><br />";
212 }
213
214 /**
215 * @return string
216 */
217 public static function getCommandToChangeOwnerOfPiwikFiles()
218 {
219 $realpath = Filesystem::realpath(PIWIK_INCLUDE_PATH . '/');
220 return "chown -R " . self::getUserAndGroup() . " " . $realpath;
221 }
222
223 public static function getOwnerOfPiwikFiles()
224 {
225 $index = Filesystem::realpath(PIWIK_INCLUDE_PATH . '/index.php');
226 $stat = stat($index);
227 if (!$stat) {
228 return '';
229 }
230
231 if (function_exists('posix_getgrgid')) {
232 $group = posix_getgrgid($stat[5]);
233
234 if (!empty($group['name'])) {
235 $group = $group['name'];
236 } else {
237 $group = $stat[5];
238 }
239 } else {
240 return '';
241 }
242
243 if (function_exists('posix_getpwuid')) {
244 $user = posix_getpwuid($stat[4]);
245 if (!empty($user['name'])) {
246 $user = $user['name'];
247 } else {
248 $user = $stat[4];
249 }
250 } else {
251 return '';
252 }
253
254 return "$user:$group";
255 }
256 }
257