TheFreeNetworkModule.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * TheFreeNetwork, "automagic" migration of internal remote profiles
  18. * between different federation protocols.
  19. *
  20. * @package GNUsocial
  21. * @author Bruno Casteleiro <brunoccast@fc.up.pt>
  22. * @copyright 2019 Free Software Foundation, Inc http://www.fsf.org
  23. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  24. */
  25. defined('GNUSOCIAL') || die();
  26. /**
  27. * Class TheFreeNetworkModule
  28. * This module ensures that multiple protocols serving the same purpose won't result in duplicated data.
  29. * This class is not to be extended but a developer implementing a new protocol should be aware of it and notify the
  30. * StartTFNCensus event.
  31. *
  32. * @category Module
  33. * @package GNUsocial
  34. * @author Bruno Casteleiro <brunoccast@fc.up.pt>
  35. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  36. */
  37. class TheFreeNetworkModule extends Module
  38. {
  39. const MODULE_VERSION = '0.1.0alpha0';
  40. public $protocols = null; // protocols TFN should handle
  41. public $priority = []; // protocols preferences
  42. private $lrdd = false; // whether LRDD plugin is active or not
  43. /**
  44. * Initialize TFN
  45. *
  46. * @return bool hook value
  47. */
  48. public function onInitializePlugin(): bool
  49. {
  50. // some protocol plugins can be unactivated,
  51. // require needed classes
  52. $plugin_dir = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'plugins';
  53. $p = 0;
  54. foreach ($this->protocols as $protocol => $class) {
  55. $this->priority[$class] = $p++;
  56. require_once $plugin_dir . DIRECTORY_SEPARATOR . $protocol . DIRECTORY_SEPARATOR . 'classes' . DIRECTORY_SEPARATOR . $class . '.php';
  57. }
  58. unset($p);
  59. // $lrdd flag
  60. $this->lrdd = PluginList::isPluginActive("LRDD");
  61. return true;
  62. }
  63. /**
  64. * A new remote profile is being added, check if we
  65. * already have someone with the same URI.
  66. *
  67. * @param string $uri
  68. * @param string $class profile class that triggered this event
  69. * @param int|null &$profile_id Profile:id associated with the remote entity found
  70. * @return bool hook flag false
  71. * @throws AlreadyHandledException Do not allow to create a profile if a preferred protocol already has one
  72. */
  73. public function onStartTFNLookup(string $uri, string $class, int &$profile_id = null): bool
  74. {
  75. [$profile_id, $cls] = $this->lookup($uri, $class) ?? [null, null];
  76. if (is_null($profile_id)) {
  77. $perf = common_config('performance', 'high');
  78. if (!$perf && $this->lrdd) {
  79. // Force lookup with online resources
  80. [$profile_id, $cls] = $this->lookup($uri, $class, true);
  81. }
  82. }
  83. // $cls being lower then $class means it has a higher priority (comes first) @see $this->onInitializePlugin()
  84. if (!is_null($profile_id) && $this->priority[$cls] < $this->priority[$class]) {
  85. throw new AlreadyHandledException("TheFreeNetworkModule->AlreadyHandled: $cls is preferred over $class");
  86. }
  87. return false;
  88. }
  89. /**
  90. * A new remote profile was successfully added, delete
  91. * other remotes associated with the same Profile entity.
  92. *
  93. * @param string $class profile class that triggered this event
  94. * @param int $profile_id Profile:id associated with the new remote profile
  95. * @return bool hook flag
  96. */
  97. public function onEndTFNLookup(string $class, int $profile_id): bool
  98. {
  99. foreach ($this->protocols as $p => $cls) {
  100. if ($cls != $class) {
  101. $profile = $cls::getKV('profile_id', $profile_id);
  102. if ($profile instanceof $cls) {
  103. $this->log(LOG_INFO, 'Deleting remote ' . $cls . ' associated with Profile:' . $profile_id);
  104. $i = new $cls();
  105. $i->profile_id = $profile_id;
  106. $i->delete();
  107. break;
  108. }
  109. }
  110. }
  111. return false;
  112. }
  113. /**
  114. * Plugin version information
  115. *
  116. * @param array $versions
  117. * @return bool hook true
  118. */
  119. public function onPluginVersion(array &$versions): bool
  120. {
  121. $versions[] = [
  122. 'name' => 'TheFreeNetwork',
  123. 'version' => self::MODULE_VERSION,
  124. 'author' => 'Bruno Casteleiro',
  125. 'homepage' => 'https://notabug.org/diogo/gnu-social/src/nightly/plugins/TheFreeNetwork',
  126. // TRANS: Module description.
  127. 'rawdescription' => '"Automagically" migrate internal remote profiles between different federation protocols'
  128. ];
  129. return true;
  130. }
  131. /**
  132. * Search remote profile tables to find someone by URI.
  133. * When set to search online, it will grab the remote
  134. * entity's aliases and search with each one.
  135. * The table associated with the class that triggered
  136. * this lookup process will be discarded in the search.
  137. *
  138. * @param string $uri
  139. * @param string $class
  140. * @param bool $online
  141. * @return null|array [Profile:id, class] associated with the remote entity found
  142. */
  143. private function lookup(string $uri, string $class, bool $online = false): ?array
  144. {
  145. if ($online) {
  146. $this->log(LOG_INFO, 'Searching with online resources for a remote profile with URI: ' . $uri);
  147. $all_ids = LRDDPlugin::grab_profile_aliases($uri);
  148. } else {
  149. $this->log(LOG_INFO, 'Searching for a remote profile with URI: ' . $uri);
  150. $all_ids = [$uri];
  151. }
  152. if ($all_ids == null) {
  153. $this->log(LOG_INFO, 'Unable to find a remote profile with URI: ' . $uri);
  154. return null;
  155. }
  156. foreach ($this->protocols as $p => $cls) {
  157. if ($cls != $class) {
  158. foreach ($all_ids as $alias) {
  159. $profile = $cls::getKV('uri', $alias);
  160. if ($profile instanceof $cls) {
  161. $this->log(LOG_INFO, 'Found a remote ' . $cls . ' associated with Profile:' . $profile->getID());
  162. return [$profile->getID(), $cls];
  163. }
  164. }
  165. }
  166. }
  167. $this->log(LOG_INFO, 'Unable to find a remote profile with URI: ' . $uri);
  168. return null;
  169. }
  170. }