Blame view

sources/3rdparty/dompdf/include/page_cache.cls.php 3.81 KB
31b7f2792   Kload   Upgrade to ownclo...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
  <?php
  /**
   * @package dompdf
   * @link    http://www.dompdf.com/
   * @author  Benj Carson <benjcarson@digitaljunkies.ca>
   * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
   * @version $Id: page_cache.cls.php 448 2011-11-13 13:00:03Z fabien.menager $
   */
  
  /**
   * Caches individual rendered PDF pages
   *
   * Not totally implemented yet.  Use at your own risk ;)
   * 
   * @access private
   * @package dompdf
   * @static
   */
  class Page_Cache {
  
    const DB_USER = "dompdf_page_cache";
    const DB_PASS = "some meaningful password";
    const DB_NAME = "dompdf_page_cache";
    
    static private $__connection = null;
    
    function init() {
      if ( is_null(self::$__connection) ) {
        $con_str = "host=" . DB_HOST .
          " dbname=" . self::DB_NAME .
          " user=" . self::DB_USER .
          " password=" . self::DB_PASS;
        
        if ( !self::$__connection = pg_connect($con_str) )
          throw new Exception("Database connection failed.");
      }
    }
    
    function __construct() { throw new Exception("Can not create instance of Page_Class.  Class is static."); }
  
    private static function __query($sql) {
      if ( !($res = pg_query(self::$__connection, $sql)) )
        throw new Exception(pg_last_error(self::$__connection));
      return $res;
    }
    
    static function store_page($id, $page_num, $data) {
      $where = "WHERE id='" . pg_escape_string($id) . "' AND ".
        "page_num=". pg_escape_string($page_num);
  
      $res = self::__query("SELECT timestamp FROM page_cache ". $where);
  
      $row = pg_fetch_assoc($res);
      
      if ( $row ) 
        self::__query("UPDATE page_cache SET data='" . pg_escape_string($data) . "' " . $where);
      else 
        self::__query("INSERT INTO page_cache (id, page_num, data) VALUES ('" . pg_escape_string($id) . "', ".
                       pg_escape_string($page_num) . ", ".
                       "'". pg_escape_string($data) . "')");
  
    }
  
    static function store_fonts($id, $fonts) {
      self::__query("BEGIN");
      // Update the font information
      self::__query("DELETE FROM page_fonts WHERE id='" . pg_escape_string($id) . "'");
  
      foreach (array_keys($fonts) as $font)
        self::__query("INSERT INTO page_fonts (id, font_name) VALUES ('" .
                      pg_escape_string($id) . "', '" . pg_escape_string($font) . "')");
      self::__query("COMMIT");
    }
    
  //   static function retrieve_page($id, $page_num) {
  
  //     $res = self::__query("SELECT data FROM page_cache WHERE id='" . pg_escape_string($id) . "' AND ".
  //                           "page_num=". pg_escape_string($page_num));
  
  //     $row = pg_fetch_assoc($res);
  
  //     return pg_unescape_bytea($row["data"]);
      
  //   }
  
    static function get_page_timestamp($id, $page_num) {
      $res = self::__query("SELECT timestamp FROM page_cache WHERE id='" . pg_escape_string($id) . "' AND ".
                            "page_num=". pg_escape_string($page_num));
  
      $row = pg_fetch_assoc($res);
  
      return $row["timestamp"];
      
    }
  
    // Adds the cached document referenced by $id to the provided pdf
    static function insert_cached_document(CPDF_Adapter $pdf, $id, $new_page = true) {
      $res = self::__query("SELECT font_name FROM page_fonts WHERE id='" . pg_escape_string($id) . "'");
  
      // Ensure that the fonts needed by the cached document are loaded into
      // the pdf
      while ($row = pg_fetch_assoc($res)) 
        $pdf->get_cpdf()->selectFont($row["font_name"]);
      
      $res = self::__query("SELECT data FROM page_cache WHERE id='" . pg_escape_string($id) . "'");
  
      if ( $new_page )
        $pdf->new_page();
  
      $first = true;
      while ($row = pg_fetch_assoc($res)) {
  
        if ( !$first ) 
          $pdf->new_page();
        else 
          $first = false;        
        
        $page = $pdf->reopen_serialized_object($row["data"]);
        //$pdf->close_object();
        $pdf->add_object($page, "add");
  
      }
        
    }
  }
  
  Page_Cache::init();