Blame view

js/_tiny_mce/plugins/ajaxfilemanager/inc/class.session.php 4.4 KB
42868d70   andryeyev   Создал GIT
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
  <?php
  
  	/**
  	 * this class provide a function like session handling engine
  	 * @author Logan Cai (cailongqun [at] yahoo [dot] com [dot] cn)
  	 * @link www.phpletter.com
  	 * @since 22/May/2007
  	 *
  	 */
  
  require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . "class.file.php");
  class Session 
  {
      var $lifeTime;
      var $fp = null;
      var $dir = null;
      var $mTime = null;
      var $sessionDir = null;
      var $sessionFile = null;
      var $ext = '.txt';
      var $gcCounter = 5; //call gc to delete expired session each ten request
  		var $gcCounterFileName = 'gc_counter.ajax.php';
  		var $gcCounterFile = null;
  		var $gcLogFileName = 'gc_log.ajax.php';
  		var $gcLogFile = null;
  		var $debug = true; //turn it on when you want to see gc log
  		
  		
      /**
       * constructor
       *
       */
      function __construct()
      {
      	//check if the session folder read and writable
      		$dir = new file();
      		if(!$dir->mkdir(CONFIG_SYS_DIR_SESSION_PATH))
      		{
      			die('Unable to create session folder.(' . CONFIG_SYS_DIR_SESSION_PATH . ")");
      		}
      		if(!$dir->isReadable(CONFIG_SYS_DIR_SESSION_PATH))
      		{
      			die('Permission denied: ' . CONFIG_SYS_DIR_SESSION_PATH . " is not readable.");
      		}    		
      		if(!$dir->isWritable(CONFIG_SYS_DIR_SESSION_PATH))
      		{
      			die('Permission denied: ' . CONFIG_SYS_DIR_SESSION_PATH . " is not writable.");
      		}
      		$this->dir = CONFIG_SYS_DIR_SESSION_PATH;
          $this->lifeTime = get_cfg_var("session.gc_maxlifetime");  
          $this->gcCounterFile = $this->dir . $this->gcCounterFileName; 
          $this->gcLogFile = $this->dir  . $this->gcLogFileName;
          $this->sessionDir = backslashToSlash(addTrailingSlash(backslashToSlash($this->dir)) . session_id() . DIRECTORY_SEPARATOR);
          $this->init();    	
      }
       /**
       * constructor
       *
       */   
      function Session() 
      {
      		$this->__construct();        
      }
      /**
       * session init
       * @return boolean
       */
      function init() 
      {
  
          
          
      }
      
      function gc()
      {
       		//init the counter file
          $fp = @fopen($this->gcCounterFile, 'a+');
          if($fp)
          {
          	$count = intval(fgets($fp, 999999)) + 1;
          	if($count > $this->gcCounter || rand(0, 23) == date('h'))
          	{
          		$this->_gc();
          		$count = 0;
          	}
          	@ftruncate($fp, 0);
          	if(!@fputs($fp, $count))
          	{
          		die(SESSION_COUNTER_FILE_WRITE_FAILED);
          	}
          	@fclose($fp);
          }else 
          {
          	die(SESSION_COUNTER_FILE_CREATE_FAILED);
          }   	
      }
  
      /**
       * garbage collection function
       *
       */
      function _gc() 
      {
  			
  	 		$dirHandler = @opendir($this->dir);
  	 		$output = '';
  	 		$output .= "gc start at " . date('d/M/Y H:i:s') . "\n";
  	 		$fo = new file();
  			if($dirHandler)
  			{
  				while(false !== ($file = readdir($dirHandler)))
  				{
  					if($file != '.' && $file != '..' && $file != $this->gcCounterFileName && $file != $this->gcLogFileName && $file != session_id() )
  					{						
  						$path=$this->dir.$file;
  						$output .= $path ;
  						//check if this is a expired session file
  						if(filemtime($path) + $this->lifeTime < time())
  						{							
  							if($fo->delete($path))
  							{
  								$output .= ' Deleted at ' . date('d/M/Y H:i:s');
  							}else 
  							{
  								$output .= " Failed at " . date('d/M/Y H:i:s');
  							}																			
  						}
  						$output .= "\n";
  											
  					}
  				}
  				if($this->debug)
  				{
  					$this->_log($output);
  				}
  				
  				@closedir($dirHandler);
  
  			}     
      }
      /**
       * log action taken by the gc
       *
       * @param unknown_type $msg
       */
      function _log($msg)
      {
      	$msg = "<?php die(); ?>\n" . $msg;
      	$fp = @fopen($this->gcLogFile, 'w+');
      	if($fp)
      	{
      		@ftruncate($fp, 0);
      		!@fputs($fp, $msg);
      		@fclose($fp);
      	}
      }
      
      /**
       * get the current session directory
       *
       * @return string return empty if failed
       */
      function getSessionDir()
      {
      	if(!file_exists($this->sessionDir) && !is_dir($this->sessionDir))
      	{
      		$dir = new file();
      		if(!$dir->mkdir($this->sessionDir))
      		{
      			die('Unable to create Session Directory.(' . $this->sessionDir . ")");
      		}
      	}else 
      	{
  	     	if(!@is_dir($this->sessionDir))
  	    	{
  	    		die('Session Directory does not exist.(' . $this->sessionDir . ")");
  	    	}   		
      	}
      	return $this->sessionDir;
      }
      
  
      
  }
  ?>