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