]> git.wh0rd.org Git - tt-rss.git/blob - db-prefs.php
properly handle relative link elements for feed and articles (closes #358)
[tt-rss.git] / db-prefs.php
1 <?php
2         require_once "config.php";
3         require_once "db.php";
4
5         if (!defined('DISABLE_SESSIONS')) {
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 ($profile) {
25                         $profile_qpart = "profile = '$profile' AND";
26                 } else {
27                         $profile_qpart = "profile IS NULL AND";
28                 }
29
30                 if (get_schema_version($link) < 63) $profile_qpart = "";
31
32                 if ($prefs_cache && !defined('DISABLE_SESSIONS') && !SINGLE_USER_MODE) {        
33                         if ($_SESSION["prefs_cache"] && @$_SESSION["prefs_cache"][$pref_name]) {
34                                 $tuple = $_SESSION["prefs_cache"][$pref_name];
35                                 return convert_pref_type($tuple["value"], $tuple["type"]);
36                         }
37                 }
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') && !SINGLE_USER_MODE) {        
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, $key, $value, $user_id = false) {
83                 $key = db_escape_string($key);
84                 $value = db_escape_string($value);
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                 $result = db_query($link, "SELECT type_name 
103                         FROM ttrss_prefs,ttrss_prefs_types 
104                         WHERE pref_name = '$key' AND type_id = ttrss_prefs_types.id");
105
106                 if (db_num_rows($result) > 0) {
107
108                         $type_name = db_fetch_result($result, 0, "type_name");
109
110                         if ($type_name == "bool") {
111                                 if ($value == "1" || $value == "true") {
112                                         $value = "true";
113                                 } else {
114                                         $value = "false";
115                                 }
116                         } else if ($type_name == "integer") {
117                                 $value = sprintf("%d", $value);
118                         }
119
120                         if ($key == 'DEFAULT_ARTICLE_LIMIT' && $value == 0) {
121                                 $value = 30;
122                         }
123
124                         if ($key == 'USER_TIMEZONE' && $value == '') {
125                                 $value = 'UTC';
126                         }
127
128                         db_query($link, "UPDATE ttrss_user_prefs SET 
129                                 value = '$value' WHERE pref_name = '$key' 
130                                         $profile_qpart
131                                         AND owner_uid = " . $_SESSION["uid"]);
132
133                         $_SESSION["prefs_cache"] = array();
134
135                 }
136         }
137 ?>