LRDDPlugin.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. set_include_path(get_include_path() . PATH_SEPARATOR . __DIR__ . '/extlib/');
  28. class LRDDPlugin extends Plugin
  29. {
  30. const PLUGIN_VERSION = '2.0.0';
  31. public function onAutoload($cls)
  32. {
  33. switch ($cls) {
  34. case 'XML_XRD':
  35. require_once __DIR__ . '/extlib/XML/XRD.php';
  36. return false;
  37. }
  38. return parent::onAutoload($cls);
  39. }
  40. public function onStartDiscoveryMethodRegistration(Discovery $disco) {
  41. $disco->registerMethod('LRDDMethod_WebFinger');
  42. }
  43. public function onEndDiscoveryMethodRegistration(Discovery $disco) {
  44. $disco->registerMethod('LRDDMethod_HostMeta');
  45. $disco->registerMethod('LRDDMethod_LinkHeader');
  46. $disco->registerMethod('LRDDMethod_LinkHTML');
  47. }
  48. public function onPluginVersion(array &$versions): bool
  49. {
  50. $versions[] = array('name' => 'LRDD',
  51. 'version' => self::PLUGIN_VERSION,
  52. 'author' => 'Mikael Nordfeldth',
  53. 'homepage' => GNUSOCIAL_ENGINE_URL,
  54. // TRANS: Plugin description.
  55. 'rawdescription' => _m('Implements LRDD support for GNU Social.'));
  56. return true;
  57. }
  58. /**
  59. * Fetch all the aliases of some remote profile
  60. *
  61. * @param string $uri profile's URI
  62. * @return array|null aliases
  63. * @throws Exception (If the Discovery's HTTP requests fail)
  64. * @author Bruno Casteleiro <brunoccast@fc.up.pt>
  65. */
  66. public static function grab_profile_aliases(string $uri): ?array
  67. {
  68. $disco = new Discovery();
  69. $xrd = $disco->lookup($uri);
  70. $all_ids = array_merge([$xrd->subject], $xrd->aliases);
  71. if (!in_array($uri, $all_ids)) {
  72. common_log(LOG_INFO, 'The original URI was not listed itself when doing discovery on it!');
  73. return null;
  74. }
  75. return $all_ids;
  76. }
  77. }