smarty_internal_compile_include_php.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. /**
  3. * Smarty Internal Plugin Compile Include PHP
  4. *
  5. * Compiles the {include_php} tag
  6. *
  7. * @package Smarty
  8. * @subpackage Compiler
  9. * @author Uwe Tews
  10. */
  11. /**
  12. * Smarty Internal Plugin Compile Insert Class
  13. */
  14. class Smarty_Internal_Compile_Include_Php extends Smarty_Internal_CompileBase {
  15. /**
  16. * Compiles code for the {include_php} tag
  17. *
  18. * @param array $args array with attributes from parser
  19. * @param object $compiler compiler object
  20. * @return string compiled code
  21. */
  22. public function compile($args, $compiler)
  23. {
  24. $this->compiler = $compiler;
  25. $this->required_attributes = array('file');
  26. $this->optional_attributes = array('once', 'assign');
  27. // check and get attributes
  28. $_attr = $this->_get_attributes($args);
  29. $_output = '<?php ';
  30. $_smarty_tpl = $compiler->template;
  31. eval('$_file = ' . $_attr['file'] . ';');
  32. $_file = realpath($_file);
  33. if ($this->compiler->smarty->security) {
  34. $this->compiler->smarty->security_handler->isTrustedPHPDir($_file);
  35. }
  36. if ($_file === false) {
  37. $this->compiler->trigger_template_error('include_php: file "' . $_attr['file'] . '" is not readable');
  38. }
  39. if ($this->compiler->smarty->security) {
  40. $this->compiler->smarty->security_handler->isTrustedPHPDir($_file);
  41. }
  42. if (isset($_attr['assign'])) {
  43. // output will be stored in a smarty variable instead of being displayed
  44. $_assign = $_attr['assign'];
  45. }
  46. $_once = '_once';
  47. if (isset($_attr['once'])) {
  48. if ($_attr['once'] == 'false') {
  49. $_once = '';
  50. }
  51. }
  52. if (isset($_assign)) {
  53. return "<?php ob_start(); include{$_once} ('{$_file}'); \$_smarty_tpl->assign({$_assign},ob_get_contents()); ob_end_clean();?>";
  54. } else {
  55. return "<?php include{$_once} ('{$_file}');?>\n";
  56. }
  57. }
  58. }
  59. ?>