]> git.wh0rd.org - tt-rss.git/blame - classes/handler/public.php
move a bunch of functions into Feeds/Article namespaces
[tt-rss.git] / classes / handler / public.php
CommitLineData
5f0a3741 1<?php
369dbc19 2class Handler_Public extends Handler {
5f0a3741 3
79178062 4 private function generate_syndicated_feed($owner_uid, $feed, $is_cat,
9090b874 5 $limit, $offset, $search,
d1e631f3 6 $view_mode = false, $format = 'atom', $order = false, $orig_guid = false, $start_ts = false) {
79178062
AD
7
8 require_once "lib/MiniTemplator.class.php";
9
10 $note_style = "background-color : #fff7d5;
11 border-width : 1px; ".
12 "padding : 5px; border-style : dashed; border-color : #e7d796;".
13 "margin-bottom : 1em; color : #9a8c59;";
14
2faef834 15 if (!$limit) $limit = 60;
79178062 16
8aa01d79 17 $date_sort_field = "date_entered DESC, updated DESC";
84c53d0e 18 $date_check_field = "date_entered";
79178062 19
84c53d0e 20 if ($feed == -2 && !$is_cat) {
7ef7dd31 21 $date_sort_field = "last_published DESC";
84c53d0e
AD
22 $date_check_field = "last_published";
23 } else if ($feed == -1 && !$is_cat) {
7ef7dd31 24 $date_sort_field = "last_marked DESC";
84c53d0e
AD
25 $date_check_field = "last_marked";
26 }
008ebad9 27
25051fb8
AD
28 switch ($order) {
29 case "title":
30 $date_sort_field = "ttrss_entries.title";
31 break;
32 case "date_reverse":
33 $date_sort_field = "date_entered, updated";
34 break;
35 case "feed_dates":
36 $date_sort_field = "updated DESC";
37 break;
38 }
39
f5a0fb8b
AD
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
aeb1abed 54 $qfh_ret = Feeds::queryFeedHeadlines($params);
2e35a707
AD
55
56 $result = $qfh_ret[0];
57
d9c85e0f 58 if ($this->dbh->num_rows($result) != 0) {
84c53d0e
AD
59
60 $ts = strtotime($this->dbh->fetch_result($result, 0, $date_check_field));
2e35a707
AD
61
62 if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) &&
d6ba77f3 63 strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) >= $ts) {
2e35a707
AD
64 header('HTTP/1.0 304 Not Modified');
65 return;
66 }
67
68 $last_modified = gmdate("D, d M Y H:i:s", $ts) . " GMT";
69 header("Last-Modified: $last_modified", true);
70 }
71
f5a0fb8b
AD
72 $params = array(
73 "owner_uid" => $owner_uid,
74 "feed" => $feed,
75 "limit" => $limit,
76 "view_mode" => $view_mode,
77 "cat_view" => $is_cat,
78 "search" => $search,
79 "override_order" => $date_sort_field,
80 "include_children" => true,
81 "ignore_vfeed_group" => true,
82 "offset" => $offset,
83 "start_ts" => $start_ts
84 );
85
aeb1abed 86 $qfh_ret = Feeds::queryFeedHeadlines($params);
2e35a707 87
79178062
AD
88 $result = $qfh_ret[0];
89 $feed_title = htmlspecialchars($qfh_ret[1]);
90 $feed_site_url = $qfh_ret[2];
4a80c57c 91 /* $last_error = $qfh_ret[3]; */
79178062
AD
92
93 $feed_self_url = get_self_url_prefix() .
39119f02
JT
94 "/public.php?op=rss&id=$feed&key=" .
95 get_feed_access_key($feed, false, $owner_uid);
79178062
AD
96
97 if (!$feed_site_url) $feed_site_url = get_self_url_prefix();
98
2ebf38a9
AD
99 if ($format == 'atom') {
100 $tpl = new MiniTemplator;
79178062 101
2ebf38a9 102 $tpl->readTemplateFromFile("templates/generated_feed.txt");
79178062 103
2ebf38a9
AD
104 $tpl->setVariable('FEED_TITLE', $feed_title, true);
105 $tpl->setVariable('VERSION', VERSION, true);
106 $tpl->setVariable('FEED_URL', htmlspecialchars($feed_self_url), true);
79178062 107
2ebf38a9
AD
108 if (PUBSUBHUBBUB_HUB && $feed == -2) {
109 $tpl->setVariable('HUB_URL', htmlspecialchars(PUBSUBHUBBUB_HUB), true);
110 $tpl->addBlock('feed_hub');
111 }
79178062 112
2ebf38a9 113 $tpl->setVariable('SELF_URL', htmlspecialchars(get_self_url_prefix()), true);
d9c85e0f 114 while ($line = $this->dbh->fetch_assoc($result)) {
399678a1 115
2f1a29d9 116 $line["content_preview"] = sanitize(truncate_string(strip_tags($line["content"]), 100, '...'));
bc262b67 117
891e36f5 118 foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_QUERY_HEADLINES) as $p) {
119 $line = $p->hook_query_headlines($line);
120 }
2e35a707 121
399678a1 122 foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_ARTICLE_EXPORT_FEED) as $p) {
92322838 123 $line = $p->hook_article_export_feed($line, $feed, $is_cat);
399678a1
AD
124 }
125
fcf6bfba
AD
126 $tpl->setVariable('ARTICLE_ID',
127 htmlspecialchars($orig_guid ? $line['link'] :
f75e7c64 128 $this->make_article_tag_uri($line['id'], $line['date_entered'])), true);
2ebf38a9
AD
129 $tpl->setVariable('ARTICLE_LINK', htmlspecialchars($line['link']), true);
130 $tpl->setVariable('ARTICLE_TITLE', htmlspecialchars($line['title']), true);
67631438 131 $tpl->setVariable('ARTICLE_EXCERPT', $line["content_preview"], true);
79178062 132
4b19d4ac 133 $content = sanitize($line["content"], false, $owner_uid,
3261dbfa 134 $feed_site_url, false, $line["id"]);
79178062 135
2ebf38a9
AD
136 if ($line['note']) {
137 $content = "<div style=\"$note_style\">Article note: " . $line['note'] . "</div>" .
138 $content;
e9c6e27d 139 $tpl->setVariable('ARTICLE_NOTE', htmlspecialchars($line['note']), true);
140 }
2ebf38a9
AD
141
142 $tpl->setVariable('ARTICLE_CONTENT', $content, true);
143
144 $tpl->setVariable('ARTICLE_UPDATED_ATOM',
145 date('c', strtotime($line["updated"])), true);
146 $tpl->setVariable('ARTICLE_UPDATED_RFC822',
147 date(DATE_RFC822, strtotime($line["updated"])), true);
148
149 $tpl->setVariable('ARTICLE_AUTHOR', htmlspecialchars($line['author']), true);
150
4b7391de 151 $tpl->setVariable('ARTICLE_SOURCE_LINK', htmlspecialchars($line['site_url'] ? $line["site_url"] : get_self_url_prefix()), true);
0ee126ee 152 $tpl->setVariable('ARTICLE_SOURCE_TITLE', htmlspecialchars($line['feed_title'] ? $line['feed_title'] : $feed_title), true);
2ebf38a9 153
7e5f8d9f 154 $tags = Article::get_article_tags($line["id"], $owner_uid);
2ebf38a9
AD
155
156 foreach ($tags as $tag) {
157 $tpl->setVariable('ARTICLE_CATEGORY', htmlspecialchars($tag), true);
158 $tpl->addBlock('category');
159 }
79178062 160
7e5f8d9f 161 $enclosures = Article::get_article_enclosures($line["id"]);
79178062 162
2ebf38a9
AD
163 foreach ($enclosures as $e) {
164 $type = htmlspecialchars($e['content_type']);
165 $url = htmlspecialchars($e['content_url']);
4b7391de 166 $length = $e['duration'] ? $e['duration'] : 1;
9c97041d 167
2ebf38a9
AD
168 $tpl->setVariable('ARTICLE_ENCLOSURE_URL', $url, true);
169 $tpl->setVariable('ARTICLE_ENCLOSURE_TYPE', $type, true);
170 $tpl->setVariable('ARTICLE_ENCLOSURE_LENGTH', $length, true);
79178062 171
2ebf38a9
AD
172 $tpl->addBlock('enclosure');
173 }
79178062 174
2ebf38a9 175 $tpl->addBlock('entry');
79178062
AD
176 }
177
2ebf38a9
AD
178 $tmp = "";
179
180 $tpl->addBlock('feed');
181 $tpl->generateOutputToString($tmp);
182
10bdeb4b
AD
183 if (@!$_REQUEST["noxml"]) {
184 header("Content-Type: text/xml; charset=utf-8");
185 } else {
186 header("Content-Type: text/plain; charset=utf-8");
187 }
2ebf38a9
AD
188
189 print $tmp;
190 } else if ($format == 'json') {
79178062 191
2ebf38a9 192 $feed = array();
79178062 193
2ebf38a9
AD
194 $feed['title'] = $feed_title;
195 $feed['version'] = VERSION;
196 $feed['feed_url'] = $feed_self_url;
79178062 197
2ebf38a9
AD
198 if (PUBSUBHUBBUB_HUB && $feed == -2) {
199 $feed['hub_url'] = PUBSUBHUBBUB_HUB;
79178062
AD
200 }
201
2ebf38a9
AD
202 $feed['self_url'] = get_self_url_prefix();
203
204 $feed['articles'] = array();
205
d9c85e0f 206 while ($line = $this->dbh->fetch_assoc($result)) {
399678a1 207
2f1a29d9 208 $line["content_preview"] = sanitize(truncate_string(strip_tags($line["content_preview"]), 100, '...'));
399678a1 209
891e36f5 210 foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_QUERY_HEADLINES) as $p) {
211 $line = $p->hook_query_headlines($line, 100);
212 }
399678a1
AD
213
214 foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_ARTICLE_EXPORT_FEED) as $p) {
92322838 215 $line = $p->hook_article_export_feed($line, $feed, $is_cat);
399678a1
AD
216 }
217
2ebf38a9 218 $article = array();
79178062 219
2ebf38a9
AD
220 $article['id'] = $line['link'];
221 $article['link'] = $line['link'];
222 $article['title'] = $line['title'];
67631438 223 $article['excerpt'] = $line["content_preview"];
3261dbfa 224 $article['content'] = sanitize($line["content"], false, $owner_uid, $feed_site_url, false, $line["id"]);
2ebf38a9 225 $article['updated'] = date('c', strtotime($line["updated"]));
79178062 226
2ebf38a9
AD
227 if ($line['note']) $article['note'] = $line['note'];
228 if ($article['author']) $article['author'] = $line['author'];
79178062 229
a42c55f0 230 $tags = get_article_tags($line["id"], $owner_uid);
2ebf38a9
AD
231
232 if (count($tags) > 0) {
233 $article['tags'] = array();
234
235 foreach ($tags as $tag) {
236 array_push($article['tags'], $tag);
237 }
238 }
239
7e5f8d9f 240 $enclosures = Article::get_article_enclosures($line["id"]);
2ebf38a9
AD
241
242 if (count($enclosures) > 0) {
243 $article['enclosures'] = array();
244
245 foreach ($enclosures as $e) {
246 $type = $e['content_type'];
247 $url = $e['content_url'];
248 $length = $e['duration'];
249
250 array_push($article['enclosures'], array("url" => $url, "type" => $type, "length" => $length));
251 }
252 }
253
254 array_push($feed['articles'], $article);
255 }
256
10bdeb4b 257 header("Content-Type: text/json; charset=utf-8");
2ebf38a9
AD
258 print json_encode($feed);
259
260 } else {
261 header("Content-Type: text/plain; charset=utf-8");
262 print json_encode(array("error" => array("message" => "Unknown format")));
263 }
79178062
AD
264 }
265
5f0a3741 266 function getUnread() {
d9c85e0f 267 $login = $this->dbh->escape_string($_REQUEST["login"]);
5f0a3741
AD
268 $fresh = $_REQUEST["fresh"] == "1";
269
d9c85e0f 270 $result = $this->dbh->query("SELECT id FROM ttrss_users WHERE login = '$login'");
5f0a3741 271
d9c85e0f
AD
272 if ($this->dbh->num_rows($result) == 1) {
273 $uid = $this->dbh->fetch_result($result, 0, "id");
5f0a3741 274
a230bf88 275 print Feeds::getGlobalUnread($uid);
5f0a3741
AD
276
277 if ($fresh) {
278 print ";";
86a8351c 279 print Feeds::getFeedArticles(-3, false, true, $uid);
5f0a3741
AD
280 }
281
282 } else {
283 print "-1;User not found";
284 }
285
286 }
287
288 function getProfiles() {
d9c85e0f 289 $login = $this->dbh->escape_string($_REQUEST["login"]);
5f0a3741 290
0806d68b 291 $result = $this->dbh->query("SELECT ttrss_settings_profiles.* FROM ttrss_settings_profiles,ttrss_users
97acbaf1 292 WHERE ttrss_users.id = ttrss_settings_profiles.owner_uid AND login = '$login' ORDER BY title");
5f0a3741 293
eeee2ccf 294 print "<select dojoType='dijit.form.Select' style='width : 220px; margin : 0px' name='profile'>";
5f0a3741 295
97acbaf1 296 print "<option value='0'>" . __("Default profile") . "</option>";
5f0a3741 297
d9c85e0f 298 while ($line = $this->dbh->fetch_assoc($result)) {
97acbaf1
AD
299 $id = $line["id"];
300 $title = $line["title"];
5f0a3741 301
97acbaf1 302 print "<option value='$id'>$title</option>";
5f0a3741 303 }
97acbaf1
AD
304
305 print "</select>";
5f0a3741
AD
306 }
307
308 function pubsub() {
d9c85e0f 309 $mode = $this->dbh->escape_string($_REQUEST['hub_mode']);
43d790f0
AD
310 if (!$mode) $mode = $this->dbh->escape_string($_REQUEST['hub.mode']);
311
d9c85e0f
AD
312 $feed_id = (int) $this->dbh->escape_string($_REQUEST['id']);
313 $feed_url = $this->dbh->escape_string($_REQUEST['hub_topic']);
5f0a3741 314
43d790f0
AD
315 if (!$feed_url) $feed_url = $this->dbh->escape_string($_REQUEST['hub.topic']);
316
5f0a3741
AD
317 if (!PUBSUBHUBBUB_ENABLED) {
318 header('HTTP/1.0 404 Not Found');
43d790f0 319 echo "404 Not found (Disabled by server)";
5f0a3741
AD
320 return;
321 }
322
323 // TODO: implement hub_verifytoken checking
43d790f0
AD
324 // TODO: store requested rel=self or whatever for verification
325 // (may be different from stored feed url) e.g. http://url/ or http://url
5f0a3741 326
d9c85e0f 327 $result = $this->dbh->query("SELECT feed_url FROM ttrss_feeds
5f0a3741
AD
328 WHERE id = '$feed_id'");
329
d9c85e0f 330 if ($this->dbh->num_rows($result) != 0) {
5f0a3741 331
d9c85e0f 332 $check_feed_url = $this->dbh->fetch_result($result, 0, "feed_url");
5f0a3741 333
43d790f0
AD
334 // ignore url checking for the time being
335 if ($check_feed_url && (true || $check_feed_url == $feed_url || !$feed_url)) {
5f0a3741
AD
336 if ($mode == "subscribe") {
337
d9c85e0f 338 $this->dbh->query("UPDATE ttrss_feeds SET pubsub_state = 2
5f0a3741
AD
339 WHERE id = '$feed_id'");
340
341 print $_REQUEST['hub_challenge'];
342 return;
343
344 } else if ($mode == "unsubscribe") {
345
d9c85e0f 346 $this->dbh->query("UPDATE ttrss_feeds SET pubsub_state = 0
5f0a3741
AD
347 WHERE id = '$feed_id'");
348
349 print $_REQUEST['hub_challenge'];
350 return;
351
352 } else if (!$mode) {
353
354 // Received update ping, schedule feed update.
a42c55f0 355 //update_rss_feed($feed_id, true, true);
5f0a3741 356
d9c85e0f 357 $this->dbh->query("UPDATE ttrss_feeds SET
5f0a3741
AD
358 last_update_started = '1970-01-01',
359 last_updated = '1970-01-01' WHERE id = '$feed_id'");
360
361 }
362 } else {
363 header('HTTP/1.0 404 Not Found');
43d790f0 364 echo "404 Not found (URL check failed)";
5f0a3741
AD
365 }
366 } else {
367 header('HTTP/1.0 404 Not Found');
43d790f0 368 echo "404 Not found (Feed not found)";
5f0a3741
AD
369 }
370
371 }
372
373 function logout() {
374 logout_user();
375 header("Location: index.php");
376 }
377
5f0a3741 378 function share() {
d9c85e0f 379 $uuid = $this->dbh->escape_string($_REQUEST["key"]);
5f0a3741 380
d9c85e0f 381 $result = $this->dbh->query("SELECT ref_id, owner_uid FROM ttrss_user_entries WHERE
5f0a3741
AD
382 uuid = '$uuid'");
383
d9c85e0f 384 if ($this->dbh->num_rows($result) != 0) {
5f0a3741
AD
385 header("Content-Type: text/html");
386
d9c85e0f
AD
387 $id = $this->dbh->fetch_result($result, 0, "ref_id");
388 $owner_uid = $this->dbh->fetch_result($result, 0, "owner_uid");
5f0a3741 389
7e5f8d9f 390 $article = Article::format_article($id, false, true, $owner_uid);
5f0a3741
AD
391
392 print_r($article['content']);
393
394 } else {
395 print "Article not found.";
396 }
397
398 }
399
400 function rss() {
d9c85e0f
AD
401 $feed = $this->dbh->escape_string($_REQUEST["id"]);
402 $key = $this->dbh->escape_string($_REQUEST["key"]);
356e13b1 403 $is_cat = sql_bool_to_bool($_REQUEST["is_cat"]);
d9c85e0f
AD
404 $limit = (int)$this->dbh->escape_string($_REQUEST["limit"]);
405 $offset = (int)$this->dbh->escape_string($_REQUEST["offset"]);
5f0a3741 406
d9c85e0f 407 $search = $this->dbh->escape_string($_REQUEST["q"]);
d9c85e0f 408 $view_mode = $this->dbh->escape_string($_REQUEST["view-mode"]);
25051fb8 409 $order = $this->dbh->escape_string($_REQUEST["order"]);
d1e631f3 410 $start_ts = $this->dbh->escape_string($_REQUEST["ts"]);
5f0a3741 411
d9c85e0f 412 $format = $this->dbh->escape_string($_REQUEST['format']);
5bcb7b61 413 $orig_guid = sql_bool_to_bool($_REQUEST["orig_guid"]);
2ebf38a9
AD
414
415 if (!$format) $format = 'atom';
416
5f0a3741 417 if (SINGLE_USER_MODE) {
a42c55f0 418 authenticate_user("admin", null);
5f0a3741
AD
419 }
420
421 $owner_id = false;
422
423 if ($key) {
d9c85e0f 424 $result = $this->dbh->query("SELECT owner_uid FROM
5f0a3741
AD
425 ttrss_access_keys WHERE access_key = '$key' AND feed_id = '$feed'");
426
d9c85e0f
AD
427 if ($this->dbh->num_rows($result) == 1)
428 $owner_id = $this->dbh->fetch_result($result, 0, "owner_uid");
5f0a3741
AD
429 }
430
431 if ($owner_id) {
2fb947eb 432 $this->generate_syndicated_feed($owner_id, $feed, $is_cat, $limit,
9090b874 433 $offset, $search, $view_mode, $format, $order, $orig_guid, $start_ts);
5f0a3741
AD
434 } else {
435 header('HTTP/1.1 403 Forbidden');
436 }
437 }
438
910592b4 439 function updateTask() {
4a80c57c 440 PluginHost::getInstance()->run_hooks(PluginHost::HOOK_UPDATE_TASK, "hook_update_task", false);
910592b4
AD
441 }
442
4e5ddeaf 443 function housekeepingTask() {
4a80c57c 444 PluginHost::getInstance()->run_hooks(PluginHost::HOOK_HOUSE_KEEPING, "hook_house_keeping", false);
4e5ddeaf
AD
445 }
446
07391e36 447 function globalUpdateFeeds() {
113c3dec 448 RPC::updaterandomfeed_real($this->dbh);
cda55d67 449
4a80c57c 450 PluginHost::getInstance()->run_hooks(PluginHost::HOOK_UPDATE_TASK, "hook_update_task", false);
07391e36 451 }
8361e724
AD
452
453 function sharepopup() {
61a748f8 454 if (SINGLE_USER_MODE) {
6322ac79 455 login_sequence();
61a748f8
AD
456 }
457
8361e724 458 header('Content-Type: text/html; charset=utf-8');
d0ee0f52
AD
459 print "<html><head><title>Tiny Tiny RSS</title>
460 <link rel=\"shortcut icon\" type=\"image/png\" href=\"images/favicon.png\">
461 <link rel=\"icon\" type=\"image/png\" sizes=\"72x72\" href=\"images/favicon-72px.png\">";
9a2aed91 462
cdbcb277 463 echo stylesheet_tag("css/utility.css");
6214a076 464 echo stylesheet_tag("css/dijit.css");
cdbcb277 465 echo javascript_tag("lib/prototype.js");
6214a076 466 echo javascript_tag("lib/scriptaculous/scriptaculous.js?load=effects,controls");
9a2aed91
AD
467 print "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>
468 </head><body id='sharepopup'>";
8361e724
AD
469
470 $action = $_REQUEST["action"];
471
472 if ($_SESSION["uid"]) {
473
d493aba2 474 if ($action == 'share') {
8361e724 475
d9c85e0f
AD
476 $title = $this->dbh->escape_string(strip_tags($_REQUEST["title"]));
477 $url = $this->dbh->escape_string(strip_tags($_REQUEST["url"]));
478 $content = $this->dbh->escape_string(strip_tags($_REQUEST["content"]));
479 $labels = $this->dbh->escape_string(strip_tags($_REQUEST["labels"]));
8361e724 480
a42c55f0 481 Article::create_published_article($title, $url, $content, $labels,
1b4d1a6b 482 $_SESSION["uid"]);
8361e724 483
d493aba2
AD
484 print "<script type='text/javascript'>";
485 print "window.close();";
486 print "</script>";
8361e724 487
d493aba2 488 } else {
8361e724
AD
489 $title = htmlspecialchars($_REQUEST["title"]);
490 $url = htmlspecialchars($_REQUEST["url"]);
491
d493aba2 492 ?>
8361e724 493
d493aba2
AD
494 <table height='100%' width='100%'><tr><td colspan='2'>
495 <h1><?php echo __("Share with Tiny Tiny RSS") ?></h1>
496 </td></tr>
8361e724 497
d493aba2 498 <form id='share_form' name='share_form'>
8361e724 499
d493aba2
AD
500 <input type="hidden" name="op" value="sharepopup">
501 <input type="hidden" name="action" value="share">
8361e724 502
22439dad 503 <tr><td align='right'><?php echo __("Title:") ?></td>
d493aba2 504 <td width='80%'><input name='title' value="<?php echo $title ?>"></td></tr>
22439dad 505 <tr><td align='right'><?php echo __("URL:") ?></td>
d493aba2 506 <td><input name='url' value="<?php echo $url ?>"></td></tr>
22439dad 507 <tr><td align='right'><?php echo __("Content:") ?></td>
d493aba2 508 <td><input name='content' value=""></td></tr>
1b4d1a6b
AD
509 <tr><td align='right'><?php echo __("Labels:") ?></td>
510 <td><input name='labels' id="labels_value"
511 placeholder='Alpha, Beta, Gamma' value="">
512 </td></tr>
513
514 <tr><td>
515 <div class="autocomplete" id="labels_choices"
516 style="display : block"></div></td></tr>
8361e724 517
d493aba2 518 <script type='text/javascript'>document.forms[0].title.focus();</script>
8361e724 519
1b4d1a6b
AD
520 <script type='text/javascript'>
521 new Ajax.Autocompleter('labels_value', 'labels_choices',
522 "backend.php?op=rpc&method=completeLabels",
523 { tokens: ',', paramName: "search" });
524 </script>
525
d493aba2
AD
526 <tr><td colspan='2'>
527 <div style='float : right' class='insensitive-small'>
528 <?php echo __("Shared article will appear in the Published feed.") ?>
529 </div>
530 <button type="submit"><?php echo __('Share') ?></button>
531 <button onclick="return window.close()"><?php echo __('Cancel') ?></button>
f5a0fb8b 532 </td>
8361e724 533
d493aba2
AD
534 </form>
535 </td></tr></table>
536 </body></html>
537 <?php
8361e724 538
8361e724
AD
539 }
540
541 } else {
542
d493aba2
AD
543 $return = urlencode($_SERVER["REQUEST_URI"])
544 ?>
545
546 <form action="public.php?return=<?php echo $return ?>"
22439dad 547 method="POST" id="loginForm" name="loginForm">
d493aba2
AD
548
549 <input type="hidden" name="op" value="login">
550
551 <table height='100%' width='100%'><tr><td colspan='2'>
22439dad 552 <h1><?php echo __("Not logged in") ?></h1></td></tr>
d493aba2
AD
553
554 <tr><td align="right"><?php echo __("Login:") ?></td>
555 <td align="right"><input name="login"
556 value="<?php echo $_SESSION["fake_login"] ?>"></td></tr>
557 <tr><td align="right"><?php echo __("Password:") ?></td>
558 <td align="right"><input type="password" name="password"
559 value="<?php echo $_SESSION["fake_password"] ?>"></td></tr>
d493aba2
AD
560 <tr><td colspan='2'>
561 <button type="submit">
562 <?php echo __('Log in') ?></button>
563
564 <button onclick="return window.close()">
565 <?php echo __('Cancel') ?></button>
566 </td></tr>
567 </table>
568
569 </form>
570 <?php
8361e724
AD
571 }
572 }
573
97acbaf1 574 function login() {
97acbaf1
AD
575 if (!SINGLE_USER_MODE) {
576
d9c85e0f 577 $login = $this->dbh->escape_string($_POST["login"]);
97acbaf1 578 $password = $_POST["password"];
aadd636a 579 $remember_me = $_POST["remember_me"];
97acbaf1 580
f231f438
AD
581 if ($remember_me) {
582 session_set_cookie_params(SESSION_COOKIE_LIFETIME);
583 } else {
584 session_set_cookie_params(0);
585 }
586
aadd636a 587 @session_start();
60ed4c9a 588
a42c55f0 589 if (authenticate_user($login, $password)) {
97acbaf1
AD
590 $_POST["password"] = "";
591
b18d109f
AD
592 if (get_schema_version() >= 120) {
593 $_SESSION["language"] = get_pref("USER_LANGUAGE", $_SESSION["uid"]);
594 }
595
a42c55f0 596 $_SESSION["ref_schema_version"] = get_schema_version(true);
97acbaf1
AD
597 $_SESSION["bw_limit"] = !!$_POST["bw_limit"];
598
599 if ($_POST["profile"]) {
600
d9c85e0f 601 $profile = $this->dbh->escape_string($_POST["profile"]);
97acbaf1 602
d9c85e0f 603 $result = $this->dbh->query("SELECT id FROM ttrss_settings_profiles
97acbaf1
AD
604 WHERE id = '$profile' AND owner_uid = " . $_SESSION["uid"]);
605
d9c85e0f 606 if ($this->dbh->num_rows($result) != 0) {
97acbaf1 607 $_SESSION["profile"] = $profile;
97acbaf1
AD
608 }
609 }
610 } else {
611 $_SESSION["login_error_msg"] = __("Incorrect username or password");
fd94d631 612 user_error("Failed login attempt for $login from {$_SERVER['REMOTE_ADDR']}", E_USER_WARNING);
97acbaf1
AD
613 }
614
615 if ($_REQUEST['return']) {
616 header("Location: " . $_REQUEST['return']);
617 } else {
618 header("Location: " . SELF_URL_PATH);
619 }
620 }
621 }
622
f7439d69 623 /* function subtest() {
97b7d5c0
AD
624 header("Content-type: text/plain; charset=utf-8");
625
626 $url = $_REQUEST["url"];
627
628 print "$url\n\n";
629
630
631 print_r(get_feeds_from_html($url, fetch_file_contents($url)));
632
f7439d69 633 } */
97b7d5c0 634
97acbaf1 635 function subscribe() {
61a748f8 636 if (SINGLE_USER_MODE) {
6322ac79 637 login_sequence();
61a748f8
AD
638 }
639
97acbaf1
AD
640 if ($_SESSION["uid"]) {
641
d9c85e0f 642 $feed_url = $this->dbh->escape_string(trim($_REQUEST["feed_url"]));
97acbaf1
AD
643
644 header('Content-Type: text/html; charset=utf-8');
645 print "<html>
646 <head>
647 <title>Tiny Tiny RSS</title>
5bbc4bb4 648 <link rel=\"stylesheet\" type=\"text/css\" href=\"css/utility.css\">
97acbaf1 649 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>
d0ee0f52
AD
650 <link rel=\"shortcut icon\" type=\"image/png\" href=\"images/favicon.png\">
651 <link rel=\"icon\" type=\"image/png\" sizes=\"72x72\" href=\"images/favicon-72px.png\">
652
97acbaf1
AD
653 </head>
654 <body>
884d1650 655 <img class=\"floatingLogo\" src=\"images/logo_small.png\"
97acbaf1 656 alt=\"Tiny Tiny RSS\"/>
884d1650 657 <h1>".__("Subscribe to feed...")."</h1><div class='content'>";
97acbaf1 658
86a8351c 659 $rc = Feeds::subscribe_to_feed($feed_url);
97acbaf1
AD
660
661 switch ($rc['code']) {
662 case 0:
663 print_warning(T_sprintf("Already subscribed to <b>%s</b>.", $feed_url));
664 break;
665 case 1:
666 print_notice(T_sprintf("Subscribed to <b>%s</b>.", $feed_url));
667 break;
668 case 2:
669 print_error(T_sprintf("Could not subscribe to <b>%s</b>.", $feed_url));
670 break;
671 case 3:
672 print_error(T_sprintf("No feeds found in <b>%s</b>.", $feed_url));
673 break;
674 case 4:
675 print_notice(__("Multiple feed URLs found."));
759e5132 676 $feed_urls = $rc["feeds"];
97acbaf1
AD
677 break;
678 case 5:
679 print_error(T_sprintf("Could not subscribe to <b>%s</b>.<br>Can't download the Feed URL.", $feed_url));
680 break;
681 }
682
683 if ($feed_urls) {
684
685 print "<form action=\"public.php\">";
686 print "<input type=\"hidden\" name=\"op\" value=\"subscribe\">";
687
688 print "<select name=\"feed_url\">";
689
690 foreach ($feed_urls as $url => $name) {
691 $url = htmlspecialchars($url);
692 $name = htmlspecialchars($name);
693
694 print "<option value=\"$url\">$name</option>";
695 }
696
697 print "<input type=\"submit\" value=\"".__("Subscribe to selected feed").
698 "\">";
699
700 print "</form>";
701 }
702
703 $tp_uri = get_self_url_prefix() . "/prefs.php";
704 $tt_uri = get_self_url_prefix();
705
706 if ($rc['code'] <= 2){
d9c85e0f 707 $result = $this->dbh->query("SELECT id FROM ttrss_feeds WHERE
97acbaf1
AD
708 feed_url = '$feed_url' AND owner_uid = " . $_SESSION["uid"]);
709
d9c85e0f 710 $feed_id = $this->dbh->fetch_result($result, 0, "id");
97acbaf1
AD
711 } else {
712 $feed_id = 0;
713 }
714 print "<p>";
715
716 if ($feed_id) {
717 print "<form method=\"GET\" style='display: inline'
718 action=\"$tp_uri\">
719 <input type=\"hidden\" name=\"tab\" value=\"feedConfig\">
720 <input type=\"hidden\" name=\"method\" value=\"editFeed\">
721 <input type=\"hidden\" name=\"methodparam\" value=\"$feed_id\">
722 <input type=\"submit\" value=\"".__("Edit subscription options")."\">
723 </form>";
724 }
725
726 print "<form style='display: inline' method=\"GET\" action=\"$tt_uri\">
727 <input type=\"submit\" value=\"".__("Return to Tiny Tiny RSS")."\">
728 </form></p>";
729
884d1650 730 print "</div></body></html>";
97acbaf1
AD
731
732 } else {
6322ac79 733 render_login_form();
97acbaf1
AD
734 }
735 }
736
97acbaf1
AD
737 function index() {
738 header("Content-Type: text/plain");
27f7b593 739 print error_json(13);
97acbaf1
AD
740 }
741
f43e9e97 742 function forgotpass() {
67e0cf9a
AD
743 startup_gettext();
744
5303f9a7
AD
745 @$hash = $_REQUEST["hash"];
746
f43e9e97 747 header('Content-Type: text/html; charset=utf-8');
d0ee0f52
AD
748 print "<html><head><title>Tiny Tiny RSS</title>
749 <link rel=\"shortcut icon\" type=\"image/png\" href=\"images/favicon.png\">
750 <link rel=\"icon\" type=\"image/png\" sizes=\"72x72\" href=\"images/favicon-72px.png\">";
e216d302 751
cdbcb277
AD
752 echo stylesheet_tag("css/utility.css");
753 echo javascript_tag("lib/prototype.js");
e216d302
AD
754
755 print "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>
756 </head><body id='forgotpass'>";
f43e9e97 757
884d1650 758 print '<div class="floatingLogo"><img src="images/logo_small.png"></div>';
483f15d5 759 print "<h1>".__("Password recovery")."</h1>";
884d1650 760 print "<div class='content'>";
f43e9e97
AD
761
762 @$method = $_POST['method'];
763
5303f9a7
AD
764 if ($hash) {
765 $login = $_REQUEST["login"];
766
767 if ($login) {
768 $result = $this->dbh->query("SELECT id, resetpass_token FROM ttrss_users
769 WHERE login = '$login'");
770
771 if ($this->dbh->num_rows($result) != 0) {
772 $id = $this->dbh->fetch_result($result, 0, "id");
773 $resetpass_token_full = $this->dbh->fetch_result($result, 0, "resetpass_token");
774 list($timestamp, $resetpass_token) = explode(":", $resetpass_token_full);
775
776 if ($timestamp && $resetpass_token &&
777 $timestamp >= time() - 15*60*60 &&
778 $resetpass_token == $hash) {
779
780 $result = $this->dbh->query("UPDATE ttrss_users SET resetpass_token = NULL
781 WHERE id = $id");
782
783 Pref_Users::resetUserPassword($id, true);
784
785 print "<p>"."Completed."."</p>";
786
787 } else {
788 print_error("Some of the information provided is missing or incorrect.");
789 }
790 } else {
791 print_error("Some of the information provided is missing or incorrect.");
792 }
793 } else {
794 print_error("Some of the information provided is missing or incorrect.");
795 }
796
797 print "<form method=\"GET\" action=\"index.php\">
798 <input type=\"submit\" value=\"".__("Return to Tiny Tiny RSS")."\">
799 </form>";
800
801 } else if (!$method) {
802 print_notice(__("You will need to provide valid account name and email. A password reset link will be sent to your email address."));
483f15d5 803
f43e9e97 804 print "<form method='POST' action='public.php'>";
f43e9e97
AD
805 print "<input type='hidden' name='method' value='do'>";
806 print "<input type='hidden' name='op' value='forgotpass'>";
807
808 print "<fieldset>";
809 print "<label>".__("Login:")."</label>";
810 print "<input type='text' name='login' value='' required>";
811 print "</fieldset>";
812
813 print "<fieldset>";
814 print "<label>".__("Email:")."</label>";
815 print "<input type='email' name='email' value='' required>";
816 print "</fieldset>";
817
818 print "<fieldset>";
819 print "<label>".__("How much is two plus two:")."</label>";
820 print "<input type='text' name='test' value='' required>";
821 print "</fieldset>";
822
823 print "<p/>";
824 print "<button type='submit'>".__("Reset password")."</button>";
825
826 print "</form>";
827 } else if ($method == 'do') {
828
d9c85e0f
AD
829 $login = $this->dbh->escape_string($_POST["login"]);
830 $email = $this->dbh->escape_string($_POST["email"]);
831 $test = $this->dbh->escape_string($_POST["test"]);
f43e9e97
AD
832
833 if (($test != 4 && $test != 'four') || !$email || !$login) {
834 print_error(__('Some of the required form parameters are missing or incorrect.'));
835
483f15d5
AD
836 print "<form method=\"GET\" action=\"public.php\">
837 <input type=\"hidden\" name=\"op\" value=\"forgotpass\">
838 <input type=\"submit\" value=\"".__("Go back")."\">
839 </form>";
f43e9e97 840
e216d302 841 } else {
f43e9e97 842
5303f9a7
AD
843 print_notice("Password reset instructions are being sent to your email address.");
844
d9c85e0f 845 $result = $this->dbh->query("SELECT id FROM ttrss_users
f43e9e97
AD
846 WHERE login = '$login' AND email = '$email'");
847
d9c85e0f
AD
848 if ($this->dbh->num_rows($result) != 0) {
849 $id = $this->dbh->fetch_result($result, 0, "id");
f43e9e97 850
5303f9a7
AD
851 if ($id) {
852 $resetpass_token = sha1(get_random_bytes(128));
853 $resetpass_link = get_self_url_prefix() . "/public.php?op=forgotpass&hash=" . $resetpass_token .
854 "&login=" . urlencode($login);
855
856 require_once 'classes/ttrssmailer.php';
857 require_once "lib/MiniTemplator.class.php";
858
859 $tpl = new MiniTemplator;
860
861 $tpl->readTemplateFromFile("templates/resetpass_link_template.txt");
862
863 $tpl->setVariable('LOGIN', $login);
864 $tpl->setVariable('RESETPASS_LINK', $resetpass_link);
865
866 $tpl->addBlock('message');
867
868 $message = "";
f43e9e97 869
5303f9a7 870 $tpl->generateOutputToString($message);
483f15d5 871
5303f9a7
AD
872 $mail = new ttrssMailer();
873
874 $rc = $mail->quickMail($email, $login,
875 __("[tt-rss] Password reset request"),
876 $message, false);
877
878 if (!$rc) print_error($mail->ErrorInfo);
879
880 $resetpass_token_full = $this->dbh->escape_string(time() . ":" . $resetpass_token);
881
882 $result = $this->dbh->query("UPDATE ttrss_users
883 SET resetpass_token = '$resetpass_token_full'
884 WHERE login = '$login' AND email = '$email'");
885
886 //Pref_Users::resetUserPassword($id, false);
887
888 print "<p>";
889
890 print "<p>"."Completed."."</p>";
891 } else {
892 print_error("User ID not found.");
893 }
483f15d5
AD
894
895 print "<form method=\"GET\" action=\"index.php\">
896 <input type=\"submit\" value=\"".__("Return to Tiny Tiny RSS")."\">
897 </form>";
f43e9e97
AD
898
899 } else {
900 print_error(__("Sorry, login and email combination not found."));
483f15d5
AD
901
902 print "<form method=\"GET\" action=\"public.php\">
903 <input type=\"hidden\" name=\"op\" value=\"forgotpass\">
904 <input type=\"submit\" value=\"".__("Go back")."\">
905 </form>";
906
f43e9e97 907 }
f43e9e97
AD
908 }
909
910 }
911
884d1650 912 print "</div>";
f43e9e97
AD
913 print "</body>";
914 print "</html>";
915
b4c47f7e
AD
916 }
917
918 function dbupdate() {
67e0cf9a
AD
919 startup_gettext();
920
f240d26e
AD
921 if (!SINGLE_USER_MODE && $_SESSION["access_level"] < 10) {
922 $_SESSION["login_error_msg"] = __("Your access level is insufficient to run this script.");
6322ac79 923 render_login_form();
f240d26e
AD
924 exit;
925 }
926
927 ?><html>
928 <head>
929 <title>Database Updater</title>
930 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
5bbc4bb4 931 <link rel="stylesheet" type="text/css" href="css/utility.css"/>
d0ee0f52
AD
932 <link rel=\"shortcut icon\" type=\"image/png\" href=\"images/favicon.png\">
933 <link rel=\"icon\" type=\"image/png\" sizes=\"72x72\" href=\"images/favicon-72px.png\">
f240d26e
AD
934 </head>
935 <style type="text/css">
936 span.ok { color : #009000; font-weight : bold; }
937 span.err { color : #ff0000; font-weight : bold; }
938 </style>
939 <body>
940 <script type='text/javascript'>
941 function confirmOP() {
942 return confirm("Update the database?");
943 }
944 </script>
945
946 <div class="floatingLogo"><img src="images/logo_small.png"></div>
947
948 <h1><?php echo __("Database Updater") ?></h1>
949
950 <div class="content">
951
952 <?php
953 @$op = $_REQUEST["subop"];
0630a100 954 $updater = new DbUpdater(Db::get(), DB_TYPE, SCHEMA_VERSION);
f240d26e
AD
955
956 if ($op == "performupdate") {
957 if ($updater->isUpdateRequired()) {
958
959 print "<h2>Performing updates</h2>";
960
961 print "<h3>Updating to schema version " . SCHEMA_VERSION . "</h3>";
962
963 print "<ul>";
964
965 for ($i = $updater->getSchemaVersion() + 1; $i <= SCHEMA_VERSION; $i++) {
966 print "<li>Performing update up to version $i...";
967
977cea14 968 $result = $updater->performUpdateTo($i, true);
f240d26e
AD
969
970 if (!$result) {
971 print "<span class='err'>FAILED!</span></li></ul>";
972
973 print_warning("One of the updates failed. Either retry the process or perform updates manually.");
974 print "<p><form method=\"GET\" action=\"index.php\">
975 <input type=\"submit\" value=\"".__("Return to Tiny Tiny RSS")."\">
976 </form>";
977
977cea14 978 return;
f240d26e
AD
979 } else {
980 print "<span class='ok'>OK!</span></li>";
981 }
982 }
983
984 print "</ul>";
b4c47f7e 985
f240d26e 986 print_notice("Your Tiny Tiny RSS database is now updated to the latest version.");
b4c47f7e 987
f240d26e
AD
988 print "<p><form method=\"GET\" action=\"index.php\">
989 <input type=\"submit\" value=\"".__("Return to Tiny Tiny RSS")."\">
990 </form>";
991
992 } else {
993 print "<h2>Your database is up to date.</h2>";
994
995 print "<p><form method=\"GET\" action=\"index.php\">
996 <input type=\"submit\" value=\"".__("Return to Tiny Tiny RSS")."\">
997 </form>";
998 }
999 } else {
1000 if ($updater->isUpdateRequired()) {
1001
1002 print "<h2>Database update required</h2>";
b4c47f7e 1003
b8774453
AD
1004 print_notice("<h4>".
1005 sprintf("Your Tiny Tiny RSS database needs update to the latest version: %d to %d.",
1006 $updater->getSchemaVersion(), SCHEMA_VERSION).
1007 "</h4>");
b4c47f7e 1008
f240d26e 1009 print_warning("Please backup your database before proceeding.");
b4c47f7e 1010
f240d26e
AD
1011 print "<form method='POST'>
1012 <input type='hidden' name='subop' value='performupdate'>
1013 <input type='submit' onclick='return confirmOP()' value='".__("Perform updates")."'>
1014 </form>";
1015
1016 } else {
1017
3c200461 1018 print_notice("Tiny Tiny RSS database is up to date.");
f240d26e
AD
1019
1020 print "<p><form method=\"GET\" action=\"index.php\">
1021 <input type=\"submit\" value=\"".__("Return to Tiny Tiny RSS")."\">
1022 </form>";
1023
1024 }
1025 }
1026 ?>
b4c47f7e 1027
f240d26e
AD
1028 </div>
1029 </body>
1030 </html>
1031 <?php
f43e9e97
AD
1032 }
1033
41bead9b 1034 function cached_url() {
0c6f7b31
AD
1035 @$hash = basename($_GET['hash']);
1036
41bead9b
AD
1037 // we don't need an extension to find the file, hash is a complete URL
1038 $hash = preg_replace("/\.[^\.]*$/", "", $hash);
1039
0c6f7b31
AD
1040 if ($hash) {
1041
0442cbb6 1042 $filename = CACHE_DIR . '/images/' . $hash;
0c6f7b31
AD
1043
1044 if (file_exists($filename)) {
38b3998b
AD
1045 header("Content-Disposition: inline; filename=\"$hash\"");
1046 $mimetype = mime_content_type($filename);
0442cbb6 1047
0c6f7b31
AD
1048 /* See if we can use X-Sendfile */
1049 $xsendfile = false;
1050 if (function_exists('apache_get_modules') &&
1051 array_search('mod_xsendfile', apache_get_modules()))
1052 $xsendfile = true;
1053
1054 if ($xsendfile) {
1055 header("X-Sendfile: $filename");
38b3998b
AD
1056 header("Content-type: $mimetype");
1057 header('Content-Disposition: inline; filename="' . basename($filename) . '"');
0c6f7b31 1058 } else {
38b3998b 1059 header("Content-type: $mimetype");
0c6f7b31
AD
1060 $stamp = gmdate("D, d M Y H:i:s", filemtime($filename)). " GMT";
1061 header("Last-Modified: $stamp", true);
1062 readfile($filename);
1063 }
1064 } else {
1065 header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
1066 echo "File not found.";
1067 }
1068 }
1069 }
1070
f75e7c64 1071 private function make_article_tag_uri($id, $timestamp) {
4b7391de
AD
1072
1073 $timestamp = date("Y-m-d", strtotime($timestamp));
1074
1075 return "tag:" . parse_url(get_self_url_prefix(), PHP_URL_HOST) . ",$timestamp:/$id";
1076 }
4daaf234
AD
1077
1078 // this should be used very carefully because this endpoint is exposed to unauthenticated users
1079 // plugin data is not loaded because there's no user context and owner_uid/session may or may not be available
1080 // in general, don't do anything user-related in here and do not modify $_SESSION
1081 public function pluginhandler() {
1082 $host = new PluginHost();
1083
1084 $plugin = basename($_REQUEST["plugin"]);
1085 $method = $_REQUEST["pmethod"];
1086
1087 $host->load($plugin, PluginHost::KIND_USER, 0);
1088 $host->load_data();
1089
1090 $pclass = $host->get_plugin($plugin);
1091
1092 if ($pclass) {
1093 if (method_exists($pclass, $method)) {
1094 if ($pclass->is_public_method($method)) {
1095 $pclass->$method();
1096 } else {
1097 header("Content-Type: text/json");
1098 print error_json(6);
1099 }
1100 } else {
1101 header("Content-Type: text/json");
1102 print error_json(13);
1103 }
1104 } else {
1105 header("Content-Type: text/json");
1106 print error_json(14);
1107 }
1108 }
5f0a3741 1109}
4daaf234 1110?>