You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

769 lines
27 KiB

8 years ago
  1. <?php
  2. namespace p3k\XRay\Formats;
  3. use HTMLPurifier, HTMLPurifier_Config;
  4. class Mf2 extends Format {
  5. use Mf2Feed;
  6. public static function matches_host($url) {
  7. return true;
  8. }
  9. public static function matches($url) {
  10. return true;
  11. }
  12. public static function parse($mf2, $url, $http, $opts=[]) {
  13. if(count($mf2['items']) == 0)
  14. return false;
  15. // If they are expecting a feed, always return a feed or an error
  16. if(isset($opts['expect']) && $opts['expect'] == 'feed') {
  17. return self::parseAsHFeed($mf2, $http, $url);
  18. }
  19. // If there is only one item on the page, just use that
  20. if(count($mf2['items']) == 1) {
  21. $item = $mf2['items'][0];
  22. if(in_array('h-entry', $item['type']) || in_array('h-cite', $item['type'])) {
  23. #Parse::debug("mf2:0: Recognized $url as an h-entry it is the only item on the page");
  24. return self::parseAsHEntry($mf2, $item, $http, $url);
  25. }
  26. if(in_array('h-event', $item['type'])) {
  27. #Parse::debug("mf2:0: Recognized $url as an h-event it is the only item on the page");
  28. return self::parseAsHEvent($mf2, $item, $http, $url);
  29. }
  30. if(in_array('h-review', $item['type'])) {
  31. #Parse::debug("mf2:0: Recognized $url as an h-review it is the only item on the page");
  32. return self::parseAsHReview($mf2, $item, $http, $url);
  33. }
  34. if(in_array('h-recipe', $item['type'])) {
  35. #Parse::debug("mf2:0: Recognized $url as an h-recipe it is the only item on the page");
  36. return self::parseAsHRecipe($mf2, $item, $http, $url);
  37. }
  38. if(in_array('h-product', $item['type'])) {
  39. #Parse::debug("mf2:0: Recognized $url as an h-product it is the only item on the page");
  40. return self::parseAsHProduct($mf2, $item, $http, $url);
  41. }
  42. if(in_array('h-item', $item['type'])) {
  43. #Parse::debug("mf2:0: Recognized $url as an h-product it is the only item on the page");
  44. return self::parseAsHItem($mf2, $item, $http, $url);
  45. }
  46. if(in_array('h-card', $item['type'])) {
  47. #Parse::debug("mf2:0: Recognized $url as an h-card it is the only item on the page");
  48. return self::parseAsHCard($item, $http, $url, $url);
  49. }
  50. if(in_array('h-feed', $item['type'])) {
  51. #Parse::debug("mf2:0: Recognized $url as an h-feed because it is the only item on the page");
  52. return self::parseAsHFeed($mf2, $http, $url);
  53. }
  54. }
  55. // Check the list of items on the page to see if one matches the URL of the page,
  56. // and treat as a permalink for that object if so.
  57. foreach($mf2['items'] as $item) {
  58. if(array_key_exists('url', $item['properties'])) {
  59. $urls = $item['properties']['url'];
  60. $urls = array_map('\p3k\XRay\normalize_url', $urls);
  61. if(in_array($url, $urls)) {
  62. #Parse::debug("mf2:1: Recognized $url as a permalink because an object on the page matched the URL of the request");
  63. if(in_array('h-card', $item['type'])) {
  64. return self::parseAsHCard($item, $http, $url, $url);
  65. } elseif(in_array('h-entry', $item['type']) || in_array('h-cite', $item['type'])) {
  66. return self::parseAsHEntry($mf2, $item, $http, $url);
  67. } elseif(in_array('h-event', $item['type'])) {
  68. return self::parseAsHEvent($mf2, $item, $http, $url);
  69. } elseif(in_array('h-review', $item['type'])) {
  70. return self::parseAsHReview($mf2, $item, $http, $url);
  71. } elseif(in_array('h-recipe', $item['type'])) {
  72. return self::parseAsHRecipe($mf2, $item, $http, $url);
  73. } elseif(in_array('h-product', $item['type'])) {
  74. return self::parseAsHProduct($mf2, $item, $http, $url);
  75. } elseif(in_array('h-item', $item['type'])) {
  76. return self::parseAsHItem($mf2, $item, $http, $url);
  77. } elseif(in_array('h-feed', $item['type'])) {
  78. return self::parseAsHFeed($mf2, $http, $url);
  79. } else {
  80. #Parse::debug('This object was not a recognized type.');
  81. return false;
  82. }
  83. }
  84. }
  85. }
  86. // Check for an h-card matching rel=author or the author URL of any h-* on the page,
  87. // and return the h-* object if so
  88. if(isset($mf2['rels']['author'])) {
  89. foreach($mf2['items'] as $card) {
  90. if(in_array('h-card', $card['type']) && array_key_exists('url', $card['properties'])) {
  91. $urls = \p3k\XRay\normalize_urls($card['properties']['url']);
  92. if(count(array_intersect($urls, \p3k\XRay\normalize_urls($mf2['rels']['author']))) > 0) {
  93. // There is an author h-card on this page
  94. // Now look for the first h-* object other than an h-card and use that as the object
  95. foreach($mf2['items'] as $item) {
  96. if(!in_array('h-card', $item['type'])) {
  97. if(in_array('h-entry', $item['type']) || in_array('h-cite', $item['type'])) {
  98. return self::parseAsHEntry($mf2, $item, $http, $url);
  99. } elseif(in_array('h-event', $item['type'])) {
  100. return self::parseAsHEvent($mf2, $item, $http, $url);
  101. } elseif(in_array('h-review', $item['type'])) {
  102. return self::parseAsHReview($mf2, $item, $http, $url);
  103. } elseif(in_array('h-recipe', $item['type'])) {
  104. return self::parseAsHRecipe($mf2, $item, $http, $url);
  105. } elseif(in_array('h-product', $item['type'])) {
  106. return self::parseAsHProduct($mf2, $item, $http, $url);
  107. } elseif(in_array('h-item', $item['type'])) {
  108. return self::parseAsHItem($mf2, $item, $http, $url);
  109. }
  110. }
  111. }
  112. }
  113. }
  114. }
  115. }
  116. // If there was more than one h-entry on the page, treat the whole page as a feed
  117. if(count($mf2['items']) > 1) {
  118. if(count(array_filter($mf2['items'], function($item){
  119. return in_array('h-entry', $item['type']);
  120. })) > 1) {
  121. #Parse::debug("mf2:2: Recognized $url as an h-feed because there are more than one object on the page");
  122. return self::parseAsHFeed($mf2, $http, $url);
  123. }
  124. }
  125. // If the first item is an h-feed, parse as a feed
  126. $first = $mf2['items'][0];
  127. if(in_array('h-feed', $first['type'])) {
  128. #Parse::debug("mf2:3: Recognized $url as an h-feed because the first item is an h-feed");
  129. return self::parseAsHFeed($mf2, $http, $url);
  130. }
  131. // Fallback case, but hopefully we have found something before this point
  132. foreach($mf2['items'] as $item) {
  133. // Otherwise check for a recognized h-* object
  134. if(in_array('h-entry', $item['type']) || in_array('h-cite', $item['type'])) {
  135. #Parse::debug("mf2:6: $url is falling back to the first h-entry on the page");
  136. return self::parseAsHEntry($mf2, $item, $http, $url);
  137. } elseif(in_array('h-event', $item['type'])) {
  138. #Parse::debug("mf2:6: $url is falling back to the first h-event on the page");
  139. return self::parseAsHEvent($mf2, $item, $http, $url);
  140. } elseif(in_array('h-review', $item['type'])) {
  141. #Parse::debug("mf2:6: $url is falling back to the first h-review on the page");
  142. return self::parseAsHReview($mf2, $item, $http, $url);
  143. } elseif(in_array('h-recipe', $item['type'])) {
  144. #Parse::debug("mf2:6: $url is falling back to the first h-recipe on the page");
  145. return self::parseAsHRecipe($mf2, $item, $http, $url);
  146. } elseif(in_array('h-product', $item['type'])) {
  147. #Parse::debug("mf2:6: $url is falling back to the first h-product on the page");
  148. return self::parseAsHProduct($mf2, $item, $http, $url);
  149. } elseif(in_array('h-item', $item['type'])) {
  150. #Parse::debug("mf2:6: $url is falling back to the first h-item on the page");
  151. return self::parseAsHItem($mf2, $item, $http, $url);
  152. }
  153. }
  154. #Parse::debug("mf2:E: No object at $url was recognized");
  155. return false;
  156. }
  157. private static function collectSingleValues($properties, $urlProperties, $item, $url, &$data) {
  158. foreach($properties as $p) {
  159. if(($v = self::getPlaintext($item, $p)) !== null) {
  160. $data[$p] = $v;
  161. }
  162. }
  163. foreach($urlProperties as $p) {
  164. if($p == 'url') {
  165. // Special handling for the 'url' property to prioritize finding the URL on the same domain
  166. if($values = self::getPlaintextValues($item, 'url')) {
  167. if(count($values) == 1) {
  168. if(self::isURL($values[0]))
  169. $data['url'] = $values[0];
  170. }
  171. else {
  172. $set = false;
  173. foreach($values as $v) {
  174. if(self::isURL($v) && parse_url($v, PHP_URL_HOST) == parse_url($url, PHP_URL_HOST)) {
  175. $set = true;
  176. $data['url'] = $v;
  177. }
  178. }
  179. if(!$set) {
  180. // Fall back to the first URL if there isn't one on the domain
  181. if(self::isURL($values[0]))
  182. $data['url'] = $values[0];
  183. }
  184. }
  185. }
  186. } else {
  187. if(($v = self::getPlaintext($item, $p)) !== null) {
  188. if(self::isURL($v))
  189. $data[$p] = $v;
  190. }
  191. }
  192. }
  193. }
  194. private static function parseHTMLValue($property, $item) {
  195. if(!array_key_exists($property, $item['properties']))
  196. return null;
  197. $textContent = false;
  198. $htmlContent = false;
  199. $content = $item['properties'][$property][0];
  200. if(is_string($content)) {
  201. $textContent = $content;
  202. } elseif(!is_string($content) && is_array($content) && array_key_exists('value', $content)) {
  203. if(array_key_exists('html', $content)) {
  204. $htmlContent = trim(self::sanitizeHTML($content['html']));
  205. #$textContent = trim(str_replace("&#xD;","\r",strip_tags($htmlContent)));
  206. $textContent = trim(str_replace("&#xD;","\r",$content['value']));
  207. } else {
  208. $textContent = trim($content['value']);
  209. }
  210. }
  211. $data = [
  212. 'text' => $textContent
  213. ];
  214. if($htmlContent && $textContent != $htmlContent) {
  215. $data['html'] = $htmlContent;
  216. }
  217. return $data;
  218. }
  219. // Always return arrays, and may contain plaintext content
  220. // Nested objects are added to refs and the URL is used as the value if present
  221. private static function collectArrayValues($properties, $item, &$data, &$refs, &$http) {
  222. foreach($properties as $p) {
  223. if(array_key_exists($p, $item['properties'])) {
  224. foreach($item['properties'][$p] as $v) {
  225. if(is_string($v)) {
  226. if(!array_key_exists($p, $data)) $data[$p] = [];
  227. if(!in_array($v, $data[$p]))
  228. $data[$p][] = $v;
  229. } elseif(self::isMicroformat($v)) {
  230. if(($u=self::getPlaintext($v, 'url')) && self::isURL($u)) {
  231. if(!array_key_exists($p, $data)) $data[$p] = [];
  232. if(!in_array($u, $data[$p]))
  233. $data[$p][] = $u;
  234. $ref = self::parse(['items'=>[$v]], $u, $http);
  235. if($ref) {
  236. $refs[$u] = $ref['data'];
  237. }
  238. } else {
  239. if(!array_key_exists($p, $data)) $data[$p] = [];
  240. if(!in_array($v['value'], $data[$p]))
  241. $data[$p][] = $v['value'];
  242. }
  243. }
  244. }
  245. }
  246. }
  247. }
  248. private static function parseEmbeddedHCard($property, $item, &$http) {
  249. if(array_key_exists($property, $item['properties'])) {
  250. $mf2 = $item['properties'][$property][0];
  251. if(is_string($mf2) && self::isURL($mf2)) {
  252. $hcard = [
  253. 'type' => 'card',
  254. 'url' => $mf2
  255. ];
  256. return $hcard;
  257. } if(self::isMicroformat($mf2) && in_array('h-card', $mf2['type'])) {
  258. $hcard = [
  259. 'type' => 'card',
  260. ];
  261. $properties = ['name','latitude','longitude','locality','region','country','url'];
  262. foreach($properties as $p) {
  263. if($v=self::getPlaintext($mf2, $p)) {
  264. $hcard[$p] = $v;
  265. }
  266. }
  267. return $hcard;
  268. }
  269. }
  270. return false;
  271. }
  272. private static function collectArrayURLValues($properties, $item, &$data, &$refs, &$http) {
  273. foreach($properties as $p) {
  274. if(array_key_exists($p, $item['properties'])) {
  275. foreach($item['properties'][$p] as $v) {
  276. if(is_string($v) && self::isURL($v)) {
  277. if(!array_key_exists($p, $data)) $data[$p] = [];
  278. $data[$p][] = $v;
  279. }
  280. elseif(self::isMicroformat($v) && ($u=self::getPlaintext($v, 'url')) && self::isURL($u)) {
  281. if(!array_key_exists($p, $data)) $data[$p] = [];
  282. $data[$p][] = $u;
  283. // parse the object and put the result in the "refs" object
  284. $ref = self::parse(['items'=>[$v]], $u, $http);
  285. if($ref) {
  286. $refs[$u] = $ref['data'];
  287. }
  288. }
  289. }
  290. }
  291. }
  292. }
  293. private static function determineNameAndContent($item, &$data) {
  294. // Determine if the name is distinct from the content
  295. $name = self::getPlaintext($item, 'name');
  296. $textContent = null;
  297. $htmlContent = null;
  298. $content = self::parseHTMLValue('content', $item);
  299. if($content) {
  300. $htmlContent = array_key_exists('html', $content) ? $content['html'] : null;
  301. $textContent = array_key_exists('text', $content) ? $content['text'] : null;
  302. }
  303. if($content) {
  304. // Trim ellipses from the name
  305. $name = preg_replace('/ ?(\.\.\.|…)$/', '', $name);
  306. // Remove all whitespace when checking equality
  307. $nameCompare = preg_replace('/\s/','',trim($name));
  308. $contentCompare = preg_replace('/\s/','',trim($textContent));
  309. // Check if the name is a prefix of the content
  310. if($contentCompare && $nameCompare && strpos($contentCompare, $nameCompare) === 0) {
  311. $name = null;
  312. }
  313. }
  314. if($name) {
  315. $data['name'] = $name;
  316. }
  317. // If there is content, always return the plaintext content, and return HTML content if it's different
  318. if($content) {
  319. $data['content']['text'] = $content['text'];
  320. if(array_key_exists('html', $content))
  321. $data['content']['html'] = $content['html'];
  322. }
  323. }
  324. private static function parseAsHEntry($mf2, $item, $http, $url) {
  325. $data = [
  326. 'type' => 'entry'
  327. ];
  328. $refs = [];
  329. // Single plaintext and URL values
  330. self::collectSingleValues(['published','summary','rsvp','swarm-coins'], ['url'], $item, $url, $data);
  331. if(isset($data['rsvp']))
  332. $data['rsvp'] = strtolower($data['rsvp']);
  333. // These properties are always returned as arrays and may contain plaintext content
  334. // First strip leading hashtags from category values if present
  335. if(array_key_exists('category', $item['properties'])) {
  336. foreach($item['properties']['category'] as $i=>$c) {
  337. if(is_string($c))
  338. $item['properties']['category'][$i] = ltrim($c, '#');
  339. }
  340. }
  341. self::collectArrayValues(['category','invitee'], $item, $data, $refs, $http);
  342. // These properties are always returned as arrays and always URLs
  343. // If the value is an h-* object with a URL, the URL is used and a "ref" is added as well
  344. self::collectArrayURLValues(['photo','video','audio','syndication','in-reply-to','like-of','repost-of','bookmark-of'], $item, $data, $refs, $http);
  345. self::determineNameAndContent($item, $data);
  346. if($author = self::findAuthor($mf2, $item, $http, $url))
  347. $data['author'] = $author;
  348. if($checkin = self::parseEmbeddedHCard('checkin', $item, $http))
  349. $data['checkin'] = $checkin;
  350. $response = [
  351. 'data' => $data
  352. ];
  353. if(count($refs)) {
  354. $response['data']['refs'] = $refs;
  355. }
  356. return $response;
  357. }
  358. private static function parseAsHReview($mf2, $item, $http, $url) {
  359. $data = [
  360. 'type' => 'review'
  361. ];
  362. $refs = [];
  363. self::collectSingleValues(['summary','published','rating','best','worst'], ['url'], $item, $url, $data);
  364. // Fallback for Mf1 "description" as content. The PHP parser does not properly map this to "content"
  365. $description = self::parseHTMLValue('description', $item);
  366. if($description) {
  367. $data['content'] = $description;
  368. }
  369. self::collectArrayValues(['category'], $item, $data, $refs, $http);
  370. self::collectArrayURLValues(['item'], $item, $data, $refs, $http);
  371. self::determineNameAndContent($item, $data);
  372. if($author = self::findAuthor($mf2, $item, $http, $url))
  373. $data['author'] = $author;
  374. $response = [
  375. 'data' => $data
  376. ];
  377. if(count($refs)) {
  378. $response['data']['refs'] = $refs;
  379. }
  380. return $response;
  381. }
  382. private static function parseAsHRecipe($mf2, $item, $http, $url) {
  383. $data = [
  384. 'type' => 'recipe'
  385. ];
  386. $refs = [];
  387. self::collectSingleValues(['name','summary','published','duration','yield','nutrition'], ['url'], $item, $url, $data);
  388. $instructions = self::parseHTMLValue('instructions', $item);
  389. if($instructions) {
  390. $data['instructions'] = $instructions;
  391. }
  392. self::collectArrayValues(['category','ingredient'], $item, $data, $refs, $http);
  393. self::collectArrayURLValues(['photo'], $item, $data, $refs, $http);
  394. if($author = self::findAuthor($mf2, $item, $http, $url))
  395. $data['author'] = $author;
  396. $response = [
  397. 'data' => $data
  398. ];
  399. if(count($refs)) {
  400. $response['data']['refs'] = $refs;
  401. }
  402. return $response;
  403. }
  404. private static function parseAsHProduct($mf2, $item, $http, $url) {
  405. $data = [
  406. 'type' => 'product'
  407. ];
  408. self::collectSingleValues(['name','identifier','price'], ['url'], $item, $url, $data);
  409. $description = self::parseHTMLValue('description', $item);
  410. if($description) {
  411. $data['description'] = $description;
  412. }
  413. self::collectArrayValues(['category','brand'], $item, $data, $refs, $http);
  414. self::collectArrayURLValues(['photo','video','audio'], $item, $data, $refs, $http);
  415. $response = [
  416. 'data' => $data
  417. ];
  418. if(count($refs)) {
  419. $response['data']['refs'] = $refs;
  420. }
  421. return $response;
  422. }
  423. private static function parseAsHItem($mf2, $item, $http, $url) {
  424. $data = [
  425. 'type' => 'item'
  426. ];
  427. self::collectSingleValues(['name'], ['url'], $item, $url, $data);
  428. self::collectArrayURLValues(['photo','video','audio'], $item, $data, $refs, $http);
  429. $response = [
  430. 'data' => $data
  431. ];
  432. if(count($refs)) {
  433. $response['data']['refs'] = $refs;
  434. }
  435. return $response;
  436. }
  437. private static function parseAsHEvent($mf2, $item, $http, $url) {
  438. $data = [
  439. 'type' => 'event'
  440. ];
  441. $refs = [];
  442. // Single plaintext and URL values
  443. self::collectSingleValues(['name','summary','published','start','end','duration'], ['url'], $item, $url, $data);
  444. // These properties are always returned as arrays and may contain plaintext content
  445. self::collectArrayValues(['category','location','attendee'], $item, $data, $refs, $http);
  446. // These properties are always returned as arrays and always URLs
  447. // If the value is an h-* object with a URL, the URL is used and a "ref" is added as well
  448. self::collectArrayURLValues(['photo','video','audio','syndication'], $item, $data, $refs, $http);
  449. // If there is a description, always return the plaintext description, and return HTML description if it's different
  450. $description = self::parseHTMLValue('description', $item);
  451. if($description) {
  452. $data['description'] = $description;
  453. }
  454. $response = [
  455. 'data' => $data
  456. ];
  457. if(count($refs)) {
  458. $response['data']['refs'] = $refs;
  459. }
  460. return $response;
  461. }
  462. private static function parseAsHCard($item, $http, $url, $authorURL=false) {
  463. $data = [
  464. 'type' => 'card',
  465. 'name' => null,
  466. 'url' => null,
  467. 'photo' => null
  468. ];
  469. $properties = ['url','name','photo'];
  470. foreach($properties as $p) {
  471. if($p == 'url' && $authorURL) {
  472. // If there is a matching author URL, use that one
  473. $found = false;
  474. foreach($item['properties']['url'] as $url) {
  475. if(self::isURL($url)) {
  476. $url = \p3k\XRay\normalize_url($url);
  477. if($url == \p3k\XRay\normalize_url($authorURL)) {
  478. $data['url'] = $url;
  479. $found = true;
  480. }
  481. }
  482. }
  483. if(!$found && self::isURL($item['properties']['url'][0])) {
  484. $data['url'] = $item['properties']['url'][0];
  485. }
  486. } else if(($v = self::getPlaintext($item, $p)) !== null) {
  487. // Make sure the URL property is actually a URL
  488. if($p == 'url' || $p == 'photo') {
  489. if(self::isURL($v))
  490. $data[$p] = $v;
  491. } else {
  492. $data[$p] = $v;
  493. }
  494. }
  495. }
  496. // If no URL property was found, use the $authorURL provided
  497. if(!$data['url'])
  498. $data['url'] = $authorURL;
  499. $response = [
  500. 'data' => $data
  501. ];
  502. return $response;
  503. }
  504. private static function findAuthor($mf2, $item, $http, $url) {
  505. $author = [
  506. 'type' => 'card',
  507. 'name' => null,
  508. 'url' => null,
  509. 'photo' => null
  510. ];
  511. // Author Discovery
  512. // http://indiewebcamp.com/authorship
  513. $authorPage = false;
  514. if(array_key_exists('author', $item['properties'])) {
  515. // Check if any of the values of the author property are an h-card
  516. foreach($item['properties']['author'] as $a) {
  517. if(self::isHCard($a)) {
  518. // 5.1 "if it has an h-card, use it, exit."
  519. return self::parseAsHCard($a, $http, $url)['data'];
  520. } elseif(is_string($a)) {
  521. if(self::isURL($a)) {
  522. // 5.2 "otherwise if author property is an http(s) URL, let the author-page have that URL"
  523. $authorPage = $a;
  524. } else {
  525. // 5.3 "otherwise use the author property as the author name, exit"
  526. // We can only set the name, no h-card or URL was found
  527. $author['name'] = self::getPlaintext($item, 'author');
  528. return $author;
  529. }
  530. } else {
  531. // This case is only hit when the author property is an mf2 object that is not an h-card
  532. $author['name'] = self::getPlaintext($item, 'author');
  533. return $author;
  534. }
  535. }
  536. }
  537. // 6. "if no author page was found" ... check for rel-author link
  538. if(!$authorPage) {
  539. if(isset($mf2['rels']) && isset($mf2['rels']['author']))
  540. $authorPage = $mf2['rels']['author'][0];
  541. }
  542. // 7. "if there is an author-page URL" ...
  543. if($authorPage) {
  544. // 7.1 "get the author-page from that URL and parse it for microformats2"
  545. $authorPageContents = self::getURL($authorPage, $http);
  546. if($authorPageContents) {
  547. foreach($authorPageContents['items'] as $i) {
  548. if(self::isHCard($i)) {
  549. // 7.2 "if author-page has 1+ h-card with url == uid == author-page's URL, then use first such h-card, exit."
  550. if(array_key_exists('url', $i['properties'])
  551. and in_array(\p3k\XRay\normalize_url($authorPage), \p3k\XRay\normalize_urls($i['properties']['url']))
  552. and array_key_exists('uid', $i['properties'])
  553. and in_array(\p3k\XRay\normalize_url($authorPage), \p3k\XRay\normalize_urls($i['properties']['uid']))
  554. ) {
  555. return self::parseAsHCard($i, $http, $url, $authorPage)['data'];
  556. }
  557. // 7.3 "else if author-page has 1+ h-card with url property which matches the href of a rel-me link on the author-page"
  558. $relMeLinks = (isset($authorPageContents['rels']) && isset($authorPageContents['rels']['me'])) ? $authorPageContents['rels']['me'] : [];
  559. if(count($relMeLinks) > 0
  560. and array_key_exists('url', $i['properties'])
  561. and count(array_intersect(\p3k\XRay\normalize_urls($i['properties']['url']), \p3k\XRay\normalize_urls($relMeLinks))) > 0
  562. ) {
  563. return self::parseAsHCard($i, $http, $url, $authorPage)['data'];
  564. }
  565. }
  566. }
  567. }
  568. // 7.4 "if the h-entry's page has 1+ h-card with url == author-page URL, use first such h-card, exit."
  569. foreach($mf2['items'] as $i) {
  570. if(self::isHCard($i)) {
  571. if(array_key_exists('url', $i['properties'])
  572. and in_array(\p3k\XRay\normalize_url($authorPage), \p3k\XRay\normalize_urls($i['properties']['url']))
  573. ) {
  574. return self::parseAsHCard($i, $http, $url)['data'];
  575. }
  576. }
  577. // Also check the "author" property
  578. // (for finding the author of an h-feed's children when the author is the p-author property of the h-feed)
  579. if(isset($i['properties']['author'])) {
  580. foreach($i['properties']['author'] as $ic) {
  581. if(self::isHCard($ic)) {
  582. if(array_key_exists('url', $ic['properties'])
  583. and in_array(\p3k\XRay\normalize_url($authorPage), \p3k\XRay\normalize_urls($ic['properties']['url']))
  584. ) {
  585. return self::parseAsHCard($ic, $http, $url)['data'];
  586. }
  587. }
  588. }
  589. }
  590. }
  591. }
  592. if(!$author['name'] && !$author['photo'] && !$author['url'])
  593. return null;
  594. return $author;
  595. }
  596. private static function hasNumericKeys(array $arr) {
  597. foreach($arr as $key=>$val)
  598. if (is_numeric($key))
  599. return true;
  600. return false;
  601. }
  602. private static function isMicroformat($mf) {
  603. return is_array($mf)
  604. and !self::hasNumericKeys($mf)
  605. and !empty($mf['type'])
  606. and isset($mf['properties']);
  607. }
  608. private static function isHCard($mf) {
  609. return is_array($mf)
  610. and !empty($mf['type'])
  611. and is_array($mf['type'])
  612. and in_array('h-card', $mf['type']);
  613. }
  614. private static function isURL($string) {
  615. return preg_match('/^https?:\/\/.+\..+$/', $string);
  616. }
  617. // Given an array of microformats properties and a key name, return the plaintext value
  618. // at that property
  619. // e.g.
  620. // {"properties":{"published":["foo"]}} results in "foo"
  621. private static function getPlaintext($mf2, $k, $fallback=null) {
  622. if(!empty($mf2['properties'][$k]) and is_array($mf2['properties'][$k])) {
  623. // $mf2['properties'][$v] will always be an array since the input was from the mf2 parser
  624. $value = $mf2['properties'][$k][0];
  625. if(is_string($value)) {
  626. return $value;
  627. } elseif(self::isMicroformat($value) && array_key_exists('value', $value)) {
  628. return $value['value'];
  629. }
  630. }
  631. return $fallback;
  632. }
  633. private static function getPlaintextValues($mf2, $k, $values=[]) {
  634. if(!empty($mf2['properties'][$k]) and is_array($mf2['properties'][$k])) {
  635. foreach($mf2['properties'][$k] as $value) {
  636. if(is_string($value)) {
  637. $values[] = $value;
  638. } elseif(self::isMicroformat($value) && array_key_exists('value', $value)) {
  639. $values[] = $value['value'];
  640. }
  641. }
  642. }
  643. return $values;
  644. }
  645. private static function getURL($url, $http) {
  646. if(!$url || !$http) return null;
  647. // TODO: consider adding caching here
  648. $result = $http->get($url);
  649. if($result['error'] || !$result['body']) {
  650. return null;
  651. }
  652. return \mf2\Parse($result['body'], $url);
  653. }
  654. }