-
Notifications
You must be signed in to change notification settings - Fork 28
/
fever_api.php
960 lines (806 loc) · 32.9 KB
/
fever_api.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
<?php
function param_to_bool($p): bool {
return $p && ($p !== "f" && $p !== "false");
}
class FeverAPI extends Handler {
const API_LEVEL = 3;
const STATUS_OK = 1;
const STATUS_ERR = 0;
// enable if you need some debug output in your tinytinyrss root
const DEBUG = FALSE;
// your user id you need to debug - look it up in your mysql database and set it to a value bigger than 0
const DEBUG_USER = 0;
const PLUGIN_NAME = "Fever";
private $id_hack = FALSE;
// add link in bottom for attached files
private $add_attached_files = TRUE;
// output as xml or json
private $xml;
// find the user in the db with a particular api key
private function setUser(): void
{
$apikey = isset($_REQUEST["api_key"]) ? clean($_REQUEST["api_key"]) : "";
// Login for Mr. Reader
if (strlen($apikey) <= 0 &&
isset($_REQUEST["action"]) &&
clean($_REQUEST["action"]) === "login" &&
isset($_REQUEST["email"])&&
isset($_REQUEST["password"]))
{
$email = $_REQUEST["email"];
$password = $_REQUEST["password"];
$apikey = strtoupper(md5($email . ":" . $password));
setcookie("fever_auth", $apikey, time() + max(SESSION_COOKIE_LIFETIME, 60 * 60 * 24));
}
// override for Mr.Reader when doing some stuff
if (strlen($apikey) <= 0 && isset($_COOKIE["fever_auth"]))
{
$apikey = $_COOKIE['fever_auth'];
}
if (strlen($apikey) > 0)
{
$sth = $this->pdo->prepare("SELECT owner_uid, content FROM ttrss_plugin_storage
WHERE name = ?");
$sth->execute([self::PLUGIN_NAME]);
while ($line = $sth->fetch())
{
$obj = unserialize($line["content"]);
if ($obj &&
isset($obj["password"]) &&
strtolower($obj["password"]) === strtolower($apikey))
{
$_SESSION["uid"] = $line["owner_uid"];
break;
}
}
if (self::DEBUG_USER > 0)
{
$_SESSION["uid"] = self::DEBUG_USER; // always authenticate and set debug user
}
}
}
// set whether xml or json
private function setXml(): void
{
$this->xml = false;
if (isset($_REQUEST["api"]))
{
if (strtolower(clean($_REQUEST["api"])) === "xml")
{
$this->xml = true;
}
}
}
private function setIdHack(): void
{
$this->id_hack = false;
$user_agent = false;
if (isset($_SERVER["HTTP_USER_AGENT"]))
{
$user_agent = $_SERVER["HTTP_USER_AGENT"];
}
// Check for all client in Android except ReadKit in Mac, Mr. Reader and Dalvik
if ($user_agent &&
(strpos($user_agent, "Dalvik") !== FALSE ||
strpos($user_agent, "ReadKit") !== FALSE ||
strpos($user_agent, "Mr. Reader") !== FALSE))
{
$this->id_hack = true;
}
}
// validate the api_key, user preferences
function before($method): bool {
/* classes/api.php before */
if (parent::before($method)) {
if (self::DEBUG) {
// add request to debug log
error_log(print_r($_REQUEST, true));
}
// set the user from the db
$this->setUser();
// are we xml or json?
$this->setXml();
// do we need to apply the ID hack
$this->setIdHack();
if ($this->xml)
header("Content-Type: text/xml");
else
header("Content-Type: text/json");
// check we have a valid user
if (!$_SESSION["uid"]) {
$this->wrap(self::STATUS_ERR, array("error" => 'NOT_LOGGED_IN'));
return false;
}
// check if user has api access enabled
if ($_SESSION["uid"] && !get_pref('ENABLE_API_ACCESS')) {
$this->wrap(self::STATUS_ERR, array("error" => 'API_DISABLED'));
return false;
}
return true;
}
return false;
}
// always include api_version, status as 'auth'
// output json/xml
function wrap($status, $reply): void
{
/* classes/api.php wrap */
$arr = array("api_version" => self::API_LEVEL,
"auth" => $status);
if (!empty($reply) && is_array($reply))
{
$arr = array_merge($arr, $reply);
}
if ($status == self::STATUS_OK)
{
$arr["last_refreshed_on_time"] = (string)$this->lastRefreshedOnTime();
}
$resp = "";
if ($this->xml)
{
$resp = $this->array_to_xml($arr);
}
else
{
$resp = json_encode($arr);
}
print $resp;
if (self::DEBUG)
{
// debug output
error_log(print_r($resp, true));
}
}
// fever supports xml wrapped in <response> tags
// TODO: holy crap replace this junk
private function array_to_xml($array, $container = 'response', $is_root = true): string
{
if (!is_array($array)) return array_to_xml(array($array));
$xml = '';
if ($is_root)
{
$xml .= '<?xml version="1.0" encoding="utf-8"?>';
$xml .= "<{$container}>";
}
foreach($array as $key => $value)
{
// make sure key is a string
$elem = $key;
if (!is_string($key) && !empty($container))
{
$elem = $container;
}
$xml .= "<{$elem}>";
if (is_array($value))
{
if (array_keys($value) !== array_keys(array_keys($value)))
{
$xml .= array_to_xml($value, '', false);
}
else
{
$xml .= array_to_xml($value, r('/s$/', '', $elem), false);
}
}
else
{
$xml .= (htmlspecialchars($value, ENT_COMPAT, 'ISO-8859-1') != $value) ? "<![CDATA[{$value}]]>" : $value;
}
$xml .= "</{$elem}>";
}
if ($is_root)
{
$xml .= "</{$container}>";
}
return preg_replace('/[\x00-\x1F\x7F]/', '', $xml);
}
// every authenticated method includes last_refreshed_on_time
private function lastRefreshedOnTime(): int
{
$sth = $this->pdo->prepare("SELECT " . SUBSTRING_FOR_DATE . "(last_updated,1,19) AS last_updated
FROM ttrss_feeds
WHERE owner_uid = ?
ORDER BY last_updated DESC");
$sth->execute([clean($_SESSION["uid"])]);
if ($row = $sth->fetch())
{
$last_refreshed_on_time = (int) strtotime($row["last_updated"]);
}
else
{
$last_refreshed_on_time = 0;
}
return $last_refreshed_on_time;
}
private function flattenGroups(&$groupsToGroups, &$groups, &$groupsToTitle, $index): void
{
foreach ($groupsToGroups[$index] as $item)
{
$id = substr($item, strpos($item, "-") + 1);
array_push($groups, array("id" => intval($id), "title" => $groupsToTitle[$id]));
if (isset($groupsToGroups[$id]))
$this->flattenGroups($groupsToGroups, $groups, $groupsToTitle, $id);
}
}
function getGroups(): array
{
// TODO: ordering of child categories etc
$groups = array();
$sth = $this->pdo->prepare("SELECT id, title, parent_cat, order_id
FROM ttrss_feed_categories
WHERE owner_uid = ?
ORDER BY order_id ASC");
$sth->execute([clean($_SESSION["uid"])]);
$groupsToGroups = array();
$groupsToTitle = array();
while ($line = $sth->fetch())
{
if ($line["parent_cat"] === NULL)
{
if (!isset($groupsToGroups[-1]))
{
$groupsToGroups[-1] = array();
}
array_push($groupsToGroups[-1], $line["order_id"] . "-" . $line["id"]);
}
else
{
if (!isset($groupsToGroups[$line["parent_cat"]]))
{
$groupsToGroups[$line["parent_cat"]] = array();
}
array_push($groupsToGroups[$line["parent_cat"]], $line["order_id"] . "-" . $line["id"]);
}
$groupsToTitle[$line["id"]] = $line["title"];
}
foreach ($groupsToGroups as $key => $value)
{
sort($value);
}
if (isset($groupsToGroups[-1]))
$this->flattenGroups($groupsToGroups, $groups, $groupsToTitle, -1);
return $groups;
}
function getFeeds(): array
{
$feeds = array();
$sth = $this->pdo->prepare("SELECT id, title, feed_url, site_url, " . SUBSTRING_FOR_DATE . "(last_updated,1,19) AS last_updated
FROM ttrss_feeds
WHERE owner_uid = ?
ORDER BY order_id ASC");
$sth->execute([clean($_SESSION["uid"])]);
while ($line = $sth->fetch())
{
array_push($feeds, array("id" => intval($line["id"]),
"favicon_id" => intval($line["id"]),
"title" => $line["title"],
"url" => $line["feed_url"],
"site_url" => $line["site_url"],
"is_spark" => 0, // unsupported
"last_updated_on_time" => (int) strtotime($line["last_updated"])
));
}
return $feeds;
}
function getFavicons(): array
{
$favicons = array();
$sth = $this->pdo->prepare("SELECT id
FROM ttrss_feeds
WHERE owner_uid = ?
ORDER BY order_id ASC");
$sth->execute([clean($_SESSION["uid"])]);
// data = "image/gif;base64,<base64 encoded image>
while ($line = $sth->fetch())
{
$filename = "feed-icons/" . $line["id"] . ".ico";
if (file_exists($filename))
{
array_push($favicons, array("id" => intval($line["id"]),
"data" => image_type_to_mime_type(exif_imagetype($filename)) . ";base64," . base64_encode(file_get_contents($filename))
));
}
}
return $favicons;
}
function getLinks(): array
{
// TODO: is there a 'hot links' alternative in ttrss?
// use ttrss_user_entries / score > 0 / unread
$links = array();
$item_limit = 50;
$where = "owner_uid = ? AND ref_id = id AND score > 0 AND unread = true";
$where_items = array();
array_push($where_items, clean($_SESSION["uid"]));
if (isset($_REQUEST["range"]))
{
// use the range argument to request a limited "updated" items
if (is_numeric($_REQUEST["range"]))
{
$range = ($_REQUEST["range"] > 0) ? intval(clean($_REQUEST["range"])) : 0;
if ($range)
{
$offset = 0;
if (isset($_REQUEST["offset"]))
{
// use the range argument to request a limited "updated" items
if (is_numeric($_REQUEST["offset"]))
{
$offset = ($_REQUEST["offset"] > 0) ? intval(clean($_REQUEST["offset"])) : 0;
}
}
if ($range) {
if ($offset == 0) {
//range > 1 AND offset = 0
$where .= " AND updated < NOW()";
$where .= " AND updated > NOW()-INTERVAL ? DAY";
array_push($where_items, $range);
} else {
//range > 1 AND offset > 0
$where .= " AND updated < NOW()-INTERVAL ? DAY";
$where .= " AND updated > NOW()-INTERVAL ? DAY";
array_push($where_items, $offset, $offset+$range);
}
}
}
}
}
$where .= " ORDER BY score DESC, updated DESC" ;
if (is_numeric($_REQUEST["page"]))
{
// use the page argument to request the next $item_limit items
// page = 1 --> 1st Page will be convertet to 0
$page = isset($_REQUEST["page"]) ? intval(clean($_REQUEST["page"]))-1 : 0;
$page = ($page<0) ? 0 : $page;
$where .= " LIMIT " . $item_limit . " OFFSET " . intval($page * $item_limit);
// array_push($where_items, $item_limit);
// array_push($where_items, ($page * $item_limit));
} else {
$where .= " LIMIT ?";
array_push($where_items, $item_limit);
}
/* classes/api.php getLinks */
// id, feed_id, title, author, html, url, is_saved, is_read, created_on_time
$sth = $this->pdo->prepare("SELECT ref_id, feed_id, title, link, score, id, marked, unread, updated
FROM ttrss_entries, ttrss_user_entries
WHERE " . $where);
$sth->execute($where_items);
while ($line = $sth->fetch())
{
array_push($links, array("id" => intval($line["id"]),
"feed_id" => intval($line["feed_id"]),
"item_id" => intval($line["ref_id"]),
"temperature" => intval($line["score"]),
"is_item" => 1,
"is_local" => 1,
"is_saved" => (param_to_bool($line["marked"]) ? 1 : 0),
"title" => $line["title"],
"url" => $line["link"],
"item_ids" => ""
));
}
return $links;
}
function formatBytes($bytes, $precision = 2): string {
$units = array('B', 'KB', 'MB', 'GB', 'TB');
$bytes = max($bytes, 0);
$pow = floor(($bytes ? log($bytes) : 0) / log(1024));
$pow = min($pow, count($units) - 1);
// Uncomment one of the following alternatives
$bytes /= pow(1024, $pow);
// $bytes /= (1 << (10 * $pow));
return round($bytes, $precision) . ' ' . $units[$pow];
}
function getItems(): array
{
// items from specific groups, feeds
$items = array();
$item_limit = 50;
$where = " owner_uid = ? AND ref_id = id ";
$where_items = array();
array_push($where_items, clean($_SESSION["uid"]));
if (isset($_REQUEST["feed_ids"]) || isset($_REQUEST["group_ids"])) // added 0.3
{
$feed_ids = array();
if (isset($_REQUEST["feed_ids"]))
{
$feed_ids = explode(",", clean($_REQUEST["feed_ids"]));
}
if (isset($_REQUEST["group_ids"]))
{
$group_ids = array_map("intval", array_filter(explode(",", clean($_REQUEST["group_ids"])), "is_numeric"));
$group_ids_qmarks = arr_qmarks($group_ids);
$sth = $this->pdo->prepare("SELECT id
FROM ttrss_feeds
WHERE owner_uid = ? AND cat_id IN ($group_ids_qmarks)");
$sth->execute(array_merge([clean($_SESSION["uid"])], $group_ids));
$group_feed_ids = array();
while ($line = $sth->fetch())
{
array_push($group_feed_ids, $line["id"]);
}
$feed_ids = array_unique(array_merge($feed_ids, $group_feed_ids));
}
$feed_ids = array_map("intval", array_filter($feed_ids, "is_numeric"));
$feed_ids_qmarks = arr_qmarks($feed_ids);
$where .= " AND feed_id IN ($feed_ids_qmarks) ";
$where_items = array_merge($where_items, $feed_ids);
}
if (isset($_REQUEST["max_id"])) // descending from most recently added
{
// use the max_id argument to request the previous $item_limit items
if (is_numeric($_REQUEST["max_id"]))
{
$max_id = ($_REQUEST["max_id"] > 0) ? intval(clean($_REQUEST["max_id"])) : 0;
if ($max_id)
{
$where .= " AND id < ? ";
array_push($where_items, $max_id);
}
$where .= " ORDER BY id DESC ";
}
}
else if (isset($_REQUEST["with_ids"])) // selective
{
$item_ids = array_map("intval", array_filter(explode(",", clean($_REQUEST["with_ids"])), "is_numeric"));
$item_ids_count_arr = array(1,"1");
if (count($item_ids) >= 1) {
$item_ids_count_arr = $item_ids;
}
$item_ids_qmarks = arr_qmarks($item_ids_count_arr);
$where .= " AND id IN ($item_ids_qmarks) ";
$where_items = array_merge($where_items, $item_ids);
}
else // ascending from first added
{
if (is_numeric($_REQUEST["since_id"]))
{
// use the since_id argument to request the next $item_limit items
$since_id = isset($_GET["since_id"]) ? intval(clean($_GET["since_id"])) : 0;
if ($since_id)
{
$where .= " AND id > ? ";
if ($this->id_hack)
{
$val = $since_id * 1000; // NASTY hack for Mr. Reader 2.0 on iOS and TinyTiny RSS Fever
}
else
{
$val = $since_id;
}
array_push($where_items, $val);
}
$where .= " ORDER BY id ASC ";
}
}
$where .= " LIMIT " . $item_limit;
/* classes/api.php getArticle */
// id, feed_id, title, author, html, url, is_saved, is_read, created_on_time
$sth = $this->pdo->prepare("SELECT ref_id, feed_id, title, link, content, id, marked, unread, author,
" . SUBSTRING_FOR_DATE . "(updated,1,16) as updated,
(SELECT site_url FROM ttrss_feeds WHERE id = feed_id) AS site_url,
(SELECT hide_images FROM ttrss_feeds WHERE id = feed_id) AS hide_images
FROM ttrss_entries, ttrss_user_entries
WHERE " . $where);
try {
$sth->execute($where_items);
} catch (PDOException $e) {
/* nop */
}
while ($line = $sth->fetch())
{
$line_content = Sanitizer::sanitize(
$line["content"],
param_to_bool($line['hide_images']),
false, $line["site_url"], [], $line["id"]);
if ($this->add_attached_files){
$enclosures = Article::_get_enclosures($line["id"]);
if (count($enclosures) > 0) {
$line_content .= '<ul type="lower-greek">';
foreach ($enclosures as $enclosure) {
if (!empty($enclosure["content_url"])) {
$enc_type = "";
if (!empty($enclosure["content_type"])) {
$enc_type = ", " . $enclosure["content_type"];
}
$enc_size = "";
if (!empty($enclosure["duration"])) {
$enc_size = " , " . $this->formatBytes($enclosure["duration"]);
}
$line_content .= '<li><a href="' . $enclosure["content_url"] . '" target="_blank">' . basename($enclosure["content_url"]) . $enc_type . $enc_size . '</a></li>';
}
}
$line_content .= '</ul>';
}
}
array_push($items, array("id" => intval($line["id"]),
"feed_id" => intval($line["feed_id"]),
"title" => $line["title"],
"author" => $line["author"],
"html" => $line_content,
"url" => $line["link"],
"is_saved" => (param_to_bool($line["marked"]) ? 1 : 0),
"is_read" => ( (!param_to_bool($line["unread"])) ? 1 : 0),
"created_on_time" => (int) strtotime($line["updated"])
));
}
return $items;
}
function getTotalItems(): int
{
// number of total items
$total_items = 0;
$sth = $this->pdo->prepare("SELECT COUNT(ref_id) as total_items
FROM ttrss_user_entries
WHERE owner_uid = ?");
$sth->execute([clean($_SESSION["uid"])]);
if ($line = $sth->fetch())
{
$total_items = $line["total_items"];
}
return $total_items;
}
function getFeedsGroup(): array
{
$feeds_groups = array();
$sth = $this->pdo->prepare("SELECT id, cat_id
FROM ttrss_feeds
WHERE owner_uid = ?
AND cat_id IS NOT NULL
ORDER BY id ASC");
$sth->execute([clean($_SESSION["uid"])]);
$groupsToFeeds = array();
while ($line = $sth->fetch())
{
if (!array_key_exists($line["cat_id"], $groupsToFeeds))
$groupsToFeeds[$line["cat_id"]] = array();
array_push($groupsToFeeds[$line["cat_id"]], $line["id"]);
}
foreach ($groupsToFeeds as $group => $feeds)
{
$feedsStr = "";
foreach ($feeds as $feed)
$feedsStr .= $feed . ",";
$feedsStr = trim($feedsStr, ",");
array_push($feeds_groups, array("group_id" => $group,
"feed_ids" => $feedsStr));
}
return $feeds_groups;
}
function getUnreadItemIds(): string
{
$unreadItemIdsCSV = "";
$sth = $this->pdo->prepare("SELECT ref_id
FROM ttrss_user_entries
WHERE owner_uid = ? AND unread = true"); // ORDER BY red_id DESC
$sth->execute([clean($_SESSION["uid"])]);
while ($line = $sth->fetch())
{
$unreadItemIdsCSV .= $line["ref_id"] . ",";
}
$unreadItemIdsCSV = trim($unreadItemIdsCSV, ",");
return $unreadItemIdsCSV;
}
function getSavedItemIds(): string
{
$savedItemIdsCSV = "";
$sth = $this->pdo->prepare("SELECT ref_id
FROM ttrss_user_entries
WHERE owner_uid = ? AND marked = true");
$sth->execute([clean($_SESSION["uid"])]);
while ($line = $sth->fetch())
{
$savedItemIdsCSV .= $line["ref_id"] . ",";
}
$savedItemIdsCSV = trim($savedItemIdsCSV, ",");
return $savedItemIdsCSV;
}
function getEqualItems($id): string
{
//get all ids which have identical links (Reference is found by id)
$sth = $this->pdo->prepare("SELECT id
FROM ttrss_entries,ttrss_user_entries
WHERE id=ref_id AND owner_uid = ?
AND link=(SELECT link FROM ttrss_entries WHERE id = ?)");
$sth->execute(array_merge([clean($_SESSION["uid"]), $id]));
$ids = "";
while ($line = $sth->fetch())
{
$ids .= $line["id"] . ",";
}
$ids = trim($ids, ",");
if (self::DEBUG) {
// add request to debug log
error_log(print_r($ids, true));
}
return $ids;
}
function setItem($id, $field_raw, $mode): void
{
/* classes/api.php updateArticle */
$article_ids = array_map("intval", array_filter(explode(",", clean($id)), "is_numeric"));
$mode = (int) clean($mode);
$field_raw = (int) clean($field_raw);
$field = "";
$set_to = "";
switch ($field_raw) {
case 0:
$field = "marked";
$additional_fields = ",last_marked = NOW()";
break;
case 1:
$field = "unread";
$additional_fields = ",last_read = NOW()";
break;
};
switch ($mode) {
case 1:
$set_to = "true";
break;
case 0:
$set_to = "false";
break;
}
if ($field && $set_to && count($article_ids) > 0) {
$article_qmarks = arr_qmarks($article_ids);
$sth = $this->pdo->prepare("UPDATE ttrss_user_entries SET
$field = $set_to $additional_fields
WHERE ref_id IN ($article_qmarks) AND owner_uid = ?");
$sth->execute(array_merge($article_ids, [clean($_SESSION["uid"])]));
$num_updated = $sth->rowCount();
if ($num_updated > 0 && $field == "unread") {
$sth = $this->pdo->prepare("SELECT DISTINCT feed_id FROM ttrss_user_entries
WHERE ref_id IN ($article_qmarks)");
$sth->execute($article_ids);
}
}
}
function setItemAsRead($id): void
{
//action is true for all Equal Items
$ids = $this->getEqualItems($id);
$this->setItem($ids, 1, 0);
}
function setItemAsUnread($id): void
{
$ids = $this->getEqualItems($id);
$this->setItem($ids, 1, 1);
}
function setItemAsSaved($id): void
{
$this->setItem($id, 0, 1);
}
function setItemAsUnsaved($id): void
{
$this->setItem($id, 0, 0);
}
function setFeed($id, $cat, $before=0): void
{
/* classes/feeds.php catchup_feed */
// if before is zero, set it to now so feeds all items are read from before this point in time
if ($before == 0)
$before = time();
if (is_numeric($id))
{
// this is a category
if ($cat)
{
// if not special feed
if ($id > 0)
{
$sth = $this->pdo->prepare("UPDATE ttrss_user_entries
SET unread = false, last_read = NOW() WHERE ref_id IN
(SELECT id FROM
(SELECT DISTINCT id FROM ttrss_entries, ttrss_user_entries WHERE ref_id = id
AND owner_uid = ? AND unread = true AND feed_id IN
(SELECT id FROM ttrss_feeds WHERE cat_id IN (?)) AND updated < ? ) as tmp)");
$sth->execute([clean($_SESSION["uid"]), intval($id), date("Y-m-d H:i:s", $before)]);
}
// this is "all" to fever, but internally "all" is -4
else if ($id == 0)
{
$id = -4;
$sth = $this->pdo->prepare("UPDATE ttrss_user_entries
SET unread = false, last_read = NOW() WHERE ref_id IN
(SELECT id FROM
(SELECT DISTINCT id FROM ttrss_entries, ttrss_user_entries WHERE ref_id = id
AND owner_uid = ? AND unread = true AND updated < ? ) as tmp)");
$sth->execute([clean($_SESSION["uid"]), date("Y-m-d H:i:s", $before)]);
}
}
// not a category
else if ($id > 0)
{
$sth = $this->pdo->prepare("UPDATE ttrss_user_entries
SET unread = false, last_read = NOW() WHERE ref_id IN
(SELECT id FROM
(SELECT DISTINCT id FROM ttrss_entries, ttrss_user_entries WHERE ref_id = id
AND owner_uid = ? AND unread = true AND feed_id = ? AND updated < ? ) as tmp)");
$sth->execute([clean($_SESSION["uid"]), intval($id), date("Y-m-d H:i:s", $before)]);
}
}
}
function setFeedAsRead($id, $before): void
{
$this->setFeed($id, false, $before);
}
function setGroupAsRead($id, $before): void
{
$this->setFeed($id, true, $before);
}
// this does all the processing, since the fever api does not have a specific variable that specifies the operation
function index(): void
{
$response_arr = array();
if (isset($_REQUEST["groups"]))
{
$response_arr["groups"] = $this->getGroups();
$response_arr["feeds_groups"] = $this->getFeedsGroup();
}
if (isset($_REQUEST["feeds"]))
{
$response_arr["feeds"] = $this->getFeeds();
$response_arr["feeds_groups"] = $this->getFeedsGroup();
}
// TODO: favicon support
if (isset($_REQUEST["favicons"]))
{
$response_arr["favicons"] = $this->getFavicons();
}
if (isset($_REQUEST["items"]))
{
$response_arr["total_items"] = $this->getTotalItems();
$response_arr["items"] = $this->getItems();
}
if (isset($_REQUEST["links"]))
{
$response_arr["links"] = $this->getLinks();
}
if (isset($_REQUEST["unread_item_ids"]))
{
$response_arr["unread_item_ids"] = $this->getUnreadItemIds();
}
if (isset($_REQUEST["saved_item_ids"]))
{
$response_arr["saved_item_ids"] = $this->getSavedItemIds();
}
if (isset($_REQUEST["mark"], $_REQUEST["as"], $_REQUEST["id"]))
{
foreach (explode(",", clean($_REQUEST["id"])) as $id) {
$this->markId($id);
}
}
/* classes/api.php index */
if ($_SESSION["uid"])
$this->wrap(self::STATUS_OK, $response_arr);
else if (!$_SESSION["uid"])
$this->wrap(self::STATUS_ERR, array("error" => 'UNKNOWN_METHOD'));
}
function markId($id): void
{
if (is_numeric($id))
{
$before = (isset($_REQUEST["before"])) ? clean($_REQUEST["before"]) : null;
if ($before !== null && $before > pow(10,10)) {
$before = round($before / 1000);
}
$method_name = "set" . ucfirst(clean($_REQUEST["mark"])) . "As" . ucfirst(clean($_REQUEST["as"]));
if (method_exists($this, $method_name))
{
$this->{$method_name}(intval($id), $before);
switch(clean($_REQUEST["as"]))
{
case "read":
case "unread":
$response_arr["unread_item_ids"] = $this->getUnreadItemIds();
break;
case 'saved':
case 'unsaved':
$response_arr["saved_item_ids"] = $this->getSavedItemIds();
break;
}
}
}
}
}
?>