]> git.wh0rd.org - tt-rss.git/blame - classes/db/prefs.php
ccache, misc: fixes
[tt-rss.git] / classes / db / prefs.php
CommitLineData
52d88392
AD
1<?php
2class 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 $profile = false;
57
58 if (!$user_id) {
59 $user_id = $_SESSION["uid"];
60 @$profile = $_SESSION["profile"];
61 } else {
62 $user_id = sprintf("%d", $user_id);
63 }
64
e130b283 65 if (isset($this->cache[$pref_name]) && !$user_id) {
52d88392
AD
66 $tuple = $this->cache[$pref_name];
67 return $this->convert($tuple["value"], $tuple["type"]);
68 }
69
fbde1958 70 if (!$profile || get_schema_version() < 63) $profile = null;
52d88392 71
fbde1958 72 $sth = $this->pdo->prepare("SELECT
52d88392
AD
73 value,ttrss_prefs_types.type_name as type_name
74 FROM
75 ttrss_user_prefs,ttrss_prefs,ttrss_prefs_types
76 WHERE
cc9450c3 77 (profile = :profile OR (:profile IS NULL AND profile IS NULL)) AND
fbde1958 78 ttrss_user_prefs.pref_name = :pref_name AND
52d88392 79 ttrss_prefs_types.id = type_id AND
fbde1958 80 owner_uid = :uid AND
52d88392 81 ttrss_user_prefs.pref_name = ttrss_prefs.pref_name");
fbde1958 82 $sth->execute([":uid" => $user_id, ":profile" => $profile, ":pref_name" => $pref_name]);
52d88392 83
fbde1958
AD
84 if ($row = $sth->fetch()) {
85 $value = $row["value"];
86 $type_name = $row["type_name"];
52d88392
AD
87
88 if ($user_id == $_SESSION["uid"]) {
89 $this->cache[$pref_name]["type"] = $type_name;
90 $this->cache[$pref_name]["value"] = $value;
91 }
92
93 return $this->convert($value, $type_name);
94
95 } else {
6a51939e 96 user_error("Fatal error, unknown preferences key: $pref_name (owner: $user_id)", $die_on_error ? E_USER_ERROR : E_USER_WARNING);
52d88392
AD
97 return null;
98 }
99 }
100
101 function convert($value, $type_name) {
102 if ($type_name == "bool") {
103 return $value == "true";
104 } else if ($type_name == "integer") {
105 return (int)$value;
106 } else {
107 return $value;
108 }
109 }
110
111 function write($pref_name, $value, $user_id = false, $strip_tags = true) {
fbde1958 112 if ($strip_tags) $value = strip_tags($value);
52d88392
AD
113
114 if (!$user_id) {
115 $user_id = $_SESSION["uid"];
116 @$profile = $_SESSION["profile"];
117 } else {
118 $user_id = sprintf("%d", $user_id);
52d88392
AD
119 }
120
121 if ($profile) {
122 $profile_qpart = "AND profile = '$profile'";
123 } else {
124 $profile_qpart = "AND profile IS NULL";
125 }
126
fbde1958 127 if (!$profile || get_schema_version() < 63) $profile = null;
52d88392
AD
128
129 $type_name = "";
130 $current_value = "";
131
132 if (isset($this->cache[$pref_name])) {
133 $type_name = $this->cache[$pref_name]["type"];
134 $current_value = $this->cache[$pref_name]["value"];
135 }
136
137 if (!$type_name) {
fbde1958 138 $sth = $this->pdo->prepare("SELECT type_name
52d88392 139 FROM ttrss_prefs,ttrss_prefs_types
fbde1958
AD
140 WHERE pref_name = ? AND type_id = ttrss_prefs_types.id");
141 $sth->execute([$pref_name]);
142
143 if ($row = $sth->fetch())
144 $type_name = $row["type_name"];
52d88392 145
52d88392
AD
146 } else if ($current_value == $value) {
147 return;
148 }
149
150 if ($type_name) {
151 if ($type_name == "bool") {
152 if ($value == "1" || $value == "true") {
153 $value = "true";
154 } else {
155 $value = "false";
156 }
157 } else if ($type_name == "integer") {
158 $value = sprintf("%d", $value);
159 }
160
161 if ($pref_name == 'USER_TIMEZONE' && $value == '') {
162 $value = 'UTC';
163 }
164
fbde1958
AD
165 $sth = $this->pdo->prepare("UPDATE ttrss_user_prefs SET
166 value = :value WHERE pref_name = :pref_name
167 AND (profile = :profile OR (:profile IS NULL AND profile IS NULL))
168 AND owner_uid = :uid");
169
170 $sth->execute([":pref_name" => $pref_name, ":value" => $value, ":uid" => $user_id, ":profile" => $profile]);
52d88392
AD
171
172 if ($user_id == $_SESSION["uid"]) {
173 $this->cache[$pref_name]["type"] = $type_name;
174 $this->cache[$pref_name]["value"] = $value;
175 }
176 }
177 }
178
891df346 179}