]>
Commit | Line | Data |
---|---|---|
1d3a17c7 | 1 | <?php |
f1a80dae | 2 | |
894ebcf5 | 3 | /* if ($_GET["debug"]) { |
cce28758 AD |
4 | define('DEFAULT_ERROR_LEVEL', E_ALL); |
5 | } else { | |
6 | define('DEFAULT_ERROR_LEVEL', E_ERROR | E_WARNING | E_PARSE); | |
894ebcf5 | 7 | } */ |
cce28758 | 8 | |
40d13c28 | 9 | require_once 'config.php'; |
b619ff15 | 10 | require_once 'db-prefs.php'; |
5bc0bd27 | 11 | require_once 'compat.php'; |
af106b0e | 12 | require_once 'errors.php'; |
8911ac8b | 13 | require_once 'version.php'; |
40d13c28 | 14 | |
7c5a308d | 15 | if (RSS_BACKEND_TYPE == "magpie") { |
ed891a51 | 16 | require_once "magpierss/rss_fetch.inc"; |
7c5a308d AD |
17 | require_once 'magpierss/rss_utils.inc'; |
18 | } else if (RSS_BACKEND_TYPE == "simplepie") { | |
19 | require_once 'simplepie/simplepie.inc'; | |
20 | } | |
387234f3 | 21 | |
a3ee2a38 AD |
22 | define('MAGPIE_OUTPUT_ENCODING', 'UTF-8'); |
23 | ||
ad507f85 AD |
24 | function purge_feed($link, $feed_id, $purge_interval, $debug = false) { |
25 | ||
26 | $rows = -1; | |
4c193675 | 27 | |
fefa6ca3 | 28 | if (DB_TYPE == "pgsql") { |
44e241cb | 29 | /* $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE |
fefa6ca3 | 30 | marked = false AND feed_id = '$feed_id' AND |
35d8cf43 | 31 | (SELECT date_entered FROM ttrss_entries WHERE |
44e241cb AD |
32 | id = ref_id) < NOW() - INTERVAL '$purge_interval days'"); */ |
33 | ||
6e7f8d26 AD |
34 | $pg_version = get_pgsql_version($link); |
35 | ||
36 | if (preg_match("/^7\./", $pg_version) || preg_match("/^8\.0/", $pg_version)) { | |
1e59ae35 AD |
37 | |
38 | $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE | |
39 | ttrss_entries.id = ref_id AND | |
40 | marked = false AND | |
41 | feed_id = '$feed_id' AND | |
42 | ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days'"); | |
43 | ||
44 | } else { | |
45 | ||
46 | $result = db_query($link, "DELETE FROM ttrss_user_entries | |
47 | USING ttrss_entries | |
48 | WHERE ttrss_entries.id = ref_id AND | |
49 | marked = false AND | |
50 | feed_id = '$feed_id' AND | |
fc774155 | 51 | ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days'"); |
1e59ae35 | 52 | } |
ad507f85 AD |
53 | |
54 | $rows = pg_affected_rows($result); | |
55 | ||
fefa6ca3 | 56 | } else { |
1e59ae35 | 57 | |
30f1746f | 58 | /* $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE |
fefa6ca3 | 59 | marked = false AND feed_id = '$feed_id' AND |
35d8cf43 | 60 | (SELECT date_entered FROM ttrss_entries WHERE |
30f1746f AD |
61 | id = ref_id) < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)"); */ |
62 | ||
63 | $result = db_query($link, "DELETE FROM ttrss_user_entries | |
64 | USING ttrss_user_entries, ttrss_entries | |
65 | WHERE ttrss_entries.id = ref_id AND | |
66 | marked = false AND | |
67 | feed_id = '$feed_id' AND | |
68 | ttrss_entries.date_entered < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)"); | |
69 | ||
ad507f85 AD |
70 | $rows = mysql_affected_rows($link); |
71 | ||
72 | } | |
73 | ||
74 | if ($debug) { | |
75 | print "Purged feed $feed_id ($purge_interval): deleted $rows articles\n"; | |
fefa6ca3 AD |
76 | } |
77 | } | |
78 | ||
44e241cb AD |
79 | function global_purge_old_posts($link, $do_output = false, $limit = false) { |
80 | ||
894ebcf5 | 81 | $random_qpart = sql_random_function(); |
fefa6ca3 | 82 | |
44e241cb AD |
83 | if ($limit) { |
84 | $limit_qpart = "LIMIT $limit"; | |
85 | } else { | |
86 | $limit_qpart = ""; | |
87 | } | |
88 | ||
fefa6ca3 | 89 | $result = db_query($link, |
44e241cb AD |
90 | "SELECT id,purge_interval,owner_uid FROM ttrss_feeds |
91 | ORDER BY $random_qpart $limit_qpart"); | |
fefa6ca3 AD |
92 | |
93 | while ($line = db_fetch_assoc($result)) { | |
94 | ||
95 | $feed_id = $line["id"]; | |
96 | $purge_interval = $line["purge_interval"]; | |
97 | $owner_uid = $line["owner_uid"]; | |
98 | ||
99 | if ($purge_interval == 0) { | |
100 | ||
101 | $tmp_result = db_query($link, | |
102 | "SELECT value FROM ttrss_user_prefs WHERE | |
103 | pref_name = 'PURGE_OLD_DAYS' AND owner_uid = '$owner_uid'"); | |
104 | ||
105 | if (db_num_rows($tmp_result) != 0) { | |
106 | $purge_interval = db_fetch_result($tmp_result, 0, "value"); | |
107 | } | |
108 | } | |
109 | ||
110 | if ($do_output) { | |
ad507f85 | 111 | // print "Feed $feed_id: purge interval = $purge_interval\n"; |
fefa6ca3 AD |
112 | } |
113 | ||
114 | if ($purge_interval > 0) { | |
ad507f85 | 115 | purge_feed($link, $feed_id, $purge_interval, $do_output); |
fefa6ca3 AD |
116 | } |
117 | } | |
118 | ||
71604ca4 AD |
119 | // purge orphaned posts in main content table |
120 | db_query($link, "DELETE FROM ttrss_entries WHERE | |
121 | (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0"); | |
122 | ||
fefa6ca3 AD |
123 | } |
124 | ||
b6eefba5 | 125 | function purge_old_posts($link) { |
5d73494a | 126 | |
f1a80dae AD |
127 | $user_id = $_SESSION["uid"]; |
128 | ||
129 | $result = db_query($link, "SELECT id,purge_interval FROM ttrss_feeds | |
130 | WHERE owner_uid = '$user_id'"); | |
5d73494a AD |
131 | |
132 | while ($line = db_fetch_assoc($result)) { | |
133 | ||
134 | $feed_id = $line["id"]; | |
135 | $purge_interval = $line["purge_interval"]; | |
136 | ||
b619ff15 | 137 | if ($purge_interval == 0) $purge_interval = get_pref($link, 'PURGE_OLD_DAYS'); |
5d73494a | 138 | |
140aae81 | 139 | if ($purge_interval > 0) { |
fefa6ca3 | 140 | purge_feed($link, $feed_id, $purge_interval); |
5d73494a AD |
141 | } |
142 | } | |
71604ca4 AD |
143 | |
144 | // purge orphaned posts in main content table | |
145 | db_query($link, "DELETE FROM ttrss_entries WHERE | |
146 | (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0"); | |
c3a8d71a AD |
147 | } |
148 | ||
1f2b01ed | 149 | function update_all_feeds($link, $fetch, $user_id = false, $force_daemon = false) { |
40d13c28 | 150 | |
4769ddaf | 151 | if (WEB_DEMO_MODE) return; |
b0b4abcf | 152 | |
a2770077 AD |
153 | if (!$user_id) { |
154 | $user_id = $_SESSION["uid"]; | |
155 | purge_old_posts($link); | |
156 | } | |
157 | ||
25af8dad | 158 | // db_query($link, "BEGIN"); |
b82af8c3 | 159 | |
cbd8650d AD |
160 | if (MAX_UPDATE_TIME > 0) { |
161 | if (DB_TYPE == "mysql") { | |
162 | $q_order = "RAND()"; | |
163 | } else { | |
164 | $q_order = "RANDOM()"; | |
165 | } | |
166 | } else { | |
167 | $q_order = "last_updated DESC"; | |
168 | } | |
169 | ||
d148926e | 170 | $result = db_query($link, "SELECT feed_url,id, |
798f722b | 171 | SUBSTRING(last_updated,1,19) AS last_updated, |
5c563acd | 172 | update_interval FROM ttrss_feeds WHERE owner_uid = '$user_id' |
cbd8650d AD |
173 | ORDER BY $q_order"); |
174 | ||
175 | $upd_start = time(); | |
40d13c28 | 176 | |
b6eefba5 | 177 | while ($line = db_fetch_assoc($result)) { |
d148926e AD |
178 | $upd_intl = $line["update_interval"]; |
179 | ||
b619ff15 | 180 | if (!$upd_intl || $upd_intl == 0) { |
a2770077 | 181 | $upd_intl = get_pref($link, 'DEFAULT_UPDATE_INTERVAL', $user_id); |
b619ff15 | 182 | } |
d148926e | 183 | |
c1e202b7 AD |
184 | if ($upd_intl < 0) { |
185 | // Updates for this feed are disabled | |
186 | continue; | |
187 | } | |
188 | ||
93d40f50 AD |
189 | if ($fetch || (!$line["last_updated"] || |
190 | time() - strtotime($line["last_updated"]) > ($upd_intl * 60))) { | |
c5142cca | 191 | |
cbd8650d AD |
192 | // print "<!-- feed: ".$line["feed_url"]." -->"; |
193 | ||
1f2b01ed | 194 | update_rss_feed($link, $line["feed_url"], $line["id"], $force_daemon); |
cbd8650d AD |
195 | |
196 | $upd_elapsed = time() - $upd_start; | |
197 | ||
198 | if (MAX_UPDATE_TIME > 0 && $upd_elapsed > MAX_UPDATE_TIME) { | |
199 | return; | |
200 | } | |
d148926e | 201 | } |
40d13c28 AD |
202 | } |
203 | ||
25af8dad | 204 | // db_query($link, "COMMIT"); |
b82af8c3 | 205 | |
40d13c28 AD |
206 | } |
207 | ||
9e997874 | 208 | function check_feed_favicon($feed_url, $feed, $link) { |
78800912 AD |
209 | $feed_url = str_replace("http://", "", $feed_url); |
210 | $feed_url = preg_replace("/\/.*$/", "", $feed_url); | |
211 | ||
212 | $icon_url = "http://$feed_url/favicon.ico"; | |
273a2f6b | 213 | $icon_file = ICONS_DIR . "/$feed.ico"; |
78800912 AD |
214 | |
215 | if (!file_exists($icon_file)) { | |
78800912 | 216 | |
c798704b AD |
217 | if (USE_CURL_FOR_ICONS) { |
218 | //error_reporting(0); | |
219 | $ch = curl_init($icon_url); | |
220 | $fp = fopen($icon_file, "w"); | |
78800912 | 221 | |
c798704b AD |
222 | if ($fp) { |
223 | curl_setopt($ch, CURLOPT_FILE, $fp); | |
224 | curl_setopt($ch, CURLOPT_FILE, $fp); | |
225 | ||
226 | curl_exec($ch); | |
227 | curl_close($ch); | |
228 | fclose($fp); | |
e695fdc8 | 229 | } |
717f5e64 | 230 | |
c798704b AD |
231 | //error_reporting (DEFAULT_ERROR_LEVEL); |
232 | ||
233 | } else { | |
234 | ||
235 | error_reporting(0); | |
236 | $r = fopen($icon_url, "r"); | |
cce28758 | 237 | error_reporting (DEFAULT_ERROR_LEVEL); |
c798704b AD |
238 | |
239 | if ($r) { | |
240 | $tmpfname = tempnam(TMP_DIRECTORY, "ttrssicon"); | |
241 | ||
242 | $t = fopen($tmpfname, "w"); | |
243 | ||
244 | while (!feof($r)) { | |
245 | $buf = fread($r, 16384); | |
246 | fwrite($t, $buf); | |
247 | } | |
248 | ||
249 | fclose($r); | |
250 | fclose($t); | |
251 | ||
252 | error_reporting(0); | |
253 | if (!rename($tmpfname, $icon_file)) { | |
254 | unlink($tmpfname); | |
255 | } | |
256 | ||
257 | chmod($icon_file, 0644); | |
258 | ||
259 | error_reporting (DEFAULT_ERROR_LEVEL); | |
260 | } | |
78800912 AD |
261 | |
262 | } | |
263 | } | |
264 | } | |
265 | ||
ddb68b81 | 266 | function update_rss_feed($link, $feed_url, $feed, $ignore_daemon = false) { |
40d13c28 | 267 | |
4769ddaf | 268 | if (WEB_DEMO_MODE) return; |
b0b4abcf | 269 | |
ddb68b81 | 270 | if (DAEMON_REFRESH_ONLY && !$_GET["daemon"] && !$ignore_daemon) { |
21cfcdf2 AD |
271 | return; |
272 | } | |
273 | ||
47c6c988 | 274 | $result = db_query($link, "SELECT update_interval,auth_login,auth_pass |
a88c1f36 AD |
275 | FROM ttrss_feeds WHERE id = '$feed'"); |
276 | ||
ff25e639 AD |
277 | $auth_login = db_unescape_string(db_fetch_result($result, 0, "auth_login")); |
278 | $auth_pass = db_unescape_string(db_fetch_result($result, 0, "auth_pass")); | |
47c6c988 | 279 | |
a88c1f36 AD |
280 | $update_interval = db_fetch_result($result, 0, "update_interval"); |
281 | ||
282 | if ($update_interval < 0) { return; } | |
283 | ||
ab3d0b99 AD |
284 | $feed = db_escape_string($feed); |
285 | ||
47c6c988 AD |
286 | $fetch_url = $feed_url; |
287 | ||
288 | if ($auth_login && $auth_pass) { | |
289 | $url_parts = array(); | |
290 | preg_match("/(^[^:]*):\/\/(.*)/", $fetch_url, $url_parts); | |
291 | ||
292 | if ($url_parts[1] && $url_parts[2]) { | |
293 | $fetch_url = $url_parts[1] . "://$auth_login:$auth_pass@" . $url_parts[2]; | |
294 | } | |
295 | ||
296 | } | |
ab3d0b99 | 297 | |
7c5a308d AD |
298 | if (RSS_BACKEND_TYPE == "magpie") { |
299 | error_reporting(0); | |
300 | $rss = fetch_rss($fetch_url); | |
301 | error_reporting (DEFAULT_ERROR_LEVEL); | |
302 | } else if (RSS_BACKEND_TYPE == "simplepie") { | |
303 | ||
304 | if (!file_exists(SIMPLEPIE_CACHE_DIR)) { | |
305 | mkdir(SIMPLEPIE_CACHE_DIR); | |
306 | } | |
76798ff3 | 307 | |
7c5a308d AD |
308 | $rss = new SimplePie(); |
309 | $rss->feed_url($fetch_url); | |
310 | $rss->cache_location(SIMPLEPIE_CACHE_DIR); | |
311 | $rss->init(); | |
312 | } | |
313 | ||
b6eefba5 | 314 | $feed = db_escape_string($feed); |
dcee8f61 | 315 | |
7c5a308d | 316 | $rss_check = $rss; |
b82af8c3 | 317 | |
7c5a308d AD |
318 | if (RSS_BACKEND_TYPE == "simplepie") { |
319 | $rss_check = $rss->data; | |
320 | } | |
321 | ||
322 | if ($rss_check) { | |
323 | ||
44e241cb | 324 | // db_query($link, "BEGIN"); |
dd8c76a9 | 325 | |
a88c1f36 | 326 | $result = db_query($link, "SELECT title,icon_url,site_url,owner_uid |
f324892e | 327 | FROM ttrss_feeds WHERE id = '$feed'"); |
331900c6 | 328 | |
b6eefba5 AD |
329 | $registered_title = db_fetch_result($result, 0, "title"); |
330 | $orig_icon_url = db_fetch_result($result, 0, "icon_url"); | |
f324892e | 331 | $orig_site_url = db_fetch_result($result, 0, "site_url"); |
331900c6 | 332 | |
7fed1940 AD |
333 | $owner_uid = db_fetch_result($result, 0, "owner_uid"); |
334 | ||
8d0ec6fd | 335 | if (get_pref($link, 'ENABLE_FEED_ICONS', $owner_uid, false)) { |
a2770077 AD |
336 | check_feed_favicon($feed_url, $feed, $link); |
337 | } | |
338 | ||
746b249f | 339 | if (!$registered_title || $registered_title == "[Unknown]") { |
7c5a308d AD |
340 | |
341 | if (RSS_BACKEND_TYPE == "magpie") { | |
342 | $feed_title = db_escape_string($rss->channel["title"]); | |
343 | } else { | |
344 | $feed_title = $rss->get_feed_title(); | |
345 | } | |
346 | ||
f324892e AD |
347 | db_query($link, "UPDATE ttrss_feeds SET |
348 | title = '$feed_title' WHERE id = '$feed'"); | |
349 | } | |
350 | ||
7c5a308d AD |
351 | if (RSS_BACKEND_TYPE == "magpie") { |
352 | $site_url = $rss->channel["link"]; | |
353 | // weird, weird Magpie | |
354 | if (!$site_url) $site_url = db_escape_string($rss->channel["link_"]); | |
355 | } else { | |
356 | $site_url = $rss->get_feed_link(); | |
357 | } | |
147f7691 AD |
358 | |
359 | if ($site_url && $orig_site_url != db_escape_string($site_url)) { | |
f324892e AD |
360 | db_query($link, "UPDATE ttrss_feeds SET |
361 | site_url = '$site_url' WHERE id = '$feed'"); | |
331900c6 | 362 | } |
40d13c28 | 363 | |
b7f4bda2 AD |
364 | // print "I: " . $rss->channel["image"]["url"]; |
365 | ||
7c5a308d AD |
366 | if (RSS_BACKEND_TYPE == "magpie") { |
367 | $icon_url = $rss->image["url"]; | |
368 | } else { | |
369 | $icon_url = $rss->get_image_url(); # FIXME | |
370 | } | |
b7f4bda2 | 371 | |
147f7691 | 372 | if ($icon_url && !$orig_icon_url != db_escape_string($icon_url)) { |
b6eefba5 AD |
373 | $icon_url = db_escape_string($icon_url); |
374 | db_query($link, "UPDATE ttrss_feeds SET icon_url = '$icon_url' WHERE id = '$feed'"); | |
b7f4bda2 AD |
375 | } |
376 | ||
e6155a06 AD |
377 | |
378 | $filters = array(); | |
379 | ||
4b3dff6e | 380 | $result = db_query($link, "SELECT reg_exp, |
db42b934 AD |
381 | ttrss_filter_types.name AS name, |
382 | ttrss_filter_actions.name AS action | |
383 | FROM ttrss_filters,ttrss_filter_types,ttrss_filter_actions WHERE | |
384 | owner_uid = $owner_uid AND | |
385 | ttrss_filter_types.id = filter_type AND | |
386 | ttrss_filter_actions.id = action_id AND | |
ead60402 | 387 | (feed_id IS NULL OR feed_id = '$feed')"); |
e6155a06 | 388 | |
b6eefba5 | 389 | while ($line = db_fetch_assoc($result)) { |
e6155a06 | 390 | if (!$filters[$line["name"]]) $filters[$line["name"]] = array(); |
19c9cb11 AD |
391 | |
392 | $filter["reg_exp"] = $line["reg_exp"]; | |
393 | $filter["action"] = $line["action"]; | |
394 | ||
395 | array_push($filters[$line["name"]], $filter); | |
e6155a06 AD |
396 | } |
397 | ||
7c5a308d AD |
398 | if (RSS_BACKEND_TYPE == "magpie") { |
399 | $iterator = $rss->items; | |
ddb68b81 | 400 | |
7c5a308d AD |
401 | if (!$iterator || !is_array($iterator)) $iterator = $rss->entries; |
402 | if (!$iterator || !is_array($iterator)) $iterator = $rss; | |
403 | ||
404 | } else { | |
405 | $iterator = $rss->get_items(); | |
406 | } | |
c22789da AD |
407 | |
408 | if (!is_array($iterator)) { | |
39541e74 | 409 | /* db_query($link, "UPDATE ttrss_feeds |
75bd0669 | 410 | SET last_error = 'Parse error: can\'t find any articles.' |
39541e74 | 411 | WHERE id = '$feed'"); */ |
c22789da AD |
412 | return; // WTF? |
413 | } | |
ddb68b81 AD |
414 | |
415 | foreach ($iterator as $item) { | |
7c5a308d AD |
416 | |
417 | if (RSS_BACKEND_TYPE == "magpie") { | |
418 | ||
419 | $entry_guid = $item["id"]; | |
420 | ||
421 | if (!$entry_guid) $entry_guid = $item["guid"]; | |
422 | if (!$entry_guid) $entry_guid = $item["link"]; | |
40d13c28 | 423 | |
7c5a308d | 424 | if (!$entry_guid) continue; |
40d13c28 | 425 | |
7c5a308d AD |
426 | $entry_timestamp = ""; |
427 | ||
428 | $rss_2_date = $item['pubdate']; | |
429 | $rss_1_date = $item['dc']['date']; | |
430 | $atom_date = $item['issued']; | |
431 | if (!$atom_date) $atom_date = $item['updated']; | |
b82af8c3 | 432 | |
7c5a308d AD |
433 | if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date); |
434 | if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date); | |
435 | if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date); | |
436 | ||
437 | if ($entry_timestamp == "") { | |
438 | $entry_timestamp = time(); | |
439 | $no_orig_date = 'true'; | |
440 | } else { | |
441 | $no_orig_date = 'false'; | |
442 | } | |
443 | ||
444 | $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp); | |
445 | ||
446 | $entry_title = $item["title"]; | |
447 | ||
448 | // strange Magpie workaround | |
449 | $entry_link = $item["link_"]; | |
450 | if (!$entry_link) $entry_link = $item["link"]; | |
451 | ||
452 | if (!$entry_title) continue; | |
453 | if (!$entry_link) continue; | |
454 | ||
455 | $entry_content = $item["content:escaped"]; | |
456 | ||
457 | if (!$entry_content) $entry_content = $item["content:encoded"]; | |
458 | if (!$entry_content) $entry_content = $item["content"]; | |
79b5d2d2 | 459 | if (!$entry_content) $entry_content = $item["atom_content"]; |
7c5a308d AD |
460 | if (!$entry_content) $entry_content = $item["summary"]; |
461 | if (!$entry_content) $entry_content = $item["description"]; | |
462 | ||
463 | // if (!$entry_content) continue; | |
464 | ||
465 | // WTF | |
466 | if (is_array($entry_content)) { | |
467 | $entry_content = $entry_content["encoded"]; | |
468 | if (!$entry_content) $entry_content = $entry_content["escaped"]; | |
469 | } | |
470 | ||
471 | // print_r($item); | |
472 | // print_r(htmlspecialchars($entry_content)); | |
473 | // print "<br>"; | |
474 | ||
475 | $entry_content_unescaped = $entry_content; | |
476 | $content_hash = "SHA1:" . sha1(strip_tags($entry_content)); | |
477 | ||
478 | $entry_comments = $item["comments"]; | |
479 | ||
480 | $entry_author = db_escape_string($item['dc']['creator']); | |
481 | ||
482 | $entry_guid = db_escape_string($entry_guid); | |
483 | ||
484 | $result = db_query($link, "SELECT id FROM ttrss_entries | |
485 | WHERE guid = '$entry_guid'"); | |
486 | ||
487 | $entry_content = db_escape_string($entry_content); | |
488 | $entry_title = db_escape_string($entry_title); | |
489 | $entry_link = db_escape_string($entry_link); | |
490 | $entry_comments = db_escape_string($entry_comments); | |
491 | ||
492 | $num_comments = db_escape_string($item["slash"]["comments"]); | |
493 | ||
494 | if (!$num_comments) $num_comments = 0; | |
71ad3959 | 495 | |
7c5a308d | 496 | } else if (RSS_BACKEND_TYPE == "simplepie") { |
ddb68b81 | 497 | |
7c5a308d | 498 | $entry_guid = $item->get_id(); |
71ad3959 | 499 | |
7c5a308d AD |
500 | if (!$entry_guid) { |
501 | $entry_guid = $item->get_permalink(); | |
502 | } | |
503 | ||
504 | if (!$entry_guid) continue; | |
505 | ||
506 | $entry_timestamp = $item->get_date("U"); | |
507 | ||
508 | if ($entry_timestamp == "") { | |
509 | $entry_timestamp = time(); | |
510 | $no_orig_date = 'true'; | |
511 | } else { | |
512 | $no_orig_date = 'false'; | |
513 | } | |
514 | ||
515 | $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp); | |
516 | ||
517 | $entry_title = $item->get_title(); | |
518 | $entry_link = $item->get_permalink(); | |
519 | ||
520 | if (!$entry_title) continue; | |
521 | if (!$entry_link) continue; | |
71ad3959 | 522 | |
7c5a308d AD |
523 | $entry_content = $item->get_description(); |
524 | ||
525 | // print_r(htmlspecialchars($entry_content)); | |
526 | // print "<br>"; | |
527 | ||
528 | $entry_content_unescaped = $entry_content; | |
529 | $content_hash = "SHA1:" . sha1(strip_tags($entry_content)); | |
530 | ||
531 | $entry_comments = ""; # FIXME | |
532 | ||
533 | $entry_author = $item->get_author(0); | |
1696229f | 534 | |
7c5a308d AD |
535 | $entry_author = db_escape_string($entry_author->name); |
536 | ||
537 | $entry_guid = db_escape_string($entry_guid); | |
a2015351 | 538 | |
7c5a308d AD |
539 | $result = db_query($link, "SELECT id FROM ttrss_entries |
540 | WHERE guid = '$entry_guid'"); | |
541 | ||
542 | $entry_content = db_escape_string($entry_content); | |
543 | $entry_title = db_escape_string($entry_title); | |
544 | $entry_link = db_escape_string($entry_link); | |
545 | $entry_comments = db_escape_string($entry_comments); | |
546 | ||
547 | $num_comments = 0; # FIXME | |
548 | ||
549 | if (!$num_comments) $num_comments = 0; | |
a2015351 | 550 | |
8add756a AD |
551 | } |
552 | ||
d48d160c | 553 | # sanitize content |
183ad07b AD |
554 | |
555 | $entry_content = sanitize_rss($entry_content); | |
556 | $entry_title = sanitize_rss($entry_title); | |
557 | $entry_link = sanitize_rss($entry_link); | |
558 | $entry_comments = sanitize_rss($entry_comments); | |
d48d160c | 559 | |
44e241cb AD |
560 | db_query($link, "BEGIN"); |
561 | ||
4c193675 AD |
562 | if (db_num_rows($result) == 0) { |
563 | ||
564 | // base post entry does not exist, create it | |
565 | ||
4c193675 AD |
566 | $result = db_query($link, |
567 | "INSERT INTO ttrss_entries | |
568 | (title, | |
569 | guid, | |
570 | link, | |
571 | updated, | |
572 | content, | |
573 | content_hash, | |
574 | no_orig_date, | |
575 | date_entered, | |
11b0dce2 | 576 | comments, |
b6104dee AD |
577 | num_comments, |
578 | author) | |
4c193675 AD |
579 | VALUES |
580 | ('$entry_title', | |
581 | '$entry_guid', | |
582 | '$entry_link', | |
583 | '$entry_timestamp_fmt', | |
584 | '$entry_content', | |
585 | '$content_hash', | |
586 | $no_orig_date, | |
587 | NOW(), | |
11b0dce2 | 588 | '$entry_comments', |
b6104dee AD |
589 | '$num_comments', |
590 | '$entry_author')"); | |
8926aab8 AD |
591 | } else { |
592 | // we keep encountering the entry in feeds, so we need to | |
593 | // update date_entered column so that we don't get horrible | |
594 | // dupes when the entry gets purged and reinserted again e.g. | |
595 | // in the case of SLOW SLOW OMG SLOW updating feeds | |
596 | ||
597 | $base_entry_id = db_fetch_result($result, 0, "id"); | |
598 | ||
599 | db_query($link, "UPDATE ttrss_entries SET date_entered = NOW() | |
600 | WHERE id = '$base_entry_id'"); | |
4c193675 AD |
601 | } |
602 | ||
603 | // now it should exist, if not - bad luck then | |
604 | ||
6385315d AD |
605 | $result = db_query($link, "SELECT |
606 | id,content_hash,no_orig_date,title, | |
8926aab8 | 607 | substring(date_entered,1,19) as date_entered, |
11b0dce2 AD |
608 | substring(updated,1,19) as updated, |
609 | num_comments | |
6385315d AD |
610 | FROM |
611 | ttrss_entries | |
612 | WHERE guid = '$entry_guid'"); | |
4c193675 AD |
613 | |
614 | if (db_num_rows($result) == 1) { | |
615 | ||
11b0dce2 AD |
616 | // this will be used below in update handler |
617 | $orig_content_hash = db_fetch_result($result, 0, "content_hash"); | |
618 | $orig_title = db_fetch_result($result, 0, "title"); | |
619 | $orig_num_comments = db_fetch_result($result, 0, "num_comments"); | |
8926aab8 AD |
620 | $orig_date_entered = strtotime(db_fetch_result($result, |
621 | 0, "date_entered")); | |
6385315d | 622 | |
11b0dce2 | 623 | $ref_id = db_fetch_result($result, 0, "id"); |
4c193675 | 624 | |
11b0dce2 | 625 | // check for user post link to main table |
4c193675 | 626 | |
11b0dce2 | 627 | // do we allow duplicate posts with same GUID in different feeds? |
8d0ec6fd | 628 | if (get_pref($link, "ALLOW_DUPLICATE_POSTS", $owner_uid, false)) { |
11b0dce2 AD |
629 | $dupcheck_qpart = "AND feed_id = '$feed'"; |
630 | } else { | |
631 | $dupcheck_qpart = ""; | |
632 | } | |
71604ca4 | 633 | |
11b0dce2 | 634 | // error_reporting(0); |
19c9cb11 | 635 | |
11b0dce2 AD |
636 | $filter_name = get_filter_name($entry_title, $entry_content, |
637 | $entry_link, $filters); | |
19c9cb11 | 638 | |
11b0dce2 AD |
639 | if ($filter_name == "filter") { |
640 | continue; | |
641 | } | |
19c9cb11 | 642 | |
11b0dce2 | 643 | // error_reporting (DEFAULT_ERROR_LEVEL); |
3a933f22 | 644 | |
11b0dce2 AD |
645 | $result = db_query($link, |
646 | "SELECT ref_id FROM ttrss_user_entries WHERE | |
647 | ref_id = '$ref_id' AND owner_uid = '$owner_uid' | |
648 | $dupcheck_qpart"); | |
649 | ||
650 | // okay it doesn't exist - create user entry | |
651 | if (db_num_rows($result) == 0) { | |
652 | ||
653 | if ($filter_name != 'catchup') { | |
654 | $unread = 'true'; | |
655 | $last_read_qpart = 'NULL'; | |
656 | } else { | |
657 | $unread = 'false'; | |
658 | $last_read_qpart = 'NOW()'; | |
659 | } | |
dd7d3187 AD |
660 | |
661 | if ($filter_name == 'mark') { | |
662 | $marked = 'true'; | |
663 | } else { | |
664 | $marked = 'false'; | |
665 | } | |
19c9cb11 | 666 | |
11b0dce2 AD |
667 | $result = db_query($link, |
668 | "INSERT INTO ttrss_user_entries | |
dd7d3187 | 669 | (ref_id, owner_uid, feed_id, unread, last_read, marked) |
11b0dce2 | 670 | VALUES ('$ref_id', '$owner_uid', '$feed', $unread, |
dd7d3187 | 671 | $last_read_qpart, $marked)"); |
11b0dce2 AD |
672 | } |
673 | ||
6385315d AD |
674 | $post_needs_update = false; |
675 | ||
8d0ec6fd | 676 | if (get_pref($link, "UPDATE_POST_ON_CHECKSUM_CHANGE", $owner_uid, false) && |
6385315d AD |
677 | ($content_hash != $orig_content_hash)) { |
678 | $post_needs_update = true; | |
679 | } | |
680 | ||
681 | if ($orig_title != $entry_title) { | |
682 | $post_needs_update = true; | |
683 | } | |
684 | ||
11b0dce2 AD |
685 | if ($orig_num_comments != $num_comments) { |
686 | $post_needs_update = true; | |
687 | } | |
688 | ||
6385315d AD |
689 | // this doesn't seem to be very reliable |
690 | // | |
691 | // if ($orig_timestamp != $entry_timestamp && !$orig_no_orig_date) { | |
692 | // $post_needs_update = true; | |
693 | // } | |
694 | ||
695 | // if post needs update, update it and mark all user entries | |
1c73bc0c | 696 | // linking to this post as updated |
6385315d AD |
697 | if ($post_needs_update) { |
698 | ||
699 | // print "<!-- post $orig_title needs update : $post_needs_update -->"; | |
700 | ||
6385315d | 701 | db_query($link, "UPDATE ttrss_entries |
11b0dce2 AD |
702 | SET title = '$entry_title', content = '$entry_content', |
703 | num_comments = '$num_comments' | |
6385315d AD |
704 | WHERE id = '$ref_id'"); |
705 | ||
8d0ec6fd | 706 | if (get_pref($link, "MARK_UNREAD_ON_UPDATE", $owner_uid, false)) { |
4919fb42 AD |
707 | db_query($link, "UPDATE ttrss_user_entries |
708 | SET last_read = null, unread = true WHERE ref_id = '$ref_id'"); | |
709 | } else { | |
710 | db_query($link, "UPDATE ttrss_user_entries | |
711 | SET last_read = null WHERE ref_id = '$ref_id' AND unread = false"); | |
712 | } | |
6385315d AD |
713 | |
714 | } | |
4c193675 AD |
715 | } |
716 | ||
44e241cb AD |
717 | db_query($link, "COMMIT"); |
718 | ||
eb36b4eb AD |
719 | /* taaaags */ |
720 | // <a href="http://technorati.com/tag/Xorg" rel="tag">Xorg</a>, // | |
721 | ||
05732aa0 | 722 | $entry_tags = null; |
eb36b4eb | 723 | |
372ced8b | 724 | preg_match_all("/<a.*?href=.http:\/\/.*?technorati.com\/tag\/([^\"\'>]+)/i", |
ee2c3050 AD |
725 | $entry_content_unescaped, $entry_tags); |
726 | ||
727 | // print "<br>$entry_title : $entry_content_unescaped<br>"; | |
728 | // print_r($entry_tags); | |
372ced8b | 729 | // print "<br>"; |
eb36b4eb AD |
730 | |
731 | $entry_tags = $entry_tags[1]; | |
732 | ||
733 | if (count($entry_tags) > 0) { | |
734 | ||
44e241cb AD |
735 | db_query($link, "BEGIN"); |
736 | ||
05732aa0 AD |
737 | $result = db_query($link, "SELECT id,int_id |
738 | FROM ttrss_entries,ttrss_user_entries | |
25da6909 | 739 | WHERE guid = '$entry_guid' |
05732aa0 | 740 | AND feed_id = '$feed' AND ref_id = id |
7fed1940 | 741 | AND owner_uid = '$owner_uid'"); |
eb36b4eb | 742 | |
fe99ab12 | 743 | if (db_num_rows($result) == 1) { |
eb36b4eb | 744 | |
fe99ab12 AD |
745 | $entry_id = db_fetch_result($result, 0, "id"); |
746 | $entry_int_id = db_fetch_result($result, 0, "int_id"); | |
747 | ||
748 | foreach ($entry_tags as $tag) { | |
749 | $tag = db_escape_string(strtolower($tag)); | |
31483fc1 AD |
750 | |
751 | $tag = str_replace("+", " ", $tag); | |
fe99ab12 AD |
752 | $tag = str_replace("technorati tag: ", "", $tag); |
753 | ||
754 | $result = db_query($link, "SELECT id FROM ttrss_tags | |
755 | WHERE tag_name = '$tag' AND post_int_id = '$entry_int_id' AND | |
756 | owner_uid = '$owner_uid' LIMIT 1"); | |
757 | ||
758 | // print db_fetch_result($result, 0, "id"); | |
759 | ||
760 | if ($result && db_num_rows($result) == 0) { | |
761 | ||
762 | // print "tagging $entry_id as $tag<br>"; | |
763 | ||
764 | db_query($link, "INSERT INTO ttrss_tags | |
765 | (owner_uid,tag_name,post_int_id) | |
766 | VALUES ('$owner_uid','$tag', '$entry_int_id')"); | |
767 | } | |
768 | } | |
eb36b4eb | 769 | } |
44e241cb | 770 | db_query($link, "COMMIT"); |
05732aa0 | 771 | } |
4c193675 | 772 | } |
40d13c28 | 773 | |
ab3d0b99 AD |
774 | db_query($link, "UPDATE ttrss_feeds |
775 | SET last_updated = NOW(), last_error = '' WHERE id = '$feed'"); | |
eb36b4eb | 776 | |
44e241cb | 777 | // db_query($link, "COMMIT"); |
dd8c76a9 | 778 | |
ab3d0b99 AD |
779 | } else { |
780 | $error_msg = db_escape_string(magpie_error()); | |
781 | db_query($link, | |
aa5f9f5f AD |
782 | "UPDATE ttrss_feeds SET last_error = '$error_msg', |
783 | last_updated = NOW() WHERE id = '$feed'"); | |
40d13c28 AD |
784 | } |
785 | ||
786 | } | |
787 | ||
f175937c | 788 | function print_select($id, $default, $values, $attributes = "") { |
79f3553b | 789 | print "<select name=\"$id\" id=\"$id\" $attributes>"; |
a0d53889 AD |
790 | foreach ($values as $v) { |
791 | if ($v == $default) | |
792 | $sel = " selected"; | |
793 | else | |
794 | $sel = ""; | |
795 | ||
796 | print "<option$sel>$v</option>"; | |
797 | } | |
798 | print "</select>"; | |
799 | } | |
40d13c28 | 800 | |
79f3553b AD |
801 | function print_select_hash($id, $default, $values, $attributes = "") { |
802 | print "<select name=\"$id\" id='$id' $attributes>"; | |
673d54ca AD |
803 | foreach (array_keys($values) as $v) { |
804 | if ($v == $default) | |
805 | $sel = "selected"; | |
806 | else | |
807 | $sel = ""; | |
808 | ||
809 | print "<option $sel value=\"$v\">".$values[$v]."</option>"; | |
810 | } | |
811 | ||
812 | print "</select>"; | |
813 | } | |
814 | ||
19c9cb11 | 815 | function get_filter_name($title, $content, $link, $filters) { |
e6155a06 AD |
816 | |
817 | if ($filters["title"]) { | |
19c9cb11 AD |
818 | foreach ($filters["title"] as $filter) { |
819 | $reg_exp = $filter["reg_exp"]; | |
820 | if (preg_match("/$reg_exp/i", $title)) { | |
821 | return $filter["action"]; | |
822 | } | |
e6155a06 AD |
823 | } |
824 | } | |
825 | ||
826 | if ($filters["content"]) { | |
19c9cb11 AD |
827 | foreach ($filters["content"] as $filter) { |
828 | $reg_exp = $filter["reg_exp"]; | |
829 | if (preg_match("/$reg_exp/i", $content)) { | |
830 | return $filter["action"]; | |
831 | } | |
e6155a06 AD |
832 | } |
833 | } | |
834 | ||
835 | if ($filters["both"]) { | |
836 | foreach ($filters["both"] as $filter) { | |
19c9cb11 AD |
837 | $reg_exp = $filter["reg_exp"]; |
838 | if (preg_match("/$reg_exp/i", $title) || | |
839 | preg_match("/$reg_exp/i", $content)) { | |
840 | return $filter["action"]; | |
841 | } | |
e6155a06 AD |
842 | } |
843 | } | |
844 | ||
3a933f22 | 845 | if ($filters["link"]) { |
19c9cb11 AD |
846 | $reg_exp = $filter["reg_exp"]; |
847 | foreach ($filters["link"] as $filter) { | |
848 | $reg_exp = $filter["reg_exp"]; | |
849 | if (preg_match("/$reg_exp/i", $link)) { | |
850 | return $filter["action"]; | |
851 | } | |
3a933f22 AD |
852 | } |
853 | } | |
854 | ||
e6155a06 AD |
855 | return false; |
856 | } | |
857 | ||
9323147e | 858 | function printFeedEntry($feed_id, $class, $feed_title, $unread, $icon_file, $link, |
fb1fb4ab | 859 | $rtl_content = false, $last_updated = false, $last_error = false) { |
254e0e4b AD |
860 | |
861 | if (file_exists($icon_file) && filesize($icon_file) > 0) { | |
023fe037 | 862 | $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"$icon_file\">"; |
254e0e4b | 863 | } else { |
023fe037 | 864 | $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"images/blank_icon.gif\">"; |
254e0e4b AD |
865 | } |
866 | ||
9323147e AD |
867 | if ($rtl_content) { |
868 | $rtl_tag = "dir=\"rtl\""; | |
869 | } else { | |
870 | $rtl_tag = "dir=\"ltr\""; | |
871 | } | |
872 | ||
78d5212c AD |
873 | $error_notify_msg = ""; |
874 | ||
fb1fb4ab AD |
875 | if ($last_error) { |
876 | $link_title = "Error: $last_error ($last_updated)"; | |
78d5212c | 877 | $error_notify_msg = "(Error)"; |
ad780e9c | 878 | } else if ($last_updated) { |
fb1fb4ab AD |
879 | $link_title = "Updated: $last_updated"; |
880 | } | |
881 | ||
767e2486 | 882 | $feed = "<a title=\"$link_title\" id=\"FEEDL-$feed_id\" href=\"javascript:viewfeed('$feed_id', '', false);\">$feed_title</a>"; |
254e0e4b AD |
883 | |
884 | print "<li id=\"FEEDR-$feed_id\" class=\"$class\">"; | |
b619ff15 | 885 | if (get_pref($link, 'ENABLE_FEED_ICONS')) { |
254e0e4b AD |
886 | print "$feed_icon"; |
887 | } | |
888 | ||
9323147e | 889 | print "<span $rtl_tag id=\"FEEDN-$feed_id\">$feed</span>"; |
254e0e4b AD |
890 | |
891 | if ($unread != 0) { | |
892 | $fctr_class = ""; | |
893 | } else { | |
894 | $fctr_class = "class=\"invisible\""; | |
895 | } | |
896 | ||
9323147e | 897 | print " <span $rtl_tag $fctr_class id=\"FEEDCTR-$feed_id\"> |
254e0e4b | 898 | (<span id=\"FEEDU-$feed_id\">$unread</span>)</span>"; |
78d5212c AD |
899 | |
900 | if (get_pref($link, "EXTENDED_FEEDLIST")) { | |
901 | print "<div class=\"feedExtInfo\"> | |
902 | <span id=\"FLUPD-$feed_id\">$last_updated $error_notify_msg</span></div>"; | |
903 | } | |
904 | ||
254e0e4b AD |
905 | print "</li>"; |
906 | ||
907 | } | |
908 | ||
406d9489 AD |
909 | function getmicrotime() { |
910 | list($usec, $sec) = explode(" ",microtime()); | |
911 | return ((float)$usec + (float)$sec); | |
912 | } | |
913 | ||
77e96719 AD |
914 | function print_radio($id, $default, $values, $attributes = "") { |
915 | foreach ($values as $v) { | |
916 | ||
917 | if ($v == $default) | |
5da169d9 | 918 | $sel = "checked"; |
77e96719 | 919 | else |
5da169d9 AD |
920 | $sel = ""; |
921 | ||
922 | if ($v == "Yes") { | |
923 | $sel .= " value=\"1\""; | |
924 | } else { | |
925 | $sel .= " value=\"0\""; | |
926 | } | |
77e96719 | 927 | |
69654950 AD |
928 | print "<input class=\"noborder\" |
929 | type=\"radio\" $sel $attributes name=\"$id\"> $v "; | |
77e96719 AD |
930 | |
931 | } | |
932 | } | |
933 | ||
ff485f1d AD |
934 | function initialize_user_prefs($link, $uid) { |
935 | ||
936 | $uid = db_escape_string($uid); | |
937 | ||
938 | db_query($link, "BEGIN"); | |
939 | ||
940 | $result = db_query($link, "SELECT pref_name,def_value FROM ttrss_prefs"); | |
941 | ||
942 | $u_result = db_query($link, "SELECT pref_name | |
943 | FROM ttrss_user_prefs WHERE owner_uid = '$uid'"); | |
944 | ||
945 | $active_prefs = array(); | |
946 | ||
947 | while ($line = db_fetch_assoc($u_result)) { | |
948 | array_push($active_prefs, $line["pref_name"]); | |
949 | } | |
950 | ||
951 | while ($line = db_fetch_assoc($result)) { | |
952 | if (array_search($line["pref_name"], $active_prefs) === FALSE) { | |
953 | // print "adding " . $line["pref_name"] . "<br>"; | |
954 | ||
955 | db_query($link, "INSERT INTO ttrss_user_prefs | |
956 | (owner_uid,pref_name,value) VALUES | |
957 | ('$uid', '".$line["pref_name"]."','".$line["def_value"]."')"); | |
958 | ||
959 | } | |
960 | } | |
961 | ||
962 | db_query($link, "COMMIT"); | |
963 | ||
964 | } | |
956c7629 AD |
965 | |
966 | function lookup_user_id($link, $user) { | |
967 | ||
968 | $result = db_query($link, "SELECT id FROM ttrss_users WHERE | |
969 | login = '$login'"); | |
970 | ||
971 | if (db_num_rows($result) == 1) { | |
972 | return db_fetch_result($result, 0, "id"); | |
973 | } else { | |
974 | return false; | |
975 | } | |
976 | } | |
977 | ||
18664970 AD |
978 | function http_authenticate_user($link) { |
979 | ||
980 | if (!$_SERVER["PHP_AUTH_USER"]) { | |
981 | ||
982 | header('WWW-Authenticate: Basic realm="Tiny Tiny RSS RSSGen"'); | |
983 | header('HTTP/1.0 401 Unauthorized'); | |
984 | exit; | |
985 | ||
986 | } else { | |
987 | $auth_result = authenticate_user($link, | |
988 | $_SERVER["PHP_AUTH_USER"], $_SERVER["PHP_AUTH_PW"]); | |
989 | ||
990 | if (!$auth_result) { | |
991 | header('WWW-Authenticate: Basic realm="Tiny Tiny RSS RSSGen"'); | |
992 | header('HTTP/1.0 401 Unauthorized'); | |
993 | exit; | |
994 | } | |
995 | } | |
996 | ||
997 | return true; | |
998 | } | |
999 | ||
c8437f35 AD |
1000 | function authenticate_user($link, $login, $password) { |
1001 | ||
131b01b3 | 1002 | if (!SINGLE_USER_MODE) { |
c8437f35 | 1003 | |
131b01b3 AD |
1004 | $pwd_hash = 'SHA1:' . sha1($password); |
1005 | ||
1006 | $result = db_query($link, "SELECT id,login,access_level FROM ttrss_users WHERE | |
1007 | login = '$login' AND pwd_hash = '$pwd_hash'"); | |
1008 | ||
1009 | if (db_num_rows($result) == 1) { | |
1010 | $_SESSION["uid"] = db_fetch_result($result, 0, "id"); | |
1011 | $_SESSION["name"] = db_fetch_result($result, 0, "login"); | |
1012 | $_SESSION["access_level"] = db_fetch_result($result, 0, "access_level"); | |
1013 | ||
1014 | db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " . | |
1015 | $_SESSION["uid"]); | |
1016 | ||
1017 | $user_theme = get_user_theme_path($link); | |
1018 | ||
1019 | $_SESSION["theme"] = $user_theme; | |
1020 | $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"]; | |
1021 | ||
1022 | initialize_user_prefs($link, $_SESSION["uid"]); | |
1023 | ||
1024 | return true; | |
1025 | } | |
1026 | ||
1027 | return false; | |
503eb349 | 1028 | |
131b01b3 | 1029 | } else { |
503eb349 | 1030 | |
131b01b3 AD |
1031 | $_SESSION["uid"] = 1; |
1032 | $_SESSION["name"] = "admin"; | |
f557cd78 | 1033 | |
0bbba72d AD |
1034 | $user_theme = get_user_theme_path($link); |
1035 | ||
1036 | $_SESSION["theme"] = $user_theme; | |
1037 | $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"]; | |
1038 | ||
1039 | initialize_user_prefs($link, $_SESSION["uid"]); | |
1040 | ||
c8437f35 AD |
1041 | return true; |
1042 | } | |
c8437f35 AD |
1043 | } |
1044 | ||
e6cb77a0 AD |
1045 | function make_password($length = 8) { |
1046 | ||
1047 | $password = ""; | |
798f722b AD |
1048 | $possible = "0123456789abcdfghjkmnpqrstvwxyzABCDFGHJKMNPQRSTVWXYZ"; |
1049 | ||
1050 | $i = 0; | |
e6cb77a0 AD |
1051 | |
1052 | while ($i < $length) { | |
1053 | $char = substr($possible, mt_rand(0, strlen($possible)-1), 1); | |
1054 | ||
1055 | if (!strstr($password, $char)) { | |
1056 | $password .= $char; | |
1057 | $i++; | |
1058 | } | |
1059 | } | |
1060 | return $password; | |
1061 | } | |
1062 | ||
1063 | // this is called after user is created to initialize default feeds, labels | |
1064 | // or whatever else | |
1065 | ||
1066 | // user preferences are checked on every login, not here | |
1067 | ||
1068 | function initialize_user($link, $uid) { | |
1069 | ||
1070 | db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description) | |
1071 | values ('$uid','unread = true', 'Unread articles')"); | |
1072 | ||
1073 | db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description) | |
1074 | values ('$uid','last_read is null and unread = false', 'Updated articles')"); | |
1075 | ||
1076 | db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url) | |
74bff337 | 1077 | values ('$uid', 'Tiny Tiny RSS: New Releases', |
628fcd2c | 1078 | 'http://tt-rss.spb.ru/releases.rss')"); |
3b0feb9b AD |
1079 | |
1080 | } | |
e6cb77a0 | 1081 | |
b8aa49bc | 1082 | function logout_user() { |
5ccc1cf5 AD |
1083 | session_destroy(); |
1084 | if (isset($_COOKIE[session_name()])) { | |
1085 | setcookie(session_name(), '', time()-42000, '/'); | |
1086 | } | |
b8aa49bc AD |
1087 | } |
1088 | ||
75836f33 | 1089 | function get_script_urlpath() { |
87a79fa4 | 1090 | return preg_replace('/\/[^\/]*$/', "", $_SERVER["REQUEST_URI"]); |
75836f33 AD |
1091 | } |
1092 | ||
1093 | function get_login_redirect() { | |
1094 | $server = $_SERVER["SERVER_NAME"]; | |
1095 | ||
1096 | if (ENABLE_LOGIN_SSL) { | |
1097 | $protocol = "https"; | |
1098 | } else { | |
1099 | $protocol = "http"; | |
1100 | } | |
1101 | ||
1102 | $url_path = get_script_urlpath(); | |
1103 | ||
1104 | $redirect_uri = "$protocol://$server$url_path/login.php"; | |
1105 | ||
1106 | return $redirect_uri; | |
1107 | } | |
1108 | ||
916f788a | 1109 | function validate_session($link) { |
a2e9b457 | 1110 | if (SESSION_CHECK_ADDRESS && $_SESSION["uid"]) { |
916f788a AD |
1111 | if ($_SESSION["ip_address"]) { |
1112 | if ($_SESSION["ip_address"] != $_SERVER["REMOTE_ADDR"]) { | |
1113 | return false; | |
1114 | } | |
1115 | } | |
1116 | } | |
1117 | return true; | |
1118 | } | |
1119 | ||
7ae65adf AD |
1120 | function basic_nosid_redirect_check() { |
1121 | if (!SINGLE_USER_MODE) { | |
3dd46f19 | 1122 | if (!$_COOKIE[get_session_cookie_name()]) { |
7ae65adf AD |
1123 | $redirect_uri = get_login_redirect(); |
1124 | $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]); | |
1125 | header("Location: $redirect_uri?rt=$return_to"); | |
1126 | exit; | |
1127 | } | |
1128 | } | |
1129 | } | |
1130 | ||
b8aa49bc AD |
1131 | function login_sequence($link) { |
1132 | if (!SINGLE_USER_MODE) { | |
75836f33 | 1133 | |
916f788a AD |
1134 | if (!validate_session($link)) { |
1135 | logout_user(); | |
1136 | $redirect_uri = get_login_redirect(); | |
1137 | $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]); | |
1138 | header("Location: $redirect_uri?rt=$return_to"); | |
1139 | exit; | |
1140 | } | |
1141 | ||
b8aa49bc AD |
1142 | if (!USE_HTTP_AUTH) { |
1143 | if (!$_SESSION["uid"]) { | |
75836f33 | 1144 | $redirect_uri = get_login_redirect(); |
e31dca14 AD |
1145 | $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]); |
1146 | header("Location: $redirect_uri?rt=$return_to"); | |
b8aa49bc AD |
1147 | exit; |
1148 | } | |
1149 | } else { | |
f557cd78 AD |
1150 | if (!$_SESSION["uid"]) { |
1151 | if (!$_SERVER["PHP_AUTH_USER"]) { | |
1152 | ||
1153 | header('WWW-Authenticate: Basic realm="Tiny Tiny RSS"'); | |
1154 | header('HTTP/1.0 401 Unauthorized'); | |
1155 | exit; | |
1156 | ||
1157 | } else { | |
1158 | $auth_result = authenticate_user($link, | |
1159 | $_SERVER["PHP_AUTH_USER"], $_SERVER["PHP_AUTH_PW"]); | |
1160 | ||
1161 | if (!$auth_result) { | |
1162 | header('WWW-Authenticate: Basic realm="Tiny Tiny RSS"'); | |
1163 | header('HTTP/1.0 401 Unauthorized'); | |
1164 | exit; | |
1165 | } | |
1166 | } | |
1167 | } | |
b8aa49bc AD |
1168 | } |
1169 | } else { | |
0bbba72d | 1170 | return authenticate_user($link, "admin", null); |
b8aa49bc AD |
1171 | } |
1172 | } | |
3547842a AD |
1173 | |
1174 | function truncate_string($str, $max_len) { | |
12db369c AD |
1175 | if (mb_strlen($str, "utf-8") > $max_len - 3) { |
1176 | return mb_substr($str, 0, $max_len, "utf-8") . "..."; | |
3547842a AD |
1177 | } else { |
1178 | return $str; | |
1179 | } | |
1180 | } | |
54a60e1a AD |
1181 | |
1182 | function get_user_theme_path($link) { | |
798f722b AD |
1183 | $result = db_query($link, "SELECT theme_path |
1184 | FROM | |
1185 | ttrss_themes,ttrss_users | |
1186 | WHERE ttrss_themes.id = theme_id AND ttrss_users.id = " . $_SESSION["uid"]); | |
54a60e1a AD |
1187 | if (db_num_rows($result) != 0) { |
1188 | return db_fetch_result($result, 0, "theme_path"); | |
1189 | } else { | |
1190 | return null; | |
1191 | } | |
1192 | } | |
be773442 AD |
1193 | |
1194 | function smart_date_time($timestamp) { | |
1195 | if (date("Y.m.d", $timestamp) == date("Y.m.d")) { | |
1196 | return date("G:i", $timestamp); | |
f26450f1 | 1197 | } else if (date("Y", $timestamp) == date("Y")) { |
be773442 AD |
1198 | return date("M d, G:i", $timestamp); |
1199 | } else { | |
b02111c2 | 1200 | return date("Y/m/d G:i", $timestamp); |
be773442 AD |
1201 | } |
1202 | } | |
1203 | ||
1204 | function smart_date($timestamp) { | |
1205 | if (date("Y.m.d", $timestamp) == date("Y.m.d")) { | |
1206 | return "Today"; | |
f26450f1 | 1207 | } else if (date("Y", $timestamp) == date("Y")) { |
be773442 AD |
1208 | return date("D m", $timestamp); |
1209 | } else { | |
b02111c2 | 1210 | return date("Y/m/d", $timestamp); |
be773442 AD |
1211 | } |
1212 | } | |
a654a595 AD |
1213 | |
1214 | function sql_bool_to_string($s) { | |
1215 | if ($s == "t" || $s == "1") { | |
1216 | return "true"; | |
1217 | } else { | |
1218 | return "false"; | |
1219 | } | |
1220 | } | |
e3c99f3b AD |
1221 | |
1222 | function sql_bool_to_bool($s) { | |
1223 | if ($s == "t" || $s == "1") { | |
1224 | return true; | |
1225 | } else { | |
1226 | return false; | |
1227 | } | |
1228 | } | |
0ea4fb50 | 1229 | |
e3c99f3b | 1230 | |
0ea4fb50 AD |
1231 | function toggleEvenOdd($a) { |
1232 | if ($a == "even") | |
1233 | return "odd"; | |
1234 | else | |
1235 | return "even"; | |
1236 | } | |
6043fb7e AD |
1237 | |
1238 | function sanity_check($link) { | |
9cbca41f | 1239 | |
aec3ce39 AD |
1240 | error_reporting(0); |
1241 | ||
6043fb7e AD |
1242 | $error_code = 0; |
1243 | $result = db_query($link, "SELECT schema_version FROM ttrss_version"); | |
1244 | $schema_version = db_fetch_result($result, 0, "schema_version"); | |
1245 | ||
1246 | if ($schema_version != SCHEMA_VERSION) { | |
1247 | $error_code = 5; | |
1248 | } | |
1249 | ||
aec3ce39 AD |
1250 | if (DB_TYPE == "mysql") { |
1251 | $result = db_query($link, "SELECT true", false); | |
1252 | if (db_num_rows($result) != 1) { | |
1253 | $error_code = 10; | |
1254 | } | |
1255 | } | |
1256 | ||
1257 | error_reporting (DEFAULT_ERROR_LEVEL); | |
1258 | ||
6043fb7e | 1259 | if ($error_code != 0) { |
aec3ce39 | 1260 | print_error_xml($error_code); |
6043fb7e AD |
1261 | return false; |
1262 | } else { | |
1263 | return true; | |
9cbca41f | 1264 | } |
6043fb7e AD |
1265 | } |
1266 | ||
27981ca3 AD |
1267 | function file_is_locked($filename) { |
1268 | error_reporting(0); | |
1269 | $fp = fopen($filename, "r"); | |
1270 | error_reporting(DEFAULT_ERROR_LEVEL); | |
1271 | if ($fp) { | |
1272 | if (flock($fp, LOCK_EX | LOCK_NB)) { | |
1273 | flock($fp, LOCK_UN); | |
1274 | fclose($fp); | |
1275 | return false; | |
1276 | } | |
1277 | fclose($fp); | |
1278 | return true; | |
1279 | } | |
1280 | return false; | |
1281 | } | |
1282 | ||
fcb4c0c9 AD |
1283 | function make_lockfile($filename) { |
1284 | $fp = fopen($filename, "w"); | |
1285 | ||
1286 | if (flock($fp, LOCK_EX | LOCK_NB)) { | |
1287 | return $fp; | |
1288 | } else { | |
1289 | return false; | |
1290 | } | |
1291 | } | |
1292 | ||
894ebcf5 AD |
1293 | function sql_random_function() { |
1294 | if (DB_TYPE == "mysql") { | |
1295 | return "RAND()"; | |
1296 | } else { | |
1297 | return "RANDOM()"; | |
1298 | } | |
1299 | } | |
1300 | ||
23aa0d16 | 1301 | function catchup_feed($link, $feed, $cat_view) { |
88040f57 AD |
1302 | |
1303 | if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) { | |
23aa0d16 AD |
1304 | |
1305 | if ($cat_view) { | |
1306 | ||
1307 | if ($feed > 0) { | |
1308 | $cat_qpart = "cat_id = '$feed'"; | |
1309 | } else { | |
1310 | $cat_qpart = "cat_id IS NULL"; | |
1311 | } | |
1312 | ||
1313 | $tmp_result = db_query($link, "SELECT id | |
1314 | FROM ttrss_feeds WHERE $cat_qpart AND owner_uid = " . | |
1315 | $_SESSION["uid"]); | |
1316 | ||
1317 | while ($tmp_line = db_fetch_assoc($tmp_result)) { | |
1318 | ||
1319 | $tmp_feed = $tmp_line["id"]; | |
1320 | ||
1321 | db_query($link, "UPDATE ttrss_user_entries | |
1322 | SET unread = false,last_read = NOW() | |
1323 | WHERE feed_id = '$tmp_feed' AND owner_uid = " . $_SESSION["uid"]); | |
1324 | } | |
1325 | ||
1326 | } else if ($feed > 0) { | |
1327 | ||
1328 | $tmp_result = db_query($link, "SELECT id | |
1329 | FROM ttrss_feeds WHERE parent_feed = '$feed' | |
1330 | ORDER BY cat_id,title"); | |
1331 | ||
1332 | $parent_ids = array(); | |
1333 | ||
1334 | if (db_num_rows($tmp_result) > 0) { | |
1335 | while ($p = db_fetch_assoc($tmp_result)) { | |
1336 | array_push($parent_ids, "feed_id = " . $p["id"]); | |
1337 | } | |
1338 | ||
1339 | $children_qpart = implode(" OR ", $parent_ids); | |
1340 | ||
1341 | db_query($link, "UPDATE ttrss_user_entries | |
1342 | SET unread = false,last_read = NOW() | |
1343 | WHERE (feed_id = '$feed' OR $children_qpart) | |
1344 | AND owner_uid = " . $_SESSION["uid"]); | |
1345 | ||
1346 | } else { | |
1347 | db_query($link, "UPDATE ttrss_user_entries | |
1348 | SET unread = false,last_read = NOW() | |
1349 | WHERE feed_id = '$feed' AND owner_uid = " . $_SESSION["uid"]); | |
1350 | } | |
1351 | ||
1352 | } else if ($feed < 0 && $feed > -10) { // special, like starred | |
1353 | ||
1354 | if ($feed == -1) { | |
1355 | db_query($link, "UPDATE ttrss_user_entries | |
1356 | SET unread = false,last_read = NOW() | |
1357 | WHERE marked = true AND owner_uid = ".$_SESSION["uid"]); | |
1358 | } | |
1359 | ||
1360 | } else if ($feed < -10) { // label | |
1361 | ||
1362 | // TODO make this more efficient | |
1363 | ||
1364 | $label_id = -$feed - 11; | |
1365 | ||
1366 | $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels | |
1367 | WHERE id = '$label_id'"); | |
1368 | ||
1369 | if ($tmp_result) { | |
1370 | $sql_exp = db_fetch_result($tmp_result, 0, "sql_exp"); | |
1371 | ||
1372 | db_query($link, "BEGIN"); | |
1373 | ||
1374 | $tmp2_result = db_query($link, | |
1375 | "SELECT | |
1376 | int_id | |
1377 | FROM | |
88040f57 | 1378 | ttrss_user_entries,ttrss_entries,ttrss_feeds |
23aa0d16 | 1379 | WHERE |
88040f57 AD |
1380 | ref_id = ttrss_entries.id AND |
1381 | ttrss_user_entries.feed_id = ttrss_feeds.id AND | |
23aa0d16 | 1382 | $sql_exp AND |
88040f57 | 1383 | ttrss_user_entries.owner_uid = " . $_SESSION["uid"]); |
23aa0d16 AD |
1384 | |
1385 | while ($tmp_line = db_fetch_assoc($tmp2_result)) { | |
1386 | db_query($link, "UPDATE | |
1387 | ttrss_user_entries | |
1388 | SET | |
1389 | unread = false, last_read = NOW() | |
1390 | WHERE | |
1391 | int_id = " . $tmp_line["int_id"]); | |
1392 | } | |
1393 | ||
1394 | db_query($link, "COMMIT"); | |
1395 | ||
1396 | /* db_query($link, "UPDATE ttrss_user_entries,ttrss_entries | |
1397 | SET unread = false,last_read = NOW() | |
1398 | WHERE $sql_exp | |
1399 | AND ref_id = id | |
1400 | AND owner_uid = ".$_SESSION["uid"]); */ | |
1401 | } | |
1402 | } | |
1403 | } else { // tag | |
1404 | db_query($link, "BEGIN"); | |
1405 | ||
1406 | $tag_name = db_escape_string($feed); | |
1407 | ||
1408 | $result = db_query($link, "SELECT post_int_id FROM ttrss_tags | |
1409 | WHERE tag_name = '$tag_name' AND owner_uid = " . $_SESSION["uid"]); | |
1410 | ||
1411 | while ($line = db_fetch_assoc($result)) { | |
1412 | db_query($link, "UPDATE ttrss_user_entries SET | |
1413 | unread = false, last_read = NOW() | |
1414 | WHERE int_id = " . $line["post_int_id"]); | |
1415 | } | |
1416 | db_query($link, "COMMIT"); | |
1417 | } | |
1418 | } | |
1419 | ||
1420 | function update_generic_feed($link, $feed, $cat_view) { | |
1421 | if ($cat_view) { | |
1422 | ||
1423 | if ($feed > 0) { | |
1424 | $cat_qpart = "cat_id = '$feed'"; | |
1425 | } else { | |
1426 | $cat_qpart = "cat_id IS NULL"; | |
1427 | } | |
1428 | ||
1429 | $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds | |
1430 | WHERE $cat_qpart AND owner_uid = " . $_SESSION["uid"]); | |
1431 | ||
1432 | while ($tmp_line = db_fetch_assoc($tmp_result)) { | |
1433 | $feed_url = $tmp_line["feed_url"]; | |
1434 | update_rss_feed($link, $feed_url, $feed, ENABLE_UPDATE_DAEMON); | |
1435 | } | |
1436 | ||
1437 | } else { | |
1438 | $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds | |
1439 | WHERE id = '$feed'"); | |
1440 | $feed_url = db_fetch_result($tmp_result, 0, "feed_url"); | |
1441 | update_rss_feed($link, $feed_url, $feed, ENABLE_UPDATE_DAEMON); | |
1442 | } | |
1443 | } | |
a9cb1f83 AD |
1444 | |
1445 | function getAllCounters($link) { | |
1446 | getLabelCounters($link); | |
1447 | getFeedCounters($link); | |
1448 | getTagCounters($link); | |
1449 | getGlobalCounters($link); | |
1450 | if (get_pref($link, 'ENABLE_FEED_CATS')) { | |
1451 | getCategoryCounters($link); | |
1452 | } | |
1453 | } | |
1454 | ||
1455 | function getCategoryCounters($link) { | |
1456 | $result = db_query($link, "SELECT cat_id,SUM((SELECT COUNT(int_id) | |
1457 | FROM ttrss_user_entries WHERE feed_id = ttrss_feeds.id | |
1458 | AND unread = true)) AS unread FROM ttrss_feeds | |
1459 | WHERE | |
cfb02131 | 1460 | hidden = false AND owner_uid = ".$_SESSION["uid"]." GROUP BY cat_id"); |
a9cb1f83 AD |
1461 | |
1462 | while ($line = db_fetch_assoc($result)) { | |
1463 | $line["cat_id"] = sprintf("%d", $line["cat_id"]); | |
1464 | print "<counter type=\"category\" id=\"".$line["cat_id"]."\" counter=\"". | |
1465 | $line["unread"]."\"/>"; | |
1466 | } | |
1467 | } | |
1468 | ||
f295c368 AD |
1469 | function getCategoryUnread($link, $cat) { |
1470 | ||
18664970 AD |
1471 | if ($cat != 0) { |
1472 | $cat_query = "cat_id = '$cat'"; | |
1473 | } else { | |
1474 | $cat_query = "cat_id IS NULL"; | |
1475 | } | |
1476 | ||
1477 | $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE $cat_query | |
cfb02131 | 1478 | AND hidden = false |
f295c368 AD |
1479 | AND owner_uid = " . $_SESSION["uid"]); |
1480 | ||
1481 | $cat_feeds = array(); | |
1482 | while ($line = db_fetch_assoc($result)) { | |
1483 | array_push($cat_feeds, "feed_id = " . $line["id"]); | |
1484 | } | |
1485 | ||
18664970 AD |
1486 | if (count($cat_feeds) == 0) return 0; |
1487 | ||
f295c368 AD |
1488 | $match_part = implode(" OR ", $cat_feeds); |
1489 | ||
1490 | $result = db_query($link, "SELECT COUNT(int_id) AS unread | |
1491 | FROM ttrss_user_entries | |
4919fb42 | 1492 | WHERE unread = true AND ($match_part) AND owner_uid = " . $_SESSION["uid"]); |
f295c368 AD |
1493 | |
1494 | $unread = 0; | |
1495 | ||
1496 | # this needs to be rewritten | |
1497 | while ($line = db_fetch_assoc($result)) { | |
1498 | $unread += $line["unread"]; | |
1499 | } | |
1500 | ||
1501 | return $unread; | |
1502 | ||
1503 | } | |
1504 | ||
1505 | function getFeedUnread($link, $feed, $is_cat = false) { | |
a9cb1f83 | 1506 | $n_feed = sprintf("%d", $feed); |
f295c368 AD |
1507 | |
1508 | if ($is_cat) { | |
831ff047 | 1509 | return getCategoryUnread($link, $n_feed); |
f295c368 | 1510 | } else if ($n_feed == -1) { |
a9cb1f83 | 1511 | $match_part = "marked = true"; |
4919fb42 | 1512 | } else if ($n_feed > 0) { |
831ff047 | 1513 | |
4919fb42 | 1514 | $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE parent_feed = '$n_feed' |
318260cc | 1515 | AND hidden = false |
4919fb42 | 1516 | AND owner_uid = " . $_SESSION["uid"]); |
831ff047 AD |
1517 | |
1518 | if (db_num_rows($result) > 0) { | |
4919fb42 | 1519 | |
831ff047 AD |
1520 | $linked_feeds = array(); |
1521 | while ($line = db_fetch_assoc($result)) { | |
1522 | array_push($linked_feeds, "feed_id = " . $line["id"]); | |
1523 | } | |
1524 | ||
1525 | $match_part = implode(" OR ", $linked_feeds); | |
1526 | ||
4919fb42 | 1527 | $result = db_query($link, "SELECT COUNT(int_id) AS unread |
318260cc | 1528 | FROM ttrss_user_entries |
4919fb42 AD |
1529 | WHERE unread = true AND ($match_part) AND owner_uid = " . $_SESSION["uid"]); |
1530 | ||
1531 | $unread = 0; | |
1532 | ||
1533 | # this needs to be rewritten | |
1534 | while ($line = db_fetch_assoc($result)) { | |
1535 | $unread += $line["unread"]; | |
1536 | } | |
1537 | ||
1538 | return $unread; | |
1539 | ||
831ff047 AD |
1540 | } else { |
1541 | $match_part = "feed_id = '$n_feed'"; | |
1542 | } | |
a9cb1f83 | 1543 | } else if ($feed < -10) { |
318260cc | 1544 | |
a9cb1f83 AD |
1545 | $label_id = -$feed - 11; |
1546 | ||
1547 | $result = db_query($link, "SELECT sql_exp FROM ttrss_labels WHERE | |
1548 | id = '$label_id' AND owner_uid = " . $_SESSION["uid"]); | |
1549 | ||
1550 | $match_part = db_fetch_result($result, 0, "sql_exp"); | |
1551 | } | |
1552 | ||
1553 | if ($match_part) { | |
1554 | ||
1555 | $result = db_query($link, "SELECT count(int_id) AS unread | |
88040f57 AD |
1556 | FROM ttrss_user_entries,ttrss_feeds,ttrss_entries WHERE |
1557 | ttrss_user_entries.feed_id = ttrss_feeds.id AND | |
1558 | ttrss_user_entries.ref_id = ttrss_entries.id AND | |
cfb02131 | 1559 | ttrss_feeds.hidden = false AND |
88040f57 | 1560 | unread = true AND ($match_part) AND ttrss_user_entries.owner_uid = " . $_SESSION["uid"]); |
a9cb1f83 AD |
1561 | |
1562 | } else { | |
1563 | ||
1564 | $result = db_query($link, "SELECT COUNT(post_int_id) AS unread | |
1565 | FROM ttrss_tags,ttrss_user_entries | |
1566 | WHERE tag_name = '$feed' AND post_int_id = int_id AND unread = true AND | |
1567 | ttrss_tags.owner_uid = " . $_SESSION["uid"]); | |
1568 | } | |
1569 | ||
1570 | $unread = db_fetch_result($result, 0, "unread"); | |
cfb02131 | 1571 | |
a9cb1f83 AD |
1572 | return $unread; |
1573 | } | |
1574 | ||
1575 | /* FIXME this needs reworking */ | |
1576 | ||
f3acc32e AD |
1577 | function getGlobalUnread($link, $user_id = false) { |
1578 | ||
1579 | if (!$user_id) { | |
1580 | $user_id = $_SESSION["uid"]; | |
1581 | } | |
1582 | ||
3831db41 | 1583 | $result = db_query($link, "SELECT count(ttrss_entries.id) as c_id FROM ttrss_entries,ttrss_user_entries,ttrss_feeds |
a9cb1f83 | 1584 | WHERE unread = true AND |
3831db41 | 1585 | ttrss_user_entries.feed_id = ttrss_feeds.id AND |
a9cb1f83 | 1586 | ttrss_user_entries.ref_id = ttrss_entries.id AND |
3831db41 | 1587 | hidden = false AND |
f3acc32e | 1588 | ttrss_user_entries.owner_uid = '$user_id'"); |
a9cb1f83 AD |
1589 | $c_id = db_fetch_result($result, 0, "c_id"); |
1590 | return $c_id; | |
1591 | } | |
1592 | ||
1593 | function getGlobalCounters($link, $global_unread = -1) { | |
1594 | if ($global_unread == -1) { | |
1595 | $global_unread = getGlobalUnread($link); | |
1596 | } | |
7bf7e4d3 AD |
1597 | print "<counter type=\"global\" id='global-unread' |
1598 | counter='$global_unread'/>"; | |
1599 | ||
1600 | $result = db_query($link, "SELECT COUNT(id) AS fn FROM | |
1601 | ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]); | |
1602 | ||
1603 | $subscribed_feeds = db_fetch_result($result, 0, "fn"); | |
1604 | ||
1605 | print "<counter type=\"global\" id='subscribed-feeds' | |
1606 | counter='$subscribed_feeds'/>"; | |
1607 | ||
a9cb1f83 AD |
1608 | } |
1609 | ||
1610 | function getTagCounters($link, $smart_mode = SMART_RPC_COUNTERS) { | |
1611 | ||
1612 | if ($smart_mode) { | |
1613 | if (!$_SESSION["tctr_last_value"]) { | |
1614 | $_SESSION["tctr_last_value"] = array(); | |
1615 | } | |
1616 | } | |
1617 | ||
1618 | $old_counters = $_SESSION["tctr_last_value"]; | |
1619 | ||
1620 | $tctrs_modified = false; | |
1621 | ||
1622 | /* $result = db_query($link, "SELECT tag_name,count(ttrss_entries.id) AS count | |
1623 | FROM ttrss_tags,ttrss_entries,ttrss_user_entries WHERE | |
1624 | ttrss_user_entries.ref_id = ttrss_entries.id AND | |
1625 | ttrss_tags.owner_uid = ".$_SESSION["uid"]." AND | |
1626 | post_int_id = ttrss_user_entries.int_id AND unread = true GROUP BY tag_name | |
1627 | UNION | |
1628 | select tag_name,0 as count FROM ttrss_tags | |
1629 | WHERE ttrss_tags.owner_uid = ".$_SESSION["uid"]); */ | |
1630 | ||
1631 | $result = db_query($link, "SELECT tag_name,SUM((SELECT COUNT(int_id) | |
1632 | FROM ttrss_user_entries WHERE int_id = post_int_id | |
1633 | AND unread = true)) AS count FROM ttrss_tags | |
1634 | WHERE owner_uid = 2 GROUP BY tag_name ORDER BY tag_name"); | |
1635 | ||
1636 | $tags = array(); | |
1637 | ||
1638 | while ($line = db_fetch_assoc($result)) { | |
1639 | $tags[$line["tag_name"]] += $line["count"]; | |
1640 | } | |
1641 | ||
1642 | foreach (array_keys($tags) as $tag) { | |
1643 | $unread = $tags[$tag]; | |
1644 | ||
1645 | $tag = htmlspecialchars($tag); | |
1646 | ||
1647 | if (!$smart_mode || $old_counters[$tag] != $unread) { | |
1648 | $old_counters[$tag] = $unread; | |
1649 | $tctrs_modified = true; | |
1650 | print "<counter type=\"tag\" id=\"$tag\" counter=\"$unread\"/>"; | |
1651 | } | |
1652 | ||
1653 | } | |
1654 | ||
1655 | if ($smart_mode && $tctrs_modified) { | |
1656 | $_SESSION["tctr_last_value"] = $old_counters; | |
1657 | } | |
1658 | ||
1659 | } | |
1660 | ||
ef393de7 | 1661 | function getLabelCounters($link, $smart_mode = SMART_RPC_COUNTERS, $ret_mode = false) { |
a9cb1f83 AD |
1662 | |
1663 | if ($smart_mode) { | |
1664 | if (!$_SESSION["lctr_last_value"]) { | |
1665 | $_SESSION["lctr_last_value"] = array(); | |
1666 | } | |
1667 | } | |
1668 | ||
ef393de7 AD |
1669 | $ret_arr = array(); |
1670 | ||
a9cb1f83 AD |
1671 | $old_counters = $_SESSION["lctr_last_value"]; |
1672 | $lctrs_modified = false; | |
1673 | ||
88040f57 | 1674 | $result = db_query($link, "SELECT count(ttrss_entries.id) as count FROM ttrss_entries,ttrss_user_entries,ttrss_feeds |
a9cb1f83 | 1675 | WHERE marked = true AND ttrss_user_entries.ref_id = ttrss_entries.id AND |
88040f57 AD |
1676 | ttrss_user_entries.feed_id = ttrss_feeds.id AND |
1677 | unread = true AND ttrss_user_entries.owner_uid = ".$_SESSION["uid"]); | |
a9cb1f83 AD |
1678 | |
1679 | $count = db_fetch_result($result, 0, "count"); | |
1680 | ||
ef393de7 AD |
1681 | if (!$ret_mode) { |
1682 | print "<counter type=\"label\" id=\"-1\" counter=\"$count\"/>"; | |
1683 | } else { | |
1684 | $ret_arr["-1"]["counter"] = $count; | |
1685 | $ret_arr["-1"]["description"] = "Starred"; | |
1686 | } | |
a9cb1f83 AD |
1687 | |
1688 | $result = db_query($link, "SELECT owner_uid,id,sql_exp,description FROM | |
1689 | ttrss_labels WHERE owner_uid = ".$_SESSION["uid"]." ORDER by description"); | |
1690 | ||
1691 | while ($line = db_fetch_assoc($result)) { | |
1692 | ||
1693 | $id = -$line["id"] - 11; | |
1694 | ||
ef393de7 AD |
1695 | $label_name = $line["description"]; |
1696 | ||
a9cb1f83 AD |
1697 | error_reporting (0); |
1698 | ||
88040f57 | 1699 | $tmp_result = db_query($link, "SELECT count(ttrss_entries.id) as count FROM ttrss_user_entries,ttrss_entries,ttrss_feeds |
a9cb1f83 | 1700 | WHERE (" . $line["sql_exp"] . ") AND unread = true AND |
cfb02131 | 1701 | ttrss_feeds.hidden = false AND |
88040f57 | 1702 | ttrss_user_entries.feed_id = ttrss_feeds.id AND |
a9cb1f83 | 1703 | ttrss_user_entries.ref_id = ttrss_entries.id AND |
88040f57 | 1704 | ttrss_user_entries.owner_uid = ".$_SESSION["uid"]); |
a9cb1f83 AD |
1705 | |
1706 | $count = db_fetch_result($tmp_result, 0, "count"); | |
1707 | ||
1708 | if (!$smart_mode || $old_counters[$id] != $count) { | |
1709 | $old_counters[$id] = $count; | |
1710 | $lctrs_modified = true; | |
ef393de7 AD |
1711 | if (!$ret_mode) { |
1712 | print "<counter type=\"label\" id=\"$id\" counter=\"$count\"/>"; | |
1713 | } else { | |
1714 | $ret_arr[$id]["counter"] = $count; | |
1715 | $ret_arr[$id]["description"] = $label_name; | |
1716 | } | |
a9cb1f83 AD |
1717 | } |
1718 | ||
1719 | error_reporting (DEFAULT_ERROR_LEVEL); | |
1720 | } | |
1721 | ||
1722 | if ($smart_mode && $lctrs_modified) { | |
1723 | $_SESSION["lctr_last_value"] = $old_counters; | |
1724 | } | |
ef393de7 AD |
1725 | |
1726 | return $ret_arr; | |
a9cb1f83 AD |
1727 | } |
1728 | ||
1729 | /* function getFeedCounter($link, $id) { | |
1730 | ||
1731 | $result = db_query($link, "SELECT | |
1732 | count(id) as count,last_error | |
1733 | FROM ttrss_entries,ttrss_user_entries,ttrss_feeds | |
1734 | WHERE feed_id = '$id' AND unread = true | |
1735 | AND ttrss_user_entries.feed_id = ttrss_feeds.id | |
1736 | AND ttrss_user_entries.ref_id = ttrss_entries.id"); | |
1737 | ||
1738 | $count = db_fetch_result($result, 0, "count"); | |
1739 | $last_error = htmlspecialchars(db_fetch_result($result, 0, "last_error")); | |
1740 | ||
1741 | print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" error=\"$last_error\"/>"; | |
1742 | } */ | |
1743 | ||
1744 | function getFeedCounters($link, $smart_mode = SMART_RPC_COUNTERS) { | |
1745 | ||
1746 | if ($smart_mode) { | |
1747 | if (!$_SESSION["fctr_last_value"]) { | |
1748 | $_SESSION["fctr_last_value"] = array(); | |
1749 | } | |
1750 | } | |
1751 | ||
1752 | $old_counters = $_SESSION["fctr_last_value"]; | |
1753 | ||
1754 | $result = db_query($link, "SELECT id,last_error,parent_feed, | |
fb1fb4ab | 1755 | SUBSTRING(last_updated,1,19) AS last_updated, |
a9cb1f83 AD |
1756 | (SELECT count(id) |
1757 | FROM ttrss_entries,ttrss_user_entries | |
1758 | WHERE feed_id = ttrss_feeds.id AND | |
1759 | ttrss_user_entries.ref_id = ttrss_entries.id | |
1760 | AND unread = true AND owner_uid = ".$_SESSION["uid"].") as count | |
1761 | FROM ttrss_feeds WHERE owner_uid = ".$_SESSION["uid"] . " | |
1762 | AND parent_feed IS NULL"); | |
1763 | ||
1764 | $fctrs_modified = false; | |
1765 | ||
fb1fb4ab AD |
1766 | $short_date = get_pref($link, 'SHORT_DATE_FORMAT'); |
1767 | ||
a9cb1f83 AD |
1768 | while ($line = db_fetch_assoc($result)) { |
1769 | ||
1770 | $id = $line["id"]; | |
1771 | $count = $line["count"]; | |
1772 | $last_error = htmlspecialchars($line["last_error"]); | |
fb1fb4ab AD |
1773 | |
1774 | if (get_pref($link, 'HEADLINES_SMART_DATE')) { | |
1775 | $last_updated = smart_date_time(strtotime($line["last_updated"])); | |
1776 | } else { | |
1777 | $last_updated = date($short_date, strtotime($line["last_updated"])); | |
1778 | } | |
1779 | ||
a9cb1f83 AD |
1780 | $has_img = is_file(ICONS_DIR . "/$id.ico"); |
1781 | ||
1782 | $tmp_result = db_query($link, | |
1783 | "SELECT id,COUNT(unread) AS unread | |
1784 | FROM ttrss_feeds LEFT JOIN ttrss_user_entries | |
1785 | ON (ttrss_feeds.id = ttrss_user_entries.feed_id) | |
1786 | WHERE parent_feed = '$id' AND unread = true GROUP BY ttrss_feeds.id"); | |
1787 | ||
1788 | if (db_num_rows($tmp_result) > 0) { | |
1789 | while ($l = db_fetch_assoc($tmp_result)) { | |
1790 | $count += $l["unread"]; | |
1791 | } | |
1792 | } | |
1793 | ||
1794 | if (!$smart_mode || $old_counters[$id] != $count) { | |
1795 | $old_counters[$id] = $count; | |
1796 | $fctrs_modified = true; | |
1797 | ||
1798 | if ($last_error) { | |
1799 | $error_part = "error=\"$last_error\""; | |
1800 | } else { | |
1801 | $error_part = ""; | |
1802 | } | |
1803 | ||
1804 | if ($has_img) { | |
1805 | $has_img_part = "hi=\"$has_img\""; | |
1806 | } else { | |
1807 | $has_img_part = ""; | |
1808 | } | |
1809 | ||
fb1fb4ab | 1810 | print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" $has_img_part $error_part updated=\"$last_updated\"/>"; |
a9cb1f83 AD |
1811 | } |
1812 | } | |
1813 | ||
1814 | if ($smart_mode && $fctrs_modified) { | |
1815 | $_SESSION["fctr_last_value"] = $old_counters; | |
1816 | } | |
1817 | } | |
1818 | ||
1b758780 AD |
1819 | function get_script_dt_add() { |
1820 | if (strpos(VERSION, "99") === false) { | |
1821 | return VERSION; | |
1822 | } else { | |
1823 | return time(); | |
1824 | } | |
1825 | } | |
1826 | ||
6e7f8d26 AD |
1827 | function get_pgsql_version($link) { |
1828 | $result = db_query($link, "SELECT version() AS version"); | |
1829 | $version = split(" ", db_fetch_result($result, 0, "version")); | |
1830 | return $version[1]; | |
1831 | } | |
1832 | ||
af106b0e AD |
1833 | function print_error_xml($code, $add_msg = "") { |
1834 | global $ERRORS; | |
1835 | ||
1836 | $error_msg = $ERRORS[$code]; | |
1837 | ||
1838 | if ($add_msg) { | |
1839 | $error_msg = "$error_msg; $add_msg"; | |
1840 | } | |
1841 | ||
4c2abbc1 | 1842 | print "<rpc-reply>"; |
af106b0e | 1843 | print "<error error-code=\"$code\" error-msg=\"$error_msg\"/>"; |
4c2abbc1 | 1844 | print "</rpc-reply>"; |
af106b0e | 1845 | } |
956c7629 AD |
1846 | |
1847 | function subscribe_to_feed($link, $feed_link, $cat_id = 0) { | |
1848 | ||
1849 | if ($cat_id == "0" || !$cat_id) { | |
1850 | $cat_qpart = "NULL"; | |
1851 | } else { | |
1852 | $cat_qpart = "'$cat_id'"; | |
1853 | } | |
1854 | ||
1855 | $result = db_query($link, | |
1856 | "SELECT id FROM ttrss_feeds | |
1857 | WHERE feed_url = '$feed_link' AND owner_uid = ".$_SESSION["uid"]); | |
1858 | ||
1859 | if (db_num_rows($result) == 0) { | |
1860 | ||
1861 | $result = db_query($link, | |
1862 | "INSERT INTO ttrss_feeds (owner_uid,feed_url,title,cat_id) | |
1863 | VALUES ('".$_SESSION["uid"]."', '$feed_link', | |
1864 | '[Unknown]', $cat_qpart)"); | |
1865 | ||
1866 | $result = db_query($link, | |
1867 | "SELECT id FROM ttrss_feeds WHERE feed_url = '$feed_link' | |
1868 | AND owner_uid = " . $_SESSION["uid"]); | |
1869 | ||
1870 | $feed_id = db_fetch_result($result, 0, "id"); | |
1871 | ||
1872 | if ($feed_id) { | |
1873 | update_rss_feed($link, $feed_link, $feed_id, true); | |
1874 | } | |
1875 | ||
1876 | return true; | |
1877 | } else { | |
1878 | return false; | |
1879 | } | |
1880 | } | |
1881 | ||
673d54ca AD |
1882 | function print_feed_select($link, $id, $default_id = "", |
1883 | $attributes = "", $include_all_feeds = true) { | |
1884 | ||
79f3553b | 1885 | print "<select id=\"$id\" name=\"$id\" $attributes>"; |
673d54ca | 1886 | if ($include_all_feeds) { |
79f3553b | 1887 | print "<option value=\"0\">All feeds</option>"; |
673d54ca AD |
1888 | } |
1889 | ||
1890 | $result = db_query($link, "SELECT id,title FROM ttrss_feeds | |
1891 | WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title"); | |
1892 | ||
1893 | if (db_num_rows($result) > 0 && $include_all_feeds) { | |
1894 | print "<option disabled>--------</option>"; | |
1895 | } | |
1896 | ||
1897 | while ($line = db_fetch_assoc($result)) { | |
1898 | if ($line["id"] == $default_id) { | |
1899 | $is_selected = "selected"; | |
1900 | } else { | |
1901 | $is_selected = ""; | |
1902 | } | |
79f3553b | 1903 | printf("<option $is_selected value='%d'>%s</option>", |
07164479 | 1904 | $line["id"], htmlspecialchars(db_unescape_string($line["title"]))); |
673d54ca AD |
1905 | } |
1906 | ||
1907 | print "</select>"; | |
1908 | } | |
1909 | ||
1910 | function print_feed_cat_select($link, $id, $default_id = "", | |
1911 | $attributes = "", $include_all_cats = true) { | |
1912 | ||
79f3553b | 1913 | print "<select id=\"$id\" name=\"$id\" $attributes>"; |
673d54ca AD |
1914 | |
1915 | if ($include_all_cats) { | |
14f69488 | 1916 | print "<option value=\"0\">Uncategorized</option>"; |
673d54ca AD |
1917 | } |
1918 | ||
1919 | $result = db_query($link, "SELECT id,title FROM ttrss_feed_categories | |
1920 | WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title"); | |
1921 | ||
1922 | if (db_num_rows($result) > 0 && $include_all_cats) { | |
1923 | print "<option disabled>--------</option>"; | |
1924 | } | |
1925 | ||
1926 | while ($line = db_fetch_assoc($result)) { | |
1927 | if ($line["id"] == $default_id) { | |
1928 | $is_selected = "selected"; | |
1929 | } else { | |
1930 | $is_selected = ""; | |
1931 | } | |
14f69488 | 1932 | printf("<option $is_selected value='%d'>%s</option>", |
07164479 | 1933 | $line["id"], htmlspecialchars(db_unescape_string($line["title"]))); |
673d54ca AD |
1934 | } |
1935 | ||
1936 | print "</select>"; | |
1937 | } | |
1938 | ||
14f69488 AD |
1939 | function checkbox_to_sql_bool($val) { |
1940 | return ($val == "on") ? "true" : "false"; | |
1941 | } | |
86b682ce AD |
1942 | |
1943 | function getFeedCatTitle($link, $id) { | |
1944 | if ($id == -1) { | |
1945 | return "Special"; | |
1946 | } else if ($id < -10) { | |
1947 | return "Labels"; | |
1948 | } else if ($id > 0) { | |
1949 | $result = db_query($link, "SELECT ttrss_feed_categories.title | |
1950 | FROM ttrss_feeds, ttrss_feed_categories WHERE ttrss_feeds.id = '$id' AND | |
1951 | cat_id = ttrss_feed_categories.id"); | |
1952 | if (db_num_rows($result) == 1) { | |
1953 | return db_fetch_result($result, 0, "title"); | |
1954 | } else { | |
eb4311d0 | 1955 | return "Uncategorized"; |
86b682ce AD |
1956 | } |
1957 | } else { | |
1958 | return "getFeedCatTitle($id) failed"; | |
1959 | } | |
1960 | ||
1961 | } | |
1962 | ||
1963 | function getFeedTitle($link, $id) { | |
1964 | if ($id == -1) { | |
1965 | return "Starred articles"; | |
1966 | } else if ($id < -10) { | |
1967 | $label_id = -10 - $id; | |
1968 | $result = db_query($link, "SELECT description FROM ttrss_labels WHERE id = '$label_id'"); | |
1969 | if (db_num_rows($result) == 1) { | |
1970 | return db_fetch_result($result, 0, "description"); | |
1971 | } else { | |
1972 | return "Unknown label ($label_id)"; | |
1973 | } | |
1974 | ||
1975 | } else if ($id > 0) { | |
1976 | $result = db_query($link, "SELECT title FROM ttrss_feeds WHERE id = '$id'"); | |
1977 | if (db_num_rows($result) == 1) { | |
1978 | return db_fetch_result($result, 0, "title"); | |
1979 | } else { | |
1980 | return "Unknown feed ($id)"; | |
1981 | } | |
1982 | } else { | |
1983 | return "getFeedTitle($id) failed"; | |
1984 | } | |
1985 | ||
1986 | } | |
3dd46f19 AD |
1987 | |
1988 | function get_session_cookie_name() { | |
1989 | return ((!defined('TTRSS_SESSION_NAME')) ? "ttrss_sid" : TTRSS_SESSION_NAME); | |
1990 | } | |
3ac2b520 AD |
1991 | |
1992 | function print_init_params($link) { | |
1993 | print "<init-params>"; | |
1994 | if ($_SESSION["stored-params"]) { | |
1995 | foreach (array_keys($_SESSION["stored-params"]) as $key) { | |
5f57b06d AD |
1996 | if ($key) { |
1997 | $value = htmlspecialchars($_SESSION["stored-params"][$key]); | |
1998 | print "<param key=\"$key\" value=\"$value\"/>"; | |
1999 | } | |
3ac2b520 AD |
2000 | } |
2001 | } | |
2002 | ||
2003 | print "<param key=\"daemon_enabled\" value=\"" . ENABLE_UPDATE_DAEMON . "\"/>"; | |
2004 | print "<param key=\"feeds_frame_refresh\" value=\"" . FEEDS_FRAME_REFRESH . "\"/>"; | |
0d51e25d | 2005 | print "<param key=\"daemon_refresh_only\" value=\"" . DAEMON_REFRESH_ONLY . "\"/>"; |
3ac2b520 AD |
2006 | |
2007 | print "<param key=\"on_catchup_show_next_feed\" value=\"" . | |
2008 | get_pref($link, "ON_CATCHUP_SHOW_NEXT_FEED") . "\"/>"; | |
2009 | ||
e8bd0da9 AD |
2010 | print "<param key=\"hide_read_feeds\" value=\"" . |
2011 | sprintf("%d", get_pref($link, "HIDE_READ_FEEDS")) . "\"/>"; | |
2012 | ||
c9268ed5 AD |
2013 | print "<param key=\"feeds_sort_by_unread\" value=\"" . |
2014 | sprintf("%d", get_pref($link, "FEEDS_SORT_BY_UNREAD")) . "\"/>"; | |
2015 | ||
3ac2b520 AD |
2016 | print "</init-params>"; |
2017 | } | |
f54f515f AD |
2018 | |
2019 | function print_runtime_info($link) { | |
2020 | print "<runtime-info>"; | |
71ad883b AD |
2021 | if (ENABLE_UPDATE_DAEMON) { |
2022 | print "<param key=\"daemon_is_running\" value=\"". | |
2023 | sprintf("%d", file_is_locked("update_daemon.lock")) . "\"/>"; | |
2024 | } | |
f54f515f AD |
2025 | print "</runtime-info>"; |
2026 | } | |
ef393de7 | 2027 | |
88040f57 | 2028 | function getSearchSql($search, $match_on) { |
ef393de7 | 2029 | |
88040f57 | 2030 | $search_query_part = ""; |
e20c9d88 | 2031 | |
88040f57 AD |
2032 | $keywords = split(" ", $search); |
2033 | $query_keywords = array(); | |
e20c9d88 | 2034 | |
88040f57 | 2035 | if ($match_on == "both") { |
e20c9d88 | 2036 | |
88040f57 AD |
2037 | foreach ($keywords as $k) { |
2038 | array_push($query_keywords, "(UPPER(ttrss_entries.title) LIKE UPPER('%$k%') | |
2039 | OR UPPER(ttrss_entries.content) LIKE UPPER('%$k%'))"); | |
2040 | } | |
e20c9d88 | 2041 | |
88040f57 | 2042 | $search_query_part = implode("AND", $query_keywords) . " AND "; |
e20c9d88 | 2043 | |
88040f57 | 2044 | } else if ($match_on == "title") { |
e20c9d88 | 2045 | |
88040f57 AD |
2046 | foreach ($keywords as $k) { |
2047 | array_push($query_keywords, "(UPPER(ttrss_entries.title) LIKE UPPER('%$k%'))"); | |
2048 | } | |
e20c9d88 | 2049 | |
88040f57 | 2050 | $search_query_part = implode("AND", $query_keywords) . " AND "; |
e20c9d88 | 2051 | |
88040f57 AD |
2052 | } else if ($match_on == "content") { |
2053 | ||
2054 | foreach ($keywords as $k) { | |
2055 | array_push($query_keywords, "(UPPER(ttrss_entries.content) LIKE UPPER('%$k%'))"); | |
2056 | } | |
2057 | } | |
2058 | ||
2059 | $search_query_part = implode("AND", $query_keywords); | |
2060 | ||
2061 | return $search_query_part; | |
2062 | } | |
2063 | ||
2064 | function queryFeedHeadlines($link, $feed, $limit, $view_mode, $cat_view, $search, $search_mode, $match_on, $override_order = false) { | |
88040f57 AD |
2065 | if ($search) { |
2066 | ||
2067 | $search_query_part = getSearchSql($search, $match_on); | |
2068 | $search_query_part .= " AND "; | |
e20c9d88 | 2069 | |
ef393de7 AD |
2070 | } else { |
2071 | $search_query_part = ""; | |
2072 | } | |
2073 | ||
2074 | $view_query_part = ""; | |
2075 | ||
2076 | if ($view_mode == "adaptive") { | |
2077 | if ($search) { | |
2078 | $view_query_part = " "; | |
2079 | } else if ($feed != -1) { | |
f295c368 | 2080 | $unread = getFeedUnread($link, $feed, $cat_view); |
ef393de7 AD |
2081 | if ($unread > 0) { |
2082 | $view_query_part = " unread = true AND "; | |
2083 | } | |
2084 | } | |
2085 | } | |
2086 | ||
2087 | if ($view_mode == "marked") { | |
2088 | $view_query_part = " marked = true AND "; | |
2089 | } | |
2090 | ||
2091 | if ($view_mode == "unread") { | |
2092 | $view_query_part = " unread = true AND "; | |
2093 | } | |
2094 | ||
2095 | if ($limit > 0) { | |
2096 | $limit_query_part = "LIMIT " . $limit; | |
2097 | } | |
2098 | ||
2099 | $vfeed_query_part = ""; | |
2100 | ||
2101 | // override query strategy and enable feed display when searching globally | |
2102 | if ($search && $search_mode == "all_feeds") { | |
2103 | $query_strategy_part = "ttrss_entries.id > 0"; | |
2104 | $vfeed_query_part = "ttrss_feeds.title AS feed_title,"; | |
2105 | } else if (preg_match("/^-?[0-9][0-9]*$/", $feed) == false) { | |
2106 | $query_strategy_part = "ttrss_entries.id > 0"; | |
2107 | $vfeed_query_part = "(SELECT title FROM ttrss_feeds WHERE | |
2108 | id = feed_id) as feed_title,"; | |
2109 | } else if ($feed >= 0 && $search && $search_mode == "this_cat") { | |
2110 | ||
2111 | $vfeed_query_part = "ttrss_feeds.title AS feed_title,"; | |
0a6c4846 AD |
2112 | |
2113 | $tmp_result = false; | |
2114 | ||
2115 | if ($cat_view) { | |
2116 | $tmp_result = db_query($link, "SELECT id | |
2117 | FROM ttrss_feeds WHERE cat_id = '$feed'"); | |
2118 | } else { | |
2119 | $tmp_result = db_query($link, "SELECT id | |
2120 | FROM ttrss_feeds WHERE cat_id = (SELECT cat_id FROM ttrss_feeds | |
2121 | WHERE id = '$feed') AND id != '$feed'"); | |
2122 | } | |
ef393de7 AD |
2123 | |
2124 | $cat_siblings = array(); | |
2125 | ||
2126 | if (db_num_rows($tmp_result) > 0) { | |
2127 | while ($p = db_fetch_assoc($tmp_result)) { | |
2128 | array_push($cat_siblings, "feed_id = " . $p["id"]); | |
2129 | } | |
2130 | ||
2131 | $query_strategy_part = sprintf("(feed_id = %d OR %s)", | |
2132 | $feed, implode(" OR ", $cat_siblings)); | |
2133 | ||
2134 | } else { | |
2135 | $query_strategy_part = "ttrss_entries.id > 0"; | |
2136 | } | |
2137 | ||
2138 | } else if ($feed >= 0) { | |
2139 | ||
2140 | if ($cat_view) { | |
5c365f60 | 2141 | |
ef393de7 AD |
2142 | if ($feed > 0) { |
2143 | $query_strategy_part = "cat_id = '$feed'"; | |
2144 | } else { | |
2145 | $query_strategy_part = "cat_id IS NULL"; | |
2146 | } | |
2147 | ||
2148 | $vfeed_query_part = "ttrss_feeds.title AS feed_title,"; | |
5c365f60 | 2149 | |
ef393de7 AD |
2150 | } else { |
2151 | $tmp_result = db_query($link, "SELECT id | |
2152 | FROM ttrss_feeds WHERE parent_feed = '$feed' | |
2153 | ORDER BY cat_id,title"); | |
2154 | ||
2155 | $parent_ids = array(); | |
2156 | ||
2157 | if (db_num_rows($tmp_result) > 0) { | |
2158 | while ($p = db_fetch_assoc($tmp_result)) { | |
2159 | array_push($parent_ids, "feed_id = " . $p["id"]); | |
2160 | } | |
2161 | ||
2162 | $query_strategy_part = sprintf("(feed_id = %d OR %s)", | |
2163 | $feed, implode(" OR ", $parent_ids)); | |
2164 | ||
2165 | $vfeed_query_part = "ttrss_feeds.title AS feed_title,"; | |
2166 | } else { | |
2167 | $query_strategy_part = "feed_id = '$feed'"; | |
2168 | } | |
2169 | } | |
2170 | } else if ($feed == -1) { // starred virtual feed | |
2171 | $query_strategy_part = "marked = true"; | |
2172 | $vfeed_query_part = "ttrss_feeds.title AS feed_title,"; | |
2173 | } else if ($feed <= -10) { // labels | |
2174 | $label_id = -$feed - 11; | |
2175 | ||
2176 | $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels | |
2177 | WHERE id = '$label_id'"); | |
2178 | ||
2179 | $query_strategy_part = db_fetch_result($tmp_result, 0, "sql_exp"); | |
2180 | ||
2181 | $vfeed_query_part = "ttrss_feeds.title AS feed_title,"; | |
2182 | } else { | |
2183 | $query_strategy_part = "id > 0"; // dumb | |
2184 | } | |
d6e5706d AD |
2185 | |
2186 | if (get_pref($link, 'REVERSE_HEADLINES')) { | |
2187 | $order_by = "updated"; | |
2188 | } else { | |
2189 | $order_by = "updated DESC"; | |
2190 | } | |
e939722a AD |
2191 | |
2192 | if ($override_order) { | |
2193 | $order_by = $override_order; | |
2194 | } | |
ef393de7 AD |
2195 | |
2196 | $feed_title = ""; | |
2197 | ||
2198 | if ($search && $search_mode == "all_feeds") { | |
2199 | $feed_title = "Global search results ($search)"; | |
2200 | } else if ($search && preg_match('/^-?[0-9][0-9]*$/', $feed) == false) { | |
e1eb2147 | 2201 | $feed_title = "Tag search results ($search, $feed)"; |
ef393de7 AD |
2202 | } else if (preg_match('/^-?[0-9][0-9]*$/', $feed) == false) { |
2203 | $feed_title = $feed; | |
2204 | } else if (preg_match('/^-?[0-9][0-9]*$/', $feed) != false && $feed >= 0) { | |
2205 | ||
2206 | if ($cat_view) { | |
5c365f60 | 2207 | |
ef393de7 AD |
2208 | if ($feed != 0) { |
2209 | $result = db_query($link, "SELECT title FROM ttrss_feed_categories | |
2210 | WHERE id = '$feed' AND owner_uid = " . $_SESSION["uid"]); | |
2211 | $feed_title = db_fetch_result($result, 0, "title"); | |
2212 | } else { | |
2213 | $feed_title = "Uncategorized"; | |
2214 | } | |
e1eb2147 AD |
2215 | |
2216 | if ($search) { | |
2217 | $feed_title = "Category search results ($search, $feed_title)"; | |
2218 | } | |
2219 | ||
ef393de7 AD |
2220 | } else { |
2221 | ||
2222 | $result = db_query($link, "SELECT title,site_url,last_error FROM ttrss_feeds | |
2223 | WHERE id = '$feed' AND owner_uid = " . $_SESSION["uid"]); | |
2224 | ||
2225 | $feed_title = db_fetch_result($result, 0, "title"); | |
2226 | $feed_site_url = db_fetch_result($result, 0, "site_url"); | |
2227 | $last_error = db_fetch_result($result, 0, "last_error"); | |
e1eb2147 AD |
2228 | |
2229 | if ($search) { | |
2230 | $feed_title = "Feed search results ($search, $feed_title)"; | |
2231 | } | |
ef393de7 AD |
2232 | } |
2233 | ||
2234 | } else if ($feed == -1) { | |
2235 | $feed_title = "Starred articles"; | |
2236 | } else if ($feed < -10) { | |
2237 | $label_id = -$feed - 11; | |
2238 | $result = db_query($link, "SELECT description FROM ttrss_labels | |
2239 | WHERE id = '$label_id'"); | |
2240 | $feed_title = db_fetch_result($result, 0, "description"); | |
88040f57 AD |
2241 | |
2242 | if ($search) { | |
2243 | $feed_title = "Label search results ($search, $feed_title)"; | |
2244 | } | |
ef393de7 AD |
2245 | } else { |
2246 | $feed_title = "?"; | |
2247 | } | |
2248 | ||
2249 | $feed_title = db_unescape_string($feed_title); | |
2250 | ||
2251 | if ($feed < -10) error_reporting (0); | |
2252 | ||
2253 | if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) { | |
2254 | ||
2255 | if ($feed >= 0) { | |
2256 | $feed_kind = "Feeds"; | |
2257 | } else { | |
2258 | $feed_kind = "Labels"; | |
2259 | } | |
2260 | ||
2261 | $content_query_part = "content as content_preview,"; | |
2262 | ||
2263 | $query = "SELECT | |
2264 | ttrss_entries.id,ttrss_entries.title, | |
2265 | SUBSTRING(updated,1,16) as updated, | |
2266 | unread,feed_id,marked,link,last_read, | |
2267 | SUBSTRING(last_read,1,19) as last_read_noms, | |
2268 | $vfeed_query_part | |
2269 | $content_query_part | |
2270 | SUBSTRING(updated,1,19) as updated_noms | |
2271 | FROM | |
2272 | ttrss_entries,ttrss_user_entries,ttrss_feeds | |
2273 | WHERE | |
cfb02131 | 2274 | ttrss_feeds.hidden = false AND |
ef393de7 AD |
2275 | ttrss_user_entries.feed_id = ttrss_feeds.id AND |
2276 | ttrss_user_entries.ref_id = ttrss_entries.id AND | |
2277 | ttrss_user_entries.owner_uid = '".$_SESSION["uid"]."' AND | |
2278 | $search_query_part | |
2279 | $view_query_part | |
2280 | $query_strategy_part ORDER BY $order_by | |
2281 | $limit_query_part"; | |
2282 | ||
2283 | $result = db_query($link, $query); | |
2284 | ||
2285 | if ($_GET["debug"]) print $query; | |
2286 | ||
2287 | } else { | |
2288 | // browsing by tag | |
2289 | ||
2290 | $feed_kind = "Tags"; | |
2291 | ||
2292 | $result = db_query($link, "SELECT | |
2293 | ttrss_entries.id as id,title, | |
2294 | SUBSTRING(updated,1,16) as updated, | |
2295 | unread,feed_id, | |
2296 | marked,link,last_read, | |
2297 | SUBSTRING(last_read,1,19) as last_read_noms, | |
2298 | $vfeed_query_part | |
2299 | $content_query_part | |
2300 | SUBSTRING(updated,1,19) as updated_noms | |
2301 | FROM | |
2302 | ttrss_entries,ttrss_user_entries,ttrss_tags | |
2303 | WHERE | |
2304 | ref_id = ttrss_entries.id AND | |
2305 | ttrss_user_entries.owner_uid = '".$_SESSION["uid"]."' AND | |
2306 | post_int_id = int_id AND tag_name = '$feed' AND | |
2307 | $view_query_part | |
2308 | $search_query_part | |
2309 | $query_strategy_part ORDER BY $order_by | |
2310 | $limit_query_part"); | |
2311 | } | |
2312 | ||
c7188969 | 2313 | return array($result, $feed_title, $feed_site_url, $last_error); |
ef393de7 AD |
2314 | |
2315 | } | |
2316 | ||
e1eb2147 AD |
2317 | function generate_syndicated_feed($link, $feed, $is_cat, |
2318 | $search, $search_mode, $match_on) { | |
18664970 AD |
2319 | |
2320 | $qfh_ret = queryFeedHeadlines($link, $feed, | |
e939722a | 2321 | 30, false, $is_cat, $search, $search_mode, $match_on, "updated DESC"); |
18664970 AD |
2322 | |
2323 | $result = $qfh_ret[0]; | |
59e2aab4 | 2324 | $feed_title = htmlspecialchars($qfh_ret[1]); |
18664970 AD |
2325 | $feed_site_url = $qfh_ret[2]; |
2326 | $last_error = $qfh_ret[3]; | |
2327 | ||
2328 | print "<rss version=\"2.0\"> | |
2329 | <channel> | |
2330 | <title>$feed_title</title> | |
2331 | <link>$feed_site_url</link> | |
2332 | <generator>Tiny Tiny RSS v".VERSION."</generator>"; | |
2333 | ||
2334 | while ($line = db_fetch_assoc($result)) { | |
2335 | print "<item>"; | |
2336 | print "<link>" . htmlspecialchars($line["link"]) . "</link>"; | |
2337 | ||
2338 | $rfc822_date = date('r', strtotime($line["updated"])); | |
2339 | ||
2340 | print "<pubDate>$rfc822_date</pubDate>"; | |
2341 | ||
2342 | print "<title>" . | |
2343 | htmlspecialchars($line["title"]) . "</title>"; | |
2344 | ||
2345 | print "<description>" . | |
2346 | htmlspecialchars($line["content_preview"]) . "</description>"; | |
2347 | ||
2348 | print "</item>"; | |
2349 | } | |
2350 | ||
2351 | print "</channel></rss>"; | |
2352 | ||
2353 | } | |
2354 | ||
0a6c4846 AD |
2355 | function getCategoryTitle($link, $cat_id) { |
2356 | ||
2357 | $result = db_query($link, "SELECT title FROM ttrss_feed_categories WHERE | |
2358 | id = '$cat_id'"); | |
2359 | ||
2360 | if (db_num_rows($result) == 1) { | |
2361 | return db_fetch_result($result, 0, "title"); | |
2362 | } else { | |
2363 | return "Uncategorized"; | |
2364 | } | |
2365 | } | |
2366 | ||
183ad07b AD |
2367 | function sanitize_rss($str) { |
2368 | $res = ""; | |
2369 | ||
2370 | $res = preg_replace('/<script.*?>/i', | |
2371 | "<p class=\"scriptWarn\">", $str); | |
2372 | ||
2373 | $res = preg_replace('/<\/script>/i', | |
2374 | "</p>", $res); | |
2375 | ||
2376 | return $res; | |
2377 | } | |
b72c3ef8 AD |
2378 | |
2379 | function check_for_update($link) { | |
2380 | $releases_feed = "http://tt-rss.spb.ru/releases.rss"; | |
2381 | ||
2382 | if (!CHECK_FOR_NEW_VERSION || $_SESSION["access_level"] < 10) { | |
2383 | return; | |
2384 | } | |
2385 | ||
2386 | error_reporting(0); | |
2387 | $rss = fetch_rss($releases_feed); | |
2388 | error_reporting (DEFAULT_ERROR_LEVEL); | |
2389 | ||
2390 | if ($rss) { | |
2391 | ||
2392 | $items = $rss->items; | |
2393 | ||
2394 | if (!$items || !is_array($items)) $items = $rss->entries; | |
2395 | if (!$items || !is_array($items)) $items = $rss; | |
2396 | ||
da412ad3 | 2397 | if (!is_array($items) || count($items) == 0) { |
b72c3ef8 | 2398 | return; |
da412ad3 | 2399 | } |
b72c3ef8 AD |
2400 | |
2401 | $last_item = $items[0]; | |
2402 | ||
2403 | $last_version = trim(preg_replace("/(Milestone)|(completed)/", "", $last_item["title"])); | |
2404 | $cur_version = preg_replace("/\.99/", "", VERSION); | |
2405 | ||
2406 | $release_url = $last_item["link"]; | |
2407 | ||
9157e015 | 2408 | if ($cur_version != $last_version) { |
b72c3ef8 | 2409 | return "<div class=\"notice\"><a target=\"_new\" href=\"$release_url\"> |
da412ad3 AD |
2410 | New version of Tiny-Tiny RSS ($last_version) is available.</a></div>"; |
2411 | } | |
b72c3ef8 AD |
2412 | } |
2413 | } | |
40d13c28 | 2414 | ?> |