123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- <?php
- defined('GNUSOCIAL') || die();
- class TheFreeNetworkModule extends Module
- {
- const MODULE_VERSION = '0.1.0alpha0';
- public $protocols = null;
- public $priority = [];
- private $lrdd = false;
-
- public function onInitializePlugin(): bool
- {
-
-
- $plugin_dir = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'plugins';
- $p = 0;
- foreach ($this->protocols as $protocol => $class) {
- $this->priority[$class] = $p++;
- require_once $plugin_dir . DIRECTORY_SEPARATOR . $protocol . DIRECTORY_SEPARATOR . 'classes' . DIRECTORY_SEPARATOR . $class . '.php';
- }
- unset($p);
-
- $this->lrdd = PluginList::isPluginActive("LRDD");
- return true;
- }
-
- public function onStartTFNLookup(string $uri, string $class, int &$profile_id = null): bool
- {
- [$profile_id, $cls] = $this->lookup($uri, $class) ?? [null, null];
- if (is_null($profile_id)) {
- $perf = common_config('performance', 'high');
- if (!$perf && $this->lrdd) {
-
- [$profile_id, $cls] = $this->lookup($uri, $class, true);
- }
- }
-
- if (!is_null($profile_id) && $this->priority[$cls] < $this->priority[$class]) {
- throw new AlreadyHandledException("TheFreeNetworkModule->AlreadyHandled: $cls is preferred over $class");
- }
- return false;
- }
-
- public function onEndTFNLookup(string $class, int $profile_id): bool
- {
- foreach ($this->protocols as $p => $cls) {
- if ($cls != $class) {
- $profile = $cls::getKV('profile_id', $profile_id);
- if ($profile instanceof $cls) {
- $this->log(LOG_INFO, 'Deleting remote ' . $cls . ' associated with Profile:' . $profile_id);
- $i = new $cls();
- $i->profile_id = $profile_id;
- $i->delete();
- break;
- }
- }
- }
- return false;
- }
-
- public function onPluginVersion(array &$versions): bool
- {
- $versions[] = [
- 'name' => 'TheFreeNetwork',
- 'version' => self::MODULE_VERSION,
- 'author' => 'Bruno Casteleiro',
- 'homepage' => 'https://notabug.org/diogo/gnu-social/src/nightly/plugins/TheFreeNetwork',
-
- 'rawdescription' => '"Automagically" migrate internal remote profiles between different federation protocols'
- ];
- return true;
- }
-
- private function lookup(string $uri, string $class, bool $online = false): ?array
- {
- if ($online) {
- $this->log(LOG_INFO, 'Searching with online resources for a remote profile with URI: ' . $uri);
- $all_ids = LRDDPlugin::grab_profile_aliases($uri);
- } else {
- $this->log(LOG_INFO, 'Searching for a remote profile with URI: ' . $uri);
- $all_ids = [$uri];
- }
- if ($all_ids == null) {
- $this->log(LOG_INFO, 'Unable to find a remote profile with URI: ' . $uri);
- return null;
- }
- foreach ($this->protocols as $p => $cls) {
- if ($cls != $class) {
- foreach ($all_ids as $alias) {
- $profile = $cls::getKV('uri', $alias);
- if ($profile instanceof $cls) {
- $this->log(LOG_INFO, 'Found a remote ' . $cls . ' associated with Profile:' . $profile->getID());
- return [$profile->getID(), $cls];
- }
- }
- }
- }
- $this->log(LOG_INFO, 'Unable to find a remote profile with URI: ' . $uri);
- return null;
- }
- }
|