Session.hx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Copyright (C)2005-2017 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. package php;
  23. /**
  24. Session consists of a way to preserve certain data across
  25. subsequent accesses.
  26. */
  27. class Session {
  28. public static function getCacheLimiter() {
  29. switch(untyped __call__("session_cache_limiter")) {
  30. case "public":
  31. return Public;
  32. case "private":
  33. return Private;
  34. case "nocache":
  35. return NoCache;
  36. case "private_no_expire":
  37. return PrivateNoExpire;
  38. }
  39. return null;
  40. }
  41. public static function setCacheLimiter(l : CacheLimiter) {
  42. if(started) throw "You can't set the cache limiter while the session is already in use";
  43. switch(l) {
  44. case Public:
  45. untyped __call__("session_cache_limiter", "public");
  46. case Private:
  47. untyped __call__("session_cache_limiter", "private");
  48. case NoCache:
  49. untyped __call__("session_cache_limiter", "nocache");
  50. case PrivateNoExpire:
  51. untyped __call__("session_cache_limiter", "private_no_expire");
  52. }
  53. }
  54. public static function getCacheExpire() : Int {
  55. return untyped __call__("session_cache_expire");
  56. }
  57. public static function setCacheExpire(minutes : Int) {
  58. if(started) throw "You can't set the cache expire time while the session is already in use";
  59. untyped __call__("session_cache_expire", minutes);
  60. }
  61. public static function setName(name : String) {
  62. if(started) throw "You can't set the name while the session is already in use";
  63. untyped __call__("session_name", name);
  64. }
  65. public static function getName() : String {
  66. return untyped __call__("session_name");
  67. }
  68. public static function getId() : String {
  69. return untyped __call__("session_id");
  70. }
  71. public static function setId(id : String) {
  72. if(started) throw "You can't set the session id while the session is already in use";
  73. untyped __call__("session_id", id);
  74. }
  75. public static function getSavePath() : String {
  76. return untyped __call__("session_save_path");
  77. }
  78. public static function setSavePath(path : String) {
  79. if(started) throw "You can't set the save path while the session is already in use";
  80. untyped __call__("session_save_path", path);
  81. }
  82. public static function getModule() : String {
  83. return untyped __call__("session_module_name");
  84. }
  85. public static function setModule(module : String) {
  86. if(started) throw "You can't set the module while the session is already in use";
  87. untyped __call__("session_module_name", module);
  88. }
  89. public static function regenerateId(?deleteold : Bool) : Bool {
  90. return untyped __call__("session_regenerate_id", deleteold);
  91. }
  92. public static function get(name : String) : Dynamic {
  93. start();
  94. if(!untyped __call__('isset', __var__("_SESSION", name))) return null;
  95. return untyped __var__("_SESSION", name);
  96. }
  97. public static function set(name : String, value : Dynamic) {
  98. start();
  99. return untyped __set__("_SESSION", name, value);
  100. }
  101. public static function setCookieParams(?lifetime : Int, ?path : String, ?domain : String, ?secure : Bool, ?httponly : Bool) {
  102. if(started) throw "You can't set the cookie params while the session is already in use";
  103. untyped __call__("session_set_cookie_params", lifetime, path, domain, secure, httponly);
  104. }
  105. public static function getCookieParams() : { lifetime : Int, path : String, domain : String, secure : Bool, httponly : Bool} {
  106. return untyped __call__("_hx_anonymous", untyped __call__("session_get_cookie_params"));
  107. }
  108. // TODO: completely untested
  109. public static function setSaveHandler(open : String -> String -> Bool, close : Void -> Bool, read : String -> String, write : String -> String -> Bool, destroy, gc) : Bool {
  110. return untyped __call__("session_set_save_handler", open, close, read, write, destroy, gc);
  111. }
  112. public static function exists(name : String) {
  113. start();
  114. return untyped __call__("array_key_exists", name, __var__("_SESSION"));
  115. }
  116. public static function remove(name : String) {
  117. start();
  118. untyped __call__("unset", __var__("_SESSION", name));
  119. }
  120. public static var started(default, null) : Bool;
  121. public static function start() {
  122. if(started) return;
  123. started = true;
  124. untyped __call__("session_start");
  125. }
  126. public static function clear() {
  127. untyped __call__("session_unset");
  128. }
  129. public static function close() {
  130. untyped __call__("session_write_close");
  131. started = false; // TODO: not sure this useful; test if a closed session can be re-opened (I doubt)
  132. }
  133. static function __init__()
  134. {
  135. started = untyped __call__("isset", __var__("_SESSION"));
  136. }
  137. }
  138. enum CacheLimiter {
  139. Public;
  140. Private;
  141. NoCache;
  142. PrivateNoExpire;
  143. }