No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

144 líneas
3.0KB

  1. <?php
  2. /**
  3. * This file is part of FPDI
  4. *
  5. * @package FPDI
  6. * @copyright Copyright (c) 2015 Setasign - Jan Slabon (http://www.setasign.com)
  7. * @license http://opensource.org/licenses/mit-license The MIT License
  8. * @version 1.6.1
  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. return ftell($this->file);
  62. } else {
  63. return 0;
  64. }
  65. }
  66. /**
  67. * Reset the position in the file stream.
  68. *
  69. * Optionally move the file pointer to a new location and reset the buffered data.
  70. *
  71. * @param null $pos
  72. * @param int $l
  73. */
  74. public function reset($pos = null, $l = 100)
  75. {
  76. if ($this->_mode == 0) {
  77. if (!is_null($pos)) {
  78. fseek ($this->file, $pos);
  79. }
  80. $this->buffer = $l > 0 ? fread($this->file, $l) : '';
  81. $this->length = strlen($this->buffer);
  82. if ($this->length < $l)
  83. $this->increaseLength($l - $this->length);
  84. } else {
  85. $this->buffer = $this->file;
  86. $this->length = strlen($this->buffer);
  87. }
  88. $this->offset = 0;
  89. $this->stack = array();
  90. }
  91. /**
  92. * Make sure that there is at least one character beyond the current offset in the buffer.
  93. *
  94. * To prevent the tokenizer from attempting to access data that does not exist.
  95. *
  96. * @return bool
  97. */
  98. public function ensureContent()
  99. {
  100. if ($this->offset >= $this->length - 1) {
  101. return $this->increaseLength();
  102. } else {
  103. return true;
  104. }
  105. }
  106. /**
  107. * Forcefully read more data into the buffer
  108. *
  109. * @param int $l
  110. * @return bool
  111. */
  112. public function increaseLength($l = 100)
  113. {
  114. if ($this->_mode == 0 && feof($this->file)) {
  115. return false;
  116. } else if ($this->_mode == 0) {
  117. $totalLength = $this->length + $l;
  118. do {
  119. $toRead = $totalLength - $this->length;
  120. if ($toRead < 1)
  121. break;
  122. $this->buffer .= fread($this->file, $toRead);
  123. } while ((($this->length = strlen($this->buffer)) != $totalLength) && !feof($this->file));
  124. return true;
  125. } else {
  126. return false;
  127. }
  128. }
  129. }