PageRenderTime 26ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/admin/include/tiny_mce/plugins/ajaxfilemanager/inc/class.session.php

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