00001 <?php
00040 class zSession extends zCore{
00047 var $bak = array();
00054 var $savePoint = array();
00055
00056 function zSession(){
00057 $this->__construct();
00058 }
00059 function __construct(){
00060 global $_SESSION;
00061 parent::__construct();
00062 if(!defined('DELETE_OLD_SESSION'))define('DELETE_OLD_SESSION', true);
00063 if(!defined('KEEP_OLD_SESSION'))define('KEEP_OLD_SESSION', false);
00064 $this->bak = $_SESSION;
00065 }
00073 function started(){
00074 if(isset($_SESSION)){
00075 zLogger::debug("zSession::start() returning true as session has been started", 'rom.session');
00076 return true;
00077 }else{
00078 zLogger::debug("zSession::start() returning false as session has not been started yet", 'rom.session');
00079 return false;
00080 }
00081 }
00093 function start(){
00094 if(!session_id() && !headers_sent() && !zSession::started()){
00095 session_name(Z_SESSION_NAME);
00096 zLogger::debug("Starting session", 'rom.session');
00097 session_start();
00098 }else{
00099 zLogger::debug("session already started", 'rom.session');
00100 }
00101 }
00113 function getEncrypted($key=null){
00114 zLogger::debug("Getting value of Encrypted Key $key", 'rom.session');
00115 zSession::start();
00116 if(is_array($key)){
00117 perror('zSession::get() doesn\'t accept array');
00118 }
00119 if($key != null){
00120 if(isset($_SESSION[$key])){
00121 return zCrypt::decrypt($_SESSION[$key], true);
00122 }else{
00123 perror('<code>zigmoyd.session.get</code><br />Trying to get non existing session Variable `'.$key.'`');
00124 }
00125 }else{
00126 return zCrypt::decrypt($_SESSION, true);
00127 }
00128 return false;
00129 }
00142 function setEncrypted($key, $val){
00143 zLogger::debug("Setting Encrypted value $val of Key $key", 'rom.session');
00144 zSession::start();
00145 if($_SESSION[$key] = zCrypt::encrypt($val, true)){return true;}
00146 return false;
00147 }
00155 function getPlain($key=null){
00156 zLogger::debug("Getting value of Key $key", 'rom.session');
00157 zSession::start();
00158 if(is_array($key)){
00159 perror('zSession::getPlain() doesn\'t accept array');
00160 }
00161 if($key != null){
00162 if(isset($_SESSION[$key])){
00163 return $_SESSION[$key];
00164 }else{
00165 perror('<code>zigmoyd.session.get</code><br />Trying to get non existing session Variable `'.$key.'`');
00166 }
00167 }else{
00168 return $_SESSION;
00169 }
00170 return false;
00171 }
00180 function setPlain($key, $val){
00181 zLogger::debug("Setting value $val of Key $key", 'rom.session');
00182 zSession::start();
00183 if($_SESSION[$key] = $val){return true;}
00184 return false;
00185 }
00193 function remove($key=null){
00194 zLogger::debug("removing Key $key", 'rom.session');
00195 if($key == null || !is_string($key)){
00196 perror("<code>zSession::remove()</code><br />You must provide which key to delete and that argumnt must be a string to zSession::remove() e.g. it accepts 1 argumnt");return false;
00197 return false;
00198 }
00199 if(!zSession::keyExists($key)){
00200 perror("<code>zSession::remove()</code><br />zSession::remove() failed to remove key $key from session as it doesn't exist");return false;
00201 return false;
00202 }else{
00203 unset($_SESSION[$key]);
00204 return true;
00205 }
00206 }
00214 function keyExists($key=null){
00215 if($key == null || !is_string($key)){
00216 perror("<code>zSession::keyExists()</code><br />You must provide which key to Check and that argumnt must be a string to zSession::keyExists() e.g. it accepts 1 argumnt");
00217 return false;
00218 }
00219 return isset($_SESSION[$key]);
00220 }
00226 function commit(){
00227 zLogger::debug("Commiting session data to the server", 'rom.session');
00228 if(!zSession::started())zSession::start();
00229 session_write_close();
00230 }
00238 function regenerateId($delOldFlag=true){
00239 zLogger::debug("Regenarating session ID", 'rom.session');
00240 if(!zSession::started())zSession::start();
00241 return session_regenerate_id($delOldFlag);
00242 }
00254 function cookieParams($lifeTime=null, $path=null, $domain=null, $secure=null, $httpOnly=null){
00255 if(!zSession::started())zSession::start();
00256 if(is_null($lifeTime) && is_null($path) && is_null($domain) && is_null($secure) && is_null($httpOnly)){
00257 return session_get_cookie_params();
00258 }else{
00259 $currentParams = session_get_cookie_params();
00260 if(is_null($lifeTime))$lifeTime = $currentParams['lifetime'];
00261 if(is_null($path))$path = $currentParams['path'];
00262 if(is_null($domain))$domain = $currentParams['domain'];
00263 if(is_null($secure))$secure = $currentParams['secure'];
00264 if(is_null($httpOnly))$httpOnly = $currentParams['httponly'];
00265 session_set_cookie_params($lifeTime, $path, $domain, $secure, $httpOnly);
00266 }
00267 }
00274 function Id($id=null){
00275 if(!zSession::started())zSession::start();
00276 if(is_null($id)){
00277 $sesId = session_id();
00278 }else{
00279 $sesId = (strlen(session_id($id)) >= 1);
00280 }
00281 zLogger::debug("Returning session Id $sesId", 'rom.session');
00282 return $sesId;
00283 }
00290 function Name($sessionName=null){
00291 if(!is_null($sessionName)){
00292 $sesName = (strlen(session_name($sessionName)) >= 1);
00293 }else{
00294 $sesName = session_name();
00295 }
00296 zLogger::debug("Returning session Name $sesName", 'rom.session');
00297 return $sesName;
00298 }
00306 function rollBack($savePointName=null){
00307 if(is_null($savePointName)){
00308 foreach($this->bak as $key => $val){
00309 zSession::setPlain($key, $val);
00310 }
00311 }else{
00312 foreach($this->savePoint[$savePointName] as $key => $val){
00313 zSession::setPlain($key, $val);
00314 }
00315 }
00316 }
00323 function savePoint($savePointName){
00324 $this->savePoint[$savePointName] = $_SESSION;
00325 }
00334 function __call($methodName, $args, &$return){
00335 switch(true){
00345 case (preg_match('~setEncrypted(\w+)~i', $methodName, $m) >= 1):
00346 $return = zSession::setEncrypted(strtolower($m[1]), $args[0]);
00347 break;
00356 case (preg_match('~getEncrypted(\w+)~i', $methodName, $m) >= 1):
00357 $return = zSession::getEncrypted(strtolower($m[1]), $args[0]);
00358 break;
00368 case (preg_match('~set(\w+)~i', $methodName, $m) >= 1):
00369 $return = zSession::setPlain(strtolower($m[1]), $args[0]);
00370 break;
00379 case (preg_match('~get(\w+)~i', $methodName, $m) >= 1):
00380 $return = zSession::getPlain(strtolower($m[1]));
00381 break;
00382 default:
00383 perror("<code>zigtmoyd.session.$methodName()</code><br />no Such method in zSession Class");
00384 }
00385 return true;
00386 }
00387 }
00388 if(function_exists('overload'))overload('zSession');
00389 if(Z_SESSION_AUTO_START)zSession::start();
00391 ?>