PhpEvaluator.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. /**
  3. * @package dompdf
  4. * @link http://dompdf.github.com/
  5. * @author Benj Carson <benjcarson@digitaljunkies.ca>
  6. * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
  7. */
  8. namespace Dompdf;
  9. /**
  10. * Executes inline PHP code during the rendering process
  11. *
  12. * @package dompdf
  13. */
  14. class PhpEvaluator
  15. {
  16. /**
  17. * @var Canvas
  18. */
  19. protected $_canvas;
  20. /**
  21. * PhpEvaluator constructor.
  22. * @param Canvas $canvas
  23. */
  24. public function __construct(Canvas $canvas)
  25. {
  26. $this->_canvas = $canvas;
  27. }
  28. /**
  29. * @param $code
  30. * @param array $vars
  31. */
  32. public function evaluate($code, $vars = [])
  33. {
  34. if (!$this->_canvas->get_dompdf()->getOptions()->getIsPhpEnabled()) {
  35. return;
  36. }
  37. // Set up some variables for the inline code
  38. $pdf = $this->_canvas;
  39. $fontMetrics = $pdf->get_dompdf()->getFontMetrics();
  40. $PAGE_NUM = $pdf->get_page_number();
  41. $PAGE_COUNT = $pdf->get_page_count();
  42. // Override those variables if passed in
  43. foreach ($vars as $k => $v) {
  44. $$k = $v;
  45. }
  46. eval($code);
  47. }
  48. /**
  49. * @param Frame $frame
  50. */
  51. public function render(Frame $frame)
  52. {
  53. $this->evaluate($frame->get_node()->nodeValue);
  54. }
  55. }