db-prefs.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. require_once "config.php";
  3. require_once "db.php";
  4. if (!defined('DISABLE_SESSIONS') && !defined('PREFS_NO_CACHE')) {
  5. if (!$_SESSION["prefs_cache"])
  6. $_SESSION["prefs_cache"] = array();
  7. }
  8. function cache_prefs() {
  9. $profile = false;
  10. $user_id = $_SESSION["uid"];
  11. @$profile = $_SESSION["profile"];
  12. if ($profile) {
  13. $profile_qpart = "profile = '$profile' AND";
  14. } else {
  15. $profile_qpart = "profile IS NULL AND";
  16. }
  17. if (get_schema_version() < 63) $profile_qpart = "";
  18. $result = db_query( "SELECT
  19. value,ttrss_prefs_types.type_name as type_name,ttrss_prefs.pref_name AS pref_name
  20. FROM
  21. ttrss_user_prefs,ttrss_prefs,ttrss_prefs_types
  22. WHERE
  23. $profile_qpart
  24. ttrss_prefs.pref_name NOT LIKE '_MOBILE%' AND
  25. ttrss_prefs_types.id = type_id AND
  26. owner_uid = '$user_id' AND
  27. ttrss_user_prefs.pref_name = ttrss_prefs.pref_name");
  28. while ($line = db_fetch_assoc($result)) {
  29. if ($user_id == $_SESSION["uid"]) {
  30. $pref_name = $line["pref_name"];
  31. $_SESSION["prefs_cache"][$pref_name]["type"] = $line["type_name"];
  32. $_SESSION["prefs_cache"][$pref_name]["value"] = $line["value"];
  33. }
  34. }
  35. }
  36. function get_pref( $pref_name, $user_id = false, $die_on_error = false) {
  37. $pref_name = db_escape_string( $pref_name);
  38. $prefs_cache = true;
  39. $profile = false;
  40. if (!$user_id) {
  41. $user_id = $_SESSION["uid"];
  42. @$profile = $_SESSION["profile"];
  43. } else {
  44. $user_id = sprintf("%d", $user_id);
  45. //$prefs_cache = false;
  46. }
  47. if ($prefs_cache && !defined('DISABLE_SESSIONS') && !defined('PREFS_NO_CACHE')) {
  48. if ($_SESSION["prefs_cache"] && @$_SESSION["prefs_cache"][$pref_name]) {
  49. $tuple = $_SESSION["prefs_cache"][$pref_name];
  50. return convert_pref_type($tuple["value"], $tuple["type"]);
  51. }
  52. }
  53. if ($profile) {
  54. $profile_qpart = "profile = '$profile' AND";
  55. } else {
  56. $profile_qpart = "profile IS NULL AND";
  57. }
  58. if (get_schema_version() < 63) $profile_qpart = "";
  59. $result = db_query( "SELECT
  60. value,ttrss_prefs_types.type_name as type_name
  61. FROM
  62. ttrss_user_prefs,ttrss_prefs,ttrss_prefs_types
  63. WHERE
  64. $profile_qpart
  65. ttrss_user_prefs.pref_name = '$pref_name' AND
  66. ttrss_prefs_types.id = type_id AND
  67. owner_uid = '$user_id' AND
  68. ttrss_user_prefs.pref_name = ttrss_prefs.pref_name");
  69. if (db_num_rows($result) > 0) {
  70. $value = db_fetch_result($result, 0, "value");
  71. $type_name = db_fetch_result($result, 0, "type_name");
  72. if (!defined('DISABLE_SESSIONS')) {
  73. if ($user_id == $_SESSION["uid"]) {
  74. $_SESSION["prefs_cache"][$pref_name]["type"] = $type_name;
  75. $_SESSION["prefs_cache"][$pref_name]["value"] = $value;
  76. }
  77. }
  78. return convert_pref_type($value, $type_name);
  79. } else {
  80. if ($die_on_error) {
  81. die("Fatal error, unknown preferences key: $pref_name");
  82. } else {
  83. return null;
  84. }
  85. }
  86. }
  87. function convert_pref_type($value, $type_name) {
  88. if ($type_name == "bool") {
  89. return $value == "true";
  90. } else if ($type_name == "integer") {
  91. return sprintf("%d", $value);
  92. } else {
  93. return $value;
  94. }
  95. }
  96. function set_pref( $pref_name, $value, $user_id = false, $strip_tags = true) {
  97. $pref_name = db_escape_string( $pref_name);
  98. $value = db_escape_string( $value, $strip_tags);
  99. if (!$user_id) {
  100. $user_id = $_SESSION["uid"];
  101. @$profile = $_SESSION["profile"];
  102. } else {
  103. $user_id = sprintf("%d", $user_id);
  104. $prefs_cache = false;
  105. }
  106. if ($profile) {
  107. $profile_qpart = "AND profile = '$profile'";
  108. } else {
  109. $profile_qpart = "AND profile IS NULL";
  110. }
  111. if (get_schema_version() < 63) $profile_qpart = "";
  112. $type_name = "";
  113. $current_value = "";
  114. if (!defined('DISABLE_SESSIONS') && !defined('PREFS_NO_CACHE')) {
  115. if ($_SESSION["prefs_cache"] && @$_SESSION["prefs_cache"][$pref_name]) {
  116. $type_name = $_SESSION["prefs_cache"][$pref_name]["type"];
  117. $current_value = $_SESSION["prefs_cache"][$pref_name]["value"];
  118. }
  119. }
  120. if (!$type_name) {
  121. $result = db_query( "SELECT type_name
  122. FROM ttrss_prefs,ttrss_prefs_types
  123. WHERE pref_name = '$pref_name' AND type_id = ttrss_prefs_types.id");
  124. if (db_num_rows($result) > 0)
  125. $type_name = db_fetch_result($result, 0, "type_name");
  126. } else if ($current_value == $value) {
  127. return;
  128. }
  129. if ($type_name) {
  130. if ($type_name == "bool") {
  131. if ($value == "1" || $value == "true") {
  132. $value = "true";
  133. } else {
  134. $value = "false";
  135. }
  136. } else if ($type_name == "integer") {
  137. $value = sprintf("%d", $value);
  138. }
  139. if ($pref_name == 'USER_TIMEZONE' && $value == '') {
  140. $value = 'UTC';
  141. }
  142. db_query( "UPDATE ttrss_user_prefs SET
  143. value = '$value' WHERE pref_name = '$pref_name'
  144. $profile_qpart
  145. AND owner_uid = " . $_SESSION["uid"]);
  146. if (!defined('DISABLE_SESSIONS')) {
  147. if ($user_id == $_SESSION["uid"]) {
  148. $_SESSION["prefs_cache"][$pref_name]["type"] = $type_name;
  149. $_SESSION["prefs_cache"][$pref_name]["value"] = $value;
  150. }
  151. }
  152. }
  153. }
  154. ?>