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