/php/main/inc/lib/fckeditor/editor/plugins/ajaxfilemanager/inc/class.session.php

https://bitbucket.org/frchico/chamilo_openshift · PHP · 232 lines · 171 code · 20 blank · 41 comment · 41 complexity · 9d3b4d6245e4820a3766997dc17b5255 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. // This is to preserve the empty index.html and during development - the hidden .svn folder. No need for Chamilo because ajaxfilemanager sessions are disabled
  109. //if($file != '.' && $file != '..' && $file != $this->gcCounterFileName && $file != $this->gcLogFileName && $file != session_id() )
  110. if($file != '.' && $file != '..' && $file != $this->gcCounterFileName && $file != $this->gcLogFileName && $file != session_id()
  111. && $file != 'index.html' && $file != '.svn')
  112. {
  113. $path=$this->dir.$file;
  114. $output .= $path ;
  115. //check if this is a expired session file
  116. if(filemtime($path) + $this->lifeTime < time())
  117. {
  118. if($fo->delete($path))
  119. {
  120. $output .= ' Deleted at ' . date('d/M/Y H:i:s');
  121. }else
  122. {
  123. $output .= " Failed at " . date('d/M/Y H:i:s');
  124. }
  125. }
  126. $output .= "\n";
  127. }
  128. }
  129. if($this->debug)
  130. {
  131. $this->_log($output);
  132. }
  133. @closedir($dirHandler);
  134. }
  135. if(CONFIG_SYS_DEMO_ENABLE)
  136. {
  137. //remove expired files from uploaded folder
  138. $dirHandler = @opendir(CONFIG_SYS_ROOT_PATH);
  139. $output = '';
  140. $output .= "gc start at " . date('d/M/Y H:i:s') . "\n";
  141. $fo = new file();
  142. if($dirHandler)
  143. {
  144. while(false !== ($file = readdir($dirHandler)))
  145. {
  146. if($file != '.' && $file != '..')
  147. {
  148. $path=CONFIG_SYS_ROOT_PATH.$file;
  149. $output .= $path ;
  150. //check if this is a expired session file
  151. if(filemtime($path) + $this->lifeTime < time())
  152. {
  153. if($fo->delete($path))
  154. {
  155. $output .= ' Deleted at ' . date('d/M/Y H:i:s');
  156. }else
  157. {
  158. $output .= " Failed at " . date('d/M/Y H:i:s');
  159. }
  160. }
  161. $output .= "\n";
  162. }
  163. }
  164. if($this->debug)
  165. {
  166. $this->_log($output);
  167. }
  168. @closedir($dirHandler);
  169. }
  170. }
  171. }
  172. /**
  173. * log action taken by the gc
  174. *
  175. * @param unknown_type $msg
  176. */
  177. function _log($msg)
  178. {
  179. $msg = "<?php die(); ?>\n" . $msg;
  180. $fp = @fopen($this->gcLogFile, 'w+');
  181. if($fp)
  182. {
  183. @ftruncate($fp, 0);
  184. !@fputs($fp, $msg);
  185. @fclose($fp);
  186. }
  187. }
  188. /**
  189. * get the current session directory
  190. *
  191. * @return string return empty if failed
  192. */
  193. function getSessionDir()
  194. {
  195. if(!file_exists($this->sessionDir) && !is_dir($this->sessionDir))
  196. {
  197. $dir = new file();
  198. if(!$dir->mkdir($this->sessionDir))
  199. {
  200. return '';
  201. }
  202. }else
  203. {
  204. if(!@is_dir($this->sessionDir))
  205. {
  206. return '';
  207. }
  208. }
  209. return $this->sessionDir;
  210. }
  211. }
  212. ?>