TheFreeNetworkModule.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. private $lrdd = false; // whether LRDD plugin is active or not
  42. /**
  43. * Initialize TFN
  44. *
  45. * @return bool hook value
  46. */
  47. public function onInitializePlugin(): bool
  48. {
  49. // some protocol plugins can be unactivated,
  50. // require needed classes
  51. $plugin_dir = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'plugins';
  52. foreach ($this->protocols as $protocol => $class) {
  53. require_once $plugin_dir . DIRECTORY_SEPARATOR . $protocol . DIRECTORY_SEPARATOR . 'classes' . DIRECTORY_SEPARATOR . $class . '.php';
  54. }
  55. // $lrdd flag
  56. $this->lrdd = PluginList::isPluginActive("LRDD");
  57. return true;
  58. }
  59. /**
  60. * A new remote profile is being added, check if we
  61. * already have someone with the same URI.
  62. *
  63. * @param string $uri
  64. * @param string $class profile class that triggered this event
  65. * @param int|null &$profile_id Profile:id associated with the remote entity found
  66. * @return bool hook flag
  67. */
  68. public function onStartTFNLookup(string $uri, string $class, int &$profile_id = null): bool
  69. {
  70. $profile_id = $this->lookup($uri, $class);
  71. if (is_null($profile_id)) {
  72. $perf = common_config('performance', 'high');
  73. if (!$perf && $this->lrdd) {
  74. // Force lookup with online resources
  75. $profile_id = $this->lookup($uri, $class, true);
  76. }
  77. }
  78. return false;
  79. }
  80. /**
  81. * A new remote profile was sucessfully added, delete
  82. * other remotes associated with the same Profile entity.
  83. *
  84. * @param string $class profile class that triggered this event
  85. * @param int $profile_id Profile:id associated with the new remote profile
  86. * @return bool hook flag
  87. */
  88. public function onEndTFNLookup(string $class, int $profile_id): bool
  89. {
  90. foreach ($this->protocols as $p => $cls) {
  91. if ($cls != $class) {
  92. $profile = $cls::getKV('profile_id', $profile_id);
  93. if ($profile instanceof $cls) {
  94. $this->log(LOG_INFO, 'Deleting remote ' . $cls . ' associated with Profile:' . $profile_id);
  95. $i = new $cls();
  96. $i->profile_id = $profile_id;
  97. $i->delete();
  98. break;
  99. }
  100. }
  101. }
  102. return false;
  103. }
  104. /**
  105. * Plugin version information
  106. *
  107. * @param array $versions
  108. * @return bool hook true
  109. */
  110. public function onPluginVersion(array &$versions): bool
  111. {
  112. $versions[] = [
  113. 'name' => 'TheFreeNetwork',
  114. 'version' => self::MODULE_VERSION,
  115. 'author' => 'Bruno Casteleiro',
  116. 'homepage' => 'https://notabug.org/diogo/gnu-social/src/nightly/plugins/TheFreeNetwork',
  117. // TRANS: Module description.
  118. 'rawdescription' => '"Automagically" migrate internal remote profiles between different federation protocols'
  119. ];
  120. return true;
  121. }
  122. /**
  123. * Search remote profile tables to find someone by URI.
  124. * When set to search online, it will grab the remote
  125. * entity's aliases and search with each one.
  126. * The table associated with the class that triggered
  127. * this lookup process will be discarded in the search.
  128. *
  129. * @param string $uri
  130. * @param string $class
  131. * @param bool $online
  132. * @return int|null Profile:id associated with the remote entity found
  133. */
  134. private function lookup(string $uri, string $class, bool $online = false): ?int
  135. {
  136. if ($online) {
  137. $this->log(LOG_INFO, 'Searching with online resources for a remote profile with URI: ' . $uri);
  138. $all_ids = LRDDPlugin::grab_profile_aliases($uri);
  139. } else {
  140. $this->log(LOG_INFO, 'Searching for a remote profile with URI: ' . $uri);
  141. $all_ids = [$uri];
  142. }
  143. if ($all_ids == null) {
  144. $this->log(LOG_INFO, 'Unable to find a remote profile with URI: ' . $uri);
  145. return null;
  146. }
  147. foreach ($this->protocols as $p => $cls) {
  148. if ($cls != $class) {
  149. foreach ($all_ids as $alias) {
  150. $profile = $cls::getKV('uri', $alias);
  151. if ($profile instanceof $cls) {
  152. $this->log(LOG_INFO, 'Found a remote ' . $cls . ' associated with Profile:' . $profile->getID());
  153. return $profile->getID();
  154. }
  155. }
  156. }
  157. }
  158. $this->log(LOG_INFO, 'Unable to find a remote profile with URI: ' . $uri);
  159. return null;
  160. }
  161. }