public.php 32 KB

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