]>
Commit | Line | Data |
---|---|---|
52d88392 AD |
1 | <?php |
2 | class Db_Prefs { | |
fbde1958 | 3 | private $pdo; |
52d88392 AD |
4 | private static $instance; |
5 | private $cache; | |
6 | ||
7 | function __construct() { | |
fbde1958 | 8 | $this->pdo = Db::pdo(); |
52d88392 AD |
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 | ||
fbde1958 | 29 | if (!$profile || get_schema_version() < 63) $profile = null; |
52d88392 | 30 | |
fbde1958 | 31 | $sth = $this->pdo->prepare("SELECT |
52d88392 AD |
32 | value,ttrss_prefs_types.type_name as type_name,ttrss_prefs.pref_name AS pref_name |
33 | FROM | |
34 | ttrss_user_prefs,ttrss_prefs,ttrss_prefs_types | |
35 | WHERE | |
cc9450c3 | 36 | (profile = :profile OR (:profile IS NULL AND profile IS NULL)) AND |
52d88392 AD |
37 | ttrss_prefs.pref_name NOT LIKE '_MOBILE%' AND |
38 | ttrss_prefs_types.id = type_id AND | |
fbde1958 | 39 | owner_uid = :uid AND |
52d88392 AD |
40 | ttrss_user_prefs.pref_name = ttrss_prefs.pref_name"); |
41 | ||
fbde1958 AD |
42 | $sth->execute([":profile" => $profile, ":uid" => $user_id]); |
43 | ||
44 | while ($line = $sth->fetch()) { | |
52d88392 AD |
45 | if ($user_id == $_SESSION["uid"]) { |
46 | $pref_name = $line["pref_name"]; | |
47 | ||
48 | $this->cache[$pref_name]["type"] = $line["type_name"]; | |
49 | $this->cache[$pref_name]["value"] = $line["value"]; | |
50 | } | |
51 | } | |
52 | } | |
53 | ||
54 | function read($pref_name, $user_id = false, $die_on_error = false) { | |
55 | ||
52d88392 AD |
56 | if (!$user_id) { |
57 | $user_id = $_SESSION["uid"]; | |
58 | @$profile = $_SESSION["profile"]; | |
59 | } else { | |
22366ccc | 60 | $profile = false; |
52d88392 AD |
61 | } |
62 | ||
22366ccc | 63 | if ($user_id == $_SESSION['uid'] && isset($this->cache[$pref_name])) { |
52d88392 AD |
64 | $tuple = $this->cache[$pref_name]; |
65 | return $this->convert($tuple["value"], $tuple["type"]); | |
66 | } | |
67 | ||
fbde1958 | 68 | if (!$profile || get_schema_version() < 63) $profile = null; |
52d88392 | 69 | |
fbde1958 | 70 | $sth = $this->pdo->prepare("SELECT |
52d88392 AD |
71 | value,ttrss_prefs_types.type_name as type_name |
72 | FROM | |
73 | ttrss_user_prefs,ttrss_prefs,ttrss_prefs_types | |
74 | WHERE | |
cc9450c3 | 75 | (profile = :profile OR (:profile IS NULL AND profile IS NULL)) AND |
fbde1958 | 76 | ttrss_user_prefs.pref_name = :pref_name AND |
52d88392 | 77 | ttrss_prefs_types.id = type_id AND |
fbde1958 | 78 | owner_uid = :uid AND |
52d88392 | 79 | ttrss_user_prefs.pref_name = ttrss_prefs.pref_name"); |
fbde1958 | 80 | $sth->execute([":uid" => $user_id, ":profile" => $profile, ":pref_name" => $pref_name]); |
52d88392 | 81 | |
fbde1958 AD |
82 | if ($row = $sth->fetch()) { |
83 | $value = $row["value"]; | |
84 | $type_name = $row["type_name"]; | |
52d88392 AD |
85 | |
86 | if ($user_id == $_SESSION["uid"]) { | |
87 | $this->cache[$pref_name]["type"] = $type_name; | |
88 | $this->cache[$pref_name]["value"] = $value; | |
89 | } | |
90 | ||
91 | return $this->convert($value, $type_name); | |
92 | ||
93 | } else { | |
6a51939e | 94 | user_error("Fatal error, unknown preferences key: $pref_name (owner: $user_id)", $die_on_error ? E_USER_ERROR : E_USER_WARNING); |
52d88392 AD |
95 | return null; |
96 | } | |
97 | } | |
98 | ||
99 | function convert($value, $type_name) { | |
100 | if ($type_name == "bool") { | |
101 | return $value == "true"; | |
102 | } else if ($type_name == "integer") { | |
103 | return (int)$value; | |
104 | } else { | |
105 | return $value; | |
106 | } | |
107 | } | |
108 | ||
109 | function write($pref_name, $value, $user_id = false, $strip_tags = true) { | |
fbde1958 | 110 | if ($strip_tags) $value = strip_tags($value); |
52d88392 AD |
111 | |
112 | if (!$user_id) { | |
113 | $user_id = $_SESSION["uid"]; | |
114 | @$profile = $_SESSION["profile"]; | |
52d88392 AD |
115 | } |
116 | ||
fbde1958 | 117 | if (!$profile || get_schema_version() < 63) $profile = null; |
52d88392 AD |
118 | |
119 | $type_name = ""; | |
120 | $current_value = ""; | |
121 | ||
122 | if (isset($this->cache[$pref_name])) { | |
123 | $type_name = $this->cache[$pref_name]["type"]; | |
124 | $current_value = $this->cache[$pref_name]["value"]; | |
125 | } | |
126 | ||
127 | if (!$type_name) { | |
fbde1958 | 128 | $sth = $this->pdo->prepare("SELECT type_name |
52d88392 | 129 | FROM ttrss_prefs,ttrss_prefs_types |
fbde1958 AD |
130 | WHERE pref_name = ? AND type_id = ttrss_prefs_types.id"); |
131 | $sth->execute([$pref_name]); | |
132 | ||
133 | if ($row = $sth->fetch()) | |
134 | $type_name = $row["type_name"]; | |
52d88392 | 135 | |
52d88392 AD |
136 | } else if ($current_value == $value) { |
137 | return; | |
138 | } | |
139 | ||
140 | if ($type_name) { | |
141 | if ($type_name == "bool") { | |
142 | if ($value == "1" || $value == "true") { | |
143 | $value = "true"; | |
144 | } else { | |
145 | $value = "false"; | |
146 | } | |
147 | } else if ($type_name == "integer") { | |
22366ccc | 148 | $value = (int)$value; |
52d88392 AD |
149 | } |
150 | ||
151 | if ($pref_name == 'USER_TIMEZONE' && $value == '') { | |
152 | $value = 'UTC'; | |
153 | } | |
154 | ||
fbde1958 AD |
155 | $sth = $this->pdo->prepare("UPDATE ttrss_user_prefs SET |
156 | value = :value WHERE pref_name = :pref_name | |
157 | AND (profile = :profile OR (:profile IS NULL AND profile IS NULL)) | |
158 | AND owner_uid = :uid"); | |
159 | ||
160 | $sth->execute([":pref_name" => $pref_name, ":value" => $value, ":uid" => $user_id, ":profile" => $profile]); | |
52d88392 AD |
161 | |
162 | if ($user_id == $_SESSION["uid"]) { | |
163 | $this->cache[$pref_name]["type"] = $type_name; | |
164 | $this->cache[$pref_name]["value"] = $value; | |
165 | } | |
166 | } | |
167 | } | |
168 | ||
891df346 | 169 | } |