Module.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. Event::addHandler('InitializeModule', [$this, 'initialize']);
  43. Event::addHandler('CleanupModule', [$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 . '/modules/' . mb_substr(get_called_class(), 0, -6);
  81. $file = null;
  82. if (preg_match('/^(\w+)(Action|Form)$/', $cls, $type)) {
  83. $type = array_map('strtolower', $type);
  84. $file = "{$basedir}/{$type[2]}s/{$type[1]}.php";
  85. }
  86. if (!file_exists($file)) {
  87. $file = "{$basedir}/classes/{$cls}.php";
  88. // library files can be put into subdirs ('_'->'/' conversion)
  89. // such as LRDDMethod_WebFinger -> lib/lrddmethod/webfinger.php
  90. if (!file_exists($file)) {
  91. $type = strtolower($cls);
  92. $type = str_replace('_', '/', $type);
  93. $file = "{$basedir}/lib/{$type}.php";
  94. }
  95. }
  96. if (!is_null($file) && file_exists($file)) {
  97. require_once $file;
  98. return false;
  99. }
  100. return true;
  101. }
  102. /**
  103. * Checks if this plugin has localization that needs to be set up.
  104. * Gettext localizations can be called via the _m() helper function.
  105. */
  106. protected function setupGettext()
  107. {
  108. $class = get_class($this);
  109. if (substr($class, -6) == 'Module') {
  110. $name = substr($class, 0, -6);
  111. $path = common_config('plugins', 'locale_path');
  112. if (!$path) {
  113. // @fixme this will fail for things installed in local/plugins
  114. // ... but then so will web links so far.
  115. $path = INSTALLDIR . "/modules/{$name}/locale";
  116. }
  117. if (file_exists($path) && is_dir($path)) {
  118. bindtextdomain($name, $path);
  119. bind_textdomain_codeset($name, 'UTF-8');
  120. }
  121. }
  122. }
  123. protected function log($level, $msg)
  124. {
  125. common_log($level, get_class($this) . ': ' . $msg);
  126. }
  127. protected function debug($msg)
  128. {
  129. $this->log(LOG_DEBUG, $msg);
  130. }
  131. public function name()
  132. {
  133. $cls = get_class($this);
  134. return mb_substr($cls, 0, -6);
  135. }
  136. public function version()
  137. {
  138. return GNUSOCIAL_VERSION;
  139. }
  140. protected function userAgent()
  141. {
  142. return HTTPClient::userAgent()
  143. . ' (' . get_class($this) . ' v' . $this->version() . ')';
  144. }
  145. public function onModuleVersion(array &$versions): bool
  146. {
  147. $name = $this->name();
  148. $versions[] = [
  149. 'name' => $name,
  150. // TRANS: Displayed as version information for a plugin if no version information was found.
  151. 'version' => _m('Unknown')
  152. ];
  153. return true;
  154. }
  155. public static function staticPath($module, $relative)
  156. {
  157. if (GNUsocial::useHTTPS()) {
  158. $server = common_config('plugins', 'sslserver');
  159. } else {
  160. $server = common_config('plugins', 'server');
  161. }
  162. if (empty($server)) {
  163. if (GNUsocial::useHTTPS()) {
  164. $server = common_config('site', 'sslserver');
  165. }
  166. if (empty($server)) {
  167. $server = common_config('site', 'server');
  168. }
  169. }
  170. if (GNUsocial::useHTTPS()) {
  171. $path = common_config('plugins', 'sslpath');
  172. } else {
  173. $path = common_config('plugins', 'path');
  174. }
  175. if (empty($path)) {
  176. $path = common_config('site', 'path') . '/modules/';
  177. }
  178. if ($path[strlen($path) - 1] != '/') {
  179. $path .= '/';
  180. }
  181. if ($path[0] != '/') {
  182. $path = '/' . $path;
  183. }
  184. $protocol = GNUsocial::useHTTPS() ? 'https' : 'http';
  185. return $protocol . '://' . $server . $path . $module . '/' . $relative;
  186. }
  187. public function path($relative)
  188. {
  189. return self::staticPath($this->name(), $relative);
  190. }
  191. }