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.

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