Plugin.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 function __construct()
  41. {
  42. Event::addHandler('InitializePlugin', [$this, 'initialize']);
  43. Event::addHandler('CleanupPlugin', [$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), [$this, $method]);
  47. }
  48. }
  49. $this->setupGettext();
  50. }
  51. public function initialize()
  52. {
  53. return true;
  54. }
  55. public function cleanup()
  56. {
  57. return true;
  58. }
  59. /**
  60. * Load related components when needed
  61. *
  62. * Most non-trivial plugins will require extra components 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 bool hook value; true means continue processing, false means stop.
  76. */
  77. public function onAutoload($cls)
  78. {
  79. $cls = basename($cls);
  80. $basedir = INSTALLDIR . '/local/plugins/' . mb_substr(get_called_class(), 0, -6);
  81. if (!file_exists($basedir)) {
  82. $basedir = INSTALLDIR . '/plugins/' . mb_substr(get_called_class(), 0, -6);
  83. }
  84. $file = null;
  85. if (preg_match('/^(\w+)(Action|Form)$/', $cls, $type)) {
  86. $type = array_map('strtolower', $type);
  87. $file = "{$basedir}/{$type[2]}s/{$type[1]}.php";
  88. }
  89. if (!file_exists($file)) {
  90. $file = "{$basedir}/classes/{$cls}.php";
  91. // library files can be put into subdirs ('_'->'/' conversion)
  92. // such as LRDDMethod_WebFinger -> lib/lrddmethod/webfinger.php
  93. if (!file_exists($file)) {
  94. $type = strtolower($cls);
  95. $type = str_replace('_', '/', $type);
  96. $file = "{$basedir}/lib/{$type}.php";
  97. }
  98. }
  99. if (!is_null($file) && file_exists($file)) {
  100. require_once $file;
  101. return false;
  102. }
  103. return true;
  104. }
  105. /**
  106. * Checks if this plugin has localization that needs to be set up.
  107. * Gettext localizations can be called via the _m() helper function.
  108. */
  109. protected function setupGettext()
  110. {
  111. $class = get_class($this);
  112. if (substr($class, -6) == 'Plugin') {
  113. $name = substr($class, 0, -6);
  114. $path = common_config('plugins', 'locale_path');
  115. if (!$path) {
  116. // @fixme this will fail for things installed in local/plugins
  117. // ... but then so will web links so far.
  118. $path = INSTALLDIR . "/plugins/{$name}/locale";
  119. if (!file_exists($path)) {
  120. $path = INSTALLDIR . "/local/plugins/{$name}/locale";
  121. }
  122. }
  123. if (file_exists($path) && is_dir($path)) {
  124. bindtextdomain($name, $path);
  125. bind_textdomain_codeset($name, 'UTF-8');
  126. }
  127. }
  128. }
  129. public function onPluginVersion(array &$versions): bool
  130. {
  131. $name = $this->name();
  132. $versions[] = [
  133. 'name' => $name,
  134. // TRANS: Displayed as version information for a plugin if no version information was found.
  135. 'version' => _m('Unknown')
  136. ];
  137. return true;
  138. }
  139. public function onModuleVersion(array &$versions): bool
  140. {
  141. return true;
  142. }
  143. public static function staticPath($plugin, $relative)
  144. {
  145. if (GNUsocial::useHTTPS()) {
  146. $server = common_config('plugins', 'sslserver');
  147. } else {
  148. $server = common_config('plugins', 'server');
  149. }
  150. if (empty($server)) {
  151. if (GNUsocial::useHTTPS()) {
  152. $server = common_config('site', 'sslserver');
  153. }
  154. if (empty($server)) {
  155. $server = common_config('site', 'server');
  156. }
  157. }
  158. if (GNUsocial::useHTTPS()) {
  159. $path = common_config('plugins', 'sslpath');
  160. } else {
  161. $path = common_config('plugins', 'path');
  162. }
  163. if (empty($path)) {
  164. // XXX: extra stat().
  165. if (@file_exists(PUBLICDIR . '/local/plugins/' . $plugin . '/' . $relative)) {
  166. $path = common_config('site', 'path') . '/local/plugins/';
  167. } else {
  168. $path = common_config('site', 'path') . '/plugins/';
  169. }
  170. }
  171. if ($path[strlen($path) - 1] != '/') {
  172. $path .= '/';
  173. }
  174. if ($path[0] != '/') {
  175. $path = '/' . $path;
  176. }
  177. $protocol = GNUsocial::useHTTPS() ? 'https' : 'http';
  178. return $protocol . '://' . $server . $path . $plugin . '/' . $relative;
  179. }
  180. public function path($relative)
  181. {
  182. return self::staticPath($this->name(), $relative);
  183. }
  184. }