Source for file OpenDocumentArchive.php
Documentation is available at OpenDocumentArchive.php
* Created on 29.12.2006 by Norman Markgraf (nmarkgraf(at)user.sourceforge.net)
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* This software consists of voluntary contributions made by many individuals
* and is licensed under the GPL. For more information please see
* <http://opendocumentphp.org>.
* $Id: OpenDocumentArchive.php 182 2007-06-11 13:32:34Z nmarkgraf $
// Check for useable PHP version:
die("You need at least PHP 5.2.0 to use OpenDocumentArchive!");
require_once 'OpenDocumentPHP/manifest/ManifestDocument.php';
* OpenDocumentArchive class.
* This is a extension of the class ZipArchive to handle OpenDocumentArchives.
* That is a file that, like jar archives in java, have meta informations.
* Unlike in jar files, this meta informations are stored in a XML file named
* 'manifest.xml' in the 'META-INF' directory of the ZIP archive.
* This class will handle everything needed.
* YOU NEED AT LEAST PHP 5.2.0 !!!
* $Id: OpenDocumentArchive.php 182 2007-06-11 13:32:34Z nmarkgraf $
* @author Norman Markgraf (nmarkgraf(at)user.sourceforge.net)
* @copyright Copyright in 2006, 2007 by The OpenDocumentPHP Team
* @license http://www.gnu.org/licenses/gpl.html GNU Public License 2.0 or above.
* @version $Revision: 182 $
* @package OpenDocumentPHP
* Full path to manifest xml in the OpenDocument archive.
* No Manifest was found in Archive that where opened.
* The OpenDocument Manifest as DOM document.
* @param string mimetype Mime type of the achrive
* @since 0.5.0 - 08.02.2007
* Adds a file to a ZIP archive from the given path.
* @param string localname The name of the entry to create.
* @param string filename The path to the file to add.
* @param string mimetype Mime type of the file.
* @since 0.5.0 - 08.02.2007
function addFile($filename, $localname = '', $mimetype = 'text/text') {
// First add to ZipArchive
$ret = parent :: addFile($filename, $localname);
// If successfully added to archive make a manifest entry
// If $localname not set, use $filename
* Add a file to a ZIP archive using its contents.
* @param string localname The name of the entry to create.
* @param string contents The contents to use to create the entry. It is used in a binary safe mode.
* @param string mimetype Mime type of the file.
* @since 0.5.0 - 08.02.2007
function addFromString($localname, $contents, $mimetype = 'text/text') {
// First add to ZipArchive
$ret = parent :: addFromString($localname, $contents);
// If successfully added to archive make a manifest entry
* Retrieve mime type of the OpenDocument archive.
* @return string Current mime type of the OpenDocument archive.
* @since 0.5.0 - 08.02.2007
* Set mime type of the OpenDocument archive.
* Therefor we delete the file 'mimetype' in the current OpenDocument archive
* and add a new file 'mimetype' with the new mime type given as parameter $mimetype.
* @param string $mimetype New mime type of the OpenDocument archive.
* @since 0.5.0 - 08.02.2007
// We need to change the file 'mimetype' in the current archive as well:
// So we first remove the file ...
parent::deleteName('mimetype');
// ... and add a new one.
parent::addFormString('mimetype', $mimetype);
* Opens a new zip archive for reading, writing or modifying.
* If we create a new archive, we add to files. First the manifest document
* stored in the ManifestDocument and second the file 'mimetype' which only
* @param string filename The file name of the ZIP archive to open.
* @param int flags The mode to use to open the archive.
* @since 0.5.0 - 08.02.2007
function open($filename, $flags = 0, $mimetype = '') {
$ret = parent :: open($filename, $flags);
if (($flags && self :: CREATE) > 0) {
// If archive was freshly made, we need a new ManifestDocument.
// Add the file 'mimetype' with the current mimetype
parent::addFromString('mimetype', $mimetype);
$mfxml = parent :: getFromName(self :: PathToManifestXml);
$ret = self :: NOMANIFEST + $mfxml;
// echo $this->manifest->saveXML();
* Delete an entry in the archive using its name.
* @param string name Name of the entry to delete.
* @since 0.5.0 - 08.02.2007
$ret = parent :: deleteName($name);
// After the file is delete from the archive we also need to
// remove it from the manifest file.
* Delete an entry in the archive using its index.
* @param int index Index of the entry to delete.
* @since 0.5.0 - 08.02.2007
$stats = parent :: statIndex($index);
* Renames an entry defined by its index.
* @param int index Index of the entry to rename.
* @param string newname New name.
* @since 0.5.0 - 08.02.2007
$stats = parent :: statIndex($index);
* Renames an entry defined by its name.
* @param string name Name of the entry to rename.
* @param string newname New name.
* @since 0.5.0 - 08.02.2007
$ret = parent :: renameName($name, $newname);
* Get the details of an entry defined by its name.
* The function obtains information about the entry defined by its name.
* @param string name Name of the entry.
* @param int flags The flags argument specifies how the name lookup should be done. Also, ZIPARCHIVE::FL_UNCHANGED may be ORed to it to request information about the original file in the archive, ignoring any changes made.
* @return mixed Returns an array containing the entry details or <b>FALSE</b> on failure.
* @since 0.5.0 - 08.02.2007
$stat = parent :: statName($name, $flags);
foreach ($tmp as $element) {
if ($element['name'] === $name) {
* @since 0.5.0 - 08.02.2007
* @since 0.5.0 - 08.02.2007
$ret = new DOMDocument();
$ret->loadXML(parent :: getFromName($filename));
* @since 0.5.0 - 08.02.2007
* Revert all changes done to an entry with the given name.
* Always returns <b>false</b>!
* @param string Name of the entry.
* @return boolean Returns <b>true</b> if success, <b>false</b> if not.
* @since 0.5.0 - 08.02.2007
* Revert all changes done to an entry at the given index.
* Always returns <b>false</b>!
* @param int index Index of the entry.
* @return boolean Returns <b>true</b> if success, <b>false</b> if not.
* @since 0.5.0 - 08.02.2007
* Undo all changes done in the archive. Always returns <b>false</b>!
* @return boolean Returns <b>true</b> if success, <b>false</b> if not.
* @since 0.5.0 - 08.02.2007
* Close opened or created archive and save changes. This method is
* automatically called at the end of the script.
* @since 0.5.0 - 08.02.2007
// First we normalize the manifest document
$this->manifest->normalize();
// Delete old manifest.xml in the archive.
parent :: deleteName(self :: PathToManifestXml);
// We have to write the ManifestDocument
parent :: addFromString(self :: PathToManifestXml, $this->manifest->saveXML());
// Now we can close the OpenDocumentArchive
return parent :: close();
|