API
6 years ago
Access
6 years ago
Application
6 years ago
Archive
6 years ago
ArchiveProcessor
6 years ago
Archiver
6 years ago
AssetManager
6 years ago
Auth
6 years ago
Category
6 years ago
CliMulti
6 years ago
Columns
6 years ago
Composer
6 years ago
Concurrency
6 years ago
Config
6 years ago
Container
6 years ago
CronArchive
6 years ago
DataAccess
5 years ago
DataFiles
6 years ago
DataTable
6 years ago
Db
6 years ago
DeviceDetector
5 years ago
Email
6 years ago
Exception
6 years ago
Http
6 years ago
Intl
6 years ago
Mail
6 years ago
Measurable
6 years ago
Menu
6 years ago
Metrics
6 years ago
Notification
6 years ago
Period
6 years ago
Plugin
6 years ago
ProfessionalServices
6 years ago
Report
6 years ago
ReportRenderer
6 years ago
Scheduler
6 years ago
Segment
6 years ago
Session
6 years ago
Settings
6 years ago
Tracker
5 years ago
Translation
6 years ago
UpdateCheck
6 years ago
Updater
6 years ago
Updates
6 years ago
Validators
6 years ago
View
6 years ago
ViewDataTable
6 years ago
Visualization
6 years ago
Widget
6 years ago
.htaccess
6 years ago
Access.php
6 years ago
Archive.php
6 years ago
ArchiveProcessor.php
6 years ago
AssetManager.php
6 years ago
Auth.php
6 years ago
BaseFactory.php
6 years ago
Cache.php
6 years ago
CacheId.php
6 years ago
CliMulti.php
6 years ago
Common.php
6 years ago
Config.php
6 years ago
Console.php
6 years ago
Context.php
6 years ago
Cookie.php
5 years ago
CronArchive.php
5 years ago
DataArray.php
6 years ago
DataTable.php
6 years ago
Date.php
6 years ago
Db.php
6 years ago
DbHelper.php
6 years ago
Development.php
6 years ago
DeviceDetectorFactory.php
6 years ago
ErrorHandler.php
6 years ago
EventDispatcher.php
6 years ago
ExceptionHandler.php
6 years ago
FileIntegrity.php
6 years ago
Filechecks.php
6 years ago
Filesystem.php
6 years ago
FrontController.php
6 years ago
Http.php
6 years ago
IP.php
6 years ago
Log.php
6 years ago
LogDeleter.php
6 years ago
Mail.php
6 years ago
Metrics.php
6 years ago
MetricsFormatter.php
6 years ago
Nonce.php
5 years ago
Notification.php
6 years ago
NumberFormatter.php
6 years ago
Option.php
5 years ago
Period.php
6 years ago
Piwik.php
6 years ago
Plugin.php
6 years ago
Profiler.php
6 years ago
ProxyHeaders.php
6 years ago
ProxyHttp.php
6 years ago
QuickForm2.php
6 years ago
RankingQuery.php
6 years ago
Registry.php
6 years ago
ReportRenderer.php
6 years ago
ScheduledTask.php
6 years ago
Segment.php
6 years ago
Sequence.php
6 years ago
Session.php
6 years ago
SettingsPiwik.php
6 years ago
SettingsServer.php
6 years ago
Singleton.php
6 years ago
Site.php
6 years ago
TCPDF.php
6 years ago
TaskScheduler.php
6 years ago
Theme.php
6 years ago
Timer.php
6 years ago
Tracker.php
6 years ago
Translate.php
6 years ago
Twig.php
6 years ago
Unzip.php
6 years ago
UpdateCheck.php
6 years ago
Updater.php
6 years ago
Updates.php
6 years ago
Url.php
6 years ago
UrlHelper.php
6 years ago
Version.php
5 years ago
View.php
6 years ago
bootstrap.php
6 years ago
dispatch.php
6 years ago
testMinimumPhpVersion.php
6 years ago
Filechecks.php
222 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Piwik - 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)) { |
| 152 | $group = 'www-data'; |
| 153 | } |
| 154 | return $user . ':' . $group; |
| 155 | } |
| 156 | |
| 157 | public static function getUser() |
| 158 | { |
| 159 | if (function_exists('shell_exec')) { |
| 160 | return trim(shell_exec('whoami')); |
| 161 | } |
| 162 | |
| 163 | $currentUser = get_current_user(); |
| 164 | if(!empty($currentUser)) { |
| 165 | return $currentUser; |
| 166 | } |
| 167 | |
| 168 | return 'www-data'; |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Returns the help text displayed to suggest which command to run to give writable access to a file or directory |
| 173 | * |
| 174 | * @param string $realpath |
| 175 | * @return string |
| 176 | */ |
| 177 | private static function getMakeWritableCommand($realpath) |
| 178 | { |
| 179 | $realpath = Common::sanitizeInputValue($realpath); |
| 180 | if (SettingsServer::isWindows()) { |
| 181 | return "<code>cacls $realpath /t /g " . Common::sanitizeInputValue(self::getUser()) . ":f</code><br />\n"; |
| 182 | } |
| 183 | return "<code>find $realpath -type f -exec chmod 644 {} \;</code><br /><code>find $realpath -type d -exec chmod 755 {} \;</code><br />"; |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * @return string |
| 188 | */ |
| 189 | public static function getCommandToChangeOwnerOfPiwikFiles() |
| 190 | { |
| 191 | $realpath = Filesystem::realpath(PIWIK_INCLUDE_PATH . '/'); |
| 192 | return "chown -R " . self::getUserAndGroup() . " " . $realpath; |
| 193 | } |
| 194 | |
| 195 | public static function getOwnerOfPiwikFiles() |
| 196 | { |
| 197 | $index = Filesystem::realpath(PIWIK_INCLUDE_PATH . '/index.php'); |
| 198 | $stat = stat($index); |
| 199 | if(!$stat) { |
| 200 | return ''; |
| 201 | } |
| 202 | |
| 203 | if (function_exists('posix_getgrgid')) { |
| 204 | $group = posix_getgrgid($stat[5]); |
| 205 | $group = $group['name']; |
| 206 | } else { |
| 207 | return ''; |
| 208 | } |
| 209 | |
| 210 | if (function_exists('posix_getpwuid')) { |
| 211 | $user = posix_getpwuid($stat[4]); |
| 212 | $user = $user['name']; |
| 213 | } else { |
| 214 | return ''; |
| 215 | } |
| 216 | |
| 217 | return "$user:$group"; |
| 218 | } |
| 219 | |
| 220 | |
| 221 | } |
| 222 |