3 * StatusNet, the distributed open-source microblogging tool
5 * Utility class for plugins
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.
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.
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/>.
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/
30 if (!defined('STATUSNET') && !defined('LACONICA')) {
35 * Base class for plugins
37 * A base class for StatusNet plugins. Mostly a light wrapper around
38 * the Event framework.
40 * Subclasses of Plugin will automatically handle an event if they define
41 * a method called "onEventName". (Well, OK -- only if they call parent::__construct()
42 * in their constructors.)
44 * They will also automatically handle the InitializePlugin and CleanupPlugin with the
45 * initialize() and cleanup() methods, respectively.
49 * @author Evan Prodromou <evan@status.net>
50 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
51 * @link http://status.net/
58 function __construct()
60 Event::addHandler('InitializePlugin', array($this, 'initialize'));
61 Event::addHandler('CleanupPlugin', array($this, 'cleanup'));
63 foreach (get_class_methods($this) as $method) {
64 if (mb_substr($method, 0, 2) == 'on') {
65 Event::addHandler(mb_substr($method, 2), array($this, $method));
69 $this->setupGettext();
83 * Load related modules when needed
85 * Most non-trivial plugins will require extra modules to do their work. Typically
86 * these include data classes, action classes, widget classes, or external libraries.
88 * This method receives a class name and loads the PHP file related to that class. By
89 * tradition, action classes typically have files named for the action, all lower-case.
90 * Data classes are in files with the data class name, initial letter capitalized.
92 * Note that this method will be called for *all* overloaded classes, not just ones
93 * in this plugin! So, make sure to return true by default to let other plugins, and
94 * the core code, get a chance.
96 * @param string $cls Name of the class to be loaded
98 * @return boolean hook value; true means continue processing, false means stop.
100 public function onAutoload($cls) {
101 $cls = basename($cls);
102 $basedir = INSTALLDIR . '/local/plugins/' . mb_substr(get_called_class(), 0, -6);
103 if (!file_exists($basedir)) {
104 $basedir = INSTALLDIR . '/plugins/' . mb_substr(get_called_class(), 0, -6);
109 if (preg_match('/^(\w+)(Action|Form)$/', $cls, $type)) {
110 $type = array_map('strtolower', $type);
111 $file = "$basedir/{$type[2]}s/{$type[1]}.php";
113 if (!file_exists($file)) {
114 $file = "$basedir/classes/{$cls}.php";
116 // library files can be put into subdirs ('_'->'/' conversion)
117 // such as LRDDMethod_WebFinger -> lib/lrddmethod/webfinger.php
118 if (!file_exists($file)) {
119 $type = strtolower($cls);
120 $type = str_replace('_', '/', $type);
121 $file = "$basedir/lib/{$type}.php";
125 if (!is_null($file) && file_exists($file)) {
134 * Checks if this plugin has localization that needs to be set up.
135 * Gettext localizations can be called via the _m() helper function.
137 protected function setupGettext()
139 $class = get_class($this);
140 if (substr($class, -6) == 'Plugin') {
141 $name = substr($class, 0, -6);
142 $path = common_config('plugins', 'locale_path');
144 // @fixme this will fail for things installed in local/plugins
145 // ... but then so will web links so far.
146 $path = INSTALLDIR . "/plugins/$name/locale";
148 if (file_exists($path) && is_dir($path)) {
149 bindtextdomain($name, $path);
150 bind_textdomain_codeset($name, 'UTF-8');
155 protected function log($level, $msg)
157 common_log($level, get_class($this) . ': '.$msg);
160 protected function debug($msg)
162 $this->log(LOG_DEBUG, $msg);
165 public function name()
167 $cls = get_class($this);
168 return mb_substr($cls, 0, -6);
171 public function version()
173 return GNUSOCIAL_VERSION;
176 protected function userAgent() {
177 return HTTPClient::userAgent()
178 . ' (' . get_class($this) . ' v' . $this->version() . ')';
181 function onPluginVersion(array &$versions)
183 $name = $this->name();
185 $versions[] = array('name' => $name,
186 // TRANS: Displayed as version information for a plugin if no version information was found.
187 'version' => _('Unknown'));
192 function path($relative)
194 return self::staticPath($this->name(), $relative);
197 static function staticPath($plugin, $relative)
199 $isHTTPS = StatusNet::isHTTPS();
202 $server = common_config('plugins', 'sslserver');
204 $server = common_config('plugins', 'server');
207 if (empty($server)) {
209 $server = common_config('site', 'sslserver');
211 if (empty($server)) {
212 $server = common_config('site', 'server');
217 $path = common_config('plugins', 'sslpath');
219 $path = common_config('plugins', 'path');
223 // XXX: extra stat().
224 if (@file_exists(INSTALLDIR.'/local/plugins/'.$plugin.'/'.$relative)) {
225 $path = common_config('site', 'path') . '/local/plugins/';
227 $path = common_config('site', 'path') . '/plugins/';
231 if ($path[strlen($path)-1] != '/') {
235 if ($path[0] != '/') {
239 $protocol = ($isHTTPS) ? 'https' : 'http';
241 return $protocol.'://'.$server.$path.$plugin.'/'.$relative;