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.

431 lines
12 KiB

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