smarty_internal_compile_include_php.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /**
  3. * Smarty Internal Plugin Compile Include PHP
  4. * Compiles the {include_php} tag
  5. *
  6. * @package Smarty
  7. * @subpackage Compiler
  8. * @author Uwe Tews
  9. */
  10. /**
  11. * Smarty Internal Plugin Compile Insert Class
  12. *
  13. * @package Smarty
  14. * @subpackage Compiler
  15. */
  16. class Smarty_Internal_Compile_Include_Php extends Smarty_Internal_CompileBase
  17. {
  18. /**
  19. * Attribute definition: Overwrites base class.
  20. *
  21. * @var array
  22. * @see Smarty_Internal_CompileBase
  23. */
  24. public $required_attributes = array('file');
  25. /**
  26. * Attribute definition: Overwrites base class.
  27. *
  28. * @var array
  29. * @see Smarty_Internal_CompileBase
  30. */
  31. public $shorttag_order = array('file');
  32. /**
  33. * Attribute definition: Overwrites base class.
  34. *
  35. * @var array
  36. * @see Smarty_Internal_CompileBase
  37. */
  38. public $optional_attributes = array('once', 'assign');
  39. /**
  40. * Compiles code for the {include_php} tag
  41. *
  42. * @param array $args array with attributes from parser
  43. * @param object $compiler compiler object
  44. *
  45. * @throws SmartyException
  46. * @return string compiled code
  47. */
  48. public function compile($args, $compiler)
  49. {
  50. if (!($compiler->smarty instanceof SmartyBC)) {
  51. throw new SmartyException("{include_php} is deprecated, use SmartyBC class to enable");
  52. }
  53. // check and get attributes
  54. $_attr = $this->getAttributes($compiler, $args);
  55. /** @var Smarty_Internal_Template $_smarty_tpl
  56. * used in evaluated code
  57. */
  58. $_smarty_tpl = $compiler->template;
  59. $_filepath = false;
  60. eval('$_file = ' . $_attr['file'] . ';');
  61. if (!isset($compiler->smarty->security_policy) && file_exists($_file)) {
  62. $_filepath = $_file;
  63. } else {
  64. if (isset($compiler->smarty->security_policy)) {
  65. $_dir = $compiler->smarty->security_policy->trusted_dir;
  66. } else {
  67. $_dir = $compiler->smarty->trusted_dir;
  68. }
  69. if (!empty($_dir)) {
  70. foreach ((array) $_dir as $_script_dir) {
  71. $_script_dir = rtrim($_script_dir, '/\\') . DS;
  72. if (file_exists($_script_dir . $_file)) {
  73. $_filepath = $_script_dir . $_file;
  74. break;
  75. }
  76. }
  77. }
  78. }
  79. if ($_filepath == false) {
  80. $compiler->trigger_template_error("{include_php} file '{$_file}' is not readable", $compiler->lex->taglineno);
  81. }
  82. if (isset($compiler->smarty->security_policy)) {
  83. $compiler->smarty->security_policy->isTrustedPHPDir($_filepath);
  84. }
  85. if (isset($_attr['assign'])) {
  86. // output will be stored in a smarty variable instead of being displayed
  87. $_assign = $_attr['assign'];
  88. }
  89. $_once = '_once';
  90. if (isset($_attr['once'])) {
  91. if ($_attr['once'] == 'false') {
  92. $_once = '';
  93. }
  94. }
  95. if (isset($_assign)) {
  96. return "<?php ob_start(); include{$_once} ('{$_filepath}'); \$_smarty_tpl->assign({$_assign},ob_get_contents()); ob_end_clean();?>";
  97. } else {
  98. return "<?php include{$_once} ('{$_filepath}');?>\n";
  99. }
  100. }
  101. }