PageRenderTime 948ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/session/cfSession.class.php

https://bitbucket.org/cyberfox/cyberfox-php-framework
PHP | 494 lines | 211 code | 39 blank | 244 comment | 32 complexity | 0b53302af9cee08415b261c1a585ebbc MD5 | raw file
Possible License(s): LGPL-3.0
  1. <?php
  2. /**
  3. -----------------------------------------------------------------------------
  4. * SESSION MANAGEMENT (singleton)
  5. *
  6. * This class is managing the standard PHP session. (start, read & write)
  7. * For each application you can define a seperate session part, so you can minimize the risk,
  8. * that outher application working with this framwork catch your data.
  9. *
  10. * Also you are able to define the session name (e.g.: PHPSESSID) in your application config.
  11. * All session variables can be accessed as if they were class members.*
  12. *
  13. -----------------------------------------------------------------------------
  14. -----------------------------------------------------------------------------
  15. * @copyright (C) 2011 Cyberfox Software Solutions e.U.
  16. * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License version 3 (LGPLv3)
  17. * @author Christian Graf <christian.graf@cyberfox.at>
  18. -----------------------------------------------------------------------------
  19. -----------------------------------------------------------------------------
  20. * @package redfox
  21. * @subpackage dataset
  22. * @category data
  23. -----------------------------------------------------------------------------
  24. -----------------------------------------------------------------------------
  25. * @version $Id: cfSession.class.php 86 2012-05-08 08:13:17Z cgraf $
  26. * @date $Date: 2012-05-08 10:13:17 +0200 (Di, 08 Mai 2012) $
  27. * @svnauthor $Author: cgraf $
  28. -----------------------------------------------------------------------------
  29. */
  30. class cfSession
  31. {
  32. /**
  33. * The current instance of this class.
  34. *
  35. * @var cfSession
  36. * @access private
  37. * @static
  38. */
  39. private static $instance = null;
  40. /**
  41. * The defined session name.
  42. * Default to "PHPSESSID"
  43. *
  44. * @var string
  45. * @access private
  46. * @static
  47. */
  48. private static $sessionname = null;
  49. /**
  50. * The current active session id.
  51. *
  52. * @var string
  53. * @access private
  54. * @static
  55. */
  56. private static $session_id = null;
  57. /**
  58. * The current active application name to differ the session data.
  59. *
  60. * @var string
  61. * @access private
  62. * @static
  63. */
  64. private static $applicationname = null;
  65. /**
  66. * Constructor
  67. *
  68. * Check the config for session settings and set the properties.
  69. * If no settings available, default settings will be set.
  70. *
  71. * @return void
  72. * @access private
  73. * @final
  74. */
  75. final private function __construct()
  76. {
  77. if(is_null(self::$sessionname))
  78. {
  79. $name = cfConfig::GetEntry('name', 'session');
  80. if(empty($name)) $name = 'PHPSESSID'; //default
  81. self::$sessionname = $name;
  82. }
  83. if(is_null(self::$session_id))
  84. {
  85. self::$session_id = session_id();
  86. }
  87. }
  88. /**
  89. * getName()
  90. *
  91. * Get the current defined session name.
  92. *
  93. * @return string The current defined session name
  94. * @access public
  95. * @static
  96. * @final
  97. */
  98. final public static function GetName()
  99. {
  100. if(is_null(self::$sessionname))
  101. {
  102. $name = cfConfig::GetEntry('name', 'session');
  103. if(empty($name)) $name = 'PHPSESSID'; //default
  104. self::$sessionname = $name;
  105. }
  106. return self::$sessionname;
  107. }
  108. /**
  109. * getId()
  110. *
  111. * Get the current active session id.
  112. *
  113. * @return string The current active session id
  114. * @access public
  115. * @static
  116. * @final
  117. */
  118. final public static function GetId()
  119. {
  120. if(is_null(self::$session_id))
  121. {
  122. self::$session_id = session_id();
  123. }
  124. return self::$session_id;
  125. }
  126. /**
  127. * getInstance()
  128. *
  129. * Returns a reference to the singleton instance of this class.
  130. *
  131. * @return cfSession Reference to the singelton instance of this class
  132. * @access public
  133. * @static
  134. * @final
  135. */
  136. final public static function &GetInstance()
  137. {
  138. if(!isset(self::$instance) || is_null(self::$instance) || !is_object(self::$instance))
  139. {
  140. self::$instance = new cfSession();
  141. }
  142. return self::$instance;
  143. }
  144. /**
  145. * getIdentifier()
  146. *
  147. * Get the current active identifier to differ the session data.
  148. *
  149. * If no application_key defined in the config, CFSESSION will be returned as identifier.
  150. *
  151. * @return string The current active identifier
  152. * @access public
  153. * @static
  154. * @final
  155. */
  156. final public static function GetIdentifier()
  157. {
  158. if(is_null(self::$applicationname))
  159. {
  160. $key = cfConfig::GetEntry('application_key', 'application');
  161. if(empty($key)) $key = 'CFSESSION'; //default
  162. self::$applicationname = $key;
  163. }
  164. return self::$applicationname;
  165. }
  166. /**
  167. * start()
  168. *
  169. * Start the PHP Session.
  170. *
  171. * Must be called before you wish to work with.
  172. *
  173. * @param bool $CheckSettings If TRUE, the settings will refreshed automaticly
  174. * @return void
  175. * @access public
  176. * @static
  177. * @final
  178. */
  179. final public static function Start($CheckSettings=true)
  180. {
  181. $sessionName = self::GetName();
  182. if(!empty($sessionName))
  183. {
  184. session_name($sessionName);
  185. }
  186. if(session_id() == '')
  187. {
  188. if($CheckSettings === true)
  189. {
  190. $settings = array( //Default values defined in php.ini - state of settings = PHP Version 5.3.1
  191. 'session.auto_start' => 'auto_start',
  192. 'session.bug_compat_42' => 'bug_compat_42',
  193. 'session.bug_compat_warn' => 'bug_compat_warn',
  194. 'session.cache_expire' => 'cache_expire',
  195. 'session.cache_limiter' => 'cache_limiter',
  196. 'session.cookie_domain' => 'cookie_domain',
  197. 'session.cookie_httponly' => 'cookie_httponly',
  198. 'session.cookie_lifetime' => 'cookie_lifetime',
  199. 'session.cookie_path' => 'cookie_path',
  200. 'session.cookie_secure' => 'cookie_secure',
  201. 'session.entropy_file' => 'entropy_file',
  202. 'session.entropy_length' => 'entropy_length',
  203. 'session.gc_divisor' => 'gc_divisor',
  204. 'session.gc_maxlifetime' => 'gc_maxlifetime',
  205. 'session.gc_probability' => 'gc_probability',
  206. 'session.hash_bits_per_character' => 'hash_bits_per_character',
  207. 'session.hash_function' => 'hash_function',
  208. 'session.name' => 'name',
  209. 'session.referer_check' => 'referer_check',
  210. 'session.save_handler' => 'save_handler',
  211. 'session.save_path' => 'save_path',
  212. 'session.serialize_handler' => 'serialize_handler',
  213. 'session.use_cookies' => 'use_cookies',
  214. 'session.use_only_cookies' => 'use_only_cookies',
  215. 'session.use_trans_sid' => 'use_trans_sid'
  216. );
  217. foreach($settings as $iniSetting => $configName)
  218. {
  219. $tmp = cfConfig::GetEntry($configName, 'session');
  220. if(!is_null($tmp))
  221. {
  222. ini_set($iniSetting, $tmp);
  223. }
  224. }
  225. }
  226. session_start();
  227. }
  228. $ident = self::GetIdentifier();
  229. if(!isset($_SESSION[$ident]))
  230. $_SESSION[$ident] = array();
  231. if(!isset($_SESSION[$ident]['_timeout']))
  232. $_SESSION[$ident]['_timeout'] = intval(time() + self::GetLifetime());
  233. }
  234. /**
  235. * Destroy the current session and unset all session stored variables.
  236. *
  237. * @return void
  238. * @access:public
  239. * @final
  240. * @static
  241. */
  242. final public static function End()
  243. {
  244. session_unset();
  245. session_destroy();
  246. }
  247. /**
  248. * getEntry()
  249. *
  250. * Get an variable stored in the session.
  251. *
  252. * @param string $Name The name of the designated session variable
  253. * @return mixed The value of your designated session varibale
  254. * @access public
  255. * @static
  256. * @final
  257. */
  258. final public static function GetEntry($Name)
  259. {
  260. $ident = self::GetIdentifier();
  261. if(isset($_SESSION[$ident][$Name]))
  262. return $_SESSION[$ident][$Name];
  263. else
  264. return null;
  265. }
  266. /**
  267. * __get() - PHP Magic Function
  268. *
  269. * Allows to get session variables directly via the $obj->varname syntax.
  270. *
  271. * @param string $Name The name of the designated session variable
  272. * @return mixed The value of your designated session varibale
  273. * @access public
  274. * @final
  275. */
  276. final public function __get($Name)
  277. {
  278. return self::GetEntry($Name);
  279. }
  280. /**
  281. * __isset() - PHP Magic Function
  282. *
  283. * Allows to use isset($obj->varname)
  284. *
  285. * @param string $Name The name of the designated session variable
  286. * @return bool TRUE if the designated variable has been set.
  287. * @access public
  288. * @final
  289. */
  290. final public function __isset($Name)
  291. {
  292. $ident = self::GetIdentifier();
  293. return isset($_SESSION[$ident][$Name]);
  294. }
  295. /**
  296. * setEntry()
  297. *
  298. * Set/Store an variable into the the session.
  299. *
  300. * @param string $Name The name of the designated session variable you wish to set
  301. * @param mixed $Value The value the designated session variable
  302. * @return void
  303. * @access public
  304. * @static
  305. * @final
  306. */
  307. final public static function SetEntry($Name, $Value)
  308. {
  309. $ident = self::GetIdentifier();
  310. if(!isset($_SESSION[$ident]))
  311. $_SESSION[$ident] = array();
  312. $_SESSION[$ident][$Name] = $Value;
  313. if($Name != '_timeout')
  314. $_SESSION[$ident]['_timeout'] = intval(time() + self::GetLifetime());
  315. }
  316. /**
  317. * __set() - PHP Magic Function
  318. *
  319. * Allows to set session variables directly via the $obj->varname syntax.
  320. *
  321. * @param string $Name The name of the designated session variable you wish to set
  322. * @param mixed $Value The value the designated session variable
  323. * @return void
  324. * @access public
  325. * @final
  326. */
  327. final public function __set($Name, $Value)
  328. {
  329. self::SetEntry($Name, $Value);
  330. }
  331. /**
  332. * unsetEntry()
  333. *
  334. * Unset/Selete a variable from the the session.
  335. *
  336. * @param string $Name The name of the designated session variable you wish to set
  337. * @return void
  338. * @access public
  339. * @static
  340. * @final
  341. */
  342. final public static function UnsetEntry($Name)
  343. {
  344. $ident = self::GetIdentifier();
  345. if(isset($_SESSION[$ident][$Name]))
  346. {
  347. $_SESSION[$ident][$Name] = null;
  348. unset($_SESSION[$ident][$Name]);
  349. if($Name != '_timeout')
  350. $_SESSION[$ident]['_timeout'] = intval(time() + self::GetLifetime());
  351. }
  352. }
  353. /**
  354. * __unset() - PHP Magic Function
  355. *
  356. * Allows to directly unset via the unset($obj->fieldname) syntax.
  357. *
  358. * @param string $Name The name of the designated session variable you wish to unset
  359. * @return void
  360. * @access public
  361. * @final
  362. */
  363. final public function __unset($Name)
  364. {
  365. self::UnsetEntry($Name);
  366. }
  367. /**
  368. * dump()
  369. *
  370. * Dumps the complete session array direclty on screen.
  371. *
  372. * @return void
  373. * @access public
  374. */
  375. public function dump($IdentOnly = false)
  376. {
  377. $data = $_SESSION;
  378. if($IdentOnly === true)
  379. {
  380. $ident = self::GetIdentifier();
  381. if(isset($_SESSION[$ident]))
  382. {
  383. $data = $_SESSION[$ident];
  384. }
  385. }
  386. if(php_sapi_name() == 'cli')
  387. { //for command line output
  388. echo "\n" .print_r($data, true) ."\n";
  389. }
  390. else
  391. { //for html output
  392. echo '<pre>' .print_r($data, true) .'</pre>';
  393. }
  394. }
  395. /**
  396. * Get the designated session lifetime in seconds.
  397. *
  398. * The lifetime can be defined in the session.php config file. Defaults to 1440 seconds (=24 minutes)
  399. *
  400. * @return int The current session lifetime in seconds
  401. * @access public
  402. * @static
  403. */
  404. public static function GetLifetime()
  405. {
  406. $maxLifetime = cfConfig::GetEntry('gc_maxlifetime', 'session');
  407. if(empty($maxLifetime))
  408. $maxLifetime = 1440; //default php session lifetime of 24 minutes
  409. return intval($maxLifetime);
  410. }
  411. /**
  412. * Check if the current session is valid.
  413. * If the lifetime has not been expired, the lifetime will be renew
  414. *
  415. * @return bool TRUE if the session lifetime has not been expired
  416. * @access public
  417. * @static
  418. */
  419. public static function CheckLifetime()
  420. {
  421. $session = self::GetInstance();
  422. if(isset($session->_timeout) && intval($session->_timeout) > time())
  423. {
  424. $ident = self::GetIdentifier();
  425. $_SESSION[$ident]['_timeout'] = intval(time() + self::GetLifetime());
  426. return true;
  427. }
  428. else
  429. return false;
  430. }
  431. /**
  432. * Destroy the current session and free all session variables.
  433. *
  434. * @return void
  435. * @access public
  436. * @static
  437. */
  438. public static function Destroy()
  439. {
  440. session_unset();
  441. session_destroy();
  442. }
  443. /**
  444. * Clenup the current session. => Free all session variables.
  445. *
  446. * @return void
  447. * @access public
  448. * @static
  449. */
  450. public static function Cleanup()
  451. {
  452. session_unset();
  453. }
  454. }
  455. ?>