prefs.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 || get_schema_version() < 63) $profile = null;
  97. $type_name = "";
  98. $current_value = "";
  99. if (isset($this->cache[$pref_name])) {
  100. $type_name = $this->cache[$pref_name]["type"];
  101. $current_value = $this->cache[$pref_name]["value"];
  102. }
  103. if (!$type_name) {
  104. $sth = $this->pdo->prepare("SELECT type_name
  105. FROM ttrss_prefs,ttrss_prefs_types
  106. WHERE pref_name = ? AND type_id = ttrss_prefs_types.id");
  107. $sth->execute([$pref_name]);
  108. if ($row = $sth->fetch())
  109. $type_name = $row["type_name"];
  110. } else if ($current_value == $value) {
  111. return;
  112. }
  113. if ($type_name) {
  114. if ($type_name == "bool") {
  115. if ($value == "1" || $value == "true") {
  116. $value = "true";
  117. } else {
  118. $value = "false";
  119. }
  120. } else if ($type_name == "integer") {
  121. $value = sprintf("%d", $value);
  122. }
  123. if ($pref_name == 'USER_TIMEZONE' && $value == '') {
  124. $value = 'UTC';
  125. }
  126. $sth = $this->pdo->prepare("UPDATE ttrss_user_prefs SET
  127. value = :value WHERE pref_name = :pref_name
  128. AND (profile = :profile OR (:profile IS NULL AND profile IS NULL))
  129. AND owner_uid = :uid");
  130. $sth->execute([":pref_name" => $pref_name, ":value" => $value, ":uid" => $user_id, ":profile" => $profile]);
  131. if ($user_id == $_SESSION["uid"]) {
  132. $this->cache[$pref_name]["type"] = $type_name;
  133. $this->cache[$pref_name]["value"] = $value;
  134. }
  135. }
  136. }
  137. }