<commit_before><?php

$app->post('/webmention', function() use($app) {

  $req = $app->request();
  $res = $app->response();

  $sourceURL = $req->post('source');
  $targetURL = $req->post('target');

  $error = function($res, $err, $description=false) {
    $res->status(400);
    $res['Content-Type'] = 'application/json';
    $error = array(
      'error' => $err
    );
    if($description)
      $error['error_description'] = $description;
    $res->body(json_encode($error));
  };
  
  $source = parse_url($sourceURL);

  # Verify $source is valid
  if($source == FALSE
    || !array_key_exists('scheme', $source)
    || !in_array($source['scheme'], array('http','https'))
    || !array_key_exists('host', $source)
    || ($source['host'] == gethostbyname($source['host']))
  ) {
    $error($res, 'source_not_found');
    return;
  }

  # Verify $target is actually a resource under our control (home page, individual post)
  $target = parse_url($targetURL);
  # Verify $source is valid
  if($target == FALSE
    || !array_key_exists('scheme', $target)
    || !in_array($target['scheme'], array('http','https'))
    || !array_key_exists('host', $target)
    || $target['host'] != $_SERVER['SERVER_NAME']
  ) {
    $error($res, 'target_not_supported');
    return;
  }

  if(!preg_match('/http:\/\/' . $_SERVER['SERVER_NAME'] . '\/post\/(.+)/', $targetURL, $match)) {
    $error($res, 'target_not_supported', 'The permalink for your post did not match the news.indiewebcamp.com URL convention. Please see news.indiewebcamp.com/constructing-post-urls for more information.');
    return;
  }

  $data = array(
    'domain' => $source['host'],
    'title' => false,
    'body' => false,
    'date' => false
  );
  $notices = array();

  # Now fetch and parse the page looking for Microformats
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $sourceURL);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  $html = curl_exec($ch);

  $parser = new ParserPlus($html);
  $output = $parser->parse();
  $page = new MF2Page($output);

  if($page->hentry) {

    if($page->hentry->property('name'))
      $data['title'] = trim($page->hentry->property('name', true));
    else
      $notices[] = 'No "name" property found on the h-entry. Using the page title instead.';

    if($page->hentry->author && $page->hentry->author->url) {
      $authorURL = parse_url($page->hentry->author->url);
      $data['domain'] = $authorURL['host'];
    } else {
      // $error($res, 'no_author', 'No author was found for the h-entry');
      $notices[] = 'No author URL was found for the h-entry. Using the domain name instead.';
    }

    if($page->hentry->property('content'))
      $data['body'] = strip_tags(trim(implode("\n", $page->hentry->property('content'))));
    else
      $notices[] = 'No post content was found in the h-entry.';

    $entry = $page->hentry;

  } elseif($page->hevent) {

    if($page->hevent->property('name')) {
      $data['title'] = trim($page->hevent->property('name', true));
    }
    if($locations=$page->hevent->location) {
      $location = $locations[0];
      $data['title'] .= ' at ' . $location->property('name', true);
    }

    $entry = $page->hevent;
    
  } else {
    // $error($res, 'no_hentry', 'No h-entry was found on the page');
    $notices[] = 'No h-entry was found on the page. Using the page title for the name' . ($parentID > 0 ? ', and no comment body will be imported.' : '.');
    $entry = false;
  }

  if($page->hentry && ($published=$page->hentry->published)) {
    $data['date'] = $published->format('U');
  } else {
    $notices[] = 'No publish date found.';
  }

  # If no h-entry was found, or if didn't find the title, look at the page title
  if($data['title'] == false) {
    $pageTitle = $parser->xpath('./*/title');
    if($pageTitle->length > 0) {
      foreach($pageTitle as $t) 
        $data['title'] = $t->textContent;<commit_msg>don't add event location name if it's not found<commit_after><?php

$app->post('/webmention', function() use($app) {

  $req = $app->request();
  $res = $app->response();

  $sourceURL = $req->post('source');
  $targetURL = $req->post('target');

  $error = function($res, $err, $description=false) {
    $res->status(400);
    $res['Content-Type'] = 'application/json';
    $error = array(
      'error' => $err
    );
    if($description)
      $error['error_description'] = $description;
    $res->body(json_encode($error));
  };
  
  $source = parse_url($sourceURL);

  # Verify $source is valid
  if($source == FALSE
    || !array_key_exists('scheme', $source)
    || !in_array($source['scheme'], array('http','https'))
    || !array_key_exists('host', $source)
    || ($source['host'] == gethostbyname($source['host']))
  ) {
    $error($res, 'source_not_found');
    return;
  }

  # Verify $target is actually a resource under our control (home page, individual post)
  $target = parse_url($targetURL);
  # Verify $source is valid
  if($target == FALSE
    || !array_key_exists('scheme', $target)
    || !in_array($target['scheme'], array('http','https'))
    || !array_key_exists('host', $target)
    || $target['host'] != $_SERVER['SERVER_NAME']
  ) {
    $error($res, 'target_not_supported');
    return;
  }

  if(!preg_match('/http:\/\/' . $_SERVER['SERVER_NAME'] . '\/post\/(.+)/', $targetURL, $match)) {
    $error($res, 'target_not_supported', 'The permalink for your post did not match the news.indiewebcamp.com URL convention. Please see news.indiewebcamp.com/constructing-post-urls for more information.');
    return;
  }

  $data = array(
    'domain' => $source['host'],
    'title' => false,
    'body' => false,
    'date' => false
  );
  $notices = array();

  # Now fetch and parse the page looking for Microformats
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $sourceURL);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  $html = curl_exec($ch);

  $parser = new ParserPlus($html);
  $output = $parser->parse();
  $page = new MF2Page($output);

  if($page->hentry) {

    if($page->hentry->property('name'))
      $data['title'] = trim($page->hentry->property('name', true));
    else
      $notices[] = 'No "name" property found on the h-entry. Using the page title instead.';

    if($page->hentry->author && $page->hentry->author->url) {
      $authorURL = parse_url($page->hentry->author->url);
      $data['domain'] = $authorURL['host'];
    } else {
      // $error($res, 'no_author', 'No author was found for the h-entry');
      $notices[] = 'No author URL was found for the h-entry. Using the domain name instead.';
    }

    if($page->hentry->property('content'))
      $data['body'] = strip_tags(trim(implode("\n", $page->hentry->property('content'))));
    else
      $notices[] = 'No post content was found in the h-entry.';

    $entry = $page->hentry;

  } elseif($page->hevent) {

    if($page->hevent->property('name')) {
      $data['title'] = trim($page->hevent->property('name', true));
    }
    if($locations=$page->hevent->location) {
      $location = $locations[0];
      if($location) {
        if(is_object($location) && $location->property('name', true)) {
          $data['title'] .= ' at ' . $location->property('name', true);
        } elseif(is_string($location) && $location) {
          $data['title'] .= ' at ' . $location;
        }
      }
    }

    $entry = $page->hevent;
    
  } else {
    // $error($res, 'no_hentry', 'No h-entry was found on the page');
    $notices[] = 'No h-entry was found on the page. Using the page title for the name' . ($parentID > 0 ? ', and no comment body will be imported.' : '.');
    $entry = false;
  }

  if($page->hentry && ($published=$page->hentry->published)) {
    $data['date'] = $published->format('U');
  } else {
    $notices[] = 'No publish date found.';
  }

  # If no h-entry was found, or if didn't find the title, look at the page title
  if($data['title'] == false) {
    $pageTitle = $parser->xpath('./*/title');
    if($pageTitle->length > 0) {
      foreach($pageTitle as $t) 
        $data['title'] = $t->textContent;
{'fraction_non_alphanumeric': 0.2027329192546584, 'fraction_numerical': 0.0017391304347826088, 'mean_word_length': 3.4163466812945695, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 102, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '7027409', 'n_tokens_mistral': 2716, 'n_tokens_neox': 2673, 'n_words': 606}