setuputils.class.inc.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. <?php
  2. // Copyright (C) 2012 Combodo SARL
  3. //
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; version 3 of the License.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. /**
  17. * The standardized result of any pass/fail check performed by the setup
  18. * @author Erwan Taloc <erwan.taloc@combodo.com>
  19. * @author Romain Quetiez <romain.quetiez@combodo.com>
  20. * @author Denis Flaven <denis.flaven@combodo.com>
  21. * @license http://www.opensource.org/licenses/gpl-3.0.html GPL
  22. */
  23. class CheckResult
  24. {
  25. // Severity levels
  26. const ERROR = 0;
  27. const WARNING = 1;
  28. const INFO = 2;
  29. public $iSeverity;
  30. public $sLabel;
  31. public $sDescription;
  32. public function __construct($iSeverity, $sLabel, $sDescription = '')
  33. {
  34. $this->iSeverity = $iSeverity;
  35. $this->sLabel = $sLabel;
  36. $this->sDescription = $sDescription;
  37. }
  38. }
  39. /**
  40. * Namespace for storing all the functions/utilities needed by both
  41. * the setup wizard and the installation process
  42. * @author Erwan Taloc <erwan.taloc@combodo.com>
  43. * @author Romain Quetiez <romain.quetiez@combodo.com>
  44. * @author Denis Flaven <denis.flaven@combodo.com>
  45. * @license http://www.opensource.org/licenses/gpl-3.0.html GPL
  46. */
  47. class SetupUtils
  48. {
  49. const PHP_MIN_VERSION = '5.2.0';
  50. const MYSQL_MIN_VERSION = '5.0.0';
  51. const MIN_MEMORY_LIMIT = 33554432; // = 32*1024*1024 Beware: Computations are not allowed in defining constants
  52. const SUHOSIN_GET_MAX_VALUE_LENGTH = 2048;
  53. /**
  54. * Check the version of PHP, the needed PHP extension and a number
  55. * of configuration parameters (memory_limit, max_upload_file_size, etc...)
  56. * @param SetupPage $oP The page used only for its 'log' method
  57. * @return array An array of CheckResults objects
  58. */
  59. static function CheckPHPVersion(SetupPage $oP)
  60. {
  61. $aResult = array();
  62. $bResult = true;
  63. $aErrors = array();
  64. $aWarnings = array();
  65. $aOk = array();
  66. $oP->log('Info - CheckPHPVersion');
  67. if (version_compare(phpversion(), self::PHP_MIN_VERSION, '>='))
  68. {
  69. $aResult[] = new CheckResult(CheckResult::INFO, "The current PHP Version (".phpversion().") is greater than the minimum required version (".self::PHP_MIN_VERSION.")");
  70. }
  71. else
  72. {
  73. $aResult[] = new CheckResult(CheckResult::ERROR, "Error: The current PHP Version (".phpversion().") is lower than the minimum required version (".self::PHP_MIN_VERSION.")");
  74. }
  75. $aMandatoryExtensions = array('mysqli', 'iconv', 'simplexml', 'soap', 'hash', 'json', 'session', 'pcre', 'dom');
  76. $aOptionalExtensions = array('mcrypt' => 'Strong encryption will not be used.',
  77. 'ldap' => 'LDAP authentication will be disabled.');
  78. asort($aMandatoryExtensions); // Sort the list to look clean !
  79. ksort($aOptionalExtensions); // Sort the list to look clean !
  80. $aExtensionsOk = array();
  81. $aMissingExtensions = array();
  82. $aMissingExtensionsLinks = array();
  83. // First check the mandatory extensions
  84. foreach($aMandatoryExtensions as $sExtension)
  85. {
  86. if (extension_loaded($sExtension))
  87. {
  88. $aExtensionsOk[] = $sExtension;
  89. }
  90. else
  91. {
  92. $aMissingExtensions[] = $sExtension;
  93. $aMissingExtensionsLinks[] = "<a href=\"http://www.php.net/manual/en/book.$sExtension.php\" target=\"_blank\">$sExtension</a>";
  94. }
  95. }
  96. if (count($aExtensionsOk) > 0)
  97. {
  98. $aResult[] = new CheckResult(CheckResult::INFO, "Required PHP extension(s): ".implode(', ', $aExtensionsOk).".");
  99. }
  100. if (count($aMissingExtensions) > 0)
  101. {
  102. $aResult[] = new CheckResult(CheckResult::ERROR, "Missing PHP extension(s): ".implode(', ', $aMissingExtensionsLinks).".");
  103. }
  104. // Next check the optional extensions
  105. $aExtensionsOk = array();
  106. $aMissingExtensions = array();
  107. foreach($aOptionalExtensions as $sExtension => $sMessage)
  108. {
  109. if (extension_loaded($sExtension))
  110. {
  111. $aExtensionsOk[] = $sExtension;
  112. }
  113. else
  114. {
  115. $aMissingExtensions[$sExtension] = $sMessage;
  116. }
  117. }
  118. if (count($aExtensionsOk) > 0)
  119. {
  120. $aResult[] = new CheckResult(CheckResult::INFO, "Optional PHP extension(s): ".implode(', ', $aExtensionsOk).".");
  121. }
  122. if (count($aMissingExtensions) > 0)
  123. {
  124. foreach($aMissingExtensions as $sExtension => $sMessage)
  125. {
  126. $aResult[] = new CheckResult(CheckResult::WARNING, "Missing optional PHP extension: $sExtension. ".$sMessage);
  127. }
  128. }
  129. // Check some ini settings here
  130. if (function_exists('php_ini_loaded_file')) // PHP >= 5.2.4
  131. {
  132. $sPhpIniFile = php_ini_loaded_file();
  133. // Other included/scanned files
  134. if ($sFileList = php_ini_scanned_files())
  135. {
  136. if (strlen($sFileList) > 0)
  137. {
  138. $aFiles = explode(',', $sFileList);
  139. foreach ($aFiles as $sFile)
  140. {
  141. $sPhpIniFile .= ', '.trim($sFile);
  142. }
  143. }
  144. }
  145. $oP->log("Info - php.ini file(s): '$sPhpIniFile'");
  146. }
  147. else
  148. {
  149. $sPhpIniFile = 'php.ini';
  150. }
  151. if (!ini_get('file_uploads'))
  152. {
  153. $aResult[] = new CheckResult(CheckResult::ERROR, "Files upload is not allowed on this server (file_uploads = ".ini_get('file_uploads').").");
  154. }
  155. $sUploadTmpDir = self::GetUploadTmpDir();
  156. if (empty($sUploadTmpDir))
  157. {
  158. $sUploadTmpDir = '/tmp';
  159. $aResult[] = new CheckResult(CheckResult::WARNING, "Temporary directory for files upload is not defined (upload_tmp_dir), assuming that $sUploadTmpDir is used.");
  160. }
  161. // check that the upload directory is indeed writable from PHP
  162. if (!empty($sUploadTmpDir))
  163. {
  164. if (!file_exists($sUploadTmpDir))
  165. {
  166. $aResult[] = new CheckResult(CheckResult::ERROR, "Temporary directory for files upload ($sUploadTmpDir) does not exist or cannot be read by PHP.");
  167. }
  168. else if (!is_writable($sUploadTmpDir))
  169. {
  170. $aResult[] = new CheckResult(CheckResult::ERROR, "Temporary directory for files upload ($sUploadTmpDir) is not writable.");
  171. }
  172. else
  173. {
  174. $oP->log("Info - Temporary directory for files upload ($sUploadTmpDir) is writable.");
  175. }
  176. }
  177. if (!ini_get('upload_max_filesize'))
  178. {
  179. $aResult[] = new CheckResult(CheckResult::ERROR, "File upload is not allowed on this server (upload_max_filesize = ".ini_get('upload_max_filesize').").");
  180. }
  181. $iMaxFileUploads = ini_get('max_file_uploads');
  182. if (!empty($iMaxFileUploads) && ($iMaxFileUploads < 1))
  183. {
  184. $aResult[] = new CheckResult(CheckResult::ERROR, "File upload is not allowed on this server (max_file_uploads = ".ini_get('max_file_uploads').").");
  185. }
  186. $iMaxUploadSize = utils::ConvertToBytes(ini_get('upload_max_filesize'));
  187. $iMaxPostSize = utils::ConvertToBytes(ini_get('post_max_size'));
  188. if ($iMaxPostSize <= $iMaxUploadSize)
  189. {
  190. $aResult[] = new CheckResult(CheckResult::WARNING, "post_max_size (".ini_get('post_max_size').") must be bigger than upload_max_filesize (".ini_get('upload_max_filesize')."). You may want to check the PHP configuration file(s): '$sPhpIniFile'. Be aware that this setting can also be overridden in the apache configuration.");
  191. }
  192. $oP->log("Info - upload_max_filesize: ".ini_get('upload_max_filesize'));
  193. $oP->log("Info - post_max_size: ".ini_get('post_max_size'));
  194. $oP->log("Info - max_file_uploads: ".ini_get('max_file_uploads'));
  195. // Check some more ini settings here, needed for file upload
  196. if (function_exists('get_magic_quotes_gpc'))
  197. {
  198. if (@get_magic_quotes_gpc())
  199. {
  200. $aResult[] = new CheckResult(CheckResult::ERROR, "'magic_quotes_gpc' is set to On. Please turn it Off before continuing. You may want to check the PHP configuration file(s): '$sPhpIniFile'. Be aware that this setting can also be overridden in the apache configuration.");
  201. }
  202. }
  203. if (function_exists('magic_quotes_runtime'))
  204. {
  205. if (@magic_quotes_runtime())
  206. {
  207. $aResult[] = new CheckResult(CheckResult::ERROR, "'magic_quotes_runtime' is set to On. Please turn it Off before continuing. You may want to check the PHP configuration file(s): '$sPhpIniFile'. Be aware that this setting can also be overridden in the apache configuration.");
  208. }
  209. }
  210. $sMemoryLimit = trim(ini_get('memory_limit'));
  211. if (empty($sMemoryLimit))
  212. {
  213. // On some PHP installations, memory_limit does not exist as a PHP setting!
  214. // (encountered on a 5.2.0 under Windows)
  215. // In that case, ini_set will not work, let's keep track of this and proceed anyway
  216. $aResult[] = new CheckResult(CheckResult::WARNING, "No memory limit has been defined in this instance of PHP");
  217. }
  218. else
  219. {
  220. // Check that the limit will allow us to load the data
  221. //
  222. $iMemoryLimit = utils::ConvertToBytes($sMemoryLimit);
  223. if ($iMemoryLimit < self::MIN_MEMORY_LIMIT)
  224. {
  225. $aResult[] = new CheckResult(CheckResult::ERROR, "memory_limit ($iMemoryLimit) is too small, the minimum value to run the application is ".self::MIN_MEMORY_LIMIT.".");
  226. }
  227. else
  228. {
  229. $oP->log_info("memory_limit is $iMemoryLimit, ok.");
  230. }
  231. }
  232. // Special case for APC
  233. if (extension_loaded('apc'))
  234. {
  235. $sAPCVersion = phpversion('apc');
  236. $aResult[] = new CheckResult(CheckResult::INFO, "APC detected (version $sAPCVersion). The APC cache will be used to speed-up the application.");
  237. }
  238. // Special case Suhosin extension
  239. if (extension_loaded('suhosin'))
  240. {
  241. $sSuhosinVersion = phpversion('suhosin');
  242. $aOk[] = "Suhosin extension detected (version $sSuhosinVersion).";
  243. $iGetMaxValueLength = ini_get('suhosin.get.max_value_length');
  244. if ($iGetMaxValueLength < self::SUHOSIN_GET_MAX_VALUE_LENGTH)
  245. {
  246. $aResult[] = new CheckResult(CheckResult::INFO, "suhosin.get.max_value_length ($iGetMaxValueLength) is too small, the minimum value to run the application is ".self::SUHOSIN_GET_MAX_VALUE_LENGTH.". This value is set by the PHP configuration file(s): '$sPhpIniFile'. Be aware that this setting can also be overridden in the apache configuration.");
  247. }
  248. else
  249. {
  250. $oP->log_info("suhosin.get.max_value_length = $iGetMaxValueLength, ok.");
  251. }
  252. }
  253. return $aResult;
  254. }
  255. /**
  256. * Helper function to retrieve the system's temporary directory
  257. * Emulates sys_get_temp_dir if neeed (PHP < 5.2.1)
  258. * @return string Path to the system's temp directory
  259. */
  260. static function GetTmpDir()
  261. {
  262. // try to figure out what is the temporary directory
  263. // prior to PHP 5.2.1 the function sys_get_temp_dir
  264. // did not exist
  265. if ( !function_exists('sys_get_temp_dir'))
  266. {
  267. if( $temp=getenv('TMP') ) return realpath($temp);
  268. if( $temp=getenv('TEMP') ) return realpath($temp);
  269. if( $temp=getenv('TMPDIR') ) return realpath($temp);
  270. $temp=tempnam(__FILE__,'');
  271. if (file_exists($temp))
  272. {
  273. unlink($temp);
  274. return realpath(dirname($temp));
  275. }
  276. return null;
  277. }
  278. else
  279. {
  280. return realpath(sys_get_temp_dir());
  281. }
  282. }
  283. /**
  284. * Helper function to retrieve the directory where files are to be uploaded
  285. * @return string Path to the temp directory used for uploading files
  286. */
  287. static function GetUploadTmpDir()
  288. {
  289. $sPath = ini_get('upload_tmp_dir');
  290. if (empty($sPath))
  291. {
  292. $sPath = self::GetTmpDir();
  293. }
  294. return $sPath;
  295. }
  296. }