public.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
  1. <?php
  2. class Handler_Public extends Handler {
  3. private function generate_syndicated_feed($owner_uid, $feed, $is_cat,
  4. $limit, $offset, $search,
  5. $view_mode = false, $format = 'atom', $order = false, $orig_guid = false, $start_ts = false) {
  6. require_once "lib/MiniTemplator.class.php";
  7. $note_style = "background-color : #fff7d5;
  8. border-width : 1px; ".
  9. "padding : 5px; border-style : dashed; border-color : #e7d796;".
  10. "margin-bottom : 1em; color : #9a8c59;";
  11. if (!$limit) $limit = 60;
  12. $date_sort_field = "date_entered DESC, updated DESC";
  13. $date_check_field = "date_entered";
  14. if ($feed == -2 && !$is_cat) {
  15. $date_sort_field = "last_published DESC";
  16. $date_check_field = "last_published";
  17. } else if ($feed == -1 && !$is_cat) {
  18. $date_sort_field = "last_marked DESC";
  19. $date_check_field = "last_marked";
  20. }
  21. switch ($order) {
  22. case "title":
  23. $date_sort_field = "ttrss_entries.title, date_entered, updated";
  24. break;
  25. case "date_reverse":
  26. $date_sort_field = "date_entered, updated";
  27. break;
  28. case "feed_dates":
  29. $date_sort_field = "updated DESC";
  30. break;
  31. }
  32. $params = array(
  33. "owner_uid" => $owner_uid,
  34. "feed" => $feed,
  35. "limit" => $limit,
  36. "view_mode" => $view_mode,
  37. "cat_view" => $is_cat,
  38. "search" => $search,
  39. "override_order" => $date_sort_field,
  40. "include_children" => true,
  41. "ignore_vfeed_group" => true,
  42. "offset" => $offset,
  43. "start_ts" => $start_ts
  44. );
  45. $qfh_ret = Feeds::queryFeedHeadlines($params);
  46. $result = $qfh_ret[0];
  47. $feed_title = htmlspecialchars($qfh_ret[1]);
  48. $feed_site_url = $qfh_ret[2];
  49. /* $last_error = $qfh_ret[3]; */
  50. $feed_self_url = get_self_url_prefix() .
  51. "/public.php?op=rss&id=$feed&key=" .
  52. get_feed_access_key($feed, false, $owner_uid);
  53. if (!$feed_site_url) $feed_site_url = get_self_url_prefix();
  54. if ($format == 'atom') {
  55. $tpl = new MiniTemplator;
  56. $tpl->readTemplateFromFile("templates/generated_feed.txt");
  57. $tpl->setVariable('FEED_TITLE', $feed_title, true);
  58. $tpl->setVariable('VERSION', VERSION, true);
  59. $tpl->setVariable('FEED_URL', htmlspecialchars($feed_self_url), true);
  60. $tpl->setVariable('SELF_URL', htmlspecialchars(get_self_url_prefix()), true);
  61. while ($line = $result->fetch()) {
  62. $line["content_preview"] = sanitize(truncate_string(strip_tags($line["content"]), 100, '...'));
  63. foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_QUERY_HEADLINES) as $p) {
  64. $line = $p->hook_query_headlines($line);
  65. }
  66. foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_ARTICLE_EXPORT_FEED) as $p) {
  67. $line = $p->hook_article_export_feed($line, $feed, $is_cat);
  68. }
  69. $tpl->setVariable('ARTICLE_ID',
  70. htmlspecialchars($orig_guid ? $line['link'] :
  71. $this->make_article_tag_uri($line['id'], $line['date_entered'])), true);
  72. $tpl->setVariable('ARTICLE_LINK', htmlspecialchars($line['link']), true);
  73. $tpl->setVariable('ARTICLE_TITLE', htmlspecialchars($line['title']), true);
  74. $tpl->setVariable('ARTICLE_EXCERPT', $line["content_preview"], true);
  75. $content = sanitize($line["content"], false, $owner_uid,
  76. $feed_site_url, false, $line["id"]);
  77. if ($line['note']) {
  78. $content = "<div style=\"$note_style\">Article note: " . $line['note'] . "</div>" .
  79. $content;
  80. $tpl->setVariable('ARTICLE_NOTE', htmlspecialchars($line['note']), true);
  81. }
  82. $tpl->setVariable('ARTICLE_CONTENT', $content, true);
  83. $tpl->setVariable('ARTICLE_UPDATED_ATOM',
  84. date('c', strtotime($line["updated"])), true);
  85. $tpl->setVariable('ARTICLE_UPDATED_RFC822',
  86. date(DATE_RFC822, strtotime($line["updated"])), true);
  87. $tpl->setVariable('ARTICLE_AUTHOR', htmlspecialchars($line['author']), true);
  88. $tpl->setVariable('ARTICLE_SOURCE_LINK', htmlspecialchars($line['site_url'] ? $line["site_url"] : get_self_url_prefix()), true);
  89. $tpl->setVariable('ARTICLE_SOURCE_TITLE', htmlspecialchars($line['feed_title'] ? $line['feed_title'] : $feed_title), true);
  90. $tags = Article::get_article_tags($line["id"], $owner_uid);
  91. foreach ($tags as $tag) {
  92. $tpl->setVariable('ARTICLE_CATEGORY', htmlspecialchars($tag), true);
  93. $tpl->addBlock('category');
  94. }
  95. $enclosures = Article::get_article_enclosures($line["id"]);
  96. foreach ($enclosures as $e) {
  97. $type = htmlspecialchars($e['content_type']);
  98. $url = htmlspecialchars($e['content_url']);
  99. $length = $e['duration'] ? $e['duration'] : 1;
  100. $tpl->setVariable('ARTICLE_ENCLOSURE_URL', $url, true);
  101. $tpl->setVariable('ARTICLE_ENCLOSURE_TYPE', $type, true);
  102. $tpl->setVariable('ARTICLE_ENCLOSURE_LENGTH', $length, true);
  103. $tpl->addBlock('enclosure');
  104. }
  105. $tpl->addBlock('entry');
  106. }
  107. $tmp = "";
  108. $tpl->addBlock('feed');
  109. $tpl->generateOutputToString($tmp);
  110. if (@!$_REQUEST["noxml"]) {
  111. header("Content-Type: text/xml; charset=utf-8");
  112. } else {
  113. header("Content-Type: text/plain; charset=utf-8");
  114. }
  115. print $tmp;
  116. } else if ($format == 'json') {
  117. $feed = array();
  118. $feed['title'] = $feed_title;
  119. $feed['version'] = VERSION;
  120. $feed['feed_url'] = $feed_self_url;
  121. $feed['self_url'] = get_self_url_prefix();
  122. $feed['articles'] = array();
  123. while ($line = $result->fetch()) {
  124. $line["content_preview"] = sanitize(truncate_string(strip_tags($line["content_preview"]), 100, '...'));
  125. foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_QUERY_HEADLINES) as $p) {
  126. $line = $p->hook_query_headlines($line, 100);
  127. }
  128. foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_ARTICLE_EXPORT_FEED) as $p) {
  129. $line = $p->hook_article_export_feed($line, $feed, $is_cat);
  130. }
  131. $article = array();
  132. $article['id'] = $line['link'];
  133. $article['link'] = $line['link'];
  134. $article['title'] = $line['title'];
  135. $article['excerpt'] = $line["content_preview"];
  136. $article['content'] = sanitize($line["content"], false, $owner_uid, $feed_site_url, false, $line["id"]);
  137. $article['updated'] = date('c', strtotime($line["updated"]));
  138. if ($line['note']) $article['note'] = $line['note'];
  139. if ($article['author']) $article['author'] = $line['author'];
  140. $tags = Article::get_article_tags($line["id"], $owner_uid);
  141. if (count($tags) > 0) {
  142. $article['tags'] = array();
  143. foreach ($tags as $tag) {
  144. array_push($article['tags'], $tag);
  145. }
  146. }
  147. $enclosures = Article::get_article_enclosures($line["id"]);
  148. if (count($enclosures) > 0) {
  149. $article['enclosures'] = array();
  150. foreach ($enclosures as $e) {
  151. $type = $e['content_type'];
  152. $url = $e['content_url'];
  153. $length = $e['duration'];
  154. array_push($article['enclosures'], array("url" => $url, "type" => $type, "length" => $length));
  155. }
  156. }
  157. array_push($feed['articles'], $article);
  158. }
  159. header("Content-Type: text/json; charset=utf-8");
  160. print json_encode($feed);
  161. } else {
  162. header("Content-Type: text/plain; charset=utf-8");
  163. print json_encode(array("error" => array("message" => "Unknown format")));
  164. }
  165. }
  166. function getUnread() {
  167. $login = $_REQUEST["login"];
  168. $fresh = $_REQUEST["fresh"] == "1";
  169. $sth = $this->pdo->prepare("SELECT id FROM ttrss_users WHERE login = ?");
  170. $sth->execute([$login]);
  171. if ($row = $sth->fetch()) {
  172. $uid = $row["id"];
  173. print Feeds::getGlobalUnread($uid);
  174. if ($fresh) {
  175. print ";";
  176. print Feeds::getFeedArticles(-3, false, true, $uid);
  177. }
  178. } else {
  179. print "-1;User not found";
  180. }
  181. }
  182. function getProfiles() {
  183. $login = $_REQUEST["login"];
  184. $sth = $this->pdo->prepare("SELECT ttrss_settings_profiles.* FROM ttrss_settings_profiles,ttrss_users
  185. WHERE ttrss_users.id = ttrss_settings_profiles.owner_uid AND login = ? ORDER BY title");
  186. $sth->execute([$login]);
  187. print "<select dojoType='dijit.form.Select' style='width : 220px; margin : 0px' name='profile'>";
  188. print "<option value='0'>" . __("Default profile") . "</option>";
  189. while ($line = $sth->fetch()) {
  190. $id = $line["id"];
  191. $title = $line["title"];
  192. print "<option value='$id'>$title</option>";
  193. }
  194. print "</select>";
  195. }
  196. function logout() {
  197. logout_user();
  198. header("Location: index.php");
  199. }
  200. function share() {
  201. $uuid = $_REQUEST["key"];
  202. $sth = $this->pdo->prepare("SELECT ref_id, owner_uid FROM ttrss_user_entries WHERE
  203. uuid = ?");
  204. $sth->execute([$uuid]);
  205. if ($row = $sth->fetch()) {
  206. header("Content-Type: text/html");
  207. $id = $row["ref_id"];
  208. $owner_uid = $row["owner_uid"];
  209. $article = Article::format_article($id, false, true, $owner_uid);
  210. print_r($article['content']);
  211. } else {
  212. print "Article not found.";
  213. }
  214. }
  215. function rss() {
  216. $feed = $_REQUEST["id"];
  217. $key = $_REQUEST["key"];
  218. $is_cat = sql_bool_to_bool($_REQUEST["is_cat"]);
  219. $limit = (int)$_REQUEST["limit"];
  220. $offset = (int)$_REQUEST["offset"];
  221. $search = $_REQUEST["q"];
  222. $view_mode = $_REQUEST["view-mode"];
  223. $order = $_REQUEST["order"];
  224. $start_ts = $_REQUEST["ts"];
  225. $format = $_REQUEST['format'];
  226. $orig_guid = sql_bool_to_bool($_REQUEST["orig_guid"]);
  227. if (!$format) $format = 'atom';
  228. if (SINGLE_USER_MODE) {
  229. authenticate_user("admin", null);
  230. }
  231. $owner_id = false;
  232. if ($key) {
  233. $sth = $this->pdo->prepare("SELECT owner_uid FROM
  234. ttrss_access_keys WHERE access_key = ? AND feed_id = ?");
  235. $sth->execute([$key, $feed]);
  236. if ($row = $sth->fetch())
  237. $owner_id = $row["owner_uid"];
  238. }
  239. if ($owner_id) {
  240. $this->generate_syndicated_feed($owner_id, $feed, $is_cat, $limit,
  241. $offset, $search, $view_mode, $format, $order, $orig_guid, $start_ts);
  242. } else {
  243. header('HTTP/1.1 403 Forbidden');
  244. }
  245. }
  246. function updateTask() {
  247. PluginHost::getInstance()->run_hooks(PluginHost::HOOK_UPDATE_TASK, "hook_update_task", false);
  248. }
  249. function housekeepingTask() {
  250. PluginHost::getInstance()->run_hooks(PluginHost::HOOK_HOUSE_KEEPING, "hook_house_keeping", false);
  251. }
  252. function globalUpdateFeeds() {
  253. RPC::updaterandomfeed_real($this->dbh);
  254. PluginHost::getInstance()->run_hooks(PluginHost::HOOK_UPDATE_TASK, "hook_update_task", false);
  255. }
  256. function sharepopup() {
  257. if (SINGLE_USER_MODE) {
  258. login_sequence();
  259. }
  260. header('Content-Type: text/html; charset=utf-8');
  261. print "<html><head><title>Tiny Tiny RSS</title>
  262. <link rel=\"shortcut icon\" type=\"image/png\" href=\"images/favicon.png\">
  263. <link rel=\"icon\" type=\"image/png\" sizes=\"72x72\" href=\"images/favicon-72px.png\">";
  264. echo stylesheet_tag("css/utility.css");
  265. echo stylesheet_tag("css/default.css");
  266. echo javascript_tag("lib/prototype.js");
  267. echo javascript_tag("lib/scriptaculous/scriptaculous.js?load=effects,controls");
  268. print "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>
  269. </head><body id='sharepopup'>";
  270. $action = $_REQUEST["action"];
  271. if ($_SESSION["uid"]) {
  272. if ($action == 'share') {
  273. $title = strip_tags($_REQUEST["title"]);
  274. $url = strip_tags($_REQUEST["url"]);
  275. $content = strip_tags($_REQUEST["content"]);
  276. $labels = strip_tags($_REQUEST["labels"]);
  277. Article::create_published_article($title, $url, $content, $labels,
  278. $_SESSION["uid"]);
  279. print "<script type='text/javascript'>";
  280. print "window.close();";
  281. print "</script>";
  282. } else {
  283. $title = htmlspecialchars($_REQUEST["title"]);
  284. $url = htmlspecialchars($_REQUEST["url"]);
  285. ?>
  286. <table height='100%' width='100%'><tr><td colspan='2'>
  287. <h1><?php echo __("Share with Tiny Tiny RSS") ?></h1>
  288. </td></tr>
  289. <form id='share_form' name='share_form'>
  290. <input type="hidden" name="op" value="sharepopup">
  291. <input type="hidden" name="action" value="share">
  292. <tr><td align='right'><?php echo __("Title:") ?></td>
  293. <td width='80%'><input name='title' value="<?php echo $title ?>"></td></tr>
  294. <tr><td align='right'><?php echo __("URL:") ?></td>
  295. <td><input name='url' value="<?php echo $url ?>"></td></tr>
  296. <tr><td align='right'><?php echo __("Content:") ?></td>
  297. <td><input name='content' value=""></td></tr>
  298. <tr><td align='right'><?php echo __("Labels:") ?></td>
  299. <td><input name='labels' id="labels_value"
  300. placeholder='Alpha, Beta, Gamma' value="">
  301. </td></tr>
  302. <tr><td>
  303. <div class="autocomplete" id="labels_choices"
  304. style="display : block"></div></td></tr>
  305. <script type='text/javascript'>document.forms[0].title.focus();</script>
  306. <script type='text/javascript'>
  307. new Ajax.Autocompleter('labels_value', 'labels_choices',
  308. "backend.php?op=rpc&method=completeLabels",
  309. { tokens: ',', paramName: "search" });
  310. </script>
  311. <tr><td colspan='2'>
  312. <div style='float : right' class='insensitive-small'>
  313. <?php echo __("Shared article will appear in the Published feed.") ?>
  314. </div>
  315. <button type="submit"><?php echo __('Share') ?></button>
  316. <button onclick="return window.close()"><?php echo __('Cancel') ?></button>
  317. </td>
  318. </form>
  319. </td></tr></table>
  320. </body></html>
  321. <?php
  322. }
  323. } else {
  324. $return = urlencode($_SERVER["REQUEST_URI"])
  325. ?>
  326. <form action="public.php?return=<?php echo $return ?>"
  327. method="POST" id="loginForm" name="loginForm">
  328. <input type="hidden" name="op" value="login">
  329. <table height='100%' width='100%'><tr><td colspan='2'>
  330. <h1><?php echo __("Not logged in") ?></h1></td></tr>
  331. <tr><td align="right"><?php echo __("Login:") ?></td>
  332. <td align="right"><input name="login"
  333. value="<?php echo $_SESSION["fake_login"] ?>"></td></tr>
  334. <tr><td align="right"><?php echo __("Password:") ?></td>
  335. <td align="right"><input type="password" name="password"
  336. value="<?php echo $_SESSION["fake_password"] ?>"></td></tr>
  337. <tr><td colspan='2'>
  338. <button type="submit">
  339. <?php echo __('Log in') ?></button>
  340. <button onclick="return window.close()">
  341. <?php echo __('Cancel') ?></button>
  342. </td></tr>
  343. </table>
  344. </form>
  345. <?php
  346. }
  347. }
  348. function login() {
  349. if (!SINGLE_USER_MODE) {
  350. $login = $_POST["login"];
  351. $password = $_POST["password"];
  352. $remember_me = $_POST["remember_me"];
  353. if ($remember_me) {
  354. session_set_cookie_params(SESSION_COOKIE_LIFETIME);
  355. } else {
  356. session_set_cookie_params(0);
  357. }
  358. @session_start();
  359. if (authenticate_user($login, $password)) {
  360. $_POST["password"] = "";
  361. if (get_schema_version() >= 120) {
  362. $_SESSION["language"] = get_pref("USER_LANGUAGE", $_SESSION["uid"]);
  363. }
  364. $_SESSION["ref_schema_version"] = get_schema_version(true);
  365. $_SESSION["bw_limit"] = !!$_POST["bw_limit"];
  366. if ($_POST["profile"]) {
  367. $profile = $_POST["profile"];
  368. $sth = $this->pdo->prepare("SELECT id FROM ttrss_settings_profiles
  369. WHERE id = ? AND owner_uid = ?");
  370. $sth->execute([$profile, $_SESSION['uid']]);
  371. if ($sth->fetch()) {
  372. $_SESSION["profile"] = $profile;
  373. }
  374. }
  375. } else {
  376. $_SESSION["login_error_msg"] = __("Incorrect username or password");
  377. user_error("Failed login attempt for $login from {$_SERVER['REMOTE_ADDR']}", E_USER_WARNING);
  378. }
  379. if ($_REQUEST['return']) {
  380. header("Location: " . $_REQUEST['return']);
  381. } else {
  382. header("Location: " . get_self_url_prefix());
  383. }
  384. }
  385. }
  386. /* function subtest() {
  387. header("Content-type: text/plain; charset=utf-8");
  388. $url = $_REQUEST["url"];
  389. print "$url\n\n";
  390. print_r(get_feeds_from_html($url, fetch_file_contents($url)));
  391. } */
  392. function subscribe() {
  393. if (SINGLE_USER_MODE) {
  394. login_sequence();
  395. }
  396. if ($_SESSION["uid"]) {
  397. $feed_url = trim($_REQUEST["feed_url"]);
  398. header('Content-Type: text/html; charset=utf-8');
  399. print "<html>
  400. <head>
  401. <title>Tiny Tiny RSS</title>
  402. <link rel=\"stylesheet\" type=\"text/css\" href=\"css/utility.css\">
  403. <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>
  404. <link rel=\"shortcut icon\" type=\"image/png\" href=\"images/favicon.png\">
  405. <link rel=\"icon\" type=\"image/png\" sizes=\"72x72\" href=\"images/favicon-72px.png\">
  406. </head>
  407. <body>
  408. <img class=\"floatingLogo\" src=\"images/logo_small.png\"
  409. alt=\"Tiny Tiny RSS\"/>
  410. <h1>".__("Subscribe to feed...")."</h1><div class='content'>";
  411. $rc = Feeds::subscribe_to_feed($feed_url);
  412. switch ($rc['code']) {
  413. case 0:
  414. print_warning(T_sprintf("Already subscribed to <b>%s</b>.", $feed_url));
  415. break;
  416. case 1:
  417. print_notice(T_sprintf("Subscribed to <b>%s</b>.", $feed_url));
  418. break;
  419. case 2:
  420. print_error(T_sprintf("Could not subscribe to <b>%s</b>.", $feed_url));
  421. break;
  422. case 3:
  423. print_error(T_sprintf("No feeds found in <b>%s</b>.", $feed_url));
  424. break;
  425. case 4:
  426. print_notice(__("Multiple feed URLs found."));
  427. $feed_urls = $rc["feeds"];
  428. break;
  429. case 5:
  430. print_error(T_sprintf("Could not subscribe to <b>%s</b>.<br>Can't download the Feed URL.", $feed_url));
  431. break;
  432. }
  433. if ($feed_urls) {
  434. print "<form action=\"public.php\">";
  435. print "<input type=\"hidden\" name=\"op\" value=\"subscribe\">";
  436. print "<select name=\"feed_url\">";
  437. foreach ($feed_urls as $url => $name) {
  438. $url = htmlspecialchars($url);
  439. $name = htmlspecialchars($name);
  440. print "<option value=\"$url\">$name</option>";
  441. }
  442. print "<input type=\"submit\" value=\"".__("Subscribe to selected feed").
  443. "\">";
  444. print "</form>";
  445. }
  446. $tp_uri = get_self_url_prefix() . "/prefs.php";
  447. $tt_uri = get_self_url_prefix();
  448. if ($rc['code'] <= 2){
  449. $sth = $this->pdo->prepare("SELECT id FROM ttrss_feeds WHERE
  450. feed_url = ? AND owner_uid = ?");
  451. $sth->execute([$feed_url, $_SESSION['uid']]);
  452. $row = $sth->fetch();
  453. $feed_id = $row["id"];
  454. } else {
  455. $feed_id = 0;
  456. }
  457. print "<p>";
  458. if ($feed_id) {
  459. print "<form method=\"GET\" style='display: inline'
  460. action=\"$tp_uri\">
  461. <input type=\"hidden\" name=\"tab\" value=\"feedConfig\">
  462. <input type=\"hidden\" name=\"method\" value=\"editFeed\">
  463. <input type=\"hidden\" name=\"methodparam\" value=\"$feed_id\">
  464. <input type=\"submit\" value=\"".__("Edit subscription options")."\">
  465. </form>";
  466. }
  467. print "<form style='display: inline' method=\"GET\" action=\"$tt_uri\">
  468. <input type=\"submit\" value=\"".__("Return to Tiny Tiny RSS")."\">
  469. </form></p>";
  470. print "</div></body></html>";
  471. } else {
  472. render_login_form();
  473. }
  474. }
  475. function index() {
  476. header("Content-Type: text/plain");
  477. print error_json(13);
  478. }
  479. function forgotpass() {
  480. startup_gettext();
  481. @$hash = $_REQUEST["hash"];
  482. header('Content-Type: text/html; charset=utf-8');
  483. print "<html><head><title>Tiny Tiny RSS</title>
  484. <link rel=\"shortcut icon\" type=\"image/png\" href=\"images/favicon.png\">
  485. <link rel=\"icon\" type=\"image/png\" sizes=\"72x72\" href=\"images/favicon-72px.png\">";
  486. echo stylesheet_tag("css/utility.css");
  487. echo javascript_tag("lib/prototype.js");
  488. print "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>
  489. </head><body id='forgotpass'>";
  490. print '<div class="floatingLogo"><img src="images/logo_small.png"></div>';
  491. print "<h1>".__("Password recovery")."</h1>";
  492. print "<div class='content'>";
  493. @$method = $_POST['method'];
  494. if ($hash) {
  495. $login = $_REQUEST["login"];
  496. if ($login) {
  497. $sth = $this->pdo->prepare("SELECT id, resetpass_token FROM ttrss_users
  498. WHERE login = ?");
  499. $sth->execute([$login]);
  500. if ($row = $sth->fetch()) {
  501. $id = $row["id"];
  502. $resetpass_token_full = $row["resetpass_token"];
  503. list($timestamp, $resetpass_token) = explode(":", $resetpass_token_full);
  504. if ($timestamp && $resetpass_token &&
  505. $timestamp >= time() - 15*60*60 &&
  506. $resetpass_token == $hash) {
  507. $sth = $this->pdo->prepare("UPDATE ttrss_users SET resetpass_token = NULL
  508. WHERE id = ?");
  509. $sth->execute([$id]);
  510. Pref_Users::resetUserPassword($id, true);
  511. print "<p>"."Completed."."</p>";
  512. } else {
  513. print_error("Some of the information provided is missing or incorrect.");
  514. }
  515. } else {
  516. print_error("Some of the information provided is missing or incorrect.");
  517. }
  518. } else {
  519. print_error("Some of the information provided is missing or incorrect.");
  520. }
  521. print "<form method=\"GET\" action=\"index.php\">
  522. <input type=\"submit\" value=\"".__("Return to Tiny Tiny RSS")."\">
  523. </form>";
  524. } else if (!$method) {
  525. print_notice(__("You will need to provide valid account name and email. A password reset link will be sent to your email address."));
  526. print "<form method='POST' action='public.php'>";
  527. print "<input type='hidden' name='method' value='do'>";
  528. print "<input type='hidden' name='op' value='forgotpass'>";
  529. print "<fieldset>";
  530. print "<label>".__("Login:")."</label>";
  531. print "<input type='text' name='login' value='' required>";
  532. print "</fieldset>";
  533. print "<fieldset>";
  534. print "<label>".__("Email:")."</label>";
  535. print "<input type='email' name='email' value='' required>";
  536. print "</fieldset>";
  537. print "<fieldset>";
  538. print "<label>".__("How much is two plus two:")."</label>";
  539. print "<input type='text' name='test' value='' required>";
  540. print "</fieldset>";
  541. print "<p/>";
  542. print "<button type='submit'>".__("Reset password")."</button>";
  543. print "</form>";
  544. } else if ($method == 'do') {
  545. $login = $_POST["login"];
  546. $email = $_POST["email"];
  547. $test = $_POST["test"];
  548. if (($test != 4 && $test != 'four') || !$email || !$login) {
  549. print_error(__('Some of the required form parameters are missing or incorrect.'));
  550. print "<form method=\"GET\" action=\"public.php\">
  551. <input type=\"hidden\" name=\"op\" value=\"forgotpass\">
  552. <input type=\"submit\" value=\"".__("Go back")."\">
  553. </form>";
  554. } else {
  555. print_notice("Password reset instructions are being sent to your email address.");
  556. $sth = $this->pdo->prepare("SELECT id FROM ttrss_users
  557. WHERE login = ? AND email = ?");
  558. $sth->execute([$login, $email]);
  559. if ($row = $sth->fetch()) {
  560. $id = $row["id"];
  561. if ($id) {
  562. $resetpass_token = sha1(get_random_bytes(128));
  563. $resetpass_link = get_self_url_prefix() . "/public.php?op=forgotpass&hash=" . $resetpass_token .
  564. "&login=" . urlencode($login);
  565. require_once 'classes/ttrssmailer.php';
  566. require_once "lib/MiniTemplator.class.php";
  567. $tpl = new MiniTemplator;
  568. $tpl->readTemplateFromFile("templates/resetpass_link_template.txt");
  569. $tpl->setVariable('LOGIN', $login);
  570. $tpl->setVariable('RESETPASS_LINK', $resetpass_link);
  571. $tpl->addBlock('message');
  572. $message = "";
  573. $tpl->generateOutputToString($message);
  574. $mail = new ttrssMailer();
  575. $rc = $mail->quickMail($email, $login,
  576. __("[tt-rss] Password reset request"),
  577. $message, false);
  578. if (!$rc) print_error($mail->ErrorInfo);
  579. $resetpass_token_full = time() . ":" . $resetpass_token;
  580. $sth = $this->pdo->prepare("UPDATE ttrss_users
  581. SET resetpass_token = ?
  582. WHERE login = ? AND email = ?");
  583. $sth->execute([$resetpass_token_full, $login, $email]);
  584. //Pref_Users::resetUserPassword($id, false);
  585. print "<p>";
  586. print "<p>"."Completed."."</p>";
  587. } else {
  588. print_error("User ID not found.");
  589. }
  590. print "<form method=\"GET\" action=\"index.php\">
  591. <input type=\"submit\" value=\"".__("Return to Tiny Tiny RSS")."\">
  592. </form>";
  593. } else {
  594. print_error(__("Sorry, login and email combination not found."));
  595. print "<form method=\"GET\" action=\"public.php\">
  596. <input type=\"hidden\" name=\"op\" value=\"forgotpass\">
  597. <input type=\"submit\" value=\"".__("Go back")."\">
  598. </form>";
  599. }
  600. }
  601. }
  602. print "</div>";
  603. print "</body>";
  604. print "</html>";
  605. }
  606. function dbupdate() {
  607. startup_gettext();
  608. if (!SINGLE_USER_MODE && $_SESSION["access_level"] < 10) {
  609. $_SESSION["login_error_msg"] = __("Your access level is insufficient to run this script.");
  610. render_login_form();
  611. exit;
  612. }
  613. ?><html>
  614. <head>
  615. <title>Database Updater</title>
  616. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  617. <link rel="stylesheet" type="text/css" href="css/utility.css"/>
  618. <link rel=\"shortcut icon\" type=\"image/png\" href=\"images/favicon.png\">
  619. <link rel=\"icon\" type=\"image/png\" sizes=\"72x72\" href=\"images/favicon-72px.png\">
  620. </head>
  621. <style type="text/css">
  622. span.ok { color : #009000; font-weight : bold; }
  623. span.err { color : #ff0000; font-weight : bold; }
  624. </style>
  625. <body>
  626. <script type='text/javascript'>
  627. function confirmOP() {
  628. return confirm("Update the database?");
  629. }
  630. </script>
  631. <div class="floatingLogo"><img src="images/logo_small.png"></div>
  632. <h1><?php echo __("Database Updater") ?></h1>
  633. <div class="content">
  634. <?php
  635. @$op = $_REQUEST["subop"];
  636. $updater = new DbUpdater(Db::pdo(), DB_TYPE, SCHEMA_VERSION);
  637. if ($op == "performupdate") {
  638. if ($updater->isUpdateRequired()) {
  639. print "<h2>Performing updates</h2>";
  640. print "<h3>Updating to schema version " . SCHEMA_VERSION . "</h3>";
  641. print "<ul>";
  642. for ($i = $updater->getSchemaVersion() + 1; $i <= SCHEMA_VERSION; $i++) {
  643. print "<li>Performing update up to version $i...";
  644. $result = $updater->performUpdateTo($i, true);
  645. if (!$result) {
  646. print "<span class='err'>FAILED!</span></li></ul>";
  647. print_warning("One of the updates failed. Either retry the process or perform updates manually.");
  648. print "<p><form method=\"GET\" action=\"index.php\">
  649. <input type=\"submit\" value=\"".__("Return to Tiny Tiny RSS")."\">
  650. </form>";
  651. return;
  652. } else {
  653. print "<span class='ok'>OK!</span></li>";
  654. }
  655. }
  656. print "</ul>";
  657. print_notice("Your Tiny Tiny RSS database is now updated to the latest version.");
  658. print "<p><form method=\"GET\" action=\"index.php\">
  659. <input type=\"submit\" value=\"".__("Return to Tiny Tiny RSS")."\">
  660. </form>";
  661. } else {
  662. print "<h2>Your database is up to date.</h2>";
  663. print "<p><form method=\"GET\" action=\"index.php\">
  664. <input type=\"submit\" value=\"".__("Return to Tiny Tiny RSS")."\">
  665. </form>";
  666. }
  667. } else {
  668. if ($updater->isUpdateRequired()) {
  669. print "<h2>Database update required</h2>";
  670. print_notice("<h4>".
  671. sprintf("Your Tiny Tiny RSS database needs update to the latest version: %d to %d.",
  672. $updater->getSchemaVersion(), SCHEMA_VERSION).
  673. "</h4>");
  674. print_warning("Please backup your database before proceeding.");
  675. print "<form method='POST'>
  676. <input type='hidden' name='subop' value='performupdate'>
  677. <input type='submit' onclick='return confirmOP()' value='".__("Perform updates")."'>
  678. </form>";
  679. } else {
  680. print_notice("Tiny Tiny RSS database is up to date.");
  681. print "<p><form method=\"GET\" action=\"index.php\">
  682. <input type=\"submit\" value=\"".__("Return to Tiny Tiny RSS")."\">
  683. </form>";
  684. }
  685. }
  686. ?>
  687. </div>
  688. </body>
  689. </html>
  690. <?php
  691. }
  692. function cached_url() {
  693. @$hash = basename($_GET['hash']);
  694. // we don't need an extension to find the file, hash is a complete URL
  695. $hash = preg_replace("/\.[^\.]*$/", "", $hash);
  696. if ($hash) {
  697. $filename = CACHE_DIR . '/images/' . $hash;
  698. if (file_exists($filename)) {
  699. header("Content-Disposition: inline; filename=\"$hash\"");
  700. send_local_file($filename);
  701. } else {
  702. header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
  703. echo "File not found.";
  704. }
  705. }
  706. }
  707. private function make_article_tag_uri($id, $timestamp) {
  708. $timestamp = date("Y-m-d", strtotime($timestamp));
  709. return "tag:" . parse_url(get_self_url_prefix(), PHP_URL_HOST) . ",$timestamp:/$id";
  710. }
  711. // this should be used very carefully because this endpoint is exposed to unauthenticated users
  712. // plugin data is not loaded because there's no user context and owner_uid/session may or may not be available
  713. // in general, don't do anything user-related in here and do not modify $_SESSION
  714. public function pluginhandler() {
  715. $host = new PluginHost();
  716. $plugin = basename($_REQUEST["plugin"]);
  717. $method = $_REQUEST["pmethod"];
  718. $host->load($plugin, PluginHost::KIND_USER, 0);
  719. $host->load_data();
  720. $pclass = $host->get_plugin($plugin);
  721. if ($pclass) {
  722. if (method_exists($pclass, $method)) {
  723. if ($pclass->is_public_method($method)) {
  724. $pclass->$method();
  725. } else {
  726. header("Content-Type: text/json");
  727. print error_json(6);
  728. }
  729. } else {
  730. header("Content-Type: text/json");
  731. print error_json(13);
  732. }
  733. } else {
  734. header("Content-Type: text/json");
  735. print error_json(14);
  736. }
  737. }
  738. }
  739. ?>