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
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
SettingsServer.php
241 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 | /** |
| 12 | * Contains helper methods that can be used to get information regarding the |
| 13 | * server, its settings and currently used PHP settings. |
| 14 | * |
| 15 | */ |
| 16 | class SettingsServer |
| 17 | { |
| 18 | /** |
| 19 | * Returns true if the current script execution was triggered by the cron archiving script. |
| 20 | * |
| 21 | * Helpful for error handling: directly throw error without HTML (eg. when DB is down). |
| 22 | * |
| 23 | * @return bool |
| 24 | * @api |
| 25 | */ |
| 26 | public static function isArchivePhpTriggered() |
| 27 | { |
| 28 | return !empty($_GET['trigger']) |
| 29 | && $_GET['trigger'] == 'archivephp' |
| 30 | && Piwik::hasUserSuperUserAccess(); |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Returns true if the current request is a Tracker request. |
| 35 | * |
| 36 | * @return bool true if the current request is a Tracking API Request (ie. piwik.php) |
| 37 | */ |
| 38 | public static function isTrackerApiRequest() |
| 39 | { |
| 40 | return !empty($GLOBALS['PIWIK_TRACKER_MODE']); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Mark the current request as a Tracker API request |
| 45 | */ |
| 46 | public static function setIsTrackerApiRequest() |
| 47 | { |
| 48 | $GLOBALS['PIWIK_TRACKER_MODE'] = true; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Set the current request is not a tracker API request |
| 53 | */ |
| 54 | public static function setIsNotTrackerApiRequest() |
| 55 | { |
| 56 | $GLOBALS['PIWIK_TRACKER_MODE'] = false; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Returns true if Matomo is running within Matomo for WordPress. |
| 61 | * |
| 62 | * @return bool true if Matomo is running in WordPress, false if Matomo is running as part of On-Premise |
| 63 | * @api |
| 64 | */ |
| 65 | public static function isMatomoForWordPress() |
| 66 | { |
| 67 | return defined( 'ABSPATH') && function_exists('\add_action'); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Returns `true` if running on Microsoft IIS 7 (or above), `false` if otherwise. |
| 72 | * |
| 73 | * @return bool |
| 74 | * @api |
| 75 | */ |
| 76 | public static function isIIS() |
| 77 | { |
| 78 | $iis = isset($_SERVER['SERVER_SOFTWARE']) && |
| 79 | preg_match('/^Microsoft-IIS\/(.+)/', $_SERVER['SERVER_SOFTWARE'], $matches) && |
| 80 | version_compare($matches[1], '7') >= 0; |
| 81 | |
| 82 | return $iis; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Returns `true` if running on a Windows operating system, `false` if otherwise. |
| 87 | * |
| 88 | * @since 0.6.5 |
| 89 | * @return bool |
| 90 | * @api |
| 91 | */ |
| 92 | public static function isWindows() |
| 93 | { |
| 94 | return DIRECTORY_SEPARATOR === '\\'; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Returns `true` if this PHP version/build supports timezone manipulation |
| 99 | * (e.g., php >= 5.2, or compiled with **EXPERIMENTAL_DATE_SUPPORT=1** for |
| 100 | * php < 5.2). |
| 101 | * |
| 102 | * @return bool |
| 103 | * @api |
| 104 | */ |
| 105 | public static function isTimezoneSupportEnabled() |
| 106 | { |
| 107 | return |
| 108 | function_exists('date_create') && |
| 109 | function_exists('date_default_timezone_set') && |
| 110 | function_exists('timezone_identifiers_list') && |
| 111 | function_exists('timezone_open') && |
| 112 | function_exists('timezone_offset_get'); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Returns `true` if the GD PHP extension is available, `false` if otherwise. |
| 117 | * |
| 118 | * _Note: ImageGraph and the sparkline report visualization depend on the GD extension._ |
| 119 | * |
| 120 | * @return bool |
| 121 | * @api |
| 122 | */ |
| 123 | public static function isGdExtensionEnabled() |
| 124 | { |
| 125 | static $gd = null; |
| 126 | if (is_null($gd)) { |
| 127 | $gd = false; |
| 128 | |
| 129 | $extensions = @get_loaded_extensions(); |
| 130 | if (is_array($extensions)) { |
| 131 | $gd = in_array('gd', $extensions) && function_exists('imageftbbox'); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | return $gd; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Raise PHP memory limit if below the minimum required |
| 140 | * |
| 141 | * @return bool true if set; false otherwise |
| 142 | */ |
| 143 | public static function raiseMemoryLimitIfNecessary() |
| 144 | { |
| 145 | if (self::isArchivePhpTriggered()) { |
| 146 | // core:archive command: no time limit |
| 147 | self::setMaxExecutionTime( 0 ); |
| 148 | } |
| 149 | |
| 150 | $memoryLimit = self::getMemoryLimitValue(); |
| 151 | if ($memoryLimit === false) { |
| 152 | return false; |
| 153 | } |
| 154 | $minimumMemoryLimit = Config::getInstance()->General['minimum_memory_limit']; |
| 155 | |
| 156 | if (self::isArchivePhpTriggered()) { |
| 157 | // core:archive command: high memory limit |
| 158 | $minimumMemoryLimitWhenArchiving = Config::getInstance()->General['minimum_memory_limit_when_archiving']; |
| 159 | if ($memoryLimit < $minimumMemoryLimitWhenArchiving) { |
| 160 | return self::setMemoryLimit($minimumMemoryLimitWhenArchiving); |
| 161 | } |
| 162 | return false; |
| 163 | } |
| 164 | if ($memoryLimit < $minimumMemoryLimit) { |
| 165 | return self::setMemoryLimit($minimumMemoryLimit); |
| 166 | } |
| 167 | return false; |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Set PHP memory limit |
| 172 | * |
| 173 | * Note: system settings may prevent scripts from overriding the master value |
| 174 | * |
| 175 | * @param int $minimumMemoryLimit |
| 176 | * @return bool true if set; false otherwise |
| 177 | */ |
| 178 | protected static function setMemoryLimit($minimumMemoryLimit) |
| 179 | { |
| 180 | // in Megabytes |
| 181 | $currentValue = self::getMemoryLimitValue(); |
| 182 | if ($currentValue === false |
| 183 | || ($currentValue < $minimumMemoryLimit && @ini_set('memory_limit', $minimumMemoryLimit . 'M')) |
| 184 | ) { |
| 185 | return true; |
| 186 | } |
| 187 | return false; |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Get php memory_limit (in Megabytes) |
| 192 | * |
| 193 | * Prior to PHP 5.2.1, or on Windows, --enable-memory-limit is not a |
| 194 | * compile-time default, so ini_get('memory_limit') may return false. |
| 195 | * |
| 196 | * @see http://www.php.net/manual/en/faq.using.php#faq.using.shorthandbytes |
| 197 | * @return int|bool memory limit in megabytes, or false if there is no limit |
| 198 | */ |
| 199 | public static function getMemoryLimitValue() |
| 200 | { |
| 201 | if (($memory = ini_get('memory_limit')) > 0) { |
| 202 | // handle shorthand byte options (case-insensitive) |
| 203 | $shorthandByteOption = substr($memory, -1); |
| 204 | switch ($shorthandByteOption) { |
| 205 | case 'G': |
| 206 | case 'g': |
| 207 | return substr($memory, 0, -1) * 1024; |
| 208 | case 'M': |
| 209 | case 'm': |
| 210 | return substr($memory, 0, -1); |
| 211 | case 'K': |
| 212 | case 'k': |
| 213 | return substr($memory, 0, -1) / 1024; |
| 214 | } |
| 215 | return $memory / 1048576; |
| 216 | } |
| 217 | |
| 218 | // no memory limit |
| 219 | return false; |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Set maximum script execution time. |
| 224 | * |
| 225 | * @param int $executionTime max execution time in seconds (0 = no limit) |
| 226 | */ |
| 227 | public static function setMaxExecutionTime($executionTime) |
| 228 | { |
| 229 | // in the event one or the other is disabled... |
| 230 | @ini_set('max_execution_time', $executionTime); |
| 231 | if (function_exists('set_time_limit')) { |
| 232 | @set_time_limit($executionTime); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | public static function isMac() |
| 237 | { |
| 238 | return defined('PHP_OS') && PHP_OS === 'Darwin'; |
| 239 | } |
| 240 | } |
| 241 |