]> git.wh0rd.org Git - tt-rss.git/blob - include/db-prefs.php
696aae5d16d9689896477912fb54909cd46d0eb8
[tt-rss.git] / include / db-prefs.php
1 <?php
2         require_once "config.php";
3         require_once "db.php";
4
5         if (!defined('DISABLE_SESSIONS') && !defined('PREFS_NO_CACHE')) {
6                 if (!$_SESSION["prefs_cache"])
7                         $_SESSION["prefs_cache"] = array();
8         }
9
10         function get_pref($link, $pref_name, $user_id = false, $die_on_error = false) {
11
12                 $pref_name = db_escape_string($pref_name);
13                 $prefs_cache = true;
14                 $profile = false;
15
16                 if (!$user_id) {
17                         $user_id = $_SESSION["uid"];
18                         @$profile = $_SESSION["profile"];
19                 } else {
20                         $user_id = sprintf("%d", $user_id);
21                         //$prefs_cache = false;
22                 }
23
24                 if ($prefs_cache && !defined('DISABLE_SESSIONS') && !defined('PREFS_NO_CACHE')) {
25                         if ($_SESSION["prefs_cache"] && @$_SESSION["prefs_cache"][$pref_name]) {
26                                 $tuple = $_SESSION["prefs_cache"][$pref_name];
27                                 return convert_pref_type($tuple["value"], $tuple["type"]);
28                         }
29                 }
30
31                 if ($profile) {
32                         $profile_qpart = "profile = '$profile' AND";
33                 } else {
34                         $profile_qpart = "profile IS NULL AND";
35                 }
36
37                 if (get_schema_version($link) < 63) $profile_qpart = "";
38
39                 $result = db_query($link, "SELECT
40                         value,ttrss_prefs_types.type_name as type_name
41                         FROM
42                                 ttrss_user_prefs,ttrss_prefs,ttrss_prefs_types
43                         WHERE
44                                 $profile_qpart
45                                 ttrss_user_prefs.pref_name = '$pref_name' AND
46                                 ttrss_prefs_types.id = type_id AND
47                                 owner_uid = '$user_id' AND
48                                 ttrss_user_prefs.pref_name = ttrss_prefs.pref_name");
49
50                 if (db_num_rows($result) > 0) {
51                         $value = db_fetch_result($result, 0, "value");
52                         $type_name = db_fetch_result($result, 0, "type_name");
53
54                         if (!defined('DISABLE_SESSIONS')) {
55                                 if ($user_id == $_SESSION["uid"]) {
56                                         $_SESSION["prefs_cache"][$pref_name]["type"] = $type_name;
57                                         $_SESSION["prefs_cache"][$pref_name]["value"] = $value;
58                                 }
59                         }
60
61                         return convert_pref_type($value, $type_name);
62
63                 } else {
64                         if ($die_on_error) {
65                                 die("Fatal error, unknown preferences key: $pref_name");
66                         } else {
67                                 return null;
68                         }
69                 }
70         }
71
72         function convert_pref_type($value, $type_name) {
73                 if ($type_name == "bool") {
74                         return $value == "true";
75                 } else if ($type_name == "integer") {
76                         return sprintf("%d", $value);
77                 } else {
78                         return $value;
79                 }
80         }
81
82         function set_pref($link, $pref_name, $value, $user_id = false, $strip_tags = true) {
83                 $pref_name = db_escape_string($pref_name);
84                 $value = db_escape_string($value, $strip_tags);
85
86                 if (!$user_id) {
87                         $user_id = $_SESSION["uid"];
88                         @$profile = $_SESSION["profile"];
89                 } else {
90                         $user_id = sprintf("%d", $user_id);
91                         $prefs_cache = false;
92                 }
93
94                 if ($profile) {
95                         $profile_qpart = "AND profile = '$profile'";
96                 } else {
97                         $profile_qpart = "AND profile IS NULL";
98                 }
99
100                 if (get_schema_version($link) < 63) $profile_qpart = "";
101
102                 $type_name = "";
103                 $current_value = "";
104
105                 if (!defined('DISABLE_SESSIONS') && !defined('PREFS_NO_CACHE')) {
106                         if ($_SESSION["prefs_cache"] && @$_SESSION["prefs_cache"][$pref_name]) {
107                                 $type_name = $_SESSION["prefs_cache"][$pref_name]["type"];
108                                 $current_value = $_SESSION["prefs_cache"][$pref_name]["value"];
109                         }
110                 }
111
112                 if (!$type_name) {
113                         $result = db_query($link, "SELECT type_name
114                                 FROM ttrss_prefs,ttrss_prefs_types
115                                 WHERE pref_name = '$pref_name' AND type_id = ttrss_prefs_types.id");
116
117                         if (db_num_rows($result) > 0)
118                                 $type_name = db_fetch_result($result, 0, "type_name");
119                 } else if ($current_value == $value) {
120                         return;
121                 }
122
123                 if ($type_name) {
124                         if ($type_name == "bool") {
125                                 if ($value == "1" || $value == "true") {
126                                         $value = "true";
127                                 } else {
128                                         $value = "false";
129                                 }
130                         } else if ($type_name == "integer") {
131                                 $value = sprintf("%d", $value);
132                         }
133
134                         if ($pref_name == 'DEFAULT_ARTICLE_LIMIT' && $value == 0) {
135                                 $value = 30;
136                         }
137
138                         if ($pref_name == 'USER_TIMEZONE' && $value == '') {
139                                 $value = 'UTC';
140                         }
141
142                         db_query($link, "UPDATE ttrss_user_prefs SET
143                                 value = '$value' WHERE pref_name = '$pref_name'
144                                         $profile_qpart
145                                         AND owner_uid = " . $_SESSION["uid"]);
146
147                         if (!defined('DISABLE_SESSIONS')) {
148                                 if ($user_id == $_SESSION["uid"]) {
149                                         $_SESSION["prefs_cache"][$pref_name]["type"] = $type_name;
150                                         $_SESSION["prefs_cache"][$pref_name]["value"] = $value;
151                                 }
152                         }
153                 }
154         }
155 ?>