Session.hx 4.9 KB

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