-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.php
84 lines (76 loc) · 3.27 KB
/
extension.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
class MicropubLikeExtension extends Minz_Extension
{
public function init() {
$current_user = Minz_Session::param('currentUser');
$filename = 'micropub.' . $current_user . '.json';
$staticPath = join_path($this->getPath(), 'static');
$filepath = join_path($staticPath, $filename);
if (file_exists($filepath)) {
$config = json_decode(file_get_contents($filepath), TRUE);
if (isset($config["endpoint"]) && isset($config["token"])) {
$this->micropub_endpoint = htmlentities($config["endpoint"]);
$this->micropub_token = htmlentities($config["token"]);
$this->registerHook('entry_after_favourite', array($this, 'syndicateFavourite'));
}
}
}
public function syndicateFavourite($id, $is_favourite = 1) {
$endpoint = $this->micropub_endpoint;
$token= $this->micropub_token;
if ($is_favourite) {
$entryDAO = FreshRSS_Factory::createEntryDao();
$entry = $entryDAO->searchById($id);
$link = $entry->link();
# Create a JSON "like" using the micropub syntax.
$data = array(
"type" => array("h-entry"),
"properties" => array (
"like-of" => array($link),
),
);
$body = json_encode($data);
$ch = curl_init($endpoint);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($body),
'Authorization: Bearer ' . $token
)
);
$result = curl_exec($ch);
curl_close($ch);
}
}
public function handleConfigureAction() {
$this->registerTranslates();
$current_user = Minz_Session::param('currentUser');
$filename = 'micropub.' . $current_user . '.json';
$staticPath = join_path($this->getPath(), 'static');
$filepath = join_path($staticPath, $filename);
if (!file_exists($filepath) && !is_writable($staticPath)) {
$tmpPath = explode(EXTENSIONS_PATH . '/', $staticPath);
$this->permission_problem = $tmpPath[1] . '/';
} else if (file_exists($filepath) && !is_writable($filepath)) {
$tmpPath = explode(EXTENSIONS_PATH . '/', $filepath);
$this->permission_problem = $tmpPath[1];
} else if (Minz_Request::isPost()) {
$endpoint = html_entity_decode(Minz_Request::param('micropub-endpoint', ''));
$token = html_entity_decode(Minz_Request::param('micropub-token', ''));
$config = json_encode(array(
"endpoint" => $endpoint,
"token" => $token,
));
file_put_contents($filepath, $config);
}
$this->micropub_endpoint = '';
$this->micropub_token= '';
if (file_exists($filepath)) {
$config = json_decode(file_get_contents($filepath), TRUE);
$this->micropub_endpoint = htmlentities($config["endpoint"]);
$this->micropub_token = htmlentities($config["token"]);
}
}
}