Session.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. <?php
  2. namespace ZN\Services;
  3. class __USE_STATIC_ACCESS__Session implements SessionInterface
  4. {
  5. /***********************************************************************************/
  6. /* SESSION COMPONENT */
  7. /***********************************************************************************/
  8. /* Yazar: Ozan UYKUN <ozanbote@windowslive.com> | <ozanbote@gmail.com>
  9. /* Site: www.zntr.net
  10. /* Lisans: The MIT License
  11. /* Telif Hakkı: Copyright (c) 2012-2016, zntr.net
  12. /*
  13. /* Sınıf Adı: Session
  14. /* Versiyon: 1.2
  15. /* Tanımlanma: Dinamik
  16. /* Dahil Edilme: Gerektirmez
  17. /* Erişim: Session:: $this->Session, zn::$use->Session, uselib('Session')
  18. /* Not: Büyük-küçük harf duyarlılığı yoktur.
  19. /***********************************************************************************/
  20. //----------------------------------------------------------------------------------------------------
  21. // Const CONFIG_NAME
  22. //----------------------------------------------------------------------------------------------------
  23. //
  24. // @const string
  25. //
  26. //----------------------------------------------------------------------------------------------------
  27. const CONFIG_NAME = 'Services:session';
  28. //----------------------------------------------------------------------------------------------------
  29. // Session Cookie Common
  30. //----------------------------------------------------------------------------------------------------
  31. //
  32. // ErrorControlTrait
  33. // CallUndefinedMethodTrait
  34. //
  35. // $config
  36. // $name
  37. // $value
  38. // $regenerate
  39. // $encode
  40. //
  41. // name()
  42. // encode()
  43. // decode()
  44. // regenerate()
  45. // value()
  46. // defaultVariable()
  47. //
  48. //----------------------------------------------------------------------------------------------------
  49. use SessionTrait;
  50. //----------------------------------------------------------------------------------------------------
  51. // Construct
  52. //----------------------------------------------------------------------------------------------------
  53. //
  54. // @param void
  55. // @return bool
  56. //
  57. //----------------------------------------------------------------------------------------------------
  58. public function __construct()
  59. {
  60. $this->config();
  61. \Config::iniSet(\Config::get('Htaccess', 'session')['settings']);
  62. $this->start();
  63. }
  64. //----------------------------------------------------------------------------------------------------
  65. // Insert Method Başlangıç
  66. //----------------------------------------------------------------------------------------------------
  67. /******************************************************************************************
  68. * INSERT *
  69. *******************************************************************************************
  70. | Genel Kullanım: Oturum oluşturmak için kullanılır. |
  71. | |
  72. | Parametreler: 2 parametresi vardır. |
  73. | 1. string var @name => Oluşturulacak oturumun adı. |
  74. | 2. mixed var @value => Oluşturulacak oturumun tutacağı değer. |
  75. | |
  76. | Örnek Kullanım: insert('isim', 'Değer'); |
  77. | Not: Application/Config/Session.php dosyası üzerinden ayarlarını yapılandırabilirsiniz. |
  78. | |
  79. ******************************************************************************************/
  80. public function insert($name = '', $value = '')
  81. {
  82. if( ! empty($name) )
  83. {
  84. if( ! isChar($name) )
  85. {
  86. \Errors::set('Error', 'valueParameter', 'name');
  87. return false;
  88. }
  89. $this->name($name);
  90. }
  91. if( ! empty($value) )
  92. {
  93. $this->value($value);
  94. }
  95. if( ! empty($this->encode) )
  96. {
  97. if( isset($this->encode['name']) )
  98. {
  99. if( isHash($this->encode['name']) )
  100. {
  101. $this->name = hash($this->encode['name'], $this->name);
  102. }
  103. }
  104. if( isset($this->encode['value']) )
  105. {
  106. if( isHash($this->encode['value']) )
  107. {
  108. $this->value = hash($this->encode['value'], $this->value);
  109. }
  110. }
  111. }
  112. $sessionConfig = $this->config;
  113. if( ! isset($this->encode['name']))
  114. {
  115. $encode = $sessionConfig["encode"];
  116. if( $encode === true )
  117. {
  118. $this->name = md5($this->name);
  119. }
  120. elseif( is_string($encode) )
  121. {
  122. if( isHash($encode) )
  123. {
  124. $this->name = hash($encode, $this->name);
  125. }
  126. }
  127. }
  128. $_SESSION[$this->name] = $this->value;
  129. if( $_SESSION[$this->name] )
  130. {
  131. if( $this->regenerate === true )
  132. {
  133. session_regenerate_id();
  134. }
  135. $this->defaultVariable();
  136. return true;
  137. }
  138. else
  139. {
  140. return false;
  141. }
  142. }
  143. //----------------------------------------------------------------------------------------------------
  144. // Insert Method Bitiş
  145. //----------------------------------------------------------------------------------------------------
  146. //----------------------------------------------------------------------------------------------------
  147. // Select Methods Başlangıç
  148. //----------------------------------------------------------------------------------------------------
  149. public function select($name = '')
  150. {
  151. if( ! is_scalar($name) || empty($name) )
  152. {
  153. return \Errors::set('Error', 'valueParameter', 'name');
  154. }
  155. if( isset($this->encode['name']) )
  156. {
  157. if( isHash($this->encode['name']) )
  158. {
  159. $name = hash($this->encode['name'], $name);
  160. $this->encode = [];
  161. }
  162. }
  163. else
  164. {
  165. $encode = $this->config['encode'];
  166. if( $encode === true )
  167. {
  168. $name = md5($name);
  169. }
  170. elseif( is_string($encode) )
  171. {
  172. if( isHash($encode) )
  173. {
  174. $name = hash($encode, $name);
  175. }
  176. }
  177. }
  178. if( isset($_SESSION[$name]) )
  179. {
  180. return $_SESSION[$name];
  181. }
  182. else
  183. {
  184. return false;
  185. }
  186. }
  187. /******************************************************************************************
  188. * SELECT ALL *
  189. *******************************************************************************************
  190. | Genel Kullanım: Oluşturulmuş tüm oturumlara erişmek için kullanılır. |
  191. | |
  192. | Parametreler: Herhangi bir parametresi yoktur. |
  193. | |
  194. | Örnek Kullanım: selectAll(); |
  195. | |
  196. ******************************************************************************************/
  197. public function selectAll()
  198. {
  199. return $_SESSION;
  200. }
  201. /******************************************************************************************
  202. * START *
  203. *******************************************************************************************
  204. | Genel Kullanım: Session oturumu başlatmak için kullanılır . |
  205. | |
  206. | Parametreler: Herhangi bir parametresi yoktur. |
  207. | |
  208. | Örnek Kullanım: start(); |
  209. | |
  210. ******************************************************************************************/
  211. public function start()
  212. {
  213. if( ! isset($_SESSION) )
  214. {
  215. session_start();
  216. }
  217. }
  218. //----------------------------------------------------------------------------------------------------
  219. // Select Methods Bitiş
  220. //----------------------------------------------------------------------------------------------------
  221. //----------------------------------------------------------------------------------------------------
  222. // Delete Methods Başlangıç
  223. //----------------------------------------------------------------------------------------------------
  224. public function delete($name = '')
  225. {
  226. if( ! is_scalar($name) || empty($name) )
  227. {
  228. return \Errors::set('Error', 'valueParameter', 'name');
  229. }
  230. $sessionConfig = $this->config;
  231. if( isset($this->encode['name']) )
  232. {
  233. if( isHash($this->encode['name']) )
  234. {
  235. $name = hash($this->encode['name'], $name);
  236. $this->encode = [];
  237. }
  238. }
  239. else
  240. {
  241. $encode = $sessionConfig["encode"];
  242. if( $encode === true )
  243. {
  244. $name = md5($name);
  245. }
  246. elseif( is_string($encode) )
  247. {
  248. if( isHash($encode) )
  249. {
  250. $name = hash($encode, $name);
  251. }
  252. }
  253. }
  254. if( isset($_SESSION[$name]) )
  255. {
  256. unset($_SESSION[$name]);
  257. }
  258. else
  259. {
  260. return false;
  261. }
  262. }
  263. /******************************************************************************************
  264. * DELETE ALL *
  265. *******************************************************************************************
  266. | Genel Kullanım: Oluşturulmuş tüm oturumları silmek için kullanılır. |
  267. | |
  268. | Parametreler: Herhangi bir parametresi yoktur. |
  269. | |
  270. | Örnek Kullanım: deleteAll(); |
  271. | |
  272. ******************************************************************************************/
  273. public function deleteAll()
  274. {
  275. session_destroy();
  276. }
  277. //----------------------------------------------------------------------------------------------------
  278. // Delete Methods Bitiş
  279. //----------------------------------------------------------------------------------------------------
  280. }