PageRenderTime 22ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/classes/ConfigurationTest.php

https://gitlab.com/goolic/PrestaShop
PHP | 404 lines | 312 code | 49 blank | 43 comment | 31 complexity | 2d22c6ef5588746bacdfb29e064f6f08 MD5 | raw file
  1. <?php
  2. /**
  3. * 2007-2015 PrestaShop
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@prestashop.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
  18. * versions in the future. If you wish to customize PrestaShop for your
  19. * needs please refer to http://www.prestashop.com for more information.
  20. *
  21. * @author PrestaShop SA <contact@prestashop.com>
  22. * @copyright 2007-2015 PrestaShop SA
  23. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  24. * International Registered Trademark & Property of PrestaShop SA
  25. */
  26. class ConfigurationTestCore
  27. {
  28. public static $test_files = array(
  29. '/cache/smarty/compile/index.php',
  30. '/classes/log/index.php',
  31. '/classes/cache/index.php',
  32. '/config/index.php',
  33. '/tools/tar/Archive_Tar.php',
  34. '/tools/pear/PEAR.php',
  35. '/controllers/admin/AdminLoginController.php',
  36. '/css/index.php',
  37. '/download/index.php',
  38. '/js/tools.js',
  39. '/js/jquery/plugins/fancybox/jquery.fancybox.js',
  40. '/localization/fr.xml',
  41. '/mails/index.php',
  42. '/modules/index.php',
  43. '/override/controllers/front/index.php',
  44. '/pdf/order-return.tpl',
  45. '/translations/export/index.php',
  46. '/webservice/dispatcher.php',
  47. '/upload/index.php',
  48. '/index.php'
  49. );
  50. /**
  51. * getDefaultTests return an array of tests to executes.
  52. * key are method name, value are parameters (false for no parameter)
  53. * all path are _PS_ROOT_DIR_ related
  54. *
  55. * @return array
  56. */
  57. public static function getDefaultTests()
  58. {
  59. $tests = array(
  60. 'upload' => false,
  61. 'cache_dir' => 'cache',
  62. 'log_dir' => 'log',
  63. 'img_dir' => 'img',
  64. 'module_dir' => 'modules',
  65. 'theme_lang_dir' => 'themes/'._THEME_NAME_.'/lang/',
  66. 'theme_pdf_lang_dir' => 'themes/'._THEME_NAME_.'/pdf/lang/',
  67. 'theme_cache_dir' => 'themes/'._THEME_NAME_.'/cache/',
  68. 'translations_dir' => 'translations',
  69. 'customizable_products_dir' => 'upload',
  70. 'virtual_products_dir' => 'download',
  71. 'app_cache_dir' => 'app/cache',
  72. 'app_logs_dir' => 'app/logs',
  73. 'config_sf2_dir' => 'app/config',
  74. );
  75. if (!defined('_PS_HOST_MODE_')) {
  76. $tests = array_merge($tests, array(
  77. 'system' => array(
  78. 'fopen', 'fclose', 'fread', 'fwrite',
  79. 'rename', 'file_exists', 'unlink', 'rmdir', 'mkdir',
  80. 'getcwd', 'chdir', 'chmod'
  81. ),
  82. 'phpversion' => false,
  83. 'apache_mod_rewrite' => false,
  84. 'gd' => false,
  85. 'mysql_support' => false,
  86. 'config_dir' => 'config',
  87. 'files' => false,
  88. 'mails_dir' => 'mails',
  89. 'zip' => false,
  90. ));
  91. }
  92. return $tests;
  93. }
  94. /**
  95. * getDefaultTestsOp return an array of tests to executes.
  96. * key are method name, value are parameters (false for no parameter)
  97. *
  98. * @return array
  99. */
  100. public static function getDefaultTestsOp()
  101. {
  102. return array(
  103. 'new_phpversion' => false,
  104. 'fopen' => false,
  105. 'gz' => false,
  106. 'mcrypt' => false,
  107. 'mbstring' => false,
  108. 'dom' => false,
  109. 'pdo_mysql' => false,
  110. );
  111. }
  112. /**
  113. * run all test defined in $tests
  114. *
  115. * @param array $tests
  116. * @return array results of tests
  117. */
  118. public static function check($tests)
  119. {
  120. $res = array();
  121. foreach ($tests as $key => $test) {
  122. $res[$key] = ConfigurationTest::run($key, $test);
  123. }
  124. return $res;
  125. }
  126. public static function run($ptr, $arg = 0)
  127. {
  128. if (call_user_func(array('ConfigurationTest', 'test_'.$ptr), $arg)) {
  129. return 'ok';
  130. }
  131. return 'fail';
  132. }
  133. public static function test_phpversion()
  134. {
  135. return version_compare(substr(phpversion(), 0, 5), '5.4.0', '>=');
  136. }
  137. public static function test_apache_mod_rewrite()
  138. {
  139. if (strpos(strtolower($_SERVER["SERVER_SOFTWARE"]), 'apache') === false || !function_exists('apache_get_modules')) {
  140. return true;
  141. }
  142. return in_array('mod_rewrite', apache_get_modules());
  143. }
  144. public static function test_new_phpversion()
  145. {
  146. return version_compare(substr(phpversion(), 0, 5), '5.4.0', '>=');
  147. }
  148. public static function test_mysql_support()
  149. {
  150. return extension_loaded('mysql') || extension_loaded('mysqli') || extension_loaded('pdo_mysql');
  151. }
  152. public static function test_pdo_mysql()
  153. {
  154. return extension_loaded('pdo_mysql');
  155. }
  156. public static function test_upload()
  157. {
  158. return ini_get('file_uploads');
  159. }
  160. public static function test_fopen()
  161. {
  162. return ini_get('allow_url_fopen');
  163. }
  164. public static function test_system($funcs)
  165. {
  166. foreach ($funcs as $func) {
  167. if (!function_exists($func)) {
  168. return false;
  169. }
  170. }
  171. return true;
  172. }
  173. public static function test_gd()
  174. {
  175. return function_exists('imagecreatetruecolor');
  176. }
  177. public static function test_gz()
  178. {
  179. if (function_exists('gzencode')) {
  180. return @gzencode('dd') !== false;
  181. }
  182. return false;
  183. }
  184. public static function test_zip()
  185. {
  186. return extension_loaded('zip');
  187. }
  188. public static function test_dir($relative_dir, $recursive = false, &$full_report = null)
  189. {
  190. $dir = rtrim(_PS_ROOT_DIR_, '\\/').DIRECTORY_SEPARATOR.trim($relative_dir, '\\/');
  191. if (!file_exists($dir) || !$dh = @opendir($dir)) {
  192. $full_report = sprintf('Directory %s does not exist or is not writable', $dir); // sprintf for future translation
  193. return false;
  194. }
  195. $dummy = rtrim($dir, '\\/').DIRECTORY_SEPARATOR.uniqid();
  196. if (@file_put_contents($dummy, 'test')) {
  197. @unlink($dummy);
  198. if (!$recursive) {
  199. closedir($dh);
  200. return true;
  201. }
  202. } elseif (!is_writable($dir)) {
  203. $full_report = sprintf('Directory %s is not writable', $dir); // sprintf for future translation
  204. return false;
  205. }
  206. if ($recursive) {
  207. while (($file = readdir($dh)) !== false) {
  208. if (is_dir($dir.DIRECTORY_SEPARATOR.$file) && $file != '.' && $file != '..' && $file != '.svn') {
  209. if (!ConfigurationTest::test_dir($relative_dir.DIRECTORY_SEPARATOR.$file, $recursive, $full_report)) {
  210. return false;
  211. }
  212. }
  213. }
  214. }
  215. closedir($dh);
  216. return true;
  217. }
  218. public static function test_file($file_relative)
  219. {
  220. $file = _PS_ROOT_DIR_.DIRECTORY_SEPARATOR.$file_relative;
  221. return (file_exists($file) && is_writable($file));
  222. }
  223. public static function test_config_dir($dir)
  224. {
  225. return ConfigurationTest::test_dir($dir);
  226. }
  227. public static function test_sitemap($dir)
  228. {
  229. return ConfigurationTest::test_file($dir);
  230. }
  231. public static function test_root_dir($dir)
  232. {
  233. return ConfigurationTest::test_dir($dir);
  234. }
  235. public static function test_log_dir($dir)
  236. {
  237. return ConfigurationTest::test_dir($dir);
  238. }
  239. public static function test_admin_dir($dir)
  240. {
  241. return ConfigurationTest::test_dir($dir);
  242. }
  243. public static function test_img_dir($dir)
  244. {
  245. return ConfigurationTest::test_dir($dir, true);
  246. }
  247. public static function test_module_dir($dir)
  248. {
  249. return ConfigurationTest::test_dir($dir, true);
  250. }
  251. public static function test_cache_dir($dir)
  252. {
  253. return ConfigurationTest::test_dir($dir, true);
  254. }
  255. public static function test_tools_v2_dir($dir)
  256. {
  257. return ConfigurationTest::test_dir($dir);
  258. }
  259. public static function test_cache_v2_dir($dir)
  260. {
  261. return ConfigurationTest::test_dir($dir);
  262. }
  263. public static function test_download_dir($dir)
  264. {
  265. return ConfigurationTest::test_dir($dir);
  266. }
  267. public static function test_mails_dir($dir)
  268. {
  269. return ConfigurationTest::test_dir($dir, true);
  270. }
  271. public static function test_translations_dir($dir)
  272. {
  273. return ConfigurationTest::test_dir($dir, true);
  274. }
  275. public static function test_app_cache_dir($dir)
  276. {
  277. return ConfigurationTest::test_dir($dir, true);
  278. }
  279. public static function test_app_logs_dir($dir)
  280. {
  281. return ConfigurationTest::test_dir($dir, true);
  282. }
  283. public static function test_config_sf2_dir($dir)
  284. {
  285. return ConfigurationTest::test_dir($dir, true);
  286. }
  287. public static function test_theme_lang_dir($dir)
  288. {
  289. $absoluteDir = rtrim(_PS_ROOT_DIR_, '\\/').DIRECTORY_SEPARATOR.trim($dir, '\\/');
  290. if (!file_exists($absoluteDir)) {
  291. return true;
  292. }
  293. return ConfigurationTest::test_dir($dir, true);
  294. }
  295. public static function test_theme_pdf_lang_dir($dir)
  296. {
  297. $absoluteDir = rtrim(_PS_ROOT_DIR_, '\\/').DIRECTORY_SEPARATOR.trim($dir, '\\/');
  298. if (!file_exists($absoluteDir)) {
  299. return true;
  300. }
  301. return ConfigurationTest::test_dir($dir, true);
  302. }
  303. public static function test_theme_cache_dir($dir)
  304. {
  305. $absoluteDir = rtrim(_PS_ROOT_DIR_, '\\/').DIRECTORY_SEPARATOR.trim($dir, '\\/');
  306. if (!file_exists($absoluteDir)) {
  307. return true;
  308. }
  309. return ConfigurationTest::test_dir($dir, true);
  310. }
  311. public static function test_customizable_products_dir($dir)
  312. {
  313. return ConfigurationTest::test_dir($dir);
  314. }
  315. public static function test_virtual_products_dir($dir)
  316. {
  317. return ConfigurationTest::test_dir($dir);
  318. }
  319. public static function test_mbstring()
  320. {
  321. return function_exists('mb_strtolower');
  322. }
  323. public static function test_mcrypt()
  324. {
  325. return function_exists('mcrypt_encrypt');
  326. }
  327. public static function test_sessions()
  328. {
  329. if (!$path = @ini_get('session.save_path')) {
  330. return true;
  331. }
  332. return is_writable($path);
  333. }
  334. public static function test_dom()
  335. {
  336. return extension_loaded('Dom');
  337. }
  338. public static function test_files($full = false)
  339. {
  340. $return = array();
  341. foreach (ConfigurationTest::$test_files as $file) {
  342. if (!file_exists(rtrim(_PS_ROOT_DIR_, DIRECTORY_SEPARATOR).str_replace('/', DIRECTORY_SEPARATOR, $file))) {
  343. if ($full) {
  344. array_push($return, $file);
  345. } else {
  346. return false;
  347. }
  348. }
  349. }
  350. if ($full) {
  351. return $return;
  352. }
  353. return true;
  354. }
  355. }