core.get_php_resource.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. /**
  3. * Smarty plugin
  4. * @package Smarty
  5. * @subpackage plugins
  6. */
  7. /**
  8. * Retrieves PHP script resource
  9. *
  10. * sets $php_resource to the returned resource
  11. * @param string $resource
  12. * @param string $resource_type
  13. * @param $php_resource
  14. * @return boolean
  15. */
  16. function smarty_core_get_php_resource(&$params, &$smarty)
  17. {
  18. $params['resource_base_path'] = $smarty->trusted_dir;
  19. $smarty->_parse_resource_name($params, $smarty);
  20. /*
  21. * Find out if the resource exists.
  22. */
  23. if ($params['resource_type'] == 'file') {
  24. $_readable = false;
  25. if(file_exists($params['resource_name']) && is_readable($params['resource_name'])) {
  26. $_readable = true;
  27. } else {
  28. // test for file in include_path
  29. $_params = array('file_path' => $params['resource_name']);
  30. require_once(SMARTY_CORE_DIR . 'core.get_include_path.php');
  31. if(smarty_core_get_include_path($_params, $smarty)) {
  32. $_include_path = $_params['new_file_path'];
  33. $_readable = true;
  34. }
  35. }
  36. } else if ($params['resource_type'] != 'file') {
  37. $_template_source = null;
  38. $_readable = is_callable($smarty->_plugins['resource'][$params['resource_type']][0][0])
  39. && call_user_func_array($smarty->_plugins['resource'][$params['resource_type']][0][0],
  40. array($params['resource_name'], &$_template_source, &$smarty));
  41. }
  42. /*
  43. * Set the error function, depending on which class calls us.
  44. */
  45. if (method_exists($smarty, '_syntax_error')) {
  46. $_error_funcc = '_syntax_error';
  47. } else {
  48. $_error_funcc = 'trigger_error';
  49. }
  50. if ($_readable) {
  51. if ($smarty->security) {
  52. require_once(SMARTY_CORE_DIR . 'core.is_trusted.php');
  53. if (!smarty_core_is_trusted($params, $smarty)) {
  54. $smarty->$_error_funcc('(secure mode) ' . $params['resource_type'] . ':' . $params['resource_name'] . ' is not trusted');
  55. return false;
  56. }
  57. }
  58. } else {
  59. $smarty->$_error_funcc($params['resource_type'] . ':' . $params['resource_name'] . ' is not readable');
  60. return false;
  61. }
  62. if ($params['resource_type'] == 'file') {
  63. $params['php_resource'] = $params['resource_name'];
  64. } else {
  65. $params['php_resource'] = $_template_source;
  66. }
  67. return true;
  68. }
  69. /* vim: set expandtab: */
  70. ?>