<commit_before><?php
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Rocks\TestData;
use Rocks\HTTP;

class Webmention {

  private function _error(ServerRequestInterface $request, ResponseInterface $response, $error, $description=false) {

    $post = $request->getParsedBody();
    if(@$post['via'] == 'browser') {
      $response->getBody()->write(view('webmention-error', [
        'title' => 'Webmention Rocks!',
        'error' => $error,
        'description' => $description
      ]));
      return $response->withStatus(400);
    } else {
      $content = $error."\n";
      if($description)
        $content .= "\n".$description."\n";
      $response->getBody()->write($content);
      return $response->withHeader('Content-Type', 'text/plain')->withStatus(400);
    }
  }

  public function get(ServerRequestInterface $request, ResponseInterface $response, array $args) {
    $num = $args['num'];

    if(!TestData::exists($num)) {
      $response->getBody()->write('Test not found');
      return $response->withHeader('Content-Type', 'text/plain')->withStatus(404);
    }

    $response->getBody()->write(view('webmention', [
      'title' => 'Webmention Rocks!',
      'num' => $args['num']
    ]));

    return $response;    
  }

  public function handle(ServerRequestInterface $request, ResponseInterface $response, array $args) {
    $num = $args['num'];

    if(!TestData::exists($num)) {
      $response->getBody()->write('Test not found');
      return $response->withHeader('Content-Type', 'text/plain')->withStatus(404);
    }

    // Check the content type of the request
    $contentType = $request->getHeaderLine('Content-type');
    if($contentType != 'application/x-www-form-urlencoded') {
      return $this->_error($request, $response, 
        'invalid_content_type', 
        'Content type must be set to application/x-www-form-urlencoded');
    }

    $post = $request->getParsedBody();

    // Validate the syntax of the source URL
    $sourceURL = @$post['source'];
    $response = $this->_validateSourceURL($request, $response, $sourceURL);
    if($response->getStatusCode() == 400)
      return $response;

    // Validate the syntax of the target URL
    $targetURL = @$post['target'];
    $response = $this->_validateTargetURL($request, $response, $targetURL, $num);
    if($response->getStatusCode() == 400)
      return $response;

    // Fetch the source URL, reporting any errors that occurred
    $result = $this->_fetchSourceURL($request, $response, $sourceURL);
    if(!is_array($result) && $result->getStatusCode() == 400)
      return $result;
    $source = $result;

    // Parse the source HTML and check for a link to target
    $response = $this->_verifySourceLinksToTarget($request, $response, $source, $targetURL);
    if($response->getStatusCode() == 400) {
      if($source['code'] == 410 || $source['code'] == 200) {
        // TODO If the source URL returned 410 or 200, and delete any existing comment if we have one

      }
      return $response;
    }

    // Parse the Microformats on the source URL to extract post/author information
    $mf2 = mf2\Parse($source['body'], $source['url']);

    $comment = false;
    if($mf2 && count($mf2['items']) > 0) {
      $http = new HTTP();
      $comment = Rocks\Formats\Mf2::parse($mf2, $source['url'], $http);
    }<commit_msg>less strict check for content type<commit_after><?php
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Rocks\TestData;
use Rocks\HTTP;

class Webmention {

  private function _error(ServerRequestInterface $request, ResponseInterface $response, $error, $description=false) {

    $post = $request->getParsedBody();
    if(@$post['via'] == 'browser') {
      $response->getBody()->write(view('webmention-error', [
        'title' => 'Webmention Rocks!',
        'error' => $error,
        'description' => $description
      ]));
      return $response->withStatus(400);
    } else {
      $content = $error."\n";
      if($description)
        $content .= "\n".$description."\n";
      $response->getBody()->write($content);
      return $response->withHeader('Content-Type', 'text/plain')->withStatus(400);
    }
  }

  public function get(ServerRequestInterface $request, ResponseInterface $response, array $args) {
    $num = $args['num'];

    if(!TestData::exists($num)) {
      $response->getBody()->write('Test not found');
      return $response->withHeader('Content-Type', 'text/plain')->withStatus(404);
    }

    $response->getBody()->write(view('webmention', [
      'title' => 'Webmention Rocks!',
      'num' => $args['num']
    ]));

    return $response;    
  }

  public function handle(ServerRequestInterface $request, ResponseInterface $response, array $args) {
    $num = $args['num'];

    if(!TestData::exists($num)) {
      $response->getBody()->write('Test not found');
      return $response->withHeader('Content-Type', 'text/plain')->withStatus(404);
    }

    // Check the content type of the request
    $contentType = $request->getHeaderLine('Content-type');
    if(strpos($contentType, 'application/x-www-form-urlencoded') === false) {
      return $this->_error($request, $response, 
        'invalid_content_type', 
        'Content type must be set to application/x-www-form-urlencoded');
    }

    $post = $request->getParsedBody();

    $sourceURL = @$post['source'];
    $targetURL = @$post['target'];

    redis()->publish('incoming', json_encode(['source'=>$sourceURL, 'target'=>$targetURL]));

    // Validate the syntax of the source URL
    $response = $this->_validateSourceURL($request, $response, $sourceURL);
    if($response->getStatusCode() == 400)
      return $response;

    // Validate the syntax of the target URL
    $response = $this->_validateTargetURL($request, $response, $targetURL, $num);
    if($response->getStatusCode() == 400)
      return $response;

    // Fetch the source URL, reporting any errors that occurred
    $result = $this->_fetchSourceURL($request, $response, $sourceURL);
    if(!is_array($result) && $result->getStatusCode() == 400)
      return $result;
    $source = $result;

    // Parse the source HTML and check for a link to target
    $response = $this->_verifySourceLinksToTarget($request, $response, $source, $targetURL);
    if($response->getStatusCode() == 400) {
      if($source['code'] == 410 || $source['code'] == 200) {
        // TODO If the source URL returned 410 or 200, and delete any existing comment if we have one

      }
      return $response;
    }

    // Parse the Microformats on the source URL to extract post/author information
    $mf2 = mf2\Parse($source['body'], $source['url']);

    $comment = false;
    if($mf2 && count($mf2['items']) > 0) {
      $http = new HTTP();
      $comment = Rocks\Formats\Mf2::parse($mf2, $source['url'], $http);
    }
{'fraction_non_alphanumeric': 0.17039511911679256, 'fraction_numerical': 0.012492736780941312, 'mean_word_length': 3.5088408644400784, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 76, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6243442', 'n_tokens_mistral': 2115, 'n_tokens_neox': 2019, 'n_words': 473}