]>
Commit | Line | Data |
---|---|---|
1d3a17c7 | 1 | <?php |
3bac89ad AD |
2 | require_once "config.php"; |
3 | require_once "db.php"; | |
4 | ||
f5a5bae4 | 5 | if (!defined('DISABLE_SESSIONS')) { |
de696427 AD |
6 | if (!$_SESSION["prefs_cache"]) |
7 | $_SESSION["prefs_cache"] = array(); | |
8 | } | |
11de82c3 | 9 | |
b4c27af7 | 10 | function get_pref($link, $pref_name, $user_id = false, $die_on_error = false) { |
3bac89ad AD |
11 | |
12 | $pref_name = db_escape_string($pref_name); | |
70364a42 | 13 | $prefs_cache = true; |
3bac89ad | 14 | |
a2770077 AD |
15 | if (!$user_id) { |
16 | $user_id = $_SESSION["uid"]; | |
d9084cf2 | 17 | $profile = $_SESSION["profile"]; |
a2770077 AD |
18 | } else { |
19 | $user_id = sprintf("%d", $user_id); | |
11de82c3 AD |
20 | $prefs_cache = false; |
21 | } | |
658e1cc1 | 22 | |
d9084cf2 | 23 | if ($profile) { |
f9aa6a89 | 24 | $profile_qpart = "profile = '$profile' AND"; |
d9084cf2 | 25 | } else { |
f9aa6a89 | 26 | $profile_qpart = "profile IS NULL AND"; |
d9084cf2 AD |
27 | } |
28 | ||
f9aa6a89 | 29 | if (get_schema_version($link) < 63) $profile_qpart = ""; |
d9084cf2 | 30 | |
70364a42 | 31 | if ($prefs_cache && !defined('DISABLE_SESSIONS') && !SINGLE_USER_MODE) { |
658e1cc1 AD |
32 | if ($_SESSION["prefs_cache"] && $_SESSION["prefs_cache"][$pref_name]) { |
33 | $tuple = $_SESSION["prefs_cache"][$pref_name]; | |
34 | return convert_pref_type($tuple["value"], $tuple["type"]); | |
35 | } | |
a2770077 AD |
36 | } |
37 | ||
4062022e AD |
38 | $result = db_query($link, "SELECT |
39 | value,ttrss_prefs_types.type_name as type_name | |
ff485f1d AD |
40 | FROM |
41 | ttrss_user_prefs,ttrss_prefs,ttrss_prefs_types | |
42 | WHERE | |
f9aa6a89 | 43 | $profile_qpart |
ff485f1d AD |
44 | ttrss_user_prefs.pref_name = '$pref_name' AND |
45 | ttrss_prefs_types.id = type_id AND | |
a2770077 | 46 | owner_uid = '$user_id' AND |
ff485f1d | 47 | ttrss_user_prefs.pref_name = ttrss_prefs.pref_name"); |
3bac89ad AD |
48 | |
49 | if (db_num_rows($result) > 0) { | |
50 | $value = db_fetch_result($result, 0, "value"); | |
4062022e AD |
51 | $type_name = db_fetch_result($result, 0, "type_name"); |
52 | ||
1503d397 | 53 | if (!defined('DISABLE_SESSIONS') && !SINGLE_USER_MODE) { |
658e1cc1 AD |
54 | if ($user_id = $_SESSION["uid"]) { |
55 | $_SESSION["prefs_cache"][$pref_name]["type"] = $type_name; | |
56 | $_SESSION["prefs_cache"][$pref_name]["value"] = $value; | |
57 | } | |
4062022e | 58 | } |
658e1cc1 | 59 | |
dc0c8985 | 60 | return convert_pref_type($value, $type_name); |
4062022e | 61 | |
3bac89ad | 62 | } else { |
1e107b0f AD |
63 | if ($die_on_error) { |
64 | die("Fatal error, unknown preferences key: $pref_name"); | |
65 | } else { | |
66 | return null; | |
67 | } | |
3bac89ad AD |
68 | } |
69 | } | |
70 | ||
dc0c8985 AD |
71 | function convert_pref_type($value, $type_name) { |
72 | if ($type_name == "bool") { | |
73 | return $value == "true"; | |
74 | } else if ($type_name == "integer") { | |
75 | return sprintf("%d", $value); | |
76 | } else { | |
77 | return $value; | |
78 | } | |
79 | } | |
7f123cda | 80 | |
5f798ab1 | 81 | function set_pref($link, $key, $value, $user_id = false) { |
7f123cda AD |
82 | $key = db_escape_string($key); |
83 | $value = db_escape_string($value); | |
7f123cda | 84 | |
d9084cf2 AD |
85 | if (!$user_id) { |
86 | $user_id = $_SESSION["uid"]; | |
87 | $profile = $_SESSION["profile"]; | |
88 | } else { | |
89 | $user_id = sprintf("%d", $user_id); | |
90 | $prefs_cache = false; | |
91 | } | |
92 | ||
93 | if ($profile) { | |
f9aa6a89 | 94 | $profile_qpart = "AND profile = '$profile'"; |
d9084cf2 | 95 | } else { |
b1242797 | 96 | $profile_qpart = "AND profile IS NULL"; |
d9084cf2 AD |
97 | } |
98 | ||
f9aa6a89 AD |
99 | if (get_schema_version($link) < 63) $profile_qpart = ""; |
100 | ||
e9105eb5 AD |
101 | $result = db_query($link, "SELECT type_name |
102 | FROM ttrss_prefs,ttrss_prefs_types | |
103 | WHERE pref_name = '$key' AND type_id = ttrss_prefs_types.id"); | |
104 | ||
105 | if (db_num_rows($result) > 0) { | |
106 | ||
107 | $type_name = db_fetch_result($result, 0, "type_name"); | |
108 | ||
109 | if ($type_name == "bool") { | |
110 | if ($value == "1" || $value == "true") { | |
111 | $value = "true"; | |
112 | } else { | |
113 | $value = "false"; | |
114 | } | |
115 | } else if ($type_name == "integer") { | |
116 | $value = sprintf("%d", $value); | |
117 | } | |
118 | ||
119 | if ($pref_name == 'DEFAULT_ARTICLE_LIMIT' && $value == 0) { | |
120 | $value = 30; | |
121 | } | |
122 | ||
123 | db_query($link, "UPDATE ttrss_user_prefs SET | |
124 | value = '$value' WHERE pref_name = '$key' | |
f9aa6a89 | 125 | $profile_qpart |
e9105eb5 | 126 | AND owner_uid = " . $_SESSION["uid"]); |
7f123cda | 127 | |
e9105eb5 AD |
128 | $_SESSION["prefs_cache"] = array(); |
129 | ||
130 | } | |
7f123cda | 131 | } |
3bac89ad | 132 | ?> |