]> git.wh0rd.org - tt-rss.git/blob - classes/db/prefs.php
64238bc7853ace398d7ac66b137e139ca946bd9c
[tt-rss.git] / classes / db / prefs.php
1 <?php
2 class Db_Prefs {
3 private $pdo;
4 private static $instance;
5 private $cache;
6
7 function __construct() {
8 $this->pdo = Db::pdo();
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() {
26 $user_id = $_SESSION["uid"];
27 @$profile = $_SESSION["profile"];
28
29 if (!$profile || get_schema_version() < 63) $profile = null;
30
31 $sth = $this->pdo->prepare("SELECT
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
36 (profile = :profile OR (:profile IS NULL AND profile IS NULL)) AND
37 ttrss_prefs.pref_name NOT LIKE '_MOBILE%' AND
38 ttrss_prefs_types.id = type_id AND
39 owner_uid = :uid AND
40 ttrss_user_prefs.pref_name = ttrss_prefs.pref_name");
41
42 $sth->execute([":profile" => $profile, ":uid" => $user_id]);
43
44 while ($line = $sth->fetch()) {
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
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
65 if (isset($this->cache[$pref_name]) && !$user_id) {
66 $tuple = $this->cache[$pref_name];
67 return $this->convert($tuple["value"], $tuple["type"]);
68 }
69
70 if (!$profile || get_schema_version() < 63) $profile = null;
71
72 $sth = $this->pdo->prepare("SELECT
73 value,ttrss_prefs_types.type_name as type_name
74 FROM
75 ttrss_user_prefs,ttrss_prefs,ttrss_prefs_types
76 WHERE
77 (profile = :profile OR (:profile IS NULL AND profile IS NULL)) AND
78 ttrss_user_prefs.pref_name = :pref_name AND
79 ttrss_prefs_types.id = type_id AND
80 owner_uid = :uid AND
81 ttrss_user_prefs.pref_name = ttrss_prefs.pref_name");
82 $sth->execute([":uid" => $user_id, ":profile" => $profile, ":pref_name" => $pref_name]);
83
84 if ($row = $sth->fetch()) {
85 $value = $row["value"];
86 $type_name = $row["type_name"];
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 {
96 user_error("Fatal error, unknown preferences key: $pref_name (owner: $user_id)", $die_on_error ? E_USER_ERROR : E_USER_WARNING);
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) {
112 if ($strip_tags) $value = strip_tags($value);
113
114 if (!$user_id) {
115 $user_id = $_SESSION["uid"];
116 @$profile = $_SESSION["profile"];
117 } else {
118 $user_id = sprintf("%d", $user_id);
119 }
120
121 if (!$profile || get_schema_version() < 63) $profile = null;
122
123 $type_name = "";
124 $current_value = "";
125
126 if (isset($this->cache[$pref_name])) {
127 $type_name = $this->cache[$pref_name]["type"];
128 $current_value = $this->cache[$pref_name]["value"];
129 }
130
131 if (!$type_name) {
132 $sth = $this->pdo->prepare("SELECT type_name
133 FROM ttrss_prefs,ttrss_prefs_types
134 WHERE pref_name = ? AND type_id = ttrss_prefs_types.id");
135 $sth->execute([$pref_name]);
136
137 if ($row = $sth->fetch())
138 $type_name = $row["type_name"];
139
140 } else if ($current_value == $value) {
141 return;
142 }
143
144 if ($type_name) {
145 if ($type_name == "bool") {
146 if ($value == "1" || $value == "true") {
147 $value = "true";
148 } else {
149 $value = "false";
150 }
151 } else if ($type_name == "integer") {
152 $value = sprintf("%d", $value);
153 }
154
155 if ($pref_name == 'USER_TIMEZONE' && $value == '') {
156 $value = 'UTC';
157 }
158
159 $sth = $this->pdo->prepare("UPDATE ttrss_user_prefs SET
160 value = :value WHERE pref_name = :pref_name
161 AND (profile = :profile OR (:profile IS NULL AND profile IS NULL))
162 AND owner_uid = :uid");
163
164 $sth->execute([":pref_name" => $pref_name, ":value" => $value, ":uid" => $user_id, ":profile" => $profile]);
165
166 if ($user_id == $_SESSION["uid"]) {
167 $this->cache[$pref_name]["type"] = $type_name;
168 $this->cache[$pref_name]["value"] = $value;
169 }
170 }
171 }
172
173 }