util.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. function mention_url_representative_hcard($url, $fn=null, $mf2=null) {
  3. if(!$mf2) {
  4. $request = HTTPClient::start();
  5. try {
  6. $response = $request->get($url);
  7. } catch(Exception $ex) {
  8. return null;
  9. }
  10. $url = $response->getEffectiveUrl();
  11. $mf2 = new Mf2\Parser($response->getBody(), $url);
  12. $mf2 = $mf2->parse();
  13. }
  14. $hcard = null;
  15. if(!empty($mf2['items'])) {
  16. $hcards = array();
  17. foreach($mf2['items'] as $item) {
  18. if(!in_array('h-card', $item['type'])) {
  19. continue;
  20. }
  21. // We found a match, return it immediately
  22. if(isset($item['properties']['url']) && in_array($url, $item['properties']['url'])) {
  23. $hcard = $item['properties'];
  24. break;
  25. }
  26. // Let's keep all the hcards for later, to return one of them at least
  27. $hcards[] = $item['properties'];
  28. }
  29. // No match immediately for the url we expected, but there were h-cards found
  30. if (count($hcards) > 0) {
  31. $hcard = $hcards[0];
  32. }
  33. }
  34. if(!$hcard && $fn) {
  35. $hcard = array('name' => array($fn));
  36. }
  37. if(!$hcard && $response) {
  38. preg_match('/<title>([^<]+)/', $response->getBody(), $match);
  39. $hcard = array('name' => array($match[1]));
  40. }
  41. if($hcard && !$hcard['url']) {
  42. $hcard['url'] = array($url);
  43. }
  44. return $hcard;
  45. }