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