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.

569 lines
18 KiB

8 years ago
8 years ago
8 years ago
  1. <?php
  2. namespace XRay\Formats;
  3. use HTMLPurifier, HTMLPurifier_Config;
  4. use Parse;
  5. class Mf2 {
  6. public static function parse($mf2, $url, $http) {
  7. if(count($mf2['items']) == 0)
  8. return false;
  9. // If there is only one item on the page, just use that
  10. if(count($mf2['items']) == 1) {
  11. $item = $mf2['items'][0];
  12. if(in_array('h-entry', $item['type']) || in_array('h-cite', $item['type'])) {
  13. Parse::debug("mf2:0: Recognized $url as an h-entry it is the only item on the page");
  14. return self::parseAsHEntry($mf2, $item, $http, $url);
  15. }
  16. if(in_array('h-event', $item['type'])) {
  17. Parse::debug("mf2:0: Recognized $url as an h-event it is the only item on the page");
  18. return self::parseAsHEvent($mf2, $item, $http, $url);
  19. }
  20. }
  21. // Check if the list of items is a bunch of h-entrys and return as a feed
  22. // Unless this page's URL matches one of the entries, then treat it as a permalink
  23. $hentrys = 0;
  24. $lastSeenEntry = false;
  25. foreach($mf2['items'] as $item) {
  26. if(in_array('h-entry', $item['type']) || in_array('h-cite', $item['type'])) {
  27. if(array_key_exists('url', $item['properties'])) {
  28. $urls = $item['properties']['url'];
  29. $urls = array_map('\normalize_url', $urls);
  30. if(in_array($url, $urls)) {
  31. Parse::debug("mf2:1: Recognized $url as an h-entry because an h-entry on the page matched the URL of the request");
  32. return self::parseAsHEntry($mf2, $item, $http, $url);
  33. }
  34. $lastSeenEntry = $item;
  35. }
  36. $hentrys++;
  37. }
  38. }
  39. // If there was more than one h-entry on the page, treat the whole page as a feed
  40. if($hentrys > 1) {
  41. Parse::debug("mf2:2: Recognized $url as an h-feed because there are more than one h-entry on the page");
  42. return self::parseAsHFeed($mf2, $http);
  43. }
  44. // If the first item is an h-feed, parse as a feed
  45. $first = $mf2['items'][0];
  46. if(in_array('h-feed', $first['type'])) {
  47. Parse::debug("mf2:3: Recognized $url as an h-feed because the first item is an h-feed");
  48. return self::parseAsHFeed($mf2, $http);
  49. }
  50. // Check each top-level h-card and h-event, and if there is one that matches this URL, the page is an h-card
  51. foreach($mf2['items'] as $item) {
  52. if((in_array('h-card', $item['type']) or in_array('h-event', $item['type']))
  53. and array_key_exists('url', $item['properties'])
  54. ) {
  55. $urls = $item['properties']['url'];
  56. $urls = array_map('\normalize_url', $urls);
  57. if(in_array($url, $urls)) {
  58. // TODO: check for children h-entrys (like tantek.com), or sibling h-entries (like aaronparecki.com)
  59. // and return the result as a feed instead
  60. if(in_array('h-card', $item['type'])) {
  61. Parse::debug("mf2:4: Recognized $url as an h-card because an h-card on the page matched the URL of the request");
  62. return self::parseAsHCard($item, $http, $url);
  63. } else {
  64. Parse::debug("mf2:4: Recognized $url as an h-event because an h-event on the page matched the URL of the request");
  65. return self::parseAsHEvent($item, $http, $url);
  66. }
  67. }
  68. }
  69. }
  70. // If there was only one h-entry, but the URL for it is not the same as this page, then treat as a feed
  71. if($hentrys == 1) {
  72. if($lastSeenEntry) {
  73. $urls = $lastSeenEntry['properties']['url'];
  74. $urls = array_map('\normalize_url', $urls);
  75. if(count($urls) && !in_array($url, $urls)) {
  76. Parse::debug("mf2:5: Recognized $url as an h-feed no h-entrys on the page matched the URL of the request");
  77. return self::parseAsHFeed($mf2, $http);
  78. }
  79. }
  80. }
  81. // Fallback case, but hopefully we have found something before this point
  82. foreach($mf2['items'] as $item) {
  83. // Otherwise check for an h-entry
  84. if(in_array('h-entry', $item['type']) || in_array('h-cite', $item['type'])) {
  85. Parse::debug("mf2:6: $url is falling back to the first h-entry on the page");
  86. return self::parseAsHEntry($mf2, $item, $http);
  87. }
  88. }
  89. Parse::debug("mf2:E: No object at $url was recognized");
  90. return false;
  91. }
  92. private static function parseAsHEntry($mf2, $item, $http) {
  93. $data = [
  94. 'type' => 'entry'
  95. ];
  96. $refs = [];
  97. // Single plaintext values
  98. $properties = ['url','published','summary','rsvp'];
  99. foreach($properties as $p) {
  100. if($v = self::getPlaintext($item, $p))
  101. $data[$p] = $v;
  102. }
  103. // Always arrays
  104. $properties = ['photo','video','audio','syndication'];
  105. foreach($properties as $p) {
  106. if(array_key_exists($p, $item['properties'])) {
  107. $data[$p] = [];
  108. foreach($item['properties'][$p] as $v) {
  109. if(is_string($v))
  110. $data[$p][] = $v;
  111. elseif(is_array($v) and array_key_exists('value', $v))
  112. $data[$p][] = $v['value'];
  113. }
  114. }
  115. }
  116. // Always returned as arrays, and may also create external references
  117. $properties = ['in-reply-to','like-of','repost-of','bookmark-of','category','invitee'];
  118. foreach($properties as $p) {
  119. if(array_key_exists($p, $item['properties'])) {
  120. $data[$p] = [];
  121. foreach($item['properties'][$p] as $v) {
  122. if(is_string($v))
  123. $data[$p][] = $v;
  124. elseif(self::isMicroformat($v) && ($u=self::getPlaintext($v, 'url'))) {
  125. $data[$p][] = $u;
  126. // parse the object and put the result in the "refs" object
  127. $ref = self::parse(['items'=>[$v]], $u, $http);
  128. if($ref) {
  129. $refs[$u] = $ref['data'];
  130. }
  131. }
  132. }
  133. }
  134. }
  135. // Determine if the name is distinct from the content
  136. $name = self::getPlaintext($item, 'name');
  137. $content = null;
  138. $textContent = null;
  139. $htmlContent = null;
  140. if(array_key_exists('content', $item['properties'])) {
  141. $content = $item['properties']['content'][0];
  142. if(is_string($content)) {
  143. $textContent = $content;
  144. } elseif(!is_string($content) && is_array($content) && array_key_exists('value', $content)) {
  145. if(array_key_exists('html', $content)) {
  146. $htmlContent = trim(self::sanitizeHTML($content['html']));
  147. $textContent = trim(str_replace("&#xD;","\r",strip_tags($htmlContent)));
  148. $textContent = trim(str_replace("&#xD;","\r",$content['value']));
  149. } else {
  150. $textContent = trim($content['value']);
  151. }
  152. }
  153. // Trim ellipses from the name
  154. $name = preg_replace('/ ?(\.\.\.|…)$/', '', $name);
  155. // Remove all whitespace when checking equality
  156. $nameCompare = preg_replace('/\s/','',trim($name));
  157. $contentCompare = preg_replace('/\s/','',trim($textContent));
  158. // Check if the name is a prefix of the content
  159. if($contentCompare && $nameCompare && strpos($contentCompare, $nameCompare) === 0) {
  160. $name = null;
  161. }
  162. }
  163. if($name) {
  164. $data['name'] = $name;
  165. }
  166. // If there is content, always return the plaintext content, and return HTML content if it's different
  167. if($content) {
  168. $data['content'] = [
  169. 'text' => $textContent
  170. ];
  171. if($htmlContent && $textContent != $htmlContent) {
  172. $data['content']['html'] = $htmlContent;
  173. }
  174. }
  175. if($author = self::findAuthor($mf2, $item, $http))
  176. $data['author'] = $author;
  177. $response = [
  178. 'data' => $data
  179. ];
  180. if(count($refs)) {
  181. $response['refs'] = $refs;
  182. }
  183. return $response;
  184. }
  185. private static function parseAsHEvent($mf2, $item, $http) {
  186. $data = [
  187. 'type' => 'event'
  188. ];
  189. $refs = [];
  190. // Single plaintext values
  191. $properties = ['name','summary','url','published','start','end','duration'];
  192. foreach($properties as $p) {
  193. if($v = self::getPlaintext($item, $p))
  194. $data[$p] = $v;
  195. }
  196. // Always arrays
  197. $properties = ['photo','video','syndication'];
  198. foreach($properties as $p) {
  199. if(array_key_exists($p, $item['properties'])) {
  200. $data[$p] = [];
  201. foreach($item['properties'][$p] as $v) {
  202. if(is_string($v))
  203. $data[$p][] = $v;
  204. elseif(is_array($v) and array_key_exists('value', $v))
  205. $data[$p][] = $v['value'];
  206. }
  207. }
  208. }
  209. // Always returned as arrays, and may also create external references
  210. $properties = ['category','location','attendee'];
  211. foreach($properties as $p) {
  212. if(array_key_exists($p, $item['properties'])) {
  213. $data[$p] = [];
  214. foreach($item['properties'][$p] as $v) {
  215. if(is_string($v))
  216. $data[$p][] = $v;
  217. elseif(self::isMicroformat($v) && ($u=self::getPlaintext($v, 'url'))) {
  218. $data[$p][] = $u;
  219. // parse the object and put the result in the "refs" object
  220. $ref = self::parse(['items'=>[$v]], $u, $http);
  221. if($ref) {
  222. $refs[$u] = $ref['data'];
  223. }
  224. }
  225. }
  226. }
  227. }
  228. // If there is a description, always return the plaintext description, and return HTML description if it's different
  229. $textDescription = null;
  230. $htmlDescription = null;
  231. if(array_key_exists('description', $item['properties'])) {
  232. $description = $item['properties']['description'][0];
  233. if(is_string($description)) {
  234. $textDescription = $description;
  235. } elseif(!is_string($description) && is_array($description) && array_key_exists('value', $description)) {
  236. if(array_key_exists('html', $description)) {
  237. $htmlDescription = trim(self::sanitizeHTML($description['html']));
  238. $textDescription = trim(str_replace("&#xD;","\r",strip_tags($htmlDescription)));
  239. $textDescription = trim(str_replace("&#xD;","\r",$description['value']));
  240. } else {
  241. $textDescription = trim($description['value']);
  242. }
  243. }
  244. }
  245. if($textDescription) {
  246. $data['description'] = [
  247. 'text' => $textDescription
  248. ];
  249. if($htmlDescription && $textDescription != $htmlDescription) {
  250. $data['description']['html'] = $htmlDescription;
  251. }
  252. }
  253. $response = [
  254. 'data' => $data
  255. ];
  256. if(count($refs)) {
  257. $response['refs'] = $refs;
  258. }
  259. return $response;
  260. }
  261. private static function parseAsHFeed($mf2, $http) {
  262. $data = [
  263. 'type' => 'feed',
  264. 'author' => [
  265. 'type' => 'card',
  266. 'name' => null,
  267. 'url' => null,
  268. 'photo' => null
  269. ],
  270. 'todo' => 'Not yet implemented. Please see https://github.com/aaronpk/XRay/issues/1'
  271. ];
  272. return [
  273. 'data' => $data,
  274. 'entries' => []
  275. ];
  276. }
  277. private static function parseAsHCard($item, $http, $authorURL=false) {
  278. $data = [
  279. 'type' => 'card',
  280. 'name' => null,
  281. 'url' => null,
  282. 'photo' => null
  283. ];
  284. $properties = ['url','name','photo'];
  285. foreach($properties as $p) {
  286. if($p == 'url' && $authorURL) {
  287. // If there is a matching author URL, use that one
  288. $found = false;
  289. foreach($item['properties']['url'] as $url) {
  290. $url = \normalize_url($url);
  291. if($url == $authorURL) {
  292. $data['url'] = $url;
  293. $found = true;
  294. }
  295. }
  296. if(!$found) $data['url'] = $item['properties']['url'][0];
  297. } else if($v = self::getPlaintext($item, $p)) {
  298. $data[$p] = $v;
  299. }
  300. }
  301. $response = [
  302. 'data' => $data
  303. ];
  304. return $response;
  305. }
  306. private static function findAuthor($mf2, $item, $http) {
  307. $author = [
  308. 'type' => 'card',
  309. 'name' => null,
  310. 'url' => null,
  311. 'photo' => null
  312. ];
  313. // Author Discovery
  314. // http://indiewebcamp.com/authorship
  315. $authorPage = false;
  316. if(array_key_exists('author', $item['properties'])) {
  317. // Check if any of the values of the author property are an h-card
  318. foreach($item['properties']['author'] as $a) {
  319. if(self::isHCard($a)) {
  320. // 5.1 "if it has an h-card, use it, exit."
  321. return self::parseAsHCard($a, $http)['data'];
  322. } elseif(is_string($a)) {
  323. if(self::isURL($a)) {
  324. // 5.2 "otherwise if author property is an http(s) URL, let the author-page have that URL"
  325. $authorPage = $a;
  326. } else {
  327. // 5.3 "otherwise use the author property as the author name, exit"
  328. // We can only set the name, no h-card or URL was found
  329. $author['name'] = self::getPlaintext($item, 'author');
  330. return $author;
  331. }
  332. } else {
  333. // This case is only hit when the author property is an mf2 object that is not an h-card
  334. $author['name'] = self::getPlaintext($item, 'author');
  335. return $author;
  336. }
  337. }
  338. }
  339. // 6. "if no author page was found" ... check for rel-author link
  340. if(!$authorPage) {
  341. if(isset($mf2['rels']) && isset($mf2['rels']['author']))
  342. $authorPage = $mf2['rels']['author'][0];
  343. }
  344. // 7. "if there is an author-page URL" ...
  345. if($authorPage) {
  346. // 7.1 "get the author-page from that URL and parse it for microformats2"
  347. $authorPageContents = self::getURL($authorPage, $http);
  348. if($authorPageContents) {
  349. foreach($authorPageContents['items'] as $i) {
  350. if(self::isHCard($i)) {
  351. // 7.2 "if author-page has 1+ h-card with url == uid == author-page's URL, then use first such h-card, exit."
  352. if(array_key_exists('url', $i['properties'])
  353. and in_array($authorPage, $i['properties']['url'])
  354. and array_key_exists('uid', $i['properties'])
  355. and in_array($authorPage, $i['properties']['uid'])
  356. ) {
  357. return self::parseAsHCard($i, $http, $authorPage)['data'];
  358. }
  359. // 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"
  360. $relMeLinks = (isset($authorPageContents['rels']) && isset($authorPageContents['rels']['me'])) ? $authorPageContents['rels']['me'] : [];
  361. if(count($relMeLinks) > 0
  362. and array_key_exists('url', $i['properties'])
  363. and count(array_intersect($i['properties']['url'], $relMeLinks)) > 0
  364. ) {
  365. return self::parseAsHCard($i, $http, $authorPage)['data'];
  366. }
  367. }
  368. }
  369. }
  370. // 7.4 "if the h-entry's page has 1+ h-card with url == author-page URL, use first such h-card, exit."
  371. foreach($mf2['items'] as $i) {
  372. if(self::isHCard($i)) {
  373. if(array_key_exists('url', $i['properties'])
  374. and in_array($authorPage, $i['properties']['url'])
  375. ) {
  376. return self::parseAsHCard($i, $http)['data'];
  377. }
  378. }
  379. }
  380. }
  381. if(!$author['name'] && !$author['photo'] && !$author['url'])
  382. return null;
  383. return $author;
  384. }
  385. private static function sanitizeHTML($html) {
  386. $config = HTMLPurifier_Config::createDefault();
  387. $config->set('Cache.DefinitionImpl', null);
  388. $config->set('HTML.AllowedElements', [
  389. 'a',
  390. 'abbr',
  391. 'b',
  392. 'code',
  393. 'del',
  394. 'em',
  395. 'i',
  396. 'img',
  397. 'q',
  398. 'strike',
  399. 'strong',
  400. 'time',
  401. 'blockquote',
  402. 'pre',
  403. 'p',
  404. 'h1',
  405. 'h2',
  406. 'h3',
  407. 'h4',
  408. 'h5',
  409. 'h6',
  410. 'ul',
  411. 'li',
  412. 'ol'
  413. ]);
  414. $def = $config->getHTMLDefinition(true);
  415. $def->addElement(
  416. 'time',
  417. 'Inline',
  418. 'Inline',
  419. 'Common',
  420. [
  421. 'datetime' => 'Text'
  422. ]
  423. );
  424. // Override the allowed classes to only support Microformats2 classes
  425. $def->manager->attrTypes->set('Class', new \HTMLPurifier_AttrDef_HTML_Microformats2());
  426. $purifier = new HTMLPurifier($config);
  427. $sanitized = $purifier->purify($html);
  428. $sanitized = str_replace("&#xD;","\r",$sanitized);
  429. return $sanitized;
  430. }
  431. private static function responseDisplayText($name, $summary, $content) {
  432. // Build a fake h-entry to pass to the comments parser
  433. $input = [
  434. 'type' => ['h-entry'],
  435. 'properties' => [
  436. 'name' => [trim($name)],
  437. 'summary' => [trim($summary)],
  438. 'content' => [trim($content)]
  439. ]
  440. ];
  441. if(!trim($name))
  442. unset($input['properties']['name']);
  443. if(!trim($summary))
  444. unset($input['properties']['summary']);
  445. $result = \IndieWeb\comments\parse($input, false, 1024, 4);
  446. return [
  447. 'name' => trim($result['name']),
  448. 'content' => $result['text']
  449. ];
  450. }
  451. private static function hasNumericKeys(array $arr) {
  452. foreach($arr as $key=>$val)
  453. if (is_numeric($key))
  454. return true;
  455. return false;
  456. }
  457. private static function isMicroformat($mf) {
  458. return is_array($mf)
  459. and !self::hasNumericKeys($mf)
  460. and !empty($mf['type'])
  461. and isset($mf['properties']);
  462. }
  463. private static function isHCard($mf) {
  464. return is_array($mf)
  465. and !empty($mf['type'])
  466. and is_array($mf['type'])
  467. and in_array('h-card', $mf['type']);
  468. }
  469. private static function isURL($string) {
  470. return preg_match('/^https?:\/\/.+\..+$/', $string);
  471. }
  472. // Given an array of microformats properties and a key name, return the plaintext value
  473. // at that property
  474. // e.g.
  475. // {"properties":{"published":["foo"]}} results in "foo"
  476. private static function getPlaintext($mf2, $k, $fallback=null) {
  477. if(!empty($mf2['properties'][$k]) and is_array($mf2['properties'][$k])) {
  478. // $mf2['properties'][$v] will always be an array since the input was from the mf2 parser
  479. $value = $mf2['properties'][$k][0];
  480. if(is_string($value)) {
  481. return $value;
  482. } elseif(self::isMicroformat($value) && array_key_exists('value', $value)) {
  483. return $value['value'];
  484. }
  485. }
  486. return $fallback;
  487. }
  488. private static function getURL($url, $http) {
  489. if(!$url) return null;
  490. // TODO: consider adding caching here
  491. $result = $http->get($url);
  492. if($result['error'] || !$result['body']) {
  493. return null;
  494. }
  495. return \mf2\Parse($result['body'], $url);
  496. }
  497. }