PageRenderTime 41ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/ConfigurationTest.php

https://bitbucket.org/tippycracker/autokraitis
PHP | 431 lines | 324 code | 63 blank | 44 comment | 32 complexity | 9c49ef3bf222ee91880fbfc531d90b22 MD5 | raw file
Possible License(s): BSD-2-Clause, GPL-2.0, GPL-3.0, BSD-3-Clause, Apache-2.0
  1. <?php
  2. /**
  3. * 2007-2017 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. * https://opensource.org/licenses/OSL-3.0
  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-2017 PrestaShop SA
  23. * @license https://opensource.org/licenses/OSL-3.0 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. '/classes/log/index.php',
  30. '/classes/cache/index.php',
  31. '/config/index.php',
  32. '/controllers/admin/AdminLoginController.php',
  33. '/download/index.php',
  34. '/js/tools.js',
  35. '/js/jquery/plugins/fancybox/jquery.fancybox.js',
  36. '/localization/fr.xml',
  37. '/mails/index.php',
  38. '/modules/index.php',
  39. '/override/controllers/front/index.php',
  40. '/pdf/order-return.tpl',
  41. '/translations/export/index.php',
  42. '/webservice/dispatcher.php',
  43. '/index.php',
  44. '/vendor/autoload.php',
  45. );
  46. /**
  47. * getDefaultTests return an array of tests to executes.
  48. * key are method name, value are parameters (false for no parameter)
  49. * all path are _PS_ROOT_DIR_ related.
  50. *
  51. * @return array
  52. */
  53. public static function getDefaultTests()
  54. {
  55. $tests = array(
  56. 'upload' => false,
  57. 'cache_dir' => 'app/cache',
  58. 'log_dir' => 'app/logs',
  59. 'img_dir' => 'img',
  60. 'module_dir' => 'modules',
  61. 'theme_lang_dir' => 'themes/'._THEME_NAME_.'/lang/',
  62. 'theme_pdf_lang_dir' => 'themes/'._THEME_NAME_.'/pdf/lang/',
  63. 'theme_cache_dir' => 'themes/'._THEME_NAME_.'/cache/',
  64. 'translations_dir' => 'translations',
  65. 'customizable_products_dir' => 'upload',
  66. 'virtual_products_dir' => 'download',
  67. 'config_sf2_dir' => 'app/config',
  68. 'translations_sf2' => 'app/Resources/translations',
  69. );
  70. if (!defined('_PS_HOST_MODE_')) {
  71. $tests = array_merge($tests, array(
  72. 'system' => array(
  73. 'fopen', 'fclose', 'fread', 'fwrite',
  74. 'rename', 'file_exists', 'unlink', 'rmdir', 'mkdir',
  75. 'getcwd', 'chdir', 'chmod',
  76. ),
  77. 'phpversion' => false,
  78. 'apache_mod_rewrite' => false,
  79. 'curl' => false,
  80. 'gd' => false,
  81. 'json' => false,
  82. 'pdo_mysql' => false,
  83. 'config_dir' => 'config',
  84. 'files' => false,
  85. 'mails_dir' => 'mails',
  86. 'openssl' => 'false',
  87. 'simplexml' => false,
  88. 'zip' => false,
  89. 'fileinfo' => false,
  90. 'fopen' => false,
  91. ));
  92. }
  93. return $tests;
  94. }
  95. /**
  96. * getDefaultTestsOp return an array of tests to executes.
  97. * key are method name, value are parameters (false for no parameter).
  98. *
  99. * @return array
  100. */
  101. public static function getDefaultTestsOp()
  102. {
  103. return array(
  104. 'new_phpversion' => false,
  105. 'gz' => false,
  106. 'mbstring' => false,
  107. 'dom' => false,
  108. 'pdo_mysql' => false,
  109. );
  110. }
  111. /**
  112. * run all test defined in $tests.
  113. *
  114. * @param array $tests
  115. *
  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 (isset($_SERVER['SERVER_SOFTWARE'])
  140. && strpos(strtolower($_SERVER['SERVER_SOFTWARE']), 'apache') === false || !function_exists('apache_get_modules')) {
  141. return true;
  142. }
  143. return in_array('mod_rewrite', apache_get_modules());
  144. }
  145. public static function test_new_phpversion()
  146. {
  147. return version_compare(substr(phpversion(), 0, 5), '5.4.0', '>=');
  148. }
  149. public static function test_mysql_support()
  150. {
  151. return extension_loaded('mysql') || extension_loaded('mysqli') || extension_loaded('pdo_mysql');
  152. }
  153. public static function test_pdo_mysql()
  154. {
  155. return extension_loaded('pdo_mysql');
  156. }
  157. public static function test_upload()
  158. {
  159. return ini_get('file_uploads');
  160. }
  161. public static function test_fopen()
  162. {
  163. return ini_get('allow_url_fopen');
  164. }
  165. public static function test_system($funcs)
  166. {
  167. foreach ($funcs as $func) {
  168. if (!function_exists($func)) {
  169. return false;
  170. }
  171. }
  172. return true;
  173. }
  174. public static function test_curl()
  175. {
  176. return extension_loaded('curl');
  177. }
  178. public static function test_gd()
  179. {
  180. return function_exists('imagecreatetruecolor');
  181. }
  182. public static function test_json()
  183. {
  184. return extension_loaded('json');
  185. }
  186. public static function test_gz()
  187. {
  188. if (function_exists('gzencode')) {
  189. return @gzencode('dd') !== false;
  190. }
  191. return false;
  192. }
  193. public static function test_simplexml()
  194. {
  195. return extension_loaded('SimpleXML');
  196. }
  197. public static function test_zip()
  198. {
  199. return extension_loaded('zip');
  200. }
  201. public static function test_fileinfo()
  202. {
  203. return extension_loaded('fileinfo');
  204. }
  205. public static function test_dir($relative_dir, $recursive = false, &$full_report = null)
  206. {
  207. $dir = rtrim(_PS_ROOT_DIR_, '\\/').DIRECTORY_SEPARATOR.trim($relative_dir, '\\/');
  208. if (!file_exists($dir) || !$dh = @opendir($dir)) {
  209. $full_report = sprintf('Directory %s does not exist or is not writable', $dir); // sprintf for future translation
  210. return false;
  211. }
  212. $dummy = rtrim($dir, '\\/').DIRECTORY_SEPARATOR.uniqid();
  213. if (@file_put_contents($dummy, 'test')) {
  214. @unlink($dummy);
  215. if (!$recursive) {
  216. closedir($dh);
  217. return true;
  218. }
  219. } elseif (!is_writable($dir)) {
  220. $full_report = sprintf('Directory %s is not writable', $dir); // sprintf for future translation
  221. return false;
  222. }
  223. if ($recursive) {
  224. while (($file = readdir($dh)) !== false) {
  225. if (is_dir($dir.DIRECTORY_SEPARATOR.$file) && $file != '.' && $file != '..' && $file != '.svn') {
  226. if (!ConfigurationTest::test_dir($relative_dir.DIRECTORY_SEPARATOR.$file, $recursive, $full_report)) {
  227. return false;
  228. }
  229. }
  230. }
  231. }
  232. closedir($dh);
  233. return true;
  234. }
  235. public static function test_file($file_relative)
  236. {
  237. $file = _PS_ROOT_DIR_.DIRECTORY_SEPARATOR.$file_relative;
  238. return file_exists($file) && is_writable($file);
  239. }
  240. public static function test_config_dir($dir)
  241. {
  242. return ConfigurationTest::test_dir($dir);
  243. }
  244. public static function test_sitemap($dir)
  245. {
  246. return ConfigurationTest::test_file($dir);
  247. }
  248. public static function test_root_dir($dir)
  249. {
  250. return ConfigurationTest::test_dir($dir);
  251. }
  252. public static function test_log_dir($dir)
  253. {
  254. return ConfigurationTest::test_dir($dir);
  255. }
  256. public static function test_admin_dir($dir)
  257. {
  258. return ConfigurationTest::test_dir($dir);
  259. }
  260. public static function test_img_dir($dir)
  261. {
  262. return ConfigurationTest::test_dir($dir, true);
  263. }
  264. public static function test_module_dir($dir)
  265. {
  266. return ConfigurationTest::test_dir($dir, true);
  267. }
  268. public static function test_cache_dir($dir)
  269. {
  270. return ConfigurationTest::test_dir($dir, true);
  271. }
  272. public static function test_tools_v2_dir($dir)
  273. {
  274. return ConfigurationTest::test_dir($dir);
  275. }
  276. public static function test_cache_v2_dir($dir)
  277. {
  278. return ConfigurationTest::test_dir($dir);
  279. }
  280. public static function test_download_dir($dir)
  281. {
  282. return ConfigurationTest::test_dir($dir);
  283. }
  284. public static function test_mails_dir($dir)
  285. {
  286. return ConfigurationTest::test_dir($dir, true);
  287. }
  288. public static function test_translations_dir($dir)
  289. {
  290. return ConfigurationTest::test_dir($dir, true);
  291. }
  292. public static function test_config_sf2_dir($dir)
  293. {
  294. return ConfigurationTest::test_dir($dir, true);
  295. }
  296. public static function test_theme_lang_dir($dir)
  297. {
  298. $absoluteDir = rtrim(_PS_ROOT_DIR_, '\\/').DIRECTORY_SEPARATOR.trim($dir, '\\/');
  299. if (!file_exists($absoluteDir)) {
  300. return true;
  301. }
  302. return ConfigurationTest::test_dir($dir, true);
  303. }
  304. public static function test_theme_pdf_lang_dir($dir)
  305. {
  306. $absoluteDir = rtrim(_PS_ROOT_DIR_, '\\/').DIRECTORY_SEPARATOR.trim($dir, '\\/');
  307. if (!file_exists($absoluteDir)) {
  308. return true;
  309. }
  310. return ConfigurationTest::test_dir($dir, true);
  311. }
  312. public static function test_theme_cache_dir($dir)
  313. {
  314. $absoluteDir = rtrim(_PS_ROOT_DIR_, '\\/').DIRECTORY_SEPARATOR.trim($dir, '\\/');
  315. if (!file_exists($absoluteDir)) {
  316. return true;
  317. }
  318. return ConfigurationTest::test_dir($dir, true);
  319. }
  320. public static function test_customizable_products_dir($dir)
  321. {
  322. return ConfigurationTest::test_dir($dir);
  323. }
  324. public static function test_virtual_products_dir($dir)
  325. {
  326. return ConfigurationTest::test_dir($dir);
  327. }
  328. public static function test_mbstring()
  329. {
  330. return function_exists('mb_strtolower');
  331. }
  332. public static function test_openssl()
  333. {
  334. return function_exists('openssl_encrypt');
  335. }
  336. public static function test_sessions()
  337. {
  338. if (!$path = @ini_get('session.save_path')) {
  339. return true;
  340. }
  341. return is_writable($path);
  342. }
  343. public static function test_dom()
  344. {
  345. return extension_loaded('Dom');
  346. }
  347. public static function test_files($full = false)
  348. {
  349. $return = array();
  350. foreach (ConfigurationTest::$test_files as $file) {
  351. if (!file_exists(rtrim(_PS_ROOT_DIR_, DIRECTORY_SEPARATOR).str_replace('/', DIRECTORY_SEPARATOR, $file))) {
  352. if ($full) {
  353. array_push($return, $file);
  354. } else {
  355. return false;
  356. }
  357. }
  358. }
  359. if ($full) {
  360. return $return;
  361. }
  362. return true;
  363. }
  364. public static function test_translations_sf2($dir)
  365. {
  366. return ConfigurationTest::test_dir($dir);
  367. }
  368. }