Skip to content

Commit 2141426

Browse files
committed
Implemented receiving webmentions for which I added php-mf2 and more RSSB stuff like showing mention type counts.
1 parent 705cc0d commit 2141426

10 files changed

Lines changed: 331 additions & 55 deletions

File tree

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"phpish/template": "dev-master",
55
"phpish/mysql": "dev-master",
66
"phpish/http": "dev-master",
7-
"michelf/php-markdown": "1.3.*@dev"
7+
"michelf/php-markdown": "1.3.*@dev",
8+
"mf2/mf2": "0.1.*"
89
}
910
}

composer.lock

Lines changed: 152 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

data.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,31 @@ function db_delete_post_channels($post_id, $channels_to_delete)
101101
return mysql\query("DELETE FROM channels WHERE post_id = %d and name in ('".implode("','", $channels_to_delete)."')", array($post_id));
102102
}
103103

104+
//TODO: Do an upsert here; add only if it doesn't exist.
104105
function db_add_post_channel($post_id, $channel_name, $now, $is_private)
105106
{
106107
return mysql\query("INSERT INTO channels (name, post_id, created_at, private) VALUES ('%s', %d, '%s', %d)", array($channel_name, $post_id, $now, $is_private));
107108
}
108109

110+
111+
function db_add_webmention($post_id, $source, $source_hash, $target, $target_hash, $now, $type, $content)
112+
{
113+
return mysql\query("INSERT INTO webmentions (post_id, source, source_hash, target, target_hash, created_at, updated_at, type, content) VALUES ('%d', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s') ON DUPLICATE KEY UPDATE updated_at = '%s', content = '%s'", array($post_id, $source, $source_hash, $target, $target_hash, $now, $now, $type, $content, $now, $content));
114+
}
115+
116+
function db_get_webmentions($post_id, $type)
117+
{
118+
return mysql\rows("SELECT source FROM webmentions where post_id = %d and type = '%s' ORDER BY created_at", array($post_id, $type));
119+
}
120+
121+
function db_get_webmention_type_counts($post_id)
122+
{
123+
return mysql\rows('SELECT type, count(type) as count FROM webmentions where post_id = %d GROUP BY type', array($post_id));
124+
}
125+
126+
127+
function db_error()
128+
{
129+
return mysql\error();
130+
}
109131
?>

helpers.php

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,26 @@
55

66
function send_webmention($source, $target)
77
{
8-
$response_headers = $matches = array();
9-
$target_webmention_endpoint = false;
8+
if ($target_webmention_endpoint = discover_webmention_endpoint($target))
9+
{
10+
$response_body = http\request("POST $target_webmention_endpoint", array(), array('source'=>$source, 'target'=>$target), $response_headers);
11+
print_r(compact('source', 'target', 'target_webmention_endpoint', 'response_headers', 'response_body'));
12+
}
13+
14+
15+
}
16+
17+
function discover_webmention_endpoint($target)
18+
{
1019
$response_body = http\request("GET $target", array(), array(), $response_headers);
1120
if (isset($response_headers['link']) and preg_match('#<(https?://[^>]+)>; rel="http://webmention.org/"#', $response_headers['link'], $matches))
1221
{
13-
$target_webmention_endpoint = $matches[1];
22+
return $matches[1];
1423
}
1524
elseif (preg_match('#<link href="([^"]+)" rel="http://webmention.org/" ?/?>#i', $response_body, $matches) or preg_match('#<link rel="http://webmention.org/" href="([^"]+)" ?/?>#i', $response_body, $matches))
1625
{
17-
$target_webmention_endpoint = $matches[1];
18-
}
19-
20-
if ($target_webmention_endpoint)
21-
{
22-
$response_body = http\request("POST $target_webmention_endpoint", array(), array('source'=>$source, 'target'=>$target), $response_headers);
23-
print_r($response_headers);
24-
print_r($response_body);
26+
return $matches[1];
2527
}
26-
2728
}
2829

2930
function gravatar_url($email, $s=80, $d='mm', $r='g', $img=false)

0 commit comments

Comments
 (0)