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