Browse Source

Initial commit

master
Chimo 4 years ago
commit
950a4ed587
4 changed files with 180 additions and 0 deletions
  1. 6
    0
      composer.json
  2. 42
    0
      extlib/webmention.php
  3. 83
    0
      index.php
  4. 49
    0
      test.html

+ 6
- 0
composer.json View File

@@ -0,0 +1,6 @@
1
+{
2
+    "require": {
3
+        "mf2/mf2": "0.2.10",
4
+        "barnabywalters/mf-cleaner": "0.1.4"
5
+    }
6
+}

+ 42
- 0
extlib/webmention.php View File

@@ -0,0 +1,42 @@
1
+<?php
2
+
3
+# Based on: https://gist.github.com/adactio/6484118
4
+# Licensed under a CC0 1.0 Universal (CC0 1.0) Public Domain Dedication
5
+# http://creativecommons.org/publicdomain/zero/1.0/
6
+
7
+function get($sourceURL, $targetURL) {
8
+    if (!isset($sourceURL) || !isset($targetURL)) {
9
+        header($_SERVER['SERVER_PROTOCOL'] . ' 400 Bad Request');
10
+        exit;
11
+    }
12
+
13
+    ob_start();
14
+    $ch = curl_init($sourceURL);
15
+    curl_setopt($ch, CURLOPT_USERAGENT, 'chromic.org (webmention.org)');
16
+    curl_setopt($ch, CURLOPT_HEADER, 0);
17
+    $ok = curl_exec($ch);
18
+    curl_close($ch);
19
+    $sourceHTML = ob_get_contents();
20
+    ob_end_clean();
21
+
22
+    header($_SERVER['SERVER_PROTOCOL'] . ' 202 Accepted');
23
+
24
+    if ($ok !== false) {
25
+        return $sourceHTML;
26
+    }
27
+}
28
+
29
+#
30
+# Additions
31
+#
32
+function head($url) {
33
+    $ch = curl_init($url);
34
+    curl_setopt($ch, CURLOPT_NOBODY, true);
35
+    $ok = curl_exec($ch);
36
+    $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
37
+    curl_close($ch);
38
+
39
+    if(substr($http_code, 0, 1) === "2") {
40
+        return true;
41
+    }
42
+}

+ 83
- 0
index.php View File

@@ -0,0 +1,83 @@
1
+<?php
2
+// TODO: DB
3
+// TODO: Check for authorship spoofing
4
+
5
+require_once('extlib/webmention.php');
6
+require_once('vendor/autoload.php');
7
+use BarnabyWalters\Mf2 as Cleaner;
8
+
9
+function safe($obj, $prop) {
10
+    $unsafe = Cleaner\getPlaintext($obj, $prop, "");
11
+
12
+    return strip_tags($unsafe);
13
+}
14
+
15
+$sourceURL = $_POST['source'];
16
+$targetURL = $_POST['target'];
17
+
18
+// Make sure the mentioned page is from our host
19
+if (parse_url($sourceURL, PHP_URL_HOST) !== "chromic.org") { // TODO: make configurable
20
+    error_log("Webmention for another host: " . $sourceURL, 4);
21
+    exit("Err");
22
+}
23
+
24
+// Make sure the page exists
25
+if (head($targetURL) === null) {
26
+    error_log("Webmention for a page that doesn't exist: " . $targetURL, 4);
27
+    exit("Err");
28
+}
29
+
30
+$sourceHTML = get($sourceURL, $targetURL);
31
+
32
+if ($sourceHTML === null) {
33
+    error_log("Failed to fetch remote page: " . $sourceURL, 4);
34
+    exit("Err");
35
+}
36
+
37
+// TODO: https://github.com/pear2/Services_Linkback/blob/2afd453254c531925484196670e2953f669ae141/src/PEAR2/Services/Linkback/Server/Callback/LinkExists.php#L40
38
+if (stristr($sourceHTML, $targetURL)) {
39
+    $mf = Mf2\parse($sourceHTML, $sourceURL);
40
+
41
+    // FIXME: Just use the first item -- for testing purposes, at least
42
+    $hEntry = $mf["items"][0];
43
+
44
+    $entry_name = safe($hEntry, 'name');
45
+    $entry_url = $sourceURL;
46
+    // $entryUrl = safe($hEntry, 'url');
47
+
48
+    if ($entry_name === "") {
49
+        $entry_name = $entry_url;
50
+    }
51
+
52
+    // Truncate this since it could be the entire page
53
+    if (strlen($entry_name) > 150) {
54
+        $entry_name = substr($entry_name, 0, 150) . '[...]';
55
+    }
56
+
57
+    $hCard = Cleaner\getAuthor($hEntry);
58
+    $author_name = safe($hCard, 'name');
59
+    $author_url = safe($hCard, 'url');
60
+
61
+    $by = '';
62
+
63
+    if ($author_name !== '') {
64
+        $by = $author_name;
65
+        
66
+        if ($author_url !== '') {
67
+            $by = '<a href="' . $author_url . '">' . $author_name . '</a>';
68
+        }
69
+    }
70
+
71
+    $on = '<a href="' . $entry_url . '">' . $entry_name . '</a>' ;
72
+
73
+    error_log( 
74
+        sprintf('This page has been mentioned %s on %s',
75
+            $by,
76
+            $on
77
+        ),
78
+        4
79
+    );
80
+
81
+} else {
82
+    error_log("Fake webmention from: " . $sourceURL, 4);
83
+}

+ 49
- 0
test.html View File

@@ -0,0 +1,49 @@
1
+<!DOCTYPE html>
2
+<html>
3
+<head>
4
+<title>Test</title>
5
+</head>
6
+<body>
7
+
8
+<!-- h-entry -->
9
+<article class="h-entry">
10
+
11
+    <span class="entry-tags"><a class="p-category" href="http://chromic.org/tags/#git" title="Pages tagged git">git</a>&nbsp;&bull;&nbsp;<a class="p-category" href="http://chromic.org/tags/#gogs" title="Pages tagged gogs">gogs</a>&nbsp;&bull;&nbsp;<a class="p-category" href="http://chromic.org/tags/#jekyll" title="Pages tagged jekyll">jekyll</a>&nbsp;&bull;&nbsp;<a class="p-category" href="http://chromic.org/tags/#blog" title="Pages tagged blog">blog</a></span>
12
+    <h1 class="entry-title p-name"><a class="u-url" href="/blog/git-gogs-jekyll/">Title <span class="link-arrow fa fa-link"></span></a></h1>
13
+
14
+    <!-- summary -->
15
+    <p class="p-summary">wtf is going on...</p>
16
+
17
+    <div class="e-content">
18
+        <!-- content -->
19
+        <p>
20
+        Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi dapibus vulputate orci, vehicula iaculis magna sollicitudin ut. Morbi risus ex, consectetur non ornare in, viverra eget augue. Pellentesque ullamcorper ex sed diam suscipit, id viverra turpis sodales. Curabitur nisi tortor, viverra consequat finibus sollicitudin, tincidunt quis lectus. Suspendisse ac libero sit amet massa dictum vehicula ut nec ante. Nunc aliquam nibh id sem scelerisque dictum. Fusce at ipsum tellus. Aenean hendrerit neque nibh, eget faucibus est aliquam at. Cras nunc mauris, molestie vitae suscipit in, varius ac ante. Sed pulvinar consequat lorem sed sodales. Fusce congue neque dui, quis mattis dolor rutrum eget.
21
+        </p>
22
+        <p>
23
+        Phasellus molestie auctor erat tempus consequat. Proin laoreet, nunc ut porttitor bibendum, eros lectus egestas magna, a rutrum ante diam vel elit. Aenean ac lobortis urna. Donec interdum mi libero, ut posuere leo hendrerit nec. Fusce quam felis, mattis ut mi in, cursus laoreet felis. Aenean blandit gravida tellus. Etiam enim lorem, porttitor ac justo ut, consequat lobortis nisi. Sed mattis sem at aliquet varius. Nam in nisi felis. Curabitur dui urna, rhoncus a dignissim id, lobortis pretium sapien. Ut at molestie ipsum.
24
+        </p>
25
+
26
+        <!-- webmention -->
27
+        <a href="http://chromic.org/blog/git-gogs-jekyll/">Something about blog post blah.</a>
28
+
29
+        <!-- other content -->
30
+        <p>
31
+        Maecenas eget dapibus justo. Phasellus eget elit nulla. Quisque blandit vehicula dapibus. Aenean accumsan, velit finibus volutpat dignissim, purus diam condimentum lectus, eu commodo sapien felis vel neque. Proin posuere velit tortor, ut tempus ipsum convallis et. Donec ac purus egestas, lobortis enim sed, commodo odio. Donec rhoncus maximus rhoncus. Donec ac consequat turpis. Phasellus laoreet purus sem, quis convallis lectus pharetra quis. Duis luctus aliquam facilisis. Phasellus sollicitudin, magna non faucibus gravida, orci lectus mollis felis, id scelerisque eros massa ut mauris.
32
+        </p>
33
+        <p>
34
+        Aenean et orci ultricies, semper est a, sagittis sapien. Aliquam eget scelerisque tortor. Mauris elementum mattis eros, sed finibus massa. Maecenas condimentum vulputate est in posuere. Vivamus viverra suscipit consequat. Cras eget interdum justo. Integer convallis urna et imperdiet pharetra. Nullam semper risus in augue interdum, ac maximus lorem laoreet. Pellentesque mattis rhoncus rutrum. Nulla pulvinar libero a leo dictum, non efficitur lorem finibus. Proin tellus leo, faucibus sed finibus sit amet, eleifend id mauris.
35
+        </p>
36
+        <p>
37
+        Ut a nisi felis. Phasellus lacinia blandit lacus. Quisque vel lorem euismod lorem finibus rhoncus. Praesent vel ante vel justo consequat tincidunt ut id eros. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Cras sit amet pellentesque sapien. Quisque id nibh sed orci sagittis hendrerit et sed ante. Nam vel fermentum ipsum. Vivamus egestas lacinia purus a venenatis. Fusce iaculis felis sed turpis laoreet scelerisque. Aenean tincidunt justo et bibendum sagittis. Nunc hendrerit blandit sapien non pulvinar. Proin ut felis tristique, hendrerit risus quis, feugiat turpis.
38
+        </p>
39
+    </div>
40
+
41
+    <!-- author hcard -->
42
+    <span class="p-author h-card">
43
+        <a class="p-name u-url" href="http://chromic.org">Chimo</a>.
44
+        <a class="u-email" href="mailto:chimo@chromic.org"><span class="fa fa-envelope"></span></a>
45
+    </span>
46
+</article>
47
+
48
+</body>
49
+</html>

Loading…
Cancel
Save