]> git.wh0rd.org Git - tt-rss.git/blob - classes/handler/public.php
syndicated feeds: add orig_guid to optionally alter item id to workaround duplicate...
[tt-rss.git] / classes / handler / public.php
1 <?php
2 class Handler_Public extends Handler {
3
4         private function generate_syndicated_feed($owner_uid, $feed, $is_cat,
5                 $limit, $offset, $search, $search_mode,
6                 $view_mode = false, $format = 'atom', $order = false, $orig_guid = false) {
7
8                 require_once "lib/MiniTemplator.class.php";
9
10                 $note_style =   "background-color : #fff7d5;
11                         border-width : 1px; ".
12                         "padding : 5px; border-style : dashed; border-color : #e7d796;".
13                         "margin-bottom : 1em; color : #9a8c59;";
14
15                 if (!$limit) $limit = 60;
16
17                 $date_sort_field = "date_entered DESC, updated DESC";
18
19                 if ($feed == -2)
20                         $date_sort_field = "last_published DESC";
21                 else if ($feed == -1)
22                         $date_sort_field = "last_marked DESC";
23
24                 switch ($order) {
25                 case "title":
26                         $date_sort_field = "ttrss_entries.title";
27                         break;
28                 case "date_reverse":
29                         $date_sort_field = "date_entered, updated";
30                         break;
31                 case "feed_dates":
32                         $date_sort_field = "updated DESC";
33                         break;
34                 }
35
36                 $qfh_ret = queryFeedHeadlines($feed,
37                         1, $view_mode, $is_cat, $search, $search_mode,
38                         $date_sort_field, $offset, $owner_uid,
39                         false, 0, false, true);
40
41                 $result = $qfh_ret[0];
42
43                 if ($this->dbh->num_rows($result) != 0) {
44                         $ts = strtotime($this->dbh->fetch_result($result, 0, "date_entered"));
45
46                         if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) &&
47                                         strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) >= $ts) {
48                       header('HTTP/1.0 304 Not Modified');
49                       return;
50                         }
51
52                         $last_modified = gmdate("D, d M Y H:i:s", $ts) . " GMT";
53                         header("Last-Modified: $last_modified", true);
54                 }
55
56                 $qfh_ret = queryFeedHeadlines($feed,
57                         $limit, $view_mode, $is_cat, $search, $search_mode,
58                         $date_sort_field, $offset, $owner_uid,
59                         false, 0, false, true);
60
61
62                 $result = $qfh_ret[0];
63                 $feed_title = htmlspecialchars($qfh_ret[1]);
64                 $feed_site_url = $qfh_ret[2];
65                 $last_error = $qfh_ret[3];
66
67                 $feed_self_url = get_self_url_prefix() .
68                         "/public.php?op=rss&id=-2&key=" .
69                         get_feed_access_key(-2, false, $owner_uid);
70
71                 if (!$feed_site_url) $feed_site_url = get_self_url_prefix();
72
73                 if ($format == 'atom') {
74                         $tpl = new MiniTemplator;
75
76                         $tpl->readTemplateFromFile("templates/generated_feed.txt");
77
78                         $tpl->setVariable('FEED_TITLE', $feed_title, true);
79                         $tpl->setVariable('VERSION', VERSION, true);
80                         $tpl->setVariable('FEED_URL', htmlspecialchars($feed_self_url), true);
81
82                         if (PUBSUBHUBBUB_HUB && $feed == -2) {
83                                 $tpl->setVariable('HUB_URL', htmlspecialchars(PUBSUBHUBBUB_HUB), true);
84                                 $tpl->addBlock('feed_hub');
85                         }
86
87                         $tpl->setVariable('SELF_URL', htmlspecialchars(get_self_url_prefix()), true);
88
89                         while ($line = $this->dbh->fetch_assoc($result)) {
90
91                                 $tpl->setVariable('ARTICLE_ID',
92                                         htmlspecialchars($orig_guid ? $line['link'] :
93                                                 get_self_url_prefix() .
94                                                         "/public.php?url=" . urlencode($line['link'])), true);
95                                 $tpl->setVariable('ARTICLE_LINK', htmlspecialchars($line['link']), true);
96                                 $tpl->setVariable('ARTICLE_TITLE', htmlspecialchars($line['title']), true);
97                                 $tpl->setVariable('ARTICLE_EXCERPT',
98                                         truncate_string(strip_tags($line["content_preview"]), 100, '...'), true);
99
100                                 $content = sanitize($line["content_preview"], false, $owner_uid);
101
102                                 if ($line['note']) {
103                                         $content = "<div style=\"$note_style\">Article note: " . $line['note'] . "</div>" .
104                                                 $content;
105                                         $tpl->setVariable('ARTICLE_NOTE', htmlspecialchars($line['note']), true);
106                                 }
107
108                                 $tpl->setVariable('ARTICLE_CONTENT', $content, true);
109
110                                 $tpl->setVariable('ARTICLE_UPDATED_ATOM',
111                                         date('c', strtotime($line["updated"])), true);
112                                 $tpl->setVariable('ARTICLE_UPDATED_RFC822',
113                                         date(DATE_RFC822, strtotime($line["updated"])), true);
114
115                                 $tpl->setVariable('ARTICLE_AUTHOR', htmlspecialchars($line['author']), true);
116
117                                 $tpl->setVariable('ARTICLE_SOURCE_LINK', htmlspecialchars($line['site_url']), true);
118                                 $tpl->setVariable('ARTICLE_SOURCE_TITLE', htmlspecialchars($line['feed_title']), true);
119
120                                 $tags = get_article_tags($line["id"], $owner_uid);
121
122                                 foreach ($tags as $tag) {
123                                         $tpl->setVariable('ARTICLE_CATEGORY', htmlspecialchars($tag), true);
124                                         $tpl->addBlock('category');
125                                 }
126
127                                 $enclosures = get_article_enclosures($line["id"]);
128
129                                 foreach ($enclosures as $e) {
130                                         $type = htmlspecialchars($e['content_type']);
131                                         $url = htmlspecialchars($e['content_url']);
132                                         $length = $e['duration'];
133
134                                         $tpl->setVariable('ARTICLE_ENCLOSURE_URL', $url, true);
135                                         $tpl->setVariable('ARTICLE_ENCLOSURE_TYPE', $type, true);
136                                         $tpl->setVariable('ARTICLE_ENCLOSURE_LENGTH', $length, true);
137
138                                         $tpl->addBlock('enclosure');
139                                 }
140
141                                 $tpl->addBlock('entry');
142                         }
143
144                         $tmp = "";
145
146                         $tpl->addBlock('feed');
147                         $tpl->generateOutputToString($tmp);
148
149                         if (@!$_REQUEST["noxml"]) {
150                                 header("Content-Type: text/xml; charset=utf-8");
151                         } else {
152                                 header("Content-Type: text/plain; charset=utf-8");
153                         }
154
155                         print $tmp;
156                 } else if ($format == 'json') {
157
158                         $feed = array();
159
160                         $feed['title'] = $feed_title;
161                         $feed['version'] = VERSION;
162                         $feed['feed_url'] = $feed_self_url;
163
164                         if (PUBSUBHUBBUB_HUB && $feed == -2) {
165                                 $feed['hub_url'] = PUBSUBHUBBUB_HUB;
166                         }
167
168                         $feed['self_url'] = get_self_url_prefix();
169
170                         $feed['articles'] = array();
171
172                         while ($line = $this->dbh->fetch_assoc($result)) {
173                                 $article = array();
174
175                                 $article['id'] = $line['link'];
176                                 $article['link']        = $line['link'];
177                                 $article['title'] = $line['title'];
178                                 $article['excerpt'] = truncate_string(strip_tags($line["content_preview"]), 100, '...');
179                                 $article['content'] = sanitize($line["content_preview"], false, $owner_uid);
180                                 $article['updated'] = date('c', strtotime($line["updated"]));
181
182                                 if ($line['note']) $article['note'] = $line['note'];
183                                 if ($article['author']) $article['author'] = $line['author'];
184
185                                 $tags = get_article_tags($line["id"], $owner_uid);
186
187                                 if (count($tags) > 0) {
188                                         $article['tags'] = array();
189
190                                         foreach ($tags as $tag) {
191                                                 array_push($article['tags'], $tag);
192                                         }
193                                 }
194
195                                 $enclosures = get_article_enclosures($line["id"]);
196
197                                 if (count($enclosures) > 0) {
198                                         $article['enclosures'] = array();
199
200                                         foreach ($enclosures as $e) {
201                                                 $type = $e['content_type'];
202                                                 $url = $e['content_url'];
203                                                 $length = $e['duration'];
204
205                                                 array_push($article['enclosures'], array("url" => $url, "type" => $type, "length" => $length));
206                                         }
207                                 }
208
209                                 array_push($feed['articles'], $article);
210                         }
211
212                         header("Content-Type: text/json; charset=utf-8");
213                         print json_encode($feed);
214
215                 } else {
216                         header("Content-Type: text/plain; charset=utf-8");
217                         print json_encode(array("error" => array("message" => "Unknown format")));
218                 }
219         }
220
221         function getUnread() {
222                 $login = $this->dbh->escape_string($_REQUEST["login"]);
223                 $fresh = $_REQUEST["fresh"] == "1";
224
225                 $result = $this->dbh->query("SELECT id FROM ttrss_users WHERE login = '$login'");
226
227                 if ($this->dbh->num_rows($result) == 1) {
228                         $uid = $this->dbh->fetch_result($result, 0, "id");
229
230                         print getGlobalUnread($uid);
231
232                         if ($fresh) {
233                                 print ";";
234                                 print getFeedArticles(-3, false, true, $uid);
235                         }
236
237                 } else {
238                         print "-1;User not found";
239                 }
240
241         }
242
243         function getProfiles() {
244                 $login = $this->dbh->escape_string($_REQUEST["login"]);
245
246                 $result = $this->dbh->query("SELECT ttrss_settings_profiles.* FROM ttrss_settings_profiles,ttrss_users
247                         WHERE ttrss_users.id = ttrss_settings_profiles.owner_uid AND login = '$login' ORDER BY title");
248
249                 print "<select dojoType='dijit.form.Select' style='width : 220px; margin : 0px' name='profile'>";
250
251                 print "<option value='0'>" . __("Default profile") . "</option>";
252
253                 while ($line = $this->dbh->fetch_assoc($result)) {
254                         $id = $line["id"];
255                         $title = $line["title"];
256
257                         print "<option value='$id'>$title</option>";
258                 }
259
260                 print "</select>";
261         }
262
263         function pubsub() {
264                 $mode = $this->dbh->escape_string($_REQUEST['hub_mode']);
265                 $feed_id = (int) $this->dbh->escape_string($_REQUEST['id']);
266                 $feed_url = $this->dbh->escape_string($_REQUEST['hub_topic']);
267
268                 if (!PUBSUBHUBBUB_ENABLED) {
269                         header('HTTP/1.0 404 Not Found');
270                         echo "404 Not found";
271                         return;
272                 }
273
274                 // TODO: implement hub_verifytoken checking
275
276                 $result = $this->dbh->query("SELECT feed_url FROM ttrss_feeds
277                         WHERE id = '$feed_id'");
278
279                 if ($this->dbh->num_rows($result) != 0) {
280
281                         $check_feed_url = $this->dbh->fetch_result($result, 0, "feed_url");
282
283                         if ($check_feed_url && ($check_feed_url == $feed_url || !$feed_url)) {
284                                 if ($mode == "subscribe") {
285
286                                         $this->dbh->query("UPDATE ttrss_feeds SET pubsub_state = 2
287                                                 WHERE id = '$feed_id'");
288
289                                         print $_REQUEST['hub_challenge'];
290                                         return;
291
292                                 } else if ($mode == "unsubscribe") {
293
294                                         $this->dbh->query("UPDATE ttrss_feeds SET pubsub_state = 0
295                                                 WHERE id = '$feed_id'");
296
297                                         print $_REQUEST['hub_challenge'];
298                                         return;
299
300                                 } else if (!$mode) {
301
302                                         // Received update ping, schedule feed update.
303                                         //update_rss_feed($feed_id, true, true);
304
305                                         $this->dbh->query("UPDATE ttrss_feeds SET
306                                                 last_update_started = '1970-01-01',
307                                                 last_updated = '1970-01-01' WHERE id = '$feed_id'");
308
309                                 }
310                         } else {
311                                 header('HTTP/1.0 404 Not Found');
312                                 echo "404 Not found";
313                         }
314                 } else {
315                         header('HTTP/1.0 404 Not Found');
316                         echo "404 Not found";
317                 }
318
319         }
320
321         function logout() {
322                 logout_user();
323                 header("Location: index.php");
324         }
325
326         function share() {
327                 $uuid = $this->dbh->escape_string($_REQUEST["key"]);
328
329                 $result = $this->dbh->query("SELECT ref_id, owner_uid FROM ttrss_user_entries WHERE
330                         uuid = '$uuid'");
331
332                 if ($this->dbh->num_rows($result) != 0) {
333                         header("Content-Type: text/html");
334
335                         $id = $this->dbh->fetch_result($result, 0, "ref_id");
336                         $owner_uid = $this->dbh->fetch_result($result, 0, "owner_uid");
337
338                         $article = format_article($id, false, true, $owner_uid);
339
340                         print_r($article['content']);
341
342                 } else {
343                         print "Article not found.";
344                 }
345
346         }
347
348         function rss() {
349                 $feed = $this->dbh->escape_string($_REQUEST["id"]);
350                 $key = $this->dbh->escape_string($_REQUEST["key"]);
351                 $is_cat = $_REQUEST["is_cat"] != "false";
352                 $limit = (int)$this->dbh->escape_string($_REQUEST["limit"]);
353                 $offset = (int)$this->dbh->escape_string($_REQUEST["offset"]);
354
355                 $search = $this->dbh->escape_string($_REQUEST["q"]);
356                 $search_mode = $this->dbh->escape_string($_REQUEST["smode"]);
357                 $view_mode = $this->dbh->escape_string($_REQUEST["view-mode"]);
358                 $order = $this->dbh->escape_string($_REQUEST["order"]);
359
360                 $format = $this->dbh->escape_string($_REQUEST['format']);
361                 $orig_guid = $_REQUEST["orig_guid"] != "false";
362
363                 if (!$format) $format = 'atom';
364
365                 if (SINGLE_USER_MODE) {
366                         authenticate_user("admin", null);
367                 }
368
369                 $owner_id = false;
370
371                 if ($key) {
372                         $result = $this->dbh->query("SELECT owner_uid FROM
373                                 ttrss_access_keys WHERE access_key = '$key' AND feed_id = '$feed'");
374
375                         if ($this->dbh->num_rows($result) == 1)
376                                 $owner_id = $this->dbh->fetch_result($result, 0, "owner_uid");
377                 }
378
379                 if ($owner_id) {
380                         $this->generate_syndicated_feed($owner_id, $feed, $is_cat, $limit,
381                                 $offset, $search, $search_mode, $view_mode, $format, $order, $orig_guid);
382                 } else {
383                         header('HTTP/1.1 403 Forbidden');
384                 }
385         }
386
387         function globalUpdateFeeds() {
388                 include "rssfuncs.php";
389                 // Update all feeds needing a update.
390                 update_daemon_common(0, true, false);
391                 housekeeping_common(false);
392
393                 PluginHost::getInstance()->run_hooks(PluginHost::HOOK_UPDATE_TASK, "hook_update_task", $op);
394
395         }
396
397         function sharepopup() {
398                 if (SINGLE_USER_MODE) {
399                         login_sequence();
400                 }
401
402                 header('Content-Type: text/html; charset=utf-8');
403                 print "<html><head><title>Tiny Tiny RSS</title>";
404
405                 stylesheet_tag("css/utility.css");
406                 javascript_tag("lib/prototype.js");
407                 javascript_tag("lib/scriptaculous/scriptaculous.js?load=effects,dragdrop,controls");
408                 print "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>
409                         </head><body id='sharepopup'>";
410
411                 $action = $_REQUEST["action"];
412
413                 if ($_SESSION["uid"]) {
414
415                         if ($action == 'share') {
416
417                                 $title = $this->dbh->escape_string(strip_tags($_REQUEST["title"]));
418                                 $url = $this->dbh->escape_string(strip_tags($_REQUEST["url"]));
419                                 $content = $this->dbh->escape_string(strip_tags($_REQUEST["content"]));
420                                 $labels = $this->dbh->escape_string(strip_tags($_REQUEST["labels"]));
421
422                                 Article::create_published_article($title, $url, $content, $labels,
423                                         $_SESSION["uid"]);
424
425                                 print "<script type='text/javascript'>";
426                                 print "window.close();";
427                                 print "</script>";
428
429                         } else {
430                                 $title = htmlspecialchars($_REQUEST["title"]);
431                                 $url = htmlspecialchars($_REQUEST["url"]);
432
433                                 ?>
434
435                                 <table height='100%' width='100%'><tr><td colspan='2'>
436                                 <h1><?php echo __("Share with Tiny Tiny RSS") ?></h1>
437                                 </td></tr>
438
439                                 <form id='share_form' name='share_form'>
440
441                                 <input type="hidden" name="op" value="sharepopup">
442                                 <input type="hidden" name="action" value="share">
443
444                                 <tr><td align='right'><?php echo __("Title:") ?></td>
445                                 <td width='80%'><input name='title' value="<?php echo $title ?>"></td></tr>
446                                 <tr><td align='right'><?php echo __("URL:") ?></td>
447                                 <td><input name='url' value="<?php echo $url ?>"></td></tr>
448                                 <tr><td align='right'><?php echo __("Content:") ?></td>
449                                 <td><input name='content' value=""></td></tr>
450                                 <tr><td align='right'><?php echo __("Labels:") ?></td>
451                                 <td><input name='labels' id="labels_value"
452                                         placeholder='Alpha, Beta, Gamma' value="">
453                                 </td></tr>
454
455                                 <tr><td>
456                                         <div class="autocomplete" id="labels_choices"
457                                                 style="display : block"></div></td></tr>
458
459                                 <script type='text/javascript'>document.forms[0].title.focus();</script>
460
461                                 <script type='text/javascript'>
462                                         new Ajax.Autocompleter('labels_value', 'labels_choices',
463                                    "backend.php?op=rpc&method=completeLabels",
464                                    { tokens: ',', paramName: "search" });
465                                 </script>
466
467                                 <tr><td colspan='2'>
468                                         <div style='float : right' class='insensitive-small'>
469                                         <?php echo __("Shared article will appear in the Published feed.") ?>
470                                         </div>
471                                         <button type="submit"><?php echo __('Share') ?></button>
472                                         <button onclick="return window.close()"><?php echo __('Cancel') ?></button>
473                                         </div>
474
475                                 </form>
476                                 </td></tr></table>
477                                 </body></html>
478                                 <?php
479
480                         }
481
482                 } else {
483
484                         $return = urlencode($_SERVER["REQUEST_URI"])
485                         ?>
486
487                         <form action="public.php?return=<?php echo $return ?>"
488                                 method="POST" id="loginForm" name="loginForm">
489
490                         <input type="hidden" name="op" value="login">
491
492                         <table height='100%' width='100%'><tr><td colspan='2'>
493                         <h1><?php echo __("Not logged in") ?></h1></td></tr>
494
495                         <tr><td align="right"><?php echo __("Login:") ?></td>
496                         <td align="right"><input name="login"
497                                 value="<?php echo $_SESSION["fake_login"] ?>"></td></tr>
498                                 <tr><td align="right"><?php echo __("Password:") ?></td>
499                                 <td align="right"><input type="password" name="password"
500                                 value="<?php echo $_SESSION["fake_password"] ?>"></td></tr>
501                         <tr><td colspan='2'>
502                                 <button type="submit">
503                                         <?php echo __('Log in') ?></button>
504
505                                 <button onclick="return window.close()">
506                                         <?php echo __('Cancel') ?></button>
507                         </td></tr>
508                         </table>
509
510                         </form>
511                         <?php
512                 }
513         }
514
515         function login() {
516                 if (!SINGLE_USER_MODE) {
517
518                         $login = $this->dbh->escape_string($_POST["login"]);
519                         $password = $_POST["password"];
520                         $remember_me = $_POST["remember_me"];
521
522                         if ($remember_me) {
523                                 session_set_cookie_params(SESSION_COOKIE_LIFETIME);
524                         } else {
525                                 session_set_cookie_params(0);
526                         }
527
528                         @session_start();
529
530                         if (authenticate_user($login, $password)) {
531                                 $_POST["password"] = "";
532
533                                 if (get_schema_version() >= 120) {
534                                         $_SESSION["language"] = get_pref("USER_LANGUAGE", $_SESSION["uid"]);
535                                 }
536
537                                 $_SESSION["ref_schema_version"] = get_schema_version(true);
538                                 $_SESSION["bw_limit"] = !!$_POST["bw_limit"];
539
540                                 if ($_POST["profile"]) {
541
542                                         $profile = $this->dbh->escape_string($_POST["profile"]);
543
544                                         $result = $this->dbh->query("SELECT id FROM ttrss_settings_profiles
545                                                 WHERE id = '$profile' AND owner_uid = " . $_SESSION["uid"]);
546
547                                         if ($this->dbh->num_rows($result) != 0) {
548                                                 $_SESSION["profile"] = $profile;
549                                         }
550                                 }
551                         } else {
552                                 $_SESSION["login_error_msg"] = __("Incorrect username or password");
553                         }
554
555                         if ($_REQUEST['return']) {
556                                 header("Location: " . $_REQUEST['return']);
557                         } else {
558                                 header("Location: " . SELF_URL_PATH);
559                         }
560                 }
561         }
562
563         function subscribe() {
564                 if (SINGLE_USER_MODE) {
565                         login_sequence();
566                 }
567
568                 if ($_SESSION["uid"]) {
569
570                         $feed_url = $this->dbh->escape_string(trim($_REQUEST["feed_url"]));
571
572                         header('Content-Type: text/html; charset=utf-8');
573                         print "<html>
574                                 <head>
575                                         <title>Tiny Tiny RSS</title>
576                                         <link rel=\"stylesheet\" type=\"text/css\" href=\"css/utility.css\">
577                                         <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>
578                                 </head>
579                                 <body>
580                                 <img class=\"floatingLogo\" src=\"images/logo_small.png\"
581                                         alt=\"Tiny Tiny RSS\"/>
582                                         <h1>".__("Subscribe to feed...")."</h1><div class='content'>";
583
584                         $rc = subscribe_to_feed($feed_url);
585
586                         switch ($rc['code']) {
587                         case 0:
588                                 print_warning(T_sprintf("Already subscribed to <b>%s</b>.", $feed_url));
589                                 break;
590                         case 1:
591                                 print_notice(T_sprintf("Subscribed to <b>%s</b>.", $feed_url));
592                                 break;
593                         case 2:
594                                 print_error(T_sprintf("Could not subscribe to <b>%s</b>.", $feed_url));
595                                 break;
596                         case 3:
597                                 print_error(T_sprintf("No feeds found in <b>%s</b>.", $feed_url));
598                                 break;
599                         case 4:
600                                 print_notice(__("Multiple feed URLs found."));
601                                 $feed_urls = $rc["feeds"];
602                                 break;
603                         case 5:
604                                 print_error(T_sprintf("Could not subscribe to <b>%s</b>.<br>Can't download the Feed URL.", $feed_url));
605                                 break;
606                         }
607
608                         if ($feed_urls) {
609
610                                 print "<form action=\"public.php\">";
611                                 print "<input type=\"hidden\" name=\"op\" value=\"subscribe\">";
612
613                                 print "<select name=\"feed_url\">";
614
615                                 foreach ($feed_urls as $url => $name) {
616                                         $url = htmlspecialchars($url);
617                                         $name = htmlspecialchars($name);
618
619                                         print "<option value=\"$url\">$name</option>";
620                                 }
621
622                                 print "<input type=\"submit\" value=\"".__("Subscribe to selected feed").
623                                         "\">";
624
625                                 print "</form>";
626                         }
627
628                         $tp_uri = get_self_url_prefix() . "/prefs.php";
629                         $tt_uri = get_self_url_prefix();
630
631                         if ($rc['code'] <= 2){
632                                 $result = $this->dbh->query("SELECT id FROM ttrss_feeds WHERE
633                                         feed_url = '$feed_url' AND owner_uid = " . $_SESSION["uid"]);
634
635                                 $feed_id = $this->dbh->fetch_result($result, 0, "id");
636                         } else {
637                                 $feed_id = 0;
638                         }
639                         print "<p>";
640
641                         if ($feed_id) {
642                                 print "<form method=\"GET\" style='display: inline'
643                                         action=\"$tp_uri\">
644                                         <input type=\"hidden\" name=\"tab\" value=\"feedConfig\">
645                                         <input type=\"hidden\" name=\"method\" value=\"editFeed\">
646                                         <input type=\"hidden\" name=\"methodparam\" value=\"$feed_id\">
647                                         <input type=\"submit\" value=\"".__("Edit subscription options")."\">
648                                         </form>";
649                         }
650
651                         print "<form style='display: inline' method=\"GET\" action=\"$tt_uri\">
652                                 <input type=\"submit\" value=\"".__("Return to Tiny Tiny RSS")."\">
653                                 </form></p>";
654
655                         print "</div></body></html>";
656
657                 } else {
658                         render_login_form();
659                 }
660         }
661
662         function subscribe2() {
663                 $feed_url = $this->dbh->escape_string(trim($_REQUEST["feed_url"]));
664                 $cat_id = $this->dbh->escape_string($_REQUEST["cat_id"]);
665                 $from = $this->dbh->escape_string($_REQUEST["from"]);
666                 $feed_urls = array();
667
668                 /* only read authentication information from POST */
669
670                 $auth_login = $this->dbh->escape_string(trim($_POST["auth_login"]));
671                 $auth_pass = $this->dbh->escape_string(trim($_POST["auth_pass"]));
672
673                 $rc = subscribe_to_feed($feed_url, $cat_id, $auth_login, $auth_pass);
674
675                 switch ($rc) {
676                 case 1:
677                         print_notice(T_sprintf("Subscribed to <b>%s</b>.", $feed_url));
678                         break;
679                 case 2:
680                         print_error(T_sprintf("Could not subscribe to <b>%s</b>.", $feed_url));
681                         break;
682                 case 3:
683                         print_error(T_sprintf("No feeds found in <b>%s</b>.", $feed_url));
684                         break;
685                 case 0:
686                         print_warning(T_sprintf("Already subscribed to <b>%s</b>.", $feed_url));
687                         break;
688                 case 4:
689                         print_notice(__("Multiple feed URLs found."));
690                         $contents = @fetch_file_contents($url, false, $auth_login, $auth_pass);
691                         if (is_html($contents)) {
692                                 $feed_urls = get_feeds_from_html($url, $contents);
693                         }
694                         break;
695                 case 5:
696                         print_error(T_sprintf("Could not subscribe to <b>%s</b>.<br>Can't download the Feed URL.", $feed_url));
697                         break;
698                 }
699
700                 if ($feed_urls) {
701                         print "<form action=\"backend.php\">";
702                         print "<input type=\"hidden\" name=\"op\" value=\"pref-feeds\">";
703                         print "<input type=\"hidden\" name=\"quiet\" value=\"1\">";
704                         print "<input type=\"hidden\" name=\"method\" value=\"add\">";
705
706                         print "<select name=\"feed_url\">";
707
708                         foreach ($feed_urls as $url => $name) {
709                                 $url = htmlspecialchars($url);
710                                 $name = htmlspecialchars($name);
711                                 print "<option value=\"$url\">$name</option>";
712                         }
713
714                         print "<input type=\"submit\" value=\"".__("Subscribe to selected feed")."\">";
715                         print "</form>";
716                 }
717
718                 $tp_uri = get_self_url_prefix() . "/prefs.php";
719                 $tt_uri = get_self_url_prefix();
720
721                 if ($rc <= 2){
722                         $result = $this->dbh->query("SELECT id FROM ttrss_feeds WHERE
723                                 feed_url = '$feed_url' AND owner_uid = " . $_SESSION["uid"]);
724
725                         $feed_id = $this->dbh->fetch_result($result, 0, "id");
726                 } else {
727                         $feed_id = 0;
728                 }
729
730                 print "<p>";
731
732                 if ($feed_id) {
733                         print "<form method=\"GET\" style='display: inline'
734                                 action=\"$tp_uri\">
735                                 <input type=\"hidden\" name=\"tab\" value=\"feedConfig\">
736                                 <input type=\"hidden\" name=\"method\" value=\"editFeed\">
737                                 <input type=\"hidden\" name=\"methodparam\" value=\"$feed_id\">
738                                 <input type=\"submit\" value=\"".__("Edit subscription options")."\">
739                                 </form>";
740                 }
741
742                 print "<form style='display: inline' method=\"GET\" action=\"$tt_uri\">
743                         <input type=\"submit\" value=\"".__("Return to Tiny Tiny RSS")."\">
744                         </form></p>";
745
746                 print "</body></html>";
747         }
748
749         function index() {
750                 header("Content-Type: text/plain");
751                 print json_encode(array("error" => array("code" => 7)));
752         }
753
754         function forgotpass() {
755                 startup_gettext();
756
757                 header('Content-Type: text/html; charset=utf-8');
758                 print "<html><head><title>Tiny Tiny RSS</title>";
759
760                 stylesheet_tag("css/utility.css");
761                 javascript_tag("lib/prototype.js");
762
763                 print "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>
764                         </head><body id='forgotpass'>";
765
766                 print '<div class="floatingLogo"><img src="images/logo_small.png"></div>';
767                 print "<h1>".__("Password recovery")."</h1>";
768                 print "<div class='content'>";
769
770                 @$method = $_POST['method'];
771
772                 if (!$method) {
773                         print_notice(__("You will need to provide valid account name and email. New password will be sent on your email address."));
774
775                         print "<form method='POST' action='public.php'>";
776                         print "<input type='hidden' name='method' value='do'>";
777                         print "<input type='hidden' name='op' value='forgotpass'>";
778
779                         print "<fieldset>";
780                         print "<label>".__("Login:")."</label>";
781                         print "<input type='text' name='login' value='' required>";
782                         print "</fieldset>";
783
784                         print "<fieldset>";
785                         print "<label>".__("Email:")."</label>";
786                         print "<input type='email' name='email' value='' required>";
787                         print "</fieldset>";
788
789                         print "<fieldset>";
790                         print "<label>".__("How much is two plus two:")."</label>";
791                         print "<input type='text' name='test' value='' required>";
792                         print "</fieldset>";
793
794                         print "<p/>";
795                         print "<button type='submit'>".__("Reset password")."</button>";
796
797                         print "</form>";
798                 } else if ($method == 'do') {
799
800                         $login = $this->dbh->escape_string($_POST["login"]);
801                         $email = $this->dbh->escape_string($_POST["email"]);
802                         $test = $this->dbh->escape_string($_POST["test"]);
803
804                         if (($test != 4 && $test != 'four') || !$email || !$login) {
805                                 print_error(__('Some of the required form parameters are missing or incorrect.'));
806
807                                 print "<form method=\"GET\" action=\"public.php\">
808                                         <input type=\"hidden\" name=\"op\" value=\"forgotpass\">
809                                         <input type=\"submit\" value=\"".__("Go back")."\">
810                                         </form>";
811
812                         } else {
813
814                                 $result = $this->dbh->query("SELECT id FROM ttrss_users
815                                         WHERE login = '$login' AND email = '$email'");
816
817                                 if ($this->dbh->num_rows($result) != 0) {
818                                         $id = $this->dbh->fetch_result($result, 0, "id");
819
820                                         Pref_Users::resetUserPassword($id, false);
821
822                                         print "<p>";
823
824                                         print "<p>"."Completed."."</p>";
825
826                                         print "<form method=\"GET\" action=\"index.php\">
827                                                 <input type=\"submit\" value=\"".__("Return to Tiny Tiny RSS")."\">
828                                                 </form>";
829
830                                 } else {
831                                         print_error(__("Sorry, login and email combination not found."));
832
833                                         print "<form method=\"GET\" action=\"public.php\">
834                                                 <input type=\"hidden\" name=\"op\" value=\"forgotpass\">
835                                                 <input type=\"submit\" value=\"".__("Go back")."\">
836                                                 </form>";
837
838                                 }
839                         }
840
841                 }
842
843                 print "</div>";
844                 print "</body>";
845                 print "</html>";
846
847         }
848
849         function dbupdate() {
850                 startup_gettext();
851
852                 if (!SINGLE_USER_MODE && $_SESSION["access_level"] < 10) {
853                         $_SESSION["login_error_msg"] = __("Your access level is insufficient to run this script.");
854                         render_login_form();
855                         exit;
856                 }
857
858                 ?><html>
859                         <head>
860                         <title>Database Updater</title>
861                         <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
862                         <link rel="stylesheet" type="text/css" href="css/utility.css"/>
863                         </head>
864                         <style type="text/css">
865                                 span.ok { color : #009000; font-weight : bold; }
866                                 span.err { color : #ff0000; font-weight : bold; }
867                         </style>
868                 <body>
869                         <script type='text/javascript'>
870                         function confirmOP() {
871                                 return confirm("Update the database?");
872                         }
873                         </script>
874
875                         <div class="floatingLogo"><img src="images/logo_small.png"></div>
876
877                         <h1><?php echo __("Database Updater") ?></h1>
878
879                         <div class="content">
880
881                         <?php
882                                 @$op = $_REQUEST["subop"];
883                                 $updater = new DbUpdater(Db::get(), DB_TYPE, SCHEMA_VERSION);
884
885                                 if ($op == "performupdate") {
886                                         if ($updater->isUpdateRequired()) {
887
888                                                 print "<h2>Performing updates</h2>";
889
890                                                 print "<h3>Updating to schema version " . SCHEMA_VERSION . "</h3>";
891
892                                                 print "<ul>";
893
894                                                 for ($i = $updater->getSchemaVersion() + 1; $i <= SCHEMA_VERSION; $i++) {
895                                                         print "<li>Performing update up to version $i...";
896
897                                                         $result = $updater->performUpdateTo($i);
898
899                                                         if (!$result) {
900                                                                 print "<span class='err'>FAILED!</span></li></ul>";
901
902                                                                 print_warning("One of the updates failed. Either retry the process or perform updates manually.");
903                                                                 print "<p><form method=\"GET\" action=\"index.php\">
904                                                                 <input type=\"submit\" value=\"".__("Return to Tiny Tiny RSS")."\">
905                                                                 </form>";
906
907                                                                 break;
908                                                         } else {
909                                                                 print "<span class='ok'>OK!</span></li>";
910                                                         }
911                                                 }
912
913                                                 print "</ul>";
914
915                                                 print_notice("Your Tiny Tiny RSS database is now updated to the latest version.");
916
917                                                 print "<p><form method=\"GET\" action=\"index.php\">
918                                                 <input type=\"submit\" value=\"".__("Return to Tiny Tiny RSS")."\">
919                                                 </form>";
920
921                                         } else {
922                                                 print "<h2>Your database is up to date.</h2>";
923
924                                                 print "<p><form method=\"GET\" action=\"index.php\">
925                                                 <input type=\"submit\" value=\"".__("Return to Tiny Tiny RSS")."\">
926                                                 </form>";
927                                         }
928                                 } else {
929                                         if ($updater->isUpdateRequired()) {
930
931                                                 print "<h2>Database update required</h2>";
932
933                                                 print "<h3>";
934                                                 printf("Your Tiny Tiny RSS database needs update to the latest version: %d to %d.",
935                                                         $updater->getSchemaVersion(), SCHEMA_VERSION);
936                                                 print "</h3>";
937
938                                                 print_warning("Please backup your database before proceeding.");
939
940                                                 print "<form method='POST'>
941                                                         <input type='hidden' name='subop' value='performupdate'>
942                                                         <input type='submit' onclick='return confirmOP()' value='".__("Perform updates")."'>
943                                                 </form>";
944
945                                         } else {
946
947                                                 print_notice("Tiny Tiny RSS database is up to date.");
948
949                                                 print "<p><form method=\"GET\" action=\"index.php\">
950                                                         <input type=\"submit\" value=\"".__("Return to Tiny Tiny RSS")."\">
951                                                 </form>";
952
953                                         }
954                                 }
955                         ?>
956
957                         </div>
958                         </body>
959                         </html>
960                 <?php
961         }
962
963 }
964 ?>