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.

326 lines
9.5 KiB

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