public.php 32 KB

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