Session.hx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package php;
  2. /**
  3. * TODO: TEST IT!
  4. */
  5. class Session {
  6. public static function getCacheLimiter() {
  7. switch(untyped __call__("session_cache_limiter")) {
  8. case "public":
  9. return Public;
  10. case "private":
  11. return Private;
  12. case "nocache":
  13. return NoCache;
  14. case "private_no_expire":
  15. return PrivateNoExpire;
  16. }
  17. return null;
  18. }
  19. public static function setCacheLimiter(l : CacheLimiter) {
  20. if(_started) throw "You can't set the cache limiter while the session is already in use";
  21. switch(l) {
  22. case Public:
  23. untyped __call__("session_cache_limiter", "public");
  24. case Private:
  25. untyped __call__("session_cache_limiter", "private");
  26. case NoCache:
  27. untyped __call__("session_cache_limiter", "nocache");
  28. case PrivateNoExpire:
  29. untyped __call__("session_cache_limiter", "private_no_expire");
  30. }
  31. }
  32. public static function getCacheExpire() : Int {
  33. return untyped __call__("session_cache_expire");
  34. }
  35. public static function setCacheExpire(minutes : Int) {
  36. if(_started) throw "You can't set the cache expire time while the session is already in use";
  37. untyped __call__("session_cache_expire", minutes);
  38. }
  39. public static function setName(name : String) {
  40. if(_started) throw "You can't set the name while the session is already in use";
  41. untyped __call__("session_name", name);
  42. }
  43. public static function getName() : String {
  44. return untyped __call__("session_name");
  45. }
  46. public static function getId() : String {
  47. return untyped __call__("session_id");
  48. }
  49. public static function setId(id : String) {
  50. if(_started) throw "You can't set the session id while the session is already in use";
  51. untyped __call__("session_id", id);
  52. }
  53. public static function getSavePath() : String {
  54. return untyped __call__("session_save_path");
  55. }
  56. public static function setSavePath(path : String) {
  57. if(_started) throw "You can't set the save path while the session is already in use";
  58. untyped __call__("session_save_path", path);
  59. }
  60. public static function getModule() : String {
  61. return untyped __call__("session_module_name");
  62. }
  63. public static function setModule(module : String) {
  64. if(_started) throw "You can't set the module while the session is already in use";
  65. untyped __call__("session_module_name", module);
  66. }
  67. public static function regenerateId(?deleteold : Bool) : Bool {
  68. return untyped __call__("session_regenerate_id", deleteold);
  69. }
  70. public static function get(name : String) : Dynamic {
  71. start();
  72. return untyped __var__("_SESSION", name);
  73. }
  74. public static function set(name : String, value : Dynamic) {
  75. start();
  76. return untyped __set__("_SESSION", name, value);
  77. }
  78. public static function setCookieParams(?lifetime : Int, ?path : String, ?domain : String, ?secure : Bool, ?httponly : Bool) {
  79. if(_started) throw "You can't set the cookie params while the session is already in use";
  80. untyped __call__("session_get_cookie_params", lifetime, path, domain, secure, httponly);
  81. }
  82. public static function getCookieParams() : { lifetime : Int, path : String, domain : String, secure : Bool, httponly : Bool} {
  83. return untyped __call__("_hx_anonymous", untyped __call__("session_get_cookie_params"));
  84. }
  85. // TODO: completely untested
  86. public static function setSaveHandler(open : String -> String -> Bool, close : Void -> Bool, read : String -> String, write : String -> String -> Bool, destroy, gc) : Bool {
  87. return untyped __call__("session_set_save_handler", open, close, read, write, destroy, gc);
  88. }
  89. public static function exists(name : String) {
  90. start();
  91. return untyped __call__("array_key_exists", name, __var__("_SESSION"));
  92. }
  93. public static function remove(name : String) {
  94. start();
  95. untyped __call__("unset", __var__("_SESSION", name));
  96. }
  97. private static var _started = false;
  98. private static function start() {
  99. if(_started) return;
  100. _started = true;
  101. untyped __call__("session_start");
  102. }
  103. public function clear() {
  104. untyped __call__("session_unset");
  105. }
  106. public function close() {
  107. untyped __call__("session_write_close");
  108. _started = false; // TODO: not sure this useful; test if a closed session can be re-opened (I doubt)
  109. }
  110. }
  111. enum CacheLimiter {
  112. Public;
  113. Private;
  114. NoCache;
  115. PrivateNoExpire;
  116. }