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