Session.hx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. if(!untyped __call__('isset', __var__("_SESSION", name))) return null;
  73. return untyped __var__("_SESSION", name);
  74. }
  75. public static function set(name : String, value : Dynamic) {
  76. start();
  77. return untyped __set__("_SESSION", name, value);
  78. }
  79. public static function setCookieParams(?lifetime : Int, ?path : String, ?domain : String, ?secure : Bool, ?httponly : Bool) {
  80. if(started) throw "You can't set the cookie params while the session is already in use";
  81. untyped __call__("session_set_cookie_params", lifetime, path, domain, secure, httponly);
  82. }
  83. public static function getCookieParams() : { lifetime : Int, path : String, domain : String, secure : Bool, httponly : Bool} {
  84. return untyped __call__("_hx_anonymous", untyped __call__("session_get_cookie_params"));
  85. }
  86. // TODO: completely untested
  87. public static function setSaveHandler(open : String -> String -> Bool, close : Void -> Bool, read : String -> String, write : String -> String -> Bool, destroy, gc) : Bool {
  88. return untyped __call__("session_set_save_handler", open, close, read, write, destroy, gc);
  89. }
  90. public static function exists(name : String) {
  91. start();
  92. return untyped __call__("array_key_exists", name, __var__("_SESSION"));
  93. }
  94. public static function remove(name : String) {
  95. start();
  96. untyped __call__("unset", __var__("_SESSION", name));
  97. }
  98. public static var started(default, null) : Bool;
  99. public static function start() {
  100. if(started) return;
  101. started = true;
  102. untyped __call__("session_start");
  103. }
  104. public static function clear() {
  105. untyped __call__("session_unset");
  106. }
  107. public static function close() {
  108. untyped __call__("session_write_close");
  109. started = false; // TODO: not sure this useful; test if a closed session can be re-opened (I doubt)
  110. }
  111. static function __init__()
  112. {
  113. started = untyped __call__("isset", __var__("_SESSION"));
  114. }
  115. }
  116. enum CacheLimiter {
  117. Public;
  118. Private;
  119. NoCache;
  120. PrivateNoExpire;
  121. }