/application/library/Thirdpart/Smarty/libs/sysplugins/smarty_internal_testinstall.php

https://gitlab.com/flyhope/Hiblog · PHP · 571 lines · 528 code · 15 blank · 28 comment · 58 complexity · 6c9ad063d486f083a4362a2a7e7a024d MD5 · raw file

  1. <?php
  2. /**
  3. * Smarty Internal TestInstall
  4. * Test Smarty installation
  5. *
  6. * @package Smarty
  7. * @subpackage Utilities
  8. * @author Uwe Tews
  9. */
  10. /**
  11. * TestInstall class
  12. *
  13. * @package Smarty
  14. * @subpackage Utilities
  15. */
  16. class Smarty_Internal_TestInstall
  17. {
  18. /**
  19. * diagnose Smarty setup
  20. * If $errors is secified, the diagnostic report will be appended to the array, rather than being output.
  21. *
  22. * @param Smarty $smarty Smarty instance to test
  23. * @param array $errors array to push results into rather than outputting them
  24. *
  25. * @return bool status, true if everything is fine, false else
  26. */
  27. public static function testInstall(Smarty $smarty, &$errors = null)
  28. {
  29. $status = true;
  30. if ($errors === null) {
  31. echo "<PRE>\n";
  32. echo "Smarty Installation test...\n";
  33. echo "Testing template directory...\n";
  34. }
  35. $_stream_resolve_include_path = function_exists('stream_resolve_include_path');
  36. // test if all registered template_dir are accessible
  37. foreach ($smarty->getTemplateDir() as $template_dir) {
  38. $_template_dir = $template_dir;
  39. $template_dir = realpath($template_dir);
  40. // resolve include_path or fail existence
  41. if (!$template_dir) {
  42. if ($smarty->use_include_path && !preg_match('/^([\/\\\\]|[a-zA-Z]:[\/\\\\])/', $_template_dir)) {
  43. // try PHP include_path
  44. if ($_stream_resolve_include_path) {
  45. $template_dir = stream_resolve_include_path($_template_dir);
  46. } else {
  47. $template_dir = Smarty_Internal_Get_Include_Path::getIncludePath($_template_dir);
  48. }
  49. if ($template_dir !== false) {
  50. if ($errors === null) {
  51. echo "$template_dir is OK.\n";
  52. }
  53. continue;
  54. } else {
  55. $status = false;
  56. $message = "FAILED: $_template_dir does not exist (and couldn't be found in include_path either)";
  57. if ($errors === null) {
  58. echo $message . ".\n";
  59. } else {
  60. $errors['template_dir'] = $message;
  61. }
  62. continue;
  63. }
  64. } else {
  65. $status = false;
  66. $message = "FAILED: $_template_dir does not exist";
  67. if ($errors === null) {
  68. echo $message . ".\n";
  69. } else {
  70. $errors['template_dir'] = $message;
  71. }
  72. continue;
  73. }
  74. }
  75. if (!is_dir($template_dir)) {
  76. $status = false;
  77. $message = "FAILED: $template_dir is not a directory";
  78. if ($errors === null) {
  79. echo $message . ".\n";
  80. } else {
  81. $errors['template_dir'] = $message;
  82. }
  83. } elseif (!is_readable($template_dir)) {
  84. $status = false;
  85. $message = "FAILED: $template_dir is not readable";
  86. if ($errors === null) {
  87. echo $message . ".\n";
  88. } else {
  89. $errors['template_dir'] = $message;
  90. }
  91. } else {
  92. if ($errors === null) {
  93. echo "$template_dir is OK.\n";
  94. }
  95. }
  96. }
  97. if ($errors === null) {
  98. echo "Testing compile directory...\n";
  99. }
  100. // test if registered compile_dir is accessible
  101. $__compile_dir = $smarty->getCompileDir();
  102. $_compile_dir = realpath($__compile_dir);
  103. if (!$_compile_dir) {
  104. $status = false;
  105. $message = "FAILED: {$__compile_dir} does not exist";
  106. if ($errors === null) {
  107. echo $message . ".\n";
  108. } else {
  109. $errors['compile_dir'] = $message;
  110. }
  111. } elseif (!is_dir($_compile_dir)) {
  112. $status = false;
  113. $message = "FAILED: {$_compile_dir} is not a directory";
  114. if ($errors === null) {
  115. echo $message . ".\n";
  116. } else {
  117. $errors['compile_dir'] = $message;
  118. }
  119. } elseif (!is_readable($_compile_dir)) {
  120. $status = false;
  121. $message = "FAILED: {$_compile_dir} is not readable";
  122. if ($errors === null) {
  123. echo $message . ".\n";
  124. } else {
  125. $errors['compile_dir'] = $message;
  126. }
  127. } elseif (!is_writable($_compile_dir)) {
  128. $status = false;
  129. $message = "FAILED: {$_compile_dir} is not writable";
  130. if ($errors === null) {
  131. echo $message . ".\n";
  132. } else {
  133. $errors['compile_dir'] = $message;
  134. }
  135. } else {
  136. if ($errors === null) {
  137. echo "{$_compile_dir} is OK.\n";
  138. }
  139. }
  140. if ($errors === null) {
  141. echo "Testing plugins directory...\n";
  142. }
  143. // test if all registered plugins_dir are accessible
  144. // and if core plugins directory is still registered
  145. $_core_plugins_dir = realpath(dirname(__FILE__) . '/../plugins');
  146. $_core_plugins_available = false;
  147. foreach ($smarty->getPluginsDir() as $plugin_dir) {
  148. $_plugin_dir = $plugin_dir;
  149. $plugin_dir = realpath($plugin_dir);
  150. // resolve include_path or fail existence
  151. if (!$plugin_dir) {
  152. if ($smarty->use_include_path && !preg_match('/^([\/\\\\]|[a-zA-Z]:[\/\\\\])/', $_plugin_dir)) {
  153. // try PHP include_path
  154. if ($_stream_resolve_include_path) {
  155. $plugin_dir = stream_resolve_include_path($_plugin_dir);
  156. } else {
  157. $plugin_dir = Smarty_Internal_Get_Include_Path::getIncludePath($_plugin_dir);
  158. }
  159. if ($plugin_dir !== false) {
  160. if ($errors === null) {
  161. echo "$plugin_dir is OK.\n";
  162. }
  163. continue;
  164. } else {
  165. $status = false;
  166. $message = "FAILED: $_plugin_dir does not exist (and couldn't be found in include_path either)";
  167. if ($errors === null) {
  168. echo $message . ".\n";
  169. } else {
  170. $errors['plugins_dir'] = $message;
  171. }
  172. continue;
  173. }
  174. } else {
  175. $status = false;
  176. $message = "FAILED: $_plugin_dir does not exist";
  177. if ($errors === null) {
  178. echo $message . ".\n";
  179. } else {
  180. $errors['plugins_dir'] = $message;
  181. }
  182. continue;
  183. }
  184. }
  185. if (!is_dir($plugin_dir)) {
  186. $status = false;
  187. $message = "FAILED: $plugin_dir is not a directory";
  188. if ($errors === null) {
  189. echo $message . ".\n";
  190. } else {
  191. $errors['plugins_dir'] = $message;
  192. }
  193. } elseif (!is_readable($plugin_dir)) {
  194. $status = false;
  195. $message = "FAILED: $plugin_dir is not readable";
  196. if ($errors === null) {
  197. echo $message . ".\n";
  198. } else {
  199. $errors['plugins_dir'] = $message;
  200. }
  201. } elseif ($_core_plugins_dir && $_core_plugins_dir == realpath($plugin_dir)) {
  202. $_core_plugins_available = true;
  203. if ($errors === null) {
  204. echo "$plugin_dir is OK.\n";
  205. }
  206. } else {
  207. if ($errors === null) {
  208. echo "$plugin_dir is OK.\n";
  209. }
  210. }
  211. }
  212. if (!$_core_plugins_available) {
  213. $status = false;
  214. $message = "WARNING: Smarty's own libs/plugins is not available";
  215. if ($errors === null) {
  216. echo $message . ".\n";
  217. } elseif (!isset($errors['plugins_dir'])) {
  218. $errors['plugins_dir'] = $message;
  219. }
  220. }
  221. if ($errors === null) {
  222. echo "Testing cache directory...\n";
  223. }
  224. // test if all registered cache_dir is accessible
  225. $__cache_dir = $smarty->getCacheDir();
  226. $_cache_dir = realpath($__cache_dir);
  227. if (!$_cache_dir) {
  228. $status = false;
  229. $message = "FAILED: {$__cache_dir} does not exist";
  230. if ($errors === null) {
  231. echo $message . ".\n";
  232. } else {
  233. $errors['cache_dir'] = $message;
  234. }
  235. } elseif (!is_dir($_cache_dir)) {
  236. $status = false;
  237. $message = "FAILED: {$_cache_dir} is not a directory";
  238. if ($errors === null) {
  239. echo $message . ".\n";
  240. } else {
  241. $errors['cache_dir'] = $message;
  242. }
  243. } elseif (!is_readable($_cache_dir)) {
  244. $status = false;
  245. $message = "FAILED: {$_cache_dir} is not readable";
  246. if ($errors === null) {
  247. echo $message . ".\n";
  248. } else {
  249. $errors['cache_dir'] = $message;
  250. }
  251. } elseif (!is_writable($_cache_dir)) {
  252. $status = false;
  253. $message = "FAILED: {$_cache_dir} is not writable";
  254. if ($errors === null) {
  255. echo $message . ".\n";
  256. } else {
  257. $errors['cache_dir'] = $message;
  258. }
  259. } else {
  260. if ($errors === null) {
  261. echo "{$_cache_dir} is OK.\n";
  262. }
  263. }
  264. if ($errors === null) {
  265. echo "Testing configs directory...\n";
  266. }
  267. // test if all registered config_dir are accessible
  268. foreach ($smarty->getConfigDir() as $config_dir) {
  269. $_config_dir = $config_dir;
  270. $config_dir = realpath($config_dir);
  271. // resolve include_path or fail existence
  272. if (!$config_dir) {
  273. if ($smarty->use_include_path && !preg_match('/^([\/\\\\]|[a-zA-Z]:[\/\\\\])/', $_config_dir)) {
  274. // try PHP include_path
  275. if ($_stream_resolve_include_path) {
  276. $config_dir = stream_resolve_include_path($_config_dir);
  277. } else {
  278. $config_dir = Smarty_Internal_Get_Include_Path::getIncludePath($_config_dir);
  279. }
  280. if ($config_dir !== false) {
  281. if ($errors === null) {
  282. echo "$config_dir is OK.\n";
  283. }
  284. continue;
  285. } else {
  286. $status = false;
  287. $message = "FAILED: $_config_dir does not exist (and couldn't be found in include_path either)";
  288. if ($errors === null) {
  289. echo $message . ".\n";
  290. } else {
  291. $errors['config_dir'] = $message;
  292. }
  293. continue;
  294. }
  295. } else {
  296. $status = false;
  297. $message = "FAILED: $_config_dir does not exist";
  298. if ($errors === null) {
  299. echo $message . ".\n";
  300. } else {
  301. $errors['config_dir'] = $message;
  302. }
  303. continue;
  304. }
  305. }
  306. if (!is_dir($config_dir)) {
  307. $status = false;
  308. $message = "FAILED: $config_dir is not a directory";
  309. if ($errors === null) {
  310. echo $message . ".\n";
  311. } else {
  312. $errors['config_dir'] = $message;
  313. }
  314. } elseif (!is_readable($config_dir)) {
  315. $status = false;
  316. $message = "FAILED: $config_dir is not readable";
  317. if ($errors === null) {
  318. echo $message . ".\n";
  319. } else {
  320. $errors['config_dir'] = $message;
  321. }
  322. } else {
  323. if ($errors === null) {
  324. echo "$config_dir is OK.\n";
  325. }
  326. }
  327. }
  328. if ($errors === null) {
  329. echo "Testing sysplugin files...\n";
  330. }
  331. // test if sysplugins are available
  332. $source = SMARTY_SYSPLUGINS_DIR;
  333. if (is_dir($source)) {
  334. $expected = array(
  335. "smarty_cacheresource.php" => true,
  336. "smarty_cacheresource_custom.php" => true,
  337. "smarty_cacheresource_keyvaluestore.php" => true,
  338. "smarty_data.php" => true,
  339. "smarty_internal_cacheresource_file.php" => true,
  340. "smarty_internal_compile_append.php" => true,
  341. "smarty_internal_compile_assign.php" => true,
  342. "smarty_internal_compile_block.php" => true,
  343. "smarty_internal_compile_break.php" => true,
  344. "smarty_internal_compile_call.php" => true,
  345. "smarty_internal_compile_capture.php" => true,
  346. "smarty_internal_compile_config_load.php" => true,
  347. "smarty_internal_compile_continue.php" => true,
  348. "smarty_internal_compile_debug.php" => true,
  349. "smarty_internal_compile_eval.php" => true,
  350. "smarty_internal_compile_extends.php" => true,
  351. "smarty_internal_compile_for.php" => true,
  352. "smarty_internal_compile_foreach.php" => true,
  353. "smarty_internal_compile_function.php" => true,
  354. "smarty_internal_compile_if.php" => true,
  355. "smarty_internal_compile_include.php" => true,
  356. "smarty_internal_compile_include_php.php" => true,
  357. "smarty_internal_compile_insert.php" => true,
  358. "smarty_internal_compile_ldelim.php" => true,
  359. "smarty_internal_compile_nocache.php" => true,
  360. "smarty_internal_compile_private_block_plugin.php" => true,
  361. "smarty_internal_compile_private_function_plugin.php" => true,
  362. "smarty_internal_compile_private_modifier.php" => true,
  363. "smarty_internal_compile_private_object_block_function.php" => true,
  364. "smarty_internal_compile_private_object_function.php" => true,
  365. "smarty_internal_compile_private_print_expression.php" => true,
  366. "smarty_internal_compile_private_registered_block.php" => true,
  367. "smarty_internal_compile_private_registered_function.php" => true,
  368. "smarty_internal_compile_private_special_variable.php" => true,
  369. "smarty_internal_compile_rdelim.php" => true,
  370. "smarty_internal_compile_section.php" => true,
  371. "smarty_internal_compile_setfilter.php" => true,
  372. "smarty_internal_compile_while.php" => true,
  373. "smarty_internal_compilebase.php" => true,
  374. "smarty_internal_config_file_compiler.php" => true,
  375. "smarty_internal_configfilelexer.php" => true,
  376. "smarty_internal_configfileparser.php" => true,
  377. "smarty_internal_data.php" => true,
  378. "smarty_internal_debug.php" => true,
  379. "smarty_internal_extension_codeframe.php" => true,
  380. "smarty_internal_extension_config.php" => true,
  381. "smarty_internal_extension_defaulttemplatehandler.php" => true,
  382. "smarty_internal_filter_handler.php" => true,
  383. "smarty_internal_function_call_handler.php" => true,
  384. "smarty_internal_get_include_path.php" => true,
  385. "smarty_internal_nocache_insert.php" => true,
  386. "smarty_internal_parsetree.php" => true,
  387. "smarty_internal_parsetree_code.php" => true,
  388. "smarty_internal_parsetree_dq.php" => true,
  389. "smarty_internal_parsetree_dqcontent.php" => true,
  390. "smarty_internal_parsetree_tag.php" => true,
  391. "smarty_internal_parsetree_template.php" => true,
  392. "smarty_internal_parsetree_text.php" => true,
  393. "smarty_internal_resource_eval.php" => true,
  394. "smarty_internal_resource_extends.php" => true,
  395. "smarty_internal_resource_file.php" => true,
  396. "smarty_internal_resource_php.php" => true,
  397. "smarty_internal_resource_registered.php" => true,
  398. "smarty_internal_resource_stream.php" => true,
  399. "smarty_internal_resource_string.php" => true,
  400. "smarty_internal_smartytemplatecompiler.php" => true,
  401. "smarty_internal_template.php" => true,
  402. "smarty_internal_templatebase.php" => true,
  403. "smarty_internal_templatecompilerbase.php" => true,
  404. "smarty_internal_templatelexer.php" => true,
  405. "smarty_internal_templateparser.php" => true,
  406. "smarty_internal_utility.php" => true,
  407. "smarty_internal_write_file.php" => true,
  408. "smarty_resource.php" => true,
  409. "smarty_resource_custom.php" => true,
  410. "smarty_resource_recompiled.php" => true,
  411. "smarty_resource_uncompiled.php" => true,
  412. "smarty_security.php" => true,
  413. "smarty_template_cached.php" => true,
  414. "smarty_template_compiled.php" => true,
  415. "smarty_template_config.php" => true,
  416. "smarty_template_source.php" => true,
  417. "smarty_undefined_variable.php" => true,
  418. "smarty_variable.php" => true,
  419. "smartycompilerexception.php" => true,
  420. "smartyexception.php" => true,
  421. );
  422. $iterator = new DirectoryIterator($source);
  423. foreach ($iterator as $file) {
  424. if (!$file->isDot()) {
  425. $filename = $file->getFilename();
  426. if (isset($expected[$filename])) {
  427. unset($expected[$filename]);
  428. }
  429. }
  430. }
  431. if ($expected) {
  432. $status = false;
  433. $message = "FAILED: files missing from libs/sysplugins: " . join(', ', array_keys($expected));
  434. if ($errors === null) {
  435. echo $message . ".\n";
  436. } else {
  437. $errors['sysplugins'] = $message;
  438. }
  439. } elseif ($errors === null) {
  440. echo "... OK\n";
  441. }
  442. } else {
  443. $status = false;
  444. $message = "FAILED: " . SMARTY_SYSPLUGINS_DIR . ' is not a directory';
  445. if ($errors === null) {
  446. echo $message . ".\n";
  447. } else {
  448. $errors['sysplugins_dir_constant'] = $message;
  449. }
  450. }
  451. if ($errors === null) {
  452. echo "Testing plugin files...\n";
  453. }
  454. // test if core plugins are available
  455. $source = SMARTY_PLUGINS_DIR;
  456. if (is_dir($source)) {
  457. $expected = array(
  458. "block.textformat.php" => true,
  459. "function.counter.php" => true,
  460. "function.cycle.php" => true,
  461. "function.fetch.php" => true,
  462. "function.html_checkboxes.php" => true,
  463. "function.html_image.php" => true,
  464. "function.html_options.php" => true,
  465. "function.html_radios.php" => true,
  466. "function.html_select_date.php" => true,
  467. "function.html_select_time.php" => true,
  468. "function.html_table.php" => true,
  469. "function.mailto.php" => true,
  470. "function.math.php" => true,
  471. "modifier.capitalize.php" => true,
  472. "modifier.date_format.php" => true,
  473. "modifier.debug_print_var.php" => true,
  474. "modifier.escape.php" => true,
  475. "modifier.regex_replace.php" => true,
  476. "modifier.replace.php" => true,
  477. "modifier.spacify.php" => true,
  478. "modifier.truncate.php" => true,
  479. "modifiercompiler.cat.php" => true,
  480. "modifiercompiler.count_characters.php" => true,
  481. "modifiercompiler.count_paragraphs.php" => true,
  482. "modifiercompiler.count_sentences.php" => true,
  483. "modifiercompiler.count_words.php" => true,
  484. "modifiercompiler.default.php" => true,
  485. "modifiercompiler.escape.php" => true,
  486. "modifiercompiler.from_charset.php" => true,
  487. "modifiercompiler.indent.php" => true,
  488. "modifiercompiler.lower.php" => true,
  489. "modifiercompiler.noprint.php" => true,
  490. "modifiercompiler.string_format.php" => true,
  491. "modifiercompiler.strip.php" => true,
  492. "modifiercompiler.strip_tags.php" => true,
  493. "modifiercompiler.to_charset.php" => true,
  494. "modifiercompiler.unescape.php" => true,
  495. "modifiercompiler.upper.php" => true,
  496. "modifiercompiler.wordwrap.php" => true,
  497. "outputfilter.trimwhitespace.php" => true,
  498. "shared.escape_special_chars.php" => true,
  499. "shared.literal_compiler_param.php" => true,
  500. "shared.make_timestamp.php" => true,
  501. "shared.mb_str_replace.php" => true,
  502. "shared.mb_unicode.php" => true,
  503. "shared.mb_wordwrap.php" => true,
  504. "variablefilter.htmlspecialchars.php" => true,
  505. );
  506. $iterator = new DirectoryIterator($source);
  507. foreach ($iterator as $file) {
  508. if (!$file->isDot()) {
  509. $filename = $file->getFilename();
  510. if (isset($expected[$filename])) {
  511. unset($expected[$filename]);
  512. }
  513. }
  514. }
  515. if ($expected) {
  516. $status = false;
  517. $message = "FAILED: files missing from libs/plugins: " . join(', ', array_keys($expected));
  518. if ($errors === null) {
  519. echo $message . ".\n";
  520. } else {
  521. $errors['plugins'] = $message;
  522. }
  523. } elseif ($errors === null) {
  524. echo "... OK\n";
  525. }
  526. } else {
  527. $status = false;
  528. $message = "FAILED: " . SMARTY_PLUGINS_DIR . ' is not a directory';
  529. if ($errors === null) {
  530. echo $message . ".\n";
  531. } else {
  532. $errors['plugins_dir_constant'] = $message;
  533. }
  534. }
  535. if ($errors === null) {
  536. echo "Tests complete.\n";
  537. echo "</PRE>\n";
  538. }
  539. return $status;
  540. }
  541. }