]> git.wh0rd.org - tt-rss.git/blob - db-prefs.php
tweak auxDlg
[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
15 if (!$user_id) {
16 $user_id = $_SESSION["uid"];
17 $profile = $_SESSION["profile"];
18 } else {
19 $user_id = sprintf("%d", $user_id);
20 $prefs_cache = false;
21 }
22
23 if ($profile) {
24 $profile_qpart = "profile = '$profile'";
25 } else {
26 $profile_qpart = "profile IS NULL";
27 }
28
29
30 if ($prefs_cache && !defined('DISABLE_SESSIONS') && !SINGLE_USER_MODE) {
31 if ($_SESSION["prefs_cache"] && $_SESSION["prefs_cache"][$pref_name]) {
32 $tuple = $_SESSION["prefs_cache"][$pref_name];
33 return convert_pref_type($tuple["value"], $tuple["type"]);
34 }
35 }
36
37 $result = db_query($link, "SELECT
38 value,ttrss_prefs_types.type_name as type_name
39 FROM
40 ttrss_user_prefs,ttrss_prefs,ttrss_prefs_types
41 WHERE
42 $profile_qpart AND
43 ttrss_user_prefs.pref_name = '$pref_name' AND
44 ttrss_prefs_types.id = type_id AND
45 owner_uid = '$user_id' AND
46 ttrss_user_prefs.pref_name = ttrss_prefs.pref_name");
47
48 if (db_num_rows($result) > 0) {
49 $value = db_fetch_result($result, 0, "value");
50 $type_name = db_fetch_result($result, 0, "type_name");
51
52 if (!defined('DISABLE_SESSIONS') && !SINGLE_USER_MODE) {
53 if ($user_id = $_SESSION["uid"]) {
54 $_SESSION["prefs_cache"][$pref_name]["type"] = $type_name;
55 $_SESSION["prefs_cache"][$pref_name]["value"] = $value;
56 }
57 }
58
59 return convert_pref_type($value, $type_name);
60
61 } else {
62 if ($die_on_error) {
63 die("Fatal error, unknown preferences key: $pref_name");
64 } else {
65 return null;
66 }
67 }
68 }
69
70 function convert_pref_type($value, $type_name) {
71 if ($type_name == "bool") {
72 return $value == "true";
73 } else if ($type_name == "integer") {
74 return sprintf("%d", $value);
75 } else {
76 return $value;
77 }
78 }
79
80 function set_pref($link, $key, $value, $user_id = false) {
81 $key = db_escape_string($key);
82 $value = db_escape_string($value);
83
84 if (!$user_id) {
85 $user_id = $_SESSION["uid"];
86 $profile = $_SESSION["profile"];
87 } else {
88 $user_id = sprintf("%d", $user_id);
89 $prefs_cache = false;
90 }
91
92 if ($profile) {
93 $profile_qpart = "profile = '$profile'";
94 } else {
95 $profile_qpart = "profile IS NULL";
96 }
97
98 $result = db_query($link, "SELECT type_name
99 FROM ttrss_prefs,ttrss_prefs_types
100 WHERE pref_name = '$key' AND type_id = ttrss_prefs_types.id");
101
102 if (db_num_rows($result) > 0) {
103
104 $type_name = db_fetch_result($result, 0, "type_name");
105
106 if ($type_name == "bool") {
107 if ($value == "1" || $value == "true") {
108 $value = "true";
109 } else {
110 $value = "false";
111 }
112 } else if ($type_name == "integer") {
113 $value = sprintf("%d", $value);
114 }
115
116 if ($pref_name == 'DEFAULT_ARTICLE_LIMIT' && $value == 0) {
117 $value = 30;
118 }
119
120 db_query($link, "UPDATE ttrss_user_prefs SET
121 value = '$value' WHERE pref_name = '$key'
122 AND $profile_qpart
123 AND owner_uid = " . $_SESSION["uid"]);
124
125 $_SESSION["prefs_cache"] = array();
126
127 }
128 }
129 ?>