prefs.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. class Db_Prefs {
  3. private $pdo;
  4. private static $instance;
  5. private $cache;
  6. function __construct() {
  7. $this->pdo = Db::pdo();
  8. $this->cache = array();
  9. if ($_SESSION["uid"]) $this->cache();
  10. }
  11. private function __clone() {
  12. //
  13. }
  14. public static function get() {
  15. if (self::$instance == null)
  16. self::$instance = new self();
  17. return self::$instance;
  18. }
  19. function cache() {
  20. $user_id = $_SESSION["uid"];
  21. @$profile = $_SESSION["profile"];
  22. if (!$profile || get_schema_version() < 63) $profile = null;
  23. $sth = $this->pdo->prepare("SELECT
  24. value,ttrss_prefs_types.type_name as type_name,ttrss_prefs.pref_name AS pref_name
  25. FROM
  26. ttrss_user_prefs,ttrss_prefs,ttrss_prefs_types
  27. WHERE
  28. (profile = :profile OR (:profile IS NULL AND profile IS NULL)) AND
  29. ttrss_prefs.pref_name NOT LIKE '_MOBILE%' AND
  30. ttrss_prefs_types.id = type_id AND
  31. owner_uid = :uid AND
  32. ttrss_user_prefs.pref_name = ttrss_prefs.pref_name");
  33. $sth->execute([":profile" => $profile, ":uid" => $user_id]);
  34. while ($line = $sth->fetch()) {
  35. if ($user_id == $_SESSION["uid"]) {
  36. $pref_name = $line["pref_name"];
  37. $this->cache[$pref_name]["type"] = $line["type_name"];
  38. $this->cache[$pref_name]["value"] = $line["value"];
  39. }
  40. }
  41. }
  42. function read($pref_name, $user_id = false, $die_on_error = false) {
  43. $profile = false;
  44. if (!$user_id) {
  45. $user_id = $_SESSION["uid"];
  46. @$profile = $_SESSION["profile"];
  47. } else {
  48. $user_id = sprintf("%d", $user_id);
  49. }
  50. if (isset($this->cache[$pref_name]) && !$user_id) {
  51. $tuple = $this->cache[$pref_name];
  52. return $this->convert($tuple["value"], $tuple["type"]);
  53. }
  54. if (!$profile || get_schema_version() < 63) $profile = null;
  55. $sth = $this->pdo->prepare("SELECT
  56. value,ttrss_prefs_types.type_name as type_name
  57. FROM
  58. ttrss_user_prefs,ttrss_prefs,ttrss_prefs_types
  59. WHERE
  60. (profile = :profile OR (:profile IS NULL AND profile IS NULL)) AND
  61. ttrss_user_prefs.pref_name = :pref_name AND
  62. ttrss_prefs_types.id = type_id AND
  63. owner_uid = :uid AND
  64. ttrss_user_prefs.pref_name = ttrss_prefs.pref_name");
  65. $sth->execute([":uid" => $user_id, ":profile" => $profile, ":pref_name" => $pref_name]);
  66. if ($row = $sth->fetch()) {
  67. $value = $row["value"];
  68. $type_name = $row["type_name"];
  69. if ($user_id == $_SESSION["uid"]) {
  70. $this->cache[$pref_name]["type"] = $type_name;
  71. $this->cache[$pref_name]["value"] = $value;
  72. }
  73. return $this->convert($value, $type_name);
  74. } else {
  75. user_error("Fatal error, unknown preferences key: $pref_name (owner: $user_id)", $die_on_error ? E_USER_ERROR : E_USER_WARNING);
  76. return null;
  77. }
  78. }
  79. function convert($value, $type_name) {
  80. if ($type_name == "bool") {
  81. return $value == "true";
  82. } else if ($type_name == "integer") {
  83. return (int)$value;
  84. } else {
  85. return $value;
  86. }
  87. }
  88. function write($pref_name, $value, $user_id = false, $strip_tags = true) {
  89. if ($strip_tags) $value = strip_tags($value);
  90. if (!$user_id) {
  91. $user_id = $_SESSION["uid"];
  92. @$profile = $_SESSION["profile"];
  93. } else {
  94. $user_id = sprintf("%d", $user_id);
  95. }
  96. if ($profile) {
  97. $profile_qpart = "AND profile = '$profile'";
  98. } else {
  99. $profile_qpart = "AND profile IS NULL";
  100. }
  101. if (!$profile || get_schema_version() < 63) $profile = null;
  102. $type_name = "";
  103. $current_value = "";
  104. if (isset($this->cache[$pref_name])) {
  105. $type_name = $this->cache[$pref_name]["type"];
  106. $current_value = $this->cache[$pref_name]["value"];
  107. }
  108. if (!$type_name) {
  109. $sth = $this->pdo->prepare("SELECT type_name
  110. FROM ttrss_prefs,ttrss_prefs_types
  111. WHERE pref_name = ? AND type_id = ttrss_prefs_types.id");
  112. $sth->execute([$pref_name]);
  113. if ($row = $sth->fetch())
  114. $type_name = $row["type_name"];
  115. } else if ($current_value == $value) {
  116. return;
  117. }
  118. if ($type_name) {
  119. if ($type_name == "bool") {
  120. if ($value == "1" || $value == "true") {
  121. $value = "true";
  122. } else {
  123. $value = "false";
  124. }
  125. } else if ($type_name == "integer") {
  126. $value = sprintf("%d", $value);
  127. }
  128. if ($pref_name == 'USER_TIMEZONE' && $value == '') {
  129. $value = 'UTC';
  130. }
  131. $sth = $this->pdo->prepare("UPDATE ttrss_user_prefs SET
  132. value = :value WHERE pref_name = :pref_name
  133. AND (profile = :profile OR (:profile IS NULL AND profile IS NULL))
  134. AND owner_uid = :uid");
  135. $sth->execute([":pref_name" => $pref_name, ":value" => $value, ":uid" => $user_id, ":profile" => $profile]);
  136. if ($user_id == $_SESSION["uid"]) {
  137. $this->cache[$pref_name]["type"] = $type_name;
  138. $this->cache[$pref_name]["value"] = $value;
  139. }
  140. }
  141. }
  142. }