pdf_context.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. //
  3. // FPDI - Version 1.5.2
  4. //
  5. // Copyright 2004-2014 Setasign - Jan Slabon
  6. //
  7. // Licensed under the Apache License, Version 2.0 (the "License");
  8. // you may not use this file except in compliance with the License.
  9. // You may obtain a copy of the License at
  10. //
  11. // http://www.apache.org/licenses/LICENSE-2.0
  12. //
  13. // Unless required by applicable law or agreed to in writing, software
  14. // distributed under the License is distributed on an "AS IS" BASIS,
  15. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. // See the License for the specific language governing permissions and
  17. // limitations under the License.
  18. //
  19. /**
  20. * Class pdf_context
  21. */
  22. class pdf_context
  23. {
  24. /**
  25. * Mode
  26. *
  27. * @var integer 0 = file | 1 = string
  28. */
  29. protected $_mode = 0;
  30. /**
  31. * @var resource|string
  32. */
  33. public $file;
  34. /**
  35. * @var string
  36. */
  37. public $buffer;
  38. /**
  39. * @var integer
  40. */
  41. public $offset;
  42. /**
  43. * @var integer
  44. */
  45. public $length;
  46. /**
  47. * @var array
  48. */
  49. public $stack;
  50. /**
  51. * The constructor
  52. *
  53. * @param resource $f
  54. */
  55. public function __construct(&$f)
  56. {
  57. $this->file =& $f;
  58. if (is_string($this->file))
  59. $this->_mode = 1;
  60. $this->reset();
  61. }
  62. /**
  63. * Get the position in the file stream
  64. *
  65. * @return int
  66. */
  67. public function getPos()
  68. {
  69. if ($this->_mode == 0) {
  70. return ftell($this->file);
  71. } else {
  72. return 0;
  73. }
  74. }
  75. /**
  76. * Reset the position in the file stream.
  77. *
  78. * Optionally move the file pointer to a new location and reset the buffered data.
  79. *
  80. * @param null $pos
  81. * @param int $l
  82. */
  83. public function reset($pos = null, $l = 100)
  84. {
  85. if ($this->_mode == 0) {
  86. if (!is_null($pos)) {
  87. fseek ($this->file, $pos);
  88. }
  89. $this->buffer = $l > 0 ? fread($this->file, $l) : '';
  90. $this->length = strlen($this->buffer);
  91. if ($this->length < $l)
  92. $this->increaseLength($l - $this->length);
  93. } else {
  94. $this->buffer = $this->file;
  95. $this->length = strlen($this->buffer);
  96. }
  97. $this->offset = 0;
  98. $this->stack = array();
  99. }
  100. /**
  101. * Make sure that there is at least one character beyond the current offset in the buffer.
  102. *
  103. * To prevent the tokenizer from attempting to access data that does not exist.
  104. *
  105. * @return bool
  106. */
  107. public function ensureContent()
  108. {
  109. if ($this->offset >= $this->length - 1) {
  110. return $this->increaseLength();
  111. } else {
  112. return true;
  113. }
  114. }
  115. /**
  116. * Forcefully read more data into the buffer
  117. *
  118. * @param int $l
  119. * @return bool
  120. */
  121. public function increaseLength($l = 100)
  122. {
  123. if ($this->_mode == 0 && feof($this->file)) {
  124. return false;
  125. } else if ($this->_mode == 0) {
  126. $totalLength = $this->length + $l;
  127. do {
  128. $toRead = $totalLength - $this->length;
  129. if ($toRead < 1)
  130. break;
  131. $this->buffer .= fread($this->file, $toRead);
  132. } while ((($this->length = strlen($this->buffer)) != $totalLength) && !feof($this->file));
  133. return true;
  134. } else {
  135. return false;
  136. }
  137. }
  138. }