public.php 31 KB

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