Session.hx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Copyright (C)2005-2012 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. * TODO: TEST IT!
  25. */
  26. class Session {
  27. public static function getCacheLimiter() {
  28. switch(untyped __call__("session_cache_limiter")) {
  29. case "public":
  30. return Public;
  31. case "private":
  32. return Private;
  33. case "nocache":
  34. return NoCache;
  35. case "private_no_expire":
  36. return PrivateNoExpire;
  37. }
  38. return null;
  39. }
  40. public static function setCacheLimiter(l : CacheLimiter) {
  41. if(started) throw "You can't set the cache limiter while the session is already in use";
  42. switch(l) {
  43. case Public:
  44. untyped __call__("session_cache_limiter", "public");
  45. case Private:
  46. untyped __call__("session_cache_limiter", "private");
  47. case NoCache:
  48. untyped __call__("session_cache_limiter", "nocache");
  49. case PrivateNoExpire:
  50. untyped __call__("session_cache_limiter", "private_no_expire");
  51. }
  52. }
  53. public static function getCacheExpire() : Int {
  54. return untyped __call__("session_cache_expire");
  55. }
  56. public static function setCacheExpire(minutes : Int) {
  57. if(started) throw "You can't set the cache expire time while the session is already in use";
  58. untyped __call__("session_cache_expire", minutes);
  59. }
  60. public static function setName(name : String) {
  61. if(started) throw "You can't set the name while the session is already in use";
  62. untyped __call__("session_name", name);
  63. }
  64. public static function getName() : String {
  65. return untyped __call__("session_name");
  66. }
  67. public static function getId() : String {
  68. return untyped __call__("session_id");
  69. }
  70. public static function setId(id : String) {
  71. if(started) throw "You can't set the session id while the session is already in use";
  72. untyped __call__("session_id", id);
  73. }
  74. public static function getSavePath() : String {
  75. return untyped __call__("session_save_path");
  76. }
  77. public static function setSavePath(path : String) {
  78. if(started) throw "You can't set the save path while the session is already in use";
  79. untyped __call__("session_save_path", path);
  80. }
  81. public static function getModule() : String {
  82. return untyped __call__("session_module_name");
  83. }
  84. public static function setModule(module : String) {
  85. if(started) throw "You can't set the module while the session is already in use";
  86. untyped __call__("session_module_name", module);
  87. }
  88. public static function regenerateId(?deleteold : Bool) : Bool {
  89. return untyped __call__("session_regenerate_id", deleteold);
  90. }
  91. public static function get(name : String) : Dynamic {
  92. start();
  93. if(!untyped __call__('isset', __var__("_SESSION", name))) return null;
  94. return untyped __var__("_SESSION", name);
  95. }
  96. public static function set(name : String, value : Dynamic) {
  97. start();
  98. return untyped __set__("_SESSION", name, value);
  99. }
  100. public static function setCookieParams(?lifetime : Int, ?path : String, ?domain : String, ?secure : Bool, ?httponly : Bool) {
  101. if(started) throw "You can't set the cookie params while the session is already in use";
  102. untyped __call__("session_set_cookie_params", lifetime, path, domain, secure, httponly);
  103. }
  104. public static function getCookieParams() : { lifetime : Int, path : String, domain : String, secure : Bool, httponly : Bool} {
  105. return untyped __call__("_hx_anonymous", untyped __call__("session_get_cookie_params"));
  106. }
  107. // TODO: completely untested
  108. public static function setSaveHandler(open : String -> String -> Bool, close : Void -> Bool, read : String -> String, write : String -> String -> Bool, destroy, gc) : Bool {
  109. return untyped __call__("session_set_save_handler", open, close, read, write, destroy, gc);
  110. }
  111. public static function exists(name : String) {
  112. start();
  113. return untyped __call__("array_key_exists", name, __var__("_SESSION"));
  114. }
  115. public static function remove(name : String) {
  116. start();
  117. untyped __call__("unset", __var__("_SESSION", name));
  118. }
  119. public static var started(default, null) : Bool;
  120. public static function start() {
  121. if(started) return;
  122. started = true;
  123. untyped __call__("session_start");
  124. }
  125. public static function clear() {
  126. untyped __call__("session_unset");
  127. }
  128. public static function close() {
  129. untyped __call__("session_write_close");
  130. started = false; // TODO: not sure this useful; test if a closed session can be re-opened (I doubt)
  131. }
  132. static function __init__()
  133. {
  134. started = untyped __call__("isset", __var__("_SESSION"));
  135. }
  136. }
  137. enum CacheLimiter {
  138. Public;
  139. Private;
  140. NoCache;
  141. PrivateNoExpire;
  142. }