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.

745 lines
25 KiB

6 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. // These properties are always returned as arrays and may contain plaintext content
  302. // First strip leading hashtags from category values if present
  303. if(array_key_exists('category', $item['properties'])) {
  304. foreach($item['properties']['category'] as $i=>$c) {
  305. if(is_string($c))
  306. $item['properties']['category'][$i] = ltrim($c, '#');
  307. }
  308. }
  309. self::collectArrayValues(['category','invitee'], $item, $data, $refs, $http);
  310. // These properties are always returned as arrays and always URLs
  311. // If the value is an h-* object with a URL, the URL is used and a "ref" is added as well
  312. self::collectArrayURLValues(['photo','video','audio','syndication','in-reply-to','like-of','repost-of','bookmark-of'], $item, $data, $refs, $http);
  313. self::determineNameAndContent($item, $data);
  314. if($author = self::findAuthor($mf2, $item, $http))
  315. $data['author'] = $author;
  316. if($checkin = self::parseEmbeddedHCard('checkin', $item, $http))
  317. $data['checkin'] = $checkin;
  318. $response = [
  319. 'data' => $data
  320. ];
  321. if(count($refs)) {
  322. $response['data']['refs'] = $refs;
  323. }
  324. return $response;
  325. }
  326. private static function parseAsHReview($mf2, $item, $http) {
  327. $data = [
  328. 'type' => 'review'
  329. ];
  330. $refs = [];
  331. self::collectSingleValues(['summary','published','rating','best','worst'], ['url'], $item, $data, $http);
  332. // Fallback for Mf1 "description" as content. The PHP parser does not properly map this to "content"
  333. $description = self::parseHTMLValue('description', $item);
  334. if($description) {
  335. $data['content'] = $description;
  336. }
  337. self::collectArrayValues(['category'], $item, $data, $refs, $http);
  338. self::collectArrayURLValues(['item'], $item, $data, $refs, $http);
  339. self::determineNameAndContent($item, $data);
  340. if($author = self::findAuthor($mf2, $item, $http))
  341. $data['author'] = $author;
  342. $response = [
  343. 'data' => $data
  344. ];
  345. if(count($refs)) {
  346. $response['data']['refs'] = $refs;
  347. }
  348. return $response;
  349. }
  350. private static function parseAsHRecipe($mf2, $item, $http) {
  351. $data = [
  352. 'type' => 'recipe'
  353. ];
  354. $refs = [];
  355. self::collectSingleValues(['name','summary','published','duration','yield','nutrition'], ['url'], $item, $data);
  356. $instructions = self::parseHTMLValue('instructions', $item);
  357. if($instructions) {
  358. $data['instructions'] = $instructions;
  359. }
  360. self::collectArrayValues(['category','ingredient'], $item, $data, $refs, $http);
  361. self::collectArrayURLValues(['photo'], $item, $data, $refs, $http);
  362. if($author = self::findAuthor($mf2, $item, $http))
  363. $data['author'] = $author;
  364. $response = [
  365. 'data' => $data
  366. ];
  367. if(count($refs)) {
  368. $response['data']['refs'] = $refs;
  369. }
  370. return $response;
  371. }
  372. private static function parseAsHProduct($mf2, $item, $http) {
  373. $data = [
  374. 'type' => 'product'
  375. ];
  376. self::collectSingleValues(['name','identifier','price'], ['url'], $item, $data, $http);
  377. $description = self::parseHTMLValue('description', $item);
  378. if($description) {
  379. $data['description'] = $description;
  380. }
  381. self::collectArrayValues(['category','brand'], $item, $data, $refs, $http);
  382. self::collectArrayURLValues(['photo','video','audio'], $item, $data, $refs, $http);
  383. $response = [
  384. 'data' => $data
  385. ];
  386. if(count($refs)) {
  387. $response['data']['refs'] = $refs;
  388. }
  389. return $response;
  390. }
  391. private static function parseAsHItem($mf2, $item, $http) {
  392. $data = [
  393. 'type' => 'item'
  394. ];
  395. self::collectSingleValues(['name'], ['url'], $item, $data);
  396. self::collectArrayURLValues(['photo','video','audio'], $item, $data, $refs, $http);
  397. $response = [
  398. 'data' => $data
  399. ];
  400. if(count($refs)) {
  401. $response['data']['refs'] = $refs;
  402. }
  403. return $response;
  404. }
  405. private static function parseAsHEvent($mf2, $item, $http) {
  406. $data = [
  407. 'type' => 'event'
  408. ];
  409. $refs = [];
  410. // Single plaintext and URL values
  411. self::collectSingleValues(['name','summary','published','start','end','duration'], ['url'], $item, $data, $http);
  412. // These properties are always returned as arrays and may contain plaintext content
  413. self::collectArrayValues(['category','location','attendee'], $item, $data, $refs, $http);
  414. // These properties are always returned as arrays and always URLs
  415. // If the value is an h-* object with a URL, the URL is used and a "ref" is added as well
  416. self::collectArrayURLValues(['photo','video','audio','syndication'], $item, $data, $refs, $http);
  417. // If there is a description, always return the plaintext description, and return HTML description if it's different
  418. $textDescription = null;
  419. $htmlDescription = null;
  420. if(array_key_exists('description', $item['properties'])) {
  421. $description = $item['properties']['description'][0];
  422. if(is_string($description)) {
  423. $textDescription = $description;
  424. } elseif(!is_string($description) && is_array($description) && array_key_exists('value', $description)) {
  425. if(array_key_exists('html', $description)) {
  426. $htmlDescription = trim(self::sanitizeHTML($description['html']));
  427. $textDescription = trim(str_replace("&#xD;","\r",strip_tags($htmlDescription)));
  428. $textDescription = trim(str_replace("&#xD;","\r",$description['value']));
  429. } else {
  430. $textDescription = trim($description['value']);
  431. }
  432. }
  433. }
  434. if($textDescription) {
  435. $data['description'] = [
  436. 'text' => $textDescription
  437. ];
  438. if($htmlDescription && $textDescription != $htmlDescription) {
  439. $data['description']['html'] = $htmlDescription;
  440. }
  441. }
  442. $response = [
  443. 'data' => $data
  444. ];
  445. if(count($refs)) {
  446. $response['data']['refs'] = $refs;
  447. }
  448. return $response;
  449. }
  450. private static function parseAsHFeed($mf2, $http) {
  451. $data = [
  452. 'type' => 'feed',
  453. 'author' => [
  454. 'type' => 'card',
  455. 'name' => null,
  456. 'url' => null,
  457. 'photo' => null
  458. ],
  459. 'todo' => 'Not yet implemented. Please see https://github.com/aaronpk/XRay/issues/1'
  460. ];
  461. return [
  462. 'data' => $data,
  463. 'entries' => []
  464. ];
  465. }
  466. private static function parseAsHCard($item, $http, $authorURL=false) {
  467. $data = [
  468. 'type' => 'card',
  469. 'name' => null,
  470. 'url' => null,
  471. 'photo' => null
  472. ];
  473. $properties = ['url','name','photo'];
  474. foreach($properties as $p) {
  475. if($p == 'url' && $authorURL) {
  476. // If there is a matching author URL, use that one
  477. $found = false;
  478. foreach($item['properties']['url'] as $url) {
  479. if(self::isURL($url)) {
  480. $url = \p3k\XRay\normalize_url($url);
  481. if($url == $authorURL) {
  482. $data['url'] = $url;
  483. $found = true;
  484. }
  485. }
  486. }
  487. if(!$found && self::isURL($item['properties']['url'][0])) {
  488. $data['url'] = $item['properties']['url'][0];
  489. }
  490. } else if(($v = self::getPlaintext($item, $p)) !== null) {
  491. // Make sure the URL property is actually a URL
  492. if($p == 'url' || $p == 'photo') {
  493. if(self::isURL($v))
  494. $data[$p] = $v;
  495. } else {
  496. $data[$p] = $v;
  497. }
  498. }
  499. }
  500. // If no URL property was found, use the $authorURL provided
  501. if(!$data['url'])
  502. $data['url'] = $authorURL;
  503. $response = [
  504. 'data' => $data
  505. ];
  506. return $response;
  507. }
  508. private static function findAuthor($mf2, $item, $http) {
  509. $author = [
  510. 'type' => 'card',
  511. 'name' => null,
  512. 'url' => null,
  513. 'photo' => null
  514. ];
  515. // Author Discovery
  516. // http://indiewebcamp.com/authorship
  517. $authorPage = false;
  518. if(array_key_exists('author', $item['properties'])) {
  519. // Check if any of the values of the author property are an h-card
  520. foreach($item['properties']['author'] as $a) {
  521. if(self::isHCard($a)) {
  522. // 5.1 "if it has an h-card, use it, exit."
  523. return self::parseAsHCard($a, $http)['data'];
  524. } elseif(is_string($a)) {
  525. if(self::isURL($a)) {
  526. // 5.2 "otherwise if author property is an http(s) URL, let the author-page have that URL"
  527. $authorPage = $a;
  528. } else {
  529. // 5.3 "otherwise use the author property as the author name, exit"
  530. // We can only set the name, no h-card or URL was found
  531. $author['name'] = self::getPlaintext($item, 'author');
  532. return $author;
  533. }
  534. } else {
  535. // This case is only hit when the author property is an mf2 object that is not an h-card
  536. $author['name'] = self::getPlaintext($item, 'author');
  537. return $author;
  538. }
  539. }
  540. }
  541. // 6. "if no author page was found" ... check for rel-author link
  542. if(!$authorPage) {
  543. if(isset($mf2['rels']) && isset($mf2['rels']['author']))
  544. $authorPage = $mf2['rels']['author'][0];
  545. }
  546. // 7. "if there is an author-page URL" ...
  547. if($authorPage) {
  548. // 7.1 "get the author-page from that URL and parse it for microformats2"
  549. $authorPageContents = self::getURL($authorPage, $http);
  550. if($authorPageContents) {
  551. foreach($authorPageContents['items'] as $i) {
  552. if(self::isHCard($i)) {
  553. // 7.2 "if author-page has 1+ h-card with url == uid == author-page's URL, then use first such h-card, exit."
  554. if(array_key_exists('url', $i['properties'])
  555. and in_array($authorPage, $i['properties']['url'])
  556. and array_key_exists('uid', $i['properties'])
  557. and in_array($authorPage, $i['properties']['uid'])
  558. ) {
  559. return self::parseAsHCard($i, $http, $authorPage)['data'];
  560. }
  561. // 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"
  562. $relMeLinks = (isset($authorPageContents['rels']) && isset($authorPageContents['rels']['me'])) ? $authorPageContents['rels']['me'] : [];
  563. if(count($relMeLinks) > 0
  564. and array_key_exists('url', $i['properties'])
  565. and count(array_intersect($i['properties']['url'], $relMeLinks)) > 0
  566. ) {
  567. return self::parseAsHCard($i, $http, $authorPage)['data'];
  568. }
  569. }
  570. }
  571. }
  572. // 7.4 "if the h-entry's page has 1+ h-card with url == author-page URL, use first such h-card, exit."
  573. foreach($mf2['items'] as $i) {
  574. if(self::isHCard($i)) {
  575. if(array_key_exists('url', $i['properties'])
  576. and in_array($authorPage, $i['properties']['url'])
  577. ) {
  578. return self::parseAsHCard($i, $http)['data'];
  579. }
  580. }
  581. }
  582. }
  583. if(!$author['name'] && !$author['photo'] && !$author['url'])
  584. return null;
  585. return $author;
  586. }
  587. private static function hasNumericKeys(array $arr) {
  588. foreach($arr as $key=>$val)
  589. if (is_numeric($key))
  590. return true;
  591. return false;
  592. }
  593. private static function isMicroformat($mf) {
  594. return is_array($mf)
  595. and !self::hasNumericKeys($mf)
  596. and !empty($mf['type'])
  597. and isset($mf['properties']);
  598. }
  599. private static function isHCard($mf) {
  600. return is_array($mf)
  601. and !empty($mf['type'])
  602. and is_array($mf['type'])
  603. and in_array('h-card', $mf['type']);
  604. }
  605. private static function isURL($string) {
  606. return preg_match('/^https?:\/\/.+\..+$/', $string);
  607. }
  608. // Given an array of microformats properties and a key name, return the plaintext value
  609. // at that property
  610. // e.g.
  611. // {"properties":{"published":["foo"]}} results in "foo"
  612. private static function getPlaintext($mf2, $k, $fallback=null) {
  613. if(!empty($mf2['properties'][$k]) and is_array($mf2['properties'][$k])) {
  614. // $mf2['properties'][$v] will always be an array since the input was from the mf2 parser
  615. $value = $mf2['properties'][$k][0];
  616. if(is_string($value)) {
  617. return $value;
  618. } elseif(self::isMicroformat($value) && array_key_exists('value', $value)) {
  619. return $value['value'];
  620. }
  621. }
  622. return $fallback;
  623. }
  624. private static function getURL($url, $http) {
  625. if(!$url) return null;
  626. // TODO: consider adding caching here
  627. $result = $http->get($url);
  628. if($result['error'] || !$result['body']) {
  629. return null;
  630. }
  631. return \mf2\Parse($result['body'], $url);
  632. }
  633. }