Module.php 6.9 KB

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