core.load_plugins.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. /**
  3. * Smarty plugin
  4. * @package Smarty
  5. * @subpackage plugins
  6. */
  7. /**
  8. * Load requested plugins
  9. *
  10. * @param array $plugins
  11. */
  12. // $plugins
  13. function smarty_core_load_plugins($params, &$smarty)
  14. {
  15. foreach ($params['plugins'] as $_plugin_info) {
  16. list($_type, $_name, $_tpl_file, $_tpl_line, $_delayed_loading) = $_plugin_info;
  17. $_plugin = &$smarty->_plugins[$_type][$_name];
  18. /*
  19. * We do not load plugin more than once for each instance of Smarty.
  20. * The following code checks for that. The plugin can also be
  21. * registered dynamically at runtime, in which case template file
  22. * and line number will be unknown, so we fill them in.
  23. *
  24. * The final element of the info array is a flag that indicates
  25. * whether the dynamically registered plugin function has been
  26. * checked for existence yet or not.
  27. */
  28. if (isset($_plugin)) {
  29. if (empty($_plugin[3])) {
  30. if (!is_callable($_plugin[0])) {
  31. $smarty->_trigger_fatal_error("[plugin] $_type '$_name' is not implemented", $_tpl_file, $_tpl_line, __FILE__, __LINE__);
  32. } else {
  33. $_plugin[1] = $_tpl_file;
  34. $_plugin[2] = $_tpl_line;
  35. $_plugin[3] = true;
  36. if (!isset($_plugin[4])) $_plugin[4] = true; /* cacheable */
  37. }
  38. }
  39. continue;
  40. } else if ($_type == 'insert') {
  41. /*
  42. * For backwards compatibility, we check for insert functions in
  43. * the symbol table before trying to load them as a plugin.
  44. */
  45. $_plugin_func = 'insert_' . $_name;
  46. if (function_exists($_plugin_func)) {
  47. $_plugin = array($_plugin_func, $_tpl_file, $_tpl_line, true, false);
  48. continue;
  49. }
  50. }
  51. $_plugin_file = $smarty->_get_plugin_filepath($_type, $_name);
  52. if (! $_found = ($_plugin_file != false)) {
  53. $_message = "could not load plugin file '$_type.$_name.php'\n";
  54. }
  55. /*
  56. * If plugin file is found, it -must- provide the properly named
  57. * plugin function. In case it doesn't, simply output the error and
  58. * do not fall back on any other method.
  59. */
  60. if ($_found) {
  61. include_once $_plugin_file;
  62. $_plugin_func = 'smarty_' . $_type . '_' . $_name;
  63. if (!function_exists($_plugin_func)) {
  64. $smarty->_trigger_fatal_error("[plugin] function $_plugin_func() not found in $_plugin_file", $_tpl_file, $_tpl_line, __FILE__, __LINE__);
  65. continue;
  66. }
  67. }
  68. /*
  69. * In case of insert plugins, their code may be loaded later via
  70. * 'script' attribute.
  71. */
  72. else if ($_type == 'insert' && $_delayed_loading) {
  73. $_plugin_func = 'smarty_' . $_type . '_' . $_name;
  74. $_found = true;
  75. }
  76. /*
  77. * Plugin specific processing and error checking.
  78. */
  79. if (!$_found) {
  80. if ($_type == 'modifier') {
  81. /*
  82. * In case modifier falls back on using PHP functions
  83. * directly, we only allow those specified in the security
  84. * context.
  85. */
  86. if ($smarty->security && !in_array($_name, $smarty->security_settings['MODIFIER_FUNCS'])) {
  87. $_message = "(secure mode) modifier '$_name' is not allowed";
  88. } else {
  89. if (!function_exists($_name)) {
  90. $_message = "modifier '$_name' is not implemented";
  91. } else {
  92. $_plugin_func = $_name;
  93. $_found = true;
  94. }
  95. }
  96. } else if ($_type == 'function') {
  97. /*
  98. * This is a catch-all situation.
  99. */
  100. $_message = "unknown tag - '$_name'";
  101. }
  102. }
  103. if ($_found) {
  104. $smarty->_plugins[$_type][$_name] = array($_plugin_func, $_tpl_file, $_tpl_line, true, true);
  105. } else {
  106. // output error
  107. $smarty->_trigger_fatal_error('[plugin] ' . $_message, $_tpl_file, $_tpl_line, __FILE__, __LINE__);
  108. }
  109. }
  110. }
  111. /* vim: set expandtab: */
  112. ?>