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.

337 lines
9.9 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. <?php
  2. namespace XRay\Formats;
  3. class Mf2 {
  4. public static function parse($mf2, $url, $http) {
  5. if($item = $mf2['items'][0]) {
  6. // If the first item is a feed, the page is a feed
  7. if(in_array('h-feed', $item['type'])) {
  8. return self::parseHFeed($mf2, $http);
  9. }
  10. // Check each top-level h-card, and if there is one that matches this URL, the page is an h-card
  11. foreach($mf2['items'] as $i) {
  12. if(in_array('h-card', $i['type'])
  13. and array_key_exists('url', $i['properties'])
  14. ) {
  15. $urls = $i['properties']['url'];
  16. $urls = array_map('\normalize_url', $urls);
  17. if(in_array($url, $urls)) {
  18. // TODO: check for children h-entrys (like tantek.com), or sibling h-entries (like aaronparecki.com)
  19. // and return the result as a feed instead
  20. return self::parseHCard($i, $http, $url);
  21. }
  22. }
  23. }
  24. // Otherwise check for an h-entry
  25. if(in_array('h-entry', $item['type'])) {
  26. return self::parseHEntry($mf2, $http);
  27. }
  28. }
  29. return false;
  30. }
  31. private static function parseHEntry($mf2, $http) {
  32. $data = [
  33. 'type' => 'entry',
  34. 'author' => [
  35. 'type' => 'card',
  36. 'name' => null,
  37. 'url' => null,
  38. 'photo' => null
  39. ]
  40. ];
  41. $item = $mf2['items'][0];
  42. // Single plaintext values
  43. $properties = ['url','published','summary','rsvp'];
  44. foreach($properties as $p) {
  45. if($v = self::getPlaintext($item, $p))
  46. $data[$p] = $v;
  47. }
  48. // Always arrays
  49. $properties = ['photo','video','syndication','in-reply-to','like-of','repost-of','category'];
  50. foreach($properties as $p) {
  51. if(array_key_exists($p, $item['properties'])) {
  52. $data[$p] = [];
  53. foreach($item['properties'][$p] as $v) {
  54. if(is_string($v))
  55. $data[$p][] = $v;
  56. elseif(is_array($v) and array_key_exists('value', $v))
  57. $data[$p][] = $v['value'];
  58. }
  59. }
  60. }
  61. // Determine if the name is distinct from the content
  62. $name = self::getPlaintext($item, 'name');
  63. $content = null;
  64. $textContent = null;
  65. $htmlContent = null;
  66. if(array_key_exists('content', $item['properties'])) {
  67. $content = $item['properties']['content'][0];
  68. if(is_string($content)) {
  69. $textContent = $content;
  70. } elseif(!is_string($content) && is_array($content) && array_key_exists('value', $content)) {
  71. if(array_key_exists('html', $content)) {
  72. $textContent = trim(strip_tags($content['html']));
  73. $htmlContent = trim($content['html']);
  74. } else {
  75. $textContent = trim($content['value']);
  76. }
  77. }
  78. // Trim ellipses from the name
  79. $name = preg_replace('/ ?(\.\.\.|…)$/', '', $name);
  80. // Remove all whitespace when checking equality
  81. $nameCompare = preg_replace('/\s/','',trim($name));
  82. $contentCompare = preg_replace('/\s/','',trim($textContent));
  83. // Check if the name is a prefix of the content
  84. if(strpos($contentCompare, $nameCompare) === 0) {
  85. $name = null;
  86. }
  87. }
  88. if($name) {
  89. $data['name'] = $name;
  90. }
  91. if($content) {
  92. $data['content'] = [
  93. 'text' => $textContent
  94. ];
  95. if($textContent != $htmlContent) {
  96. $data['content']['html'] = $htmlContent;
  97. }
  98. }
  99. $data['author'] = self::findAuthor($mf2, $item, $http);
  100. return $data;
  101. }
  102. private static function parseHFeed($mf2, $http) {
  103. $data = [
  104. 'type' => 'feed',
  105. 'author' => [
  106. 'type' => 'card',
  107. 'name' => null,
  108. 'url' => null,
  109. 'photo' => null
  110. ],
  111. 'items' => [],
  112. 'todo' => 'Not yet implemented. Please see https://github.com/aaronpk/XRay/issues/1'
  113. ];
  114. return $data;
  115. }
  116. private static function parseHCard($item, $http, $authorURL=false) {
  117. $data = [
  118. 'type' => 'card',
  119. 'name' => null,
  120. 'url' => null,
  121. 'photo' => null
  122. ];
  123. $properties = ['url','name','photo'];
  124. foreach($properties as $p) {
  125. if($p == 'url' && $authorURL) {
  126. // If there is a matching author URL, use that one
  127. $found = false;
  128. foreach($item['properties']['url'] as $url) {
  129. $url = \normalize_url($url);
  130. if($url == $authorURL) {
  131. $data['url'] = $url;
  132. $found = true;
  133. }
  134. }
  135. if(!$found) $data['url'] = $item['properties']['url'][0];
  136. } else if($v = self::getPlaintext($item, $p)) {
  137. $data[$p] = $v;
  138. }
  139. }
  140. return $data;
  141. }
  142. private static function findAuthor($mf2, $item, $http) {
  143. $author = [
  144. 'type' => 'card',
  145. 'name' => null,
  146. 'url' => null,
  147. 'photo' => null
  148. ];
  149. // Author Discovery
  150. // http://indiewebcamp.com/authorship
  151. $authorPage = false;
  152. if(array_key_exists('author', $item['properties'])) {
  153. // Check if any of the values of the author property are an h-card
  154. foreach($item['properties']['author'] as $a) {
  155. if(self::isHCard($a)) {
  156. // 5.1 "if it has an h-card, use it, exit."
  157. return self::parseHCard($a, $http);
  158. } elseif(is_string($a)) {
  159. if(self::isURL($a)) {
  160. // 5.2 "otherwise if author property is an http(s) URL, let the author-page have that URL"
  161. $authorPage = $a;
  162. } else {
  163. // 5.3 "otherwise use the author property as the author name, exit"
  164. // We can only set the name, no h-card or URL was found
  165. $author['name'] = self::getPlaintext($item, 'author');
  166. return $author;
  167. }
  168. } else {
  169. // This case is only hit when the author property is an mf2 object that is not an h-card
  170. $author['name'] = self::getPlaintext($item, 'author');
  171. return $author;
  172. }
  173. }
  174. }
  175. // 6. "if no author page was found" ... check for rel-author link
  176. if(!$authorPage) {
  177. if(isset($mf2['rels']) && isset($mf2['rels']['author']))
  178. $authorPage = $mf2['rels']['author'][0];
  179. }
  180. // 7. "if there is an author-page URL" ...
  181. if($authorPage) {
  182. // 7.1 "get the author-page from that URL and parse it for microformats2"
  183. $authorPageContents = self::getURL($authorPage, $http);
  184. if($authorPageContents) {
  185. foreach($authorPageContents['items'] as $i) {
  186. if(self::isHCard($i)) {
  187. // 7.2 "if author-page has 1+ h-card with url == uid == author-page's URL, then use first such h-card, exit."
  188. if(array_key_exists('url', $i['properties'])
  189. and in_array($authorPage, $i['properties']['url'])
  190. and array_key_exists('uid', $i['properties'])
  191. and in_array($authorPage, $i['properties']['uid'])
  192. ) {
  193. return self::parseHCard($i, $http, $authorPage);
  194. }
  195. // 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"
  196. $relMeLinks = (isset($authorPageContents['rels']) && isset($authorPageContents['rels']['me'])) ? $authorPageContents['rels']['me'] : [];
  197. if(count($relMeLinks) > 0
  198. and array_key_exists('url', $i['properties'])
  199. and count(array_intersect($i['properties']['url'], $relMeLinks)) > 0
  200. ) {
  201. return self::parseHCard($i, $http, $authorPage);
  202. }
  203. }
  204. }
  205. }
  206. // 7.4 "if the h-entry's page has 1+ h-card with url == author-page URL, use first such h-card, exit."
  207. foreach($mf2['items'] as $i) {
  208. if(self::isHCard($i)) {
  209. if(array_key_exists('url', $i['properties'])
  210. and in_array($authorPage, $i['properties']['url'])
  211. ) {
  212. return self::parseHCard($i, $http);
  213. }
  214. }
  215. }
  216. }
  217. return $author;
  218. }
  219. private static function responseDisplayText($name, $summary, $content) {
  220. // Build a fake h-entry to pass to the comments parser
  221. $input = [
  222. 'type' => ['h-entry'],
  223. 'properties' => [
  224. 'name' => [trim($name)],
  225. 'summary' => [trim($summary)],
  226. 'content' => [trim($content)]
  227. ]
  228. ];
  229. if(!trim($name))
  230. unset($input['properties']['name']);
  231. if(!trim($summary))
  232. unset($input['properties']['summary']);
  233. $result = \IndieWeb\comments\parse($input, false, 1024, 4);
  234. return [
  235. 'name' => trim($result['name']),
  236. 'content' => $result['text']
  237. ];
  238. }
  239. private static function hasNumericKeys(array $arr) {
  240. foreach($arr as $key=>$val)
  241. if (is_numeric($key))
  242. return true;
  243. return false;
  244. }
  245. private static function isMicroformat($mf) {
  246. return is_array($mf)
  247. and !self::hasNumericKeys($mf)
  248. and !empty($mf['type'])
  249. and isset($mf['properties']);
  250. }
  251. private static function isHCard($mf) {
  252. return is_array($mf)
  253. and !empty($mf['type'])
  254. and is_array($mf['type'])
  255. and in_array('h-card', $mf['type']);
  256. }
  257. private static function isURL($string) {
  258. return preg_match('/^https?:\/\/.+\..+$/', $string);
  259. }
  260. // Given an array of microformats properties and a key name, return the plaintext value
  261. // at that property
  262. // e.g.
  263. // {"properties":{"published":["foo"]}} results in "foo"
  264. private static function getPlaintext($mf2, $k, $fallback=null) {
  265. if(!empty($mf2['properties'][$k]) and is_array($mf2['properties'][$k])) {
  266. // $mf2['properties'][$v] will always be an array since the input was from the mf2 parser
  267. $value = $mf2['properties'][$k][0];
  268. if(is_string($value)) {
  269. return $value;
  270. } elseif(self::isMicroformat($value) && array_key_exists('value', $value)) {
  271. return $value['value'];
  272. }
  273. }
  274. return $fallback;
  275. }
  276. private static function getURL($url, $http) {
  277. if(!$url) return null;
  278. // TODO: consider adding caching here
  279. $result = $http->get($url);
  280. if($result['error'] || !$result['body']) {
  281. return null;
  282. }
  283. return \mf2\Parse($result['body'], $url);
  284. }
  285. }