-
Notifications
You must be signed in to change notification settings - Fork 1
/
models.php
64 lines (55 loc) · 1.53 KB
/
models.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
class Webmention {
public $data = null;
public $sourceUrl = null;
public $targetUrl = null;
public $mentionId = null;
public function __construct($mention) {
$this->data = $mention;
$prop = $mention['wm-property'];
if ($prop == 'rsvp') {
$prop = "in-reply-to";
}
$this->targetUrl = $mention[$prop];
$this->sourceUrl = $mention['url'];
$this->mentionId = hash('sha256', $this->sourceUrl . $this->targetUrl);
}
}
class WebmentionStore {
public function add_mention($mention) {
$file_path = APP_DATA_DIR . DIRECTORY_SEPARATOR . $mention->mentionId . ".json";
file_put_contents(
$file_path,
json_encode($mention->data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)
);
}
}
class WebmentionIndex {
/**
* Array of URL path => [ mentionId, mentionId, ... ]
* TODO: make sortable by sub-indexing on wm-received time.
*/
private $_index = array();
public function __construct() {
$index_path = APP_INDEX_PATH;
if (file_exists($index_path)) {
$this->_index = json_decode(file_get_contents($index_path), true);
}
}
public function add_mention($mention) {
$path = parse_url($mention->targetUrl, PHP_URL_PATH);
$mention_ids = array_key_exists($path, $this->_index)
? $this->_index[$path] : [];
if (! in_array($mention->mentionId, $mention_ids) ) {
$mention_ids []= $mention->mentionId;
$this->_index[$path] = $mention_ids;
}
}
public function save() {
$index_path = APP_INDEX_PATH;
file_put_contents(
$index_path,
json_encode($this->_index, JSON_UNESCAPED_SLASHES)
);
}
}