-
Notifications
You must be signed in to change notification settings - Fork 9
/
init.php
177 lines (133 loc) · 5.76 KB
/
init.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
class oneclickpocket extends Plugin {
private $host;
function init($host) {
$this->link = $host->get_link();
$this->host = $host;
$host->add_hook($host::HOOK_ARTICLE_BUTTON, $this);
$host->add_hook($host::HOOK_PREFS_TAB, $this);
$host->add_hook($host::HOOK_HOTKEY_MAP, $this);
$host->add_hook($host::HOOK_HOTKEY_INFO, $this);
}
function about() {
return array(0.35,
"Add articles to Pocket with a single click",
"fxneumann");
}
function save() {
$pocket_consumer_key = clean($_POST["pocket_consumer_key"]);
$this->host->set($this, "pocket_consumer_key", $pocket_consumer_key);
$pocket_access_token = clean($_POST["pocket_access_token"]);
$this->host->set($this, "pocket_access_token", $pocket_access_token);
echo "Consumer Key set to<br/> <small>$pocket_consumer_key</small><br/>Access Token set to<br/> <small>$pocket_access_token</small>";
}
function api_version() {
return 2;
}
function get_js() {
return file_get_contents(dirname(__FILE__) . "/pocket.js");
}
function hook_article_button($line) {
$article_id = $line["id"];
$rv = "<img src=\"plugins/oneclickpocket/pocketgrey.png\"
class=\"tagsPic\" id=\"ocp$article_id\" style=\"cursor : pointer\"
onclick=\"Plugins.oneclickpocket.shareArticleToPocket($article_id, this)\"
title='".__('Save to Pocket')."'>";
return $rv;
}
function getInfo() {
//retrieve Data from the DB
$id = $_REQUEST['id'];
$sth = $this->pdo->prepare("SELECT title, link
FROM ttrss_entries, ttrss_user_entries
WHERE id = ? AND ref_id = id AND owner_uid = ?");
$sth->execute([$id, $_SESSION['uid']]);
if($sth->rowCount() != 0) {
$row = $sth->fetch();
$title = truncate_string(strip_tags($row['title']), 100, '...');
$article_link = $row['link'];
}
$consumer_key = $this->host->get($this, "pocket_consumer_key");
$pocket_access_token = $this->host->get($this, "pocket_access_token");
//Call Pocket API
if (function_exists('curl_init')) {
$postfields = array(
'consumer_key' => $consumer_key,
'access_token' => $pocket_access_token,
'url' => $article_link,
'title' => $title
);
$cURL = curl_init();
curl_setopt($cURL, CURLOPT_URL, 'https://getpocket.com/v3/add');
curl_setopt($cURL, CURLOPT_HEADER, 1);
curl_setopt($cURL, CURLOPT_HTTPHEADER, array('Content-type: application/x-www-form-urlencoded;charset=UTF-8'));
curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true);
curl_setopt($cURL, CURLOPT_TIMEOUT, 5);
curl_setopt($cURL, CURLOPT_POST, 4);
curl_setopt($cURL, CURLOPT_POSTFIELDS, http_build_query($postfields));
$apicall = curl_exec($cURL);
curl_close($cURL);
//Store error code in $status
$status = preg_match('/^X-Error: .*$/m', $apicall, $matches) ? $matches[0] : 1;
} else {
$status = 'For the plugin to work you need to <strong>enable PHP extension CURL</strong>!';
}
//Return information on article and status
print json_encode(array(
"title" => $title,
"link" => $article_link,
"id" => $id,
"status" => $status
));
}
function hook_prefs_tab($args) {
//Add preferences pane
if ($args != "prefPrefs") return;
print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__("Pocket")."\">";
print "<br/>";
$pocket_consumer_key = $this->host->get($this, "pocket_consumer_key");
$pocket_access_token = $this->host->get($this, "pocket_access_token");
print "<form dojoType=\"dijit.form.Form\">";
print "<script type=\"dojo/method\" event=\"onSubmit\" args=\"evt\">
evt.preventDefault();
if (this.validate()) {
console.log(dojo.objectToQuery(this.getValues()));
xhrPost('backend.php',
this.getValues(),
(transport) => { Notify.info(transport.responseText); });
}
</script>";
print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"op\" value=\"pluginhandler\">";
print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"method\" value=\"save\">";
print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"plugin\" value=\"oneclickpocket\">";
print "<table width=\"100%\" class=\"prefPrefsList\">";
if (!function_exists('curl_init')) {
print '<tr><td colspan="3" style="color:red;font-size:large">For the plugin to work you need to <strong>enable PHP extension CURL</strong>!</td></tr>';
}
print "<tr><td width=\"20%\">".__("Pocket Consumer Key")."</td>";
print '<td width=\"20%\">Get a <a href="http://getpocket.com/developer/apps/new">Pocket Consumer Key</a></td>';
print "<td class=\"prefValue\"><input dojoType=\"dijit.form.ValidationTextBox\" required=\"1\" name=\"pocket_consumer_key\" value=\"$pocket_consumer_key\"></td>";
print "<tr><td width=\"20%\">".__("Pocket Access Token")."</td>";
print "<td width=\"20%\"><a href=\"plugins/oneclickpocket/auth.php?consumer_key=$pocket_consumer_key\">Generate Access Token</a></td>";
print "<td class=\"prefValue\"><input dojoType=\"dijit.form.ValidationTextBox\" required=\"1\" name=\"pocket_access_token\" value=\"$pocket_access_token\"></td></tr>";
print "</table>";
print "<p><button dojoType=\"dijit.form.Button\" type=\"submit\">".__("Save")."</button>";
print "</form>";
print "</div>"; #pane
}
function hook_hotkey_map($hotkeys) {
// Use the new target "pock_it" to define your own
// hotkey to this function in other plugins.
$hotkeys['i'] = 'pock_it';
return $hotkeys;
}
function hook_hotkey_info($hotkeys) {
$offset = 1 + array_search('open_in_new_window', array_keys($hotkeys[__('Article')]));
$hotkeys[__('Article')] =
array_slice($hotkeys[__('Article')], 0, $offset, true) +
array('pock_it' => __('Save to Pocket')) +
array_slice($hotkeys[__('Article')], $offset, NULL, true);
return $hotkeys;
}
}
?>