Beispiel #1
0
 public static function set()
 {
     if (self::$action) {
         return;
     }
     if (!isset($_SESSION['client']['__history_id__'])) {
         $_SESSION['client']['__history_id__'] = 0;
     }
     $data = serialize($_SESSION['client']['__module_vars__']);
     if (GZIP_HISTORY && function_exists('gzcompress')) {
         $data = gzcompress($data);
     }
     if (DB::is_postgresql()) {
         $data = '\'' . DB::BlobEncode($data) . '\'';
     } else {
         $data = DB::qstr($data);
     }
     DB::StartTrans();
     DB::Replace('history', array('data' => $data, 'page_id' => $_SESSION['client']['__history_id__'], 'session_name' => DB::qstr(self::session_id()), 'client_id' => CID), array('session_name', 'page_id'));
     $_SESSION['client']['__history_id__']++;
     $ret = DB::Execute('SELECT page_id FROM history WHERE session_name=%s AND (page_id>=%d OR page_id<%d) AND client_id=%d', array(self::session_id(), $_SESSION['client']['__history_id__'], $_SESSION['client']['__history_id__'] - 20, CID));
     while ($row = $ret->FetchRow()) {
         DB::Execute('DELETE FROM history WHERE session_name=%s AND page_id=%d AND client_id=%d', array(self::session_id(), $row['page_id'], CID));
     }
     DB::CompleteTrans();
 }
Beispiel #2
0
 public static function write($name, $data)
 {
     if (READ_ONLY_SESSION || defined('SESSION_EXPIRED')) {
         return true;
     }
     $name = self::truncated_session_id($name);
     $ret = 1;
     if (CID !== false && isset($_SESSION['client'])) {
         $data = serialize($_SESSION['client']);
         switch (self::$session_type) {
             case 'file':
                 ftruncate(self::$session_client_fp, 0);
                 // truncate file
                 rewind(self::$session_client_fp);
                 fwrite(self::$session_client_fp, $data);
                 fflush(self::$session_client_fp);
                 // flush output before releasing the lock
                 flock(self::$session_client_fp, LOCK_UN);
                 // release the lock
                 fclose(self::$session_client_fp);
                 break;
             case 'memcache':
                 if (self::$memcached->is_lock(MEMCACHE_SESSION_TOKEN . $name . '_' . CID, self::$memcached_lock_time)) {
                     $data = str_split($data, 1000000);
                     //something little less then 1MB
                     $data[] = '';
                     foreach ($data as $i => $d) {
                         self::$memcached->set(MEMCACHE_SESSION_TOKEN . $name . '_' . CID . '/' . $i, $d, self::$lifetime);
                     }
                     self::$memcached->unlock(MEMCACHE_SESSION_TOKEN . $name . '_' . CID);
                 }
                 break;
             case 'sql':
                 if (DB::is_mysql()) {
                     $data = DB::qstr($data);
                 } else {
                     $data = '\'' . DB::BlobEncode($data) . '\'';
                 }
                 $ret &= DB::Replace('session_client', array('data' => $data, 'session_name' => DB::qstr($name), 'client_id' => CID), array('session_name', 'client_id'));
                 break;
         }
     }
     if (isset($_SESSION['client'])) {
         unset($_SESSION['client']);
     }
     $data = serialize($_SESSION);
     switch (self::$session_type) {
         case 'file':
             ftruncate(self::$session_fp, 0);
             // truncate file
             rewind(self::$session_fp);
             fwrite(self::$session_fp, $data);
             fflush(self::$session_fp);
             // flush output before releasing the lock
             flock(self::$session_fp, LOCK_UN);
             // release the lock
             fclose(self::$session_fp);
             $ret &= DB::Replace('session', array('expires' => time(), 'name' => DB::qstr($name)), 'name');
             break;
         case 'memcache':
             if (self::$memcached->is_lock(MEMCACHE_SESSION_TOKEN . $name, self::$memcached_lock_time)) {
                 $data = str_split($data, 1000000);
                 //something little less then 1MB
                 $data[] = '';
                 foreach ($data as $i => $d) {
                     self::$memcached->set(MEMCACHE_SESSION_TOKEN . $name . '/' . $i, $d, self::$lifetime);
                 }
                 self::$memcached->unlock(MEMCACHE_SESSION_TOKEN . $name);
                 $ret &= DB::Replace('session', array('expires' => time(), 'name' => DB::qstr($name)), 'name');
             }
             break;
         case 'sql':
             if (DB::is_mysql()) {
                 $data = DB::qstr($data);
             } else {
                 $data = '\'' . DB::BlobEncode($data) . '\'';
             }
             $ret &= DB::Replace('session', array('expires' => time(), 'data' => $data, 'name' => DB::qstr($name)), 'name');
             break;
     }
     return $ret > 0 ? true : false;
 }