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.

310 lines
8.9 KiB

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