setuputils.class.inc.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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()
  60. {
  61. $aResult = array();
  62. $aErrors = array();
  63. $aWarnings = array();
  64. $aOk = array();
  65. SetupPage::log('Info - CheckPHPVersion');
  66. if (version_compare(phpversion(), self::PHP_MIN_VERSION, '>='))
  67. {
  68. $aResult[] = new CheckResult(CheckResult::INFO, "The current PHP Version (".phpversion().") is greater than the minimum required version (".self::PHP_MIN_VERSION.")");
  69. }
  70. else
  71. {
  72. $aResult[] = new CheckResult(CheckResult::ERROR, "Error: The current PHP Version (".phpversion().") is lower than the minimum required version (".self::PHP_MIN_VERSION.")");
  73. }
  74. $aMandatoryExtensions = array('mysqli', 'iconv', 'simplexml', 'soap', 'hash', 'json', 'session', 'pcre', 'dom');
  75. $aOptionalExtensions = array('mcrypt' => 'Strong encryption will not be used.',
  76. 'ldap' => 'LDAP authentication will be disabled.');
  77. asort($aMandatoryExtensions); // Sort the list to look clean !
  78. ksort($aOptionalExtensions); // Sort the list to look clean !
  79. $aExtensionsOk = array();
  80. $aMissingExtensions = array();
  81. $aMissingExtensionsLinks = array();
  82. // First check the mandatory extensions
  83. foreach($aMandatoryExtensions as $sExtension)
  84. {
  85. if (extension_loaded($sExtension))
  86. {
  87. $aExtensionsOk[] = $sExtension;
  88. }
  89. else
  90. {
  91. $aMissingExtensions[] = $sExtension;
  92. $aMissingExtensionsLinks[] = "<a href=\"http://www.php.net/manual/en/book.$sExtension.php\" target=\"_blank\">$sExtension</a>";
  93. }
  94. }
  95. if (count($aExtensionsOk) > 0)
  96. {
  97. $aResult[] = new CheckResult(CheckResult::INFO, "Required PHP extension(s): ".implode(', ', $aExtensionsOk).".");
  98. }
  99. if (count($aMissingExtensions) > 0)
  100. {
  101. $aResult[] = new CheckResult(CheckResult::ERROR, "Missing PHP extension(s): ".implode(', ', $aMissingExtensionsLinks).".");
  102. }
  103. // Next check the optional extensions
  104. $aExtensionsOk = array();
  105. $aMissingExtensions = array();
  106. foreach($aOptionalExtensions as $sExtension => $sMessage)
  107. {
  108. if (extension_loaded($sExtension))
  109. {
  110. $aExtensionsOk[] = $sExtension;
  111. }
  112. else
  113. {
  114. $aMissingExtensions[$sExtension] = $sMessage;
  115. }
  116. }
  117. if (count($aExtensionsOk) > 0)
  118. {
  119. $aResult[] = new CheckResult(CheckResult::INFO, "Optional PHP extension(s): ".implode(', ', $aExtensionsOk).".");
  120. }
  121. if (count($aMissingExtensions) > 0)
  122. {
  123. foreach($aMissingExtensions as $sExtension => $sMessage)
  124. {
  125. $aResult[] = new CheckResult(CheckResult::WARNING, "Missing optional PHP extension: $sExtension. ".$sMessage);
  126. }
  127. }
  128. // Check some ini settings here
  129. if (function_exists('php_ini_loaded_file')) // PHP >= 5.2.4
  130. {
  131. $sPhpIniFile = php_ini_loaded_file();
  132. // Other included/scanned files
  133. if ($sFileList = php_ini_scanned_files())
  134. {
  135. if (strlen($sFileList) > 0)
  136. {
  137. $aFiles = explode(',', $sFileList);
  138. foreach ($aFiles as $sFile)
  139. {
  140. $sPhpIniFile .= ', '.trim($sFile);
  141. }
  142. }
  143. }
  144. SetupPage::log("Info - php.ini file(s): '$sPhpIniFile'");
  145. }
  146. else
  147. {
  148. $sPhpIniFile = 'php.ini';
  149. }
  150. if (!ini_get('file_uploads'))
  151. {
  152. $aResult[] = new CheckResult(CheckResult::ERROR, "Files upload is not allowed on this server (file_uploads = ".ini_get('file_uploads').").");
  153. }
  154. $sUploadTmpDir = self::GetUploadTmpDir();
  155. if (empty($sUploadTmpDir))
  156. {
  157. $sUploadTmpDir = '/tmp';
  158. $aResult[] = new CheckResult(CheckResult::WARNING, "Temporary directory for files upload is not defined (upload_tmp_dir), assuming that $sUploadTmpDir is used.");
  159. }
  160. // check that the upload directory is indeed writable from PHP
  161. if (!empty($sUploadTmpDir))
  162. {
  163. if (!file_exists($sUploadTmpDir))
  164. {
  165. $aResult[] = new CheckResult(CheckResult::ERROR, "Temporary directory for files upload ($sUploadTmpDir) does not exist or cannot be read by PHP.");
  166. }
  167. else if (!is_writable($sUploadTmpDir))
  168. {
  169. $aResult[] = new CheckResult(CheckResult::ERROR, "Temporary directory for files upload ($sUploadTmpDir) is not writable.");
  170. }
  171. else
  172. {
  173. SetupPage::log("Info - Temporary directory for files upload ($sUploadTmpDir) is writable.");
  174. }
  175. }
  176. if (!ini_get('upload_max_filesize'))
  177. {
  178. $aResult[] = new CheckResult(CheckResult::ERROR, "File upload is not allowed on this server (upload_max_filesize = ".ini_get('upload_max_filesize').").");
  179. }
  180. $iMaxFileUploads = ini_get('max_file_uploads');
  181. if (!empty($iMaxFileUploads) && ($iMaxFileUploads < 1))
  182. {
  183. $aResult[] = new CheckResult(CheckResult::ERROR, "File upload is not allowed on this server (max_file_uploads = ".ini_get('max_file_uploads').").");
  184. }
  185. $iMaxUploadSize = utils::ConvertToBytes(ini_get('upload_max_filesize'));
  186. $iMaxPostSize = utils::ConvertToBytes(ini_get('post_max_size'));
  187. if ($iMaxPostSize <= $iMaxUploadSize)
  188. {
  189. $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.");
  190. }
  191. SetupPage::log("Info - upload_max_filesize: ".ini_get('upload_max_filesize'));
  192. SetupPage::log("Info - post_max_size: ".ini_get('post_max_size'));
  193. SetupPage::log("Info - max_file_uploads: ".ini_get('max_file_uploads'));
  194. // Check some more ini settings here, needed for file upload
  195. if (function_exists('get_magic_quotes_gpc'))
  196. {
  197. if (@get_magic_quotes_gpc())
  198. {
  199. $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.");
  200. }
  201. }
  202. if (function_exists('magic_quotes_runtime'))
  203. {
  204. if (@magic_quotes_runtime())
  205. {
  206. $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.");
  207. }
  208. }
  209. $sMemoryLimit = trim(ini_get('memory_limit'));
  210. if (empty($sMemoryLimit))
  211. {
  212. // On some PHP installations, memory_limit does not exist as a PHP setting!
  213. // (encountered on a 5.2.0 under Windows)
  214. // In that case, ini_set will not work, let's keep track of this and proceed anyway
  215. $aResult[] = new CheckResult(CheckResult::WARNING, "No memory limit has been defined in this instance of PHP");
  216. }
  217. else
  218. {
  219. // Check that the limit will allow us to load the data
  220. //
  221. $iMemoryLimit = utils::ConvertToBytes($sMemoryLimit);
  222. if ($iMemoryLimit < self::MIN_MEMORY_LIMIT)
  223. {
  224. $aResult[] = new CheckResult(CheckResult::ERROR, "memory_limit ($iMemoryLimit) is too small, the minimum value to run the application is ".self::MIN_MEMORY_LIMIT.".");
  225. }
  226. else
  227. {
  228. SetupPage::log("Info - memory_limit is $iMemoryLimit, ok.");
  229. }
  230. }
  231. // Special case for APC
  232. if (extension_loaded('apc'))
  233. {
  234. $sAPCVersion = phpversion('apc');
  235. $aResult[] = new CheckResult(CheckResult::INFO, "APC detected (version $sAPCVersion). The APC cache will be used to speed-up the application.");
  236. }
  237. // Special case Suhosin extension
  238. if (extension_loaded('suhosin'))
  239. {
  240. $sSuhosinVersion = phpversion('suhosin');
  241. $aOk[] = "Suhosin extension detected (version $sSuhosinVersion).";
  242. $iGetMaxValueLength = ini_get('suhosin.get.max_value_length');
  243. if ($iGetMaxValueLength < self::SUHOSIN_GET_MAX_VALUE_LENGTH)
  244. {
  245. $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.");
  246. }
  247. else
  248. {
  249. SetupPage::log("Info - suhosin.get.max_value_length = $iGetMaxValueLength, ok.");
  250. }
  251. }
  252. return $aResult;
  253. }
  254. /**
  255. * Check that the backup could be executed
  256. * @param Page $oP The page used only for its 'log' method
  257. * @return array An array of CheckResults objects
  258. */
  259. static function CheckBackupPrerequisites($sDestDir)
  260. {
  261. $aResult = array();
  262. SetupPage::log('Info - CheckBackupPrerequisites');
  263. // zip extension
  264. //
  265. if (!extension_loaded('zip'))
  266. {
  267. $sMissingExtensionLink = "<a href=\"http://www.php.net/manual/en/book.zip.php\" target=\"_blank\">zip</a>";
  268. $aResult[] = new CheckResult(CheckResult::ERROR, "Missing PHP extension: zip", $sMissingExtensionLink);
  269. }
  270. // availability of exec()
  271. //
  272. $aDisabled = explode(', ', ini_get('disable_functions'));
  273. SetupPage::log('Info - PHP functions disabled: '.implode(', ', $aDisabled));
  274. if (in_array('exec', $aDisabled))
  275. {
  276. $aResult[] = new CheckResult(CheckResult::ERROR, "The PHP exec() function has been disabled on this server");
  277. }
  278. // availability of mysqldump
  279. $sMySQLBinDir = utils::ReadParam('mysql_bindir', '', true);
  280. if (empty($sMySQLBinDir))
  281. {
  282. $sMySQLDump = 'mysqldump';
  283. }
  284. else
  285. {
  286. SetupPage::log('Info - Found mysql_bindir: '.$sMySQLBinDir);
  287. $sMySQLDump = '"'.$sMySQLBinDir.'/mysqldump"';
  288. }
  289. $sCommand = "$sMySQLDump -V 2>&1";
  290. $aOutput = array();
  291. $iRetCode = 0;
  292. exec($sCommand, $aOutput, $iRetCode);
  293. if ($iRetCode == 0)
  294. {
  295. $aResult[] = new CheckResult(CheckResult::INFO, "mysqldump is present: ".$aOutput[0]);
  296. }
  297. elseif ($iRetCode == 1)
  298. {
  299. $aResult[] = new CheckResult(CheckResult::ERROR, "mysqldump could not be found: ".implode(' ', $aOutput)." - Please make sure it is installed and in the path.");
  300. }
  301. else
  302. {
  303. $aResult[] = new CheckResult(CheckResult::ERROR, "mysqldump could not be executed (retcode=$iRetCode): Please make sure it is installed and in the path");
  304. }
  305. foreach($aOutput as $sLine)
  306. {
  307. SetupPage::log('Info - mysqldump -V said: '.$sLine);
  308. }
  309. // check disk space
  310. // to do... evaluate how we can correlate the DB size with the size of the dump (and the zip!)
  311. // E.g. 2,28 Mb after a full install, giving a zip of 26 Kb (data = 26 Kb)
  312. // Example of query (DB without a suffix)
  313. //$sDBSize = "SELECT SUM(ROUND(DATA_LENGTH/1024/1024, 2)) AS size_mb FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = `$sDBName`";
  314. return $aResult;
  315. }
  316. /**
  317. * Helper function to retrieve the system's temporary directory
  318. * Emulates sys_get_temp_dir if neeed (PHP < 5.2.1)
  319. * @return string Path to the system's temp directory
  320. */
  321. static function GetTmpDir()
  322. {
  323. // try to figure out what is the temporary directory
  324. // prior to PHP 5.2.1 the function sys_get_temp_dir
  325. // did not exist
  326. if ( !function_exists('sys_get_temp_dir'))
  327. {
  328. if( $temp=getenv('TMP') ) return realpath($temp);
  329. if( $temp=getenv('TEMP') ) return realpath($temp);
  330. if( $temp=getenv('TMPDIR') ) return realpath($temp);
  331. $temp=tempnam(__FILE__,'');
  332. if (file_exists($temp))
  333. {
  334. unlink($temp);
  335. return realpath(dirname($temp));
  336. }
  337. return null;
  338. }
  339. else
  340. {
  341. return realpath(sys_get_temp_dir());
  342. }
  343. }
  344. /**
  345. * Helper function to retrieve the directory where files are to be uploaded
  346. * @return string Path to the temp directory used for uploading files
  347. */
  348. static function GetUploadTmpDir()
  349. {
  350. $sPath = ini_get('upload_tmp_dir');
  351. if (empty($sPath))
  352. {
  353. $sPath = self::GetTmpDir();
  354. }
  355. return $sPath;
  356. }
  357. /**
  358. * Helper to recursively remove a directory
  359. */
  360. public static function rrmdir($dir)
  361. {
  362. if ((strlen(trim($dir)) == 0) || ($dir == '/') || ($dir == '\\'))
  363. {
  364. throw new Exception("Attempting to delete directory: '$dir'");
  365. }
  366. self::tidydir($dir);
  367. rmdir($dir);
  368. }
  369. /**
  370. * Helper to recursively cleanup a directory
  371. */
  372. public static function tidydir($dir)
  373. {
  374. if ((strlen(trim($dir)) == 0) || ($dir == '/') || ($dir == '\\'))
  375. {
  376. throw new Exception("Attempting to delete directory: '$dir'");
  377. }
  378. foreach(glob($dir . '/*') as $file)
  379. {
  380. if(is_dir($file))
  381. {
  382. self::tidydir($file);
  383. rmdir($file);
  384. }
  385. else
  386. {
  387. unlink($file);
  388. }
  389. }
  390. }
  391. /**
  392. * Helper to build the full path of a new directory
  393. */
  394. public static function builddir($dir)
  395. {
  396. $parent = dirname($dir);
  397. if(!is_dir($parent))
  398. {
  399. self::builddir($parent);
  400. }
  401. if (!is_dir($dir))
  402. {
  403. mkdir($dir);
  404. }
  405. }
  406. /**
  407. * Helper to copy a directory to a target directory, skipping .SVN files (for developer's comfort!)
  408. * Returns true if successfull
  409. */
  410. public static function copydir($sSource, $sDest)
  411. {
  412. if (is_dir($sSource))
  413. {
  414. if (!is_dir($sDest))
  415. {
  416. mkdir($sDest);
  417. }
  418. $aFiles = scandir($sSource);
  419. if(sizeof($aFiles) > 0 )
  420. {
  421. foreach($aFiles as $sFile)
  422. {
  423. if ($sFile == '.' || $sFile == '..' || $sFile == '.svn')
  424. {
  425. // Skip
  426. continue;
  427. }
  428. if (is_dir($sSource.'/'.$sFile))
  429. {
  430. // Recurse
  431. self::copydir($sSource.'/'.$sFile, $sDest.'/'.$sFile);
  432. }
  433. else
  434. {
  435. copy($sSource.'/'.$sFile, $sDest.'/'.$sFile);
  436. }
  437. }
  438. }
  439. return true;
  440. }
  441. elseif (is_file($sSource))
  442. {
  443. return copy($sSource, $sDest);
  444. }
  445. else
  446. {
  447. return false;
  448. }
  449. }
  450. }