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"]) $this->cache(); |
12
|
|
|
} |
13
|
|
|
|
14
|
|
|
public static function get() { |
15
|
|
|
if (self::$instance == null) |
16
|
|
|
self::$instance = new self(); |
17
|
|
|
|
18
|
|
|
return self::$instance; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function cache() { |
22
|
|
|
$user_id = $_SESSION["uid"]; |
23
|
|
|
@$profile = $_SESSION["profile"]; |
24
|
|
|
|
25
|
|
|
if (!is_numeric($profile) || !$profile || get_schema_version() < 63) $profile = null; |
26
|
|
|
|
27
|
|
|
$sth = $this->pdo->prepare("SELECT |
28
|
|
|
value,ttrss_prefs_types.type_name as type_name,ttrss_prefs.pref_name AS pref_name |
29
|
|
|
FROM |
30
|
|
|
ttrss_user_prefs,ttrss_prefs,ttrss_prefs_types |
31
|
|
|
WHERE |
32
|
|
|
(profile = :profile OR (:profile IS NULL AND profile IS NULL)) AND |
33
|
|
|
ttrss_prefs.pref_name NOT LIKE '_MOBILE%' AND |
34
|
|
|
ttrss_prefs_types.id = type_id AND |
35
|
|
|
owner_uid = :uid AND |
36
|
|
|
ttrss_user_prefs.pref_name = ttrss_prefs.pref_name"); |
37
|
|
|
|
38
|
|
|
$sth->execute([":profile" => $profile, ":uid" => $user_id]); |
39
|
|
|
|
40
|
|
|
while ($line = $sth->fetch()) { |
41
|
|
|
if ($user_id == $_SESSION["uid"]) { |
42
|
|
|
$pref_name = $line["pref_name"]; |
43
|
|
|
|
44
|
|
|
$this->cache[$pref_name]["type"] = $line["type_name"]; |
45
|
|
|
$this->cache[$pref_name]["value"] = $line["value"]; |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function read($pref_name, $user_id = false, $die_on_error = false) { |
51
|
|
|
|
52
|
|
|
if (!$user_id) { |
53
|
|
|
$user_id = $_SESSION["uid"]; |
54
|
|
|
@$profile = $_SESSION["profile"]; |
55
|
|
|
} else { |
56
|
|
|
$profile = false; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
if ($user_id == $_SESSION['uid'] && isset($this->cache[$pref_name])) { |
60
|
|
|
$tuple = $this->cache[$pref_name]; |
61
|
|
|
return $this->convert($tuple["value"], $tuple["type"]); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if (!is_numeric($profile) || !$profile || get_schema_version() < 63) $profile = null; |
65
|
|
|
|
66
|
|
|
$sth = $this->pdo->prepare("SELECT |
67
|
|
|
value,ttrss_prefs_types.type_name as type_name |
68
|
|
|
FROM |
69
|
|
|
ttrss_user_prefs,ttrss_prefs,ttrss_prefs_types |
70
|
|
|
WHERE |
71
|
|
|
(profile = :profile OR (:profile IS NULL AND profile IS NULL)) AND |
72
|
|
|
ttrss_user_prefs.pref_name = :pref_name AND |
73
|
|
|
ttrss_prefs_types.id = type_id AND |
74
|
|
|
owner_uid = :uid AND |
75
|
|
|
ttrss_user_prefs.pref_name = ttrss_prefs.pref_name"); |
76
|
|
|
$sth->execute([":uid" => $user_id, ":profile" => $profile, ":pref_name" => $pref_name]); |
77
|
|
|
|
78
|
|
|
if ($row = $sth->fetch()) { |
79
|
|
|
$value = $row["value"]; |
80
|
|
|
$type_name = $row["type_name"]; |
81
|
|
|
|
82
|
|
|
if ($user_id == $_SESSION["uid"]) { |
83
|
|
|
$this->cache[$pref_name]["type"] = $type_name; |
84
|
|
|
$this->cache[$pref_name]["value"] = $value; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $this->convert($value, $type_name); |
88
|
|
|
|
89
|
|
|
} else if ($die_on_error) { |
90
|
|
|
user_error("Fatal error, unknown preferences key: $pref_name (owner: $user_id)", E_USER_ERROR); |
91
|
|
|
return null; |
92
|
|
|
} else { |
93
|
|
|
return null; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function convert($value, $type_name) { |
98
|
|
|
if ($type_name == "bool") { |
99
|
|
|
return $value == "true"; |
100
|
|
|
} else if ($type_name == "integer") { |
101
|
|
|
return (int)$value; |
102
|
|
|
} else { |
103
|
|
|
return $value; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function write($pref_name, $value, $user_id = false, $strip_tags = true) { |
108
|
|
|
if ($strip_tags) $value = strip_tags($value); |
109
|
|
|
|
110
|
|
|
if (!$user_id) { |
111
|
|
|
$user_id = $_SESSION["uid"]; |
112
|
|
|
@$profile = $_SESSION["profile"]; |
113
|
|
|
} else { |
114
|
|
|
$profile = null; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
if (!is_numeric($profile) || !$profile || get_schema_version() < 63) $profile = null; |
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) { |
128
|
|
|
$sth = $this->pdo->prepare("SELECT type_name |
129
|
|
|
FROM ttrss_prefs,ttrss_prefs_types |
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"]; |
135
|
|
|
|
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") { |
148
|
|
|
$value = (int)$value; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
if ($pref_name == 'USER_TIMEZONE' && $value == '') { |
152
|
|
|
$value = 'UTC'; |
153
|
|
|
} |
154
|
|
|
|
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]); |
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
|
|
|
} |
169
|
|
|
|