Module.php 6.8 KB

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