public.php 31 KB

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