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.digitaljunkies.ca/dompdf |
---|
32 | * |
---|
33 | * @link http://www.digitaljunkies.ca/dompdf |
---|
34 | * @copyright 2004 Benj Carson |
---|
35 | * @author Benj Carson <benjcarson@digitaljunkies.ca> |
---|
36 | * @package dompdf |
---|
37 | * @version 0.5.1 |
---|
38 | */ |
---|
39 | |
---|
40 | /* $Id: page_cache.cls.php 186 2009-10-19 22:42:06Z eclecticgeek@gmail.com $ */ |
---|
41 | |
---|
42 | /** |
---|
43 | * Caches individual rendered PDF pages |
---|
44 | * |
---|
45 | * Not totally implmented yet. Use at your own risk ;) |
---|
46 | * |
---|
47 | * @access private |
---|
48 | * @package dompdf |
---|
49 | * @static |
---|
50 | */ |
---|
51 | class Page_Cache { |
---|
52 | |
---|
53 | const DB_USER = "dompdf_page_cache"; |
---|
54 | const DB_PASS = "some meaningful password"; |
---|
55 | const DB_NAME = "dompdf_page_cache"; |
---|
56 | |
---|
57 | static private $__connection = null; |
---|
58 | |
---|
59 | function init() { |
---|
60 | if ( is_null(self::$__connection) ) { |
---|
61 | $con_str = "host=" . DB_HOST . |
---|
62 | " dbname=" . self::DB_NAME . |
---|
63 | " user=" . self::DB_USER . |
---|
64 | " password=" . self::DB_PASS; |
---|
65 | |
---|
66 | if ( !self::$__connection = pg_connect($con_str) ) |
---|
67 | throw new Exception("Database connection failed."); |
---|
68 | } |
---|
69 | } |
---|
70 | |
---|
71 | function __construct() { throw new Exception("Can not create instance of Page_Class. Class is static."); } |
---|
72 | |
---|
73 | private static function __query($sql) { |
---|
74 | if ( !($res = pg_query(self::$__connection, $sql)) ) |
---|
75 | throw new Exception(pg_last_error(self::$__connection)); |
---|
76 | return $res; |
---|
77 | } |
---|
78 | |
---|
79 | static function store_page($id, $page_num, $data) { |
---|
80 | $where = "WHERE id='" . pg_escape_string($id) . "' AND ". |
---|
81 | "page_num=". pg_escape_string($page_num); |
---|
82 | |
---|
83 | $res = self::__query("SELECT timestamp FROM page_cache ". $where); |
---|
84 | |
---|
85 | $row = pg_fetch_assoc($res); |
---|
86 | |
---|
87 | if ( $row ) |
---|
88 | self::__query("UPDATE page_cache SET data='" . pg_escape_string($data) . "' " . $where); |
---|
89 | else |
---|
90 | self::__query("INSERT INTO page_cache (id, page_num, data) VALUES ('" . pg_escape_string($id) . "', ". |
---|
91 | pg_escape_string($page_num) . ", ". |
---|
92 | "'". pg_escape_string($data) . "')"); |
---|
93 | |
---|
94 | } |
---|
95 | |
---|
96 | static function store_fonts($id, $fonts) { |
---|
97 | self::__query("BEGIN"); |
---|
98 | // Update the font information |
---|
99 | self::__query("DELETE FROM page_fonts WHERE id='" . pg_escape_string($id) . "'"); |
---|
100 | |
---|
101 | foreach (array_keys($fonts) as $font) |
---|
102 | self::__query("INSERT INTO page_fonts (id, font_name) VALUES ('" . |
---|
103 | pg_escape_string($id) . "', '" . pg_escape_string($font) . "')"); |
---|
104 | self::__query("COMMIT"); |
---|
105 | } |
---|
106 | |
---|
107 | // static function retrieve_page($id, $page_num) { |
---|
108 | |
---|
109 | // $res = self::__query("SELECT data FROM page_cache WHERE id='" . pg_escape_string($id) . "' AND ". |
---|
110 | // "page_num=". pg_escape_string($page_num)); |
---|
111 | |
---|
112 | // $row = pg_fetch_assoc($res); |
---|
113 | |
---|
114 | // return pg_unescape_bytea($row["data"]); |
---|
115 | |
---|
116 | // } |
---|
117 | |
---|
118 | static function get_page_timestamp($id, $page_num) { |
---|
119 | $res = self::__query("SELECT timestamp FROM page_cache WHERE id='" . pg_escape_string($id) . "' AND ". |
---|
120 | "page_num=". pg_escape_string($page_num)); |
---|
121 | |
---|
122 | $row = pg_fetch_assoc($res); |
---|
123 | |
---|
124 | return $row["timestamp"]; |
---|
125 | |
---|
126 | } |
---|
127 | |
---|
128 | // Adds the cached document referenced by $id to the provided pdf |
---|
129 | static function insert_cached_document(CPDF_Adapter $pdf, $id, $new_page = true) { |
---|
130 | $res = self::__query("SELECT font_name FROM page_fonts WHERE id='" . pg_escape_string($id) . "'"); |
---|
131 | |
---|
132 | // Ensure that the fonts needed by the cached document are loaded into |
---|
133 | // the pdf |
---|
134 | while ($row = pg_fetch_assoc($res)) |
---|
135 | $pdf->get_cpdf()->selectFont($row["font_name"]); |
---|
136 | |
---|
137 | $res = self::__query("SELECT data FROM page_cache WHERE id='" . pg_escape_string($id) . "'"); |
---|
138 | |
---|
139 | if ( $new_page ) |
---|
140 | $pdf->new_page(); |
---|
141 | |
---|
142 | $first = true; |
---|
143 | while ($row = pg_fetch_assoc($res)) { |
---|
144 | |
---|
145 | if ( !$first ) |
---|
146 | $pdf->new_page(); |
---|
147 | else |
---|
148 | $first = false; |
---|
149 | |
---|
150 | $page = $pdf->reopen_serialized_object($row["data"]); |
---|
151 | //$pdf->close_object(); |
---|
152 | $pdf->add_object($page, "add"); |
---|
153 | |
---|
154 | } |
---|
155 | |
---|
156 | } |
---|
157 | } |
---|
158 | |
---|
159 | Page_Cache::init(); |
---|
160 | ?> |
---|