util.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. defined('GNUSOCIAL') || die();
  17. /**
  18. * Pull (and store) remote profile from the its uri
  19. *
  20. * @param string $uri
  21. * @return null|Ostatus_profile
  22. */
  23. function pullRemoteProfile(string $uri): ?Ostatus_profile
  24. {
  25. $validate = new Validate();
  26. try {
  27. $uri = Discovery::normalize($uri);
  28. } catch (Exception $e) {
  29. return null;
  30. }
  31. try {
  32. if (Discovery::isAcct($uri) && $validate->email(mb_substr($uri, 5))) {
  33. $profile = Ostatus_profile::ensureWebfinger($uri);
  34. } else if ($validate->uri($uri)) {
  35. $profile = Ostatus_profile::ensureProfileURL($uri);
  36. } else {
  37. common_log(LOG_ERR, 'Invalid address format.');
  38. return null;
  39. }
  40. return $profile;
  41. } catch (FeedSubBadURLException $e) {
  42. common_log(LOG_ERR, 'Invalid URL or could not reach server.');
  43. } catch (FeedSubBadResponseException $e) {
  44. common_log(LOG_ERR, 'Cannot read feed; server returned error.');
  45. } catch (FeedSubEmptyException $e) {
  46. common_log(LOG_ERR, 'Cannot read feed; server returned an empty page.');
  47. } catch (FeedSubBadHTMLException $e) {
  48. common_log(LOG_ERR, 'Bad HTML, could not find feed link.');
  49. } catch (FeedSubNoFeedException $e) {
  50. common_log(LOG_ERR, 'Could not find a feed linked from this URL.');
  51. } catch (FeedSubUnrecognizedTypeException $e) {
  52. common_log(LOG_ERR, 'Not a recognized feed type.');
  53. } catch (FeedSubNoHubException $e) {
  54. common_log(LOG_ERR, 'No hub found.');
  55. } catch (Exception $e) {
  56. common_log(LOG_ERR, sprintf('Bad feed URL: %s %s', get_class($e), $e->getMessage()));
  57. }
  58. return null;
  59. }