page_cache.cls.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. /**
  3. * DOMPDF - PHP5 HTML to PDF renderer
  4. *
  5. * File: $RCSfile: page_cache.cls.php,v $
  6. * Created on: 2004-07-23
  7. *
  8. * Copyright (c) 2004 - Benj Carson <benjcarson@digitaljunkies.ca>
  9. *
  10. * This library is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2.1 of the License, or (at your option) any later version.
  14. *
  15. * This library is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public License
  21. * along with this library in the file LICENSE.LGPL; if not, write to the
  22. * Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  23. * 02111-1307 USA
  24. *
  25. * Alternatively, you may distribute this software under the terms of the
  26. * PHP License, version 3.0 or later. A copy of this license should have
  27. * been distributed with this file in the file LICENSE.PHP . If this is not
  28. * the case, you can obtain a copy at http://www.php.net/license/3_0.txt.
  29. *
  30. * The latest version of DOMPDF might be available at:
  31. * http://www.dompdf.com/
  32. *
  33. * @link http://www.dompdf.com/
  34. * @copyright 2004 Benj Carson
  35. * @author Benj Carson <benjcarson@digitaljunkies.ca>
  36. * @package dompdf
  37. */
  38. /* $Id: page_cache.cls.php 216 2010-03-11 22:49:18Z ryan.masten $ */
  39. /**
  40. * Caches individual rendered PDF pages
  41. *
  42. * Not totally implmented yet. Use at your own risk ;)
  43. *
  44. * @access private
  45. * @package dompdf
  46. * @static
  47. */
  48. class Page_Cache {
  49. const DB_USER = "dompdf_page_cache";
  50. const DB_PASS = "some meaningful password";
  51. const DB_NAME = "dompdf_page_cache";
  52. static private $__connection = null;
  53. function init() {
  54. if ( is_null(self::$__connection) ) {
  55. $con_str = "host=" . DB_HOST .
  56. " dbname=" . self::DB_NAME .
  57. " user=" . self::DB_USER .
  58. " password=" . self::DB_PASS;
  59. if ( !self::$__connection = pg_connect($con_str) )
  60. throw new Exception("Database connection failed.");
  61. }
  62. }
  63. function __construct() { throw new Exception("Can not create instance of Page_Class. Class is static."); }
  64. private static function __query($sql) {
  65. if ( !($res = pg_query(self::$__connection, $sql)) )
  66. throw new Exception(pg_last_error(self::$__connection));
  67. return $res;
  68. }
  69. static function store_page($id, $page_num, $data) {
  70. $where = "WHERE id='" . pg_escape_string($id) . "' AND ".
  71. "page_num=". pg_escape_string($page_num);
  72. $res = self::__query("SELECT timestamp FROM page_cache ". $where);
  73. $row = pg_fetch_assoc($res);
  74. if ( $row )
  75. self::__query("UPDATE page_cache SET data='" . pg_escape_string($data) . "' " . $where);
  76. else
  77. self::__query("INSERT INTO page_cache (id, page_num, data) VALUES ('" . pg_escape_string($id) . "', ".
  78. pg_escape_string($page_num) . ", ".
  79. "'". pg_escape_string($data) . "')");
  80. }
  81. static function store_fonts($id, $fonts) {
  82. self::__query("BEGIN");
  83. // Update the font information
  84. self::__query("DELETE FROM page_fonts WHERE id='" . pg_escape_string($id) . "'");
  85. foreach (array_keys($fonts) as $font)
  86. self::__query("INSERT INTO page_fonts (id, font_name) VALUES ('" .
  87. pg_escape_string($id) . "', '" . pg_escape_string($font) . "')");
  88. self::__query("COMMIT");
  89. }
  90. // static function retrieve_page($id, $page_num) {
  91. // $res = self::__query("SELECT data FROM page_cache WHERE id='" . pg_escape_string($id) . "' AND ".
  92. // "page_num=". pg_escape_string($page_num));
  93. // $row = pg_fetch_assoc($res);
  94. // return pg_unescape_bytea($row["data"]);
  95. // }
  96. static function get_page_timestamp($id, $page_num) {
  97. $res = self::__query("SELECT timestamp FROM page_cache WHERE id='" . pg_escape_string($id) . "' AND ".
  98. "page_num=". pg_escape_string($page_num));
  99. $row = pg_fetch_assoc($res);
  100. return $row["timestamp"];
  101. }
  102. // Adds the cached document referenced by $id to the provided pdf
  103. static function insert_cached_document(CPDF_Adapter $pdf, $id, $new_page = true) {
  104. $res = self::__query("SELECT font_name FROM page_fonts WHERE id='" . pg_escape_string($id) . "'");
  105. // Ensure that the fonts needed by the cached document are loaded into
  106. // the pdf
  107. while ($row = pg_fetch_assoc($res))
  108. $pdf->get_cpdf()->selectFont($row["font_name"]);
  109. $res = self::__query("SELECT data FROM page_cache WHERE id='" . pg_escape_string($id) . "'");
  110. if ( $new_page )
  111. $pdf->new_page();
  112. $first = true;
  113. while ($row = pg_fetch_assoc($res)) {
  114. if ( !$first )
  115. $pdf->new_page();
  116. else
  117. $first = false;
  118. $page = $pdf->reopen_serialized_object($row["data"]);
  119. //$pdf->close_object();
  120. $pdf->add_object($page, "add");
  121. }
  122. }
  123. }
  124. Page_Cache::init();