MISP (core software) - Open Source Threat Intelligence and Sharing Platform (formely known as Malware Information Sharing Platform) https://www.misp-project.org/
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

150 lines
4.0 KiB

  1. <?php
  2. class TmpFileTool
  3. {
  4. /** @var resource */
  5. private $tmpfile;
  6. /** @var string */
  7. private $separator;
  8. /**
  9. * @param int $maxInMemory How many bytes should keep in memory before creating file on disk. By default is is 2 MB.
  10. * @throws Exception
  11. */
  12. public function __construct($maxInMemory = null)
  13. {
  14. if ($maxInMemory === null) {
  15. $maxInMemory = 2 * 1024 * 1024;
  16. }
  17. $this->tmpfile = fopen("php://temp/maxmemory:$maxInMemory", "w+");
  18. if ($this->tmpfile === false) {
  19. throw new Exception('Could not create temporary file.');
  20. }
  21. }
  22. /**
  23. * Write data to stream with separator. Separator will be prepend to content for next call.
  24. * @param string|Generator $content
  25. * @param string $separator
  26. * @throws Exception
  27. */
  28. public function writeWithSeparator($content, $separator)
  29. {
  30. if (isset($this->separator)) {
  31. if ($content instanceof Generator) {
  32. $this->write($this->separator);
  33. foreach ($content as $part) {
  34. $this->write($part);
  35. }
  36. } else {
  37. $this->write($this->separator . $content);
  38. }
  39. } else {
  40. if ($content instanceof Generator) {
  41. foreach ($content as $part) {
  42. $this->write($part);
  43. }
  44. } else {
  45. $this->write($content);
  46. }
  47. }
  48. $this->separator = $separator;
  49. }
  50. /**
  51. * @param string $content
  52. * @throws Exception
  53. */
  54. public function write($content)
  55. {
  56. if (fwrite($this->tmpfile, $content) === false) {
  57. if ($this->tmpfile === null) {
  58. throw new Exception('Could not write to finished temporary file.');
  59. }
  60. $tmpFolder = sys_get_temp_dir();
  61. $freeSpace = disk_free_space($tmpFolder);
  62. throw new Exception("Could not write to temporary file in $tmpFolder folder. Maybe not enough space? ($freeSpace bytes left)");
  63. }
  64. }
  65. /**
  66. * Get one line from file parsed as CSV.
  67. *
  68. * @param string $delimiter
  69. * @param string $enclosure
  70. * @param string $escape
  71. * @return Generator
  72. * @throws Exception
  73. */
  74. public function csv($delimiter = ',', $enclosure = '"', $escape = "\\")
  75. {
  76. $this->rewind();
  77. $line = 0;
  78. while (!feof($this->tmpfile)) {
  79. $result = fgetcsv($this->tmpfile, 0, $delimiter, $enclosure, $escape);
  80. if ($result === false) {
  81. throw new Exception("Could not read line $line from temporary CSV file.");
  82. }
  83. $line++;
  84. yield $result;
  85. }
  86. fclose($this->tmpfile);
  87. $this->tmpfile = null;
  88. }
  89. /**
  90. * @return Generator
  91. * @throws Exception
  92. */
  93. public function lines()
  94. {
  95. $this->rewind();
  96. while (!feof($this->tmpfile)) {
  97. $result = fgets($this->tmpfile);
  98. if ($result === false) {
  99. throw new Exception('Could not read line from temporary file.');
  100. }
  101. yield $result;
  102. }
  103. fclose($this->tmpfile);
  104. $this->tmpfile = null;
  105. }
  106. /**
  107. * @return string
  108. * @throws Exception
  109. */
  110. public function finish()
  111. {
  112. $this->rewind();
  113. $final = stream_get_contents($this->tmpfile);
  114. if ($final === false) {
  115. throw new Exception('Could not read from temporary file.');
  116. }
  117. fclose($this->tmpfile);
  118. $this->tmpfile = null;
  119. return $final;
  120. }
  121. /**
  122. * @return string
  123. * @throws Exception
  124. */
  125. public function __toString()
  126. {
  127. return $this->finish();
  128. }
  129. /**
  130. * Seek to start of file.
  131. *
  132. * @throws Exception
  133. */
  134. private function rewind()
  135. {
  136. if (fseek($this->tmpfile, 0) === -1) {
  137. throw new Exception('Could not seek to start of temporary file.');
  138. }
  139. }
  140. }