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.

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