]> git.wh0rd.org - tt-rss.git/blame - classes/api.php
feeds: use PDO
[tt-rss.git] / classes / api.php
CommitLineData
de8260cb 1<?php
de8260cb
AD
2class 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
3467e1fd 52 $login = $_REQUEST["user"];
de8260cb
AD
53 $password = $_REQUEST["password"];
54 $password_base64 = base64_decode($_REQUEST["password"]);
55
56 if (SINGLE_USER_MODE) $login = "admin";
57
3467e1fd
AD
58 $sth = $this->pdo->prepare("SELECT id FROM ttrss_users WHERE login = ?");
59 $sth->execute([$login]);
de8260cb 60
3467e1fd
AD
61 if ($row = $sth->fetch()) {
62 $uid = $row["id"];
de8260cb
AD
63 } else {
64 $uid = 0;
65 }
66
67 if (!$uid) {
6f7798b6 68 $this->wrap(self::STATUS_ERR, array("error" => "LOGIN_ERROR"));
de8260cb
AD
69 return;
70 }
71
a42c55f0
AD
72 if (get_pref("ENABLE_API_ACCESS", $uid)) {
73 if (authenticate_user($login, $password)) { // try login with normal password
6f7798b6 74 $this->wrap(self::STATUS_OK, array("session_id" => session_id(),
5ba4ebc6 75 "api_level" => self::API_LEVEL));
a42c55f0 76 } else if (authenticate_user($login, $password_base64)) { // else try with base64_decoded password
6f7798b6 77 $this->wrap(self::STATUS_OK, array("session_id" => session_id(),
5ba4ebc6 78 "api_level" => self::API_LEVEL));
de8260cb 79 } else { // else we are not logged in
a2108ee9 80 user_error("Failed login attempt for $login from {$_SERVER['REMOTE_ADDR']}", E_USER_WARNING);
6f7798b6 81 $this->wrap(self::STATUS_ERR, array("error" => "LOGIN_ERROR"));
de8260cb
AD
82 }
83 } else {
6f7798b6 84 $this->wrap(self::STATUS_ERR, array("error" => "API_DISABLED"));
de8260cb
AD
85 }
86
87 }
88
89 function logout() {
90 logout_user();
6f7798b6 91 $this->wrap(self::STATUS_OK, array("status" => "OK"));
de8260cb
AD
92 }
93
94 function isLoggedIn() {
6f7798b6 95 $this->wrap(self::STATUS_OK, array("status" => $_SESSION["uid"] != ''));
de8260cb
AD
96 }
97
98 function getUnread() {
3467e1fd
AD
99 $feed_id = $_REQUEST["feed_id"];
100 $is_cat = $_REQUEST["is_cat"];
de8260cb
AD
101
102 if ($feed_id) {
6f7798b6 103 $this->wrap(self::STATUS_OK, array("unread" => getFeedUnread($feed_id, $is_cat)));
de8260cb 104 } else {
a230bf88 105 $this->wrap(self::STATUS_OK, array("unread" => Feeds::getGlobalUnread()));
de8260cb
AD
106 }
107 }
108
109 /* Method added for ttrss-reader for Android */
110 function getCounters() {
65af3b2c 111 $this->wrap(self::STATUS_OK, Counters::getAllCounters());
de8260cb
AD
112 }
113
114 function getFeeds() {
3467e1fd 115 $cat_id = $_REQUEST["cat_id"];
9955a134 116 $unread_only = sql_bool_to_bool($_REQUEST["unread_only"]);
3467e1fd
AD
117 $limit = (int) $_REQUEST["limit"];
118 $offset = (int) $_REQUEST["offset"];
9955a134 119 $include_nested = sql_bool_to_bool($_REQUEST["include_nested"]);
de8260cb 120
a42c55f0 121 $feeds = $this->api_get_feeds($cat_id, $unread_only, $limit, $offset, $include_nested);
de8260cb 122
6f7798b6 123 $this->wrap(self::STATUS_OK, $feeds);
de8260cb
AD
124 }
125
126 function getCategories() {
9955a134
AD
127 $unread_only = sql_bool_to_bool($_REQUEST["unread_only"]);
128 $enable_nested = sql_bool_to_bool($_REQUEST["enable_nested"]);
b3575bd8 129 $include_empty = sql_bool_to_bool($_REQUEST['include_empty']);
de8260cb
AD
130
131 // TODO do not return empty categories, return Uncategorized and standard virtual cats
132
48646336
AD
133 if ($enable_nested)
134 $nested_qpart = "parent_cat IS NULL";
135 else
136 $nested_qpart = "true";
137
3467e1fd 138 $sth = $this->pdo->prepare("SELECT
d49dfa38
AD
139 id, title, order_id, (SELECT COUNT(id) FROM
140 ttrss_feeds WHERE
7be3fcd5
AD
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
d49dfa38 145 FROM ttrss_feed_categories
3467e1fd
AD
146 WHERE $nested_qpart AND owner_uid = ?");
147 $sth->execute([$_SESSION['uid']]);
de8260cb
AD
148
149 $cats = array();
150
3467e1fd 151 while ($line = $sth->fetch()) {
66ca7c30 152 if ($include_empty || $line["num_feeds"] > 0 || $line["num_cats"] > 0) {
a42c55f0 153 $unread = getFeedUnread($line["id"], true);
d49dfa38
AD
154
155 if ($enable_nested)
86a8351c 156 $unread += Feeds::getCategoryChildrenUnread($line["id"]);
d49dfa38
AD
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 }
de8260cb
AD
165 }
166 }
167
168 foreach (array(-2,-1,0) as $cat_id) {
c0a08063 169 if ($include_empty || !$this->isCategoryEmpty($cat_id)) {
a42c55f0 170 $unread = getFeedUnread($cat_id, true);
de8260cb 171
c0a08063
AD
172 if ($unread || !$unread_only) {
173 array_push($cats, array("id" => $cat_id,
a230bf88 174 "title" => Feeds::getCategoryTitle($cat_id),
c0a08063
AD
175 "unread" => $unread));
176 }
de8260cb
AD
177 }
178 }
179
6f7798b6 180 $this->wrap(self::STATUS_OK, $cats);
de8260cb
AD
181 }
182
183 function getHeadlines() {
3467e1fd 184 $feed_id = $_REQUEST["feed_id"];
8aa3becc 185 if ($feed_id != "") {
de8260cb 186
96ac72bc
AD
187 if (is_numeric($feed_id)) $feed_id = (int) $feed_id;
188
3467e1fd 189 $limit = (int)$_REQUEST["limit"];
1a740cf6 190
6421b429 191 if (!$limit || $limit >= 200) $limit = 200;
1a740cf6 192
3467e1fd
AD
193 $offset = (int)$_REQUEST["skip"];
194 $filter = $_REQUEST["filter"];
9955a134
AD
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"]);
de8260cb 198 /* all_articles, unread, adaptive, marked, updated */
3467e1fd 199 $view_mode = $_REQUEST["view_mode"];
9955a134 200 $include_attachments = sql_bool_to_bool($_REQUEST["include_attachments"]);
3467e1fd 201 $since_id = (int)$_REQUEST["since_id"];
9955a134 202 $include_nested = sql_bool_to_bool($_REQUEST["include_nested"]);
bd3c6723
AD
203 $sanitize_content = !isset($_REQUEST["sanitize"]) ||
204 sql_bool_to_bool($_REQUEST["sanitize"]);
4baa1afa 205 $force_update = sql_bool_to_bool($_REQUEST["force_update"]);
9997b38e 206 $has_sandbox = sql_bool_to_bool($_REQUEST["has_sandbox"]);
3467e1fd
AD
207 $excerpt_length = (int)$_REQUEST["excerpt_length"];
208 $check_first_id = (int)$_REQUEST["check_first_id"];
5c784e70 209 $include_header = sql_bool_to_bool($_REQUEST["include_header"]);
9997b38e
AD
210
211 $_SESSION['hasSandbox'] = $has_sandbox;
ffd07864 212
19e47ad6
AD
213 $skip_first_id_check = false;
214
0bbd1414 215 $override_order = false;
216 switch ($_REQUEST["order_by"]) {
273c33e5 217 case "title":
81d96c0d 218 $override_order = "ttrss_entries.title, date_entered, updated";
273c33e5 219 break;
0bbd1414 220 case "date_reverse":
e9687f67 221 $override_order = "score DESC, date_entered, updated";
19e47ad6 222 $skip_first_id_check = true;
0bbd1414 223 break;
224 case "feed_dates":
225 $override_order = "updated DESC";
226 break;
227 }
ffd07864 228
3e4af5b0
AD
229 /* do not rely on params below */
230
3467e1fd 231 $search = $_REQUEST["search"];
3e4af5b0 232
5c784e70 233 list($headlines, $headlines_header) = $this->api_get_headlines($feed_id, $limit, $offset,
0bbd1414 234 $filter, $is_cat, $show_excerpt, $show_content, $view_mode, $override_order,
9090b874 235 $include_attachments, $since_id, $search,
19e47ad6 236 $include_nested, $sanitize_content, $force_update, $excerpt_length, $check_first_id, $skip_first_id_check);
de8260cb 237
5c784e70
AD
238 if ($include_header) {
239 $this->wrap(self::STATUS_OK, array($headlines_header, $headlines));
240 } else {
241 $this->wrap(self::STATUS_OK, $headlines);
242 }
de8260cb 243 } else {
6f7798b6 244 $this->wrap(self::STATUS_ERR, array("error" => 'INCORRECT_USAGE'));
de8260cb
AD
245 }
246 }
247
248 function updateArticle() {
3467e1fd
AD
249 $article_ids = explode(",", $_REQUEST["article_ids"]);
250 $mode = (int) $_REQUEST["mode"];
251 $data = $_REQUEST["data"];
252 $field_raw = (int)$_REQUEST["field"];
de8260cb
AD
253
254 $field = "";
255 $set_to = "";
256
257 switch ($field_raw) {
258 case 0:
259 $field = "marked";
7873d588 260 $additional_fields = ",last_marked = NOW()";
de8260cb
AD
261 break;
262 case 1:
263 $field = "published";
7873d588 264 $additional_fields = ",last_published = NOW()";
de8260cb
AD
265 break;
266 case 2:
267 $field = "unread";
7873d588 268 $additional_fields = ",last_read = NOW()";
de8260cb
AD
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
3467e1fd 286 if ($field == "note") $set_to = $this->pdo->quote($data);
de8260cb
AD
287
288 if ($field && $set_to && count($article_ids) > 0) {
289
3467e1fd 290 $article_qmarks = arr_qmarks($article_ids);
de8260cb 291
3467e1fd
AD
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']]));
de8260cb 296
3467e1fd 297 $num_updated = $sth->rowCount();
de8260cb
AD
298
299 if ($num_updated > 0 && $field == "unread") {
3467e1fd 300 $sth = $this->pdo->query("SELECT DISTINCT feed_id FROM ttrss_user_entries
9652fa6b
AD
301 WHERE ref_id IN ($article_qmarks)");
302 $sth->execute($article_ids);
de8260cb 303
3467e1fd 304 while ($line = $sth->fetch()) {
2ed0d6c4 305 CCache::update($line["feed_id"], $_SESSION["uid"]);
de8260cb
AD
306 }
307 }
308
6f7798b6 309 $this->wrap(self::STATUS_OK, array("status" => "OK",
de8260cb
AD
310 "updated" => $num_updated));
311
312 } else {
6f7798b6 313 $this->wrap(self::STATUS_ERR, array("error" => 'INCORRECT_USAGE'));
de8260cb
AD
314 }
315
316 }
317
318 function getArticle() {
319
3467e1fd 320 $article_ids = explode(",", $_REQUEST["article_id"]);
73c77ab0
J
321 $sanitize_content = !isset($_REQUEST["sanitize"]) ||
322 sql_bool_to_bool($_REQUEST["sanitize"]);
de8260cb 323
3467e1fd 324 if ($article_ids) {
de8260cb 325
3467e1fd
AD
326 $article_qmarks = arr_qmarks($article_ids);
327
328 $sth = $this->pdo->prepare("SELECT id,guid,title,link,content,feed_id,comments,int_id,
429ff9af 329 marked,unread,published,score,note,lang,
6f81395d 330 ".SUBSTRING_FOR_DATE."(updated,1,16) as updated,
73c77ab0
J
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
6f81395d 334 FROM ttrss_entries,ttrss_user_entries
3467e1fd 335 WHERE id IN ($article_qmarks) AND ref_id = id AND owner_uid = ?");
de8260cb 336
3467e1fd 337 $sth->execute(array_merge($article_ids, [$_SESSION['uid']]));
de8260cb 338
6f81395d 339 $articles = array();
de8260cb 340
3467e1fd
AD
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 }
6f81395d 373
3467e1fd
AD
374 foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_RENDER_ARTICLE_API) as $p) {
375 $article = $p->hook_render_article_api(array("article" => $article));
376 }
6f81395d 377
3467e1fd 378 array_push($articles, $article);
de8260cb 379
6f81395d 380 }
de8260cb 381
6f81395d
AD
382 $this->wrap(self::STATUS_OK, $articles);
383 } else {
384 $this->wrap(self::STATUS_ERR, array("error" => 'INCORRECT_USAGE'));
385 }
de8260cb
AD
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
3467e1fd
AD
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();
de8260cb 399
3467e1fd 400 $config["num_feeds"] = $row["cf"];
de8260cb 401
6f7798b6 402 $this->wrap(self::STATUS_OK, $config);
de8260cb
AD
403 }
404
405 function updateFeed() {
3467e1fd 406 $feed_id = (int) $_REQUEST["feed_id"];
de8260cb 407
29c92d7b 408 if (!ini_get("open_basedir")) {
e6c886bf 409 RSSUtils::update_rss_feed($feed_id);
29c92d7b 410 }
de8260cb 411
6f7798b6 412 $this->wrap(self::STATUS_OK, array("status" => "OK"));
de8260cb
AD
413 }
414
415 function catchupFeed() {
3467e1fd
AD
416 $feed_id = $_REQUEST["feed_id"];
417 $is_cat = $_REQUEST["is_cat"];
de8260cb 418
86a8351c 419 Feeds::catchup_feed($feed_id, $is_cat);
de8260cb 420
6f7798b6 421 $this->wrap(self::STATUS_OK, array("status" => "OK"));
de8260cb
AD
422 }
423
424 function getPref() {
3467e1fd 425 $pref_name = $_REQUEST["pref_name"];
de8260cb 426
6f7798b6 427 $this->wrap(self::STATUS_OK, array("value" => get_pref($pref_name)));
de8260cb
AD
428 }
429
ea1c2903 430 function getLabels() {
ea1c2903
AD
431 $article_id = (int)$_REQUEST['article_id'];
432
433 $rv = array();
434
3467e1fd 435 $sth = $this->pdo->prepare("SELECT id, caption, fg_color, bg_color
ea1c2903 436 FROM ttrss_labels2
3467e1fd
AD
437 WHERE owner_uid = ? ORDER BY caption");
438 $sth->execute([$_SESSION['uid']]);
ea1c2903
AD
439
440 if ($article_id)
4a0da0e5 441 $article_labels = Article::get_article_labels($article_id);
ea1c2903
AD
442 else
443 $article_labels = array();
444
3467e1fd 445 while ($line = $sth->fetch()) {
ea1c2903
AD
446
447 $checked = false;
448 foreach ($article_labels as $al) {
7c9b5a3f 449 if (Labels::feed_to_label_id($al[0]) == $line['id']) {
ea1c2903
AD
450 $checked = true;
451 break;
452 }
453 }
454
455 array_push($rv, array(
7c9b5a3f 456 "id" => (int)Labels::label_to_feed_id($line['id']),
ea1c2903
AD
457 "caption" => $line['caption'],
458 "fg_color" => $line['fg_color'],
459 "bg_color" => $line['bg_color'],
460 "checked" => $checked));
461 }
462
6f7798b6 463 $this->wrap(self::STATUS_OK, $rv);
ea1c2903
AD
464 }
465
396bfdf9
AD
466 function setArticleLabel() {
467
3467e1fd
AD
468 $article_ids = explode(",", $_REQUEST["article_ids"]);
469 $label_id = (int) $_REQUEST['label_id'];
91f49ba1 470 $assign = sql_bool_to_bool($_REQUEST['assign']);
396bfdf9 471
3467e1fd 472 $label = Labels::find_caption(Labels::feed_to_label_id($label_id), $_SESSION["uid"]);
396bfdf9
AD
473
474 $num_updated = 0;
475
476 if ($label) {
477
478 foreach ($article_ids as $id) {
479
480 if ($assign)
7c9b5a3f 481 Labels::add_article($id, $label, $_SESSION["uid"]);
396bfdf9 482 else
7c9b5a3f 483 Labels::remove_article($id, $label, $_SESSION["uid"]);
396bfdf9
AD
484
485 ++$num_updated;
486
487 }
488 }
489
6f7798b6 490 $this->wrap(self::STATUS_OK, array("status" => "OK",
396bfdf9
AD
491 "updated" => $num_updated));
492
493 }
494
79f9bef7 495 function index($method) {
1ffe3391 496 $plugin = PluginHost::getInstance()->get_api_method(strtolower($method));
79f9bef7
AD
497
498 if ($plugin && method_exists($plugin, $method)) {
499 $reply = $plugin->$method();
500
6f7798b6 501 $this->wrap($reply[0], $reply[1]);
79f9bef7
AD
502
503 } else {
6f7798b6 504 $this->wrap(self::STATUS_ERR, array("error" => 'UNKNOWN_METHOD', "method" => $method));
79f9bef7 505 }
de8260cb
AD
506 }
507
8361e724 508 function shareToPublished() {
3467e1fd
AD
509 $title = strip_tags($_REQUEST["title"]);
510 $url = strip_tags($_REQUEST["url"]);
511 $content = strip_tags($_REQUEST["content"]);
8361e724 512
a42c55f0 513 if (Article::create_published_article($title, $url, $content, "", $_SESSION["uid"])) {
6f7798b6 514 $this->wrap(self::STATUS_OK, array("status" => 'OK'));
8361e724 515 } else {
6f7798b6 516 $this->wrap(self::STATUS_ERR, array("error" => 'Publishing failed'));
8361e724
AD
517 }
518 }
04f60eb7 519
a42c55f0 520 static function api_get_feeds($cat_id, $unread_only, $limit, $offset, $include_nested = false) {
04f60eb7
AD
521
522 $feeds = array();
523
3467e1fd
AD
524 $pdo = Db::pdo();
525
526 $limit = (int) $limit;
527 $offset = (int) $offset;
528 $cat_id = (int) $cat_id;
529
04f60eb7
AD
530 /* Labels */
531
532 if ($cat_id == -4 || $cat_id == -2) {
65af3b2c 533 $counters = Counters::getLabelCounters(true);
04f60eb7
AD
534
535 foreach (array_values($counters) as $cv) {
536
537 $unread = $cv["counter"];
538
539 if ($unread || !$unread_only) {
540
541 $row = array(
1d3cbe31 542 "id" => (int) $cv["id"],
04f60eb7
AD
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) {
a42c55f0 557 $unread = getFeedUnread($i);
04f60eb7
AD
558
559 if ($unread || !$unread_only) {
86a8351c 560 $title = Feeds::getFeedTitle($i);
04f60eb7
AD
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) {
3467e1fd 577 $sth = $pdo->prepare("SELECT
c053b976 578 id, title, order_id FROM ttrss_feed_categories
3467e1fd
AD
579 WHERE parent_cat = ? AND owner_uid = ? ORDER BY id, title");
580
581 $sth->execute([$cat_id, $_SESSION['uid']]);
04f60eb7 582
3467e1fd 583 while ($line = $sth->fetch()) {
a42c55f0 584 $unread = getFeedUnread($line["id"], true) +
86a8351c 585 Feeds::getCategoryChildrenUnread($line["id"]);
04f60eb7
AD
586
587 if ($unread || !$unread_only) {
588 $row = array(
1d3cbe31 589 "id" => (int) $line["id"],
04f60eb7
AD
590 "title" => $line["title"],
591 "unread" => $unread,
592 "is_cat" => true,
c053b976 593 "order_id" => (int) $line["order_id"]
04f60eb7
AD
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) {
3467e1fd 609 $sth = $pdo->prepare("SELECT
04f60eb7
AD
610 id, feed_url, cat_id, title, order_id, ".
611 SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated
3467e1fd
AD
612 FROM ttrss_feeds WHERE owner_uid = ?
613 ORDER BY cat_id, title " . $limit_qpart);
614 $sth->execute([$_SESSION['uid']]);
04f60eb7 615
3467e1fd 616 } else {
04f60eb7 617
3467e1fd 618 $sth = $pdo->prepare("SELECT
04f60eb7
AD
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
3467e1fd
AD
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]);
04f60eb7
AD
626 }
627
3467e1fd 628 while ($line = $sth->fetch()) {
04f60eb7 629
a42c55f0 630 $unread = getFeedUnread($line["id"]);
04f60eb7
AD
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"],
ef3da31c 643 "last_updated" => (int) strtotime($line["last_updated"]),
04f60eb7
AD
644 "order_id" => (int) $line["order_id"],
645 );
646
647 array_push($feeds, $row);
648 }
649 }
650
651 return $feeds;
652 }
653
7b55001e
AD
654 /**
655 * @SuppressWarnings(PHPMD.UnusedFormalParameter)
656 */
a42c55f0 657 static function api_get_headlines($feed_id, $limit, $offset,
04f60eb7
AD
658 $filter, $is_cat, $show_excerpt, $show_content, $view_mode, $order,
659 $include_attachments, $since_id,
5c784e70 660 $search = "", $include_nested = false, $sanitize_content = true,
19e47ad6 661 $force_update = false, $excerpt_length = 100, $check_first_id = false, $skip_first_id_check = false) {
4baa1afa 662
3467e1fd
AD
663 $pdo = Db::pdo();
664
4baa1afa
AD
665 if ($force_update && $feed_id > 0 && is_numeric($feed_id)) {
666 // Update the feed if required with some basic flood control
667
3467e1fd 668 $sth = $pdo->prepare(
4baa1afa 669 "SELECT cache_images,".SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated
3467e1fd
AD
670 FROM ttrss_feeds WHERE id = ?");
671 $sth->execute([$feed_id]);
4baa1afa 672
3467e1fd
AD
673 if ($row = $sth->fetch()) {
674 $last_updated = strtotime($row["last_updated"]);
675 $cache_images = sql_bool_to_bool($row["cache_images"]);
4baa1afa
AD
676
677 if (!$cache_images && time() - $last_updated > 120) {
e6c886bf 678 RSSUtils::update_rss_feed($feed_id, true);
4baa1afa 679 } else {
3467e1fd
AD
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]);
4baa1afa
AD
683 }
684 }
685 }
04f60eb7 686
f5a0fb8b
AD
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,
19e47ad6
AD
697 "check_first_id" => $check_first_id,
698 "skip_first_id_check" => $skip_first_id_check
f5a0fb8b
AD
699 );
700
aeb1abed 701 $qfh_ret = Feeds::queryFeedHeadlines($params);
f5a0fb8b 702
34440201 703 $result = $qfh_ret[0];
04f60eb7 704 $feed_title = $qfh_ret[1];
48fefe2f 705 $first_id = $qfh_ret[6];
04f60eb7
AD
706
707 $headlines = array();
b0ce3d33
AD
708
709 $headlines_header = array(
710 'id' => $feed_id,
48fefe2f 711 'first_id' => $first_id,
b0ce3d33 712 'is_cat' => $is_cat);
4f2a2ca9 713
ec57104d 714 if (!is_numeric($result)) {
3467e1fd 715 while ($line = $result->fetch()) {
34440201
AD
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 }
04f60eb7 720
34440201
AD
721 $is_updated = ($line["last_read"] == "" &&
722 ($line["unread"] != "t" && $line["unread"] != "1"));
f71a669b 723
34440201 724 $tags = explode(",", $line["tag_cache"]);
f71a669b 725
34440201
AD
726 $label_cache = $line["label_cache"];
727 $labels = false;
f71a669b
AD
728
729 if ($label_cache) {
34440201
AD
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 }
f71a669b 738 }
f71a669b 739
4a0da0e5 740 if (!is_array($labels)) $labels = Article::get_article_labels($line["id"]);
04f60eb7 741
34440201 742 $headline_row = array(
04f60eb7 743 "id" => (int)$line["id"],
18186149 744 "guid" => $line["guid"],
04f60eb7
AD
745 "unread" => sql_bool_to_bool($line["unread"]),
746 "marked" => sql_bool_to_bool($line["marked"]),
747 "published" => sql_bool_to_bool($line["published"]),
34440201 748 "updated" => (int)strtotime($line["updated"]),
04f60eb7
AD
749 "is_updated" => $is_updated,
750 "title" => $line["title"],
751 "link" => $line["link"],
752 "feed_id" => $line["feed_id"],
753 "tags" => $tags,
754 );
755
34440201 756 if ($include_attachments)
7e5f8d9f 757 $headline_row['attachments'] = Article::get_article_enclosures(
34440201 758 $line['id']);
04f60eb7 759
34440201
AD
760 if ($show_excerpt)
761 $headline_row["excerpt"] = $line["content_preview"];
04f60eb7 762
34440201 763 if ($show_content) {
04f60eb7 764
34440201
AD
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 }
04f60eb7 773 }
04f60eb7 774
34440201
AD
775 // unify label output to ease parsing
776 if ($labels["no-labels"] == 1) $labels = array();
04f60eb7 777
34440201 778 $headline_row["labels"] = $labels;
04f60eb7 779
34440201
AD
780 $headline_row["feed_title"] = $line["feed_title"] ? $line["feed_title"] :
781 $feed_title;
04f60eb7 782
34440201
AD
783 $headline_row["comments_count"] = (int)$line["num_comments"];
784 $headline_row["comments_link"] = $line["comments"];
04f60eb7 785
34440201 786 $headline_row["always_display_attachments"] = sql_bool_to_bool($line["always_display_enclosures"]);
04f60eb7 787
34440201 788 $headline_row["author"] = $line["author"];
4f2a2ca9 789
34440201
AD
790 $headline_row["score"] = (int)$line["score"];
791 $headline_row["note"] = $line["note"];
792 $headline_row["lang"] = $line["lang"];
583dbc56 793
34440201
AD
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 }
b6604c96 797
34440201
AD
798 array_push($headlines, $headline_row);
799 }
5c784e70 800 } else if (is_numeric($result) && $result == -1) {
48fefe2f 801 $headlines_header['first_id_changed'] = true;
04f60eb7
AD
802 }
803
5c784e70 804 return array($headlines, $headlines_header);
04f60eb7
AD
805 }
806
efc6553d 807 function unsubscribeFeed() {
3467e1fd 808 $feed_id = (int) $_REQUEST["feed_id"];
efc6553d 809
3467e1fd
AD
810 $sth = $this->pdo->prepare("SELECT id FROM ttrss_feeds WHERE
811 id = ? AND owner_uid = ?");
812 $sth->execute([$feed_id, $_SESSION['uid']]);
efc6553d 813
3467e1fd 814 if ($row = $sth->fetch()) {
a42c55f0 815 Pref_Feeds::remove_feed($feed_id, $_SESSION["uid"]);
6f7798b6 816 $this->wrap(self::STATUS_OK, array("status" => "OK"));
efc6553d 817 } else {
6f7798b6 818 $this->wrap(self::STATUS_ERR, array("error" => "FEED_NOT_FOUND"));
efc6553d
AD
819 }
820 }
821
822 function subscribeToFeed() {
3467e1fd
AD
823 $feed_url = $_REQUEST["feed_url"];
824 $category_id = (int) $_REQUEST["category_id"];
825 $login = $_REQUEST["login"];
826 $password = $_REQUEST["password"];
efc6553d
AD
827
828 if ($feed_url) {
86a8351c 829 $rc = Feeds::subscribe_to_feed($feed_url, $category_id, $login, $password);
efc6553d 830
6f7798b6 831 $this->wrap(self::STATUS_OK, array("status" => $rc));
efc6553d 832 } else {
6f7798b6 833 $this->wrap(self::STATUS_ERR, array("error" => 'INCORRECT_USAGE'));
efc6553d
AD
834 }
835 }
836
0bb5833b 837 function getFeedTree() {
b3575bd8 838 $include_empty = sql_bool_to_bool($_REQUEST['include_empty']);
66ca7c30 839
a42c55f0 840 $pf = new Pref_Feeds($_REQUEST);
0bb5833b
AD
841
842 $_REQUEST['mode'] = 2;
66ca7c30 843 $_REQUEST['force_show_empty'] = $include_empty;
0bb5833b
AD
844
845 if ($pf){
846 $data = $pf->makefeedtree();
6f7798b6 847 $this->wrap(self::STATUS_OK, array("categories" => $data));
0bb5833b 848 } else {
6f7798b6 849 $this->wrap(self::STATUS_ERR, array("error" =>
0bb5833b
AD
850 'UNABLE_TO_INSTANTIATE_OBJECT'));
851 }
852
853 }
c0a08063 854
dc5a8a21 855 // only works for labels or uncategorized for the time being
c0a08063
AD
856 private function isCategoryEmpty($id) {
857
858 if ($id == -2) {
3467e1fd
AD
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();
c0a08063 863
3467e1fd 864 return $row["count"] == 0;
c0a08063 865
dc5a8a21 866 } else if ($id == 0) {
3467e1fd
AD
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();
dc5a8a21 871
3467e1fd 872 return $row["count"] == 0;
dc5a8a21 873
c0a08063
AD
874 }
875
876 return false;
877 }
878
879
891df346 880}