Qual: update fpdi libraries:

FPDF_TPL v1.1.4
FPDI v1.3.2
FPDI_Protection v1.0.3
This commit is contained in:
Regis Houssin 2010-03-03 09:13:45 +00:00
parent 3605390156
commit 925ead825c
14 changed files with 2829 additions and 2590 deletions

View File

@ -14,10 +14,10 @@ ArtiChow 1.07 Public Domain Yes Graphics
Php-barcode 0.3pl1 GPL 2.0 Yes Bar code generation
EFC/XFSS 1.0.1 LGPL 3.0 Yes Enhanced File Crypt/Extended File Stealth System
FCKEditor 2.6.4 LGPL 2.1 or Mozilla PL 1.0 Yes Editor WYSIWYG
FPDF 1.53 Public domain Yes PDF generation (original code is modified)
FPDF_TPL 1.1.2 Apache Software License 2.0 No GPL3 only PDF templates management
FPDI 1.2.1 Apache Software License 2.0 No GPL3 only PDF templates management
FPDI_Protection 1.0.2 Apache Software License 2.0 No GPL3 only PDF encryption (8 files)
FPDF 1.6 Public domain Yes PDF generation (original code is modified)
FPDF_TPL 1.1.4 Apache Software License 2.0 No GPL3 only PDF templates management
FPDI 1.3.2 Apache Software License 2.0 No GPL3 only PDF templates management
FPDI_Protection 1.0.3 Apache Software License 2.0 No GPL3 only PDF encryption (8 files)
GeoIP x.x Yes GeoIP Maxmind conversion
iWebkit 4.6.2 LGPL 3.0 Yes Iphone templates framework
JCrop 0.9.8 MIT Licence Yes JS library to crop images

View File

@ -1,95 +0,0 @@
<?php
//
// FPDI - Version 1.2.1
//
// Copyright 2004-2008 Setasign - Jan Slabon
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
if (!defined("ORD_z"))
define("ORD_z",ord('z'));
if (!defined("ORD_exclmark"))
define("ORD_exclmark", ord('!'));
if (!defined("ORD_u"))
define("ORD_u", ord("u"));
if (!defined("ORD_tilde"))
define("ORD_tilde", ord('~'));
class ASCII85Decode {
function ASCII85Decode(&$fpdi) {
$this->fpdi =& $fpdi;
}
function decode($in) {
$out = "";
$state = 0;
$chn = null;
$l = strlen($in);
for ($k = 0; $k < $l; ++$k) {
$ch = ord($in[$k]) & 0xff;
if ($ch == ORD_tilde) {
break;
}
if (preg_match("/^\s$/",chr($ch))) {
continue;
}
if ($ch == ORD_z && $state == 0) {
$out .= chr(0).chr(0).chr(0).chr(0);
continue;
}
if ($ch < ORD_exclmark || $ch > ORD_u) {
$this->fpdi->error("Illegal character in ASCII85Decode.");
}
$chn[$state++] = $ch - ORD_exclmark;
if ($state == 5) {
$state = 0;
$r = 0;
for ($j = 0; $j < 5; ++$j)
$r = $r * 85 + $chn[$j];
$out .= chr($r >> 24);
$out .= chr($r >> 16);
$out .= chr($r >> 8);
$out .= chr($r);
}
}
$r = 0;
if ($state == 1)
$this->fpdi->error("Illegal length in ASCII85Decode.");
if ($state == 2) {
$r = $chn[0] * 85 * 85 * 85 * 85 + ($chn[1]+1) * 85 * 85 * 85;
$out .= chr($r >> 24);
}
else if ($state == 3) {
$r = $chn[0] * 85 * 85 * 85 * 85 + $chn[1] * 85 * 85 * 85 + ($chn[2]+1) * 85 * 85;
$out .= chr($r >> 24);
$out .= chr($r >> 16);
}
else if ($state == 4) {
$r = $chn[0] * 85 * 85 * 85 * 85 + $chn[1] * 85 * 85 * 85 + $chn[2] * 85 * 85 + ($chn[3]+1) * 85 ;
$out .= chr($r >> 24);
$out .= chr($r >> 16);
$out .= chr($r >> 8);
}
return $out;
}
}

View File

@ -1,147 +0,0 @@
<?php
//
// FPDI - Version 1.2.1
//
// Copyright 2004-2008 Setasign - Jan Slabon
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
class LZWDecode {
var $sTable = array();
var $data = null;
var $tIdx;
var $bitsToGet = 9;
var $bytePointer;
var $bitPointer;
var $nextData = 0;
var $nextBits = 0;
var $andTable = array(511, 1023, 2047, 4095);
function LZWDecode(&$fpdi) {
$this->fpdi =& $fpdi;
}
/**
* Method to decode LZW compressed data.
*
* @param string data The compressed data.
*/
function decode($data) {
if($data[0] == 0x00 && $data[1] == 0x01) {
$this->fpdi->error("LZW flavour not supported.");
}
$this->initsTable();
$this->data = $data;
// Initialize pointers
$this->bytePointer = 0;
$this->bitPointer = 0;
$this->nextData = 0;
$this->nextBits = 0;
$oldCode = 0;
$string = "";
$uncompData = "";
while (($code = $this->getNextCode()) != 257) {
if ($code == 256) {
$this->initsTable();
$code = $this->getNextCode();
if ($code == 257) {
break;
}
$uncompData .= $this->sTable[$code];
$oldCode = $code;
} else {
if ($code < $this->tIdx) {
$string = $this->sTable[$code];
$uncompData .= $string;
$this->addStringToTable($this->sTable[$oldCode], $string[0]);
$oldCode = $code;
} else {
$string = $this->sTable[$oldCode];
$string = $string.$string[0];
$uncompData .= $string;
$this->addStringToTable($string);
$oldCode = $code;
}
}
}
return $uncompData;
}
/**
* Initialize the string table.
*/
function initsTable() {
$this->sTable = array();
for ($i = 0; $i < 256; $i++)
$this->sTable[$i] = chr($i);
$this->tIdx = 258;
$this->bitsToGet = 9;
}
/**
* Add a new string to the string table.
*/
function addStringToTable ($oldString, $newString="") {
$string = $oldString.$newString;
// Add this new String to the table
$this->sTable[$this->tIdx++] = $string;
if ($this->tIdx == 511) {
$this->bitsToGet = 10;
} else if ($this->tIdx == 1023) {
$this->bitsToGet = 11;
} else if ($this->tIdx == 2047) {
$this->bitsToGet = 12;
}
}
// Returns the next 9, 10, 11 or 12 bits
function getNextCode() {
if ($this->bytePointer == strlen($this->data))
return 257;
$this->nextData = ($this->nextData << 8) | (ord($this->data[$this->bytePointer++]) & 0xff);
$this->nextBits += 8;
if ($this->nextBits < $this->bitsToGet) {
$this->nextData = ($this->nextData << 8) | (ord($this->data[$this->bytePointer++]) & 0xff);
$this->nextBits += 8;
}
$code = ($this->nextData >> ($this->nextBits - $this->bitsToGet)) & $this->andTable[$this->bitsToGet-9];
$this->nextBits -= $this->bitsToGet;
return $code;
}
}

View File

@ -0,0 +1,104 @@
<?php
//
// FPDI - Version 1.3.2
//
// Copyright 2004-2010 Setasign - Jan Slabon
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
if (!defined('ORD_z'))
define('ORD_z',ord('z'));
if (!defined('ORD_exclmark'))
define('ORD_exclmark', ord('!'));
if (!defined('ORD_u'))
define('ORD_u', ord('u'));
if (!defined('ORD_tilde'))
define('ORD_tilde', ord('~'));
$__tmp = version_compare(phpversion(), "5") == -1 ? array('FilterASCII85') : array('FilterASCII85', false);
if (!call_user_func_array('class_exists', $__tmp)) {
class FilterASCII85 {
function error($msg) {
die($msg);
}
function decode($in) {
$out = '';
$state = 0;
$chn = null;
$l = strlen($in);
for ($k = 0; $k < $l; ++$k) {
$ch = ord($in[$k]) & 0xff;
if ($ch == ORD_tilde) {
break;
}
if (preg_match('/^\s$/',chr($ch))) {
continue;
}
if ($ch == ORD_z && $state == 0) {
$out .= chr(0).chr(0).chr(0).chr(0);
continue;
}
if ($ch < ORD_exclmark || $ch > ORD_u) {
return $this->error('Illegal character in ASCII85Decode.');
}
$chn[$state++] = $ch - ORD_exclmark;
if ($state == 5) {
$state = 0;
$r = 0;
for ($j = 0; $j < 5; ++$j)
$r = $r * 85 + $chn[$j];
$out .= chr($r >> 24);
$out .= chr($r >> 16);
$out .= chr($r >> 8);
$out .= chr($r);
}
}
$r = 0;
if ($state == 1)
return $this->error('Illegal length in ASCII85Decode.');
if ($state == 2) {
$r = $chn[0] * 85 * 85 * 85 * 85 + ($chn[1]+1) * 85 * 85 * 85;
$out .= chr($r >> 24);
}
else if ($state == 3) {
$r = $chn[0] * 85 * 85 * 85 * 85 + $chn[1] * 85 * 85 * 85 + ($chn[2]+1) * 85 * 85;
$out .= chr($r >> 24);
$out .= chr($r >> 16);
}
else if ($state == 4) {
$r = $chn[0] * 85 * 85 * 85 * 85 + $chn[1] * 85 * 85 * 85 + $chn[2] * 85 * 85 + ($chn[3]+1) * 85 ;
$out .= chr($r >> 24);
$out .= chr($r >> 16);
$out .= chr($r >> 8);
}
return $out;
}
function encode($in) {
return $this->error("ASCII85 encoding not implemented.");
}
}
}
unset($__tmp);

View File

@ -0,0 +1,33 @@
<?php
//
// FPDI - Version 1.3.2
//
// Copyright 2004-2010 Setasign - Jan Slabon
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
require_once('FilterASCII85.php');
class FilterASCII85_FPDI extends FilterASCII85 {
var $fpdi;
function FPDI_FilterASCII85(&$fpdi) {
$this->fpdi =& $fpdi;
}
function error($msg) {
$this->fpdi->error($msg);
}
}

View File

@ -0,0 +1,160 @@
<?php
//
// FPDI - Version 1.3.2
//
// Copyright 2004-2010 Setasign - Jan Slabon
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
$__tmp = version_compare(phpversion(), "5") == -1 ? array('FilterLZW') : array('FilterLZW', false);
if (!call_user_func_array('class_exists', $__tmp)) {
class FilterLZW {
var $sTable = array();
var $data = null;
var $dataLength = 0;
var $tIdx;
var $bitsToGet = 9;
var $bytePointer;
var $bitPointer;
var $nextData = 0;
var $nextBits = 0;
var $andTable = array(511, 1023, 2047, 4095);
function error($msg) {
die($msg);
}
/**
* Method to decode LZW compressed data.
*
* @param string data The compressed data.
*/
function decode($data) {
if($data[0] == 0x00 && $data[1] == 0x01) {
$this->error('LZW flavour not supported.');
}
$this->initsTable();
$this->data = $data;
$this->dataLength = strlen($data);
// Initialize pointers
$this->bytePointer = 0;
$this->bitPointer = 0;
$this->nextData = 0;
$this->nextBits = 0;
$oldCode = 0;
$string = '';
$uncompData = '';
while (($code = $this->getNextCode()) != 257) {
if ($code == 256) {
$this->initsTable();
$code = $this->getNextCode();
if ($code == 257) {
break;
}
$uncompData .= $this->sTable[$code];
$oldCode = $code;
} else {
if ($code < $this->tIdx) {
$string = $this->sTable[$code];
$uncompData .= $string;
$this->addStringToTable($this->sTable[$oldCode], $string[0]);
$oldCode = $code;
} else {
$string = $this->sTable[$oldCode];
$string = $string.$string[0];
$uncompData .= $string;
$this->addStringToTable($string);
$oldCode = $code;
}
}
}
return $uncompData;
}
/**
* Initialize the string table.
*/
function initsTable() {
$this->sTable = array();
for ($i = 0; $i < 256; $i++)
$this->sTable[$i] = chr($i);
$this->tIdx = 258;
$this->bitsToGet = 9;
}
/**
* Add a new string to the string table.
*/
function addStringToTable ($oldString, $newString='') {
$string = $oldString.$newString;
// Add this new String to the table
$this->sTable[$this->tIdx++] = $string;
if ($this->tIdx == 511) {
$this->bitsToGet = 10;
} else if ($this->tIdx == 1023) {
$this->bitsToGet = 11;
} else if ($this->tIdx == 2047) {
$this->bitsToGet = 12;
}
}
// Returns the next 9, 10, 11 or 12 bits
function getNextCode() {
if ($this->bytePointer == $this->dataLength) {
return 257;
}
$this->nextData = ($this->nextData << 8) | (ord($this->data[$this->bytePointer++]) & 0xff);
$this->nextBits += 8;
if ($this->nextBits < $this->bitsToGet) {
$this->nextData = ($this->nextData << 8) | (ord($this->data[$this->bytePointer++]) & 0xff);
$this->nextBits += 8;
}
$code = ($this->nextData >> ($this->nextBits - $this->bitsToGet)) & $this->andTable[$this->bitsToGet-9];
$this->nextBits -= $this->bitsToGet;
return $code;
}
function encode($in) {
$this->error("LZW encoding not implemented.");
}
}
}
unset($__tmp);

View File

@ -0,0 +1,33 @@
<?php
//
// FPDI - Version 1.3.2
//
// Copyright 2004-2010 Setasign - Jan Slabon
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
require_once('FilterLZW.php');
class FilterLZW_FPDI extends FilterLZW {
var $fpdi;
function FilterLZW_FPDI(&$fpdi) {
$this->fpdi =& $fpdi;
}
function error($msg) {
$this->fpdi->error($msg);
}
}

View File

@ -1,8 +1,8 @@
<?php
//
// FPDF_TPL - Version 1.1.2
// FPDF_TPL - Version 1.1.4
//
// Copyright 2004-2008 Setasign - Jan Slabon
// Copyright 2004-2010 Setasign - Jan Slabon
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@ -19,8 +19,6 @@
// DOLCHANGE
require_once(FPDF_PATH."fpdf.php");
//require_once('E:/Mes Developpements/dolibarr/htdocs/includes/fpdf/fpdf/fpdf.php');
//require_once('C:/temp/16/fpdf.php');
class FPDF_TPL extends FPDF {
/**
@ -53,6 +51,13 @@ class FPDF_TPL extends FPDF {
*/
var $_res = array();
/**
* Last used Template data
*
* @var array
*/
var $lastUsedTemplateData = array();
/**
* Start a Template
*
@ -171,21 +176,37 @@ class FPDF_TPL extends FPDF {
}
$tpl =& $this->tpls[$tplidx];
$x = $tpl['x'];
$y = $tpl['y'];
$w = $tpl['w'];
$h = $tpl['h'];
if ($_x == null)
$_x = $x;
$_x = 0;
if ($_y == null)
$_y = $y;
$_y = 0;
$_x += $tpl['x'];
$_y += $tpl['y'];
$wh = $this->getTemplateSize($tplidx, $_w, $_h);
$_w = $wh['w'];
$_h = $wh['h'];
$this->_out(sprintf("q %.4F 0 0 %.4F %.2F %.2F cm", ($_w/$w), ($_h/$h), $_x*$this->k, ($this->h-($_y+$_h))*$this->k)); // Translate
$this->_out($this->tplprefix.$tplidx." Do Q");
$tData = array(
'x' => $this->x,
'y' => $this->y,
'w' => $_w,
'h' => $_h,
'scaleX' => ($_w/$w),
'scaleY' => ($_h/$h),
'tx' => $_x,
'ty' => ($this->h-$_y-$_h),
'lty' => ($this->h-$_y-$_h) - ($this->h-$h) * ($_h/$h)
);
$this->_out(sprintf("q %.4F 0 0 %.4F %.4F %.4F cm", $tData['scaleX'], $tData['scaleY'], $tData['tx']*$this->k, $tData['ty']*$this->k)); // Translate
$this->_out(sprintf('%s%d Do Q', $this->tplprefix, $tplidx));
$this->lastUsedTemplateData = $tData;
return array("w" => $_w, "h" => $_h);
}
@ -222,16 +243,19 @@ class FPDF_TPL extends FPDF {
}
/**
* See FPDF-Documentation ;-)
* See FPDF/TCPDF-Documentation ;-)
*/
function SetFont($family, $style='', $size=0) {
function SetFont($family, $style='', $size=0, $fontfile='') {
if (!is_subclass_of($this, 'TCPDF') && func_num_args() > 3) {
$this->Error('More than 3 arguments for the SetFont method are only available in TCPDF.');
}
/**
* force the resetting of font changes in a template
*/
if ($this->_intpl)
$this->FontFamily = '';
parent::SetFont($family, $style, $size);
parent::SetFont($family, $style, $size, $fontfile);
$fontkey = $this->FontFamily.$this->FontStyle;
@ -245,12 +269,12 @@ class FPDF_TPL extends FPDF {
/**
* See FPDF/TCPDF-Documentation ;-)
*/
function Image($file, $x, $y, $w=0, $h=0, $type='', $link='', $align='', $resize=false, $dpi=300) {
function Image($file, $x, $y, $w=0, $h=0, $type='', $link='', $align='', $resize=false, $dpi=300, $palign='', $ismask=false, $imgmask=false, $border=0, $fitbox = false, $hidden = false) {
if (!is_subclass_of($this, 'TCPDF') && func_num_args() > 7) {
$this->Error('More than 7 arguments for the Image method are only available in TCPDF.');
}
parent::Image($file, $x, $y, $w, $h, $type, $link, $align, $resize, $dpi);
parent::Image($file, $x, $y, $w, $h, $type, $link, $align, $resize, $dpi, $palign, $ismask, $imgmask, $border, $fitbox, $hidden);
if ($this->_intpl) {
$this->_res['tpl'][$this->tpl]['images'][$file] =& $this->images[$file];
} else {
@ -272,10 +296,14 @@ class FPDF_TPL extends FPDF {
/**
* Preserve adding Links in Templates ...won't work
*/
function Link($x, $y, $w, $h, $link) {
function Link($x, $y, $w, $h, $link, $spaces=0) {
if (!is_subclass_of($this, 'TCPDF') && func_num_args() > 5) {
$this->Error('More than 5 arguments for the Image method are only available in TCPDF.');
}
if ($this->_intpl)
$this->Error('Using links in templates aren\'t possible!');
parent::Link($x, $y, $w, $h, $link);
parent::Link($x, $y, $w, $h, $link, $spaces);
}
function AddLink() {
@ -304,7 +332,23 @@ class FPDF_TPL extends FPDF {
$this->_out('<<'.$filter.'/Type /XObject');
$this->_out('/Subtype /Form');
$this->_out('/FormType 1');
$this->_out(sprintf('/BBox [%.2F %.2F %.2F %.2F]',$tpl['x']*$this->k, ($tpl['h']-$tpl['y'])*$this->k, $tpl['w']*$this->k, ($tpl['h']-$tpl['y']-$tpl['h'])*$this->k));
$this->_out(sprintf('/BBox [%.2F %.2F %.2F %.2F]',
// llx
$tpl['x']*$this->k,
// lly
-$tpl['y']*$this->k,
// urx
($tpl['w']+$tpl['x'])*$this->k,
// ury
($tpl['h']-$tpl['y'])*$this->k
));
if ($tpl['x'] != 0 || $tpl['y'] != 0) {
$this->_out(sprintf('/Matrix [1 0 0 1 %.5F %.5F]',
-$tpl['x']*$this->k*2, $tpl['y']*$this->k*2
));
}
$this->_out('/Resources ');
$this->_out('<</ProcSet [/PDF /Text /ImageB /ImageC /ImageI]');
@ -337,46 +381,12 @@ class FPDF_TPL extends FPDF {
}
/**
* Private Method
* Overwritten to add _putformxobjects() after _putimages()
*
*/
function _putresources() {
if (!is_subclass_of($this, 'TCPDF')) {
$this->_putfonts();
$this->_putimages();
$this->_putformxobjects();
//Resource dictionary
$this->offsets[2]=strlen($this->buffer);
$this->_out('2 0 obj');
$this->_out('<<');
$this->_putresourcedict();
$this->_out('>>');
$this->_out('endobj');
} else {
$this->_putextgstates();
$this->_putocg();
$this->_putfonts();
$this->_putimages();
$this->_putshaders();
$this->_putformxobjects();
//Resource dictionary
$this->offsets[2]=strlen($this->buffer);
$this->_out('2 0 obj');
$this->_out('<<');
$this->_putresourcedict();
$this->_out('>>');
$this->_out('endobj');
$this->_putjavascript();
$this->_putbookmarks();
// encryption
if ($this->encrypted) {
$this->_newobj();
$this->enc_obj_id = $this->n;
$this->_out('<<');
$this->_putencryption();
$this->_out('>>');
$this->_out('endobj');
}
}
function _putimages() {
parent::_putimages();
$this->_putformxobjects();
}
function _putxobjectdict() {
@ -384,7 +394,7 @@ class FPDF_TPL extends FPDF {
if (count($this->tpls)) {
foreach($this->tpls as $tplidx => $tpl) {
$this->_out($this->tplprefix.$tplidx.' '.$tpl['n'].' 0 R');
$this->_out(sprintf('%s%d %d 0 R', $this->tplprefix, $tplidx, $tpl['n']));
}
}
}

View File

@ -1,8 +1,8 @@
<?php
//
// FPDI - Version 1.2.1
// FPDI - Version 1.3.2
//
// Copyright 2004-2008 Setasign - Jan Slabon
// Copyright 2004-2010 Setasign - Jan Slabon
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@ -17,15 +17,17 @@
// limitations under the License.
//
define('FPDI_VERSION','1.2.1');
define('FPDI_VERSION','1.3.2');
// Check for TCPDF and remap TCPDF to FPDF
if (class_exists('TCPDF')) {
$__tmp = version_compare(phpversion(), "5") == -1 ? array('TCPDF') : array('TCPDF', false);
if (call_user_func_array('class_exists', $__tmp)) {
require_once('fpdi2tcpdf_bridge.php');
}
unset($__tmp);
require_once("fpdf_tpl.php");
require_once("fpdi_pdf_parser.php");
require_once('fpdf_tpl.php');
require_once('fpdi_pdf_parser.php');
class FPDI extends FPDF_TPL {
@ -85,7 +87,7 @@ class FPDI extends FPDF_TPL {
$fn =& $this->current_filename;
if (!isset($this->parsers[$fn]))
$this->parsers[$fn] = new fpdi_pdf_parser($fn,$this);
$this->parsers[$fn] = new fpdi_pdf_parser($fn, $this);
$this->current_parser =& $this->parsers[$fn];
return $this->parsers[$fn]->getPageCount();
@ -99,7 +101,7 @@ class FPDI extends FPDF_TPL {
*/
function importPage($pageno, $boxName='/CropBox') {
if ($this->_intpl) {
return $this->error("Please import the desired pages before creating a new template.");
return $this->error('Please import the desired pages before creating a new template.');
}
$fn =& $this->current_filename;
@ -112,15 +114,8 @@ class FPDI extends FPDF_TPL {
$parser =& $this->parsers[$fn];
$parser->setPageno($pageno);
$this->tpl++;
$this->tpls[$this->tpl] = array();
$tpl =& $this->tpls[$this->tpl];
$tpl['parser'] =& $parser;
$tpl['resources'] = $parser->getPageResources();
$tpl['buffer'] = $parser->getContent();
if (!in_array($boxName, $parser->availableBoxes))
return $this->Error(sprintf("Unknown box: %s", $boxName));
return $this->Error(sprintf('Unknown box: %s', $boxName));
$pageboxes = $parser->getPageBoxes($pageno);
/**
@ -130,28 +125,35 @@ class FPDI extends FPDF_TPL {
* TrimBox: Default -> CropBox
* ArtBox: Default -> CropBox
*/
if (!isset($pageboxes[$boxName]) && ($boxName == "/BleedBox" || $boxName == "/TrimBox" || $boxName == "/ArtBox"))
$boxName = "/CropBox";
if (!isset($pageboxes[$boxName]) && $boxName == "/CropBox")
$boxName = "/MediaBox";
if (!isset($pageboxes[$boxName]) && ($boxName == '/BleedBox' || $boxName == '/TrimBox' || $boxName == '/ArtBox'))
$boxName = '/CropBox';
if (!isset($pageboxes[$boxName]) && $boxName == '/CropBox')
$boxName = '/MediaBox';
if (!isset($pageboxes[$boxName]))
return false;
$this->lastUsedPageBox = $boxName;
$box = $pageboxes[$boxName];
$this->tpl++;
$this->tpls[$this->tpl] = array();
$tpl =& $this->tpls[$this->tpl];
$tpl['parser'] =& $parser;
$tpl['resources'] = $parser->getPageResources();
$tpl['buffer'] = $parser->getContent();
$tpl['box'] = $box;
// To build an array that can be used by PDF_TPL::useTemplate()
$this->tpls[$this->tpl] = array_merge($this->tpls[$this->tpl],$box);
$this->tpls[$this->tpl] = array_merge($this->tpls[$this->tpl], $box);
// An imported page will start at 0,0 everytime. Translation will be set in _putformxobjects()
$tpl['x'] = 0;
$tpl['y'] = 0;
$page =& $parser->pages[$parser->pageno];
// fix for rotated pages
// handle rotated pages
$rotation = $parser->getPageRotation($pageno);
$tpl['_rotationAngle'] = 0;
if (isset($rotation[1]) && ($angle = $rotation[1] % 360) != 0) {
$steps = $angle / 90;
@ -160,23 +162,7 @@ class FPDI extends FPDF_TPL {
$tpl['w'] = $steps % 2 == 0 ? $_w : $_h;
$tpl['h'] = $steps % 2 == 0 ? $_h : $_w;
if ($steps % 2 != 0) {
$x = $y = ($steps == 1 || $steps == -3) ? $tpl['h'] : $tpl['w'];
} else {
$x = $tpl['w'];
$y = $tpl['h'];
}
$cx=($x/2+$tpl['box']['x'])*$this->k;
$cy=($y/2+$tpl['box']['y'])*$this->k;
$angle*=-1;
$angle*=M_PI/180;
$c=cos($angle);
$s=sin($angle);
$tpl['buffer'] = sprintf('q %.5F %.5F %.5F %.5F %.2F %.2F cm 1 0 0 1 %.2F %.2F cm %s Q',$c,$s,-$s,$c,$cx,$cy,-$cx,-$cy, $tpl['buffer']);
$tpl['_rotationAngle'] = $angle*-1;
}
$this->_importedPages[$pageKey] = $this->tpl;
@ -188,7 +174,21 @@ class FPDI extends FPDF_TPL {
return $this->lastUsedPageBox;
}
function useTemplate($tplidx, $_x=null, $_y=null, $_w=0, $_h=0) {
function useTemplate($tplidx, $_x=null, $_y=null, $_w=0, $_h=0, $adjustPageSize=false) {
if ($adjustPageSize == true && is_null($_x) && is_null($_y)) {
$size = $this->getTemplateSize($tplidx, $_w, $_h);
$format = array($size['w'], $size['h']);
if ($format[0]!=$this->CurPageFormat[0] || $format[1]!=$this->CurPageFormat[1]) {
$this->w=$format[0];
$this->h=$format[1];
$this->wPt=$this->w*$this->k;
$this->hPt=$this->h*$this->k;
$this->PageBreakTrigger=$this->h-$this->bMargin;
$this->CurPageFormat=$format;
$this->PageSizes[$this->page]=array($this->wPt, $this->hPt);
}
}
$this->_out('q 0 J 1 w 0 j 0 G 0 g'); // reset standard values
$s = parent::useTemplate($tplidx, $_x, $_y, $_w, $_h);
$this->_out('Q');
@ -203,7 +203,7 @@ class FPDI extends FPDF_TPL {
foreach($this->parsers AS $filename => $p) {
$this->current_parser =& $this->parsers[$filename];
if (isset($this->_obj_stack[$filename]) && is_array($this->_obj_stack[$filename])) {
while($n = key($this->_obj_stack[$filename])) {
while(($n = key($this->_obj_stack[$filename])) !== null) {
$nObj = $this->current_parser->pdf_resolve_object($this->current_parser->c,$this->_obj_stack[$filename][$n][1]);
$this->_newobj($this->_obj_stack[$filename][$n][0]);
@ -224,51 +224,6 @@ class FPDI extends FPDF_TPL {
}
}
/**
* Put resources
*/
function _putresources() {
if (!is_subclass_of($this, 'TCPDF')) {
$this->_putfonts();
$this->_putimages();
$this->_putformxobjects();
$this->_putimportedobjects();
//Resource dictionary
$this->offsets[2]=strlen($this->buffer);
$this->_out('2 0 obj');
$this->_out('<<');
$this->_putresourcedict();
$this->_out('>>');
$this->_out('endobj');
} else { // TCPDF - Part
$this->_putextgstates();
$this->_putocg();
$this->_putfonts();
$this->_putimages();
$this->_putshaders();
$this->_putformxobjects();
$this->_putimportedobjects();
//Resource dictionary
$this->offsets[2]=strlen($this->buffer);
$this->_out('2 0 obj');
$this->_out('<<');
$this->_putresourcedict();
$this->_out('>>');
$this->_out('endobj');
$this->_putjavascript();
$this->_putbookmarks();
// encryption
if ($this->encrypted) {
$this->_newobj();
$this->enc_obj_id = $this->n;
$this->_out('<<');
$this->_putencryption();
$this->_out('>>');
$this->_out('endobj');
}
}
}
/**
* Private Method that writes the form xobjects
@ -287,14 +242,54 @@ class FPDI extends FPDF_TPL {
$this->_out('/FormType 1');
$this->_out(sprintf('/BBox [%.2F %.2F %.2F %.2F]',
($tpl['x'] + (isset($tpl['box']['x'])?$tpl['box']['x']:0))*$this->k,
($tpl['h'] + (isset($tpl['box']['y'])?$tpl['box']['y']:0) - $tpl['y'])*$this->k,
($tpl['w'] + (isset($tpl['box']['x'])?$tpl['box']['x']:0))*$this->k,
($tpl['h'] + (isset($tpl['box']['y'])?$tpl['box']['y']:0) - $tpl['y']-$tpl['h'])*$this->k)
);
(isset($tpl['box']['llx']) ? $tpl['box']['llx'] : $tpl['x'])*$this->k,
(isset($tpl['box']['lly']) ? $tpl['box']['lly'] : -$tpl['y'])*$this->k,
(isset($tpl['box']['urx']) ? $tpl['box']['urx'] : $tpl['w'] + $tpl['x'])*$this->k,
(isset($tpl['box']['ury']) ? $tpl['box']['ury'] : $tpl['h']-$tpl['y'])*$this->k
));
if (isset($tpl['box']))
$this->_out(sprintf('/Matrix [1 0 0 1 %.5F %.5F]',-$tpl['box']['x']*$this->k, -$tpl['box']['y']*$this->k));
$c = 1;
$s = 0;
$tx = 0;
$ty = 0;
if (isset($tpl['box'])) {
$tx = -$tpl['box']['llx'];
$ty = -$tpl['box']['lly'];
if ($tpl['_rotationAngle'] <> 0) {
$angle = $tpl['_rotationAngle'] * M_PI/180;
$c=cos($angle);
$s=sin($angle);
switch($tpl['_rotationAngle']) {
case -90:
$tx = -$tpl['box']['lly'];
$ty = $tpl['box']['urx'];
break;
case -180:
$tx = $tpl['box']['urx'];
$ty = $tpl['box']['ury'];
break;
case -270:
$tx = $tpl['box']['ury'];
$ty = -$tpl['box']['llx'];
break;
}
}
} else if ($tpl['x'] != 0 || $tpl['y'] != 0) {
$tx = -$tpl['x']*2;
$ty = $tpl['y']*2;
}
$tx *= $this->k;
$ty *= $this->k;
if ($c != 1 || $s != 0 || $tx != 0 || $ty != 0) {
$this->_out(sprintf('/Matrix [%.5F %.5F %.5F %.5F %.5F %.5F]',
$c, $s, -$s, $c, $tx, $ty
));
}
$this->_out('/Resources ');
@ -333,6 +328,8 @@ class FPDI extends FPDF_TPL {
$this->_out('endobj');
$this->n = $nN; // TCPDF: reset to new "n"
}
$this->_putimportedobjects();
}
/**
@ -343,12 +340,13 @@ class FPDI extends FPDF_TPL {
$obj_id = ++$this->n;
}
//Begin a new object
//Begin a new object
if (!$onlynewobj) {
$this->offsets[$obj_id] = strlen($this->buffer);
$this->offsets[$obj_id] = is_subclass_of($this, 'TCPDF') ? $this->bufferlen : strlen($this->buffer);
$this->_out($obj_id.' 0 obj');
$this->_current_obj_id = $obj_id; // for later use with encryption
}
return $obj_id;
}
/**
@ -365,12 +363,16 @@ class FPDI extends FPDF_TPL {
switch ($value[0]) {
case PDF_TYPE_NUMERIC :
case PDF_TYPE_TOKEN :
$this->_straightOut($value[1] . ' ');
break;
case PDF_TYPE_NUMERIC :
case PDF_TYPE_REAL :
// A numeric value or a token.
// Simply output them
$this->_out($value[1]." ", false);
if (is_float($value[1]) && $value[1] != 0) {
$this->_straightOut(rtrim(rtrim(sprintf('%F', $value[1]), '0'), '.') .' ');
} else {
$this->_straightOut($value[1] . ' ');
}
break;
case PDF_TYPE_ARRAY :
@ -378,27 +380,27 @@ class FPDI extends FPDF_TPL {
// An array. Output the proper
// structure and move on.
$this->_out("[",false);
$this->_straightOut('[');
for ($i = 0; $i < count($value[1]); $i++) {
$this->pdf_write_value($value[1][$i]);
}
$this->_out("]");
$this->_out(']');
break;
case PDF_TYPE_DICTIONARY :
// A dictionary.
$this->_out("<<",false);
$this->_straightOut('<<');
reset ($value[1]);
while (list($k, $v) = each($value[1])) {
$this->_out($k . " ",false);
$this->_straightOut($k . ' ');
$this->pdf_write_value($v);
}
$this->_out(">>");
$this->_straightOut('>>');
break;
case PDF_TYPE_OBJREF :
@ -406,20 +408,21 @@ class FPDI extends FPDF_TPL {
// An indirect object reference
// Fill the object stack if needed
$cpfn =& $this->current_parser->filename;
if (!isset($this->_don_obj_stack[$cpfn][$value[1]])) {
$this->_newobj(false,true);
$this->_obj_stack[$cpfn][$value[1]] = array($this->n, $value);
$this->_newobj(false,true);
$this->_obj_stack[$cpfn][$value[1]] = array($this->n, $value);
$this->_don_obj_stack[$cpfn][$value[1]] = array($this->n, $value); // Value is maybee obsolete!!!
}
$objid = $this->_don_obj_stack[$cpfn][$value[1]][0];
$this->_out("{$objid} 0 R");
$this->_out($objid.' 0 R');
break;
case PDF_TYPE_STRING :
// A string.
$this->_out('('.$value[1].')');
$this->_straightOut('('.$value[1].')');
break;
@ -429,23 +432,22 @@ class FPDI extends FPDF_TPL {
// stream dictionary, then the
// stream data itself.
$this->pdf_write_value($value[1]);
$this->_out("stream");
$this->_out('stream');
$this->_out($value[2][1]);
$this->_out("endstream");
$this->_out('endstream');
break;
case PDF_TYPE_HEX :
$this->_out("<".$value[1].">");
$this->_straightOut('<'.$value[1].'>');
break;
case PDF_TYPE_BOOLEAN :
$this->_out($value[1] ? 'true ' : 'false ', false);
$this->_straightOut($value[1] ? 'true ' : 'false ');
break;
case PDF_TYPE_NULL :
// The null object.
$this->_out("null");
$this->_straightOut('null ');
break;
}
}
@ -454,22 +456,25 @@ class FPDI extends FPDF_TPL {
/**
* Modified so not each call will add a newline to the output.
*/
function _out($s, $ln=true) {
//Add a line to the document
if ($this->state==2) {
if (!$this->_intpl) {
if (is_subclass_of($this, 'TCPDF') && isset($this->footerlen[$this->page]) AND ($this->footerlen[$this->page] > 0)) {
// puts data before page footer
$page = substr($this->pages[$this->page], 0, -$this->footerlen[$this->page]);
$footer = substr($this->pages[$this->page], -$this->footerlen[$this->page]);
$this->pages[$this->page] = $page." ".$s."\n".$footer;
} else {
$this->pages[$this->page] .= $s.($ln == true ? "\n" : '');
}
} else
$this->tpls[$this->tpl]['buffer'] .= $s.($ln == true ? "\n" : '');
function _straightOut($s) {
if (!is_subclass_of($this, 'TCPDF')) {
if($this->state==2)
$this->pages[$this->page] .= $s;
else
$this->buffer .= $s;
} else {
$this->buffer.=$s.($ln == true ? "\n" : '');
if ($this->state == 2) {
if (isset($this->footerlen[$this->page]) AND ($this->footerlen[$this->page] > 0)) {
// puts data before page footer
$page = substr($this->getPageBuffer($this->page), 0, -$this->footerlen[$this->page]);
$footer = substr($this->getPageBuffer($this->page), -$this->footerlen[$this->page]);
$this->setPageBuffer($this->page, $page.' '.$s."\n".$footer);
} else {
$this->setPageBuffer($this->page, $s, true);
}
} else {
$this->setBuffer($s);
}
}
}
@ -496,5 +501,4 @@ class FPDI extends FPDF_TPL {
}
return false;
}
}

View File

@ -1,8 +1,8 @@
<?php
//
// FPDI - Version 1.2.1
// FPDI - Version 1.3.2
//
// Copyright 2004-2008 Setasign - Jan Slabon
// Copyright 2004-2010 Setasign - Jan Slabon
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@ -28,21 +28,12 @@
*/
class FPDF extends TCPDF {
/**
* Missing in TCPDF
*
* @var string
*/
var $padding;
function __get($name) {
switch ($name) {
case 'PDFVersion':
return $this->PDFVersion;
case 'k':
return $this->k;
case 'lastUsedPageBox':
return $this->lastUsedPageBox;
default:
// Error handling
$this->Error('Cannot access protected property '.get_class($this).':$'.$name.' / Undefined property: '.get_class($this).'::$'.$name);
@ -100,16 +91,62 @@ class FPDF extends TCPDF {
* @return string
*/
function _unescape($s) {
return strtr($s, array(
'\\\\' => "\\",
'\)' => ')',
'\(' => '(',
'\\f' => chr(0x0C),
'\\b' => chr(0x08),
'\\t' => chr(0x09),
'\\r' => chr(0x0D),
'\\n' => chr(0x0A),
));
$out = '';
for ($count = 0, $n = strlen($s); $count < $n; $count++) {
if ($s[$count] != '\\' || $count == $n-1) {
$out .= $s[$count];
} else {
switch ($s[++$count]) {
case ')':
case '(':
case '\\':
$out .= $s[$count];
break;
case 'f':
$out .= chr(0x0C);
break;
case 'b':
$out .= chr(0x08);
break;
case 't':
$out .= chr(0x09);
break;
case 'r':
$out .= chr(0x0D);
break;
case 'n':
$out .= chr(0x0A);
break;
case "\r":
if ($count != $n-1 && $s[$count+1] == "\n")
$count++;
break;
case "\n":
break;
default:
// Octal-Values
if (ord($s[$count]) >= ord('0') &&
ord($s[$count]) <= ord('9')) {
$oct = ''. $s[$count];
if (ord($s[$count+1]) >= ord('0') &&
ord($s[$count+1]) <= ord('9')) {
$oct .= $s[++$count];
if (ord($s[$count+1]) >= ord('0') &&
ord($s[$count+1]) <= ord('9')) {
$oct .= $s[++$count];
}
}
$out .= chr(octdec($oct));
} else {
$out .= $s[$count];
}
}
}
}
return $out;
}
/**
@ -119,7 +156,7 @@ class FPDF extends TCPDF {
* @return string
*/
function hex2str($hex) {
return pack("H*", str_replace(array("\r", "\n", " "), "", $hex));
return pack('H*', str_replace(array("\r", "\n", ' '), '', $hex));
}
/**
@ -129,6 +166,6 @@ class FPDF extends TCPDF {
* @return string
*/
function str2hex($str) {
return current(unpack("H*", $str));
return current(unpack('H*', $str));
}
}

View File

@ -1,8 +1,8 @@
<?php
//
// FPDI - Version 1.2.1
// FPDI - Version 1.3.2
//
// Copyright 2004-2008 Setasign - Jan Slabon
// Copyright 2004-2010 Setasign - Jan Slabon
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@ -17,7 +17,7 @@
// limitations under the License.
//
require_once("pdf_parser.php");
require_once('pdf_parser.php');
class fpdi_pdf_parser extends pdf_parser {
@ -58,7 +58,7 @@ class fpdi_pdf_parser extends pdf_parser {
*
* @var array
*/
var $availableBoxes = array("/MediaBox","/CropBox","/BleedBox","/TrimBox","/ArtBox");
var $availableBoxes = array('/MediaBox', '/CropBox', '/BleedBox', '/TrimBox', '/ArtBox');
/**
* Constructor
@ -66,9 +66,8 @@ class fpdi_pdf_parser extends pdf_parser {
* @param string $filename Source-Filename
* @param object $fpdi Object of type fpdi
*/
function fpdi_pdf_parser($filename,&$fpdi) {
function fpdi_pdf_parser($filename, &$fpdi) {
$this->fpdi =& $fpdi;
$this->filename = $filename;
parent::pdf_parser($filename);
@ -110,7 +109,7 @@ class fpdi_pdf_parser extends pdf_parser {
$pageno = ((int) $pageno) - 1;
if ($pageno < 0 || $pageno >= $this->getPageCount()) {
$this->fpdi->error("Pagenumber is wrong!");
$this->fpdi->error('Pagenumber is wrong!');
}
$this->pageno = $pageno;
@ -163,7 +162,7 @@ class fpdi_pdf_parser extends pdf_parser {
* @return string
*/
function getContent() {
$buffer = "";
$buffer = '';
if (isset($this->pages[$this->pageno][1][1]['/Contents'])) {
$contents = $this->_getPageContent($this->pages[$this->pageno][1][1]['/Contents']);
@ -225,31 +224,31 @@ class fpdi_pdf_parser extends pdf_parser {
foreach ($filters AS $_filter) {
switch ($_filter[1]) {
case "/FlateDecode":
case '/FlateDecode':
if (function_exists('gzuncompress')) {
$stream = (strlen($stream) > 0) ? @gzuncompress($stream) : '';
} else {
$this->fpdi->error(sprintf("To handle %s filter, please compile php with zlib support.",$_filter[1]));
$this->error(sprintf('To handle %s filter, please compile php with zlib support.',$_filter[1]));
}
if ($stream === false) {
$this->fpdi->error("Error while decompressing stream.");
$this->error('Error while decompressing stream.');
}
break;
case '/LZWDecode':
include_once('filters/FilterLZW_FPDI.php');
$decoder = new FilterLZW_FPDI($this->fpdi);
$stream = $decoder->decode($stream);
break;
case '/ASCII85Decode':
include_once('filters/FilterASCII85_FPDI.php');
$decoder = new FilterASCII85_FPDI($this->fpdi);
$stream = $decoder->decode($stream);
break;
case null:
$stream = $stream;
break;
default:
if (preg_match("/^\/[a-z85]*$/i", $_filter[1], $filterName) && @include_once('decoders'.$_filter[1].'.php')) {
$filterName = substr($_filter[1],1);
if (class_exists($filterName)) {
$decoder = new $filterName($this->fpdi);
$stream = $decoder->decode(trim($stream));
} else {
$this->fpdi->error(sprintf("Unsupported Filter: %s",$_filter[1]));
}
} else {
$this->fpdi->error(sprintf("Unsupported Filter: %s",$_filter[1]));
}
$this->error(sprintf('Unsupported Filter: %s',$_filter[1]));
}
}
@ -278,10 +277,15 @@ class fpdi_pdf_parser extends pdf_parser {
if (!is_null($box) && $box[0] == PDF_TYPE_ARRAY) {
$b =& $box[1];
return array("x" => $b[0][1]/$this->fpdi->k,
"y" => $b[1][1]/$this->fpdi->k,
"w" => abs($b[0][1]-$b[2][1])/$this->fpdi->k,
"h" => abs($b[1][1]-$b[3][1])/$this->fpdi->k);
return array('x' => $b[0][1]/$this->fpdi->k,
'y' => $b[1][1]/$this->fpdi->k,
'w' => abs($b[0][1]-$b[2][1])/$this->fpdi->k,
'h' => abs($b[1][1]-$b[3][1])/$this->fpdi->k,
'llx' => min($b[0][1], $b[2][1])/$this->fpdi->k,
'lly' => min($b[1][1], $b[3][1])/$this->fpdi->k,
'urx' => max($b[0][1], $b[2][1])/$this->fpdi->k,
'ury' => max($b[1][1], $b[3][1])/$this->fpdi->k,
);
} else if (!isset ($page[1][1]['/Parent'])) {
return false;
} else {
@ -349,11 +353,18 @@ class fpdi_pdf_parser extends pdf_parser {
*/
function read_pages (&$c, &$pages, &$result) {
// Get the kids dictionary
$kids = $this->pdf_resolve_object ($c, $pages[1][1]['/Kids']);
$_kids = $this->pdf_resolve_object ($c, $pages[1][1]['/Kids']);
if (!is_array($kids))
$this->fpdi->Error("Cannot find /Kids in current /Page-Dictionary");
foreach ($kids[1] as $v) {
if (!is_array($_kids))
$this->error('Cannot find /Kids in current /Page-Dictionary');
if ($_kids[1][0] == PDF_TYPE_ARRAY) {
$kids = $_kids[1][1];
} else {
$kids = $_kids[1];
}
foreach ($kids as $v) {
$pg = $this->pdf_resolve_object ($c, $v);
if ($pg[1][1]['/Type'][1] === '/Pages') {
// If one of the kids is an embedded

View File

@ -1,8 +1,8 @@
<?php
/****************************************************************************
* Software: FPDI_Protection *
* Version: 1.0.2 *
* Date: 2006/11/20 *
* Version: 1.0.3 *
* Date: 2009/03/06 *
* Author: Klemen VODOPIVEC, Jan Slabon *
* License: Freeware *
* *
@ -17,19 +17,21 @@
* *
****************************************************************************/
require_once("fpdi.php");
require_once('fpdi.php');
class FPDI_Protection extends FPDI {
var $encrypted; //whether document is protected
var $Uvalue; //U entry in pdf document
var $Ovalue; //O entry in pdf document
var $Pvalue; //P entry in pdf document
var $enc_obj_id; //encryption object id
var $last_rc4_key; //last RC4 key encrypted (cached for optimisation)
var $last_rc4_key_c; //last RC4 computed key
var $padding = '';
var $encrypted = false; //whether document is protected
var $Uvalue; //U entry in pdf document
var $Ovalue; //O entry in pdf document
var $Pvalue; //P entry in pdf document
var $enc_obj_id; //encryption object id
var $last_rc4_key = ''; //last RC4 key encrypted (cached for optimisation)
var $last_rc4_key_c; //last RC4 computed key
var $padding = "\x28\xBF\x4E\x5E\x4E\x75\x8A\x41\x64\x00\x4E\x56\xFF\xFA\x01\x08\x2E\x2E\x00\xB6\xD0\x68\x3E\x80\x2F\x0C\xA9\xFE\x64\x53\x69\x7A";
// DOL_CHANGE
/*
function FPDI_Protection($orientation='P',$unit='mm',$format='A4')
{
parent::FPDI($orientation,$unit,$format);
@ -40,6 +42,7 @@ class FPDI_Protection extends FPDI {
$this->padding = "\x28\xBF\x4E\x5E\x4E\x75\x8A\x41\x64\x00\x4E\x56\xFF\xFA\x01\x08".
"\x2E\x2E\x00\xB6\xD0\x68\x3E\x80\x2F\x0C\xA9\xFE\x64\x53\x69\x7A";
}
*/
/**
* Function to set permissions as well as user and owner passwords
@ -52,8 +55,7 @@ class FPDI_Protection extends FPDI {
* - If an owner password is set, document can be opened in privilege mode with no
* restriction if that password is entered
*/
function SetProtection($permissions=array(),$user_pass='',$owner_pass=null)
{
function SetProtection($permissions=array(), $user_pass='', $owner_pass=null) {
$options = array('print' => 4, 'modify' => 8, 'copy' => 16, 'annot-forms' => 32 );
$protection = 192;
foreach($permissions as $permission){
@ -62,14 +64,13 @@ class FPDI_Protection extends FPDI {
$protection += $options[$permission];
}
if ($owner_pass === null)
$owner_pass = uniqid(mt_rand());
$owner_pass = uniqid(rand());
$this->encrypted = true;
$this->_generateencryptionkey($user_pass, $owner_pass, $protection);
}
function _putstream($s)
{
function _putstream($s) {
if ($this->encrypted) {
$s = $this->_RC4($this->_objectkey($this->_current_obj_id), $s);
}
@ -77,8 +78,7 @@ class FPDI_Protection extends FPDI {
}
function _textstring($s)
{
function _textstring($s) {
if ($this->encrypted) {
$s = $this->_RC4($this->_objectkey($this->_current_obj_id), $s);
}
@ -89,24 +89,21 @@ class FPDI_Protection extends FPDI {
/**
* Compute key depending on object number where the encrypted data is stored
*/
function _objectkey($n)
{
return substr($this->_md5_16($this->encryption_key.pack('VXxx',$n)),0,10);
function _objectkey($n) {
return substr($this->_md5_16($this->encryption_key.pack('VXxx', $n)), 0, 10);
}
/**
* Escape special characters
*/
function _escape($s)
{
function _escape($s) {
return str_replace(
array('\\',')','(',"\r"),
array('\\\\','\\)','\\(','\\r'),$s);
array('\\',')','(',"\r", "\n", "\t"),
array('\\\\','\\)','\\(','\\r', '\\n', '\\t'),$s);
}
function _putresources()
{
function _putresources() {
parent::_putresources();
if ($this->encrypted) {
$this->_newobj();
@ -117,8 +114,7 @@ class FPDI_Protection extends FPDI {
}
}
function _putencryption()
{
function _putencryption() {
$this->_out('/Filter /Standard');
$this->_out('/V 1');
$this->_out('/R 2');
@ -128,13 +124,11 @@ class FPDI_Protection extends FPDI {
}
function _puttrailer()
{
function _puttrailer() {
parent::_puttrailer();
if ($this->encrypted) {
$this->_out('/Encrypt '.$this->enc_obj_id.' 0 R');
$id = isset($this->fileidentifier) ? $this->fileidentifier : '';
$this->_out('/ID [<'.$id.'><'.$id.'>]');
$this->_out('/ID [()()]');
}
}
@ -142,8 +136,7 @@ class FPDI_Protection extends FPDI {
/**
* RC4 is the standard encryption algorithm used in PDF format
*/
function _RC4($key, $text)
{
function _RC4($key, $text) {
if ($this->last_rc4_key != $key) {
$k = str_repeat($key, 256/strlen($key)+1);
$rc4 = range(0,255);
@ -181,16 +174,14 @@ class FPDI_Protection extends FPDI {
/**
* Get MD5 as binary string
*/
function _md5_16($string)
{
function _md5_16($string) {
return pack('H*',md5($string));
}
/**
* Compute O value
*/
function _Ovalue($user_pass, $owner_pass)
{
function _Ovalue($user_pass, $owner_pass) {
$tmp = $this->_md5_16($owner_pass);
$owner_RC4_key = substr($tmp,0,5);
return $this->_RC4($owner_RC4_key, $user_pass);
@ -200,8 +191,7 @@ class FPDI_Protection extends FPDI {
/**
* Compute U value
*/
function _Uvalue()
{
function _Uvalue() {
return $this->_RC4($this->encryption_key, $this->padding);
}
@ -209,8 +199,7 @@ class FPDI_Protection extends FPDI {
/**
* Compute encryption key
*/
function _generateencryptionkey($user_pass, $owner_pass, $protection)
{
function _generateencryptionkey($user_pass, $owner_pass, $protection) {
// Pad passwords
$user_pass = substr($user_pass.$this->padding,0,32);
$owner_pass = substr($owner_pass.$this->padding,0,32);
@ -230,6 +219,7 @@ class FPDI_Protection extends FPDI {
switch ($value[0]) {
case PDF_TYPE_STRING :
if ($this->encrypted) {
$value[1] = $this->_unescape($value[1]);
$value[1] = $this->_RC4($this->_objectkey($this->_current_obj_id), $value[1]);
$value[1] = $this->_escape($value[1]);
}
@ -258,12 +248,72 @@ class FPDI_Protection extends FPDI {
function hex2str($hex) {
return pack("H*", str_replace(array("\r","\n"," "),"", $hex));
return pack('H*', str_replace(array("\r","\n",' '),'', $hex));
}
function str2hex($str) {
return current(unpack("H*",$str));
return current(unpack('H*',$str));
}
/**
* Deescape special characters
*/
function _unescape($s) {
$out = '';
for ($count = 0, $n = strlen($s); $count < $n; $count++) {
if ($s[$count] != '\\' || $count == $n-1) {
$out .= $s[$count];
} else {
switch ($s[++$count]) {
case ')':
case '(':
case '\\':
$out .= $s[$count];
break;
case 'f':
$out .= chr(0x0C);
break;
case 'b':
$out .= chr(0x08);
break;
case 't':
$out .= chr(0x09);
break;
case 'r':
$out .= chr(0x0D);
break;
case 'n':
$out .= chr(0x0A);
break;
case "\r":
if ($count != $n-1 && $s[$count+1] == "\n")
$count++;
break;
case "\n":
break;
default:
// Octal-Values
if (ord($s[$count]) >= ord('0') &&
ord($s[$count]) <= ord('9')) {
$oct = ''. $s[$count];
if (ord($s[$count+1]) >= ord('0') &&
ord($s[$count+1]) <= ord('9')) {
$oct .= $s[++$count];
if (ord($s[$count+1]) >= ord('0') &&
ord($s[$count+1]) <= ord('9')) {
$oct .= $s[++$count];
}
}
$out .= chr(octdec($oct));
} else {
$out .= $s[$count];
}
}
}
}
return $out;
}
}
?>

View File

@ -1,8 +1,8 @@
<?php
//
// FPDI - Version 1.2.1
// FPDI - Version 1.3.2
//
// Copyright 2004-2008 Setasign - Jan Slabon
// Copyright 2004-2010 Setasign - Jan Slabon
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@ -17,66 +17,87 @@
// limitations under the License.
//
class pdf_context {
$__tmp = version_compare(phpversion(), "5") == -1 ? array('pdf_context') : array('pdf_context', false);
if (!call_user_func_array('class_exists', $__tmp)) {
var $file;
var $buffer;
var $offset;
var $length;
class pdf_context {
var $stack;
/**
* Modi
*
* @var integer 0 = file | 1 = string
*/
var $_mode = 0;
// Constructor
var $file;
var $buffer;
var $offset;
var $length;
function pdf_context($f) {
$this->file = $f;
$this->reset();
}
var $stack;
// Optionally move the file
// pointer to a new location
// and reset the buffered data
// Constructor
function reset($pos = null, $l = 100) {
if (!is_null ($pos)) {
fseek ($this->file, $pos);
}
function pdf_context(&$f) {
$this->file =& $f;
if (is_string($this->file))
$this->_mode = 1;
$this->reset();
}
$this->buffer = $l > 0 ? fread($this->file, $l) : '';
$this->length = strlen($this->buffer);
if ($this->length < $l)
$this->increase_length($l - $this->length);
$this->offset = 0;
$this->stack = array();
}
// Optionally move the file
// pointer to a new location
// and reset the buffered data
// Make sure that there is at least one
// character beyond the current offset in
// the buffer to prevent the tokenizer
// from attempting to access data that does
// not exist
function reset($pos = null, $l = 100) {
if ($this->_mode == 0) {
if (!is_null ($pos)) {
fseek ($this->file, $pos);
}
function ensure_content() {
if ($this->offset >= $this->length - 1) {
return $this->increase_length();
} else {
return true;
}
}
$this->buffer = $l > 0 ? fread($this->file, $l) : '';
$this->length = strlen($this->buffer);
if ($this->length < $l)
$this->increase_length($l - $this->length);
} else {
$this->buffer = $this->file;
$this->length = strlen($this->buffer);
}
$this->offset = 0;
$this->stack = array();
}
// Forcefully read more data into the buffer
// Make sure that there is at least one
// character beyond the current offset in
// the buffer to prevent the tokenizer
// from attempting to access data that does
// not exist
function increase_length($l=100) {
if (feof($this->file)) {
return false;
} else {
$totalLength = $this->length + $l;
do {
$this->buffer .= fread($this->file, $totalLength-$this->length);
} while ((($this->length = strlen($this->buffer)) != $totalLength) && !feof($this->file));
function ensure_content() {
if ($this->offset >= $this->length - 1) {
return $this->increase_length();
} else {
return true;
}
}
return true;
}
}
// Forcefully read more data into the buffer
function increase_length($l=100) {
if ($this->_mode == 0 && feof($this->file)) {
return false;
} else if ($this->_mode == 0) {
$totalLength = $this->length + $l;
do {
$this->buffer .= fread($this->file, $totalLength-$this->length);
} while ((($this->length = strlen($this->buffer)) != $totalLength) && !feof($this->file));
return true;
} else {
return false;
}
}
}
}
unset($__tmp);

File diff suppressed because it is too large Load Diff