]>
Commit | Line | Data |
---|---|---|
40d13c28 | 1 | <? |
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 | |
387234f3 AD |
15 | require_once 'magpierss/rss_utils.inc'; |
16 | ||
a3ee2a38 AD |
17 | define('MAGPIE_OUTPUT_ENCODING', 'UTF-8'); |
18 | ||
ad507f85 AD |
19 | function purge_feed($link, $feed_id, $purge_interval, $debug = false) { |
20 | ||
21 | $rows = -1; | |
4c193675 | 22 | |
fefa6ca3 | 23 | if (DB_TYPE == "pgsql") { |
44e241cb | 24 | /* $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE |
fefa6ca3 | 25 | marked = false AND feed_id = '$feed_id' AND |
35d8cf43 | 26 | (SELECT date_entered FROM ttrss_entries WHERE |
44e241cb AD |
27 | id = ref_id) < NOW() - INTERVAL '$purge_interval days'"); */ |
28 | ||
6e7f8d26 AD |
29 | $pg_version = get_pgsql_version($link); |
30 | ||
31 | if (preg_match("/^7\./", $pg_version) || preg_match("/^8\.0/", $pg_version)) { | |
1e59ae35 AD |
32 | |
33 | $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE | |
34 | ttrss_entries.id = ref_id AND | |
35 | marked = false AND | |
36 | feed_id = '$feed_id' AND | |
37 | ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days'"); | |
38 | ||
39 | } else { | |
40 | ||
41 | $result = db_query($link, "DELETE FROM ttrss_user_entries | |
42 | USING ttrss_entries | |
43 | WHERE ttrss_entries.id = ref_id AND | |
44 | marked = false AND | |
45 | feed_id = '$feed_id' AND | |
fc774155 | 46 | ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days'"); |
1e59ae35 | 47 | } |
ad507f85 AD |
48 | |
49 | $rows = pg_affected_rows($result); | |
50 | ||
fefa6ca3 | 51 | } else { |
1e59ae35 | 52 | |
30f1746f | 53 | /* $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE |
fefa6ca3 | 54 | marked = false AND feed_id = '$feed_id' AND |
35d8cf43 | 55 | (SELECT date_entered FROM ttrss_entries WHERE |
30f1746f AD |
56 | id = ref_id) < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)"); */ |
57 | ||
58 | $result = db_query($link, "DELETE FROM ttrss_user_entries | |
59 | USING ttrss_user_entries, ttrss_entries | |
60 | WHERE ttrss_entries.id = ref_id AND | |
61 | marked = false AND | |
62 | feed_id = '$feed_id' AND | |
63 | ttrss_entries.date_entered < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)"); | |
64 | ||
ad507f85 AD |
65 | $rows = mysql_affected_rows($link); |
66 | ||
67 | } | |
68 | ||
69 | if ($debug) { | |
70 | print "Purged feed $feed_id ($purge_interval): deleted $rows articles\n"; | |
fefa6ca3 AD |
71 | } |
72 | } | |
73 | ||
44e241cb AD |
74 | function global_purge_old_posts($link, $do_output = false, $limit = false) { |
75 | ||
894ebcf5 | 76 | $random_qpart = sql_random_function(); |
fefa6ca3 | 77 | |
44e241cb AD |
78 | if ($limit) { |
79 | $limit_qpart = "LIMIT $limit"; | |
80 | } else { | |
81 | $limit_qpart = ""; | |
82 | } | |
83 | ||
fefa6ca3 | 84 | $result = db_query($link, |
44e241cb AD |
85 | "SELECT id,purge_interval,owner_uid FROM ttrss_feeds |
86 | ORDER BY $random_qpart $limit_qpart"); | |
fefa6ca3 AD |
87 | |
88 | while ($line = db_fetch_assoc($result)) { | |
89 | ||
90 | $feed_id = $line["id"]; | |
91 | $purge_interval = $line["purge_interval"]; | |
92 | $owner_uid = $line["owner_uid"]; | |
93 | ||
94 | if ($purge_interval == 0) { | |
95 | ||
96 | $tmp_result = db_query($link, | |
97 | "SELECT value FROM ttrss_user_prefs WHERE | |
98 | pref_name = 'PURGE_OLD_DAYS' AND owner_uid = '$owner_uid'"); | |
99 | ||
100 | if (db_num_rows($tmp_result) != 0) { | |
101 | $purge_interval = db_fetch_result($tmp_result, 0, "value"); | |
102 | } | |
103 | } | |
104 | ||
105 | if ($do_output) { | |
ad507f85 | 106 | // print "Feed $feed_id: purge interval = $purge_interval\n"; |
fefa6ca3 AD |
107 | } |
108 | ||
109 | if ($purge_interval > 0) { | |
ad507f85 | 110 | purge_feed($link, $feed_id, $purge_interval, $do_output); |
fefa6ca3 AD |
111 | } |
112 | } | |
113 | ||
71604ca4 AD |
114 | // purge orphaned posts in main content table |
115 | db_query($link, "DELETE FROM ttrss_entries WHERE | |
116 | (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0"); | |
117 | ||
fefa6ca3 AD |
118 | } |
119 | ||
b6eefba5 | 120 | function purge_old_posts($link) { |
5d73494a | 121 | |
f1a80dae AD |
122 | $user_id = $_SESSION["uid"]; |
123 | ||
124 | $result = db_query($link, "SELECT id,purge_interval FROM ttrss_feeds | |
125 | WHERE owner_uid = '$user_id'"); | |
5d73494a AD |
126 | |
127 | while ($line = db_fetch_assoc($result)) { | |
128 | ||
129 | $feed_id = $line["id"]; | |
130 | $purge_interval = $line["purge_interval"]; | |
131 | ||
b619ff15 | 132 | if ($purge_interval == 0) $purge_interval = get_pref($link, 'PURGE_OLD_DAYS'); |
5d73494a | 133 | |
140aae81 | 134 | if ($purge_interval > 0) { |
fefa6ca3 | 135 | purge_feed($link, $feed_id, $purge_interval); |
5d73494a AD |
136 | } |
137 | } | |
71604ca4 AD |
138 | |
139 | // purge orphaned posts in main content table | |
140 | db_query($link, "DELETE FROM ttrss_entries WHERE | |
141 | (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0"); | |
c3a8d71a AD |
142 | } |
143 | ||
1f2b01ed | 144 | function update_all_feeds($link, $fetch, $user_id = false, $force_daemon = false) { |
40d13c28 | 145 | |
4769ddaf | 146 | if (WEB_DEMO_MODE) return; |
b0b4abcf | 147 | |
a2770077 AD |
148 | if (!$user_id) { |
149 | $user_id = $_SESSION["uid"]; | |
150 | purge_old_posts($link); | |
151 | } | |
152 | ||
25af8dad | 153 | // db_query($link, "BEGIN"); |
b82af8c3 | 154 | |
cbd8650d AD |
155 | if (MAX_UPDATE_TIME > 0) { |
156 | if (DB_TYPE == "mysql") { | |
157 | $q_order = "RAND()"; | |
158 | } else { | |
159 | $q_order = "RANDOM()"; | |
160 | } | |
161 | } else { | |
162 | $q_order = "last_updated DESC"; | |
163 | } | |
164 | ||
d148926e | 165 | $result = db_query($link, "SELECT feed_url,id, |
798f722b | 166 | SUBSTRING(last_updated,1,19) AS last_updated, |
5c563acd | 167 | update_interval FROM ttrss_feeds WHERE owner_uid = '$user_id' |
cbd8650d AD |
168 | ORDER BY $q_order"); |
169 | ||
170 | $upd_start = time(); | |
40d13c28 | 171 | |
b6eefba5 | 172 | while ($line = db_fetch_assoc($result)) { |
d148926e AD |
173 | $upd_intl = $line["update_interval"]; |
174 | ||
b619ff15 | 175 | if (!$upd_intl || $upd_intl == 0) { |
a2770077 | 176 | $upd_intl = get_pref($link, 'DEFAULT_UPDATE_INTERVAL', $user_id); |
b619ff15 | 177 | } |
d148926e | 178 | |
c1e202b7 AD |
179 | if ($upd_intl < 0) { |
180 | // Updates for this feed are disabled | |
181 | continue; | |
182 | } | |
183 | ||
93d40f50 AD |
184 | if ($fetch || (!$line["last_updated"] || |
185 | time() - strtotime($line["last_updated"]) > ($upd_intl * 60))) { | |
c5142cca | 186 | |
cbd8650d AD |
187 | // print "<!-- feed: ".$line["feed_url"]." -->"; |
188 | ||
1f2b01ed | 189 | update_rss_feed($link, $line["feed_url"], $line["id"], $force_daemon); |
cbd8650d AD |
190 | |
191 | $upd_elapsed = time() - $upd_start; | |
192 | ||
193 | if (MAX_UPDATE_TIME > 0 && $upd_elapsed > MAX_UPDATE_TIME) { | |
194 | return; | |
195 | } | |
d148926e | 196 | } |
40d13c28 AD |
197 | } |
198 | ||
25af8dad | 199 | // db_query($link, "COMMIT"); |
b82af8c3 | 200 | |
40d13c28 AD |
201 | } |
202 | ||
9e997874 | 203 | function check_feed_favicon($feed_url, $feed, $link) { |
78800912 AD |
204 | $feed_url = str_replace("http://", "", $feed_url); |
205 | $feed_url = preg_replace("/\/.*$/", "", $feed_url); | |
206 | ||
207 | $icon_url = "http://$feed_url/favicon.ico"; | |
273a2f6b | 208 | $icon_file = ICONS_DIR . "/$feed.ico"; |
78800912 AD |
209 | |
210 | if (!file_exists($icon_file)) { | |
e695fdc8 | 211 | |
78800912 AD |
212 | error_reporting(0); |
213 | $r = fopen($icon_url, "r"); | |
cce28758 | 214 | error_reporting (DEFAULT_ERROR_LEVEL); |
78800912 AD |
215 | |
216 | if ($r) { | |
7d7cbaf5 | 217 | $tmpfname = tempnam(TMP_DIRECTORY, "ttrssicon"); |
78800912 AD |
218 | |
219 | $t = fopen($tmpfname, "w"); | |
220 | ||
221 | while (!feof($r)) { | |
222 | $buf = fread($r, 16384); | |
223 | fwrite($t, $buf); | |
224 | } | |
225 | ||
226 | fclose($r); | |
227 | fclose($t); | |
228 | ||
e695fdc8 AD |
229 | error_reporting(0); |
230 | if (!rename($tmpfname, $icon_file)) { | |
231 | unlink($tmpfname); | |
232 | } | |
717f5e64 AD |
233 | |
234 | chmod($icon_file, 0644); | |
235 | ||
cce28758 | 236 | error_reporting (DEFAULT_ERROR_LEVEL); |
78800912 AD |
237 | |
238 | } | |
239 | } | |
240 | } | |
241 | ||
ddb68b81 | 242 | function update_rss_feed($link, $feed_url, $feed, $ignore_daemon = false) { |
40d13c28 | 243 | |
4769ddaf | 244 | if (WEB_DEMO_MODE) return; |
b0b4abcf | 245 | |
ddb68b81 | 246 | if (DAEMON_REFRESH_ONLY && !$_GET["daemon"] && !$ignore_daemon) { |
21cfcdf2 AD |
247 | return; |
248 | } | |
249 | ||
47c6c988 | 250 | $result = db_query($link, "SELECT update_interval,auth_login,auth_pass |
a88c1f36 AD |
251 | FROM ttrss_feeds WHERE id = '$feed'"); |
252 | ||
47c6c988 AD |
253 | $auth_login = db_fetch_result($result, 0, "auth_login"); |
254 | $auth_pass = db_fetch_result($result, 0, "auth_pass"); | |
255 | ||
a88c1f36 AD |
256 | $update_interval = db_fetch_result($result, 0, "update_interval"); |
257 | ||
258 | if ($update_interval < 0) { return; } | |
259 | ||
ab3d0b99 AD |
260 | $feed = db_escape_string($feed); |
261 | ||
47c6c988 AD |
262 | $fetch_url = $feed_url; |
263 | ||
264 | if ($auth_login && $auth_pass) { | |
265 | $url_parts = array(); | |
266 | preg_match("/(^[^:]*):\/\/(.*)/", $fetch_url, $url_parts); | |
267 | ||
268 | if ($url_parts[1] && $url_parts[2]) { | |
269 | $fetch_url = $url_parts[1] . "://$auth_login:$auth_pass@" . $url_parts[2]; | |
270 | } | |
271 | ||
272 | } | |
3ad5aa85 | 273 | error_reporting(0); |
47c6c988 | 274 | $rss = fetch_rss($fetch_url); |
ab3d0b99 | 275 | |
cce28758 | 276 | error_reporting (DEFAULT_ERROR_LEVEL); |
76798ff3 | 277 | |
b6eefba5 | 278 | $feed = db_escape_string($feed); |
dcee8f61 | 279 | |
40d13c28 | 280 | if ($rss) { |
b82af8c3 | 281 | |
44e241cb | 282 | // db_query($link, "BEGIN"); |
dd8c76a9 | 283 | |
a88c1f36 | 284 | $result = db_query($link, "SELECT title,icon_url,site_url,owner_uid |
f324892e | 285 | FROM ttrss_feeds WHERE id = '$feed'"); |
331900c6 | 286 | |
b6eefba5 AD |
287 | $registered_title = db_fetch_result($result, 0, "title"); |
288 | $orig_icon_url = db_fetch_result($result, 0, "icon_url"); | |
f324892e | 289 | $orig_site_url = db_fetch_result($result, 0, "site_url"); |
331900c6 | 290 | |
7fed1940 AD |
291 | $owner_uid = db_fetch_result($result, 0, "owner_uid"); |
292 | ||
a2770077 AD |
293 | if (get_pref($link, 'ENABLE_FEED_ICONS', $owner_uid)) { |
294 | check_feed_favicon($feed_url, $feed, $link); | |
295 | } | |
296 | ||
746b249f | 297 | if (!$registered_title || $registered_title == "[Unknown]") { |
e1305a97 | 298 | $feed_title = db_escape_string($rss->channel["title"]); |
f324892e AD |
299 | db_query($link, "UPDATE ttrss_feeds SET |
300 | title = '$feed_title' WHERE id = '$feed'"); | |
301 | } | |
302 | ||
147f7691 | 303 | $site_url = $rss->channel["link"]; |
832b7bfc AD |
304 | // weird, weird Magpie |
305 | if (!$site_url) $site_url = db_escape_string($rss->channel["link_"]); | |
147f7691 AD |
306 | |
307 | if ($site_url && $orig_site_url != db_escape_string($site_url)) { | |
f324892e AD |
308 | db_query($link, "UPDATE ttrss_feeds SET |
309 | site_url = '$site_url' WHERE id = '$feed'"); | |
331900c6 | 310 | } |
40d13c28 | 311 | |
b7f4bda2 AD |
312 | // print "I: " . $rss->channel["image"]["url"]; |
313 | ||
314 | $icon_url = $rss->image["url"]; | |
315 | ||
147f7691 | 316 | if ($icon_url && !$orig_icon_url != db_escape_string($icon_url)) { |
b6eefba5 AD |
317 | $icon_url = db_escape_string($icon_url); |
318 | db_query($link, "UPDATE ttrss_feeds SET icon_url = '$icon_url' WHERE id = '$feed'"); | |
b7f4bda2 AD |
319 | } |
320 | ||
e6155a06 AD |
321 | |
322 | $filters = array(); | |
323 | ||
4b3dff6e | 324 | $result = db_query($link, "SELECT reg_exp, |
db42b934 AD |
325 | ttrss_filter_types.name AS name, |
326 | ttrss_filter_actions.name AS action | |
327 | FROM ttrss_filters,ttrss_filter_types,ttrss_filter_actions WHERE | |
328 | owner_uid = $owner_uid AND | |
329 | ttrss_filter_types.id = filter_type AND | |
330 | ttrss_filter_actions.id = action_id AND | |
ead60402 | 331 | (feed_id IS NULL OR feed_id = '$feed')"); |
e6155a06 | 332 | |
b6eefba5 | 333 | while ($line = db_fetch_assoc($result)) { |
e6155a06 | 334 | if (!$filters[$line["name"]]) $filters[$line["name"]] = array(); |
19c9cb11 AD |
335 | |
336 | $filter["reg_exp"] = $line["reg_exp"]; | |
337 | $filter["action"] = $line["action"]; | |
338 | ||
339 | array_push($filters[$line["name"]], $filter); | |
e6155a06 AD |
340 | } |
341 | ||
ddb68b81 AD |
342 | $iterator = $rss->items; |
343 | ||
c22789da AD |
344 | if (!$iterator || !is_array($iterator)) $iterator = $rss->entries; |
345 | if (!$iterator || !is_array($iterator)) $iterator = $rss; | |
346 | ||
347 | if (!is_array($iterator)) { | |
39541e74 | 348 | /* db_query($link, "UPDATE ttrss_feeds |
75bd0669 | 349 | SET last_error = 'Parse error: can\'t find any articles.' |
39541e74 | 350 | WHERE id = '$feed'"); */ |
c22789da AD |
351 | return; // WTF? |
352 | } | |
ddb68b81 AD |
353 | |
354 | foreach ($iterator as $item) { | |
40d13c28 AD |
355 | |
356 | $entry_guid = $item["id"]; | |
357 | ||
358 | if (!$entry_guid) $entry_guid = $item["guid"]; | |
359 | if (!$entry_guid) $entry_guid = $item["link"]; | |
466001c4 AD |
360 | |
361 | if (!$entry_guid) continue; | |
a116f569 | 362 | |
9c9c7e6b | 363 | $entry_timestamp = ""; |
b82af8c3 | 364 | |
9c9c7e6b AD |
365 | $rss_2_date = $item['pubdate']; |
366 | $rss_1_date = $item['dc']['date']; | |
367 | $atom_date = $item['issued']; | |
59ba2c75 | 368 | if (!$atom_date) $atom_date = $item['updated']; |
b197f117 | 369 | |
9c9c7e6b AD |
370 | if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date); |
371 | if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date); | |
372 | if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date); | |
b82af8c3 AD |
373 | |
374 | if ($entry_timestamp == "") { | |
375 | $entry_timestamp = time(); | |
376 | $no_orig_date = 'true'; | |
466001c4 AD |
377 | } else { |
378 | $no_orig_date = 'false'; | |
b82af8c3 | 379 | } |
b197f117 | 380 | |
466001c4 | 381 | $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp); |
71ad3959 | 382 | |
40d13c28 | 383 | $entry_title = $item["title"]; |
ddb68b81 AD |
384 | |
385 | // strange Magpie workaround | |
386 | $entry_link = $item["link_"]; | |
387 | if (!$entry_link) $entry_link = $item["link"]; | |
71ad3959 AD |
388 | |
389 | if (!$entry_title) continue; | |
390 | if (!$entry_link) continue; | |
391 | ||
1696229f AD |
392 | $entry_content = $item["content:escaped"]; |
393 | ||
394 | if (!$entry_content) $entry_content = $item["content:encoded"]; | |
40d13c28 | 395 | if (!$entry_content) $entry_content = $item["content"]; |
372ced8b | 396 | if (!$entry_content) $entry_content = $item["summary"]; |
1696229f | 397 | if (!$entry_content) $entry_content = $item["description"]; |
a2015351 | 398 | |
a116f569 | 399 | // if (!$entry_content) continue; |
a2015351 | 400 | |
8add756a AD |
401 | // WTF |
402 | if (is_array($entry_content)) { | |
403 | $entry_content = $entry_content["encoded"]; | |
1696229f | 404 | if (!$entry_content) $entry_content = $entry_content["escaped"]; |
8add756a AD |
405 | } |
406 | ||
1696229f | 407 | // print_r($item); |
372ced8b AD |
408 | // print_r(htmlspecialchars($entry_content)); |
409 | // print "<br>"; | |
1696229f | 410 | |
372ced8b | 411 | $entry_content_unescaped = $entry_content; |
466001c4 | 412 | $content_hash = "SHA1:" . sha1(strip_tags($entry_content)); |
cb0bd8bd | 413 | |
a1ea1e12 AD |
414 | $entry_comments = $item["comments"]; |
415 | ||
b6104dee AD |
416 | $entry_author = db_escape_string($item['dc']['creator']); |
417 | ||
b6eefba5 | 418 | $entry_guid = db_escape_string($entry_guid); |
2651fc4f | 419 | |
05732aa0 AD |
420 | $result = db_query($link, "SELECT id FROM ttrss_entries |
421 | WHERE guid = '$entry_guid'"); | |
4c193675 | 422 | |
b17fcb1a AD |
423 | $entry_content = db_escape_string($entry_content); |
424 | $entry_title = db_escape_string($entry_title); | |
425 | $entry_link = db_escape_string($entry_link); | |
426 | $entry_comments = db_escape_string($entry_comments); | |
427 | ||
27f089dc | 428 | $num_comments = db_escape_string($item["slash"]["comments"]); |
11b0dce2 | 429 | |
e31073bd AD |
430 | if (!$num_comments) $num_comments = 0; |
431 | ||
44e241cb AD |
432 | db_query($link, "BEGIN"); |
433 | ||
4c193675 AD |
434 | if (db_num_rows($result) == 0) { |
435 | ||
436 | // base post entry does not exist, create it | |
437 | ||
4c193675 AD |
438 | $result = db_query($link, |
439 | "INSERT INTO ttrss_entries | |
440 | (title, | |
441 | guid, | |
442 | link, | |
443 | updated, | |
444 | content, | |
445 | content_hash, | |
446 | no_orig_date, | |
447 | date_entered, | |
11b0dce2 | 448 | comments, |
b6104dee AD |
449 | num_comments, |
450 | author) | |
4c193675 AD |
451 | VALUES |
452 | ('$entry_title', | |
453 | '$entry_guid', | |
454 | '$entry_link', | |
455 | '$entry_timestamp_fmt', | |
456 | '$entry_content', | |
457 | '$content_hash', | |
458 | $no_orig_date, | |
459 | NOW(), | |
11b0dce2 | 460 | '$entry_comments', |
b6104dee AD |
461 | '$num_comments', |
462 | '$entry_author')"); | |
8926aab8 AD |
463 | } else { |
464 | // we keep encountering the entry in feeds, so we need to | |
465 | // update date_entered column so that we don't get horrible | |
466 | // dupes when the entry gets purged and reinserted again e.g. | |
467 | // in the case of SLOW SLOW OMG SLOW updating feeds | |
468 | ||
469 | $base_entry_id = db_fetch_result($result, 0, "id"); | |
470 | ||
471 | db_query($link, "UPDATE ttrss_entries SET date_entered = NOW() | |
472 | WHERE id = '$base_entry_id'"); | |
4c193675 AD |
473 | } |
474 | ||
475 | // now it should exist, if not - bad luck then | |
476 | ||
6385315d AD |
477 | $result = db_query($link, "SELECT |
478 | id,content_hash,no_orig_date,title, | |
8926aab8 | 479 | substring(date_entered,1,19) as date_entered, |
11b0dce2 AD |
480 | substring(updated,1,19) as updated, |
481 | num_comments | |
6385315d AD |
482 | FROM |
483 | ttrss_entries | |
484 | WHERE guid = '$entry_guid'"); | |
4c193675 AD |
485 | |
486 | if (db_num_rows($result) == 1) { | |
487 | ||
11b0dce2 AD |
488 | // this will be used below in update handler |
489 | $orig_content_hash = db_fetch_result($result, 0, "content_hash"); | |
490 | $orig_title = db_fetch_result($result, 0, "title"); | |
491 | $orig_num_comments = db_fetch_result($result, 0, "num_comments"); | |
8926aab8 AD |
492 | $orig_date_entered = strtotime(db_fetch_result($result, |
493 | 0, "date_entered")); | |
6385315d | 494 | |
11b0dce2 | 495 | $ref_id = db_fetch_result($result, 0, "id"); |
4c193675 | 496 | |
11b0dce2 | 497 | // check for user post link to main table |
4c193675 | 498 | |
11b0dce2 AD |
499 | // do we allow duplicate posts with same GUID in different feeds? |
500 | if (get_pref($link, "ALLOW_DUPLICATE_POSTS", $owner_uid)) { | |
501 | $dupcheck_qpart = "AND feed_id = '$feed'"; | |
502 | } else { | |
503 | $dupcheck_qpart = ""; | |
504 | } | |
71604ca4 | 505 | |
11b0dce2 | 506 | // error_reporting(0); |
19c9cb11 | 507 | |
11b0dce2 AD |
508 | $filter_name = get_filter_name($entry_title, $entry_content, |
509 | $entry_link, $filters); | |
19c9cb11 | 510 | |
11b0dce2 AD |
511 | if ($filter_name == "filter") { |
512 | continue; | |
513 | } | |
19c9cb11 | 514 | |
11b0dce2 | 515 | // error_reporting (DEFAULT_ERROR_LEVEL); |
3a933f22 | 516 | |
11b0dce2 AD |
517 | $result = db_query($link, |
518 | "SELECT ref_id FROM ttrss_user_entries WHERE | |
519 | ref_id = '$ref_id' AND owner_uid = '$owner_uid' | |
520 | $dupcheck_qpart"); | |
521 | ||
522 | // okay it doesn't exist - create user entry | |
523 | if (db_num_rows($result) == 0) { | |
524 | ||
525 | if ($filter_name != 'catchup') { | |
526 | $unread = 'true'; | |
527 | $last_read_qpart = 'NULL'; | |
528 | } else { | |
529 | $unread = 'false'; | |
530 | $last_read_qpart = 'NOW()'; | |
531 | } | |
dd7d3187 AD |
532 | |
533 | if ($filter_name == 'mark') { | |
534 | $marked = 'true'; | |
535 | } else { | |
536 | $marked = 'false'; | |
537 | } | |
19c9cb11 | 538 | |
11b0dce2 AD |
539 | $result = db_query($link, |
540 | "INSERT INTO ttrss_user_entries | |
dd7d3187 | 541 | (ref_id, owner_uid, feed_id, unread, last_read, marked) |
11b0dce2 | 542 | VALUES ('$ref_id', '$owner_uid', '$feed', $unread, |
dd7d3187 | 543 | $last_read_qpart, $marked)"); |
11b0dce2 AD |
544 | } |
545 | ||
6385315d AD |
546 | $post_needs_update = false; |
547 | ||
a2770077 | 548 | if (get_pref($link, "UPDATE_POST_ON_CHECKSUM_CHANGE", $owner_uid) && |
6385315d AD |
549 | ($content_hash != $orig_content_hash)) { |
550 | $post_needs_update = true; | |
551 | } | |
552 | ||
553 | if ($orig_title != $entry_title) { | |
554 | $post_needs_update = true; | |
555 | } | |
556 | ||
11b0dce2 AD |
557 | if ($orig_num_comments != $num_comments) { |
558 | $post_needs_update = true; | |
559 | } | |
560 | ||
6385315d AD |
561 | // this doesn't seem to be very reliable |
562 | // | |
563 | // if ($orig_timestamp != $entry_timestamp && !$orig_no_orig_date) { | |
564 | // $post_needs_update = true; | |
565 | // } | |
566 | ||
567 | // if post needs update, update it and mark all user entries | |
1c73bc0c | 568 | // linking to this post as updated |
6385315d AD |
569 | if ($post_needs_update) { |
570 | ||
571 | // print "<!-- post $orig_title needs update : $post_needs_update -->"; | |
572 | ||
6385315d | 573 | db_query($link, "UPDATE ttrss_entries |
11b0dce2 AD |
574 | SET title = '$entry_title', content = '$entry_content', |
575 | num_comments = '$num_comments' | |
6385315d AD |
576 | WHERE id = '$ref_id'"); |
577 | ||
578 | db_query($link, "UPDATE ttrss_user_entries | |
579 | SET last_read = null WHERE ref_id = '$ref_id' AND unread = false"); | |
580 | ||
581 | } | |
4c193675 AD |
582 | } |
583 | ||
44e241cb AD |
584 | db_query($link, "COMMIT"); |
585 | ||
eb36b4eb AD |
586 | /* taaaags */ |
587 | // <a href="http://technorati.com/tag/Xorg" rel="tag">Xorg</a>, // | |
588 | ||
05732aa0 | 589 | $entry_tags = null; |
eb36b4eb | 590 | |
372ced8b | 591 | preg_match_all("/<a.*?href=.http:\/\/.*?technorati.com\/tag\/([^\"\'>]+)/i", |
ee2c3050 AD |
592 | $entry_content_unescaped, $entry_tags); |
593 | ||
594 | // print "<br>$entry_title : $entry_content_unescaped<br>"; | |
595 | // print_r($entry_tags); | |
372ced8b | 596 | // print "<br>"; |
eb36b4eb AD |
597 | |
598 | $entry_tags = $entry_tags[1]; | |
599 | ||
600 | if (count($entry_tags) > 0) { | |
601 | ||
44e241cb AD |
602 | db_query($link, "BEGIN"); |
603 | ||
05732aa0 AD |
604 | $result = db_query($link, "SELECT id,int_id |
605 | FROM ttrss_entries,ttrss_user_entries | |
25da6909 | 606 | WHERE guid = '$entry_guid' |
05732aa0 | 607 | AND feed_id = '$feed' AND ref_id = id |
7fed1940 | 608 | AND owner_uid = '$owner_uid'"); |
eb36b4eb | 609 | |
fe99ab12 | 610 | if (db_num_rows($result) == 1) { |
eb36b4eb | 611 | |
fe99ab12 AD |
612 | $entry_id = db_fetch_result($result, 0, "id"); |
613 | $entry_int_id = db_fetch_result($result, 0, "int_id"); | |
614 | ||
615 | foreach ($entry_tags as $tag) { | |
616 | $tag = db_escape_string(strtolower($tag)); | |
31483fc1 AD |
617 | |
618 | $tag = str_replace("+", " ", $tag); | |
fe99ab12 AD |
619 | $tag = str_replace("technorati tag: ", "", $tag); |
620 | ||
621 | $result = db_query($link, "SELECT id FROM ttrss_tags | |
622 | WHERE tag_name = '$tag' AND post_int_id = '$entry_int_id' AND | |
623 | owner_uid = '$owner_uid' LIMIT 1"); | |
624 | ||
625 | // print db_fetch_result($result, 0, "id"); | |
626 | ||
627 | if ($result && db_num_rows($result) == 0) { | |
628 | ||
629 | // print "tagging $entry_id as $tag<br>"; | |
630 | ||
631 | db_query($link, "INSERT INTO ttrss_tags | |
632 | (owner_uid,tag_name,post_int_id) | |
633 | VALUES ('$owner_uid','$tag', '$entry_int_id')"); | |
634 | } | |
635 | } | |
eb36b4eb | 636 | } |
44e241cb | 637 | db_query($link, "COMMIT"); |
05732aa0 | 638 | } |
4c193675 | 639 | } |
40d13c28 | 640 | |
ab3d0b99 AD |
641 | db_query($link, "UPDATE ttrss_feeds |
642 | SET last_updated = NOW(), last_error = '' WHERE id = '$feed'"); | |
eb36b4eb | 643 | |
44e241cb | 644 | // db_query($link, "COMMIT"); |
dd8c76a9 | 645 | |
ab3d0b99 AD |
646 | } else { |
647 | $error_msg = db_escape_string(magpie_error()); | |
648 | db_query($link, | |
aa5f9f5f AD |
649 | "UPDATE ttrss_feeds SET last_error = '$error_msg', |
650 | last_updated = NOW() WHERE id = '$feed'"); | |
40d13c28 AD |
651 | } |
652 | ||
653 | } | |
654 | ||
f175937c | 655 | function print_select($id, $default, $values, $attributes = "") { |
79f3553b | 656 | print "<select name=\"$id\" id=\"$id\" $attributes>"; |
a0d53889 AD |
657 | foreach ($values as $v) { |
658 | if ($v == $default) | |
659 | $sel = " selected"; | |
660 | else | |
661 | $sel = ""; | |
662 | ||
663 | print "<option$sel>$v</option>"; | |
664 | } | |
665 | print "</select>"; | |
666 | } | |
40d13c28 | 667 | |
79f3553b AD |
668 | function print_select_hash($id, $default, $values, $attributes = "") { |
669 | print "<select name=\"$id\" id='$id' $attributes>"; | |
673d54ca AD |
670 | foreach (array_keys($values) as $v) { |
671 | if ($v == $default) | |
672 | $sel = "selected"; | |
673 | else | |
674 | $sel = ""; | |
675 | ||
676 | print "<option $sel value=\"$v\">".$values[$v]."</option>"; | |
677 | } | |
678 | ||
679 | print "</select>"; | |
680 | } | |
681 | ||
19c9cb11 | 682 | function get_filter_name($title, $content, $link, $filters) { |
e6155a06 AD |
683 | |
684 | if ($filters["title"]) { | |
19c9cb11 AD |
685 | foreach ($filters["title"] as $filter) { |
686 | $reg_exp = $filter["reg_exp"]; | |
687 | if (preg_match("/$reg_exp/i", $title)) { | |
688 | return $filter["action"]; | |
689 | } | |
e6155a06 AD |
690 | } |
691 | } | |
692 | ||
693 | if ($filters["content"]) { | |
19c9cb11 AD |
694 | foreach ($filters["content"] as $filter) { |
695 | $reg_exp = $filter["reg_exp"]; | |
696 | if (preg_match("/$reg_exp/i", $content)) { | |
697 | return $filter["action"]; | |
698 | } | |
e6155a06 AD |
699 | } |
700 | } | |
701 | ||
702 | if ($filters["both"]) { | |
703 | foreach ($filters["both"] as $filter) { | |
19c9cb11 AD |
704 | $reg_exp = $filter["reg_exp"]; |
705 | if (preg_match("/$reg_exp/i", $title) || | |
706 | preg_match("/$reg_exp/i", $content)) { | |
707 | return $filter["action"]; | |
708 | } | |
e6155a06 AD |
709 | } |
710 | } | |
711 | ||
3a933f22 | 712 | if ($filters["link"]) { |
19c9cb11 AD |
713 | $reg_exp = $filter["reg_exp"]; |
714 | foreach ($filters["link"] as $filter) { | |
715 | $reg_exp = $filter["reg_exp"]; | |
716 | if (preg_match("/$reg_exp/i", $link)) { | |
717 | return $filter["action"]; | |
718 | } | |
3a933f22 AD |
719 | } |
720 | } | |
721 | ||
e6155a06 AD |
722 | return false; |
723 | } | |
724 | ||
9323147e | 725 | function printFeedEntry($feed_id, $class, $feed_title, $unread, $icon_file, $link, |
fb1fb4ab | 726 | $rtl_content = false, $last_updated = false, $last_error = false) { |
254e0e4b AD |
727 | |
728 | if (file_exists($icon_file) && filesize($icon_file) > 0) { | |
023fe037 | 729 | $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"$icon_file\">"; |
254e0e4b | 730 | } else { |
023fe037 | 731 | $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"images/blank_icon.gif\">"; |
254e0e4b AD |
732 | } |
733 | ||
9323147e AD |
734 | if ($rtl_content) { |
735 | $rtl_tag = "dir=\"rtl\""; | |
736 | } else { | |
737 | $rtl_tag = "dir=\"ltr\""; | |
738 | } | |
739 | ||
78d5212c AD |
740 | $error_notify_msg = ""; |
741 | ||
fb1fb4ab AD |
742 | if ($last_error) { |
743 | $link_title = "Error: $last_error ($last_updated)"; | |
78d5212c | 744 | $error_notify_msg = "(Error)"; |
ad780e9c | 745 | } else if ($last_updated) { |
fb1fb4ab AD |
746 | $link_title = "Updated: $last_updated"; |
747 | } | |
748 | ||
749 | $feed = "<a title=\"$link_title\" id=\"FEEDL-$feed_id\" href=\"javascript:viewfeed('$feed_id', 0);\">$feed_title</a>"; | |
254e0e4b AD |
750 | |
751 | print "<li id=\"FEEDR-$feed_id\" class=\"$class\">"; | |
b619ff15 | 752 | if (get_pref($link, 'ENABLE_FEED_ICONS')) { |
254e0e4b AD |
753 | print "$feed_icon"; |
754 | } | |
755 | ||
9323147e | 756 | print "<span $rtl_tag id=\"FEEDN-$feed_id\">$feed</span>"; |
254e0e4b AD |
757 | |
758 | if ($unread != 0) { | |
759 | $fctr_class = ""; | |
760 | } else { | |
761 | $fctr_class = "class=\"invisible\""; | |
762 | } | |
763 | ||
9323147e | 764 | print " <span $rtl_tag $fctr_class id=\"FEEDCTR-$feed_id\"> |
254e0e4b | 765 | (<span id=\"FEEDU-$feed_id\">$unread</span>)</span>"; |
78d5212c AD |
766 | |
767 | if (get_pref($link, "EXTENDED_FEEDLIST")) { | |
768 | print "<div class=\"feedExtInfo\"> | |
769 | <span id=\"FLUPD-$feed_id\">$last_updated $error_notify_msg</span></div>"; | |
770 | } | |
771 | ||
254e0e4b AD |
772 | print "</li>"; |
773 | ||
774 | } | |
775 | ||
406d9489 AD |
776 | function getmicrotime() { |
777 | list($usec, $sec) = explode(" ",microtime()); | |
778 | return ((float)$usec + (float)$sec); | |
779 | } | |
780 | ||
77e96719 AD |
781 | function print_radio($id, $default, $values, $attributes = "") { |
782 | foreach ($values as $v) { | |
783 | ||
784 | if ($v == $default) | |
5da169d9 | 785 | $sel = "checked"; |
77e96719 | 786 | else |
5da169d9 AD |
787 | $sel = ""; |
788 | ||
789 | if ($v == "Yes") { | |
790 | $sel .= " value=\"1\""; | |
791 | } else { | |
792 | $sel .= " value=\"0\""; | |
793 | } | |
77e96719 | 794 | |
69654950 AD |
795 | print "<input class=\"noborder\" |
796 | type=\"radio\" $sel $attributes name=\"$id\"> $v "; | |
77e96719 AD |
797 | |
798 | } | |
799 | } | |
800 | ||
ff485f1d AD |
801 | function initialize_user_prefs($link, $uid) { |
802 | ||
803 | $uid = db_escape_string($uid); | |
804 | ||
805 | db_query($link, "BEGIN"); | |
806 | ||
807 | $result = db_query($link, "SELECT pref_name,def_value FROM ttrss_prefs"); | |
808 | ||
809 | $u_result = db_query($link, "SELECT pref_name | |
810 | FROM ttrss_user_prefs WHERE owner_uid = '$uid'"); | |
811 | ||
812 | $active_prefs = array(); | |
813 | ||
814 | while ($line = db_fetch_assoc($u_result)) { | |
815 | array_push($active_prefs, $line["pref_name"]); | |
816 | } | |
817 | ||
818 | while ($line = db_fetch_assoc($result)) { | |
819 | if (array_search($line["pref_name"], $active_prefs) === FALSE) { | |
820 | // print "adding " . $line["pref_name"] . "<br>"; | |
821 | ||
822 | db_query($link, "INSERT INTO ttrss_user_prefs | |
823 | (owner_uid,pref_name,value) VALUES | |
824 | ('$uid', '".$line["pref_name"]."','".$line["def_value"]."')"); | |
825 | ||
826 | } | |
827 | } | |
828 | ||
829 | db_query($link, "COMMIT"); | |
830 | ||
831 | } | |
956c7629 AD |
832 | |
833 | function lookup_user_id($link, $user) { | |
834 | ||
835 | $result = db_query($link, "SELECT id FROM ttrss_users WHERE | |
836 | login = '$login'"); | |
837 | ||
838 | if (db_num_rows($result) == 1) { | |
839 | return db_fetch_result($result, 0, "id"); | |
840 | } else { | |
841 | return false; | |
842 | } | |
843 | } | |
844 | ||
c8437f35 AD |
845 | function authenticate_user($link, $login, $password) { |
846 | ||
131b01b3 | 847 | if (!SINGLE_USER_MODE) { |
c8437f35 | 848 | |
131b01b3 AD |
849 | $pwd_hash = 'SHA1:' . sha1($password); |
850 | ||
851 | $result = db_query($link, "SELECT id,login,access_level FROM ttrss_users WHERE | |
852 | login = '$login' AND pwd_hash = '$pwd_hash'"); | |
853 | ||
854 | if (db_num_rows($result) == 1) { | |
855 | $_SESSION["uid"] = db_fetch_result($result, 0, "id"); | |
856 | $_SESSION["name"] = db_fetch_result($result, 0, "login"); | |
857 | $_SESSION["access_level"] = db_fetch_result($result, 0, "access_level"); | |
858 | ||
859 | db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " . | |
860 | $_SESSION["uid"]); | |
861 | ||
862 | $user_theme = get_user_theme_path($link); | |
863 | ||
864 | $_SESSION["theme"] = $user_theme; | |
865 | $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"]; | |
866 | ||
867 | initialize_user_prefs($link, $_SESSION["uid"]); | |
868 | ||
869 | return true; | |
870 | } | |
871 | ||
872 | return false; | |
503eb349 | 873 | |
131b01b3 | 874 | } else { |
503eb349 | 875 | |
131b01b3 AD |
876 | $_SESSION["uid"] = 1; |
877 | $_SESSION["name"] = "admin"; | |
f557cd78 | 878 | |
0bbba72d AD |
879 | $user_theme = get_user_theme_path($link); |
880 | ||
881 | $_SESSION["theme"] = $user_theme; | |
882 | $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"]; | |
883 | ||
884 | initialize_user_prefs($link, $_SESSION["uid"]); | |
885 | ||
c8437f35 AD |
886 | return true; |
887 | } | |
c8437f35 AD |
888 | } |
889 | ||
e6cb77a0 AD |
890 | function make_password($length = 8) { |
891 | ||
892 | $password = ""; | |
798f722b AD |
893 | $possible = "0123456789abcdfghjkmnpqrstvwxyzABCDFGHJKMNPQRSTVWXYZ"; |
894 | ||
895 | $i = 0; | |
e6cb77a0 AD |
896 | |
897 | while ($i < $length) { | |
898 | $char = substr($possible, mt_rand(0, strlen($possible)-1), 1); | |
899 | ||
900 | if (!strstr($password, $char)) { | |
901 | $password .= $char; | |
902 | $i++; | |
903 | } | |
904 | } | |
905 | return $password; | |
906 | } | |
907 | ||
908 | // this is called after user is created to initialize default feeds, labels | |
909 | // or whatever else | |
910 | ||
911 | // user preferences are checked on every login, not here | |
912 | ||
913 | function initialize_user($link, $uid) { | |
914 | ||
915 | db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description) | |
916 | values ('$uid','unread = true', 'Unread articles')"); | |
917 | ||
918 | db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description) | |
919 | values ('$uid','last_read is null and unread = false', 'Updated articles')"); | |
920 | ||
921 | db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url) | |
74bff337 | 922 | values ('$uid', 'Tiny Tiny RSS: New Releases', |
628fcd2c | 923 | 'http://tt-rss.spb.ru/releases.rss')"); |
3b0feb9b AD |
924 | |
925 | } | |
e6cb77a0 | 926 | |
b8aa49bc | 927 | function logout_user() { |
5ccc1cf5 AD |
928 | session_destroy(); |
929 | if (isset($_COOKIE[session_name()])) { | |
930 | setcookie(session_name(), '', time()-42000, '/'); | |
931 | } | |
b8aa49bc AD |
932 | } |
933 | ||
75836f33 | 934 | function get_script_urlpath() { |
87a79fa4 | 935 | return preg_replace('/\/[^\/]*$/', "", $_SERVER["REQUEST_URI"]); |
75836f33 AD |
936 | } |
937 | ||
938 | function get_login_redirect() { | |
939 | $server = $_SERVER["SERVER_NAME"]; | |
940 | ||
941 | if (ENABLE_LOGIN_SSL) { | |
942 | $protocol = "https"; | |
943 | } else { | |
944 | $protocol = "http"; | |
945 | } | |
946 | ||
947 | $url_path = get_script_urlpath(); | |
948 | ||
949 | $redirect_uri = "$protocol://$server$url_path/login.php"; | |
950 | ||
951 | return $redirect_uri; | |
952 | } | |
953 | ||
916f788a | 954 | function validate_session($link) { |
a2e9b457 | 955 | if (SESSION_CHECK_ADDRESS && $_SESSION["uid"]) { |
916f788a AD |
956 | if ($_SESSION["ip_address"]) { |
957 | if ($_SESSION["ip_address"] != $_SERVER["REMOTE_ADDR"]) { | |
958 | return false; | |
959 | } | |
960 | } | |
961 | } | |
962 | return true; | |
963 | } | |
964 | ||
7ae65adf AD |
965 | function basic_nosid_redirect_check() { |
966 | if (!SINGLE_USER_MODE) { | |
3dd46f19 | 967 | if (!$_COOKIE[get_session_cookie_name()]) { |
7ae65adf AD |
968 | $redirect_uri = get_login_redirect(); |
969 | $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]); | |
970 | header("Location: $redirect_uri?rt=$return_to"); | |
971 | exit; | |
972 | } | |
973 | } | |
974 | } | |
975 | ||
b8aa49bc AD |
976 | function login_sequence($link) { |
977 | if (!SINGLE_USER_MODE) { | |
75836f33 | 978 | |
916f788a AD |
979 | if (!validate_session($link)) { |
980 | logout_user(); | |
981 | $redirect_uri = get_login_redirect(); | |
982 | $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]); | |
983 | header("Location: $redirect_uri?rt=$return_to"); | |
984 | exit; | |
985 | } | |
986 | ||
b8aa49bc AD |
987 | if (!USE_HTTP_AUTH) { |
988 | if (!$_SESSION["uid"]) { | |
75836f33 | 989 | $redirect_uri = get_login_redirect(); |
e31dca14 AD |
990 | $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]); |
991 | header("Location: $redirect_uri?rt=$return_to"); | |
b8aa49bc AD |
992 | exit; |
993 | } | |
994 | } else { | |
f557cd78 AD |
995 | if (!$_SESSION["uid"]) { |
996 | if (!$_SERVER["PHP_AUTH_USER"]) { | |
997 | ||
998 | header('WWW-Authenticate: Basic realm="Tiny Tiny RSS"'); | |
999 | header('HTTP/1.0 401 Unauthorized'); | |
1000 | exit; | |
1001 | ||
1002 | } else { | |
1003 | $auth_result = authenticate_user($link, | |
1004 | $_SERVER["PHP_AUTH_USER"], $_SERVER["PHP_AUTH_PW"]); | |
1005 | ||
1006 | if (!$auth_result) { | |
1007 | header('WWW-Authenticate: Basic realm="Tiny Tiny RSS"'); | |
1008 | header('HTTP/1.0 401 Unauthorized'); | |
1009 | exit; | |
1010 | } | |
1011 | } | |
1012 | } | |
b8aa49bc AD |
1013 | } |
1014 | } else { | |
0bbba72d | 1015 | return authenticate_user($link, "admin", null); |
b8aa49bc AD |
1016 | } |
1017 | } | |
3547842a AD |
1018 | |
1019 | function truncate_string($str, $max_len) { | |
12db369c AD |
1020 | if (mb_strlen($str, "utf-8") > $max_len - 3) { |
1021 | return mb_substr($str, 0, $max_len, "utf-8") . "..."; | |
3547842a AD |
1022 | } else { |
1023 | return $str; | |
1024 | } | |
1025 | } | |
54a60e1a AD |
1026 | |
1027 | function get_user_theme_path($link) { | |
798f722b AD |
1028 | $result = db_query($link, "SELECT theme_path |
1029 | FROM | |
1030 | ttrss_themes,ttrss_users | |
1031 | WHERE ttrss_themes.id = theme_id AND ttrss_users.id = " . $_SESSION["uid"]); | |
54a60e1a AD |
1032 | if (db_num_rows($result) != 0) { |
1033 | return db_fetch_result($result, 0, "theme_path"); | |
1034 | } else { | |
1035 | return null; | |
1036 | } | |
1037 | } | |
be773442 AD |
1038 | |
1039 | function smart_date_time($timestamp) { | |
1040 | if (date("Y.m.d", $timestamp) == date("Y.m.d")) { | |
1041 | return date("G:i", $timestamp); | |
f26450f1 | 1042 | } else if (date("Y", $timestamp) == date("Y")) { |
be773442 AD |
1043 | return date("M d, G:i", $timestamp); |
1044 | } else { | |
b02111c2 | 1045 | return date("Y/m/d G:i", $timestamp); |
be773442 AD |
1046 | } |
1047 | } | |
1048 | ||
1049 | function smart_date($timestamp) { | |
1050 | if (date("Y.m.d", $timestamp) == date("Y.m.d")) { | |
1051 | return "Today"; | |
f26450f1 | 1052 | } else if (date("Y", $timestamp) == date("Y")) { |
be773442 AD |
1053 | return date("D m", $timestamp); |
1054 | } else { | |
b02111c2 | 1055 | return date("Y/m/d", $timestamp); |
be773442 AD |
1056 | } |
1057 | } | |
a654a595 AD |
1058 | |
1059 | function sql_bool_to_string($s) { | |
1060 | if ($s == "t" || $s == "1") { | |
1061 | return "true"; | |
1062 | } else { | |
1063 | return "false"; | |
1064 | } | |
1065 | } | |
e3c99f3b AD |
1066 | |
1067 | function sql_bool_to_bool($s) { | |
1068 | if ($s == "t" || $s == "1") { | |
1069 | return true; | |
1070 | } else { | |
1071 | return false; | |
1072 | } | |
1073 | } | |
0ea4fb50 | 1074 | |
e3c99f3b | 1075 | |
0ea4fb50 AD |
1076 | function toggleEvenOdd($a) { |
1077 | if ($a == "even") | |
1078 | return "odd"; | |
1079 | else | |
1080 | return "even"; | |
1081 | } | |
6043fb7e AD |
1082 | |
1083 | function sanity_check($link) { | |
9cbca41f | 1084 | |
6043fb7e AD |
1085 | $error_code = 0; |
1086 | $result = db_query($link, "SELECT schema_version FROM ttrss_version"); | |
1087 | $schema_version = db_fetch_result($result, 0, "schema_version"); | |
1088 | ||
1089 | if ($schema_version != SCHEMA_VERSION) { | |
1090 | $error_code = 5; | |
1091 | } | |
1092 | ||
6043fb7e | 1093 | if ($error_code != 0) { |
3560a0be | 1094 | print_error_xml(5); |
6043fb7e AD |
1095 | return false; |
1096 | } else { | |
1097 | return true; | |
9cbca41f | 1098 | } |
6043fb7e AD |
1099 | } |
1100 | ||
27981ca3 AD |
1101 | function file_is_locked($filename) { |
1102 | error_reporting(0); | |
1103 | $fp = fopen($filename, "r"); | |
1104 | error_reporting(DEFAULT_ERROR_LEVEL); | |
1105 | if ($fp) { | |
1106 | if (flock($fp, LOCK_EX | LOCK_NB)) { | |
1107 | flock($fp, LOCK_UN); | |
1108 | fclose($fp); | |
1109 | return false; | |
1110 | } | |
1111 | fclose($fp); | |
1112 | return true; | |
1113 | } | |
1114 | return false; | |
1115 | } | |
1116 | ||
fcb4c0c9 AD |
1117 | function make_lockfile($filename) { |
1118 | $fp = fopen($filename, "w"); | |
1119 | ||
1120 | if (flock($fp, LOCK_EX | LOCK_NB)) { | |
1121 | return $fp; | |
1122 | } else { | |
1123 | return false; | |
1124 | } | |
1125 | } | |
1126 | ||
894ebcf5 AD |
1127 | function sql_random_function() { |
1128 | if (DB_TYPE == "mysql") { | |
1129 | return "RAND()"; | |
1130 | } else { | |
1131 | return "RANDOM()"; | |
1132 | } | |
1133 | } | |
1134 | ||
23aa0d16 AD |
1135 | function catchup_feed($link, $feed, $cat_view) { |
1136 | if (preg_match("/^[0-9][0-9]*$/", $feed) != false && $feed >= 0) { | |
1137 | ||
1138 | if ($cat_view) { | |
1139 | ||
1140 | if ($feed > 0) { | |
1141 | $cat_qpart = "cat_id = '$feed'"; | |
1142 | } else { | |
1143 | $cat_qpart = "cat_id IS NULL"; | |
1144 | } | |
1145 | ||
1146 | $tmp_result = db_query($link, "SELECT id | |
1147 | FROM ttrss_feeds WHERE $cat_qpart AND owner_uid = " . | |
1148 | $_SESSION["uid"]); | |
1149 | ||
1150 | while ($tmp_line = db_fetch_assoc($tmp_result)) { | |
1151 | ||
1152 | $tmp_feed = $tmp_line["id"]; | |
1153 | ||
1154 | db_query($link, "UPDATE ttrss_user_entries | |
1155 | SET unread = false,last_read = NOW() | |
1156 | WHERE feed_id = '$tmp_feed' AND owner_uid = " . $_SESSION["uid"]); | |
1157 | } | |
1158 | ||
1159 | } else if ($feed > 0) { | |
1160 | ||
1161 | $tmp_result = db_query($link, "SELECT id | |
1162 | FROM ttrss_feeds WHERE parent_feed = '$feed' | |
1163 | ORDER BY cat_id,title"); | |
1164 | ||
1165 | $parent_ids = array(); | |
1166 | ||
1167 | if (db_num_rows($tmp_result) > 0) { | |
1168 | while ($p = db_fetch_assoc($tmp_result)) { | |
1169 | array_push($parent_ids, "feed_id = " . $p["id"]); | |
1170 | } | |
1171 | ||
1172 | $children_qpart = implode(" OR ", $parent_ids); | |
1173 | ||
1174 | db_query($link, "UPDATE ttrss_user_entries | |
1175 | SET unread = false,last_read = NOW() | |
1176 | WHERE (feed_id = '$feed' OR $children_qpart) | |
1177 | AND owner_uid = " . $_SESSION["uid"]); | |
1178 | ||
1179 | } else { | |
1180 | db_query($link, "UPDATE ttrss_user_entries | |
1181 | SET unread = false,last_read = NOW() | |
1182 | WHERE feed_id = '$feed' AND owner_uid = " . $_SESSION["uid"]); | |
1183 | } | |
1184 | ||
1185 | } else if ($feed < 0 && $feed > -10) { // special, like starred | |
1186 | ||
1187 | if ($feed == -1) { | |
1188 | db_query($link, "UPDATE ttrss_user_entries | |
1189 | SET unread = false,last_read = NOW() | |
1190 | WHERE marked = true AND owner_uid = ".$_SESSION["uid"]); | |
1191 | } | |
1192 | ||
1193 | } else if ($feed < -10) { // label | |
1194 | ||
1195 | // TODO make this more efficient | |
1196 | ||
1197 | $label_id = -$feed - 11; | |
1198 | ||
1199 | $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels | |
1200 | WHERE id = '$label_id'"); | |
1201 | ||
1202 | if ($tmp_result) { | |
1203 | $sql_exp = db_fetch_result($tmp_result, 0, "sql_exp"); | |
1204 | ||
1205 | db_query($link, "BEGIN"); | |
1206 | ||
1207 | $tmp2_result = db_query($link, | |
1208 | "SELECT | |
1209 | int_id | |
1210 | FROM | |
1211 | ttrss_user_entries,ttrss_entries | |
1212 | WHERE | |
1213 | ref_id = id AND | |
1214 | $sql_exp AND | |
1215 | owner_uid = " . $_SESSION["uid"]); | |
1216 | ||
1217 | while ($tmp_line = db_fetch_assoc($tmp2_result)) { | |
1218 | db_query($link, "UPDATE | |
1219 | ttrss_user_entries | |
1220 | SET | |
1221 | unread = false, last_read = NOW() | |
1222 | WHERE | |
1223 | int_id = " . $tmp_line["int_id"]); | |
1224 | } | |
1225 | ||
1226 | db_query($link, "COMMIT"); | |
1227 | ||
1228 | /* db_query($link, "UPDATE ttrss_user_entries,ttrss_entries | |
1229 | SET unread = false,last_read = NOW() | |
1230 | WHERE $sql_exp | |
1231 | AND ref_id = id | |
1232 | AND owner_uid = ".$_SESSION["uid"]); */ | |
1233 | } | |
1234 | } | |
1235 | } else { // tag | |
1236 | db_query($link, "BEGIN"); | |
1237 | ||
1238 | $tag_name = db_escape_string($feed); | |
1239 | ||
1240 | $result = db_query($link, "SELECT post_int_id FROM ttrss_tags | |
1241 | WHERE tag_name = '$tag_name' AND owner_uid = " . $_SESSION["uid"]); | |
1242 | ||
1243 | while ($line = db_fetch_assoc($result)) { | |
1244 | db_query($link, "UPDATE ttrss_user_entries SET | |
1245 | unread = false, last_read = NOW() | |
1246 | WHERE int_id = " . $line["post_int_id"]); | |
1247 | } | |
1248 | db_query($link, "COMMIT"); | |
1249 | } | |
1250 | } | |
1251 | ||
1252 | function update_generic_feed($link, $feed, $cat_view) { | |
1253 | if ($cat_view) { | |
1254 | ||
1255 | if ($feed > 0) { | |
1256 | $cat_qpart = "cat_id = '$feed'"; | |
1257 | } else { | |
1258 | $cat_qpart = "cat_id IS NULL"; | |
1259 | } | |
1260 | ||
1261 | $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds | |
1262 | WHERE $cat_qpart AND owner_uid = " . $_SESSION["uid"]); | |
1263 | ||
1264 | while ($tmp_line = db_fetch_assoc($tmp_result)) { | |
1265 | $feed_url = $tmp_line["feed_url"]; | |
1266 | update_rss_feed($link, $feed_url, $feed, ENABLE_UPDATE_DAEMON); | |
1267 | } | |
1268 | ||
1269 | } else { | |
1270 | $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds | |
1271 | WHERE id = '$feed'"); | |
1272 | $feed_url = db_fetch_result($tmp_result, 0, "feed_url"); | |
1273 | update_rss_feed($link, $feed_url, $feed, ENABLE_UPDATE_DAEMON); | |
1274 | } | |
1275 | } | |
a9cb1f83 AD |
1276 | |
1277 | function getAllCounters($link) { | |
1278 | getLabelCounters($link); | |
1279 | getFeedCounters($link); | |
1280 | getTagCounters($link); | |
1281 | getGlobalCounters($link); | |
1282 | if (get_pref($link, 'ENABLE_FEED_CATS')) { | |
1283 | getCategoryCounters($link); | |
1284 | } | |
1285 | } | |
1286 | ||
1287 | function getCategoryCounters($link) { | |
1288 | $result = db_query($link, "SELECT cat_id,SUM((SELECT COUNT(int_id) | |
1289 | FROM ttrss_user_entries WHERE feed_id = ttrss_feeds.id | |
1290 | AND unread = true)) AS unread FROM ttrss_feeds | |
1291 | WHERE | |
1292 | owner_uid = ".$_SESSION["uid"]." GROUP BY cat_id"); | |
1293 | ||
1294 | while ($line = db_fetch_assoc($result)) { | |
1295 | $line["cat_id"] = sprintf("%d", $line["cat_id"]); | |
1296 | print "<counter type=\"category\" id=\"".$line["cat_id"]."\" counter=\"". | |
1297 | $line["unread"]."\"/>"; | |
1298 | } | |
1299 | } | |
1300 | ||
1301 | function getFeedUnread($link, $feed) { | |
1302 | $n_feed = sprintf("%d", $feed); | |
1303 | ||
1304 | if ($n_feed == -1) { | |
1305 | $match_part = "marked = true"; | |
1306 | } else if ($feed > 0) { | |
1307 | $match_part = "feed_id = '$n_feed'"; | |
1308 | } else if ($feed < -10) { | |
1309 | $label_id = -$feed - 11; | |
1310 | ||
1311 | $result = db_query($link, "SELECT sql_exp FROM ttrss_labels WHERE | |
1312 | id = '$label_id' AND owner_uid = " . $_SESSION["uid"]); | |
1313 | ||
1314 | $match_part = db_fetch_result($result, 0, "sql_exp"); | |
1315 | } | |
1316 | ||
1317 | if ($match_part) { | |
1318 | ||
1319 | $result = db_query($link, "SELECT count(int_id) AS unread | |
1320 | FROM ttrss_user_entries | |
1321 | WHERE unread = true AND $match_part AND owner_uid = " . $_SESSION["uid"]); | |
1322 | ||
1323 | } else { | |
1324 | ||
1325 | $result = db_query($link, "SELECT COUNT(post_int_id) AS unread | |
1326 | FROM ttrss_tags,ttrss_user_entries | |
1327 | WHERE tag_name = '$feed' AND post_int_id = int_id AND unread = true AND | |
1328 | ttrss_tags.owner_uid = " . $_SESSION["uid"]); | |
1329 | } | |
1330 | ||
1331 | $unread = db_fetch_result($result, 0, "unread"); | |
1332 | return $unread; | |
1333 | } | |
1334 | ||
1335 | /* FIXME this needs reworking */ | |
1336 | ||
1337 | function getGlobalUnread($link) { | |
1338 | $result = db_query($link, "SELECT count(id) as c_id FROM ttrss_entries,ttrss_user_entries | |
1339 | WHERE unread = true AND | |
1340 | ttrss_user_entries.ref_id = ttrss_entries.id AND | |
1341 | owner_uid = " . $_SESSION["uid"]); | |
1342 | $c_id = db_fetch_result($result, 0, "c_id"); | |
1343 | return $c_id; | |
1344 | } | |
1345 | ||
1346 | function getGlobalCounters($link, $global_unread = -1) { | |
1347 | if ($global_unread == -1) { | |
1348 | $global_unread = getGlobalUnread($link); | |
1349 | } | |
1350 | print "<counter type=\"global\" id='global-unread' counter='$global_unread'/>"; | |
1351 | } | |
1352 | ||
1353 | function getTagCounters($link, $smart_mode = SMART_RPC_COUNTERS) { | |
1354 | ||
1355 | if ($smart_mode) { | |
1356 | if (!$_SESSION["tctr_last_value"]) { | |
1357 | $_SESSION["tctr_last_value"] = array(); | |
1358 | } | |
1359 | } | |
1360 | ||
1361 | $old_counters = $_SESSION["tctr_last_value"]; | |
1362 | ||
1363 | $tctrs_modified = false; | |
1364 | ||
1365 | /* $result = db_query($link, "SELECT tag_name,count(ttrss_entries.id) AS count | |
1366 | FROM ttrss_tags,ttrss_entries,ttrss_user_entries WHERE | |
1367 | ttrss_user_entries.ref_id = ttrss_entries.id AND | |
1368 | ttrss_tags.owner_uid = ".$_SESSION["uid"]." AND | |
1369 | post_int_id = ttrss_user_entries.int_id AND unread = true GROUP BY tag_name | |
1370 | UNION | |
1371 | select tag_name,0 as count FROM ttrss_tags | |
1372 | WHERE ttrss_tags.owner_uid = ".$_SESSION["uid"]); */ | |
1373 | ||
1374 | $result = db_query($link, "SELECT tag_name,SUM((SELECT COUNT(int_id) | |
1375 | FROM ttrss_user_entries WHERE int_id = post_int_id | |
1376 | AND unread = true)) AS count FROM ttrss_tags | |
1377 | WHERE owner_uid = 2 GROUP BY tag_name ORDER BY tag_name"); | |
1378 | ||
1379 | $tags = array(); | |
1380 | ||
1381 | while ($line = db_fetch_assoc($result)) { | |
1382 | $tags[$line["tag_name"]] += $line["count"]; | |
1383 | } | |
1384 | ||
1385 | foreach (array_keys($tags) as $tag) { | |
1386 | $unread = $tags[$tag]; | |
1387 | ||
1388 | $tag = htmlspecialchars($tag); | |
1389 | ||
1390 | if (!$smart_mode || $old_counters[$tag] != $unread) { | |
1391 | $old_counters[$tag] = $unread; | |
1392 | $tctrs_modified = true; | |
1393 | print "<counter type=\"tag\" id=\"$tag\" counter=\"$unread\"/>"; | |
1394 | } | |
1395 | ||
1396 | } | |
1397 | ||
1398 | if ($smart_mode && $tctrs_modified) { | |
1399 | $_SESSION["tctr_last_value"] = $old_counters; | |
1400 | } | |
1401 | ||
1402 | } | |
1403 | ||
1404 | function getLabelCounters($link, $smart_mode = SMART_RPC_COUNTERS) { | |
1405 | ||
1406 | if ($smart_mode) { | |
1407 | if (!$_SESSION["lctr_last_value"]) { | |
1408 | $_SESSION["lctr_last_value"] = array(); | |
1409 | } | |
1410 | } | |
1411 | ||
1412 | $old_counters = $_SESSION["lctr_last_value"]; | |
1413 | $lctrs_modified = false; | |
1414 | ||
1415 | $result = db_query($link, "SELECT count(id) as count FROM ttrss_entries,ttrss_user_entries | |
1416 | WHERE marked = true AND ttrss_user_entries.ref_id = ttrss_entries.id AND | |
1417 | unread = true AND owner_uid = ".$_SESSION["uid"]); | |
1418 | ||
1419 | $count = db_fetch_result($result, 0, "count"); | |
1420 | ||
1421 | print "<counter type=\"label\" id=\"-1\" counter=\"$count\"/>"; | |
1422 | ||
1423 | $result = db_query($link, "SELECT owner_uid,id,sql_exp,description FROM | |
1424 | ttrss_labels WHERE owner_uid = ".$_SESSION["uid"]." ORDER by description"); | |
1425 | ||
1426 | while ($line = db_fetch_assoc($result)) { | |
1427 | ||
1428 | $id = -$line["id"] - 11; | |
1429 | ||
1430 | error_reporting (0); | |
1431 | ||
1432 | $tmp_result = db_query($link, "SELECT count(id) as count FROM ttrss_user_entries,ttrss_entries | |
1433 | WHERE (" . $line["sql_exp"] . ") AND unread = true AND | |
1434 | ttrss_user_entries.ref_id = ttrss_entries.id AND | |
1435 | owner_uid = ".$_SESSION["uid"]); | |
1436 | ||
1437 | $count = db_fetch_result($tmp_result, 0, "count"); | |
1438 | ||
1439 | if (!$smart_mode || $old_counters[$id] != $count) { | |
1440 | $old_counters[$id] = $count; | |
1441 | $lctrs_modified = true; | |
1442 | print "<counter type=\"label\" id=\"$id\" counter=\"$count\"/>"; | |
1443 | } | |
1444 | ||
1445 | error_reporting (DEFAULT_ERROR_LEVEL); | |
1446 | } | |
1447 | ||
1448 | if ($smart_mode && $lctrs_modified) { | |
1449 | $_SESSION["lctr_last_value"] = $old_counters; | |
1450 | } | |
1451 | } | |
1452 | ||
1453 | /* function getFeedCounter($link, $id) { | |
1454 | ||
1455 | $result = db_query($link, "SELECT | |
1456 | count(id) as count,last_error | |
1457 | FROM ttrss_entries,ttrss_user_entries,ttrss_feeds | |
1458 | WHERE feed_id = '$id' AND unread = true | |
1459 | AND ttrss_user_entries.feed_id = ttrss_feeds.id | |
1460 | AND ttrss_user_entries.ref_id = ttrss_entries.id"); | |
1461 | ||
1462 | $count = db_fetch_result($result, 0, "count"); | |
1463 | $last_error = htmlspecialchars(db_fetch_result($result, 0, "last_error")); | |
1464 | ||
1465 | print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" error=\"$last_error\"/>"; | |
1466 | } */ | |
1467 | ||
1468 | function getFeedCounters($link, $smart_mode = SMART_RPC_COUNTERS) { | |
1469 | ||
1470 | if ($smart_mode) { | |
1471 | if (!$_SESSION["fctr_last_value"]) { | |
1472 | $_SESSION["fctr_last_value"] = array(); | |
1473 | } | |
1474 | } | |
1475 | ||
1476 | $old_counters = $_SESSION["fctr_last_value"]; | |
1477 | ||
1478 | $result = db_query($link, "SELECT id,last_error,parent_feed, | |
fb1fb4ab | 1479 | SUBSTRING(last_updated,1,19) AS last_updated, |
a9cb1f83 AD |
1480 | (SELECT count(id) |
1481 | FROM ttrss_entries,ttrss_user_entries | |
1482 | WHERE feed_id = ttrss_feeds.id AND | |
1483 | ttrss_user_entries.ref_id = ttrss_entries.id | |
1484 | AND unread = true AND owner_uid = ".$_SESSION["uid"].") as count | |
1485 | FROM ttrss_feeds WHERE owner_uid = ".$_SESSION["uid"] . " | |
1486 | AND parent_feed IS NULL"); | |
1487 | ||
1488 | $fctrs_modified = false; | |
1489 | ||
fb1fb4ab AD |
1490 | $short_date = get_pref($link, 'SHORT_DATE_FORMAT'); |
1491 | ||
a9cb1f83 AD |
1492 | while ($line = db_fetch_assoc($result)) { |
1493 | ||
1494 | $id = $line["id"]; | |
1495 | $count = $line["count"]; | |
1496 | $last_error = htmlspecialchars($line["last_error"]); | |
fb1fb4ab AD |
1497 | |
1498 | if (get_pref($link, 'HEADLINES_SMART_DATE')) { | |
1499 | $last_updated = smart_date_time(strtotime($line["last_updated"])); | |
1500 | } else { | |
1501 | $last_updated = date($short_date, strtotime($line["last_updated"])); | |
1502 | } | |
1503 | ||
a9cb1f83 AD |
1504 | $has_img = is_file(ICONS_DIR . "/$id.ico"); |
1505 | ||
1506 | $tmp_result = db_query($link, | |
1507 | "SELECT id,COUNT(unread) AS unread | |
1508 | FROM ttrss_feeds LEFT JOIN ttrss_user_entries | |
1509 | ON (ttrss_feeds.id = ttrss_user_entries.feed_id) | |
1510 | WHERE parent_feed = '$id' AND unread = true GROUP BY ttrss_feeds.id"); | |
1511 | ||
1512 | if (db_num_rows($tmp_result) > 0) { | |
1513 | while ($l = db_fetch_assoc($tmp_result)) { | |
1514 | $count += $l["unread"]; | |
1515 | } | |
1516 | } | |
1517 | ||
1518 | if (!$smart_mode || $old_counters[$id] != $count) { | |
1519 | $old_counters[$id] = $count; | |
1520 | $fctrs_modified = true; | |
1521 | ||
1522 | if ($last_error) { | |
1523 | $error_part = "error=\"$last_error\""; | |
1524 | } else { | |
1525 | $error_part = ""; | |
1526 | } | |
1527 | ||
1528 | if ($has_img) { | |
1529 | $has_img_part = "hi=\"$has_img\""; | |
1530 | } else { | |
1531 | $has_img_part = ""; | |
1532 | } | |
1533 | ||
fb1fb4ab | 1534 | print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" $has_img_part $error_part updated=\"$last_updated\"/>"; |
a9cb1f83 AD |
1535 | } |
1536 | } | |
1537 | ||
1538 | if ($smart_mode && $fctrs_modified) { | |
1539 | $_SESSION["fctr_last_value"] = $old_counters; | |
1540 | } | |
1541 | } | |
1542 | ||
1b758780 AD |
1543 | function get_script_dt_add() { |
1544 | if (strpos(VERSION, "99") === false) { | |
1545 | return VERSION; | |
1546 | } else { | |
1547 | return time(); | |
1548 | } | |
1549 | } | |
1550 | ||
6e7f8d26 AD |
1551 | function get_pgsql_version($link) { |
1552 | $result = db_query($link, "SELECT version() AS version"); | |
1553 | $version = split(" ", db_fetch_result($result, 0, "version")); | |
1554 | return $version[1]; | |
1555 | } | |
1556 | ||
af106b0e AD |
1557 | function print_error_xml($code, $add_msg = "") { |
1558 | global $ERRORS; | |
1559 | ||
1560 | $error_msg = $ERRORS[$code]; | |
1561 | ||
1562 | if ($add_msg) { | |
1563 | $error_msg = "$error_msg; $add_msg"; | |
1564 | } | |
1565 | ||
4c2abbc1 | 1566 | print "<rpc-reply>"; |
af106b0e | 1567 | print "<error error-code=\"$code\" error-msg=\"$error_msg\"/>"; |
4c2abbc1 | 1568 | print "</rpc-reply>"; |
af106b0e | 1569 | } |
956c7629 AD |
1570 | |
1571 | function subscribe_to_feed($link, $feed_link, $cat_id = 0) { | |
1572 | ||
1573 | if ($cat_id == "0" || !$cat_id) { | |
1574 | $cat_qpart = "NULL"; | |
1575 | } else { | |
1576 | $cat_qpart = "'$cat_id'"; | |
1577 | } | |
1578 | ||
1579 | $result = db_query($link, | |
1580 | "SELECT id FROM ttrss_feeds | |
1581 | WHERE feed_url = '$feed_link' AND owner_uid = ".$_SESSION["uid"]); | |
1582 | ||
1583 | if (db_num_rows($result) == 0) { | |
1584 | ||
1585 | $result = db_query($link, | |
1586 | "INSERT INTO ttrss_feeds (owner_uid,feed_url,title,cat_id) | |
1587 | VALUES ('".$_SESSION["uid"]."', '$feed_link', | |
1588 | '[Unknown]', $cat_qpart)"); | |
1589 | ||
1590 | $result = db_query($link, | |
1591 | "SELECT id FROM ttrss_feeds WHERE feed_url = '$feed_link' | |
1592 | AND owner_uid = " . $_SESSION["uid"]); | |
1593 | ||
1594 | $feed_id = db_fetch_result($result, 0, "id"); | |
1595 | ||
1596 | if ($feed_id) { | |
1597 | update_rss_feed($link, $feed_link, $feed_id, true); | |
1598 | } | |
1599 | ||
1600 | return true; | |
1601 | } else { | |
1602 | return false; | |
1603 | } | |
1604 | } | |
1605 | ||
673d54ca AD |
1606 | function print_feed_select($link, $id, $default_id = "", |
1607 | $attributes = "", $include_all_feeds = true) { | |
1608 | ||
79f3553b | 1609 | print "<select id=\"$id\" name=\"$id\" $attributes>"; |
673d54ca | 1610 | if ($include_all_feeds) { |
79f3553b | 1611 | print "<option value=\"0\">All feeds</option>"; |
673d54ca AD |
1612 | } |
1613 | ||
1614 | $result = db_query($link, "SELECT id,title FROM ttrss_feeds | |
1615 | WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title"); | |
1616 | ||
1617 | if (db_num_rows($result) > 0 && $include_all_feeds) { | |
1618 | print "<option disabled>--------</option>"; | |
1619 | } | |
1620 | ||
1621 | while ($line = db_fetch_assoc($result)) { | |
1622 | if ($line["id"] == $default_id) { | |
1623 | $is_selected = "selected"; | |
1624 | } else { | |
1625 | $is_selected = ""; | |
1626 | } | |
79f3553b | 1627 | printf("<option $is_selected value='%d'>%s</option>", |
07164479 | 1628 | $line["id"], htmlspecialchars(db_unescape_string($line["title"]))); |
673d54ca AD |
1629 | } |
1630 | ||
1631 | print "</select>"; | |
1632 | } | |
1633 | ||
1634 | function print_feed_cat_select($link, $id, $default_id = "", | |
1635 | $attributes = "", $include_all_cats = true) { | |
1636 | ||
79f3553b | 1637 | print "<select id=\"$id\" name=\"$id\" $attributes>"; |
673d54ca AD |
1638 | |
1639 | if ($include_all_cats) { | |
14f69488 | 1640 | print "<option value=\"0\">Uncategorized</option>"; |
673d54ca AD |
1641 | } |
1642 | ||
1643 | $result = db_query($link, "SELECT id,title FROM ttrss_feed_categories | |
1644 | WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title"); | |
1645 | ||
1646 | if (db_num_rows($result) > 0 && $include_all_cats) { | |
1647 | print "<option disabled>--------</option>"; | |
1648 | } | |
1649 | ||
1650 | while ($line = db_fetch_assoc($result)) { | |
1651 | if ($line["id"] == $default_id) { | |
1652 | $is_selected = "selected"; | |
1653 | } else { | |
1654 | $is_selected = ""; | |
1655 | } | |
14f69488 | 1656 | printf("<option $is_selected value='%d'>%s</option>", |
07164479 | 1657 | $line["id"], htmlspecialchars(db_unescape_string($line["title"]))); |
673d54ca AD |
1658 | } |
1659 | ||
1660 | print "</select>"; | |
1661 | } | |
1662 | ||
14f69488 AD |
1663 | function checkbox_to_sql_bool($val) { |
1664 | return ($val == "on") ? "true" : "false"; | |
1665 | } | |
86b682ce AD |
1666 | |
1667 | function getFeedCatTitle($link, $id) { | |
1668 | if ($id == -1) { | |
1669 | return "Special"; | |
1670 | } else if ($id < -10) { | |
1671 | return "Labels"; | |
1672 | } else if ($id > 0) { | |
1673 | $result = db_query($link, "SELECT ttrss_feed_categories.title | |
1674 | FROM ttrss_feeds, ttrss_feed_categories WHERE ttrss_feeds.id = '$id' AND | |
1675 | cat_id = ttrss_feed_categories.id"); | |
1676 | if (db_num_rows($result) == 1) { | |
1677 | return db_fetch_result($result, 0, "title"); | |
1678 | } else { | |
eb4311d0 | 1679 | return "Uncategorized"; |
86b682ce AD |
1680 | } |
1681 | } else { | |
1682 | return "getFeedCatTitle($id) failed"; | |
1683 | } | |
1684 | ||
1685 | } | |
1686 | ||
1687 | function getFeedTitle($link, $id) { | |
1688 | if ($id == -1) { | |
1689 | return "Starred articles"; | |
1690 | } else if ($id < -10) { | |
1691 | $label_id = -10 - $id; | |
1692 | $result = db_query($link, "SELECT description FROM ttrss_labels WHERE id = '$label_id'"); | |
1693 | if (db_num_rows($result) == 1) { | |
1694 | return db_fetch_result($result, 0, "description"); | |
1695 | } else { | |
1696 | return "Unknown label ($label_id)"; | |
1697 | } | |
1698 | ||
1699 | } else if ($id > 0) { | |
1700 | $result = db_query($link, "SELECT title FROM ttrss_feeds WHERE id = '$id'"); | |
1701 | if (db_num_rows($result) == 1) { | |
1702 | return db_fetch_result($result, 0, "title"); | |
1703 | } else { | |
1704 | return "Unknown feed ($id)"; | |
1705 | } | |
1706 | } else { | |
1707 | return "getFeedTitle($id) failed"; | |
1708 | } | |
1709 | ||
1710 | } | |
3dd46f19 AD |
1711 | |
1712 | function get_session_cookie_name() { | |
1713 | return ((!defined('TTRSS_SESSION_NAME')) ? "ttrss_sid" : TTRSS_SESSION_NAME); | |
1714 | } | |
3ac2b520 AD |
1715 | |
1716 | function print_init_params($link) { | |
1717 | print "<init-params>"; | |
1718 | if ($_SESSION["stored-params"]) { | |
1719 | foreach (array_keys($_SESSION["stored-params"]) as $key) { | |
1720 | $value = htmlspecialchars($_SESSION["stored-params"][$key]); | |
1721 | print "<param key=\"$key\" value=\"$value\"/>"; | |
1722 | } | |
1723 | } | |
1724 | ||
1725 | print "<param key=\"daemon_enabled\" value=\"" . ENABLE_UPDATE_DAEMON . "\"/>"; | |
1726 | print "<param key=\"feeds_frame_refresh\" value=\"" . FEEDS_FRAME_REFRESH . "\"/>"; | |
0d51e25d | 1727 | print "<param key=\"daemon_refresh_only\" value=\"" . DAEMON_REFRESH_ONLY . "\"/>"; |
3ac2b520 AD |
1728 | |
1729 | print "<param key=\"on_catchup_show_next_feed\" value=\"" . | |
1730 | get_pref($link, "ON_CATCHUP_SHOW_NEXT_FEED") . "\"/>"; | |
1731 | ||
e8bd0da9 AD |
1732 | print "<param key=\"hide_read_feeds\" value=\"" . |
1733 | sprintf("%d", get_pref($link, "HIDE_READ_FEEDS")) . "\"/>"; | |
1734 | ||
c9268ed5 AD |
1735 | print "<param key=\"feeds_sort_by_unread\" value=\"" . |
1736 | sprintf("%d", get_pref($link, "FEEDS_SORT_BY_UNREAD")) . "\"/>"; | |
1737 | ||
3ac2b520 AD |
1738 | print "</init-params>"; |
1739 | } | |
f54f515f AD |
1740 | |
1741 | function print_runtime_info($link) { | |
1742 | print "<runtime-info>"; | |
71ad883b AD |
1743 | if (ENABLE_UPDATE_DAEMON) { |
1744 | print "<param key=\"daemon_is_running\" value=\"". | |
1745 | sprintf("%d", file_is_locked("update_daemon.lock")) . "\"/>"; | |
1746 | } | |
f54f515f AD |
1747 | print "</runtime-info>"; |
1748 | } | |
40d13c28 | 1749 | ?> |