plugin.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Utility class for plugins
  6. *
  7. * PHP version 5
  8. *
  9. * LICENCE: This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * @category Plugin
  23. * @package StatusNet
  24. * @author Evan Prodromou <evan@status.net>
  25. * @copyright 2008 StatusNet, Inc.
  26. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  27. * @link http://status.net/
  28. */
  29. if (!defined('STATUSNET') && !defined('LACONICA')) {
  30. exit(1);
  31. }
  32. /**
  33. * Base class for plugins
  34. *
  35. * A base class for StatusNet plugins. Mostly a light wrapper around
  36. * the Event framework.
  37. *
  38. * Subclasses of Plugin will automatically handle an event if they define
  39. * a method called "onEventName". (Well, OK -- only if they call parent::__construct()
  40. * in their constructors.)
  41. *
  42. * They will also automatically handle the InitializePlugin and CleanupPlugin with the
  43. * initialize() and cleanup() methods, respectively.
  44. *
  45. * @category Plugin
  46. * @package StatusNet
  47. * @author Evan Prodromou <evan@status.net>
  48. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  49. * @link http://status.net/
  50. *
  51. * @see Event
  52. */
  53. class Plugin
  54. {
  55. function __construct()
  56. {
  57. Event::addHandler('InitializePlugin', array($this, 'initialize'));
  58. Event::addHandler('CleanupPlugin', array($this, 'cleanup'));
  59. foreach (get_class_methods($this) as $method) {
  60. if (mb_substr($method, 0, 2) == 'on') {
  61. Event::addHandler(mb_substr($method, 2), array($this, $method));
  62. }
  63. }
  64. $this->setupGettext();
  65. }
  66. function initialize()
  67. {
  68. return true;
  69. }
  70. function cleanup()
  71. {
  72. return true;
  73. }
  74. /**
  75. * Load related modules when needed
  76. *
  77. * Most non-trivial plugins will require extra modules to do their work. Typically
  78. * these include data classes, action classes, widget classes, or external libraries.
  79. *
  80. * This method receives a class name and loads the PHP file related to that class. By
  81. * tradition, action classes typically have files named for the action, all lower-case.
  82. * Data classes are in files with the data class name, initial letter capitalized.
  83. *
  84. * Note that this method will be called for *all* overloaded classes, not just ones
  85. * in this plugin! So, make sure to return true by default to let other plugins, and
  86. * the core code, get a chance.
  87. *
  88. * @param string $cls Name of the class to be loaded
  89. *
  90. * @return boolean hook value; true means continue processing, false means stop.
  91. */
  92. public function onAutoload($cls) {
  93. $cls = basename($cls);
  94. $basedir = INSTALLDIR . '/local/plugins/' . mb_substr(get_called_class(), 0, -6);
  95. if (!file_exists($basedir)) {
  96. $basedir = INSTALLDIR . '/plugins/' . mb_substr(get_called_class(), 0, -6);
  97. }
  98. $file = null;
  99. if (preg_match('/^(\w+)(Action|Form)$/', $cls, $type)) {
  100. $type = array_map('strtolower', $type);
  101. $file = "$basedir/{$type[2]}s/{$type[1]}.php";
  102. }
  103. if (!file_exists($file)) {
  104. $file = "$basedir/classes/{$cls}.php";
  105. // library files can be put into subdirs ('_'->'/' conversion)
  106. // such as LRDDMethod_WebFinger -> lib/lrddmethod/webfinger.php
  107. if (!file_exists($file)) {
  108. $type = strtolower($cls);
  109. $type = str_replace('_', '/', $type);
  110. $file = "$basedir/lib/{$type}.php";
  111. }
  112. }
  113. if (!is_null($file) && file_exists($file)) {
  114. require_once($file);
  115. return false;
  116. }
  117. return true;
  118. }
  119. /**
  120. * Checks if this plugin has localization that needs to be set up.
  121. * Gettext localizations can be called via the _m() helper function.
  122. */
  123. protected function setupGettext()
  124. {
  125. $class = get_class($this);
  126. if (substr($class, -6) == 'Plugin') {
  127. $name = substr($class, 0, -6);
  128. $path = common_config('plugins', 'locale_path');
  129. if (!$path) {
  130. // @fixme this will fail for things installed in local/plugins
  131. // ... but then so will web links so far.
  132. $path = INSTALLDIR . "/plugins/$name/locale";
  133. if (!file_exists($path)) {
  134. $path = INSTALLDIR . "/local/plugins/$name/locale";
  135. }
  136. }
  137. if (file_exists($path) && is_dir($path)) {
  138. bindtextdomain($name, $path);
  139. bind_textdomain_codeset($name, 'UTF-8');
  140. }
  141. }
  142. }
  143. protected function log($level, $msg)
  144. {
  145. common_log($level, get_class($this) . ': '.$msg);
  146. }
  147. protected function debug($msg)
  148. {
  149. $this->log(LOG_DEBUG, $msg);
  150. }
  151. public function name()
  152. {
  153. $cls = get_class($this);
  154. return mb_substr($cls, 0, -6);
  155. }
  156. public function version()
  157. {
  158. return GNUSOCIAL_VERSION;
  159. }
  160. protected function userAgent() {
  161. return HTTPClient::userAgent()
  162. . ' (' . get_class($this) . ' v' . $this->version() . ')';
  163. }
  164. function onPluginVersion(array &$versions)
  165. {
  166. $name = $this->name();
  167. $versions[] = array('name' => $name,
  168. // TRANS: Displayed as version information for a plugin if no version information was found.
  169. 'version' => _('Unknown'));
  170. return true;
  171. }
  172. function path($relative)
  173. {
  174. return self::staticPath($this->name(), $relative);
  175. }
  176. static function staticPath($plugin, $relative)
  177. {
  178. if (GNUsocial::useHTTPS()) {
  179. $server = common_config('plugins', 'sslserver');
  180. } else {
  181. $server = common_config('plugins', 'server');
  182. }
  183. if (empty($server)) {
  184. if (GNUsocial::useHTTPS()) {
  185. $server = common_config('site', 'sslserver');
  186. }
  187. if (empty($server)) {
  188. $server = common_config('site', 'server');
  189. }
  190. }
  191. if (GNUsocial::useHTTPS()) {
  192. $path = common_config('plugins', 'sslpath');
  193. } else {
  194. $path = common_config('plugins', 'path');
  195. }
  196. if (empty($path)) {
  197. // XXX: extra stat().
  198. if (@file_exists(INSTALLDIR.'/local/plugins/'.$plugin.'/'.$relative)) {
  199. $path = common_config('site', 'path') . '/local/plugins/';
  200. } else {
  201. $path = common_config('site', 'path') . '/plugins/';
  202. }
  203. }
  204. if ($path[strlen($path)-1] != '/') {
  205. $path .= '/';
  206. }
  207. if ($path[0] != '/') {
  208. $path = '/'.$path;
  209. }
  210. $protocol = GNUsocial::useHTTPS() ? 'https' : 'http';
  211. return $protocol.'://'.$server.$path.$plugin.'/'.$relative;
  212. }
  213. }