smarty_internal_compile_include_php.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. * @package Smarty
  15. * @subpackage Compiler
  16. */
  17. class Smarty_Internal_Compile_Include_Php extends Smarty_Internal_CompileBase {
  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. * @return string compiled code
  45. */
  46. public function compile($args, $compiler)
  47. {
  48. if (!($compiler->smarty instanceof SmartyBC)) {
  49. throw new SmartyException("{include_php} is deprecated, use SmartyBC class to enable");
  50. }
  51. // check and get attributes
  52. $_attr = $this->getAttributes($compiler, $args);
  53. $_output = '<?php ';
  54. $_smarty_tpl = $compiler->template;
  55. $_filepath = false;
  56. eval('$_file = ' . $_attr['file'] . ';');
  57. if (!isset($compiler->smarty->security_policy) && file_exists($_file)) {
  58. $_filepath = $_file;
  59. } else {
  60. if (isset($compiler->smarty->security_policy)) {
  61. $_dir = $compiler->smarty->security_policy->trusted_dir;
  62. } else {
  63. $_dir = $compiler->smarty->trusted_dir;
  64. }
  65. if (!empty($_dir)) {
  66. foreach((array)$_dir as $_script_dir) {
  67. $_script_dir = rtrim($_script_dir, '/\\') . DS;
  68. if (file_exists($_script_dir . $_file)) {
  69. $_filepath = $_script_dir . $_file;
  70. break;
  71. }
  72. }
  73. }
  74. }
  75. if ($_filepath == false) {
  76. $compiler->trigger_template_error("{include_php} file '{$_file}' is not readable", $compiler->lex->taglineno);
  77. }
  78. if (isset($compiler->smarty->security_policy)) {
  79. $compiler->smarty->security_policy->isTrustedPHPDir($_filepath);
  80. }
  81. if (isset($_attr['assign'])) {
  82. // output will be stored in a smarty variable instead of being displayed
  83. $_assign = $_attr['assign'];
  84. }
  85. $_once = '_once';
  86. if (isset($_attr['once'])) {
  87. if ($_attr['once'] == 'false') {
  88. $_once = '';
  89. }
  90. }
  91. if (isset($_assign)) {
  92. return "<?php ob_start(); include{$_once} ('{$_filepath}'); \$_smarty_tpl->assign({$_assign},ob_get_contents()); ob_end_clean();?>";
  93. } else {
  94. return "<?php include{$_once} ('{$_filepath}');?>\n";
  95. }
  96. }
  97. }
  98. ?>