prefs.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 (!is_numeric($profile) || !$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. if (!$user_id) {
  44. $user_id = $_SESSION["uid"];
  45. @$profile = $_SESSION["profile"];
  46. } else {
  47. $profile = false;
  48. }
  49. if ($user_id == $_SESSION['uid'] && isset($this->cache[$pref_name])) {
  50. $tuple = $this->cache[$pref_name];
  51. return $this->convert($tuple["value"], $tuple["type"]);
  52. }
  53. if (!is_numeric($profile) || !$profile || get_schema_version() < 63) $profile = null;
  54. $sth = $this->pdo->prepare("SELECT
  55. value,ttrss_prefs_types.type_name as type_name
  56. FROM
  57. ttrss_user_prefs,ttrss_prefs,ttrss_prefs_types
  58. WHERE
  59. (profile = :profile OR (:profile IS NULL AND profile IS NULL)) AND
  60. ttrss_user_prefs.pref_name = :pref_name AND
  61. ttrss_prefs_types.id = type_id AND
  62. owner_uid = :uid AND
  63. ttrss_user_prefs.pref_name = ttrss_prefs.pref_name");
  64. $sth->execute([":uid" => $user_id, ":profile" => $profile, ":pref_name" => $pref_name]);
  65. if ($row = $sth->fetch()) {
  66. $value = $row["value"];
  67. $type_name = $row["type_name"];
  68. if ($user_id == $_SESSION["uid"]) {
  69. $this->cache[$pref_name]["type"] = $type_name;
  70. $this->cache[$pref_name]["value"] = $value;
  71. }
  72. return $this->convert($value, $type_name);
  73. } else {
  74. user_error("Fatal error, unknown preferences key: $pref_name (owner: $user_id)", $die_on_error ? E_USER_ERROR : E_USER_WARNING);
  75. return null;
  76. }
  77. }
  78. function convert($value, $type_name) {
  79. if ($type_name == "bool") {
  80. return $value == "true";
  81. } else if ($type_name == "integer") {
  82. return (int)$value;
  83. } else {
  84. return $value;
  85. }
  86. }
  87. function write($pref_name, $value, $user_id = false, $strip_tags = true) {
  88. if ($strip_tags) $value = strip_tags($value);
  89. if (!$user_id) {
  90. $user_id = $_SESSION["uid"];
  91. @$profile = $_SESSION["profile"];
  92. } else {
  93. $profile = null;
  94. }
  95. if (!is_numeric($profile) || !$profile || get_schema_version() < 63) $profile = null;
  96. $type_name = "";
  97. $current_value = "";
  98. if (isset($this->cache[$pref_name])) {
  99. $type_name = $this->cache[$pref_name]["type"];
  100. $current_value = $this->cache[$pref_name]["value"];
  101. }
  102. if (!$type_name) {
  103. $sth = $this->pdo->prepare("SELECT type_name
  104. FROM ttrss_prefs,ttrss_prefs_types
  105. WHERE pref_name = ? AND type_id = ttrss_prefs_types.id");
  106. $sth->execute([$pref_name]);
  107. if ($row = $sth->fetch())
  108. $type_name = $row["type_name"];
  109. } else if ($current_value == $value) {
  110. return;
  111. }
  112. if ($type_name) {
  113. if ($type_name == "bool") {
  114. if ($value == "1" || $value == "true") {
  115. $value = "true";
  116. } else {
  117. $value = "false";
  118. }
  119. } else if ($type_name == "integer") {
  120. $value = (int)$value;
  121. }
  122. if ($pref_name == 'USER_TIMEZONE' && $value == '') {
  123. $value = 'UTC';
  124. }
  125. $sth = $this->pdo->prepare("UPDATE ttrss_user_prefs SET
  126. value = :value WHERE pref_name = :pref_name
  127. AND (profile = :profile OR (:profile IS NULL AND profile IS NULL))
  128. AND owner_uid = :uid");
  129. $sth->execute([":pref_name" => $pref_name, ":value" => $value, ":uid" => $user_id, ":profile" => $profile]);
  130. if ($user_id == $_SESSION["uid"]) {
  131. $this->cache[$pref_name]["type"] = $type_name;
  132. $this->cache[$pref_name]["value"] = $value;
  133. }
  134. }
  135. }
  136. }