You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

smarty_internal_compile_include_php.php 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 \Smarty_Internal_TemplateCompilerBase $compiler compiler object
  44. *
  45. * @return string
  46. * @throws \SmartyCompilerException
  47. * @throws \SmartyException
  48. */
  49. public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler)
  50. {
  51. if (!($compiler->smarty instanceof SmartyBC)) {
  52. throw new SmartyException("{include_php} is deprecated, use SmartyBC class to enable");
  53. }
  54. // check and get attributes
  55. $_attr = $this->getAttributes($compiler, $args);
  56. /** @var Smarty_Internal_Template $_smarty_tpl
  57. * used in evaluated code
  58. */
  59. $_smarty_tpl = $compiler->template;
  60. $_filepath = false;
  61. $_file = null;
  62. eval('$_file = @' . $_attr[ 'file' ] . ';');
  63. if (!isset($compiler->smarty->security_policy) && file_exists($_file)) {
  64. $_filepath = $compiler->smarty->_realpath($_file, true);
  65. } else {
  66. if (isset($compiler->smarty->security_policy)) {
  67. $_dir = $compiler->smarty->security_policy->trusted_dir;
  68. } else {
  69. $_dir = $compiler->smarty->trusted_dir;
  70. }
  71. if (!empty($_dir)) {
  72. foreach ((array)$_dir as $_script_dir) {
  73. $_path = $compiler->smarty->_realpath($_script_dir . DIRECTORY_SEPARATOR . $_file, true);
  74. if (file_exists($_path)) {
  75. $_filepath = $_path;
  76. break;
  77. }
  78. }
  79. }
  80. }
  81. if ($_filepath === false) {
  82. $compiler->trigger_template_error("{include_php} file '{$_file}' is not readable", null, true);
  83. }
  84. if (isset($compiler->smarty->security_policy)) {
  85. $compiler->smarty->security_policy->isTrustedPHPDir($_filepath);
  86. }
  87. if (isset($_attr[ 'assign' ])) {
  88. // output will be stored in a smarty variable instead of being displayed
  89. $_assign = $_attr[ 'assign' ];
  90. }
  91. $_once = '_once';
  92. if (isset($_attr[ 'once' ])) {
  93. if ($_attr[ 'once' ] === 'false') {
  94. $_once = '';
  95. }
  96. }
  97. if (isset($_assign)) {
  98. return "<?php ob_start();\ninclude{$_once} ('{$_filepath}');\n\$_smarty_tpl->assign({$_assign},ob_get_clean());\n?>";
  99. } else {
  100. return "<?php include{$_once} ('{$_filepath}');?>\n";
  101. }
  102. }
  103. }