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