pdf_context.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. /**
  3. * This file is part of FPDI
  4. *
  5. * @package FPDI
  6. * @copyright Copyright (c) 2017 Setasign - Jan Slabon (https://www.setasign.com)
  7. * @license http://opensource.org/licenses/mit-license The MIT License
  8. * @version 1.6.2
  9. */
  10. /**
  11. * Class pdf_context
  12. */
  13. class pdf_context
  14. {
  15. /**
  16. * Mode
  17. *
  18. * @var integer 0 = file | 1 = string
  19. */
  20. protected $_mode = 0;
  21. /**
  22. * @var resource|string
  23. */
  24. public $file;
  25. /**
  26. * @var string
  27. */
  28. public $buffer;
  29. /**
  30. * @var integer
  31. */
  32. public $offset;
  33. /**
  34. * @var integer
  35. */
  36. public $length;
  37. /**
  38. * @var array
  39. */
  40. public $stack;
  41. /**
  42. * The constructor
  43. *
  44. * @param resource $f
  45. */
  46. public function __construct(&$f)
  47. {
  48. $this->file =& $f;
  49. if (is_string($this->file))
  50. $this->_mode = 1;
  51. $this->reset();
  52. }
  53. /**
  54. * Get the position in the file stream
  55. *
  56. * @return int
  57. */
  58. public function getPos()
  59. {
  60. if ($this->_mode == 0) {
  61. if (feof($this->file)) {
  62. $stat = fstat($this->file);
  63. fseek($this->file, $stat['size']);
  64. }
  65. $pos = ftell($this->file);
  66. return $pos;
  67. } else {
  68. return 0;
  69. }
  70. }
  71. /**
  72. * Reset the position in the file stream.
  73. *
  74. * Optionally move the file pointer to a new location and reset the buffered data.
  75. *
  76. * @param null $pos
  77. * @param int $l
  78. */
  79. public function reset($pos = null, $l = 100)
  80. {
  81. if ($this->_mode == 0) {
  82. if (!is_null($pos)) {
  83. fseek($this->file, $pos);
  84. }
  85. $this->buffer = $l > 0 ? fread($this->file, $l) : '';
  86. $this->length = strlen($this->buffer);
  87. if ($this->length < $l)
  88. $this->increaseLength($l - $this->length);
  89. } else {
  90. $this->buffer = $this->file;
  91. $this->length = strlen($this->buffer);
  92. }
  93. $this->offset = 0;
  94. $this->stack = array();
  95. }
  96. /**
  97. * Make sure that there is at least one character beyond the current offset in the buffer.
  98. *
  99. * To prevent the tokenizer from attempting to access data that does not exist.
  100. *
  101. * @return bool
  102. */
  103. public function ensureContent()
  104. {
  105. if ($this->offset >= $this->length - 1) {
  106. return $this->increaseLength();
  107. } else {
  108. return true;
  109. }
  110. }
  111. /**
  112. * Forcefully read more data into the buffer
  113. *
  114. * @param int $l
  115. * @return bool
  116. */
  117. public function increaseLength($l = 100)
  118. {
  119. if ($this->_mode == 0 && feof($this->file)) {
  120. return false;
  121. } else if ($this->_mode == 0) {
  122. $totalLength = $this->length + $l;
  123. do {
  124. $toRead = $totalLength - $this->length;
  125. if ($toRead < 1)
  126. break;
  127. $this->buffer .= fread($this->file, $toRead);
  128. } while ((($this->length = strlen($this->buffer)) != $totalLength) && !feof($this->file));
  129. return true;
  130. } else {
  131. return false;
  132. }
  133. }
  134. }