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.

878 lines
30 KiB

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