Plugin.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 GNU social plugin. 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 Module
  31. * @package GNUsocial
  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 extends Module
  39. {
  40. public $widgetOpts;
  41. public $scoped;
  42. public function __construct()
  43. {
  44. Event::addHandler('InitializePlugin', [$this, 'initialize']);
  45. Event::addHandler('CleanupPlugin', [$this, 'cleanup']);
  46. foreach (get_class_methods($this) as $method) {
  47. if (mb_substr($method, 0, 2) == 'on') {
  48. Event::addHandler(mb_substr($method, 2), [$this, $method]);
  49. }
  50. }
  51. $this->setupGettext();
  52. }
  53. public function initialize()
  54. {
  55. return true;
  56. }
  57. public function cleanup()
  58. {
  59. return true;
  60. }
  61. /**
  62. * Load related components when needed
  63. *
  64. * Most non-trivial plugins will require extra components to do their work. Typically
  65. * these include data classes, action classes, widget classes, or external libraries.
  66. *
  67. * This method receives a class name and loads the PHP file related to that class. By
  68. * tradition, action classes typically have files named for the action, all lower-case.
  69. * Data classes are in files with the data class name, initial letter capitalized.
  70. *
  71. * Note that this method will be called for *all* overloaded classes, not just ones
  72. * in this plugin! So, make sure to return true by default to let other plugins, and
  73. * the core code, get a chance.
  74. *
  75. * @param string $cls Name of the class to be loaded
  76. *
  77. * @return bool hook value; true means continue processing, false means stop.
  78. */
  79. public function onAutoload($cls)
  80. {
  81. $cls = basename($cls);
  82. $basedir = INSTALLDIR . '/local/plugins/' . mb_substr(get_called_class(), 0, -6);
  83. if (!file_exists($basedir)) {
  84. $basedir = INSTALLDIR . '/plugins/' . mb_substr(get_called_class(), 0, -6);
  85. }
  86. $file = null;
  87. if (preg_match('/^(\w+)(Action|Form)$/', $cls, $type)) {
  88. $type = array_map('strtolower', $type);
  89. $file = "{$basedir}/{$type[2]}s/{$type[1]}.php";
  90. }
  91. if ($file === null || !file_exists($file)) {
  92. $file = "{$basedir}/classes/{$cls}.php";
  93. // library files can be put into subdirs ('_'->'/' conversion)
  94. // such as LRDDMethod_WebFinger -> lib/lrddmethod/webfinger.php
  95. if (!file_exists($file)) {
  96. $type = strtolower($cls);
  97. $type = str_replace('_', '/', $type);
  98. $file = "{$basedir}/lib/{$type}.php";
  99. }
  100. }
  101. if (!is_null($file) && file_exists($file)) {
  102. require_once $file;
  103. return false;
  104. }
  105. return true;
  106. }
  107. /**
  108. * Checks if this plugin has localization that needs to be set up.
  109. * Gettext localizations can be called via the _m() helper function.
  110. */
  111. protected function setupGettext()
  112. {
  113. $class = get_class($this);
  114. if (substr($class, -6) == 'Plugin') {
  115. $name = substr($class, 0, -6);
  116. $path = common_config('plugins', 'locale_path');
  117. if (!$path) {
  118. // @fixme this will fail for things installed in local/plugins
  119. // ... but then so will web links so far.
  120. $path = INSTALLDIR . "/plugins/{$name}/locale";
  121. if (!file_exists($path)) {
  122. $path = INSTALLDIR . "/local/plugins/{$name}/locale";
  123. }
  124. }
  125. if (file_exists($path) && is_dir($path)) {
  126. bindtextdomain($name, $path);
  127. bind_textdomain_codeset($name, 'UTF-8');
  128. }
  129. }
  130. }
  131. public function onPluginVersion(array &$versions): bool
  132. {
  133. $name = $this->name();
  134. $versions[] = [
  135. 'name' => $name,
  136. // TRANS: Displayed as version information for a plugin if no version information was found.
  137. 'version' => _m('Unknown')
  138. ];
  139. return true;
  140. }
  141. public function onModuleVersion(array &$versions): bool
  142. {
  143. return true;
  144. }
  145. public static function staticPath($plugin, $relative)
  146. {
  147. if (GNUsocial::useHTTPS()) {
  148. $server = common_config('plugins', 'sslserver');
  149. } else {
  150. $server = common_config('plugins', 'server');
  151. }
  152. if (empty($server)) {
  153. if (GNUsocial::useHTTPS()) {
  154. $server = common_config('site', 'sslserver');
  155. }
  156. if (empty($server)) {
  157. $server = common_config('site', 'server');
  158. }
  159. }
  160. if (GNUsocial::useHTTPS()) {
  161. $path = common_config('plugins', 'sslpath');
  162. } else {
  163. $path = common_config('plugins', 'path');
  164. }
  165. if (empty($path)) {
  166. // XXX: extra stat().
  167. if (@file_exists(PUBLICDIR . '/local/plugins/' . $plugin . '/' . $relative)) {
  168. $path = common_config('site', 'path') . '/local/plugins/';
  169. } else {
  170. $path = common_config('site', 'path') . '/plugins/';
  171. }
  172. }
  173. if ($path[strlen($path) - 1] != '/') {
  174. $path .= '/';
  175. }
  176. if ($path[0] != '/') {
  177. $path = '/' . $path;
  178. }
  179. $protocol = GNUsocial::useHTTPS() ? 'https' : 'http';
  180. return $protocol . '://' . $server . $path . $plugin . '/' . $relative;
  181. }
  182. public function path($relative)
  183. {
  184. return self::staticPath($this->name(), $relative);
  185. }
  186. }