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