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