db-prefs.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. require_once "config.php";
  3. require_once "db.php";
  4. if (!defined('DISABLE_SESSIONS')) {
  5. if (!$_SESSION["prefs_cache"])
  6. $_SESSION["prefs_cache"] = array();
  7. }
  8. function get_pref($link, $pref_name, $user_id = false, $die_on_error = false) {
  9. $prefs_cache = true;
  10. if (!$user_id) {
  11. $user_id = $_SESSION["uid"];
  12. } else {
  13. $user_id = sprintf("%d", $user_id);
  14. //$prefs_cache = false;
  15. }
  16. if ($prefs_cache && !defined('DISABLE_SESSIONS')) {
  17. if ($_SESSION["prefs_cache"] && @$_SESSION["prefs_cache"][$pref_name]) {
  18. $tuple = $_SESSION["prefs_cache"][$pref_name];
  19. return convert_pref_type($tuple["value"], $tuple["type"]);
  20. }
  21. }
  22. $dbh = DB::get();
  23. $sth = $dbh->prepare("SELECT
  24. value,ttirc_prefs_types.type_name as type_name
  25. FROM
  26. ttirc_user_prefs,ttirc_prefs,ttirc_prefs_types
  27. WHERE
  28. ttirc_user_prefs.pref_name = ? AND
  29. ttirc_prefs_types.id = type_id AND
  30. owner_uid = ? AND
  31. ttirc_user_prefs.pref_name = ttirc_prefs.pref_name");
  32. $sth->execute([$pref_name, $user_id]);
  33. if ($row = $sth->fetch()) {
  34. $value = $row['value'];
  35. $type_name = $row['type_name'];
  36. if (!defined('DISABLE_SESSIONS')) {
  37. if ($user_id == $_SESSION["uid"]) {
  38. $_SESSION["prefs_cache"][$pref_name]["type"] = $type_name;
  39. $_SESSION["prefs_cache"][$pref_name]["value"] = $value;
  40. }
  41. }
  42. return convert_pref_type($value, $type_name);
  43. } else {
  44. if ($die_on_error) {
  45. die("Fatal error, unknown preferences key: $pref_name");
  46. } else {
  47. return null;
  48. }
  49. }
  50. }
  51. function convert_pref_type($value, $type_name) {
  52. if ($type_name == "bool") {
  53. return $value == "true";
  54. } else if ($type_name == "integer") {
  55. return sprintf("%d", $value);
  56. } else {
  57. return $value;
  58. }
  59. }
  60. function set_pref($link, $pref_name, $value, $user_id = false, $strip_tags = true) {
  61. $pref_name = db_escape_string($pref_name);
  62. $value = db_escape_string($value, $strip_tags);
  63. if (!$user_id) {
  64. $user_id = $_SESSION["uid"];
  65. } else {
  66. $user_id = sprintf("%d", $user_id);
  67. $prefs_cache = false;
  68. }
  69. $type_name = "";
  70. $current_value = "";
  71. if (!defined('DISABLE_SESSIONS')) {
  72. if ($_SESSION["prefs_cache"] && @$_SESSION["prefs_cache"][$pref_name]) {
  73. $type_name = $_SESSION["prefs_cache"][$pref_name]["type"];
  74. $current_value = $_SESSION["prefs_cache"][$pref_name]["value"];
  75. }
  76. }
  77. if (!$type_name) {
  78. $result = db_query($link, "SELECT type_name
  79. FROM ttirc_prefs,ttirc_prefs_types
  80. WHERE pref_name = '$pref_name' AND type_id = ttirc_prefs_types.id");
  81. if (db_num_rows($result) > 0)
  82. $type_name = db_fetch_result($result, 0, "type_name");
  83. } else if ($current_value == $value) {
  84. return;
  85. }
  86. if ($type_name) {
  87. if ($type_name == "bool") {
  88. if ($value == "1" || $value == "true") {
  89. $value = "true";
  90. } else {
  91. $value = "false";
  92. }
  93. } else if ($type_name == "integer") {
  94. $value = sprintf("%d", $value);
  95. }
  96. if ($pref_name == 'USER_TIMEZONE' && $value == '') {
  97. $value = 'UTC';
  98. }
  99. $dbh = DB::get();
  100. $sth = $dbh->prepare("UPDATE ttirc_user_prefs SET
  101. value = ? WHERE pref_name = ?
  102. AND owner_uid = ?");
  103. $sth->execute([$value, $pref_name, $user_id]);
  104. if (!defined('DISABLE_SESSIONS')) {
  105. if ($user_id == $_SESSION["uid"]) {
  106. $_SESSION["prefs_cache"][$pref_name]["type"] = $type_name;
  107. $_SESSION["prefs_cache"][$pref_name]["value"] = $value;
  108. }
  109. }
  110. }
  111. }
  112. ?>