]> git.wh0rd.org - tt-rss.git/blob - db-prefs.php
update NEWS
[tt-rss.git] / db-prefs.php
1 <?
2 require_once "config.php";
3 require_once "db.php";
4
5 if (! DISABLE_SESSIONS) {
6 if (!$_SESSION["prefs_cache"])
7 $_SESSION["prefs_cache"] = array();
8 }
9
10 function get_pref($link, $pref_name, $user_id = false) {
11
12 $pref_name = db_escape_string($pref_name);
13
14 if (!$user_id) {
15 $user_id = $_SESSION["uid"];
16 } else {
17 $user_id = sprintf("%d", $user_id);
18 $prefs_cache = false;
19 }
20
21 if ($_SESSION["prefs_cache"] && $_SESSION["prefs_cache"][$pref_name]) {
22 $tuple = $_SESSION["prefs_cache"][$pref_name];
23 return convert_pref_type($tuple["value"], $tuple["type"]);
24 }
25
26 $result = db_query($link, "SELECT
27 value,ttrss_prefs_types.type_name as type_name
28 FROM
29 ttrss_user_prefs,ttrss_prefs,ttrss_prefs_types
30 WHERE
31 ttrss_user_prefs.pref_name = '$pref_name' AND
32 ttrss_prefs_types.id = type_id AND
33 owner_uid = '$user_id' AND
34 ttrss_user_prefs.pref_name = ttrss_prefs.pref_name");
35
36 if (db_num_rows($result) > 0) {
37 $value = db_fetch_result($result, 0, "value");
38 $type_name = db_fetch_result($result, 0, "type_name");
39
40 if ($user_id = $_SESSION["uid"]) {
41 $_SESSION["prefs_cache"][$pref_name]["type"] = $type_name;
42 $_SESSION["prefs_cache"][$pref_name]["value"] = $value;
43 }
44 return convert_pref_type($value, $type_name);
45
46 } else {
47 die("Fatal error, unknown preferences key: $pref_name");
48 }
49 }
50
51 function convert_pref_type($value, $type_name) {
52 if ($type_name == "bool") {
53 return $value == "true";
54 } else if ($type_name == "integer") {
55 return sprintf("%d", $value);
56 } else {
57 return $value;
58 }
59 }
60
61 // doesn't peform any type checking, be vigilant
62
63 function set_pref($link, $key, $value) {
64 $key = db_escape_string($key);
65 $value = db_escape_string($value);
66
67 db_query($link, "UPDATE ttrss_user_prefs SET
68 value = '$value' WHERE pref_name = '$key'
69 AND owner_uid = " . $_SESSION["uid"]);
70
71 $_SESSION["prefs_cache"] = array();
72
73 }
74 ?>