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.

790 lines
28 KiB

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