Session.hx 4.9 KB

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