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.

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