Example #1
0
 public static function send($id, $client = false, $http = false)
 {
     $webmention = ORM::for_table('webmentions')->where('id', $id)->find_one();
     if (!$webmention) {
         echo 'Webmention ' . $id . ' was not found' . "\n";
         return;
     }
     if (!$client) {
         $client = new MentionClient();
     }
     if (!$http) {
         $http = new HTTP();
     }
     self::$http = $http;
     // Discover the webmention or pingback endpoint
     $endpoint = $client->discoverWebmentionEndpoint($webmention->target);
     if (!$endpoint) {
         // If no webmention endpoint found, try to send a pingback
         $pingbackEndpoint = $client->discoverPingbackEndpoint($webmention->target);
         // If no pingback endpoint was found, we can't do anything else
         if (!$pingbackEndpoint) {
             return self::updateStatus($webmention, null, 'not_supported');
         }
         $webmention->pingback_endpoint = $pingbackEndpoint;
         $webmention->save();
         $success = $client->sendPingbackToEndpoint($pingbackEndpoint, $webmention->source, $webmention->target);
         return self::updateStatus($webmention, null, $success ? 'accepted' : 'error');
     }
     // There is a webmention endpoint, send the webmention now
     $webmention->webmention_endpoint = $endpoint;
     $webmention->save();
     $response = $client->sendWebmentionToEndpoint($endpoint, $webmention->source, $webmention->target);
     if (in_array($response['code'], [200, 201, 202])) {
         $status = 'accepted';
         $webmention->complete = $response['code'] == 200 ? 1 : 0;
         // Check if the endpoint returned a status URL
         if (array_key_exists('Location', $response['headers'])) {
             $webmention->webmention_status_url = \Mf2\resolveUrl($endpoint, $response['headers']['Location']);
             // TODO: queue a job to poll the endpoint for updates and deliver to the callback URL
         } else {
             // No status URL was returned, so we can't follow up with this later. Mark as complete.
             $webmention->complete = 1;
         }
     } else {
         $webmention->complete = 1;
         $status = 'error';
     }
     $webmention->save();
     return self::updateStatus($webmention, $response['code'], $status, $response['body']);
 }
Example #2
0
            $nameState = null;
            if ($content != null and $content != $parsedName) {
                $nameState = mb_strlen($parsedName) > mb_strlen($content) ? 'invalid' : 'valid';
            }
        } else {
            $postType = $hEntry = $nameState = null;
        }
        return crossOriginResponse(render('validate-h-entry.html', array('showResult' => true, 'postType' => $postType, 'hEntry' => $hEntry, 'nameState' => $nameState, 'url' => htmlspecialchars($url))));
    }
});
$app->get('/send-webmentions/', function (Http\Request $request) {
    return render('send-webmentions.html', array('url' => $request->query->get('url', '')));
});
$app->post('/send-webmentions/', function (Http\Request $request) {
    ob_start();
    $url = web_address_to_uri($request->get('url'), true);
    ob_end_clean();
    $errorResponse = errorResponder('send-webmentions.html', $url);
    if (empty($url)) {
        return $errorResponse('Empty URLs lead nowhere!');
    }
    list($mfs, $err) = fetchMf($url);
    if ($err) {
        return $errorResponse(htmlspecialchars($err->getMessage()));
    }
    $hEntries = Mf2\findMicroformatsByType($mfs, 'h-entry');
    $mentioner = new MentionClient($url);
    $numSent = $mentioner->sendSupportedMentions();
    return crossOriginResponse(render('send-webmentions.html', array('numSent' => $numSent, 'url' => htmlspecialchars($url), 'hEntriesFound' => count($hEntries))));
});
$app->run();