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

/classes/ConfigurationTest.php

https://gitlab.com/staging06/myproject
PHP | 385 lines | 296 code | 46 blank | 43 comment | 29 complexity | 3d3fc9a09bbd722129e2825f9cf96646 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. '/img/404.gif',
  39. '/js/tools.js',
  40. '/js/jquery/plugins/fancybox/jquery.fancybox.js',
  41. '/localization/fr.xml',
  42. '/mails/index.php',
  43. '/modules/index.php',
  44. '/override/controllers/front/index.php',
  45. '/pdf/order-return.tpl',
  46. '/themes/default-bootstrap/css/global.css',
  47. '/translations/export/index.php',
  48. '/webservice/dispatcher.php',
  49. '/upload/index.php',
  50. '/index.php'
  51. );
  52. /**
  53. * getDefaultTests return an array of tests to executes.
  54. * key are method name, value are parameters (false for no parameter)
  55. * all path are _PS_ROOT_DIR_ related
  56. *
  57. * @return array
  58. */
  59. public static function getDefaultTests()
  60. {
  61. $tests = array(
  62. 'upload' => false,
  63. 'cache_dir' => 'cache',
  64. 'log_dir' => 'log',
  65. 'img_dir' => 'img',
  66. 'module_dir' => 'modules',
  67. 'theme_lang_dir' => 'themes/'._THEME_NAME_.'/lang/',
  68. 'theme_pdf_lang_dir' => 'themes/'._THEME_NAME_.'/pdf/lang/',
  69. 'theme_cache_dir' => 'themes/'._THEME_NAME_.'/cache/',
  70. 'translations_dir' => 'translations',
  71. 'customizable_products_dir' => 'upload',
  72. 'virtual_products_dir' => 'download'
  73. );
  74. if (!defined('_PS_HOST_MODE_')) {
  75. $tests = array_merge($tests, array(
  76. 'system' => array(
  77. 'fopen', 'fclose', 'fread', 'fwrite',
  78. 'rename', 'file_exists', 'unlink', 'rmdir', 'mkdir',
  79. 'getcwd', 'chdir', 'chmod'
  80. ),
  81. 'phpversion' => false,
  82. 'gd' => false,
  83. 'mysql_support' => false,
  84. 'config_dir' => 'config',
  85. 'files' => false,
  86. 'mails_dir' => 'mails',
  87. ));
  88. }
  89. return $tests;
  90. }
  91. /**
  92. * getDefaultTestsOp return an array of tests to executes.
  93. * key are method name, value are parameters (false for no parameter)
  94. *
  95. * @return array
  96. */
  97. public static function getDefaultTestsOp()
  98. {
  99. return array(
  100. 'new_phpversion' => false,
  101. 'fopen' => false,
  102. 'register_globals' => false,
  103. 'gz' => false,
  104. 'mcrypt' => false,
  105. 'mbstring' => false,
  106. 'magicquotes' => false,
  107. 'dom' => false,
  108. 'pdo_mysql' => false,
  109. );
  110. }
  111. /**
  112. * run all test defined in $tests
  113. *
  114. * @param array $tests
  115. * @return array results of tests
  116. */
  117. public static function check($tests)
  118. {
  119. $res = array();
  120. foreach ($tests as $key => $test) {
  121. $res[$key] = ConfigurationTest::run($key, $test);
  122. }
  123. return $res;
  124. }
  125. public static function run($ptr, $arg = 0)
  126. {
  127. if (call_user_func(array('ConfigurationTest', 'test_'.$ptr), $arg)) {
  128. return 'ok';
  129. }
  130. return 'fail';
  131. }
  132. public static function test_phpversion()
  133. {
  134. return version_compare(substr(phpversion(), 0, 5), '5.2.0', '>=');
  135. }
  136. public static function test_new_phpversion()
  137. {
  138. return version_compare(substr(phpversion(), 0, 5), '5.4.0', '>=');
  139. }
  140. public static function test_mysql_support()
  141. {
  142. return extension_loaded('mysql') || extension_loaded('mysqli') || extension_loaded('pdo_mysql');
  143. }
  144. public static function test_pdo_mysql()
  145. {
  146. return extension_loaded('pdo_mysql');
  147. }
  148. public static function test_magicquotes()
  149. {
  150. return !get_magic_quotes_gpc();
  151. }
  152. public static function test_upload()
  153. {
  154. return ini_get('file_uploads');
  155. }
  156. public static function test_fopen()
  157. {
  158. return ini_get('allow_url_fopen');
  159. }
  160. public static function test_system($funcs)
  161. {
  162. foreach ($funcs as $func) {
  163. if (!function_exists($func)) {
  164. return false;
  165. }
  166. }
  167. return true;
  168. }
  169. public static function test_gd()
  170. {
  171. return function_exists('imagecreatetruecolor');
  172. }
  173. public static function test_register_globals()
  174. {
  175. return !ini_get('register_globals');
  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_dir($relative_dir, $recursive = false, &$full_report = null)
  185. {
  186. $dir = rtrim(_PS_ROOT_DIR_, '\\/').DIRECTORY_SEPARATOR.trim($relative_dir, '\\/');
  187. if (!file_exists($dir) || !$dh = @opendir($dir)) {
  188. $full_report = sprintf('Directory %s does not exist or is not writable', $dir); // sprintf for future translation
  189. return false;
  190. }
  191. $dummy = rtrim($dir, '\\/').DIRECTORY_SEPARATOR.uniqid();
  192. if (@file_put_contents($dummy, 'test')) {
  193. @unlink($dummy);
  194. if (!$recursive) {
  195. closedir($dh);
  196. return true;
  197. }
  198. } elseif (!is_writable($dir)) {
  199. $full_report = sprintf('Directory %s is not writable', $dir); // sprintf for future translation
  200. return false;
  201. }
  202. if ($recursive) {
  203. while (($file = readdir($dh)) !== false) {
  204. if (is_dir($dir.DIRECTORY_SEPARATOR.$file) && $file != '.' && $file != '..' && $file != '.svn') {
  205. if (!ConfigurationTest::test_dir($relative_dir.DIRECTORY_SEPARATOR.$file, $recursive, $full_report)) {
  206. return false;
  207. }
  208. }
  209. }
  210. }
  211. closedir($dh);
  212. return true;
  213. }
  214. public static function test_file($file_relative)
  215. {
  216. $file = _PS_ROOT_DIR_.DIRECTORY_SEPARATOR.$file_relative;
  217. return (file_exists($file) && is_writable($file));
  218. }
  219. public static function test_config_dir($dir)
  220. {
  221. return ConfigurationTest::test_dir($dir);
  222. }
  223. public static function test_sitemap($dir)
  224. {
  225. return ConfigurationTest::test_file($dir);
  226. }
  227. public static function test_root_dir($dir)
  228. {
  229. return ConfigurationTest::test_dir($dir);
  230. }
  231. public static function test_log_dir($dir)
  232. {
  233. return ConfigurationTest::test_dir($dir);
  234. }
  235. public static function test_admin_dir($dir)
  236. {
  237. return ConfigurationTest::test_dir($dir);
  238. }
  239. public static function test_img_dir($dir)
  240. {
  241. return ConfigurationTest::test_dir($dir, true);
  242. }
  243. public static function test_module_dir($dir)
  244. {
  245. return ConfigurationTest::test_dir($dir, true);
  246. }
  247. public static function test_cache_dir($dir)
  248. {
  249. return ConfigurationTest::test_dir($dir, true);
  250. }
  251. public static function test_tools_v2_dir($dir)
  252. {
  253. return ConfigurationTest::test_dir($dir);
  254. }
  255. public static function test_cache_v2_dir($dir)
  256. {
  257. return ConfigurationTest::test_dir($dir);
  258. }
  259. public static function test_download_dir($dir)
  260. {
  261. return ConfigurationTest::test_dir($dir);
  262. }
  263. public static function test_mails_dir($dir)
  264. {
  265. return ConfigurationTest::test_dir($dir, true);
  266. }
  267. public static function test_translations_dir($dir)
  268. {
  269. return ConfigurationTest::test_dir($dir, true);
  270. }
  271. public static function test_theme_lang_dir($dir)
  272. {
  273. $absoluteDir = rtrim(_PS_ROOT_DIR_, '\\/').DIRECTORY_SEPARATOR.trim($dir, '\\/');
  274. if (!file_exists($absoluteDir)) {
  275. return true;
  276. }
  277. return ConfigurationTest::test_dir($dir, true);
  278. }
  279. public static function test_theme_pdf_lang_dir($dir)
  280. {
  281. $absoluteDir = rtrim(_PS_ROOT_DIR_, '\\/').DIRECTORY_SEPARATOR.trim($dir, '\\/');
  282. if (!file_exists($absoluteDir)) {
  283. return true;
  284. }
  285. return ConfigurationTest::test_dir($dir, true);
  286. }
  287. public static function test_theme_cache_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_customizable_products_dir($dir)
  296. {
  297. return ConfigurationTest::test_dir($dir);
  298. }
  299. public static function test_virtual_products_dir($dir)
  300. {
  301. return ConfigurationTest::test_dir($dir);
  302. }
  303. public static function test_mbstring()
  304. {
  305. return function_exists('mb_strtolower');
  306. }
  307. public static function test_mcrypt()
  308. {
  309. return function_exists('mcrypt_encrypt');
  310. }
  311. public static function test_sessions()
  312. {
  313. if (!$path = @ini_get('session.save_path')) {
  314. return true;
  315. }
  316. return is_writable($path);
  317. }
  318. public static function test_dom()
  319. {
  320. return extension_loaded('Dom');
  321. }
  322. public static function test_files($full = false)
  323. {
  324. $return = array();
  325. foreach (ConfigurationTest::$test_files as $file) {
  326. if (!file_exists(rtrim(_PS_ROOT_DIR_, DIRECTORY_SEPARATOR).str_replace('/', DIRECTORY_SEPARATOR, $file))) {
  327. if ($full) {
  328. array_push($return, $file);
  329. } else {
  330. return false;
  331. }
  332. }
  333. }
  334. if ($full) {
  335. return $return;
  336. }
  337. return true;
  338. }
  339. }