public.php 30 KB

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