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.

806 lines
28 KiB

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