plugin.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. defined('GNUSOCIAL') || die();
  17. /**
  18. * Base class for plugins
  19. *
  20. * A base class for StatusNet plugins. Mostly a light wrapper around
  21. * the Event framework.
  22. *
  23. * Subclasses of Plugin will automatically handle an event if they define
  24. * a method called "onEventName". (Well, OK -- only if they call parent::__construct()
  25. * in their constructors.)
  26. *
  27. * They will also automatically handle the InitializePlugin and CleanupPlugin with the
  28. * initialize() and cleanup() methods, respectively.
  29. *
  30. * @category Plugin
  31. * @package GNU social
  32. * @author Evan Prodromou <evan@status.net>
  33. * @copyright 2010-2019 Free Software Foundation, Inc http://www.fsf.org
  34. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  35. *
  36. * @see Event
  37. */
  38. class Plugin
  39. {
  40. function __construct()
  41. {
  42. Event::addHandler('InitializePlugin', array($this, 'initialize'));
  43. Event::addHandler('CleanupPlugin', array($this, 'cleanup'));
  44. foreach (get_class_methods($this) as $method) {
  45. if (mb_substr($method, 0, 2) == 'on') {
  46. Event::addHandler(mb_substr($method, 2), array($this, $method));
  47. }
  48. }
  49. $this->setupGettext();
  50. }
  51. function initialize()
  52. {
  53. return true;
  54. }
  55. function cleanup()
  56. {
  57. return true;
  58. }
  59. /**
  60. * Load related modules when needed
  61. *
  62. * Most non-trivial plugins will require extra modules to do their work. Typically
  63. * these include data classes, action classes, widget classes, or external libraries.
  64. *
  65. * This method receives a class name and loads the PHP file related to that class. By
  66. * tradition, action classes typically have files named for the action, all lower-case.
  67. * Data classes are in files with the data class name, initial letter capitalized.
  68. *
  69. * Note that this method will be called for *all* overloaded classes, not just ones
  70. * in this plugin! So, make sure to return true by default to let other plugins, and
  71. * the core code, get a chance.
  72. *
  73. * @param string $cls Name of the class to be loaded
  74. *
  75. * @return boolean hook value; true means continue processing, false means stop.
  76. */
  77. public function onAutoload($cls) {
  78. $cls = basename($cls);
  79. $basedir = INSTALLDIR . '/local/plugins/' . mb_substr(get_called_class(), 0, -6);
  80. if (!file_exists($basedir)) {
  81. $basedir = INSTALLDIR . '/plugins/' . mb_substr(get_called_class(), 0, -6);
  82. }
  83. $file = null;
  84. if (preg_match('/^(\w+)(Action|Form)$/', $cls, $type)) {
  85. $type = array_map('strtolower', $type);
  86. $file = "$basedir/{$type[2]}s/{$type[1]}.php";
  87. }
  88. if (!file_exists($file)) {
  89. $file = "$basedir/classes/{$cls}.php";
  90. // library files can be put into subdirs ('_'->'/' conversion)
  91. // such as LRDDMethod_WebFinger -> lib/lrddmethod/webfinger.php
  92. if (!file_exists($file)) {
  93. $type = strtolower($cls);
  94. $type = str_replace('_', '/', $type);
  95. $file = "$basedir/lib/{$type}.php";
  96. }
  97. }
  98. if (!is_null($file) && file_exists($file)) {
  99. require_once($file);
  100. return false;
  101. }
  102. return true;
  103. }
  104. /**
  105. * Checks if this plugin has localization that needs to be set up.
  106. * Gettext localizations can be called via the _m() helper function.
  107. */
  108. protected function setupGettext()
  109. {
  110. $class = get_class($this);
  111. if (substr($class, -6) == 'Plugin') {
  112. $name = substr($class, 0, -6);
  113. $path = common_config('plugins', 'locale_path');
  114. if (!$path) {
  115. // @fixme this will fail for things installed in local/plugins
  116. // ... but then so will web links so far.
  117. $path = INSTALLDIR . "/plugins/$name/locale";
  118. if (!file_exists($path)) {
  119. $path = INSTALLDIR . "/local/plugins/$name/locale";
  120. }
  121. }
  122. if (file_exists($path) && is_dir($path)) {
  123. bindtextdomain($name, $path);
  124. bind_textdomain_codeset($name, 'UTF-8');
  125. }
  126. }
  127. }
  128. protected function log($level, $msg)
  129. {
  130. common_log($level, get_class($this) . ': '.$msg);
  131. }
  132. protected function debug($msg)
  133. {
  134. $this->log(LOG_DEBUG, $msg);
  135. }
  136. public function name()
  137. {
  138. $cls = get_class($this);
  139. return mb_substr($cls, 0, -6);
  140. }
  141. public function version()
  142. {
  143. return GNUSOCIAL_VERSION;
  144. }
  145. protected function userAgent() {
  146. return HTTPClient::userAgent()
  147. . ' (' . get_class($this) . ' v' . $this->version() . ')';
  148. }
  149. function onPluginVersion(array &$versions)
  150. {
  151. $name = $this->name();
  152. $versions[] = array('name' => $name,
  153. // TRANS: Displayed as version information for a plugin if no version information was found.
  154. 'version' => _('Unknown'));
  155. return true;
  156. }
  157. function path($relative)
  158. {
  159. return self::staticPath($this->name(), $relative);
  160. }
  161. static function staticPath($plugin, $relative)
  162. {
  163. if (GNUsocial::useHTTPS()) {
  164. $server = common_config('plugins', 'sslserver');
  165. } else {
  166. $server = common_config('plugins', 'server');
  167. }
  168. if (empty($server)) {
  169. if (GNUsocial::useHTTPS()) {
  170. $server = common_config('site', 'sslserver');
  171. }
  172. if (empty($server)) {
  173. $server = common_config('site', 'server');
  174. }
  175. }
  176. if (GNUsocial::useHTTPS()) {
  177. $path = common_config('plugins', 'sslpath');
  178. } else {
  179. $path = common_config('plugins', 'path');
  180. }
  181. if (empty($path)) {
  182. // XXX: extra stat().
  183. if (@file_exists(PUBLICDIR.'/local/plugins/'.$plugin.'/'.$relative)) {
  184. $path = common_config('site', 'path') . '/local/plugins/';
  185. } else {
  186. $path = common_config('site', 'path') . '/plugins/';
  187. }
  188. }
  189. if ($path[strlen($path)-1] != '/') {
  190. $path .= '/';
  191. }
  192. if ($path[0] != '/') {
  193. $path = '/'.$path;
  194. }
  195. $protocol = GNUsocial::useHTTPS() ? 'https' : 'http';
  196. return $protocol.'://'.$server.$path.$plugin.'/'.$relative;
  197. }
  198. }