LRDDPlugin.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /*
  3. * GNU Social - a federating social network
  4. * Copyright (C) 2013, Free Software Foundation, Inc.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * Implements Link-based Resource Descriptor Discovery based on RFC6415,
  21. * Web Host Metadata, i.e. the predecessor to WebFinger resource discovery.
  22. *
  23. * @package GNUsocial
  24. * @author Mikael Nordfeldth <mmn@hethane.se>
  25. */
  26. if (!defined('GNUSOCIAL')) { exit(1); }
  27. include("../plugins/LRDD/lib/discovery.php");
  28. set_include_path(get_include_path() . PATH_SEPARATOR . __DIR__ . '/extlib/');
  29. class LRDDPlugin extends Plugin
  30. {
  31. const PLUGIN_VERSION = '2.0.0';
  32. public function onAutoload($cls)
  33. {
  34. switch ($cls) {
  35. case 'XML_XRD':
  36. require_once __DIR__ . '/extlib/XML/XRD.php';
  37. return false;
  38. }
  39. return parent::onAutoload($cls);
  40. }
  41. public function onStartDiscoveryMethodRegistration(Discovery $disco) {
  42. $disco->registerMethod('LRDDMethod_WebFinger');
  43. }
  44. public function onEndDiscoveryMethodRegistration(Discovery $disco) {
  45. $disco->registerMethod('LRDDMethod_HostMeta');
  46. $disco->registerMethod('LRDDMethod_LinkHeader');
  47. $disco->registerMethod('LRDDMethod_LinkHTML');
  48. }
  49. public function onPluginVersion(array &$versions): bool
  50. {
  51. $versions[] = array('name' => 'LRDD',
  52. 'version' => self::PLUGIN_VERSION,
  53. 'author' => 'Mikael Nordfeldth',
  54. 'homepage' => GNUSOCIAL_ENGINE_URL,
  55. // TRANS: Plugin description.
  56. 'rawdescription' => _m('Implements LRDD support for GNU Social.'));
  57. return true;
  58. }
  59. /**
  60. * Fetch all the aliases of some remote profile
  61. *
  62. * @param string $uri profile's URI
  63. * @return array|null aliases
  64. * @throws Exception (If the Discovery's HTTP requests fail)
  65. * @author Bruno Casteleiro <brunoccast@fc.up.pt>
  66. */
  67. public static function grab_profile_aliases(string $uri): ?array
  68. {
  69. $disco = new Discovery();
  70. $xrd = $disco->lookup($uri);
  71. $all_ids = array_merge([$xrd->subject], $xrd->aliases);
  72. if (!in_array($uri, $all_ids)) {
  73. common_log(LOG_INFO, 'The original URI was not listed itself when doing discovery on it!');
  74. return null;
  75. }
  76. return $all_ids;
  77. }
  78. }