Browse Source

support parsing posted HTML

pull/39/head
Aaron Parecki 5 years ago
parent
commit
c4b80506da
3 changed files with 35 additions and 26 deletions
  1. +31
    -25
      controllers/Parse.php
  2. +3
    -1
      lib/Formats/Mf2.php
  3. +1
    -0
      public/index.php

+ 31
- 25
controllers/Parse.php View File

@ -45,41 +45,47 @@ class Parse {
}
$url = $request->get('url');
$html = $request->get('html');
if(!$url) {
if(!$url && !$html) {
return $this->respond($response, 400, [
'error' => 'missing_url',
'error_description' => 'Provide a URL to fetch'
'error_description' => 'Provide a URL or HTML to fetch'
]);
}
// Attempt some basic URL validation
$scheme = parse_url($url, PHP_URL_SCHEME);
if(!in_array($scheme, ['http','https'])) {
return $this->respond($response, 400, [
'error' => 'invalid_url',
'error_description' => 'Only http and https URLs are supported'
]);
}
if($html) {
// If HTML is provided in the request, parse that, and use the URL provided as the base URL for mf2 resolving
$result['body'] = $html;
} else {
// Attempt some basic URL validation
$scheme = parse_url($url, PHP_URL_SCHEME);
if(!in_array($scheme, ['http','https'])) {
return $this->respond($response, 400, [
'error' => 'invalid_url',
'error_description' => 'Only http and https URLs are supported'
]);
}
$host = parse_url($url, PHP_URL_HOST);
if(!$host) {
return $this->respond($response, 400, [
'error' => 'invalid_url',
'error_description' => 'The URL provided was not valid'
]);
}
$host = parse_url($url, PHP_URL_HOST);
if(!$host) {
return $this->respond($response, 400, [
'error' => 'invalid_url',
'error_description' => 'The URL provided was not valid'
]);
}
$url = \normalize_url($url);
$url = \normalize_url($url);
// Now fetch the URL and check for any curl errors
$result = $this->http->get($url);
// Now fetch the URL and check for any curl errors
$result = $this->http->get($url);
if($result['error']) {
return $this->respond($response, 400, [
'error' => $result['error'],
'error_description' => $result['error_description']
]);
if($result['error']) {
return $this->respond($response, 400, [
'error' => $result['error'],
'error_description' => $result['error_description']
]);
}
}
// attempt to parse the page as HTML

+ 3
- 1
lib/Formats/Mf2.php View File

@ -78,7 +78,7 @@ class Mf2 {
}
// Always returned as arrays, and may also create external references
$properties = ['in-reply-to','like-of','repost-of','bookmark-of','category'];
$properties = ['in-reply-to','like-of','repost-of','bookmark-of','category','invitee'];
foreach($properties as $p) {
if(array_key_exists($p, $item['properties'])) {
$data[$p] = [];
@ -132,6 +132,8 @@ class Mf2 {
if($name) {
$data['name'] = $name;
}
// If there is content, always return the plaintext content, and return HTML content if it's different
if($content) {
$data['content'] = [
'text' => $textContent

+ 1
- 0
public/index.php View File

@ -10,6 +10,7 @@ $templates = new League\Plates\Engine(dirname(__FILE__).'/../views');
$router->addRoute('GET', '/', 'Main::index');
$router->addRoute('GET', '/parse', 'Parse::parse');
$router->addRoute('POST', '/parse', 'Parse::parse');
$dispatcher = $router->getDispatcher();
$request = Request::createFromGlobals();

Loading…
Cancel
Save