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.

753 lines
26 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);
  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);
  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);
  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);
  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);
  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);
  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);
  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);
  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);
  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);
  65. } elseif(in_array('h-entry', $item['type']) || in_array('h-cite', $item['type'])) {
  66. return self::parseAsHEntry($mf2, $item, $http);
  67. } elseif(in_array('h-event', $item['type'])) {
  68. return self::parseAsHEvent($mf2, $item, $http);
  69. } elseif(in_array('h-review', $item['type'])) {
  70. return self::parseAsHReview($mf2, $item, $http);
  71. } elseif(in_array('h-recipe', $item['type'])) {
  72. return self::parseAsHRecipe($mf2, $item, $http);
  73. } elseif(in_array('h-product', $item['type'])) {
  74. return self::parseAsHProduct($mf2, $item, $http);
  75. } elseif(in_array('h-item', $item['type'])) {
  76. return self::parseAsHItem($mf2, $item, $http);
  77. } elseif(in_array('h-feed', $item['type'])) {
  78. return self::parseAsHFeed($mf2, $http);
  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);
  99. } elseif(in_array('h-event', $item['type'])) {
  100. return self::parseAsHEvent($mf2, $item, $http);
  101. } elseif(in_array('h-review', $item['type'])) {
  102. return self::parseAsHReview($mf2, $item, $http);
  103. } elseif(in_array('h-recipe', $item['type'])) {
  104. return self::parseAsHRecipe($mf2, $item, $http);
  105. } elseif(in_array('h-product', $item['type'])) {
  106. return self::parseAsHProduct($mf2, $item, $http);
  107. } elseif(in_array('h-item', $item['type'])) {
  108. return self::parseAsHItem($mf2, $item, $http);
  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);
  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);
  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);
  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);
  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);
  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::parseAsHReview($mf2, $item, $http);
  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);
  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);
  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, &$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(($v = self::getPlaintext($item, $p)) !== null) {
  165. if(self::isURL($v))
  166. $data[$p] = $v;
  167. }
  168. }
  169. }
  170. private static function parseHTMLValue($property, $item) {
  171. if(!array_key_exists($property, $item['properties']))
  172. return null;
  173. $textContent = false;
  174. $htmlContent = false;
  175. $content = $item['properties'][$property][0];
  176. if(is_string($content)) {
  177. $textContent = $content;
  178. } elseif(!is_string($content) && is_array($content) && array_key_exists('value', $content)) {
  179. if(array_key_exists('html', $content)) {
  180. $htmlContent = trim(self::sanitizeHTML($content['html']));
  181. #$textContent = trim(str_replace("&#xD;","\r",strip_tags($htmlContent)));
  182. $textContent = trim(str_replace("&#xD;","\r",$content['value']));
  183. } else {
  184. $textContent = trim($content['value']);
  185. }
  186. }
  187. $data = [
  188. 'text' => $textContent
  189. ];
  190. if($htmlContent && $textContent != $htmlContent) {
  191. $data['html'] = $htmlContent;
  192. }
  193. return $data;
  194. }
  195. // Always return arrays, and may contain plaintext content
  196. // Nested objects are added to refs and the URL is used as the value if present
  197. private static function collectArrayValues($properties, $item, &$data, &$refs, &$http) {
  198. foreach($properties as $p) {
  199. if(array_key_exists($p, $item['properties'])) {
  200. foreach($item['properties'][$p] as $v) {
  201. if(is_string($v)) {
  202. if(!array_key_exists($p, $data)) $data[$p] = [];
  203. if(!in_array($v, $data[$p]))
  204. $data[$p][] = $v;
  205. } elseif(self::isMicroformat($v)) {
  206. if(($u=self::getPlaintext($v, 'url')) && self::isURL($u)) {
  207. if(!array_key_exists($p, $data)) $data[$p] = [];
  208. if(!in_array($u, $data[$p]))
  209. $data[$p][] = $u;
  210. $ref = self::parse(['items'=>[$v]], $u, $http);
  211. if($ref) {
  212. $refs[$u] = $ref['data'];
  213. }
  214. } else {
  215. if(!array_key_exists($p, $data)) $data[$p] = [];
  216. if(!in_array($v['value'], $data[$p]))
  217. $data[$p][] = $v['value'];
  218. }
  219. }
  220. }
  221. }
  222. }
  223. }
  224. private static function parseEmbeddedHCard($property, $item, &$http) {
  225. if(array_key_exists($property, $item['properties'])) {
  226. $mf2 = $item['properties'][$property][0];
  227. if(is_string($mf2) && self::isURL($mf2)) {
  228. $hcard = [
  229. 'type' => 'card',
  230. 'url' => $mf2
  231. ];
  232. return $hcard;
  233. } if(self::isMicroformat($mf2) && in_array('h-card', $mf2['type'])) {
  234. $hcard = [
  235. 'type' => 'card',
  236. ];
  237. $properties = ['name','latitude','longitude','locality','region','country','url'];
  238. foreach($properties as $p) {
  239. if($v=self::getPlaintext($mf2, $p)) {
  240. $hcard[$p] = $v;
  241. }
  242. }
  243. return $hcard;
  244. }
  245. }
  246. return false;
  247. }
  248. private static function collectArrayURLValues($properties, $item, &$data, &$refs, &$http) {
  249. foreach($properties as $p) {
  250. if(array_key_exists($p, $item['properties'])) {
  251. foreach($item['properties'][$p] as $v) {
  252. if(is_string($v) && self::isURL($v)) {
  253. if(!array_key_exists($p, $data)) $data[$p] = [];
  254. $data[$p][] = $v;
  255. }
  256. elseif(self::isMicroformat($v) && ($u=self::getPlaintext($v, 'url')) && self::isURL($u)) {
  257. if(!array_key_exists($p, $data)) $data[$p] = [];
  258. $data[$p][] = $u;
  259. // parse the object and put the result in the "refs" object
  260. $ref = self::parse(['items'=>[$v]], $u, $http);
  261. if($ref) {
  262. $refs[$u] = $ref['data'];
  263. }
  264. }
  265. }
  266. }
  267. }
  268. }
  269. private static function determineNameAndContent($item, &$data) {
  270. // Determine if the name is distinct from the content
  271. $name = self::getPlaintext($item, 'name');
  272. $textContent = null;
  273. $htmlContent = null;
  274. $content = self::parseHTMLValue('content', $item);
  275. if($content) {
  276. $htmlContent = array_key_exists('html', $content) ? $content['html'] : null;
  277. $textContent = array_key_exists('text', $content) ? $content['text'] : null;
  278. }
  279. if($content) {
  280. // Trim ellipses from the name
  281. $name = preg_replace('/ ?(\.\.\.|…)$/', '', $name);
  282. // Remove all whitespace when checking equality
  283. $nameCompare = preg_replace('/\s/','',trim($name));
  284. $contentCompare = preg_replace('/\s/','',trim($textContent));
  285. // Check if the name is a prefix of the content
  286. if($contentCompare && $nameCompare && strpos($contentCompare, $nameCompare) === 0) {
  287. $name = null;
  288. }
  289. }
  290. if($name) {
  291. $data['name'] = $name;
  292. }
  293. // If there is content, always return the plaintext content, and return HTML content if it's different
  294. if($content) {
  295. $data['content']['text'] = $content['text'];
  296. if(array_key_exists('html', $content))
  297. $data['content']['html'] = $content['html'];
  298. }
  299. }
  300. private static function parseAsHEntry($mf2, $item, $http) {
  301. $data = [
  302. 'type' => 'entry'
  303. ];
  304. $refs = [];
  305. // Single plaintext and URL values
  306. self::collectSingleValues(['published','summary','rsvp','swarm-coins'], ['url'], $item, $data, $http);
  307. if(isset($data['rsvp']))
  308. $data['rsvp'] = strtolower($data['rsvp']);
  309. // These properties are always returned as arrays and may contain plaintext content
  310. // First strip leading hashtags from category values if present
  311. if(array_key_exists('category', $item['properties'])) {
  312. foreach($item['properties']['category'] as $i=>$c) {
  313. if(is_string($c))
  314. $item['properties']['category'][$i] = ltrim($c, '#');
  315. }
  316. }
  317. self::collectArrayValues(['category','invitee'], $item, $data, $refs, $http);
  318. // These properties are always returned as arrays and always URLs
  319. // If the value is an h-* object with a URL, the URL is used and a "ref" is added as well
  320. self::collectArrayURLValues(['photo','video','audio','syndication','in-reply-to','like-of','repost-of','bookmark-of'], $item, $data, $refs, $http);
  321. self::determineNameAndContent($item, $data);
  322. if($author = self::findAuthor($mf2, $item, $http))
  323. $data['author'] = $author;
  324. if($checkin = self::parseEmbeddedHCard('checkin', $item, $http))
  325. $data['checkin'] = $checkin;
  326. $response = [
  327. 'data' => $data
  328. ];
  329. if(count($refs)) {
  330. $response['data']['refs'] = $refs;
  331. }
  332. return $response;
  333. }
  334. private static function parseAsHReview($mf2, $item, $http) {
  335. $data = [
  336. 'type' => 'review'
  337. ];
  338. $refs = [];
  339. self::collectSingleValues(['summary','published','rating','best','worst'], ['url'], $item, $data, $http);
  340. // Fallback for Mf1 "description" as content. The PHP parser does not properly map this to "content"
  341. $description = self::parseHTMLValue('description', $item);
  342. if($description) {
  343. $data['content'] = $description;
  344. }
  345. self::collectArrayValues(['category'], $item, $data, $refs, $http);
  346. self::collectArrayURLValues(['item'], $item, $data, $refs, $http);
  347. self::determineNameAndContent($item, $data);
  348. if($author = self::findAuthor($mf2, $item, $http))
  349. $data['author'] = $author;
  350. $response = [
  351. 'data' => $data
  352. ];
  353. if(count($refs)) {
  354. $response['data']['refs'] = $refs;
  355. }
  356. return $response;
  357. }
  358. private static function parseAsHRecipe($mf2, $item, $http) {
  359. $data = [
  360. 'type' => 'recipe'
  361. ];
  362. $refs = [];
  363. self::collectSingleValues(['name','summary','published','duration','yield','nutrition'], ['url'], $item, $data);
  364. $instructions = self::parseHTMLValue('instructions', $item);
  365. if($instructions) {
  366. $data['instructions'] = $instructions;
  367. }
  368. self::collectArrayValues(['category','ingredient'], $item, $data, $refs, $http);
  369. self::collectArrayURLValues(['photo'], $item, $data, $refs, $http);
  370. if($author = self::findAuthor($mf2, $item, $http))
  371. $data['author'] = $author;
  372. $response = [
  373. 'data' => $data
  374. ];
  375. if(count($refs)) {
  376. $response['data']['refs'] = $refs;
  377. }
  378. return $response;
  379. }
  380. private static function parseAsHProduct($mf2, $item, $http) {
  381. $data = [
  382. 'type' => 'product'
  383. ];
  384. self::collectSingleValues(['name','identifier','price'], ['url'], $item, $data, $http);
  385. $description = self::parseHTMLValue('description', $item);
  386. if($description) {
  387. $data['description'] = $description;
  388. }
  389. self::collectArrayValues(['category','brand'], $item, $data, $refs, $http);
  390. self::collectArrayURLValues(['photo','video','audio'], $item, $data, $refs, $http);
  391. $response = [
  392. 'data' => $data
  393. ];
  394. if(count($refs)) {
  395. $response['data']['refs'] = $refs;
  396. }
  397. return $response;
  398. }
  399. private static function parseAsHItem($mf2, $item, $http) {
  400. $data = [
  401. 'type' => 'item'
  402. ];
  403. self::collectSingleValues(['name'], ['url'], $item, $data);
  404. self::collectArrayURLValues(['photo','video','audio'], $item, $data, $refs, $http);
  405. $response = [
  406. 'data' => $data
  407. ];
  408. if(count($refs)) {
  409. $response['data']['refs'] = $refs;
  410. }
  411. return $response;
  412. }
  413. private static function parseAsHEvent($mf2, $item, $http) {
  414. $data = [
  415. 'type' => 'event'
  416. ];
  417. $refs = [];
  418. // Single plaintext and URL values
  419. self::collectSingleValues(['name','summary','published','start','end','duration'], ['url'], $item, $data, $http);
  420. // These properties are always returned as arrays and may contain plaintext content
  421. self::collectArrayValues(['category','location','attendee'], $item, $data, $refs, $http);
  422. // These properties are always returned as arrays and always URLs
  423. // If the value is an h-* object with a URL, the URL is used and a "ref" is added as well
  424. self::collectArrayURLValues(['photo','video','audio','syndication'], $item, $data, $refs, $http);
  425. // If there is a description, always return the plaintext description, and return HTML description if it's different
  426. $textDescription = null;
  427. $htmlDescription = null;
  428. if(array_key_exists('description', $item['properties'])) {
  429. $description = $item['properties']['description'][0];
  430. if(is_string($description)) {
  431. $textDescription = $description;
  432. } elseif(!is_string($description) && is_array($description) && array_key_exists('value', $description)) {
  433. if(array_key_exists('html', $description)) {
  434. $htmlDescription = trim(self::sanitizeHTML($description['html']));
  435. $textDescription = trim(str_replace("&#xD;","\r",strip_tags($htmlDescription)));
  436. $textDescription = trim(str_replace("&#xD;","\r",$description['value']));
  437. } else {
  438. $textDescription = trim($description['value']);
  439. }
  440. }
  441. }
  442. if($textDescription) {
  443. $data['description'] = [
  444. 'text' => $textDescription
  445. ];
  446. if($htmlDescription && $textDescription != $htmlDescription) {
  447. $data['description']['html'] = $htmlDescription;
  448. }
  449. }
  450. $response = [
  451. 'data' => $data
  452. ];
  453. if(count($refs)) {
  454. $response['data']['refs'] = $refs;
  455. }
  456. return $response;
  457. }
  458. private static function parseAsHCard($item, $http, $authorURL=false) {
  459. $data = [
  460. 'type' => 'card',
  461. 'name' => null,
  462. 'url' => null,
  463. 'photo' => null
  464. ];
  465. $properties = ['url','name','photo'];
  466. foreach($properties as $p) {
  467. if($p == 'url' && $authorURL) {
  468. // If there is a matching author URL, use that one
  469. $found = false;
  470. foreach($item['properties']['url'] as $url) {
  471. if(self::isURL($url)) {
  472. $url = \p3k\XRay\normalize_url($url);
  473. if($url == \p3k\XRay\normalize_url($authorURL)) {
  474. $data['url'] = $url;
  475. $found = true;
  476. }
  477. }
  478. }
  479. if(!$found && self::isURL($item['properties']['url'][0])) {
  480. $data['url'] = $item['properties']['url'][0];
  481. }
  482. } else if(($v = self::getPlaintext($item, $p)) !== null) {
  483. // Make sure the URL property is actually a URL
  484. if($p == 'url' || $p == 'photo') {
  485. if(self::isURL($v))
  486. $data[$p] = $v;
  487. } else {
  488. $data[$p] = $v;
  489. }
  490. }
  491. }
  492. // If no URL property was found, use the $authorURL provided
  493. if(!$data['url'])
  494. $data['url'] = $authorURL;
  495. $response = [
  496. 'data' => $data
  497. ];
  498. return $response;
  499. }
  500. private static function findAuthor($mf2, $item, $http) {
  501. $author = [
  502. 'type' => 'card',
  503. 'name' => null,
  504. 'url' => null,
  505. 'photo' => null
  506. ];
  507. // Author Discovery
  508. // http://indiewebcamp.com/authorship
  509. $authorPage = false;
  510. if(array_key_exists('author', $item['properties'])) {
  511. // Check if any of the values of the author property are an h-card
  512. foreach($item['properties']['author'] as $a) {
  513. if(self::isHCard($a)) {
  514. // 5.1 "if it has an h-card, use it, exit."
  515. return self::parseAsHCard($a, $http)['data'];
  516. } elseif(is_string($a)) {
  517. if(self::isURL($a)) {
  518. // 5.2 "otherwise if author property is an http(s) URL, let the author-page have that URL"
  519. $authorPage = $a;
  520. } else {
  521. // 5.3 "otherwise use the author property as the author name, exit"
  522. // We can only set the name, no h-card or URL was found
  523. $author['name'] = self::getPlaintext($item, 'author');
  524. return $author;
  525. }
  526. } else {
  527. // This case is only hit when the author property is an mf2 object that is not an h-card
  528. $author['name'] = self::getPlaintext($item, 'author');
  529. return $author;
  530. }
  531. }
  532. }
  533. // 6. "if no author page was found" ... check for rel-author link
  534. if(!$authorPage) {
  535. if(isset($mf2['rels']) && isset($mf2['rels']['author']))
  536. $authorPage = $mf2['rels']['author'][0];
  537. }
  538. // 7. "if there is an author-page URL" ...
  539. if($authorPage) {
  540. // 7.1 "get the author-page from that URL and parse it for microformats2"
  541. $authorPageContents = self::getURL($authorPage, $http);
  542. if($authorPageContents) {
  543. foreach($authorPageContents['items'] as $i) {
  544. if(self::isHCard($i)) {
  545. // 7.2 "if author-page has 1+ h-card with url == uid == author-page's URL, then use first such h-card, exit."
  546. if(array_key_exists('url', $i['properties'])
  547. and in_array(\p3k\XRay\normalize_url($authorPage), \p3k\XRay\normalize_urls($i['properties']['url']))
  548. and array_key_exists('uid', $i['properties'])
  549. and in_array(\p3k\XRay\normalize_url($authorPage), \p3k\XRay\normalize_urls($i['properties']['uid']))
  550. ) {
  551. return self::parseAsHCard($i, $http, $authorPage)['data'];
  552. }
  553. // 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"
  554. $relMeLinks = (isset($authorPageContents['rels']) && isset($authorPageContents['rels']['me'])) ? $authorPageContents['rels']['me'] : [];
  555. if(count($relMeLinks) > 0
  556. and array_key_exists('url', $i['properties'])
  557. and count(array_intersect(\p3k\XRay\normalize_urls($i['properties']['url']), \p3k\XRay\normalize_urls($relMeLinks))) > 0
  558. ) {
  559. return self::parseAsHCard($i, $http, $authorPage)['data'];
  560. }
  561. }
  562. }
  563. }
  564. // 7.4 "if the h-entry's page has 1+ h-card with url == author-page URL, use first such h-card, exit."
  565. foreach($mf2['items'] as $i) {
  566. if(self::isHCard($i)) {
  567. if(array_key_exists('url', $i['properties'])
  568. and in_array(\p3k\XRay\normalize_url($authorPage), \p3k\XRay\normalize_urls($i['properties']['url']))
  569. ) {
  570. return self::parseAsHCard($i, $http)['data'];
  571. }
  572. }
  573. // Also check the "author" property
  574. // (for finding the author of an h-feed's children when the author is the p-author property of the h-feed)
  575. if(isset($i['properties']['author'])) {
  576. foreach($i['properties']['author'] as $ic) {
  577. if(self::isHCard($ic)) {
  578. if(array_key_exists('url', $ic['properties'])
  579. and in_array(\p3k\XRay\normalize_url($authorPage), \p3k\XRay\normalize_urls($ic['properties']['url']))
  580. ) {
  581. return self::parseAsHCard($ic, $http)['data'];
  582. }
  583. }
  584. }
  585. }
  586. }
  587. }
  588. if(!$author['name'] && !$author['photo'] && !$author['url'])
  589. return null;
  590. return $author;
  591. }
  592. private static function hasNumericKeys(array $arr) {
  593. foreach($arr as $key=>$val)
  594. if (is_numeric($key))
  595. return true;
  596. return false;
  597. }
  598. private static function isMicroformat($mf) {
  599. return is_array($mf)
  600. and !self::hasNumericKeys($mf)
  601. and !empty($mf['type'])
  602. and isset($mf['properties']);
  603. }
  604. private static function isHCard($mf) {
  605. return is_array($mf)
  606. and !empty($mf['type'])
  607. and is_array($mf['type'])
  608. and in_array('h-card', $mf['type']);
  609. }
  610. private static function isURL($string) {
  611. return preg_match('/^https?:\/\/.+\..+$/', $string);
  612. }
  613. // Given an array of microformats properties and a key name, return the plaintext value
  614. // at that property
  615. // e.g.
  616. // {"properties":{"published":["foo"]}} results in "foo"
  617. private static function getPlaintext($mf2, $k, $fallback=null) {
  618. if(!empty($mf2['properties'][$k]) and is_array($mf2['properties'][$k])) {
  619. // $mf2['properties'][$v] will always be an array since the input was from the mf2 parser
  620. $value = $mf2['properties'][$k][0];
  621. if(is_string($value)) {
  622. return $value;
  623. } elseif(self::isMicroformat($value) && array_key_exists('value', $value)) {
  624. return $value['value'];
  625. }
  626. }
  627. return $fallback;
  628. }
  629. private static function getURL($url, $http) {
  630. if(!$url || !$http) return null;
  631. // TODO: consider adding caching here
  632. $result = $http->get($url);
  633. if($result['error'] || !$result['body']) {
  634. return null;
  635. }
  636. return \mf2\Parse($result['body'], $url);
  637. }
  638. }