]>
Commit | Line | Data |
---|---|---|
40d13c28 | 1 | <? |
f1a80dae AD |
2 | session_start(); |
3 | ||
40d13c28 | 4 | require_once 'config.php'; |
b619ff15 | 5 | require_once 'db-prefs.php'; |
40d13c28 | 6 | |
1c7f75ed AD |
7 | // $_SESSION["uid"] = PLACEHOLDER_UID; // FIXME: placeholder |
8 | // $_SESSION["name"] = PLACEHOLDER_NAME; | |
06da450f | 9 | |
a3ee2a38 AD |
10 | define('MAGPIE_OUTPUT_ENCODING', 'UTF-8'); |
11 | ||
fefa6ca3 AD |
12 | function purge_feed($link, $feed_id, $purge_interval) { |
13 | ||
14 | if (DB_TYPE == "pgsql") { | |
15 | db_query($link, "DELETE FROM ttrss_entries WHERE | |
16 | marked = false AND feed_id = '$feed_id' AND | |
17 | date_entered < NOW() - INTERVAL '$purge_interval days'"); | |
18 | } else { | |
19 | db_query($link, "DELETE FROM ttrss_entries WHERE | |
20 | marked = false AND feed_id = '$feed_id' AND | |
21 | date_entered < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)"); | |
22 | } | |
23 | } | |
24 | ||
25 | function global_purge_old_posts($link, $do_output = false) { | |
26 | ||
27 | $result = db_query($link, | |
28 | "SELECT id,purge_interval,owner_uid FROM ttrss_feeds"); | |
29 | ||
30 | while ($line = db_fetch_assoc($result)) { | |
31 | ||
32 | $feed_id = $line["id"]; | |
33 | $purge_interval = $line["purge_interval"]; | |
34 | $owner_uid = $line["owner_uid"]; | |
35 | ||
36 | if ($purge_interval == 0) { | |
37 | ||
38 | $tmp_result = db_query($link, | |
39 | "SELECT value FROM ttrss_user_prefs WHERE | |
40 | pref_name = 'PURGE_OLD_DAYS' AND owner_uid = '$owner_uid'"); | |
41 | ||
42 | if (db_num_rows($tmp_result) != 0) { | |
43 | $purge_interval = db_fetch_result($tmp_result, 0, "value"); | |
44 | } | |
45 | } | |
46 | ||
47 | if ($do_output) { | |
48 | print "<feed id='$feed_id' p_intl='$purge_interval'/>"; | |
49 | } | |
50 | ||
51 | if ($purge_interval > 0) { | |
52 | purge_feed($link, $feed_id, $purge_interval); | |
53 | } | |
54 | } | |
55 | ||
56 | } | |
57 | ||
b6eefba5 | 58 | function purge_old_posts($link) { |
5d73494a | 59 | |
f1a80dae AD |
60 | $user_id = $_SESSION["uid"]; |
61 | ||
62 | $result = db_query($link, "SELECT id,purge_interval FROM ttrss_feeds | |
63 | WHERE owner_uid = '$user_id'"); | |
5d73494a AD |
64 | |
65 | while ($line = db_fetch_assoc($result)) { | |
66 | ||
67 | $feed_id = $line["id"]; | |
68 | $purge_interval = $line["purge_interval"]; | |
69 | ||
b619ff15 | 70 | if ($purge_interval == 0) $purge_interval = get_pref($link, 'PURGE_OLD_DAYS'); |
5d73494a | 71 | |
140aae81 | 72 | if ($purge_interval > 0) { |
fefa6ca3 | 73 | purge_feed($link, $feed_id, $purge_interval); |
5d73494a AD |
74 | } |
75 | } | |
c3a8d71a AD |
76 | } |
77 | ||
9c9c7e6b | 78 | function update_all_feeds($link, $fetch) { |
40d13c28 | 79 | |
4769ddaf | 80 | if (WEB_DEMO_MODE) return; |
b0b4abcf | 81 | |
b619ff15 | 82 | if (get_pref($link, 'DAEMON_REFRESH_ONLY')) { |
c5142cca AD |
83 | if (!$_GET["daemon"]) { |
84 | return; | |
85 | } | |
c70d731e AD |
86 | } |
87 | ||
b6eefba5 | 88 | db_query($link, "BEGIN"); |
b82af8c3 | 89 | |
f1a80dae AD |
90 | $user_id = $_SESSION["uid"]; |
91 | ||
d148926e AD |
92 | $result = db_query($link, "SELECT feed_url,id, |
93 | substring(last_updated,1,19) as last_updated, | |
f1a80dae | 94 | update_interval FROM ttrss_feeds WHERE owner_uid = '$user_id'"); |
40d13c28 | 95 | |
b6eefba5 | 96 | while ($line = db_fetch_assoc($result)) { |
d148926e AD |
97 | $upd_intl = $line["update_interval"]; |
98 | ||
b619ff15 AD |
99 | if (!$upd_intl || $upd_intl == 0) { |
100 | $upd_intl = get_pref($link, 'DEFAULT_UPDATE_INTERVAL'); | |
101 | } | |
d148926e | 102 | |
93d40f50 AD |
103 | if ($fetch || (!$line["last_updated"] || |
104 | time() - strtotime($line["last_updated"]) > ($upd_intl * 60))) { | |
c5142cca | 105 | |
8143ae1f | 106 | update_rss_feed($link, $line["feed_url"], $line["id"]); |
d148926e | 107 | } |
40d13c28 AD |
108 | } |
109 | ||
b6eefba5 | 110 | purge_old_posts($link); |
c3a8d71a | 111 | |
b6eefba5 | 112 | db_query($link, "COMMIT"); |
b82af8c3 | 113 | |
40d13c28 AD |
114 | } |
115 | ||
9e997874 | 116 | function check_feed_favicon($feed_url, $feed, $link) { |
78800912 AD |
117 | $feed_url = str_replace("http://", "", $feed_url); |
118 | $feed_url = preg_replace("/\/.*$/", "", $feed_url); | |
119 | ||
120 | $icon_url = "http://$feed_url/favicon.ico"; | |
273a2f6b | 121 | $icon_file = ICONS_DIR . "/$feed.ico"; |
78800912 AD |
122 | |
123 | if (!file_exists($icon_file)) { | |
e695fdc8 | 124 | |
78800912 AD |
125 | error_reporting(0); |
126 | $r = fopen($icon_url, "r"); | |
127 | error_reporting (E_ERROR | E_WARNING | E_PARSE); | |
128 | ||
129 | if ($r) { | |
130 | $tmpfname = tempnam("/tmp", "ttrssicon"); | |
131 | ||
132 | $t = fopen($tmpfname, "w"); | |
133 | ||
134 | while (!feof($r)) { | |
135 | $buf = fread($r, 16384); | |
136 | fwrite($t, $buf); | |
137 | } | |
138 | ||
139 | fclose($r); | |
140 | fclose($t); | |
141 | ||
e695fdc8 AD |
142 | error_reporting(0); |
143 | if (!rename($tmpfname, $icon_file)) { | |
144 | unlink($tmpfname); | |
145 | } | |
717f5e64 AD |
146 | |
147 | chmod($icon_file, 0644); | |
148 | ||
e695fdc8 | 149 | error_reporting (E_ERROR | E_WARNING | E_PARSE); |
78800912 AD |
150 | |
151 | } | |
152 | } | |
153 | } | |
154 | ||
40d13c28 AD |
155 | function update_rss_feed($link, $feed_url, $feed) { |
156 | ||
4769ddaf | 157 | if (WEB_DEMO_MODE) return; |
b0b4abcf | 158 | |
ab3d0b99 AD |
159 | $feed = db_escape_string($feed); |
160 | ||
3ad5aa85 | 161 | error_reporting(0); |
40d13c28 | 162 | $rss = fetch_rss($feed_url); |
ab3d0b99 | 163 | |
3ad5aa85 | 164 | error_reporting (E_ERROR | E_WARNING | E_PARSE); |
76798ff3 | 165 | |
b6eefba5 | 166 | db_query($link, "BEGIN"); |
b7f4bda2 | 167 | |
b6eefba5 | 168 | $feed = db_escape_string($feed); |
dcee8f61 | 169 | |
40d13c28 | 170 | if ($rss) { |
b82af8c3 | 171 | |
b619ff15 | 172 | if (get_pref($link, 'ENABLE_FEED_ICONS')) { |
9e997874 | 173 | check_feed_favicon($feed_url, $feed, $link); |
78800912 AD |
174 | } |
175 | ||
b6eefba5 | 176 | $result = db_query($link, "SELECT title,icon_url FROM ttrss_feeds WHERE id = '$feed'"); |
331900c6 | 177 | |
b6eefba5 AD |
178 | $registered_title = db_fetch_result($result, 0, "title"); |
179 | $orig_icon_url = db_fetch_result($result, 0, "icon_url"); | |
331900c6 AD |
180 | |
181 | if (!$registered_title) { | |
e1305a97 | 182 | $feed_title = db_escape_string($rss->channel["title"]); |
b6eefba5 | 183 | db_query($link, "UPDATE ttrss_feeds SET title = '$feed_title' WHERE id = '$feed'"); |
331900c6 | 184 | } |
40d13c28 | 185 | |
b7f4bda2 AD |
186 | // print "I: " . $rss->channel["image"]["url"]; |
187 | ||
188 | $icon_url = $rss->image["url"]; | |
189 | ||
190 | if ($icon_url && !$orig_icon_url) { | |
b6eefba5 AD |
191 | $icon_url = db_escape_string($icon_url); |
192 | db_query($link, "UPDATE ttrss_feeds SET icon_url = '$icon_url' WHERE id = '$feed'"); | |
b7f4bda2 AD |
193 | } |
194 | ||
e6155a06 AD |
195 | |
196 | $filters = array(); | |
197 | ||
4b3dff6e | 198 | $result = db_query($link, "SELECT reg_exp, |
e6155a06 AD |
199 | (SELECT name FROM ttrss_filter_types |
200 | WHERE id = filter_type) as name | |
06da450f | 201 | FROM ttrss_filters WHERE owner_uid = ".$_SESSION["uid"]); |
e6155a06 | 202 | |
b6eefba5 | 203 | while ($line = db_fetch_assoc($result)) { |
e6155a06 | 204 | if (!$filters[$line["name"]]) $filters[$line["name"]] = array(); |
4b3dff6e | 205 | array_push($filters[$line["name"]], $line["reg_exp"]); |
e6155a06 AD |
206 | } |
207 | ||
40d13c28 AD |
208 | foreach ($rss->items as $item) { |
209 | ||
210 | $entry_guid = $item["id"]; | |
211 | ||
212 | if (!$entry_guid) $entry_guid = $item["guid"]; | |
213 | if (!$entry_guid) $entry_guid = $item["link"]; | |
466001c4 AD |
214 | |
215 | if (!$entry_guid) continue; | |
a116f569 | 216 | |
9c9c7e6b | 217 | $entry_timestamp = ""; |
b82af8c3 | 218 | |
9c9c7e6b AD |
219 | $rss_2_date = $item['pubdate']; |
220 | $rss_1_date = $item['dc']['date']; | |
221 | $atom_date = $item['issued']; | |
b197f117 | 222 | |
9c9c7e6b AD |
223 | if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date); |
224 | if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date); | |
225 | if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date); | |
b82af8c3 AD |
226 | |
227 | if ($entry_timestamp == "") { | |
228 | $entry_timestamp = time(); | |
229 | $no_orig_date = 'true'; | |
466001c4 AD |
230 | } else { |
231 | $no_orig_date = 'false'; | |
b82af8c3 | 232 | } |
b197f117 | 233 | |
466001c4 | 234 | $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp); |
71ad3959 | 235 | |
40d13c28 AD |
236 | $entry_title = $item["title"]; |
237 | $entry_link = $item["link"]; | |
71ad3959 AD |
238 | |
239 | if (!$entry_title) continue; | |
240 | if (!$entry_link) continue; | |
241 | ||
1696229f AD |
242 | $entry_content = $item["content:escaped"]; |
243 | ||
244 | if (!$entry_content) $entry_content = $item["content:encoded"]; | |
40d13c28 | 245 | if (!$entry_content) $entry_content = $item["content"]; |
1696229f | 246 | if (!$entry_content) $entry_content = $item["description"]; |
a2015351 | 247 | |
a116f569 | 248 | // if (!$entry_content) continue; |
a2015351 | 249 | |
8add756a AD |
250 | // WTF |
251 | if (is_array($entry_content)) { | |
252 | $entry_content = $entry_content["encoded"]; | |
1696229f | 253 | if (!$entry_content) $entry_content = $entry_content["escaped"]; |
8add756a AD |
254 | } |
255 | ||
1696229f AD |
256 | // print_r($item); |
257 | // print_r($entry_content); | |
258 | ||
466001c4 | 259 | $content_hash = "SHA1:" . sha1(strip_tags($entry_content)); |
cb0bd8bd | 260 | |
a1ea1e12 AD |
261 | $entry_comments = $item["comments"]; |
262 | ||
b6eefba5 | 263 | $entry_guid = db_escape_string($entry_guid); |
2651fc4f | 264 | |
b6eefba5 | 265 | $result = db_query($link, " |
40d13c28 | 266 | SELECT |
ecb14114 AD |
267 | id,last_read,no_orig_date,title,feed_id,content_hash, |
268 | substring(updated,1,19) as updated | |
40d13c28 AD |
269 | FROM |
270 | ttrss_entries | |
271 | WHERE | |
4e90d6d2 AD |
272 | guid = '$entry_guid' AND |
273 | owner_uid = " . $_SESSION["uid"]." AND | |
274 | feed_id = '$feed'"); | |
466001c4 | 275 | |
1696229f AD |
276 | // print db_num_rows($result) . "$entry_guid<br/>"; |
277 | ||
b6eefba5 | 278 | if (db_num_rows($result) == 0) { |
466001c4 | 279 | |
e6155a06 AD |
280 | error_reporting(0); |
281 | if (is_filtered($entry_title, $entry_content, $filters)) { | |
282 | continue; | |
283 | } | |
284 | error_reporting (E_ERROR | E_WARNING | E_PARSE); | |
285 | ||
b6eefba5 AD |
286 | //$entry_guid = db_escape_string($entry_guid); |
287 | $entry_content = db_escape_string($entry_content); | |
288 | $entry_title = db_escape_string($entry_title); | |
289 | $entry_link = db_escape_string($entry_link); | |
290 | $entry_comments = db_escape_string($entry_comments); | |
466001c4 AD |
291 | |
292 | $query = "INSERT | |
293 | INTO ttrss_entries | |
294 | (title, | |
295 | guid, | |
296 | link, | |
297 | updated, | |
298 | content, | |
299 | content_hash, | |
300 | feed_id, | |
deaaa02c | 301 | comments, |
3b063a95 | 302 | no_orig_date, |
06da450f AD |
303 | date_entered, |
304 | owner_uid) | |
40d13c28 | 305 | VALUES |
466001c4 AD |
306 | ('$entry_title', |
307 | '$entry_guid', | |
308 | '$entry_link', | |
309 | '$entry_timestamp_fmt', | |
310 | '$entry_content', | |
311 | '$content_hash', | |
312 | '$feed', | |
a1ea1e12 | 313 | '$entry_comments', |
3b063a95 | 314 | $no_orig_date, |
06da450f | 315 | NOW(),".$_SESSION["uid"].")"; |
466001c4 | 316 | |
b6eefba5 | 317 | $result = db_query($link, $query); |
76798ff3 | 318 | |
40d13c28 | 319 | } else { |
466001c4 | 320 | |
b6eefba5 AD |
321 | $orig_entry_id = db_fetch_result($result, 0, "id"); |
322 | $orig_feed_id = db_fetch_result($result, 0, "feed_id"); | |
466001c4 | 323 | |
ecb14114 AD |
324 | // print "OED: $orig_entry_id; OID: $orig_feed_id ; FID: $feed<br>"; |
325 | ||
466001c4 | 326 | if ($orig_feed_id != $feed) { |
ecb14114 | 327 | // print "<p>GUID $entry_guid: update from different feed ($orig_feed_id, $feed): $entry_guid [$entry_title]"; |
466001c4 AD |
328 | continue; |
329 | } | |
ad3024fc AD |
330 | |
331 | $entry_is_modified = false; | |
466001c4 | 332 | |
ecb14114 AD |
333 | $orig_timestamp = strtotime(db_fetch_result($result, 0, "updated")); |
334 | ||
b6eefba5 AD |
335 | $orig_content_hash = db_fetch_result($result, 0, "content_hash"); |
336 | $orig_last_read = db_fetch_result($result, 0, "last_read"); | |
337 | $orig_no_orig_date = db_fetch_result($result, 0, "no_orig_date"); | |
338 | $orig_title = db_fetch_result($result, 0, "title"); | |
cac95b8d | 339 | |
2d84262b AD |
340 | $last_read_qpart = ""; |
341 | ||
ad3024fc | 342 | if ($orig_content_hash != $content_hash) { |
ecb14114 AD |
343 | // print "$orig_content_hash :: $content_hash<br>"; |
344 | ||
b619ff15 | 345 | if (get_pref($link, 'UPDATE_POST_ON_CHECKSUM_CHANGE')) { |
ad3024fc AD |
346 | $last_read_qpart = 'last_read = null,'; |
347 | } | |
348 | $entry_is_modified = true; | |
349 | } | |
350 | ||
351 | if ($orig_title != $entry_title) { | |
352 | $entry_is_modified = true; | |
353 | } | |
354 | ||
355 | if ($orig_timestamp != $entry_timestamp && !$orig_no_orig_date) { | |
356 | $entry_is_modified = true; | |
cac95b8d AD |
357 | } |
358 | ||
ad3024fc | 359 | if ($entry_is_modified) { |
a2015351 | 360 | |
ecb14114 AD |
361 | // print "$entry_guid Modified!<br>"; |
362 | ||
b6eefba5 AD |
363 | $entry_comments = db_escape_string($entry_comments); |
364 | $entry_content = db_escape_string($entry_content); | |
365 | $entry_title = db_escape_string($entry_title); | |
366 | $entry_link = db_escape_string($entry_link); | |
a2015351 | 367 | |
ad3024fc AD |
368 | $query = "UPDATE ttrss_entries |
369 | SET | |
370 | $last_read_qpart | |
371 | title = '$entry_title', | |
372 | link = '$entry_link', | |
373 | updated = '$entry_timestamp_fmt', | |
374 | content = '$entry_content', | |
375 | comments = '$entry_comments', | |
376 | content_hash = '$content_hash' | |
377 | WHERE | |
378 | id = '$orig_entry_id'"; | |
379 | ||
b6eefba5 | 380 | $result = db_query($link, $query); |
ad3024fc | 381 | } |
466001c4 | 382 | } |
40d13c28 | 383 | |
eb36b4eb AD |
384 | /* taaaags */ |
385 | // <a href="http://technorati.com/tag/Xorg" rel="tag">Xorg</a>, // | |
386 | ||
387 | $entry_tags = null; | |
388 | ||
389 | preg_match_all("/<a.*?rel=.tag.*?>([^>]+)<\/a>/i", $entry_content, | |
390 | $entry_tags); | |
391 | ||
392 | $entry_tags = $entry_tags[1]; | |
393 | ||
394 | if (count($entry_tags) > 0) { | |
395 | ||
396 | $result = db_query($link, "SELECT id FROM ttrss_entries | |
25da6909 AD |
397 | WHERE guid = '$entry_guid' |
398 | AND feed_id = '$feed' | |
399 | AND owner_uid = " . $_SESSION["uid"]); | |
eb36b4eb AD |
400 | |
401 | if (!$result || db_num_rows($result) != 1) { | |
402 | return; | |
403 | } | |
404 | ||
405 | $entry_id = db_fetch_result($result, 0, "id"); | |
ab15e65d | 406 | |
eb36b4eb AD |
407 | foreach ($entry_tags as $tag) { |
408 | $tag = db_escape_string(strtolower($tag)); | |
409 | ||
3b0948c4 AD |
410 | $tag = str_replace("technorati tag: ", "", $tag); |
411 | ||
eb36b4eb | 412 | $result = db_query($link, "SELECT id FROM ttrss_tags |
ab15e65d AD |
413 | WHERE tag_name = '$tag' AND post_id = '$entry_id' AND |
414 | owner_uid = ".$_SESSION["uid"]." LIMIT 1"); | |
415 | ||
416 | // print db_fetch_result($result, 0, "id"); | |
eb36b4eb AD |
417 | |
418 | if ($result && db_num_rows($result) == 0) { | |
419 | ||
420 | // print "tagging $entry_id as $tag<br>"; | |
421 | ||
06da450f AD |
422 | db_query($link, "INSERT INTO ttrss_tags (owner_uid,tag_name,post_id) |
423 | VALUES ('".$_SESSION["uid"]."','$tag', '$entry_id')"); | |
eb36b4eb AD |
424 | } |
425 | } | |
426 | } | |
76798ff3 | 427 | } |
40d13c28 | 428 | |
ab3d0b99 AD |
429 | db_query($link, "UPDATE ttrss_feeds |
430 | SET last_updated = NOW(), last_error = '' WHERE id = '$feed'"); | |
eb36b4eb | 431 | |
ab3d0b99 AD |
432 | } else { |
433 | $error_msg = db_escape_string(magpie_error()); | |
434 | db_query($link, | |
aa5f9f5f AD |
435 | "UPDATE ttrss_feeds SET last_error = '$error_msg', |
436 | last_updated = NOW() WHERE id = '$feed'"); | |
40d13c28 AD |
437 | } |
438 | ||
b6eefba5 | 439 | db_query($link, "COMMIT"); |
f48ba3c9 | 440 | |
40d13c28 AD |
441 | } |
442 | ||
f175937c AD |
443 | function print_select($id, $default, $values, $attributes = "") { |
444 | print "<select id=\"$id\" $attributes>"; | |
a0d53889 AD |
445 | foreach ($values as $v) { |
446 | if ($v == $default) | |
447 | $sel = " selected"; | |
448 | else | |
449 | $sel = ""; | |
450 | ||
451 | print "<option$sel>$v</option>"; | |
452 | } | |
453 | print "</select>"; | |
454 | } | |
40d13c28 | 455 | |
e6155a06 AD |
456 | function is_filtered($title, $content, $filters) { |
457 | ||
458 | if ($filters["title"]) { | |
459 | foreach ($filters["title"] as $title_filter) { | |
460 | if (preg_match("/$title_filter/i", $title)) | |
461 | return true; | |
462 | } | |
463 | } | |
464 | ||
465 | if ($filters["content"]) { | |
466 | foreach ($filters["content"] as $content_filter) { | |
467 | if (preg_match("/$content_filter/i", $content)) | |
468 | return true; | |
469 | } | |
470 | } | |
471 | ||
472 | if ($filters["both"]) { | |
473 | foreach ($filters["both"] as $filter) { | |
474 | if (preg_match("/$filter/i", $title) || preg_match("/$filter/i", $content)) | |
475 | return true; | |
476 | } | |
477 | } | |
478 | ||
479 | return false; | |
480 | } | |
481 | ||
4668523d | 482 | function printFeedEntry($feed_id, $class, $feed_title, $unread, $icon_file, $link) { |
254e0e4b AD |
483 | |
484 | if (file_exists($icon_file) && filesize($icon_file) > 0) { | |
485 | $feed_icon = "<img src=\"$icon_file\">"; | |
486 | } else { | |
487 | $feed_icon = "<img src=\"images/blank_icon.gif\">"; | |
488 | } | |
489 | ||
8143ae1f | 490 | $feed = "<a href=\"javascript:viewfeed('$feed_id', 0);\">$feed_title</a>"; |
254e0e4b AD |
491 | |
492 | print "<li id=\"FEEDR-$feed_id\" class=\"$class\">"; | |
b619ff15 | 493 | if (get_pref($link, 'ENABLE_FEED_ICONS')) { |
254e0e4b AD |
494 | print "$feed_icon"; |
495 | } | |
496 | ||
497 | print "<span id=\"FEEDN-$feed_id\">$feed</span>"; | |
498 | ||
499 | if ($unread != 0) { | |
500 | $fctr_class = ""; | |
501 | } else { | |
502 | $fctr_class = "class=\"invisible\""; | |
503 | } | |
504 | ||
505 | print "<span $fctr_class id=\"FEEDCTR-$feed_id\"> | |
506 | (<span id=\"FEEDU-$feed_id\">$unread</span>)</span>"; | |
507 | ||
508 | print "</li>"; | |
509 | ||
510 | } | |
511 | ||
406d9489 AD |
512 | function getmicrotime() { |
513 | list($usec, $sec) = explode(" ",microtime()); | |
514 | return ((float)$usec + (float)$sec); | |
515 | } | |
516 | ||
77e96719 AD |
517 | function print_radio($id, $default, $values, $attributes = "") { |
518 | foreach ($values as $v) { | |
519 | ||
520 | if ($v == $default) | |
5da169d9 | 521 | $sel = "checked"; |
77e96719 | 522 | else |
5da169d9 AD |
523 | $sel = ""; |
524 | ||
525 | if ($v == "Yes") { | |
526 | $sel .= " value=\"1\""; | |
527 | } else { | |
528 | $sel .= " value=\"0\""; | |
529 | } | |
77e96719 AD |
530 | |
531 | print "<input type=\"radio\" $sel $attributes name=\"$id\"> $v "; | |
532 | ||
533 | } | |
534 | } | |
535 | ||
ff485f1d AD |
536 | function initialize_user_prefs($link, $uid) { |
537 | ||
538 | $uid = db_escape_string($uid); | |
539 | ||
540 | db_query($link, "BEGIN"); | |
541 | ||
542 | $result = db_query($link, "SELECT pref_name,def_value FROM ttrss_prefs"); | |
543 | ||
544 | $u_result = db_query($link, "SELECT pref_name | |
545 | FROM ttrss_user_prefs WHERE owner_uid = '$uid'"); | |
546 | ||
547 | $active_prefs = array(); | |
548 | ||
549 | while ($line = db_fetch_assoc($u_result)) { | |
550 | array_push($active_prefs, $line["pref_name"]); | |
551 | } | |
552 | ||
553 | while ($line = db_fetch_assoc($result)) { | |
554 | if (array_search($line["pref_name"], $active_prefs) === FALSE) { | |
555 | // print "adding " . $line["pref_name"] . "<br>"; | |
556 | ||
557 | db_query($link, "INSERT INTO ttrss_user_prefs | |
558 | (owner_uid,pref_name,value) VALUES | |
559 | ('$uid', '".$line["pref_name"]."','".$line["def_value"]."')"); | |
560 | ||
561 | } | |
562 | } | |
563 | ||
564 | db_query($link, "COMMIT"); | |
565 | ||
566 | } | |
c8437f35 AD |
567 | |
568 | function authenticate_user($link, $login, $password) { | |
569 | ||
570 | $pwd_hash = 'SHA1:' . sha1($password); | |
571 | ||
203b6d25 | 572 | $result = db_query($link, "SELECT id,login,access_level FROM ttrss_users WHERE |
c8437f35 AD |
573 | login = '$login' AND (pwd_hash = '$password' OR pwd_hash = '$pwd_hash')"); |
574 | ||
575 | if (db_num_rows($result) == 1) { | |
576 | $_SESSION["uid"] = db_fetch_result($result, 0, "id"); | |
577 | $_SESSION["name"] = db_fetch_result($result, 0, "login"); | |
203b6d25 | 578 | $_SESSION["access_level"] = db_fetch_result($result, 0, "access_level"); |
c8437f35 | 579 | |
f6f32198 AD |
580 | db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " . |
581 | $_SESSION["uid"]); | |
582 | ||
c8437f35 AD |
583 | return true; |
584 | } | |
ff485f1d | 585 | |
c8437f35 AD |
586 | return false; |
587 | ||
588 | } | |
589 | ||
590 | function http_authenticate_user($link) { | |
1c7f75ed AD |
591 | |
592 | if (!$_SERVER['PHP_AUTH_USER']) { | |
593 | ||
594 | header('WWW-Authenticate: Basic realm="Tiny Tiny RSS"'); | |
595 | header('HTTP/1.0 401 Unauthorized'); | |
596 | print "<h1>401 Unathorized</h1>"; | |
597 | exit; | |
598 | ||
599 | } else { | |
600 | ||
601 | $login = db_escape_string($_SERVER['PHP_AUTH_USER']); | |
602 | $password = db_escape_string($_SERVER['PHP_AUTH_PW']); | |
1c7f75ed | 603 | |
c8437f35 AD |
604 | return authenticate_user($link, $login, $password); |
605 | } | |
1c7f75ed AD |
606 | } |
607 | ||
e6cb77a0 AD |
608 | function make_password($length = 8) { |
609 | ||
610 | $password = ""; | |
611 | $possible = "0123456789bcdfghjkmnpqrstvwxyz"; | |
612 | ||
613 | $i = 0; | |
614 | ||
615 | while ($i < $length) { | |
616 | $char = substr($possible, mt_rand(0, strlen($possible)-1), 1); | |
617 | ||
618 | if (!strstr($password, $char)) { | |
619 | $password .= $char; | |
620 | $i++; | |
621 | } | |
622 | } | |
623 | return $password; | |
624 | } | |
625 | ||
626 | // this is called after user is created to initialize default feeds, labels | |
627 | // or whatever else | |
628 | ||
629 | // user preferences are checked on every login, not here | |
630 | ||
631 | function initialize_user($link, $uid) { | |
632 | ||
633 | db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description) | |
634 | values ('$uid','unread = true', 'Unread articles')"); | |
635 | ||
636 | db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description) | |
637 | values ('$uid','last_read is null and unread = false', 'Updated articles')"); | |
638 | ||
639 | db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url) | |
640 | values ('$uid', 'Tiny Tiny RSS Dev. Feed', | |
641 | 'http://bah.spb.su/darcsweb/darcsweb.cgi?r=tt-rss;a=rss')"); | |
642 | ||
643 | } | |
644 | ||
40d13c28 | 645 | ?> |