2 require_once "config.php";
5 if (!defined('DISABLE_SESSIONS')) {
6 if (!$_SESSION["prefs_cache"])
7 $_SESSION["prefs_cache"] = array();
10 function get_pref($link, $pref_name, $user_id = false, $die_on_error = false) {
12 $pref_name = db_escape_string($pref_name);
17 $user_id = $_SESSION["uid"];
18 @$profile = $_SESSION["profile"];
20 $user_id = sprintf("%d", $user_id);
25 $profile_qpart = "profile = '$profile' AND";
27 $profile_qpart = "profile IS NULL AND";
30 if (get_schema_version($link) < 63) $profile_qpart = "";
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"]);
39 $result = db_query($link, "SELECT
40 value,ttrss_prefs_types.type_name as type_name
42 ttrss_user_prefs,ttrss_prefs,ttrss_prefs_types
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");
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");
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;
61 return convert_pref_type($value, $type_name);
65 die("Fatal error, unknown preferences key: $pref_name");
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);
82 function set_pref($link, $key, $value, $user_id = false) {
83 $key = db_escape_string($key);
84 $value = db_escape_string($value);
87 $user_id = $_SESSION["uid"];
88 @$profile = $_SESSION["profile"];
90 $user_id = sprintf("%d", $user_id);
95 $profile_qpart = "AND profile = '$profile'";
97 $profile_qpart = "AND profile IS NULL";
100 if (get_schema_version($link) < 63) $profile_qpart = "";
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");
106 if (db_num_rows($result) > 0) {
108 $type_name = db_fetch_result($result, 0, "type_name");
110 if ($type_name == "bool") {
111 if ($value == "1" || $value == "true") {
116 } else if ($type_name == "integer") {
117 $value = sprintf("%d", $value);
120 if ($key == 'DEFAULT_ARTICLE_LIMIT' && $value == 0) {
124 if ($key == 'USER_TIMEZONE' && $value == '') {
128 db_query($link, "UPDATE ttrss_user_prefs SET
129 value = '$value' WHERE pref_name = '$key'
131 AND owner_uid = " . $_SESSION["uid"]);
133 $_SESSION["prefs_cache"] = array();