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.

939 lines
33 KiB

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