]> git.wh0rd.org Git - tt-rss.git/blob - classes/rpc.php
move several methods from dlg; fix displayed tags not updated after editing
[tt-rss.git] / classes / rpc.php
1 <?php
2 class RPC extends Handler_Protected {
3
4         function csrf_ignore($method) {
5                 $csrf_ignored = array("sanitycheck", "completelabels");
6
7                 return array_search($method, $csrf_ignored) !== false;
8         }
9
10         function setprofile() {
11                 $id = db_escape_string($this->link, $_REQUEST["id"]);
12
13                 $_SESSION["profile"] = $id;
14                 $_SESSION["prefs_cache"] = array();
15         }
16
17         function remprofiles() {
18                 $ids = explode(",", db_escape_string($this->link, trim($_REQUEST["ids"])));
19
20                 foreach ($ids as $id) {
21                         if ($_SESSION["profile"] != $id) {
22                                 db_query($this->link, "DELETE FROM ttrss_settings_profiles WHERE id = '$id' AND
23                                                         owner_uid = " . $_SESSION["uid"]);
24                         }
25                 }
26         }
27
28         // Silent
29         function addprofile() {
30                 $title = db_escape_string($this->link, trim($_REQUEST["title"]));
31                 if ($title) {
32                         db_query($this->link, "BEGIN");
33
34                         $result = db_query($this->link, "SELECT id FROM ttrss_settings_profiles
35                                 WHERE title = '$title' AND owner_uid = " . $_SESSION["uid"]);
36
37                         if (db_num_rows($result) == 0) {
38
39                                 db_query($this->link, "INSERT INTO ttrss_settings_profiles (title, owner_uid)
40                                                         VALUES ('$title', ".$_SESSION["uid"] .")");
41
42                                 $result = db_query($this->link, "SELECT id FROM ttrss_settings_profiles WHERE
43                                         title = '$title'");
44
45                                 if (db_num_rows($result) != 0) {
46                                         $profile_id = db_fetch_result($result, 0, "id");
47
48                                         if ($profile_id) {
49                                                 initialize_user_prefs($this->link, $_SESSION["uid"], $profile_id);
50                                         }
51                                 }
52                         }
53
54                         db_query($this->link, "COMMIT");
55                 }
56         }
57
58         // Silent
59         function saveprofile() {
60                 $id = db_escape_string($this->link, $_REQUEST["id"]);
61                 $title = db_escape_string($this->link, trim($_REQUEST["value"]));
62
63                 if ($id == 0) {
64                         print __("Default profile");
65                         return;
66                 }
67
68                 if ($title) {
69                         db_query($this->link, "BEGIN");
70
71                         $result = db_query($this->link, "SELECT id FROM ttrss_settings_profiles
72                                 WHERE title = '$title' AND owner_uid =" . $_SESSION["uid"]);
73
74                         if (db_num_rows($result) == 0) {
75                                 db_query($this->link, "UPDATE ttrss_settings_profiles
76                                                         SET title = '$title' WHERE id = '$id' AND
77                                                         owner_uid = " . $_SESSION["uid"]);
78                                 print $title;
79                         } else {
80                                 $result = db_query($this->link, "SELECT title FROM ttrss_settings_profiles
81                                                         WHERE id = '$id' AND owner_uid =" . $_SESSION["uid"]);
82                                 print db_fetch_result($result, 0, "title");
83                         }
84
85                         db_query($this->link, "COMMIT");
86                 }
87         }
88
89         // Silent
90         function remarchive() {
91                 $ids = explode(",", db_escape_string($this->link, $_REQUEST["ids"]));
92
93                 foreach ($ids as $id) {
94                         $result = db_query($this->link, "DELETE FROM ttrss_archived_feeds WHERE
95                 (SELECT COUNT(*) FROM ttrss_user_entries
96                                                         WHERE orig_feed_id = '$id') = 0 AND
97                 id = '$id' AND owner_uid = ".$_SESSION["uid"]);
98
99                         $rc = db_affected_rows($this->link, $result);
100                 }
101         }
102
103         function addfeed() {
104                 $feed = db_escape_string($this->link, $_REQUEST['feed']);
105                 $cat = db_escape_string($this->link, $_REQUEST['cat']);
106                 $login = db_escape_string($this->link, $_REQUEST['login']);
107                 $pass = db_escape_string($this->link, $_REQUEST['pass']);
108
109                 $rc = subscribe_to_feed($this->link, $feed, $cat, $login, $pass);
110
111                 print json_encode(array("result" => $rc));
112         }
113
114         function togglepref() {
115                 $key = db_escape_string($this->link, $_REQUEST["key"]);
116                 set_pref($this->link, $key, !get_pref($this->link, $key));
117                 $value = get_pref($this->link, $key);
118
119                 print json_encode(array("param" =>$key, "value" => $value));
120         }
121
122         function setpref() {
123                 // set_pref escapes input, so no need to double escape it here
124                 $key = $_REQUEST['key'];
125                 $value = str_replace("\n", "<br/>", $_REQUEST['value']);
126
127                 set_pref($this->link, $key, $value, $_SESSION['uid'], $key != 'USER_STYLESHEET');
128
129                 print json_encode(array("param" =>$key, "value" => $value));
130         }
131
132         function mark() {
133                 $mark = $_REQUEST["mark"];
134                 $id = db_escape_string($this->link, $_REQUEST["id"]);
135
136                 if ($mark == "1") {
137                         $mark = "true";
138                 } else {
139                         $mark = "false";
140                 }
141
142                 $result = db_query($this->link, "UPDATE ttrss_user_entries SET marked = $mark,
143                                         last_marked = NOW()
144                                         WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
145
146                 print json_encode(array("message" => "UPDATE_COUNTERS"));
147         }
148
149         function delete() {
150                 $ids = db_escape_string($this->link, $_REQUEST["ids"]);
151
152                 $result = db_query($this->link, "DELETE FROM ttrss_user_entries
153                 WHERE ref_id IN ($ids) AND owner_uid = " . $_SESSION["uid"]);
154
155                 purge_orphans($this->link);
156
157                 print json_encode(array("message" => "UPDATE_COUNTERS"));
158         }
159
160         function unarchive() {
161                 $ids = explode(",", $_REQUEST["ids"]);
162
163                 foreach ($ids as $id) {
164                         $id = db_escape_string($this->link, trim($id));
165                         db_query($this->link, "BEGIN");
166
167                         $result = db_query($this->link, "SELECT feed_url,site_url,title FROM ttrss_archived_feeds
168                                 WHERE id = (SELECT orig_feed_id FROM ttrss_user_entries WHERE ref_id = $id
169                                 AND owner_uid = ".$_SESSION["uid"].")");
170
171                         if (db_num_rows($result) != 0) {
172                                 $feed_url = db_escape_string($this->link, db_fetch_result($result, 0, "feed_url"));
173                                 $site_url = db_escape_string($this->link, db_fetch_result($result, 0, "site_url"));
174                                 $title = db_escape_string($this->link, db_fetch_result($result, 0, "title"));
175
176                                 $result = db_query($this->link, "SELECT id FROM ttrss_feeds WHERE feed_url = '$feed_url'
177                                         AND owner_uid = " .$_SESSION["uid"]);
178
179                                 if (db_num_rows($result) == 0) {
180
181                                         if (!$title) $title = '[Unknown]';
182
183                                         $result = db_query($this->link,
184                                                 "INSERT INTO ttrss_feeds
185                                                         (owner_uid,feed_url,site_url,title,cat_id,auth_login,auth_pass,update_method)
186                                                         VALUES (".$_SESSION["uid"].",
187                                                         '$feed_url',
188                                                         '$site_url',
189                                                         '$title',
190                                                         NULL, '', '', 0)");
191
192                                         $result = db_query($this->link,
193                                                 "SELECT id FROM ttrss_feeds WHERE feed_url = '$feed_url'
194                                                 AND owner_uid = ".$_SESSION["uid"]);
195
196                                         if (db_num_rows($result) != 0) {
197                                                 $feed_id = db_fetch_result($result, 0, "id");
198                                         }
199
200                                 } else {
201                                         $feed_id = db_fetch_result($result, 0, "id");
202                                 }
203
204                                 if ($feed_id) {
205                                         $result = db_query($this->link, "UPDATE ttrss_user_entries
206                                                 SET feed_id = '$feed_id', orig_feed_id = NULL
207                                                 WHERE ref_id = $id AND owner_uid = " . $_SESSION["uid"]);
208                                 }
209                         }
210
211                         db_query($this->link, "COMMIT");
212                 }
213
214                 print json_encode(array("message" => "UPDATE_COUNTERS"));
215         }
216
217         function archive() {
218                 $ids = explode(",", db_escape_string($this->link, $_REQUEST["ids"]));
219
220                 foreach ($ids as $id) {
221                         $this->archive_article($this->link, $id, $_SESSION["uid"]);
222                 }
223
224                 print json_encode(array("message" => "UPDATE_COUNTERS"));
225         }
226
227         private function archive_article($link, $id, $owner_uid) {
228                 db_query($link, "BEGIN");
229
230                 $result = db_query($link, "SELECT feed_id FROM ttrss_user_entries
231                         WHERE ref_id = '$id' AND owner_uid = $owner_uid");
232
233                 if (db_num_rows($result) != 0) {
234
235                         /* prepare the archived table */
236
237                         $feed_id = (int) db_fetch_result($result, 0, "feed_id");
238
239                         if ($feed_id) {
240                                 $result = db_query($link, "SELECT id FROM ttrss_archived_feeds
241                                         WHERE id = '$feed_id'");
242
243                                 if (db_num_rows($result) == 0) {
244                                         db_query($link, "INSERT INTO ttrss_archived_feeds
245                                                 (id, owner_uid, title, feed_url, site_url)
246                                         SELECT id, owner_uid, title, feed_url, site_url from ttrss_feeds
247                                         WHERE id = '$feed_id'");
248                                 }
249
250                                 db_query($link, "UPDATE ttrss_user_entries
251                                         SET orig_feed_id = feed_id, feed_id = NULL
252                                         WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
253                         }
254                 }
255
256                 db_query($link, "COMMIT");
257         }
258
259         function publ() {
260                 $pub = $_REQUEST["pub"];
261                 $id = db_escape_string($this->link, $_REQUEST["id"]);
262                 $note = trim(strip_tags(db_escape_string($this->link, $_REQUEST["note"])));
263
264                 if ($pub == "1") {
265                         $pub = "true";
266                 } else {
267                         $pub = "false";
268                 }
269
270                 $result = db_query($this->link, "UPDATE ttrss_user_entries SET
271                         published = $pub, last_published = NOW()
272                         WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
273
274                 $pubsub_result = false;
275
276                 if (PUBSUBHUBBUB_HUB) {
277                         $rss_link = get_self_url_prefix() .
278                                 "/public.php?op=rss&id=-2&key=" .
279                                 get_feed_access_key($this->link, -2, false);
280
281                         $p = new Publisher(PUBSUBHUBBUB_HUB);
282
283                         $pubsub_result = $p->publish_update($rss_link);
284                 }
285
286                 print json_encode(array("message" => "UPDATE_COUNTERS",
287                         "pubsub_result" => $pubsub_result));
288         }
289
290         function getAllCounters() {
291                 $last_article_id = (int) $_REQUEST["last_article_id"];
292
293                 $reply = array();
294
295                 if ($seq) $reply['seq'] = $seq;
296
297                 if ($last_article_id != getLastArticleId($this->link)) {
298                         $reply['counters'] = getAllCounters($this->link);
299                 }
300
301                 $reply['runtime-info'] = make_runtime_info($this->link);
302
303                 print json_encode($reply);
304         }
305
306         /* GET["cmode"] = 0 - mark as read, 1 - as unread, 2 - toggle */
307         function catchupSelected() {
308                 $ids = explode(",", db_escape_string($this->link, $_REQUEST["ids"]));
309                 $cmode = sprintf("%d", $_REQUEST["cmode"]);
310
311                 catchupArticlesById($this->link, $ids, $cmode);
312
313                 print json_encode(array("message" => "UPDATE_COUNTERS"));
314         }
315
316         function markSelected() {
317                 $ids = explode(",", db_escape_string($this->link, $_REQUEST["ids"]));
318                 $cmode = sprintf("%d", $_REQUEST["cmode"]);
319
320                 $this->markArticlesById($this->link, $ids, $cmode);
321
322                 print json_encode(array("message" => "UPDATE_COUNTERS"));
323         }
324
325         function publishSelected() {
326                 $ids = explode(",", db_escape_string($this->link, $_REQUEST["ids"]));
327                 $cmode = sprintf("%d", $_REQUEST["cmode"]);
328
329                 $this->publishArticlesById($this->link, $ids, $cmode);
330
331                 print json_encode(array("message" => "UPDATE_COUNTERS"));
332         }
333
334         function sanityCheck() {
335                 $_SESSION["hasAudio"] = $_REQUEST["hasAudio"] === "true";
336                 $_SESSION["hasSandbox"] = $_REQUEST["hasSandbox"] === "true";
337
338                 $reply = array();
339
340                 $reply['error'] = sanity_check($this->link);
341
342                 if ($reply['error']['code'] == 0) {
343                         $reply['init-params'] = make_init_params($this->link);
344                         $reply['runtime-info'] = make_runtime_info($this->link);
345                 }
346
347                 print json_encode($reply);
348         }
349
350         function setArticleTags() {
351
352                 $id = db_escape_string($this->link, $_REQUEST["id"]);
353
354                 $tags_str = db_escape_string($this->link, $_REQUEST["tags_str"]);
355                 $tags = array_unique(trim_array(explode(",", $tags_str)));
356
357                 db_query($this->link, "BEGIN");
358
359                 $result = db_query($this->link, "SELECT int_id FROM ttrss_user_entries WHERE
360                                 ref_id = '$id' AND owner_uid = '".$_SESSION["uid"]."' LIMIT 1");
361
362                 if (db_num_rows($result) == 1) {
363
364                         $tags_to_cache = array();
365
366                         $int_id = db_fetch_result($result, 0, "int_id");
367
368                         db_query($this->link, "DELETE FROM ttrss_tags WHERE
369                                 post_int_id = $int_id AND owner_uid = '".$_SESSION["uid"]."'");
370
371                         foreach ($tags as $tag) {
372                                 $tag = sanitize_tag($tag);
373
374                                 if (!tag_is_valid($tag)) {
375                                         continue;
376                                 }
377
378                                 if (preg_match("/^[0-9]*$/", $tag)) {
379                                         continue;
380                                 }
381
382                                 //                                      print "<!-- $id : $int_id : $tag -->";
383
384                                 if ($tag != '') {
385                                         db_query($this->link, "INSERT INTO ttrss_tags
386                                                                 (post_int_id, owner_uid, tag_name) VALUES ('$int_id', '".$_SESSION["uid"]."', '$tag')");
387                                 }
388
389                                 array_push($tags_to_cache, $tag);
390                         }
391
392                         /* update tag cache */
393
394                         sort($tags_to_cache);
395                         $tags_str = join(",", $tags_to_cache);
396
397                         db_query($this->link, "UPDATE ttrss_user_entries
398                                 SET tag_cache = '$tags_str' WHERE ref_id = '$id'
399                                                 AND owner_uid = " . $_SESSION["uid"]);
400                 }
401
402                 db_query($this->link, "COMMIT");
403
404                 $tags = get_article_tags($this->link, $id);
405                 $tags_str = format_tags_string($tags, $id);
406                 $tags_str_full = join(", ", $tags);
407
408                 if (!$tags_str_full) $tags_str_full = __("no tags");
409
410                 print json_encode(array("id" => (int)$id,
411                                 "content" => $tags_str, "content_full" => $tags_str_full));
412         }
413
414         function regenOPMLKey() {
415                 $this->update_feed_access_key($this->link, 'OPML:Publish',
416                 false, $_SESSION["uid"]);
417
418                 $new_link = Opml::opml_publish_url($this->link);
419
420                 print json_encode(array("link" => $new_link));
421         }
422
423         function completeLabels() {
424                 $search = db_escape_string($this->link, $_REQUEST["search"]);
425
426                 $result = db_query($this->link, "SELECT DISTINCT caption FROM
427                                 ttrss_labels2
428                                 WHERE owner_uid = '".$_SESSION["uid"]."' AND
429                                 LOWER(caption) LIKE LOWER('$search%') ORDER BY caption
430                                 LIMIT 5");
431
432                 print "<ul>";
433                 while ($line = db_fetch_assoc($result)) {
434                         print "<li>" . $line["caption"] . "</li>";
435                 }
436                 print "</ul>";
437         }
438
439
440         function completeTags() {
441                 $search = db_escape_string($this->link, $_REQUEST["search"]);
442
443                 $result = db_query($this->link, "SELECT DISTINCT tag_name FROM ttrss_tags
444                                 WHERE owner_uid = '".$_SESSION["uid"]."' AND
445                                 tag_name LIKE '$search%' ORDER BY tag_name
446                                 LIMIT 10");
447
448                 print "<ul>";
449                 while ($line = db_fetch_assoc($result)) {
450                         print "<li>" . $line["tag_name"] . "</li>";
451                 }
452                 print "</ul>";
453         }
454
455         function purge() {
456                 $ids = explode(",", db_escape_string($this->link, $_REQUEST["ids"]));
457                 $days = sprintf("%d", $_REQUEST["days"]);
458
459                 foreach ($ids as $id) {
460
461                         $result = db_query($this->link, "SELECT id FROM ttrss_feeds WHERE
462                                 id = '$id' AND owner_uid = ".$_SESSION["uid"]);
463
464                         if (db_num_rows($result) == 1) {
465                                 purge_feed($this->link, $id, $days);
466                         }
467                 }
468         }
469
470         function getArticles() {
471                 $ids = explode(",", db_escape_string($this->link, $_REQUEST["ids"]));
472                 $articles = array();
473
474                 foreach ($ids as $id) {
475                         if ($id) {
476                                 array_push($articles, format_article($this->link, $id, 0, false));
477                         }
478                 }
479
480                 print json_encode($articles);
481         }
482
483         function checkDate() {
484                 $date = db_escape_string($this->link, $_REQUEST["date"]);
485                 $date_parsed = strtotime($date);
486
487                 print json_encode(array("result" => (bool)$date_parsed,
488                         "date" => date("c", $date_parsed)));
489         }
490
491         function assigntolabel() {
492                 return $this->labelops(true);
493         }
494
495         function removefromlabel() {
496                 return $this->labelops(false);
497         }
498
499         function labelops($assign) {
500                 $reply = array();
501
502                 $ids = explode(",", db_escape_string($this->link, $_REQUEST["ids"]));
503                 $label_id = db_escape_string($this->link, $_REQUEST["lid"]);
504
505                 $label = db_escape_string($this->link, label_find_caption($this->link, $label_id,
506                 $_SESSION["uid"]));
507
508                 $reply["info-for-headlines"] = array();
509
510                 if ($label) {
511
512                         foreach ($ids as $id) {
513
514                                 if ($assign)
515                                         label_add_article($this->link, $id, $label, $_SESSION["uid"]);
516                                 else
517                                         label_remove_article($this->link, $id, $label, $_SESSION["uid"]);
518
519                                 $labels = get_article_labels($this->link, $id, $_SESSION["uid"]);
520
521                                 array_push($reply["info-for-headlines"],
522                                 array("id" => $id, "labels" => format_article_labels($labels, $id)));
523
524                         }
525                 }
526
527                 $reply["message"] = "UPDATE_COUNTERS";
528
529                 print json_encode($reply);
530         }
531
532         function updateFeedBrowser() {
533                 $search = db_escape_string($this->link, $_REQUEST["search"]);
534                 $limit = db_escape_string($this->link, $_REQUEST["limit"]);
535                 $mode = (int) db_escape_string($this->link, $_REQUEST["mode"]);
536
537                 require_once "feedbrowser.php";
538
539                 print json_encode(array("content" =>
540                         make_feed_browser($this->link, $search, $limit, $mode),
541                                 "mode" => $mode));
542         }
543
544         // Silent
545         function massSubscribe() {
546
547                 $payload = json_decode($_REQUEST["payload"], false);
548                 $mode = $_REQUEST["mode"];
549
550                 if (!$payload || !is_array($payload)) return;
551
552                 if ($mode == 1) {
553                         foreach ($payload as $feed) {
554
555                                 $title = db_escape_string($this->link, $feed[0]);
556                                 $feed_url = db_escape_string($this->link, $feed[1]);
557
558                                 $result = db_query($this->link, "SELECT id FROM ttrss_feeds WHERE
559                                         feed_url = '$feed_url' AND owner_uid = " . $_SESSION["uid"]);
560
561                                 if (db_num_rows($result) == 0) {
562                                         $result = db_query($this->link, "INSERT INTO ttrss_feeds
563                                                                         (owner_uid,feed_url,title,cat_id,site_url)
564                                                                         VALUES ('".$_SESSION["uid"]."',
565                                                                         '$feed_url', '$title', NULL, '')");
566                                 }
567                         }
568                 } else if ($mode == 2) {
569                         // feed archive
570                         foreach ($payload as $id) {
571                                 $result = db_query($this->link, "SELECT * FROM ttrss_archived_feeds
572                                         WHERE id = '$id' AND owner_uid = " . $_SESSION["uid"]);
573
574                                 if (db_num_rows($result) != 0) {
575                                         $site_url = db_escape_string($this->link, db_fetch_result($result, 0, "site_url"));
576                                         $feed_url = db_escape_string($this->link, db_fetch_result($result, 0, "feed_url"));
577                                         $title = db_escape_string($this->link, db_fetch_result($result, 0, "title"));
578
579                                         $result = db_query($this->link, "SELECT id FROM ttrss_feeds WHERE
580                                                 feed_url = '$feed_url' AND owner_uid = " . $_SESSION["uid"]);
581
582                                         if (db_num_rows($result) == 0) {
583                                                 $result = db_query($this->link, "INSERT INTO ttrss_feeds
584                                                                                 (owner_uid,feed_url,title,cat_id,site_url)
585                                                                         VALUES ('$id','".$_SESSION["uid"]."',
586                                                                         '$feed_url', '$title', NULL, '$site_url')");
587                                         }
588                                 }
589                         }
590                 }
591         }
592
593         function catchupFeed() {
594                 $feed_id = db_escape_string($this->link, $_REQUEST['feed_id']);
595                 $is_cat = db_escape_string($this->link, $_REQUEST['is_cat']) == "true";
596                 $mode = db_escape_string($this->link, $_REQUEST['mode']);
597
598                 catchup_feed($this->link, $feed_id, $is_cat, false, false, $mode);
599
600                 print json_encode(array("message" => "UPDATE_COUNTERS"));
601         }
602
603         function quickAddCat() {
604                 $cat = db_escape_string($this->link, $_REQUEST["cat"]);
605
606                 add_feed_category($this->link, $cat);
607
608                 $result = db_query($this->link, "SELECT id FROM ttrss_feed_categories WHERE
609                         title = '$cat' AND owner_uid = " . $_SESSION["uid"]);
610
611                 if (db_num_rows($result) == 1) {
612                         $id = db_fetch_result($result, 0, "id");
613                 } else {
614                         $id = 0;
615                 }
616
617                 print_feed_cat_select($this->link, "cat_id", $id);
618         }
619
620         function regenFeedKey() {
621                 $feed_id = db_escape_string($this->link, $_REQUEST['id']);
622                 $is_cat = db_escape_string($this->link, $_REQUEST['is_cat']) == "true";
623
624                 $new_key = $this->update_feed_access_key($this->link, $feed_id, $is_cat);
625
626                 print json_encode(array("link" => $new_key));
627         }
628
629         // Silent
630         function clearKeys() {
631                 db_query($this->link, "DELETE FROM ttrss_access_keys WHERE
632                         owner_uid = " . $_SESSION["uid"]);
633         }
634
635         // Silent
636         function clearArticleKeys() {
637                 db_query($this->link, "UPDATE ttrss_user_entries SET uuid = '' WHERE
638                         owner_uid = " . $_SESSION["uid"]);
639
640                 return;
641         }
642
643         function verifyRegexp() {
644                 $reg_exp = $_REQUEST["reg_exp"];
645
646                 $status = @preg_match("/$reg_exp/i", "TEST") !== false;
647
648                 print json_encode(array("status" => $status));
649         }
650
651         /* function buttonPlugin() {
652                 $pclass = "button_" . basename($_REQUEST['plugin']);
653                 $method = $_REQUEST['plugin_method'];
654
655                 if (class_exists($pclass)) {
656                         $plugin = new $pclass($this->link);
657                         if (method_exists($plugin, $method)) {
658                                 return $plugin->$method();
659                         }
660                 }
661         } */
662
663         function genHash() {
664                 $hash = sha1(uniqid(rand(), true));
665
666                 print json_encode(array("hash" => $hash));
667         }
668
669         function batchAddFeeds() {
670                 $cat_id = db_escape_string($this->link, $_REQUEST['cat']);
671                 $feeds = explode("\n", db_escape_string($this->link, $_REQUEST['feeds']));
672                 $login = db_escape_string($this->link, $_REQUEST['login']);
673                 $pass = db_escape_string($this->link, $_REQUEST['pass']);
674
675                 foreach ($feeds as $feed) {
676                         $feed = trim($feed);
677
678                         if (validate_feed_url($feed)) {
679
680                                 db_query($this->link, "BEGIN");
681
682                                 if ($cat_id == "0" || !$cat_id) {
683                                         $cat_qpart = "NULL";
684                                 } else {
685                                         $cat_qpart = "'$cat_id'";
686                                 }
687
688                                 $result = db_query($this->link,
689                                         "SELECT id FROM ttrss_feeds
690                                         WHERE feed_url = '$feed' AND owner_uid = ".$_SESSION["uid"]);
691
692                                 if (db_num_rows($result) == 0) {
693                                         $result = db_query($this->link,
694                                                 "INSERT INTO ttrss_feeds
695                                                         (owner_uid,feed_url,title,cat_id,auth_login,auth_pass,update_method)
696                                                 VALUES ('".$_SESSION["uid"]."', '$feed',
697                                                         '[Unknown]', $cat_qpart, '$login', '$pass', 0)");
698                                 }
699
700                                 db_query($this->link, "COMMIT");
701                         }
702                 }
703         }
704
705         function setScore() {
706                 $ids = db_escape_string($this->link, $_REQUEST['id']);
707                 $score = (int)db_escape_string($this->link, $_REQUEST['score']);
708
709                 db_query($this->link, "UPDATE ttrss_user_entries SET
710                         score = '$score' WHERE ref_id IN ($ids) AND owner_uid = " . $_SESSION["uid"]);
711
712                 print json_encode(array("id" => $id,
713                         "score_pic" => get_score_pic($score)));
714         }
715
716         function setpanelmode() {
717                 $wide = (int) $_REQUEST["wide"];
718
719                 setcookie("ttrss_widescreen", $wide,
720                         time() + SESSION_COOKIE_LIFETIME);
721
722                 print json_encode(array("wide" => $wide));
723         }
724
725         function updaterandomfeed() {
726                 // Test if the feed need a update (update interval exceded).
727                 if (DB_TYPE == "pgsql") {
728                         $update_limit_qpart = "AND ((
729                                         ttrss_feeds.update_interval = 0
730                                         AND ttrss_feeds.last_updated < NOW() - CAST((ttrss_user_prefs.value || ' minutes') AS INTERVAL)
731                                 ) OR (
732                                         ttrss_feeds.update_interval > 0
733                                         AND ttrss_feeds.last_updated < NOW() - CAST((ttrss_feeds.update_interval || ' minutes') AS INTERVAL)
734                                 ) OR ttrss_feeds.last_updated IS NULL
735                                 OR last_updated = '1970-01-01 00:00:00')";
736                 } else {
737                         $update_limit_qpart = "AND ((
738                                         ttrss_feeds.update_interval = 0
739                                         AND ttrss_feeds.last_updated < DATE_SUB(NOW(), INTERVAL CONVERT(ttrss_user_prefs.value, SIGNED INTEGER) MINUTE)
740                                 ) OR (
741                                         ttrss_feeds.update_interval > 0
742                                         AND ttrss_feeds.last_updated < DATE_SUB(NOW(), INTERVAL ttrss_feeds.update_interval MINUTE)
743                                 ) OR ttrss_feeds.last_updated IS NULL
744                                 OR last_updated = '1970-01-01 00:00:00')";
745                 }
746
747                 // Test if feed is currently being updated by another process.
748                 if (DB_TYPE == "pgsql") {
749                         $updstart_thresh_qpart = "AND (ttrss_feeds.last_update_started IS NULL OR ttrss_feeds.last_update_started < NOW() - INTERVAL '5 minutes')";
750                 } else {
751                         $updstart_thresh_qpart = "AND (ttrss_feeds.last_update_started IS NULL OR ttrss_feeds.last_update_started < DATE_SUB(NOW(), INTERVAL 5 MINUTE))";
752                 }
753
754                 $random_qpart = sql_random_function();
755
756                 // We search for feed needing update.
757                 $result = db_query($this->link, "SELECT ttrss_feeds.feed_url,ttrss_feeds.id
758                         FROM
759                                 ttrss_feeds, ttrss_users, ttrss_user_prefs
760                         WHERE
761                                 ttrss_feeds.owner_uid = ttrss_users.id
762                                 AND ttrss_users.id = ttrss_user_prefs.owner_uid
763                                 AND ttrss_user_prefs.pref_name = 'DEFAULT_UPDATE_INTERVAL'
764                                 AND ttrss_feeds.owner_uid = ".$_SESSION["uid"]."
765                                 $update_limit_qpart $updstart_thresh_qpart
766                         ORDER BY $random_qpart LIMIT 30");
767
768                 $feed_id = -1;
769
770                 require_once "rssfuncs.php";
771
772                 $num_updated = 0;
773
774                 $tstart = time();
775
776                 while ($line = db_fetch_assoc($result)) {
777                         $feed_id = $line["id"];
778
779                         if (time() - $tstart < ini_get("max_execution_time") * 0.7) {
780                                 update_rss_feed($this->link, $feed_id, true);
781                                 ++$num_updated;
782                         } else {
783                                 break;
784                         }
785                 }
786
787                 // Purge orphans and cleanup tags
788                 purge_orphans($this->link);
789                 cleanup_tags($this->link, 14, 50000);
790
791                 if ($num_updated > 0) {
792                         print json_encode(array("message" => "UPDATE_COUNTERS",
793                                 "num_updated" => $num_updated));
794                 } else {
795                         print json_encode(array("message" => "NOTHING_TO_UPDATE"));
796                 }
797
798         }
799
800         function update_feed_access_key($link, $feed_id, $is_cat, $owner_uid = false) {
801                 if (!$owner_uid) $owner_uid = $_SESSION["uid"];
802
803                 $sql_is_cat = bool_to_sql_bool($is_cat);
804
805                 $result = db_query($link, "SELECT access_key FROM ttrss_access_keys
806                         WHERE feed_id = '$feed_id'      AND is_cat = $sql_is_cat
807                         AND owner_uid = " . $owner_uid);
808
809                 if (db_num_rows($result) == 1) {
810                         $key = db_escape_string($this->link, sha1(uniqid(rand(), true)));
811
812                         db_query($link, "UPDATE ttrss_access_keys SET access_key = '$key'
813                                 WHERE feed_id = '$feed_id' AND is_cat = $sql_is_cat
814                                 AND owner_uid = " . $owner_uid);
815
816                         return $key;
817
818                 } else {
819                         return get_feed_access_key($link, $feed_id, $is_cat, $owner_uid);
820                 }
821         }
822
823         private function markArticlesById($link, $ids, $cmode) {
824
825                 $tmp_ids = array();
826
827                 foreach ($ids as $id) {
828                         array_push($tmp_ids, "ref_id = '$id'");
829                 }
830
831                 $ids_qpart = join(" OR ", $tmp_ids);
832
833                 if ($cmode == 0) {
834                         db_query($link, "UPDATE ttrss_user_entries SET
835                         marked = false, last_marked = NOW()
836                         WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
837                 } else if ($cmode == 1) {
838                         db_query($link, "UPDATE ttrss_user_entries SET
839                         marked = true, last_marked = NOW()
840                         WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
841                 } else {
842                         db_query($link, "UPDATE ttrss_user_entries SET
843                         marked = NOT marked,last_marked = NOW()
844                         WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
845                 }
846         }
847
848         private function publishArticlesById($link, $ids, $cmode) {
849
850                 $tmp_ids = array();
851
852                 foreach ($ids as $id) {
853                         array_push($tmp_ids, "ref_id = '$id'");
854                 }
855
856                 $ids_qpart = join(" OR ", $tmp_ids);
857
858                 if ($cmode == 0) {
859                         db_query($link, "UPDATE ttrss_user_entries SET
860                         published = false,last_published = NOW()
861                         WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
862                 } else if ($cmode == 1) {
863                         db_query($link, "UPDATE ttrss_user_entries SET
864                         published = true,last_published = NOW()
865                         WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
866                 } else {
867                         db_query($link, "UPDATE ttrss_user_entries SET
868                         published = NOT published,last_published = NOW()
869                         WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
870                 }
871
872                 if (PUBSUBHUBBUB_HUB) {
873                         $rss_link = get_self_url_prefix() .
874                                 "/public.php?op=rss&id=-2&key=" .
875                                 get_feed_access_key($link, -2, false);
876
877                         $p = new Publisher(PUBSUBHUBBUB_HUB);
878
879                         $pubsub_result = $p->publish_update($rss_link);
880                 }
881         }
882
883         function getlinktitlebyid() {
884                 $id = db_escape_string($this->link, $_REQUEST['id']);
885
886                 $result = db_query($this->link, "SELECT link, title FROM ttrss_entries, ttrss_user_entries
887                         WHERE ref_id = '$id' AND ref_id = id AND owner_uid = ". $_SESSION["uid"]);
888
889                 if (db_num_rows($result) != 0) {
890                         $link = db_fetch_result($result, 0, "link");
891                         $title = db_fetch_result($result, 0, "title");
892
893                         echo json_encode(array("link" => $link, "title" => $title));
894                 } else {
895                         echo json_encode(array("error" => "ARTICLE_NOT_FOUND"));
896                 }
897         }
898
899         function cdmArticlePreview() {
900                 $id = db_escape_string($this->link, $_REQUEST['id']);
901
902                 $result = db_query($this->link, "SELECT link,
903                         ttrss_entries.title, content, feed_url
904                         FROM
905                         ttrss_entries, ttrss_user_entries
906                                 LEFT JOIN ttrss_feeds ON (ttrss_user_entries.feed_id = ttrss_feeds.id)
907                         WHERE ref_id = '$id' AND ref_id = ttrss_entries.id AND
908                                 ttrss_user_entries.owner_uid = ". $_SESSION["uid"]);
909
910                 if (db_num_rows($result) != 0) {
911                         $link = db_fetch_result($result, 0, "link");
912                         $title = db_fetch_result($result, 0, "title");
913                         $feed_url = db_fetch_result($result, 0, "feed_url");
914
915                         $content = sanitize($this->link,
916                                 db_fetch_result($result, 0, "content"), false, false, $feed_url);
917
918                         print "<div class='content'>".$content."</content>";
919
920                 } else {
921                         print "Article not found.";
922                 }
923
924         }
925
926 }
927 ?>