plugin.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. }
  134. if (file_exists($path) && is_dir($path)) {
  135. bindtextdomain($name, $path);
  136. bind_textdomain_codeset($name, 'UTF-8');
  137. }
  138. }
  139. }
  140. protected function log($level, $msg)
  141. {
  142. common_log($level, get_class($this) . ': '.$msg);
  143. }
  144. protected function debug($msg)
  145. {
  146. $this->log(LOG_DEBUG, $msg);
  147. }
  148. public function name()
  149. {
  150. $cls = get_class($this);
  151. return mb_substr($cls, 0, -6);
  152. }
  153. public function version()
  154. {
  155. return GNUSOCIAL_VERSION;
  156. }
  157. protected function userAgent() {
  158. return HTTPClient::userAgent()
  159. . ' (' . get_class($this) . ' v' . $this->version() . ')';
  160. }
  161. function onPluginVersion(&$versions)
  162. {
  163. $name = $this->name();
  164. $versions[] = array('name' => $name,
  165. // TRANS: Displayed as version information for a plugin if no version information was found.
  166. 'version' => _('Unknown'));
  167. return true;
  168. }
  169. function path($relative)
  170. {
  171. return self::staticPath($this->name(), $relative);
  172. }
  173. static function staticPath($plugin, $relative)
  174. {
  175. $isHTTPS = StatusNet::isHTTPS();
  176. if ($isHTTPS) {
  177. $server = common_config('plugins', 'sslserver');
  178. } else {
  179. $server = common_config('plugins', 'server');
  180. }
  181. if (empty($server)) {
  182. if ($isHTTPS) {
  183. $server = common_config('site', 'sslserver');
  184. }
  185. if (empty($server)) {
  186. $server = common_config('site', 'server');
  187. }
  188. }
  189. if ($isHTTPS) {
  190. $path = common_config('plugins', 'sslpath');
  191. } else {
  192. $path = common_config('plugins', 'path');
  193. }
  194. if (empty($path)) {
  195. // XXX: extra stat().
  196. if (@file_exists(INSTALLDIR.'/local/plugins/'.$plugin.'/'.$relative)) {
  197. $path = common_config('site', 'path') . '/local/plugins/';
  198. } else {
  199. $path = common_config('site', 'path') . '/plugins/';
  200. }
  201. }
  202. if ($path[strlen($path)-1] != '/') {
  203. $path .= '/';
  204. }
  205. if ($path[0] != '/') {
  206. $path = '/'.$path;
  207. }
  208. $protocol = ($isHTTPS) ? 'https' : 'http';
  209. return $protocol.'://'.$server.$path.$plugin.'/'.$relative;
  210. }
  211. }