]>
Commit | Line | Data |
---|---|---|
1d3a17c7 | 1 | <?php |
3bac89ad AD |
2 | require_once "config.php"; |
3 | require_once "db.php"; | |
4 | ||
f5a5bae4 | 5 | if (!defined('DISABLE_SESSIONS')) { |
de696427 AD |
6 | if (!$_SESSION["prefs_cache"]) |
7 | $_SESSION["prefs_cache"] = array(); | |
8 | } | |
11de82c3 | 9 | |
b4c27af7 | 10 | function get_pref($link, $pref_name, $user_id = false, $die_on_error = false) { |
3bac89ad AD |
11 | |
12 | $pref_name = db_escape_string($pref_name); | |
70364a42 | 13 | $prefs_cache = true; |
3bac89ad | 14 | |
a2770077 AD |
15 | if (!$user_id) { |
16 | $user_id = $_SESSION["uid"]; | |
d9084cf2 | 17 | $profile = $_SESSION["profile"]; |
a2770077 AD |
18 | } else { |
19 | $user_id = sprintf("%d", $user_id); | |
11de82c3 AD |
20 | $prefs_cache = false; |
21 | } | |
658e1cc1 | 22 | |
d9084cf2 AD |
23 | if ($profile) { |
24 | $profile_qpart = "profile = '$profile'"; | |
25 | } else { | |
26 | $profile_qpart = "profile IS NULL"; | |
27 | } | |
28 | ||
29 | ||
70364a42 | 30 | if ($prefs_cache && !defined('DISABLE_SESSIONS') && !SINGLE_USER_MODE) { |
658e1cc1 AD |
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 | } | |
a2770077 AD |
35 | } |
36 | ||
4062022e AD |
37 | $result = db_query($link, "SELECT |
38 | value,ttrss_prefs_types.type_name as type_name | |
ff485f1d AD |
39 | FROM |
40 | ttrss_user_prefs,ttrss_prefs,ttrss_prefs_types | |
41 | WHERE | |
d9084cf2 | 42 | $profile_qpart AND |
ff485f1d AD |
43 | ttrss_user_prefs.pref_name = '$pref_name' AND |
44 | ttrss_prefs_types.id = type_id AND | |
a2770077 | 45 | owner_uid = '$user_id' AND |
ff485f1d | 46 | ttrss_user_prefs.pref_name = ttrss_prefs.pref_name"); |
3bac89ad AD |
47 | |
48 | if (db_num_rows($result) > 0) { | |
49 | $value = db_fetch_result($result, 0, "value"); | |
4062022e AD |
50 | $type_name = db_fetch_result($result, 0, "type_name"); |
51 | ||
1503d397 | 52 | if (!defined('DISABLE_SESSIONS') && !SINGLE_USER_MODE) { |
658e1cc1 AD |
53 | if ($user_id = $_SESSION["uid"]) { |
54 | $_SESSION["prefs_cache"][$pref_name]["type"] = $type_name; | |
55 | $_SESSION["prefs_cache"][$pref_name]["value"] = $value; | |
56 | } | |
4062022e | 57 | } |
658e1cc1 | 58 | |
dc0c8985 | 59 | return convert_pref_type($value, $type_name); |
4062022e | 60 | |
3bac89ad | 61 | } else { |
1e107b0f AD |
62 | if ($die_on_error) { |
63 | die("Fatal error, unknown preferences key: $pref_name"); | |
64 | } else { | |
65 | return null; | |
66 | } | |
3bac89ad AD |
67 | } |
68 | } | |
69 | ||
dc0c8985 AD |
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 | } | |
7f123cda | 79 | |
5f798ab1 | 80 | function set_pref($link, $key, $value, $user_id = false) { |
7f123cda AD |
81 | $key = db_escape_string($key); |
82 | $value = db_escape_string($value); | |
7f123cda | 83 | |
d9084cf2 AD |
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 | ||
e9105eb5 AD |
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' | |
d9084cf2 | 122 | AND $profile_qpart |
e9105eb5 | 123 | AND owner_uid = " . $_SESSION["uid"]); |
7f123cda | 124 | |
e9105eb5 AD |
125 | $_SESSION["prefs_cache"] = array(); |
126 | ||
127 | } | |
7f123cda | 128 | } |
3bac89ad | 129 | ?> |