Skip to content

Commit

Permalink
Merge pull request #67 from aik099/circular-object-references-v2
Browse files Browse the repository at this point in the history
Indirect recursion detection in xObjects
  • Loading branch information
smalot committed May 26, 2015
2 parents da38fd4 + fd9902d commit 613d00c
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/Smalot/PdfParser/Object.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ class Object

const COMMAND = 'c';

/**
* The recursion stack.
*
* @var array
*/
static $recursionStack = array();

/**
* @var Document
*/
Expand Down Expand Up @@ -244,7 +251,8 @@ public function getText(Page $page = null)
$current_font = new Font($this->document);
$current_position_td = array('x' => false, 'y' => false);
$current_position_tm = array('x' => false, 'y' => false);
$object_hash = spl_object_hash($this);

array_push(self::$recursionStack, $this->getUniqueId());

foreach ($sections as $section) {

Expand Down Expand Up @@ -356,7 +364,7 @@ public function getText(Page $page = null)
$id = trim(array_pop($args), '/ ');
$xobject = $page->getXObject($id);

if ( is_object($xobject) && $object_hash !== spl_object_hash($xobject) ) {
if ( is_object($xobject) && !in_array($xobject->getUniqueId(), self::$recursionStack) ) {
// Not a circular reference.
$text .= $xobject->getText($page);
}
Expand Down Expand Up @@ -402,6 +410,8 @@ public function getText(Page $page = null)
}
}

array_pop(self::$recursionStack);

return $text . ' ';
}

Expand Down Expand Up @@ -610,4 +620,14 @@ public static function factory(Document $document, Header $header, $content)
return new Object($document, $header, $content);
}
}

/**
* Returns unique id identifying the object.
*
* @return string
*/
protected function getUniqueId()
{
return spl_object_hash($this);
}
}

0 comments on commit 613d00c

Please sign in to comment.