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.

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