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.

415 lines
12 KiB

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