TheFreeNetworkModule.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. $perf = common_config('performance', 'high');
  72. if (is_null($profile_id) && !$perf && $this->lrdd) {
  73. // Force lookup with online resources
  74. $profile_id = $this->lookup($uri, $class, true);
  75. }
  76. return is_null($profile_id);
  77. }
  78. /**
  79. * A new remote profile was sucessfully added, delete
  80. * other remotes associated with the same Profile entity.
  81. *
  82. * @param string $class profile class that triggered this event
  83. * @param int $profile_id Profile:id associated with the new remote profile
  84. * @return bool hook flag
  85. */
  86. public function onEndTFNLookup(string $class, int $profile_id): bool
  87. {
  88. foreach ($this->protocols as $p => $cls) {
  89. if ($cls != $class) {
  90. $profile = $cls::getKV('profile_id', $profile_id);
  91. if ($profile instanceof $cls) {
  92. $this->log(LOG_INFO, 'Deleting remote ' . $cls . ' associated with Profile:' . $profile_id);
  93. $i = new $cls();
  94. $i->profile_id = $profile_id;
  95. $i->delete();
  96. break;
  97. }
  98. }
  99. }
  100. return false;
  101. }
  102. /**
  103. * Plugin version information
  104. *
  105. * @param array $versions
  106. * @return bool hook true
  107. */
  108. public function onPluginVersion(array &$versions): bool
  109. {
  110. $versions[] = [
  111. 'name' => 'TheFreeNetwork',
  112. 'version' => self::MODULE_VERSION,
  113. 'author' => 'Bruno Casteleiro',
  114. 'homepage' => 'https://notabug.org/diogo/gnu-social/src/nightly/plugins/TheFreeNetwork',
  115. // TRANS: Module description.
  116. 'rawdescription' => '"Automagically" migrate internal remote profiles between different federation protocols'
  117. ];
  118. return true;
  119. }
  120. /**
  121. * Search remote profile tables to find someone by URI.
  122. * When set to search online, it will grab the remote
  123. * entity's aliases and search with each one.
  124. * The table associated with the class that triggered
  125. * this lookup process will be discarded in the search.
  126. *
  127. * @param string $uri
  128. * @param string $class
  129. * @param bool $online
  130. * @return int|null Profile:id associated with the remote entity found
  131. */
  132. private function lookup(string $uri, string $class, bool $online = false): ?int
  133. {
  134. if ($online) {
  135. $this->log(LOG_INFO, 'Searching with online resources for a remote profile with URI: ' . $uri);
  136. $all_ids = LRDDPlugin::grab_profile_aliases($uri);
  137. } else {
  138. $this->log(LOG_INFO, 'Searching for a remote profile with URI: ' . $uri);
  139. $all_ids = [$uri];
  140. }
  141. if ($all_ids == null) {
  142. $this->log(LOG_INFO, 'Unable to find a remote profile with URI: ' . $uri);
  143. return null;
  144. }
  145. foreach ($this->protocols as $p => $cls) {
  146. if ($cls != $class) {
  147. foreach ($all_ids as $alias) {
  148. $profile = $cls::getKV('uri', $alias);
  149. if ($profile instanceof $cls) {
  150. $this->log(LOG_INFO, 'Found a remote ' . $cls . ' associated with Profile:' . $profile->getID());
  151. return $profile->getID();
  152. }
  153. }
  154. }
  155. }
  156. $this->log(LOG_INFO, 'Unable to find a remote profile with URI: ' . $uri);
  157. return null;
  158. }
  159. }