Upgrade EPub meta library

This commit is contained in:
Sébastien Lucas 2014-01-11 20:22:21 +01:00
parent 3e29824a78
commit aac567a731
2 changed files with 100 additions and 24 deletions

View file

@ -3,7 +3,7 @@
* PHP EPub Meta library * PHP EPub Meta library
* *
* @author Andreas Gohr <andi@splitbrain.org> * @author Andreas Gohr <andi@splitbrain.org>
* @author Sébastien Lucas <sebastien@slucas.fr> * @author Sébastien Lucas <sebastien@slucas.fr>
*/ */
require_once(realpath( dirname( __FILE__ ) ) . '/tbszip.php'); require_once(realpath( dirname( __FILE__ ) ) . '/tbszip.php');
@ -161,8 +161,6 @@ class EPub {
} }
$data = $this->zip->FileRead($path); $data = $this->zip->FileRead($path);
$data = preg_replace ("/src=[\"']([\w\/\.]*?)[\"']/", "src='epubfs.php?comp=$1'", $data);
$data = preg_replace ("/href=[\"']([\w\/\.]*?)[\"']/", "href='epubfs.php?comp=$1'", $data);
return $data; return $data;
} }
@ -189,7 +187,6 @@ class EPub {
return $contents; return $contents;
} }
/** /**
* Get or set the book author(s) * Get or set the book author(s)
* *
@ -200,7 +197,7 @@ class EPub {
* *
* array( * array(
* 'Pratchett, Terry' => 'Terry Pratchett', * 'Pratchett, Terry' => 'Terry Pratchett',
* 'Simpson, Jacqeline' => 'Jacqueline Simpson', * 'Simpson, Jacqueline' => 'Jacqueline Simpson',
* ) * )
* *
* @params array $authors * @params array $authors
@ -400,7 +397,7 @@ class EPub {
* @param string $serie * @param string $serie
*/ */
public function Serie($serie=false){ public function Serie($serie=false){
return $this->getset('opf:meta',$serie,'name','calibre:series','content'); return $this->getset('opf:meta',$serie,'name','cops:series','content');
} }
/** /**
@ -409,7 +406,7 @@ class EPub {
* @param string $serieIndex * @param string $serieIndex
*/ */
public function SerieIndex($serieIndex=false){ public function SerieIndex($serieIndex=false){
return $this->getset('opf:meta',$serieIndex,'name','calibre:series_index','content'); return $this->getset('opf:meta',$serieIndex,'name','cops:series_index','content');
} }
/** /**

View file

@ -1,8 +1,8 @@
<?php <?php
/* /*
TbsZip version 2.12 TbsZip version 2.15
Date : 2013-03-16 Date : 2013-10-16
Author : Skrol29 (email: http://www.tinybutstrong.com/onlyyou.html) Author : Skrol29 (email: http://www.tinybutstrong.com/onlyyou.html)
Licence : LGPL Licence : LGPL
This class is independent from any other classes and has been originally created for the OpenTbs plug-in This class is independent from any other classes and has been originally created for the OpenTbs plug-in
@ -42,10 +42,16 @@ class clsTbsZip {
if (!isset($this->Meth8Ok)) $this->__construct(); // for PHP 4 compatibility if (!isset($this->Meth8Ok)) $this->__construct(); // for PHP 4 compatibility
$this->Close(); // close handle and init info $this->Close(); // close handle and init info
$this->Error = false; $this->Error = false;
$this->ArchFile = $ArchFile;
$this->ArchIsNew = false; $this->ArchIsNew = false;
// open the file $this->ArchIsStream = (is_resource($ArchFile) && (get_resource_type($ArchFile)=='stream'));
$this->ArchHnd = fopen($ArchFile, 'rb', $UseIncludePath); if ($this->ArchIsStream) {
$this->ArchFile = 'from_stream.zip';
$this->ArchHnd = $ArchFile;
} else {
// open the file
$this->ArchFile = $ArchFile;
$this->ArchHnd = fopen($ArchFile, 'rb', $UseIncludePath);
}
$ok = !($this->ArchHnd===false); $ok = !($this->ArchHnd===false);
if ($ok) $ok = $this->CentralDirRead(); if ($ok) $ok = $this->CentralDirRead();
return $ok; return $ok;
@ -90,9 +96,18 @@ class clsTbsZip {
$cd_pos = -22; $cd_pos = -22;
$this->_MoveTo($cd_pos, SEEK_END); $this->_MoveTo($cd_pos, SEEK_END);
$b = $this->_ReadData(4); $b = $this->_ReadData(4);
if ($b!==$cd_info) return $this->RaiseError('The End of Central Rirectory Record is not found.'); if ($b===$cd_info) {
$this->CdEndPos = ftell($this->ArchHnd) - 4;
$this->CdEndPos = ftell($this->ArchHnd) - 4; } else {
$p = $this->_FindCDEnd($cd_info);
//echo 'p='.var_export($p,true); exit;
if ($p===false) {
return $this->RaiseError('The End of Central Directory Record is not found.');
} else {
$this->CdEndPos = $p;
$this->_MoveTo($p+4);
}
}
$this->CdInfo = $this->CentralDirRead_End($cd_info); $this->CdInfo = $this->CentralDirRead_End($cd_info);
$this->CdFileLst = array(); $this->CdFileLst = array();
$this->CdFileNbr = $this->CdInfo['file_nbr_curr']; $this->CdFileNbr = $this->CdInfo['file_nbr_curr'];
@ -161,7 +176,13 @@ class clsTbsZip {
} }
function RaiseError($Msg) { function RaiseError($Msg) {
if ($this->DisplayError) echo '<strong>'.get_class($this).' ERROR :</strong> '.$Msg.'<br>'."\r\n"; if ($this->DisplayError) {
if (PHP_SAPI==='cli') {
echo get_class($this).' ERROR with the zip archive: '.$Msg."\r\n";
} else {
echo '<strong>'.get_class($this).' ERROR with the zip archive:</strong> '.$Msg.'<br>'."\r\n";
}
}
$this->Error = $Msg; $this->Error = $Msg;
return false; return false;
} }
@ -182,10 +203,10 @@ class clsTbsZip {
$idx++; $idx++;
} }
} }
$nl = "\r\n"; $nl = "\r\n";
echo "<pre>"; echo "<pre>";
echo "-------------------------------".$nl; echo "-------------------------------".$nl;
echo "End of Central Directory record".$nl; echo "End of Central Directory record".$nl;
echo "-------------------------------".$nl; echo "-------------------------------".$nl;
@ -206,7 +227,7 @@ class clsTbsZip {
} }
echo "</pre>"; echo "</pre>";
} }
function DebugArray($arr) { function DebugArray($arr) {
@ -219,7 +240,7 @@ class clsTbsZip {
} }
return $arr; return $arr;
} }
function FileExists($NameOrIdx) { function FileExists($NameOrIdx) {
return ($this->FileGetIdx($NameOrIdx)!==false); return ($this->FileGetIdx($NameOrIdx)!==false);
} }
@ -293,7 +314,7 @@ class clsTbsZip {
// read the file header (and maybe the data ) in the archive, assuming the cursor in at a new file position // read the file header (and maybe the data ) in the archive, assuming the cursor in at a new file position
$b = $this->_ReadData(30); $b = $this->_ReadData(30);
$x = $this->_GetHex($b,0,4); $x = $this->_GetHex($b,0,4);
if ($x!=='h:04034b50') return $this->RaiseError("Signature of Local File Header #".$idx." (data section) expected but not found at position ".$this->_TxtPos(ftell($this->ArchHnd)-30)."."); if ($x!=='h:04034b50') return $this->RaiseError("Signature of Local File Header #".$idx." (data section) expected but not found at position ".$this->_TxtPos(ftell($this->ArchHnd)-30).".");
@ -335,7 +356,7 @@ class clsTbsZip {
} else { } else {
$this->_MoveTo($len, SEEK_CUR); $this->_MoveTo($len, SEEK_CUR);
} }
// Description information // Description information
$desc_ok = ($x['purp'][2+3]=='1'); $desc_ok = ($x['purp'][2+3]=='1');
if ($desc_ok) { if ($desc_ok) {
@ -395,6 +416,32 @@ class clsTbsZip {
} }
/**
* Return the state of the file.
* @return {string} 'u'=unchanged, 'm'=modified, 'd'=deleted, 'a'=added, false=unknown
*/
function FileGetState($NameOrIdx) {
$idx = $this->FileGetIdx($NameOrIdx);
if ($idx===false) {
$idx = $this->FileGetIdxAdd($NameOrIdx);
if ($idx===false) {
return false;
} else {
return 'a';
}
} elseif (isset($this->ReplInfo[$idx])) {
if ($this->ReplInfo[$idx]===false) {
return 'd';
} else {
return 'm';
}
} else {
return 'u';
}
}
function FileCancelModif($NameOrIdx, $ReplacedAndDeleted=true) { function FileCancelModif($NameOrIdx, $ReplacedAndDeleted=true) {
// cancel added, modified or deleted modifications on a file in the archive // cancel added, modified or deleted modifications on a file in the archive
// return the number of cancels // return the number of cancels
@ -585,8 +632,7 @@ class clsTbsZip {
if (''.$File=='') $File = basename($this->ArchFile).'.zip'; if (''.$File=='') $File = basename($this->ArchFile).'.zip';
$this->OutputHandle = @fopen($File, 'w'); $this->OutputHandle = @fopen($File, 'w');
if ($this->OutputHandle===false) { if ($this->OutputHandle===false) {
$this->RaiseError('Method Flush() cannot overwrite the target file \''.$File.'\'. This may not be a valid file path or the file may be locked by another process or because of a denied permission.'); return $this->RaiseError('Method Flush() cannot overwrite the target file \''.$File.'\'. This may not be a valid file path or the file may be locked by another process or because of a denied permission.');
return false;
} }
} elseif (($Render & TBSZIP_STRING)==TBSZIP_STRING) { } elseif (($Render & TBSZIP_STRING)==TBSZIP_STRING) {
$this->OutputMode = TBSZIP_STRING; $this->OutputMode = TBSZIP_STRING;
@ -608,6 +654,8 @@ class clsTbsZip {
$Len = $this->_EstimateNewArchSize(); $Len = $this->_EstimateNewArchSize();
if ($Len!==false) header('Content-Length: '.$Len); if ($Len!==false) header('Content-Length: '.$Len);
} }
} else {
return $this->RaiseError('Method Flush is called with a unsupported render option.');
} }
return true; return true;
@ -748,6 +796,34 @@ class clsTbsZip {
// Return the human readable position in both decimal and hexa // Return the human readable position in both decimal and hexa
return $pos." (h:".dechex($pos).")"; return $pos." (h:".dechex($pos).")";
} }
/**
* Search the record of end of the Central Directory.
* Return the position of the record in the file.
* Return false if the record is not found. The comment cannot exceed 65335 bytes (=FFFF).
* The method is read backwards a block of 256 bytes and search the key in this block.
*/
function _FindCDEnd($cd_info) {
$nbr = 1;
$p = false;
$pos = ftell($this->ArchHnd) - 4 - 256;
while ( ($p===false) && ($nbr<256) ) {
if ($pos<=0) {
$pos = 0;
$nbr = 256; // in order to make this a last check
}
$this->_MoveTo($pos);
$x = $this->_ReadData(256);
$p = strpos($x, $cd_info);
if ($p===false) {
$nbr++;
$pos = $pos - 256 - 256;
} else {
return $pos + $p;
}
}
return false;
}
function _DataOuputAddedFile($Idx, $PosLoc) { function _DataOuputAddedFile($Idx, $PosLoc) {
@ -880,6 +956,9 @@ class clsTbsZip {
if ($this->ArchIsNew) { if ($this->ArchIsNew) {
$Len = strlen($this->CdInfo['bin']); $Len = strlen($this->CdInfo['bin']);
} elseif ($this->ArchIsStream) {
$x = fstat($this->ArchHnd);
$Len = $x['size'];
} else { } else {
$Len = filesize($this->ArchFile); $Len = filesize($this->ArchFile);
} }