]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | require_once "config.php"; | |
3 | require_once "db.php"; | |
4 | ||
5 | if (!defined('DISABLE_SESSIONS') && !defined('PREFS_NO_CACHE')) { | |
6 | if (!$_SESSION["prefs_cache"]) | |
7 | $_SESSION["prefs_cache"] = array(); | |
8 | } | |
9 | ||
10 | function cache_prefs($link) { | |
11 | $profile = false; | |
12 | ||
13 | $user_id = $_SESSION["uid"]; | |
14 | @$profile = $_SESSION["profile"]; | |
15 | ||
16 | if ($profile) { | |
17 | $profile_qpart = "profile = '$profile' AND"; | |
18 | } else { | |
19 | $profile_qpart = "profile IS NULL AND"; | |
20 | } | |
21 | ||
22 | if (get_schema_version($link) < 63) $profile_qpart = ""; | |
23 | ||
24 | $result = db_query($link, "SELECT | |
25 | value,ttrss_prefs_types.type_name as type_name,ttrss_prefs.pref_name AS pref_name | |
26 | FROM | |
27 | ttrss_user_prefs,ttrss_prefs,ttrss_prefs_types | |
28 | WHERE | |
29 | $profile_qpart | |
30 | ttrss_prefs.pref_name NOT LIKE '_MOBILE%' AND | |
31 | ttrss_prefs_types.id = type_id AND | |
32 | owner_uid = '$user_id' AND | |
33 | ttrss_user_prefs.pref_name = ttrss_prefs.pref_name"); | |
34 | ||
35 | while ($line = db_fetch_assoc($result)) { | |
36 | if ($user_id == $_SESSION["uid"]) { | |
37 | $pref_name = $line["pref_name"]; | |
38 | ||
39 | $_SESSION["prefs_cache"][$pref_name]["type"] = $line["type_name"]; | |
40 | $_SESSION["prefs_cache"][$pref_name]["value"] = $line["value"]; | |
41 | } | |
42 | } | |
43 | } | |
44 | ||
45 | function get_pref($link, $pref_name, $user_id = false, $die_on_error = false) { | |
46 | ||
47 | $pref_name = db_escape_string($link, $pref_name); | |
48 | $prefs_cache = true; | |
49 | $profile = false; | |
50 | ||
51 | if (!$user_id) { | |
52 | $user_id = $_SESSION["uid"]; | |
53 | @$profile = $_SESSION["profile"]; | |
54 | } else { | |
55 | $user_id = sprintf("%d", $user_id); | |
56 | //$prefs_cache = false; | |
57 | } | |
58 | ||
59 | if ($prefs_cache && !defined('DISABLE_SESSIONS') && !defined('PREFS_NO_CACHE')) { | |
60 | if ($_SESSION["prefs_cache"] && @$_SESSION["prefs_cache"][$pref_name]) { | |
61 | $tuple = $_SESSION["prefs_cache"][$pref_name]; | |
62 | return convert_pref_type($tuple["value"], $tuple["type"]); | |
63 | } | |
64 | } | |
65 | ||
66 | if ($profile) { | |
67 | $profile_qpart = "profile = '$profile' AND"; | |
68 | } else { | |
69 | $profile_qpart = "profile IS NULL AND"; | |
70 | } | |
71 | ||
72 | if (get_schema_version($link) < 63) $profile_qpart = ""; | |
73 | ||
74 | $result = db_query($link, "SELECT | |
75 | value,ttrss_prefs_types.type_name as type_name | |
76 | FROM | |
77 | ttrss_user_prefs,ttrss_prefs,ttrss_prefs_types | |
78 | WHERE | |
79 | $profile_qpart | |
80 | ttrss_user_prefs.pref_name = '$pref_name' AND | |
81 | ttrss_prefs_types.id = type_id AND | |
82 | owner_uid = '$user_id' AND | |
83 | ttrss_user_prefs.pref_name = ttrss_prefs.pref_name"); | |
84 | ||
85 | if (db_num_rows($result) > 0) { | |
86 | $value = db_fetch_result($result, 0, "value"); | |
87 | $type_name = db_fetch_result($result, 0, "type_name"); | |
88 | ||
89 | if (!defined('DISABLE_SESSIONS')) { | |
90 | if ($user_id == $_SESSION["uid"]) { | |
91 | $_SESSION["prefs_cache"][$pref_name]["type"] = $type_name; | |
92 | $_SESSION["prefs_cache"][$pref_name]["value"] = $value; | |
93 | } | |
94 | } | |
95 | ||
96 | return convert_pref_type($value, $type_name); | |
97 | ||
98 | } else { | |
99 | if ($die_on_error) { | |
100 | die("Fatal error, unknown preferences key: $pref_name"); | |
101 | } else { | |
102 | return null; | |
103 | } | |
104 | } | |
105 | } | |
106 | ||
107 | function convert_pref_type($value, $type_name) { | |
108 | if ($type_name == "bool") { | |
109 | return $value == "true"; | |
110 | } else if ($type_name == "integer") { | |
111 | return sprintf("%d", $value); | |
112 | } else { | |
113 | return $value; | |
114 | } | |
115 | } | |
116 | ||
117 | function set_pref($link, $pref_name, $value, $user_id = false, $strip_tags = true) { | |
118 | $pref_name = db_escape_string($link, $pref_name); | |
119 | $value = db_escape_string($link, $value, $strip_tags); | |
120 | ||
121 | if (!$user_id) { | |
122 | $user_id = $_SESSION["uid"]; | |
123 | @$profile = $_SESSION["profile"]; | |
124 | } else { | |
125 | $user_id = sprintf("%d", $user_id); | |
126 | $prefs_cache = false; | |
127 | } | |
128 | ||
129 | if ($profile) { | |
130 | $profile_qpart = "AND profile = '$profile'"; | |
131 | } else { | |
132 | $profile_qpart = "AND profile IS NULL"; | |
133 | } | |
134 | ||
135 | if (get_schema_version($link) < 63) $profile_qpart = ""; | |
136 | ||
137 | $type_name = ""; | |
138 | $current_value = ""; | |
139 | ||
140 | if (!defined('DISABLE_SESSIONS') && !defined('PREFS_NO_CACHE')) { | |
141 | if ($_SESSION["prefs_cache"] && @$_SESSION["prefs_cache"][$pref_name]) { | |
142 | $type_name = $_SESSION["prefs_cache"][$pref_name]["type"]; | |
143 | $current_value = $_SESSION["prefs_cache"][$pref_name]["value"]; | |
144 | } | |
145 | } | |
146 | ||
147 | if (!$type_name) { | |
148 | $result = db_query($link, "SELECT type_name | |
149 | FROM ttrss_prefs,ttrss_prefs_types | |
150 | WHERE pref_name = '$pref_name' AND type_id = ttrss_prefs_types.id"); | |
151 | ||
152 | if (db_num_rows($result) > 0) | |
153 | $type_name = db_fetch_result($result, 0, "type_name"); | |
154 | } else if ($current_value == $value) { | |
155 | return; | |
156 | } | |
157 | ||
158 | if ($type_name) { | |
159 | if ($type_name == "bool") { | |
160 | if ($value == "1" || $value == "true") { | |
161 | $value = "true"; | |
162 | } else { | |
163 | $value = "false"; | |
164 | } | |
165 | } else if ($type_name == "integer") { | |
166 | $value = sprintf("%d", $value); | |
167 | } | |
168 | ||
169 | if ($pref_name == 'DEFAULT_ARTICLE_LIMIT' && $value == 0) { | |
170 | $value = 30; | |
171 | } | |
172 | ||
173 | if ($pref_name == 'USER_TIMEZONE' && $value == '') { | |
174 | $value = 'UTC'; | |
175 | } | |
176 | ||
177 | db_query($link, "UPDATE ttrss_user_prefs SET | |
178 | value = '$value' WHERE pref_name = '$pref_name' | |
179 | $profile_qpart | |
180 | AND owner_uid = " . $_SESSION["uid"]); | |
181 | ||
182 | if (!defined('DISABLE_SESSIONS')) { | |
183 | if ($user_id == $_SESSION["uid"]) { | |
184 | $_SESSION["prefs_cache"][$pref_name]["type"] = $type_name; | |
185 | $_SESSION["prefs_cache"][$pref_name]["value"] = $value; | |
186 | } | |
187 | } | |
188 | } | |
189 | } | |
190 | ?> |