PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/core/SettingsServer.php

https://github.com/CodeYellowBV/piwik
PHP | 212 lines | 160 code | 8 blank | 44 comment | 3 complexity | 184f935416262e0e92f3f0e83b5ddc6f MD5 | raw file
Possible License(s): LGPL-3.0, JSON, MIT, GPL-3.0, LGPL-2.1, GPL-2.0, AGPL-1.0, BSD-2-Clause, BSD-3-Clause
  1. <?php
  2. /**
  3. * Piwik - free/libre analytics platform
  4. *
  5. * @link http://piwik.org
  6. * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
  7. *
  8. */
  9. namespace Piwik;
  10. /**
  11. * Contains helper methods that can be used to get information regarding the
  12. * server, its settings and currently used PHP settings.
  13. *
  14. */
  15. class SettingsServer
  16. {
  17. /**
  18. * Returns true if the current script execution was triggered by the cron archiving script.
  19. *
  20. * Helpful for error handling: directly throw error without HTML (eg. when DB is down).
  21. *
  22. * @return bool
  23. * @api
  24. */
  25. public static function isArchivePhpTriggered()
  26. {
  27. return !empty($_GET['trigger'])
  28. && $_GET['trigger'] == 'archivephp'
  29. && Piwik::hasUserSuperUserAccess();
  30. }
  31. /**
  32. * Returns true if the current request is a Tracker request.
  33. *
  34. * @return bool true if the current request is a Tracking API Request (ie. piwik.php)
  35. */
  36. public static function isTrackerApiRequest()
  37. {
  38. return !empty($GLOBALS['PIWIK_TRACKER_MODE']);
  39. }
  40. /**
  41. * Returns `true` if running on Microsoft IIS 7 (or above), `false` if otherwise.
  42. *
  43. * @return bool
  44. * @api
  45. */
  46. public static function isIIS()
  47. {
  48. $iis = isset($_SERVER['SERVER_SOFTWARE']) &&
  49. preg_match('/^Microsoft-IIS\/(.+)/', $_SERVER['SERVER_SOFTWARE'], $matches) &&
  50. version_compare($matches[1], '7') >= 0;
  51. return $iis;
  52. }
  53. /**
  54. * Returns `true` if running on an Apache web server, `false` if otherwise.
  55. *
  56. * @return bool
  57. * @api
  58. */
  59. public static function isApache()
  60. {
  61. $apache = isset($_SERVER['SERVER_SOFTWARE']) &&
  62. !strncmp($_SERVER['SERVER_SOFTWARE'], 'Apache', 6);
  63. return $apache;
  64. }
  65. /**
  66. * Returns `true` if running on a Windows operating system, `false` if otherwise.
  67. *
  68. * @since 0.6.5
  69. * @return bool
  70. * @api
  71. */
  72. public static function isWindows()
  73. {
  74. return DIRECTORY_SEPARATOR === '\\';
  75. }
  76. /**
  77. * Returns `true` if this PHP version/build supports timezone manipulation
  78. * (e.g., php >= 5.2, or compiled with **EXPERIMENTAL_DATE_SUPPORT=1** for
  79. * php < 5.2).
  80. *
  81. * @return bool
  82. * @api
  83. */
  84. public static function isTimezoneSupportEnabled()
  85. {
  86. return
  87. function_exists('date_create') &&
  88. function_exists('date_default_timezone_set') &&
  89. function_exists('timezone_identifiers_list') &&
  90. function_exists('timezone_open') &&
  91. function_exists('timezone_offset_get');
  92. }
  93. /**
  94. * Returns `true` if the GD PHP extension is available, `false` if otherwise.
  95. *
  96. * _Note: ImageGraph and the sparkline report visualization depend on the GD extension._
  97. *
  98. * @return bool
  99. * @api
  100. */
  101. public static function isGdExtensionEnabled()
  102. {
  103. static $gd = null;
  104. if (is_null($gd)) {
  105. $extensions = @get_loaded_extensions();
  106. $gd = in_array('gd', $extensions) && function_exists('imageftbbox');
  107. }
  108. return $gd;
  109. }
  110. /**
  111. * Raise PHP memory limit if below the minimum required
  112. *
  113. * @return bool true if set; false otherwise
  114. */
  115. public static function raiseMemoryLimitIfNecessary()
  116. {
  117. $memoryLimit = self::getMemoryLimitValue();
  118. if ($memoryLimit === false) {
  119. return false;
  120. }
  121. $minimumMemoryLimit = Config::getInstance()->General['minimum_memory_limit'];
  122. if (self::isArchivePhpTriggered()) {
  123. // core:archive command: no time limit, high memory limit
  124. self::setMaxExecutionTime(0);
  125. $minimumMemoryLimitWhenArchiving = Config::getInstance()->General['minimum_memory_limit_when_archiving'];
  126. if ($memoryLimit < $minimumMemoryLimitWhenArchiving) {
  127. return self::setMemoryLimit($minimumMemoryLimitWhenArchiving);
  128. }
  129. return false;
  130. }
  131. if ($memoryLimit < $minimumMemoryLimit) {
  132. return self::setMemoryLimit($minimumMemoryLimit);
  133. }
  134. return false;
  135. }
  136. /**
  137. * Set PHP memory limit
  138. *
  139. * Note: system settings may prevent scripts from overriding the master value
  140. *
  141. * @param int $minimumMemoryLimit
  142. * @return bool true if set; false otherwise
  143. */
  144. protected static function setMemoryLimit($minimumMemoryLimit)
  145. {
  146. // in Megabytes
  147. $currentValue = self::getMemoryLimitValue();
  148. if ($currentValue === false
  149. || ($currentValue < $minimumMemoryLimit && @ini_set('memory_limit', $minimumMemoryLimit . 'M'))
  150. ) {
  151. return true;
  152. }
  153. return false;
  154. }
  155. /**
  156. * Get php memory_limit (in Megabytes)
  157. *
  158. * Prior to PHP 5.2.1, or on Windows, --enable-memory-limit is not a
  159. * compile-time default, so ini_get('memory_limit') may return false.
  160. *
  161. * @see http://www.php.net/manual/en/faq.using.php#faq.using.shorthandbytes
  162. * @return int|bool memory limit in megabytes, or false if there is no limit
  163. */
  164. public static function getMemoryLimitValue()
  165. {
  166. if (($memory = ini_get('memory_limit')) > 0) {
  167. // handle shorthand byte options (case-insensitive)
  168. $shorthandByteOption = substr($memory, -1);
  169. switch ($shorthandByteOption) {
  170. case 'G':
  171. case 'g':
  172. return substr($memory, 0, -1) * 1024;
  173. case 'M':
  174. case 'm':
  175. return substr($memory, 0, -1);
  176. case 'K':
  177. case 'k':
  178. return substr($memory, 0, -1) / 1024;
  179. }
  180. return $memory / 1048576;
  181. }
  182. // no memory limit
  183. return false;
  184. }
  185. /**
  186. * Set maximum script execution time.
  187. *
  188. * @param int $executionTime max execution time in seconds (0 = no limit)
  189. */
  190. public static function setMaxExecutionTime($executionTime)
  191. {
  192. // in the event one or the other is disabled...
  193. @ini_set('max_execution_time', $executionTime);
  194. @set_time_limit($executionTime);
  195. }
  196. }