]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/plugin.php
plugins onAutoload now only overloads if necessary (extlibs etc.)
[quix0rs-gnu-social.git] / lib / plugin.php
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
30 if (!defined('STATUSNET') && !defined('LACONICA')) {
31     exit(1);
32 }
33
34 /**
35  * Base class for plugins
36  *
37  * A base class for StatusNet plugins. Mostly a light wrapper around
38  * the Event framework.
39  *
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.)
43  *
44  * They will also automatically handle the InitializePlugin and CleanupPlugin with the
45  * initialize() and cleanup() methods, respectively.
46  *
47  * @category Plugin
48  * @package  StatusNet
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/
52  *
53  * @see      Event
54  */
55
56 class Plugin
57 {
58     function __construct()
59     {
60         Event::addHandler('InitializePlugin', array($this, 'initialize'));
61         Event::addHandler('CleanupPlugin', array($this, 'cleanup'));
62
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));
66             }
67         }
68
69         $this->setupGettext();
70     }
71
72     function initialize()
73     {
74         return true;
75     }
76
77     function cleanup()
78     {
79         return true;
80     }
81
82     /**
83      * Load related modules when needed
84      *
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.
87      *
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.
91      *
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.
95      *
96      * @param string $cls Name of the class to be loaded
97      *
98      * @return boolean hook value; true means continue processing, false means stop.
99      */
100     public function onAutoload($cls) {
101         $cls = basename($cls);
102         $basedir = INSTALLDIR . '/plugins/' . mb_substr(get_called_class(), 0, -6);
103         $file = null;
104
105         if (preg_match('/^(\w+)(Action|Form)$/', $cls, $type)) {
106             $type = array_map('strtolower', $type);
107             $file = "$basedir/{$type[2]}s/{$type[1]}.php";
108         } else {
109             $file = "$basedir/classes/{$cls}.php";
110
111             if (!file_exists($file)) {
112                 $type = strtolower($cls);
113                 $file = "$basedir/lib/{$type}.php";
114             }
115         }
116
117         if (!is_null($file) && file_exists($file)) {
118             require_once($file);
119             return false;
120         }
121
122         return true;
123     }
124
125     /**
126      * Checks if this plugin has localization that needs to be set up.
127      * Gettext localizations can be called via the _m() helper function.
128      */
129     protected function setupGettext()
130     {
131         $class = get_class($this);
132         if (substr($class, -6) == 'Plugin') {
133             $name = substr($class, 0, -6);
134             $path = common_config('plugins', 'locale_path');
135             if (!$path) {
136                 // @fixme this will fail for things installed in local/plugins
137                 // ... but then so will web links so far.
138                 $path = INSTALLDIR . "/plugins/$name/locale";
139             }
140             if (file_exists($path) && is_dir($path)) {
141                 bindtextdomain($name, $path);
142                 bind_textdomain_codeset($name, 'UTF-8');
143             }
144         }
145     }
146
147     protected function log($level, $msg)
148     {
149         common_log($level, get_class($this) . ': '.$msg);
150     }
151
152     protected function debug($msg)
153     {
154         $this->log(LOG_DEBUG, $msg);
155     }
156     
157     function name()
158     {
159         $cls = get_class($this);
160         return mb_substr($cls, 0, -6);
161     }
162
163     function onPluginVersion(&$versions)
164     {
165         $name = $this->name();
166
167         $versions[] = array('name' => $name,
168                             // TRANS: Displayed as version information for a plugin if no version information was found.
169                             'version' => _('Unknown'));
170
171         return true;
172     }
173
174     function path($relative)
175     {
176         return self::staticPath($this->name(), $relative);
177     }
178
179     static function staticPath($plugin, $relative)
180     {
181         $isHTTPS = StatusNet::isHTTPS();
182
183         if ($isHTTPS) {
184             $server = common_config('plugins', 'sslserver');
185         } else {
186             $server = common_config('plugins', 'server');
187         }
188
189         if (empty($server)) {
190             if ($isHTTPS) {
191                 $server = common_config('site', 'sslserver');
192             }
193             if (empty($server)) {
194                 $server = common_config('site', 'server');
195             }
196         }
197
198         if ($isHTTPS) {
199             $path = common_config('plugins', 'sslpath');
200         } else {
201             $path = common_config('plugins', 'path');
202         }
203
204         if (empty($path)) {
205             // XXX: extra stat().
206             if (@file_exists(INSTALLDIR.'/local/plugins/'.$plugin.'/'.$relative)) {
207                 $path = common_config('site', 'path') . '/local/plugins/';
208             } else {
209                 $path = common_config('site', 'path') . '/plugins/';
210             }
211         }
212
213         if ($path[strlen($path)-1] != '/') {
214             $path .= '/';
215         }
216
217         if ($path[0] != '/') {
218             $path = '/'.$path;
219         }
220
221         $protocol = ($isHTTPS) ? 'https' : 'http';
222
223         return $protocol.'://'.$server.$path.$plugin.'/'.$relative;
224     }
225 }