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