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