public.php 28 KB

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