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.

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