PageRenderTime 144ms CodeModel.GetById 50ms app.highlight 35ms RepoModel.GetById 55ms app.codeStats 0ms

/htdocs/includes/fpdfi/fpdi2tcpdf_bridge.php

https://bitbucket.org/speedealing/speedealing
PHP | 167 lines | 102 code | 14 blank | 51 comment | 16 complexity | 12198416b0e16340e0fd10b7f1843f7f MD5 | raw file
  1<?php
  2//
  3//  FPDI - Version 1.4.2
  4//
  5//    Copyright 2004-2011 Setasign - Jan Slabon
  6//
  7//  Licensed under the Apache License, Version 2.0 (the "License");
  8//  you may not use this file except in compliance with the License.
  9//  You may obtain a copy of the License at
 10//
 11//      http://www.apache.org/licenses/LICENSE-2.0
 12//
 13//  Unless required by applicable law or agreed to in writing, software
 14//  distributed under the License is distributed on an "AS IS" BASIS,
 15//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 16//  See the License for the specific language governing permissions and
 17//  limitations under the License.
 18//
 19
 20/**
 21 * This class is used as a bridge between TCPDF and FPDI
 22 * and will create the possibility to use both FPDF and TCPDF
 23 * via one FPDI version.
 24 * 
 25 * We'll simply remap TCPDF to FPDF again.
 26 * 
 27 * It'll be loaded and extended by FPDF_TPL.
 28 */
 29class FPDF extends TCPDF {
 30    
 31	function _putstream($s) {
 32		$this->_out($this->_getstream($s));
 33	}
 34	
 35	function _getxobjectdict() {
 36        $out = parent::_getxobjectdict();
 37        if (count($this->tpls)) {
 38            foreach($this->tpls as $tplidx => $tpl) {
 39                $out .= sprintf('%s%d %d 0 R', $this->tplprefix, $tplidx, $tpl['n']);
 40            }
 41        }
 42        
 43        return $out;
 44    }
 45	
 46    /**
 47     * Encryption of imported data by FPDI
 48     *
 49     * @param array $value
 50     */
 51    function pdf_write_value(&$value) {
 52        switch ($value[0]) {
 53    		case PDF_TYPE_STRING:
 54				if ($this->encrypted) {
 55				    $value[1] = $this->_unescape($value[1]);
 56                    $value[1] = $this->_encrypt_data($this->_current_obj_id, $value[1]);
 57                 	$value[1] = $this->_escape($value[1]);
 58                } 
 59    			break;
 60    			
 61			case PDF_TYPE_STREAM:
 62			    if ($this->encrypted) {
 63			        $value[2][1] = $this->_encrypt_data($this->_current_obj_id, $value[2][1]);
 64			        $value[1][1]['/Length'] = array(
 65                        PDF_TYPE_NUMERIC,
 66                        strlen($value[2][1])
 67                    );
 68                }
 69                break;
 70                
 71            case PDF_TYPE_HEX:
 72            	if ($this->encrypted) {
 73                	$value[1] = $this->hex2str($value[1]);
 74                	$value[1] = $this->_encrypt_data($this->_current_obj_id, $value[1]);
 75                    
 76                	// remake hexstring of encrypted string
 77    				$value[1] = $this->str2hex($value[1]);
 78                }
 79                break;
 80    	}
 81    }
 82    
 83    /**
 84     * Unescapes a PDF string
 85     *
 86     * @param string $s
 87     * @return string
 88     */
 89    function _unescape($s) {
 90        $out = '';
 91        for ($count = 0, $n = strlen($s); $count < $n; $count++) {
 92            if ($s[$count] != '\\' || $count == $n-1) {
 93                $out .= $s[$count];
 94            } else {
 95                switch ($s[++$count]) {
 96                    case ')':
 97                    case '(':
 98                    case '\\':
 99                        $out .= $s[$count];
100                        break;
101                    case 'f':
102                        $out .= chr(0x0C);
103                        break;
104                    case 'b':
105                        $out .= chr(0x08);
106                        break;
107                    case 't':
108                        $out .= chr(0x09);
109                        break;
110                    case 'r':
111                        $out .= chr(0x0D);
112                        break;
113                    case 'n':
114                        $out .= chr(0x0A);
115                        break;
116                    case "\r":
117                        if ($count != $n-1 && $s[$count+1] == "\n")
118                            $count++;
119                        break;
120                    case "\n":
121                        break;
122                    default:
123                        // Octal-Values
124                        if (ord($s[$count]) >= ord('0') &&
125                            ord($s[$count]) <= ord('9')) {
126                            $oct = ''. $s[$count];
127                                
128                            if (ord($s[$count+1]) >= ord('0') &&
129                                ord($s[$count+1]) <= ord('9')) {
130                                $oct .= $s[++$count];
131                                
132                                if (ord($s[$count+1]) >= ord('0') &&
133                                    ord($s[$count+1]) <= ord('9')) {
134                                    $oct .= $s[++$count];    
135                                }                            
136                            }
137                            
138                            $out .= chr(octdec($oct));
139                        } else {
140                            $out .= $s[$count];
141                        }
142                }
143            }
144        }
145        return $out;
146    }
147    
148    /**
149     * Hexadecimal to string
150     *
151     * @param string $hex
152     * @return string
153     */
154    function hex2str($hex) {
155    	return pack('H*', str_replace(array("\r", "\n", ' '), '', $hex));
156    }
157    
158    /**
159     * String to hexadecimal
160     *
161     * @param string $str
162     * @return string
163     */
164    function str2hex($str) {
165        return current(unpack('H*', $str));
166    }
167}