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.

748 lines
25 KiB

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