123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- <?php
- defined('GNUSOCIAL') || die();
- class Plugin extends Module
- {
- public function __construct()
- {
- Event::addHandler('InitializePlugin', [$this, 'initialize']);
- Event::addHandler('CleanupPlugin', [$this, 'cleanup']);
- foreach (get_class_methods($this) as $method) {
- if (mb_substr($method, 0, 2) == 'on') {
- Event::addHandler(mb_substr($method, 2), [$this, $method]);
- }
- }
- $this->setupGettext();
- }
- public function initialize()
- {
- return true;
- }
- public function cleanup()
- {
- return true;
- }
-
- public function onAutoload($cls)
- {
- $cls = basename($cls);
- $basedir = INSTALLDIR . '/local/plugins/' . mb_substr(get_called_class(), 0, -6);
- if (!file_exists($basedir)) {
- $basedir = INSTALLDIR . '/plugins/' . mb_substr(get_called_class(), 0, -6);
- }
- $file = null;
- if (preg_match('/^(\w+)(Action|Form)$/', $cls, $type)) {
- $type = array_map('strtolower', $type);
- $file = "{$basedir}/{$type[2]}s/{$type[1]}.php";
- }
- if (!file_exists($file)) {
- $file = "{$basedir}/classes/{$cls}.php";
-
-
- if (!file_exists($file)) {
- $type = strtolower($cls);
- $type = str_replace('_', '/', $type);
- $file = "{$basedir}/lib/{$type}.php";
- }
- }
- if (!is_null($file) && file_exists($file)) {
- require_once $file;
- return false;
- }
- return true;
- }
-
- protected function setupGettext()
- {
- $class = get_class($this);
- if (substr($class, -6) == 'Plugin') {
- $name = substr($class, 0, -6);
- $path = common_config('plugins', 'locale_path');
- if (!$path) {
-
-
- $path = INSTALLDIR . "/plugins/{$name}/locale";
- if (!file_exists($path)) {
- $path = INSTALLDIR . "/local/plugins/{$name}/locale";
- }
- }
- if (file_exists($path) && is_dir($path)) {
- bindtextdomain($name, $path);
- bind_textdomain_codeset($name, 'UTF-8');
- }
- }
- }
- public function onPluginVersion(array &$versions): bool
- {
- $name = $this->name();
- $versions[] = [
- 'name' => $name,
-
- 'version' => _m('Unknown')
- ];
- return true;
- }
- public function onModuleVersion(array &$versions): bool
- {
- return true;
- }
- public static function staticPath($plugin, $relative)
- {
- if (GNUsocial::useHTTPS()) {
- $server = common_config('plugins', 'sslserver');
- } else {
- $server = common_config('plugins', 'server');
- }
- if (empty($server)) {
- if (GNUsocial::useHTTPS()) {
- $server = common_config('site', 'sslserver');
- }
- if (empty($server)) {
- $server = common_config('site', 'server');
- }
- }
- if (GNUsocial::useHTTPS()) {
- $path = common_config('plugins', 'sslpath');
- } else {
- $path = common_config('plugins', 'path');
- }
- if (empty($path)) {
-
- if (@file_exists(PUBLICDIR . '/local/plugins/' . $plugin . '/' . $relative)) {
- $path = common_config('site', 'path') . '/local/plugins/';
- } else {
- $path = common_config('site', 'path') . '/plugins/';
- }
- }
- if ($path[strlen($path) - 1] != '/') {
- $path .= '/';
- }
- if ($path[0] != '/') {
- $path = '/' . $path;
- }
- $protocol = GNUsocial::useHTTPS() ? 'https' : 'http';
- return $protocol . '://' . $server . $path . $plugin . '/' . $relative;
- }
- public function path($relative)
- {
- return self::staticPath($this->name(), $relative);
- }
- }
|