session.php (1804B)
1 <?php 2 /** 3 * @package OpenCart 4 * @author Daniel Kerr 5 * @copyright Copyright (c) 2005 - 2017, OpenCart, Ltd. (https://www.opencart.com/) 6 * @license https://opensource.org/licenses/GPL-3.0 7 * @link https://www.opencart.com 8 */ 9 10 /** 11 * Session class 12 */ 13 class Session { 14 protected $adaptor; 15 protected $session_id; 16 public $data = array(); 17 18 /** 19 * Constructor 20 * 21 * @param string $adaptor 22 * @param object $registry 23 */ 24 public function __construct($adaptor, $registry = '') { 25 $class = 'Session\\' . $adaptor; 26 27 if (class_exists($class)) { 28 if ($registry) { 29 $this->adaptor = new $class($registry); 30 } else { 31 $this->adaptor = new $class(); 32 } 33 34 register_shutdown_function(array($this, 'close')); 35 } else { 36 trigger_error('Error: Could not load cache adaptor ' . $adaptor . ' session!'); 37 exit(); 38 } 39 } 40 41 /** 42 * 43 * 44 * @return string 45 */ 46 public function getId() { 47 return $this->session_id; 48 } 49 50 /** 51 * 52 * 53 * @param string $session_id 54 * 55 * @return string 56 */ 57 public function start($session_id = '') { 58 if (!$session_id) { 59 if (function_exists('random_bytes')) { 60 $session_id = substr(bin2hex(random_bytes(26)), 0, 26); 61 } else { 62 $session_id = substr(bin2hex(openssl_random_pseudo_bytes(26)), 0, 26); 63 } 64 } 65 66 if (preg_match('/^[a-zA-Z0-9,\-]{22,52}$/', $session_id)) { 67 $this->session_id = $session_id; 68 } else { 69 exit('Error: Invalid session ID!'); 70 } 71 72 $this->data = $this->adaptor->read($session_id); 73 74 return $session_id; 75 } 76 77 /** 78 * 79 */ 80 public function close() { 81 $this->adaptor->write($this->session_id, $this->data); 82 } 83 84 /** 85 * 86 */ 87 public function __destroy() { 88 $this->adaptor->destroy($this->session_id); 89 } 90 }