]>
Commit | Line | Data |
---|---|---|
de8260cb | 1 | <?php |
de8260cb AD |
2 | class API extends Handler { |
3 | ||
f1b3b3f3 | 4 | const API_LEVEL = 14; |
de8260cb AD |
5 | |
6 | const STATUS_OK = 0; | |
7 | const STATUS_ERR = 1; | |
8 | ||
9 | private $seq; | |
10 | ||
11 | function before($method) { | |
12 | if (parent::before($method)) { | |
12f31782 | 13 | header("Content-Type: text/json"); |
de8260cb AD |
14 | |
15 | if (!$_SESSION["uid"] && $method != "login" && $method != "isloggedin") { | |
6f7798b6 | 16 | $this->wrap(self::STATUS_ERR, array("error" => 'NOT_LOGGED_IN')); |
de8260cb AD |
17 | return false; |
18 | } | |
19 | ||
a42c55f0 | 20 | if ($_SESSION["uid"] && $method != "logout" && !get_pref('ENABLE_API_ACCESS')) { |
6f7798b6 | 21 | $this->wrap(self::STATUS_ERR, array("error" => 'API_DISABLED')); |
de8260cb AD |
22 | return false; |
23 | } | |
24 | ||
25 | $this->seq = (int) $_REQUEST['seq']; | |
26 | ||
de8260cb AD |
27 | return true; |
28 | } | |
29 | return false; | |
30 | } | |
31 | ||
32 | function wrap($status, $reply) { | |
33 | print json_encode(array("seq" => $this->seq, | |
34 | "status" => $status, | |
35 | "content" => $reply)); | |
36 | } | |
37 | ||
38 | function getVersion() { | |
39 | $rv = array("version" => VERSION); | |
6f7798b6 | 40 | $this->wrap(self::STATUS_OK, $rv); |
de8260cb AD |
41 | } |
42 | ||
43 | function getApiLevel() { | |
a3b5394a | 44 | $rv = array("level" => self::API_LEVEL); |
6f7798b6 | 45 | $this->wrap(self::STATUS_OK, $rv); |
de8260cb AD |
46 | } |
47 | ||
48 | function login() { | |
79bb5589 | 49 | @session_destroy(); |
5160620c AD |
50 | @session_start(); |
51 | ||
d9c85e0f | 52 | $login = $this->dbh->escape_string($_REQUEST["user"]); |
de8260cb AD |
53 | $password = $_REQUEST["password"]; |
54 | $password_base64 = base64_decode($_REQUEST["password"]); | |
55 | ||
56 | if (SINGLE_USER_MODE) $login = "admin"; | |
57 | ||
d9c85e0f | 58 | $result = $this->dbh->query("SELECT id FROM ttrss_users WHERE login = '$login'"); |
de8260cb | 59 | |
d9c85e0f AD |
60 | if ($this->dbh->num_rows($result) != 0) { |
61 | $uid = $this->dbh->fetch_result($result, 0, "id"); | |
de8260cb AD |
62 | } else { |
63 | $uid = 0; | |
64 | } | |
65 | ||
66 | if (!$uid) { | |
6f7798b6 | 67 | $this->wrap(self::STATUS_ERR, array("error" => "LOGIN_ERROR")); |
de8260cb AD |
68 | return; |
69 | } | |
70 | ||
a42c55f0 AD |
71 | if (get_pref("ENABLE_API_ACCESS", $uid)) { |
72 | if (authenticate_user($login, $password)) { // try login with normal password | |
6f7798b6 | 73 | $this->wrap(self::STATUS_OK, array("session_id" => session_id(), |
5ba4ebc6 | 74 | "api_level" => self::API_LEVEL)); |
a42c55f0 | 75 | } else if (authenticate_user($login, $password_base64)) { // else try with base64_decoded password |
6f7798b6 | 76 | $this->wrap(self::STATUS_OK, array("session_id" => session_id(), |
5ba4ebc6 | 77 | "api_level" => self::API_LEVEL)); |
de8260cb | 78 | } else { // else we are not logged in |
a2108ee9 | 79 | user_error("Failed login attempt for $login from {$_SERVER['REMOTE_ADDR']}", E_USER_WARNING); |
6f7798b6 | 80 | $this->wrap(self::STATUS_ERR, array("error" => "LOGIN_ERROR")); |
de8260cb AD |
81 | } |
82 | } else { | |
6f7798b6 | 83 | $this->wrap(self::STATUS_ERR, array("error" => "API_DISABLED")); |
de8260cb AD |
84 | } |
85 | ||
86 | } | |
87 | ||
88 | function logout() { | |
89 | logout_user(); | |
6f7798b6 | 90 | $this->wrap(self::STATUS_OK, array("status" => "OK")); |
de8260cb AD |
91 | } |
92 | ||
93 | function isLoggedIn() { | |
6f7798b6 | 94 | $this->wrap(self::STATUS_OK, array("status" => $_SESSION["uid"] != '')); |
de8260cb AD |
95 | } |
96 | ||
97 | function getUnread() { | |
d9c85e0f AD |
98 | $feed_id = $this->dbh->escape_string($_REQUEST["feed_id"]); |
99 | $is_cat = $this->dbh->escape_string($_REQUEST["is_cat"]); | |
de8260cb AD |
100 | |
101 | if ($feed_id) { | |
6f7798b6 | 102 | $this->wrap(self::STATUS_OK, array("unread" => getFeedUnread($feed_id, $is_cat))); |
de8260cb | 103 | } else { |
6f7798b6 | 104 | $this->wrap(self::STATUS_OK, array("unread" => getGlobalUnread())); |
de8260cb AD |
105 | } |
106 | } | |
107 | ||
108 | /* Method added for ttrss-reader for Android */ | |
109 | function getCounters() { | |
6f7798b6 | 110 | $this->wrap(self::STATUS_OK, getAllCounters()); |
de8260cb AD |
111 | } |
112 | ||
113 | function getFeeds() { | |
d9c85e0f | 114 | $cat_id = $this->dbh->escape_string($_REQUEST["cat_id"]); |
9955a134 | 115 | $unread_only = sql_bool_to_bool($_REQUEST["unread_only"]); |
d9c85e0f AD |
116 | $limit = (int) $this->dbh->escape_string($_REQUEST["limit"]); |
117 | $offset = (int) $this->dbh->escape_string($_REQUEST["offset"]); | |
9955a134 | 118 | $include_nested = sql_bool_to_bool($_REQUEST["include_nested"]); |
de8260cb | 119 | |
a42c55f0 | 120 | $feeds = $this->api_get_feeds($cat_id, $unread_only, $limit, $offset, $include_nested); |
de8260cb | 121 | |
6f7798b6 | 122 | $this->wrap(self::STATUS_OK, $feeds); |
de8260cb AD |
123 | } |
124 | ||
125 | function getCategories() { | |
9955a134 AD |
126 | $unread_only = sql_bool_to_bool($_REQUEST["unread_only"]); |
127 | $enable_nested = sql_bool_to_bool($_REQUEST["enable_nested"]); | |
b3575bd8 | 128 | $include_empty = sql_bool_to_bool($_REQUEST['include_empty']); |
de8260cb AD |
129 | |
130 | // TODO do not return empty categories, return Uncategorized and standard virtual cats | |
131 | ||
48646336 AD |
132 | if ($enable_nested) |
133 | $nested_qpart = "parent_cat IS NULL"; | |
134 | else | |
135 | $nested_qpart = "true"; | |
136 | ||
d9c85e0f | 137 | $result = $this->dbh->query("SELECT |
d49dfa38 AD |
138 | id, title, order_id, (SELECT COUNT(id) FROM |
139 | ttrss_feeds WHERE | |
7be3fcd5 AD |
140 | ttrss_feed_categories.id IS NOT NULL AND cat_id = ttrss_feed_categories.id) AS num_feeds, |
141 | (SELECT COUNT(id) FROM | |
142 | ttrss_feed_categories AS c2 WHERE | |
143 | c2.parent_cat = ttrss_feed_categories.id) AS num_cats | |
d49dfa38 | 144 | FROM ttrss_feed_categories |
48646336 | 145 | WHERE $nested_qpart AND owner_uid = " . |
de8260cb AD |
146 | $_SESSION["uid"]); |
147 | ||
148 | $cats = array(); | |
149 | ||
d9c85e0f | 150 | while ($line = $this->dbh->fetch_assoc($result)) { |
66ca7c30 | 151 | if ($include_empty || $line["num_feeds"] > 0 || $line["num_cats"] > 0) { |
a42c55f0 | 152 | $unread = getFeedUnread($line["id"], true); |
d49dfa38 AD |
153 | |
154 | if ($enable_nested) | |
a42c55f0 | 155 | $unread += getCategoryChildrenUnread($line["id"]); |
d49dfa38 AD |
156 | |
157 | if ($unread || !$unread_only) { | |
158 | array_push($cats, array("id" => $line["id"], | |
159 | "title" => $line["title"], | |
160 | "unread" => $unread, | |
161 | "order_id" => (int) $line["order_id"], | |
162 | )); | |
163 | } | |
de8260cb AD |
164 | } |
165 | } | |
166 | ||
167 | foreach (array(-2,-1,0) as $cat_id) { | |
c0a08063 | 168 | if ($include_empty || !$this->isCategoryEmpty($cat_id)) { |
a42c55f0 | 169 | $unread = getFeedUnread($cat_id, true); |
de8260cb | 170 | |
c0a08063 AD |
171 | if ($unread || !$unread_only) { |
172 | array_push($cats, array("id" => $cat_id, | |
a42c55f0 | 173 | "title" => getCategoryTitle($cat_id), |
c0a08063 AD |
174 | "unread" => $unread)); |
175 | } | |
de8260cb AD |
176 | } |
177 | } | |
178 | ||
6f7798b6 | 179 | $this->wrap(self::STATUS_OK, $cats); |
de8260cb AD |
180 | } |
181 | ||
182 | function getHeadlines() { | |
d9c85e0f | 183 | $feed_id = $this->dbh->escape_string($_REQUEST["feed_id"]); |
8aa3becc | 184 | if ($feed_id != "") { |
de8260cb | 185 | |
96ac72bc AD |
186 | if (is_numeric($feed_id)) $feed_id = (int) $feed_id; |
187 | ||
d9c85e0f | 188 | $limit = (int)$this->dbh->escape_string($_REQUEST["limit"]); |
1a740cf6 | 189 | |
6421b429 | 190 | if (!$limit || $limit >= 200) $limit = 200; |
1a740cf6 | 191 | |
d9c85e0f AD |
192 | $offset = (int)$this->dbh->escape_string($_REQUEST["skip"]); |
193 | $filter = $this->dbh->escape_string($_REQUEST["filter"]); | |
9955a134 AD |
194 | $is_cat = sql_bool_to_bool($_REQUEST["is_cat"]); |
195 | $show_excerpt = sql_bool_to_bool($_REQUEST["show_excerpt"]); | |
196 | $show_content = sql_bool_to_bool($_REQUEST["show_content"]); | |
de8260cb | 197 | /* all_articles, unread, adaptive, marked, updated */ |
d9c85e0f | 198 | $view_mode = $this->dbh->escape_string($_REQUEST["view_mode"]); |
9955a134 | 199 | $include_attachments = sql_bool_to_bool($_REQUEST["include_attachments"]); |
d9c85e0f | 200 | $since_id = (int)$this->dbh->escape_string($_REQUEST["since_id"]); |
9955a134 | 201 | $include_nested = sql_bool_to_bool($_REQUEST["include_nested"]); |
bd3c6723 AD |
202 | $sanitize_content = !isset($_REQUEST["sanitize"]) || |
203 | sql_bool_to_bool($_REQUEST["sanitize"]); | |
4baa1afa | 204 | $force_update = sql_bool_to_bool($_REQUEST["force_update"]); |
9997b38e | 205 | $has_sandbox = sql_bool_to_bool($_REQUEST["has_sandbox"]); |
3194a70a | 206 | $excerpt_length = (int)$this->dbh->escape_string($_REQUEST["excerpt_length"]); |
48fefe2f | 207 | $check_first_id = (int)$this->dbh->escape_string($_REQUEST["check_first_id"]); |
5c784e70 | 208 | $include_header = sql_bool_to_bool($_REQUEST["include_header"]); |
9997b38e AD |
209 | |
210 | $_SESSION['hasSandbox'] = $has_sandbox; | |
ffd07864 | 211 | |
19e47ad6 AD |
212 | $skip_first_id_check = false; |
213 | ||
0bbd1414 | 214 | $override_order = false; |
215 | switch ($_REQUEST["order_by"]) { | |
273c33e5 AD |
216 | case "title": |
217 | $override_order = "ttrss_entries.title"; | |
218 | break; | |
0bbd1414 | 219 | case "date_reverse": |
e9687f67 | 220 | $override_order = "score DESC, date_entered, updated"; |
19e47ad6 | 221 | $skip_first_id_check = true; |
0bbd1414 | 222 | break; |
223 | case "feed_dates": | |
224 | $override_order = "updated DESC"; | |
225 | break; | |
226 | } | |
ffd07864 | 227 | |
3e4af5b0 AD |
228 | /* do not rely on params below */ |
229 | ||
d9c85e0f | 230 | $search = $this->dbh->escape_string($_REQUEST["search"]); |
3e4af5b0 | 231 | |
5c784e70 | 232 | list($headlines, $headlines_header) = $this->api_get_headlines($feed_id, $limit, $offset, |
0bbd1414 | 233 | $filter, $is_cat, $show_excerpt, $show_content, $view_mode, $override_order, |
9090b874 | 234 | $include_attachments, $since_id, $search, |
19e47ad6 | 235 | $include_nested, $sanitize_content, $force_update, $excerpt_length, $check_first_id, $skip_first_id_check); |
de8260cb | 236 | |
5c784e70 AD |
237 | if ($include_header) { |
238 | $this->wrap(self::STATUS_OK, array($headlines_header, $headlines)); | |
239 | } else { | |
240 | $this->wrap(self::STATUS_OK, $headlines); | |
241 | } | |
de8260cb | 242 | } else { |
6f7798b6 | 243 | $this->wrap(self::STATUS_ERR, array("error" => 'INCORRECT_USAGE')); |
de8260cb AD |
244 | } |
245 | } | |
246 | ||
247 | function updateArticle() { | |
d9c85e0f AD |
248 | $article_ids = array_filter(explode(",", $this->dbh->escape_string($_REQUEST["article_ids"])), is_numeric); |
249 | $mode = (int) $this->dbh->escape_string($_REQUEST["mode"]); | |
250 | $data = $this->dbh->escape_string($_REQUEST["data"]); | |
251 | $field_raw = (int)$this->dbh->escape_string($_REQUEST["field"]); | |
de8260cb AD |
252 | |
253 | $field = ""; | |
254 | $set_to = ""; | |
255 | ||
256 | switch ($field_raw) { | |
257 | case 0: | |
258 | $field = "marked"; | |
7873d588 | 259 | $additional_fields = ",last_marked = NOW()"; |
de8260cb AD |
260 | break; |
261 | case 1: | |
262 | $field = "published"; | |
7873d588 | 263 | $additional_fields = ",last_published = NOW()"; |
de8260cb AD |
264 | break; |
265 | case 2: | |
266 | $field = "unread"; | |
7873d588 | 267 | $additional_fields = ",last_read = NOW()"; |
de8260cb AD |
268 | break; |
269 | case 3: | |
270 | $field = "note"; | |
271 | }; | |
272 | ||
273 | switch ($mode) { | |
274 | case 1: | |
275 | $set_to = "true"; | |
276 | break; | |
277 | case 0: | |
278 | $set_to = "false"; | |
279 | break; | |
280 | case 2: | |
281 | $set_to = "NOT $field"; | |
282 | break; | |
283 | } | |
284 | ||
285 | if ($field == "note") $set_to = "'$data'"; | |
286 | ||
287 | if ($field && $set_to && count($article_ids) > 0) { | |
288 | ||
289 | $article_ids = join(", ", $article_ids); | |
290 | ||
d9c85e0f | 291 | $result = $this->dbh->query("UPDATE ttrss_user_entries SET $field = $set_to $additional_fields WHERE ref_id IN ($article_ids) AND owner_uid = " . $_SESSION["uid"]); |
de8260cb | 292 | |
d9c85e0f | 293 | $num_updated = $this->dbh->affected_rows($result); |
de8260cb AD |
294 | |
295 | if ($num_updated > 0 && $field == "unread") { | |
d9c85e0f | 296 | $result = $this->dbh->query("SELECT DISTINCT feed_id FROM ttrss_user_entries |
de8260cb AD |
297 | WHERE ref_id IN ($article_ids)"); |
298 | ||
d9c85e0f | 299 | while ($line = $this->dbh->fetch_assoc($result)) { |
a42c55f0 | 300 | ccache_update($line["feed_id"], $_SESSION["uid"]); |
de8260cb AD |
301 | } |
302 | } | |
303 | ||
7873d588 AD |
304 | if ($num_updated > 0 && $field == "published") { |
305 | if (PUBSUBHUBBUB_HUB) { | |
306 | $rss_link = get_self_url_prefix() . | |
307 | "/public.php?op=rss&id=-2&key=" . | |
a42c55f0 | 308 | get_feed_access_key(-2, false); |
7873d588 | 309 | |
cb3f8773 | 310 | $p = new pubsubhubbub\publisher\Publisher(PUBSUBHUBBUB_HUB); |
7b55001e | 311 | $p->publish_update($rss_link); |
7873d588 AD |
312 | } |
313 | } | |
314 | ||
6f7798b6 | 315 | $this->wrap(self::STATUS_OK, array("status" => "OK", |
de8260cb AD |
316 | "updated" => $num_updated)); |
317 | ||
318 | } else { | |
6f7798b6 | 319 | $this->wrap(self::STATUS_ERR, array("error" => 'INCORRECT_USAGE')); |
de8260cb AD |
320 | } |
321 | ||
322 | } | |
323 | ||
324 | function getArticle() { | |
325 | ||
d9c85e0f | 326 | $article_id = join(",", array_filter(explode(",", $this->dbh->escape_string($_REQUEST["article_id"])), is_numeric)); |
73c77ab0 J |
327 | $sanitize_content = !isset($_REQUEST["sanitize"]) || |
328 | sql_bool_to_bool($_REQUEST["sanitize"]); | |
de8260cb | 329 | |
6f81395d | 330 | if ($article_id) { |
de8260cb | 331 | |
18186149 | 332 | $query = "SELECT id,guid,title,link,content,feed_id,comments,int_id, |
429ff9af | 333 | marked,unread,published,score,note,lang, |
6f81395d | 334 | ".SUBSTRING_FOR_DATE."(updated,1,16) as updated, |
73c77ab0 J |
335 | author,(SELECT title FROM ttrss_feeds WHERE id = feed_id) AS feed_title, |
336 | (SELECT site_url FROM ttrss_feeds WHERE id = feed_id) AS site_url, | |
337 | (SELECT hide_images FROM ttrss_feeds WHERE id = feed_id) AS hide_images | |
6f81395d AD |
338 | FROM ttrss_entries,ttrss_user_entries |
339 | WHERE id IN ($article_id) AND ref_id = id AND owner_uid = " . | |
340 | $_SESSION["uid"] ; | |
de8260cb | 341 | |
6f81395d | 342 | $result = $this->dbh->query($query); |
de8260cb | 343 | |
6f81395d | 344 | $articles = array(); |
de8260cb | 345 | |
6f81395d | 346 | if ($this->dbh->num_rows($result) != 0) { |
0db61af8 | 347 | |
6f81395d | 348 | while ($line = $this->dbh->fetch_assoc($result)) { |
0db61af8 | 349 | |
6f81395d | 350 | $attachments = get_article_enclosures($line['id']); |
de8260cb | 351 | |
6f81395d AD |
352 | $article = array( |
353 | "id" => $line["id"], | |
18186149 | 354 | "guid" => $line["guid"], |
6f81395d AD |
355 | "title" => $line["title"], |
356 | "link" => $line["link"], | |
357 | "labels" => get_article_labels($line['id']), | |
358 | "unread" => sql_bool_to_bool($line["unread"]), | |
359 | "marked" => sql_bool_to_bool($line["marked"]), | |
360 | "published" => sql_bool_to_bool($line["published"]), | |
361 | "comments" => $line["comments"], | |
362 | "author" => $line["author"], | |
363 | "updated" => (int) strtotime($line["updated"]), | |
6f81395d AD |
364 | "feed_id" => $line["feed_id"], |
365 | "attachments" => $attachments, | |
366 | "score" => (int)$line["score"], | |
21bb3c06 | 367 | "feed_title" => $line["feed_title"], |
429ff9af AD |
368 | "note" => $line["note"], |
369 | "lang" => $line["lang"] | |
6f81395d AD |
370 | ); |
371 | ||
73c77ab0 J |
372 | if ($sanitize_content) { |
373 | $article["content"] = sanitize( | |
374 | $line["content"], | |
375 | sql_bool_to_bool($line['hide_images']), | |
376 | false, $line["site_url"], false, $line["id"]); | |
377 | } else { | |
378 | $article["content"] = $line["content"]; | |
379 | } | |
380 | ||
6f81395d AD |
381 | foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_RENDER_ARTICLE_API) as $p) { |
382 | $article = $p->hook_render_article_api(array("article" => $article)); | |
383 | } | |
384 | ||
385 | ||
386 | array_push($articles, $article); | |
de8260cb | 387 | |
6f81395d AD |
388 | } |
389 | } | |
de8260cb | 390 | |
6f81395d AD |
391 | $this->wrap(self::STATUS_OK, $articles); |
392 | } else { | |
393 | $this->wrap(self::STATUS_ERR, array("error" => 'INCORRECT_USAGE')); | |
394 | } | |
de8260cb AD |
395 | } |
396 | ||
397 | function getConfig() { | |
398 | $config = array( | |
399 | "icons_dir" => ICONS_DIR, | |
400 | "icons_url" => ICONS_URL); | |
401 | ||
402 | $config["daemon_is_running"] = file_is_locked("update_daemon.lock"); | |
403 | ||
d9c85e0f | 404 | $result = $this->dbh->query("SELECT COUNT(*) AS cf FROM |
de8260cb AD |
405 | ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]); |
406 | ||
d9c85e0f | 407 | $num_feeds = $this->dbh->fetch_result($result, 0, "cf"); |
de8260cb AD |
408 | |
409 | $config["num_feeds"] = (int)$num_feeds; | |
410 | ||
6f7798b6 | 411 | $this->wrap(self::STATUS_OK, $config); |
de8260cb AD |
412 | } |
413 | ||
414 | function updateFeed() { | |
c1f6e5f8 AD |
415 | require_once "include/rssfuncs.php"; |
416 | ||
d9c85e0f | 417 | $feed_id = (int) $this->dbh->escape_string($_REQUEST["feed_id"]); |
de8260cb | 418 | |
29c92d7b | 419 | if (!ini_get("open_basedir")) { |
7b55001e | 420 | update_rss_feed($feed_id); |
29c92d7b | 421 | } |
de8260cb | 422 | |
6f7798b6 | 423 | $this->wrap(self::STATUS_OK, array("status" => "OK")); |
de8260cb AD |
424 | } |
425 | ||
426 | function catchupFeed() { | |
d9c85e0f AD |
427 | $feed_id = $this->dbh->escape_string($_REQUEST["feed_id"]); |
428 | $is_cat = $this->dbh->escape_string($_REQUEST["is_cat"]); | |
de8260cb | 429 | |
a42c55f0 | 430 | catchup_feed($feed_id, $is_cat); |
de8260cb | 431 | |
6f7798b6 | 432 | $this->wrap(self::STATUS_OK, array("status" => "OK")); |
de8260cb AD |
433 | } |
434 | ||
435 | function getPref() { | |
d9c85e0f | 436 | $pref_name = $this->dbh->escape_string($_REQUEST["pref_name"]); |
de8260cb | 437 | |
6f7798b6 | 438 | $this->wrap(self::STATUS_OK, array("value" => get_pref($pref_name))); |
de8260cb AD |
439 | } |
440 | ||
ea1c2903 | 441 | function getLabels() { |
d9c85e0f | 442 | //$article_ids = array_filter(explode(",", $this->dbh->escape_string($_REQUEST["article_ids"])), is_numeric); |
ea1c2903 AD |
443 | |
444 | $article_id = (int)$_REQUEST['article_id']; | |
445 | ||
446 | $rv = array(); | |
447 | ||
d9c85e0f | 448 | $result = $this->dbh->query("SELECT id, caption, fg_color, bg_color |
ea1c2903 AD |
449 | FROM ttrss_labels2 |
450 | WHERE owner_uid = '".$_SESSION['uid']."' ORDER BY caption"); | |
451 | ||
452 | if ($article_id) | |
a42c55f0 | 453 | $article_labels = get_article_labels($article_id); |
ea1c2903 AD |
454 | else |
455 | $article_labels = array(); | |
456 | ||
d9c85e0f | 457 | while ($line = $this->dbh->fetch_assoc($result)) { |
ea1c2903 AD |
458 | |
459 | $checked = false; | |
460 | foreach ($article_labels as $al) { | |
25237aea | 461 | if (feed_to_label_id($al[0]) == $line['id']) { |
ea1c2903 AD |
462 | $checked = true; |
463 | break; | |
464 | } | |
465 | } | |
466 | ||
467 | array_push($rv, array( | |
891d033c | 468 | "id" => (int)label_to_feed_id($line['id']), |
ea1c2903 AD |
469 | "caption" => $line['caption'], |
470 | "fg_color" => $line['fg_color'], | |
471 | "bg_color" => $line['bg_color'], | |
472 | "checked" => $checked)); | |
473 | } | |
474 | ||
6f7798b6 | 475 | $this->wrap(self::STATUS_OK, $rv); |
ea1c2903 AD |
476 | } |
477 | ||
396bfdf9 AD |
478 | function setArticleLabel() { |
479 | ||
d9c85e0f AD |
480 | $article_ids = array_filter(explode(",", $this->dbh->escape_string($_REQUEST["article_ids"])), is_numeric); |
481 | $label_id = (int) $this->dbh->escape_string($_REQUEST['label_id']); | |
3517d363 | 482 | $assign = (bool) ($this->dbh->escape_string($_REQUEST['assign']) == "true"); |
396bfdf9 | 483 | |
d9c85e0f | 484 | $label = $this->dbh->escape_string(label_find_caption( |
788cf0a1 | 485 | feed_to_label_id($label_id), $_SESSION["uid"])); |
396bfdf9 AD |
486 | |
487 | $num_updated = 0; | |
488 | ||
489 | if ($label) { | |
490 | ||
491 | foreach ($article_ids as $id) { | |
492 | ||
493 | if ($assign) | |
a42c55f0 | 494 | label_add_article($id, $label, $_SESSION["uid"]); |
396bfdf9 | 495 | else |
a42c55f0 | 496 | label_remove_article($id, $label, $_SESSION["uid"]); |
396bfdf9 AD |
497 | |
498 | ++$num_updated; | |
499 | ||
500 | } | |
501 | } | |
502 | ||
6f7798b6 | 503 | $this->wrap(self::STATUS_OK, array("status" => "OK", |
396bfdf9 AD |
504 | "updated" => $num_updated)); |
505 | ||
506 | } | |
507 | ||
79f9bef7 | 508 | function index($method) { |
1ffe3391 | 509 | $plugin = PluginHost::getInstance()->get_api_method(strtolower($method)); |
79f9bef7 AD |
510 | |
511 | if ($plugin && method_exists($plugin, $method)) { | |
512 | $reply = $plugin->$method(); | |
513 | ||
6f7798b6 | 514 | $this->wrap($reply[0], $reply[1]); |
79f9bef7 AD |
515 | |
516 | } else { | |
6f7798b6 | 517 | $this->wrap(self::STATUS_ERR, array("error" => 'UNKNOWN_METHOD', "method" => $method)); |
79f9bef7 | 518 | } |
de8260cb AD |
519 | } |
520 | ||
8361e724 | 521 | function shareToPublished() { |
d9c85e0f AD |
522 | $title = $this->dbh->escape_string(strip_tags($_REQUEST["title"])); |
523 | $url = $this->dbh->escape_string(strip_tags($_REQUEST["url"])); | |
524 | $content = $this->dbh->escape_string(strip_tags($_REQUEST["content"])); | |
8361e724 | 525 | |
a42c55f0 | 526 | if (Article::create_published_article($title, $url, $content, "", $_SESSION["uid"])) { |
6f7798b6 | 527 | $this->wrap(self::STATUS_OK, array("status" => 'OK')); |
8361e724 | 528 | } else { |
6f7798b6 | 529 | $this->wrap(self::STATUS_ERR, array("error" => 'Publishing failed')); |
8361e724 AD |
530 | } |
531 | } | |
04f60eb7 | 532 | |
a42c55f0 | 533 | static function api_get_feeds($cat_id, $unread_only, $limit, $offset, $include_nested = false) { |
04f60eb7 AD |
534 | |
535 | $feeds = array(); | |
536 | ||
537 | /* Labels */ | |
538 | ||
539 | if ($cat_id == -4 || $cat_id == -2) { | |
a42c55f0 | 540 | $counters = getLabelCounters(true); |
04f60eb7 AD |
541 | |
542 | foreach (array_values($counters) as $cv) { | |
543 | ||
544 | $unread = $cv["counter"]; | |
545 | ||
546 | if ($unread || !$unread_only) { | |
547 | ||
548 | $row = array( | |
1d3cbe31 | 549 | "id" => (int) $cv["id"], |
04f60eb7 AD |
550 | "title" => $cv["description"], |
551 | "unread" => $cv["counter"], | |
552 | "cat_id" => -2, | |
553 | ); | |
554 | ||
555 | array_push($feeds, $row); | |
556 | } | |
557 | } | |
558 | } | |
559 | ||
560 | /* Virtual feeds */ | |
561 | ||
562 | if ($cat_id == -4 || $cat_id == -1) { | |
563 | foreach (array(-1, -2, -3, -4, -6, 0) as $i) { | |
a42c55f0 | 564 | $unread = getFeedUnread($i); |
04f60eb7 AD |
565 | |
566 | if ($unread || !$unread_only) { | |
a42c55f0 | 567 | $title = getFeedTitle($i); |
04f60eb7 AD |
568 | |
569 | $row = array( | |
570 | "id" => $i, | |
571 | "title" => $title, | |
572 | "unread" => $unread, | |
573 | "cat_id" => -1, | |
574 | ); | |
575 | array_push($feeds, $row); | |
576 | } | |
577 | ||
578 | } | |
579 | } | |
580 | ||
581 | /* Child cats */ | |
582 | ||
583 | if ($include_nested && $cat_id) { | |
a42c55f0 | 584 | $result = db_query("SELECT |
04f60eb7 AD |
585 | id, title FROM ttrss_feed_categories |
586 | WHERE parent_cat = '$cat_id' AND owner_uid = " . $_SESSION["uid"] . | |
587 | " ORDER BY id, title"); | |
588 | ||
589 | while ($line = db_fetch_assoc($result)) { | |
a42c55f0 AD |
590 | $unread = getFeedUnread($line["id"], true) + |
591 | getCategoryChildrenUnread($line["id"]); | |
04f60eb7 AD |
592 | |
593 | if ($unread || !$unread_only) { | |
594 | $row = array( | |
1d3cbe31 | 595 | "id" => (int) $line["id"], |
04f60eb7 AD |
596 | "title" => $line["title"], |
597 | "unread" => $unread, | |
598 | "is_cat" => true, | |
599 | ); | |
600 | array_push($feeds, $row); | |
601 | } | |
602 | } | |
603 | } | |
604 | ||
605 | /* Real feeds */ | |
606 | ||
607 | if ($limit) { | |
608 | $limit_qpart = "LIMIT $limit OFFSET $offset"; | |
609 | } else { | |
610 | $limit_qpart = ""; | |
611 | } | |
612 | ||
613 | if ($cat_id == -4 || $cat_id == -3) { | |
a42c55f0 | 614 | $result = db_query("SELECT |
04f60eb7 AD |
615 | id, feed_url, cat_id, title, order_id, ". |
616 | SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated | |
617 | FROM ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"] . | |
618 | " ORDER BY cat_id, title " . $limit_qpart); | |
619 | } else { | |
620 | ||
621 | if ($cat_id) | |
622 | $cat_qpart = "cat_id = '$cat_id'"; | |
623 | else | |
624 | $cat_qpart = "cat_id IS NULL"; | |
625 | ||
a42c55f0 | 626 | $result = db_query("SELECT |
04f60eb7 AD |
627 | id, feed_url, cat_id, title, order_id, ". |
628 | SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated | |
629 | FROM ttrss_feeds WHERE | |
630 | $cat_qpart AND owner_uid = " . $_SESSION["uid"] . | |
631 | " ORDER BY cat_id, title " . $limit_qpart); | |
632 | } | |
633 | ||
634 | while ($line = db_fetch_assoc($result)) { | |
635 | ||
a42c55f0 | 636 | $unread = getFeedUnread($line["id"]); |
04f60eb7 AD |
637 | |
638 | $has_icon = feed_has_icon($line['id']); | |
639 | ||
640 | if ($unread || !$unread_only) { | |
641 | ||
642 | $row = array( | |
643 | "feed_url" => $line["feed_url"], | |
644 | "title" => $line["title"], | |
645 | "id" => (int)$line["id"], | |
646 | "unread" => (int)$unread, | |
647 | "has_icon" => $has_icon, | |
648 | "cat_id" => (int)$line["cat_id"], | |
ef3da31c | 649 | "last_updated" => (int) strtotime($line["last_updated"]), |
04f60eb7 AD |
650 | "order_id" => (int) $line["order_id"], |
651 | ); | |
652 | ||
653 | array_push($feeds, $row); | |
654 | } | |
655 | } | |
656 | ||
657 | return $feeds; | |
658 | } | |
659 | ||
7b55001e AD |
660 | /** |
661 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) | |
662 | */ | |
a42c55f0 | 663 | static function api_get_headlines($feed_id, $limit, $offset, |
04f60eb7 AD |
664 | $filter, $is_cat, $show_excerpt, $show_content, $view_mode, $order, |
665 | $include_attachments, $since_id, | |
5c784e70 | 666 | $search = "", $include_nested = false, $sanitize_content = true, |
19e47ad6 | 667 | $force_update = false, $excerpt_length = 100, $check_first_id = false, $skip_first_id_check = false) { |
4baa1afa AD |
668 | |
669 | if ($force_update && $feed_id > 0 && is_numeric($feed_id)) { | |
670 | // Update the feed if required with some basic flood control | |
671 | ||
672 | $result = db_query( | |
673 | "SELECT cache_images,".SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated | |
674 | FROM ttrss_feeds WHERE id = '$feed_id'"); | |
675 | ||
676 | if (db_num_rows($result) != 0) { | |
677 | $last_updated = strtotime(db_fetch_result($result, 0, "last_updated")); | |
678 | $cache_images = sql_bool_to_bool(db_fetch_result($result, 0, "cache_images")); | |
679 | ||
680 | if (!$cache_images && time() - $last_updated > 120) { | |
681 | include "rssfuncs.php"; | |
7b55001e | 682 | update_rss_feed($feed_id, true); |
4baa1afa AD |
683 | } else { |
684 | db_query("UPDATE ttrss_feeds SET last_updated = '1970-01-01', last_update_started = '1970-01-01' | |
685 | WHERE id = '$feed_id'"); | |
686 | } | |
687 | } | |
688 | } | |
04f60eb7 | 689 | |
f5a0fb8b | 690 | /*$qfh_ret = queryFeedHeadlines($feed_id, $limit, |
9090b874 | 691 | $view_mode, $is_cat, $search, false, |
f5a0fb8b AD |
692 | $order, $offset, 0, false, $since_id, $include_nested);*/ |
693 | ||
694 | //function queryFeedHeadlines($feed, $limit, | |
695 | // $view_mode, $cat_view, $search, $search_mode, | |
696 | // $override_order = false, $offset = 0, $owner_uid = 0, $filter = false, $since_id = 0, $include_children = false, | |
697 | // $ignore_vfeed_group = false, $override_strategy = false, $override_vfeed = false, $start_ts = false, $check_top_id = false) { | |
698 | ||
699 | $params = array( | |
700 | "feed" => $feed_id, | |
701 | "limit" => $limit, | |
702 | "view_mode" => $view_mode, | |
703 | "cat_view" => $is_cat, | |
704 | "search" => $search, | |
705 | "override_order" => $order, | |
706 | "offset" => $offset, | |
707 | "since_id" => $since_id, | |
708 | "include_children" => $include_nested, | |
19e47ad6 AD |
709 | "check_first_id" => $check_first_id, |
710 | "skip_first_id_check" => $skip_first_id_check | |
f5a0fb8b AD |
711 | ); |
712 | ||
713 | $qfh_ret = queryFeedHeadlines($params); | |
714 | ||
34440201 | 715 | $result = $qfh_ret[0]; |
04f60eb7 | 716 | $feed_title = $qfh_ret[1]; |
48fefe2f | 717 | $first_id = $qfh_ret[6]; |
04f60eb7 AD |
718 | |
719 | $headlines = array(); | |
b0ce3d33 AD |
720 | |
721 | $headlines_header = array( | |
722 | 'id' => $feed_id, | |
48fefe2f | 723 | 'first_id' => $first_id, |
b0ce3d33 | 724 | 'is_cat' => $is_cat); |
4f2a2ca9 | 725 | |
ec57104d | 726 | if (!is_numeric($result)) { |
34440201 AD |
727 | while ($line = db_fetch_assoc($result)) { |
728 | $line["content_preview"] = truncate_string(strip_tags($line["content"]), $excerpt_length); | |
729 | foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_QUERY_HEADLINES) as $p) { | |
730 | $line = $p->hook_query_headlines($line, $excerpt_length, true); | |
731 | } | |
04f60eb7 | 732 | |
34440201 AD |
733 | $is_updated = ($line["last_read"] == "" && |
734 | ($line["unread"] != "t" && $line["unread"] != "1")); | |
f71a669b | 735 | |
34440201 | 736 | $tags = explode(",", $line["tag_cache"]); |
f71a669b | 737 | |
34440201 AD |
738 | $label_cache = $line["label_cache"]; |
739 | $labels = false; | |
f71a669b AD |
740 | |
741 | if ($label_cache) { | |
34440201 AD |
742 | $label_cache = json_decode($label_cache, true); |
743 | ||
744 | if ($label_cache) { | |
745 | if ($label_cache["no-labels"] == 1) | |
746 | $labels = array(); | |
747 | else | |
748 | $labels = $label_cache; | |
749 | } | |
f71a669b | 750 | } |
f71a669b | 751 | |
34440201 | 752 | if (!is_array($labels)) $labels = get_article_labels($line["id"]); |
04f60eb7 | 753 | |
34440201 AD |
754 | //if (!$tags) $tags = get_article_tags($line["id"]); |
755 | //if (!$labels) $labels = get_article_labels($line["id"]); | |
04f60eb7 | 756 | |
34440201 | 757 | $headline_row = array( |
04f60eb7 | 758 | "id" => (int)$line["id"], |
18186149 | 759 | "guid" => $line["guid"], |
04f60eb7 AD |
760 | "unread" => sql_bool_to_bool($line["unread"]), |
761 | "marked" => sql_bool_to_bool($line["marked"]), | |
762 | "published" => sql_bool_to_bool($line["published"]), | |
34440201 | 763 | "updated" => (int)strtotime($line["updated"]), |
04f60eb7 AD |
764 | "is_updated" => $is_updated, |
765 | "title" => $line["title"], | |
766 | "link" => $line["link"], | |
767 | "feed_id" => $line["feed_id"], | |
768 | "tags" => $tags, | |
769 | ); | |
770 | ||
34440201 AD |
771 | if ($include_attachments) |
772 | $headline_row['attachments'] = get_article_enclosures( | |
773 | $line['id']); | |
04f60eb7 | 774 | |
34440201 AD |
775 | if ($show_excerpt) |
776 | $headline_row["excerpt"] = $line["content_preview"]; | |
04f60eb7 | 777 | |
34440201 | 778 | if ($show_content) { |
04f60eb7 | 779 | |
34440201 AD |
780 | if ($sanitize_content) { |
781 | $headline_row["content"] = sanitize( | |
782 | $line["content"], | |
783 | sql_bool_to_bool($line['hide_images']), | |
784 | false, $line["site_url"], false, $line["id"]); | |
785 | } else { | |
786 | $headline_row["content"] = $line["content"]; | |
787 | } | |
04f60eb7 | 788 | } |
04f60eb7 | 789 | |
34440201 AD |
790 | // unify label output to ease parsing |
791 | if ($labels["no-labels"] == 1) $labels = array(); | |
04f60eb7 | 792 | |
34440201 | 793 | $headline_row["labels"] = $labels; |
04f60eb7 | 794 | |
34440201 AD |
795 | $headline_row["feed_title"] = $line["feed_title"] ? $line["feed_title"] : |
796 | $feed_title; | |
04f60eb7 | 797 | |
34440201 AD |
798 | $headline_row["comments_count"] = (int)$line["num_comments"]; |
799 | $headline_row["comments_link"] = $line["comments"]; | |
04f60eb7 | 800 | |
34440201 | 801 | $headline_row["always_display_attachments"] = sql_bool_to_bool($line["always_display_enclosures"]); |
04f60eb7 | 802 | |
34440201 | 803 | $headline_row["author"] = $line["author"]; |
4f2a2ca9 | 804 | |
34440201 AD |
805 | $headline_row["score"] = (int)$line["score"]; |
806 | $headline_row["note"] = $line["note"]; | |
807 | $headline_row["lang"] = $line["lang"]; | |
583dbc56 | 808 | |
34440201 AD |
809 | foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_RENDER_ARTICLE_API) as $p) { |
810 | $headline_row = $p->hook_render_article_api(array("headline" => $headline_row)); | |
811 | } | |
b6604c96 | 812 | |
34440201 AD |
813 | array_push($headlines, $headline_row); |
814 | } | |
5c784e70 | 815 | } else if (is_numeric($result) && $result == -1) { |
48fefe2f | 816 | $headlines_header['first_id_changed'] = true; |
04f60eb7 AD |
817 | } |
818 | ||
5c784e70 | 819 | return array($headlines, $headlines_header); |
04f60eb7 AD |
820 | } |
821 | ||
efc6553d | 822 | function unsubscribeFeed() { |
d9c85e0f | 823 | $feed_id = (int) $this->dbh->escape_string($_REQUEST["feed_id"]); |
efc6553d | 824 | |
d9c85e0f | 825 | $result = $this->dbh->query("SELECT id FROM ttrss_feeds WHERE |
efc6553d AD |
826 | id = '$feed_id' AND owner_uid = ".$_SESSION["uid"]); |
827 | ||
d9c85e0f | 828 | if ($this->dbh->num_rows($result) != 0) { |
a42c55f0 | 829 | Pref_Feeds::remove_feed($feed_id, $_SESSION["uid"]); |
6f7798b6 | 830 | $this->wrap(self::STATUS_OK, array("status" => "OK")); |
efc6553d | 831 | } else { |
6f7798b6 | 832 | $this->wrap(self::STATUS_ERR, array("error" => "FEED_NOT_FOUND")); |
efc6553d AD |
833 | } |
834 | } | |
835 | ||
836 | function subscribeToFeed() { | |
d9c85e0f AD |
837 | $feed_url = $this->dbh->escape_string($_REQUEST["feed_url"]); |
838 | $category_id = (int) $this->dbh->escape_string($_REQUEST["category_id"]); | |
839 | $login = $this->dbh->escape_string($_REQUEST["login"]); | |
840 | $password = $this->dbh->escape_string($_REQUEST["password"]); | |
efc6553d AD |
841 | |
842 | if ($feed_url) { | |
6f7798b6 | 843 | $rc = subscribe_to_feed($feed_url, $category_id, $login, $password); |
efc6553d | 844 | |
6f7798b6 | 845 | $this->wrap(self::STATUS_OK, array("status" => $rc)); |
efc6553d | 846 | } else { |
6f7798b6 | 847 | $this->wrap(self::STATUS_ERR, array("error" => 'INCORRECT_USAGE')); |
efc6553d AD |
848 | } |
849 | } | |
850 | ||
0bb5833b | 851 | function getFeedTree() { |
b3575bd8 | 852 | $include_empty = sql_bool_to_bool($_REQUEST['include_empty']); |
66ca7c30 | 853 | |
a42c55f0 | 854 | $pf = new Pref_Feeds($_REQUEST); |
0bb5833b AD |
855 | |
856 | $_REQUEST['mode'] = 2; | |
66ca7c30 | 857 | $_REQUEST['force_show_empty'] = $include_empty; |
0bb5833b AD |
858 | |
859 | if ($pf){ | |
860 | $data = $pf->makefeedtree(); | |
6f7798b6 | 861 | $this->wrap(self::STATUS_OK, array("categories" => $data)); |
0bb5833b | 862 | } else { |
6f7798b6 | 863 | $this->wrap(self::STATUS_ERR, array("error" => |
0bb5833b AD |
864 | 'UNABLE_TO_INSTANTIATE_OBJECT')); |
865 | } | |
866 | ||
867 | } | |
c0a08063 | 868 | |
dc5a8a21 | 869 | // only works for labels or uncategorized for the time being |
c0a08063 AD |
870 | private function isCategoryEmpty($id) { |
871 | ||
872 | if ($id == -2) { | |
d9c85e0f | 873 | $result = $this->dbh->query("SELECT COUNT(*) AS count FROM ttrss_labels2 |
c0a08063 AD |
874 | WHERE owner_uid = " . $_SESSION["uid"]); |
875 | ||
d9c85e0f | 876 | return $this->dbh->fetch_result($result, 0, "count") == 0; |
c0a08063 | 877 | |
dc5a8a21 | 878 | } else if ($id == 0) { |
d9c85e0f | 879 | $result = $this->dbh->query("SELECT COUNT(*) AS count FROM ttrss_feeds |
dc5a8a21 AD |
880 | WHERE cat_id IS NULL AND owner_uid = " . $_SESSION["uid"]); |
881 | ||
d9c85e0f | 882 | return $this->dbh->fetch_result($result, 0, "count") == 0; |
dc5a8a21 | 883 | |
c0a08063 AD |
884 | } |
885 | ||
886 | return false; | |
887 | } | |
888 | ||
889 | ||
891df346 | 890 | } |