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_method_loadplugin.php 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /**
  3. * Smarty Extension Loadplugin
  4. *
  5. * $smarty->loadPlugin() method
  6. *
  7. * @package Smarty
  8. * @subpackage PluginsInternal
  9. * @author Uwe Tews
  10. */
  11. class Smarty_Internal_Method_LoadPlugin
  12. {
  13. /**
  14. * Cache of searched plugin files
  15. *
  16. * @var array
  17. */
  18. public $plugin_files = array();
  19. /**
  20. * Takes unknown classes and loads plugin files for them
  21. * class name format: Smarty_PluginType_PluginName
  22. * plugin filename format: plugintype.pluginname.php
  23. *
  24. * @param \Smarty $smarty
  25. * @param string $plugin_name class plugin name to load
  26. * @param bool $check check if already loaded
  27. *
  28. * @return bool|string
  29. * @throws \SmartyException
  30. */
  31. public function loadPlugin(Smarty $smarty, $plugin_name, $check)
  32. {
  33. // if function or class exists, exit silently (already loaded)
  34. if ($check && (is_callable($plugin_name) || class_exists($plugin_name, false))) {
  35. return true;
  36. }
  37. if (!preg_match('#^smarty_((internal)|([^_]+))_(.+)$#i', $plugin_name, $match)) {
  38. throw new SmartyException("plugin {$plugin_name} is not a valid name format");
  39. }
  40. if (!empty($match[2])) {
  41. $file = SMARTY_SYSPLUGINS_DIR . strtolower($plugin_name) . '.php';
  42. if (isset($this->plugin_files[$file])) {
  43. if ($this->plugin_files[$file] !== false) {
  44. return $this->plugin_files[$file];
  45. } else {
  46. return false;
  47. }
  48. } else {
  49. if (is_file($file)) {
  50. $this->plugin_files[$file] = $file;
  51. require_once($file);
  52. return $file;
  53. } else {
  54. $this->plugin_files[$file] = false;
  55. return false;
  56. }
  57. }
  58. }
  59. // plugin filename is expected to be: [type].[name].php
  60. $_plugin_filename = "{$match[1]}.{$match[4]}.php";
  61. $_lower_filename = strtolower($_plugin_filename);
  62. if (isset($this->plugin_files)) {
  63. if (isset($this->plugin_files['plugins_dir'][$_lower_filename])) {
  64. if (!$smarty->use_include_path || $this->plugin_files['plugins_dir'][$_lower_filename] !== false) {
  65. return $this->plugin_files['plugins_dir'][$_lower_filename];
  66. }
  67. }
  68. if (!$smarty->use_include_path || $smarty->ext->_getIncludePath->isNewIncludePath($smarty)) {
  69. unset($this->plugin_files['include_path']);
  70. } else {
  71. if (isset($this->plugin_files['include_path'][$_lower_filename])) {
  72. return $this->plugin_files['include_path'][$_lower_filename];
  73. }
  74. }
  75. }
  76. $_file_names = array($_plugin_filename);
  77. if ($_lower_filename != $_plugin_filename) {
  78. $_file_names[] = $_lower_filename;
  79. }
  80. $_p_dirs = $smarty->getPluginsDir();
  81. if (!isset($this->plugin_files['plugins_dir'][$_lower_filename])) {
  82. // loop through plugin dirs and find the plugin
  83. foreach ($_p_dirs as $_plugin_dir) {
  84. foreach ($_file_names as $name) {
  85. $file = $_plugin_dir . $name;
  86. if (is_file($file)) {
  87. $this->plugin_files['plugins_dir'][$_lower_filename] = $file;
  88. require_once($file);
  89. return $file;
  90. }
  91. $this->plugin_files['plugins_dir'][$_lower_filename] = false;
  92. }
  93. }
  94. }
  95. if ($smarty->use_include_path) {
  96. foreach ($_file_names as $_file_name) {
  97. // try PHP include_path
  98. $file = $smarty->ext->_getIncludePath->getIncludePath($_p_dirs, $_file_name, $smarty);
  99. $this->plugin_files['include_path'][$_lower_filename] = $file;
  100. if ($file !== false) {
  101. require_once($file);
  102. return $file;
  103. }
  104. }
  105. }
  106. // no plugin loaded
  107. return false;
  108. }
  109. }