PageRenderTime 47ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Store/File.php

https://gitlab.com/shinvdu/php-webim
PHP | 180 lines | 152 code | 21 blank | 7 comment | 22 complexity | b8379340d3f9debd68b10cfa4cb6c3f3 MD5 | raw file
  1. <?php
  2. namespace WebIM\Store;
  3. class File
  4. {
  5. protected $online_dir;
  6. protected $save_dir;
  7. protected $history_fp;
  8. protected $history = array();
  9. protected $history_max_size = 100;
  10. protected $history_write_count = 0;
  11. protected $last_day;
  12. static function clearDir($dir)
  13. {
  14. $n = 0;
  15. if ($dh = opendir($dir))
  16. {
  17. while (($file = readdir($dh)) !== false)
  18. {
  19. if ($file == '.' or $file == '..')
  20. {
  21. continue;
  22. }
  23. if (is_file($dir . $file)) {
  24. unlink($dir . $file);
  25. //echo "delete ".$dir . $file.PHP_EOL;
  26. $n++;
  27. }
  28. if (is_dir($dir . $file)) {
  29. self::clearDir($dir . $file . '/');
  30. $n++;
  31. //echo "rmdir ".$dir . $file . PHP_EOL;
  32. //rmdir($dir . $file . '/');
  33. }
  34. }
  35. }
  36. closedir($dh);
  37. return $n;
  38. }
  39. function checkDir($dir, $clear_file = false)
  40. {
  41. if (!is_dir($dir))
  42. {
  43. if (!mkdir($dir, 0777, true))
  44. {
  45. rw_deny:
  46. trigger_error("can not read/write dir[".$dir."]", E_ERROR);
  47. return;
  48. }
  49. }
  50. else if ($clear_file)
  51. {
  52. self::clearDir($dir);
  53. }
  54. }
  55. function __construct($save_dir, $online_dir = '/dev/shm/swoole_webim/')
  56. {
  57. $this->online_dir = $online_dir;
  58. $this->checkDir($this->online_dir, true);
  59. $this->last_day = date('d');
  60. $this->save_dir = $save_dir;
  61. $this->checkDir($save_dir);
  62. $this->loadHistory();
  63. $this->history_fp = fopen($save_dir.'/'.date('Ymd').'.log', 'a+');
  64. if (!$this->history_fp)
  65. {
  66. trigger_error("can not write file[".$save_dir."]", E_ERROR);
  67. return;
  68. }
  69. }
  70. /**
  71. * 加载历史聊天记录
  72. */
  73. protected function loadHistory()
  74. {
  75. $file = $this->save_dir.'/'.date('Ymd').'.log';
  76. if (!is_file($file)) return;
  77. $handle = fopen($file, "r");
  78. if ($handle)
  79. {
  80. while (($line = fgets($handle, 4096)) !== false)
  81. {
  82. $log = json_decode($line);
  83. if (!$log) continue;
  84. $this->history[] = $log;
  85. if (count($this->history) > $this->history_max_size)
  86. {
  87. array_shift($this->history);
  88. }
  89. }
  90. fclose($handle);
  91. }
  92. }
  93. function login($client_id, $info)
  94. {
  95. file_put_contents($this->online_dir.$client_id, serialize($info));
  96. }
  97. function logout($client_id)
  98. {
  99. unlink($this->online_dir.$client_id);
  100. }
  101. function getOnlineUsers()
  102. {
  103. $online_users = array_slice(scandir($this->online_dir), 2);
  104. return $online_users;
  105. }
  106. function getUsers($users)
  107. {
  108. $ret = array();
  109. foreach($users as $v)
  110. {
  111. $ret[] = $this->getUser($v);
  112. }
  113. return $ret;
  114. }
  115. function getUser($userid)
  116. {
  117. if (!is_file($this->online_dir.$userid))
  118. {
  119. return false;
  120. }
  121. $ret = file_get_contents($this->online_dir.$userid);
  122. $info = unserialize($ret);
  123. return $info;
  124. }
  125. function exists($userid)
  126. {
  127. return is_file($this->online_dir.$userid);
  128. }
  129. function addHistory($userid, $msg)
  130. {
  131. $info = $this->getUser($userid);
  132. $log['user'] = $info;
  133. $log['msg'] = $msg;
  134. $log['time'] = time();
  135. $log['type'] = empty($msg['type']) ? '' : $msg['type'];
  136. $this->history[] = $log;
  137. if (count($this->history) > $this->history_max_size)
  138. {
  139. //丢弃历史消息
  140. array_shift($this->history);
  141. }
  142. fwrite($this->history_fp, json_encode($log).PHP_EOL);
  143. $this->history_write_count ++;
  144. if ($this->history_write_count % 1000)
  145. {
  146. $day = date('d');
  147. if ($day != $this->last_day)
  148. {
  149. fclose($this->history_fp);
  150. $this->history_write_count = 0;
  151. $this->history_fp = fopen($this->save_dir.'/'.date('Ymd').'.log', 'a+');
  152. }
  153. }
  154. }
  155. function getHistory($offset = 0, $num = 100)
  156. {
  157. return $this->history;
  158. }
  159. }