OpenDocumentPHP
[ class tree: OpenDocumentPHP ] [ index: OpenDocumentPHP ] [ all elements ]

Source for file OpenDocumentArchive.php

Documentation is available at OpenDocumentArchive.php

  1. <?php
  2. /*
  3.  * Created on 29.12.2006 by Norman Markgraf (nmarkgraf(at)user.sourceforge.net)
  4.  *
  5.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6.  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8.  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9.  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11.  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15.  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16.  *
  17.  * This software consists of voluntary contributions made by many individuals
  18.  * and is licensed under the GPL. For more information please see
  19.  * <http://opendocumentphp.org>.
  20.  * 
  21.  * $Id: OpenDocumentArchive.php 182 2007-06-11 13:32:34Z nmarkgraf $
  22.  */
  23. // Check for useable PHP version:
  24. if (version_compare(PHP_VERSION"5.2.0"0{
  25.     die("You need at least PHP 5.2.0 to use OpenDocumentArchive!");
  26. }
  27. require_once 'OpenDocumentPHP/manifest/ManifestDocument.php';
  28. /**
  29.  * OpenDocumentArchive class.
  30.  * 
  31.  * This is a extension of the class ZipArchive to handle OpenDocumentArchives.
  32.  * That is a file that, like jar archives in java, have meta informations.
  33.  * Unlike in jar files, this meta informations are stored in a XML file named
  34.  * 'manifest.xml' in the 'META-INF' directory of the ZIP archive.
  35.  * 
  36.  * This class will handle everything needed.
  37.  * 
  38.  * YOU NEED AT LEAST PHP 5.2.0 !!!
  39.  * 
  40.  * $Id: OpenDocumentArchive.php 182 2007-06-11 13:32:34Z nmarkgraf $
  41.  * 
  42.  * @author         Norman Markgraf (nmarkgraf(at)user.sourceforge.net)
  43.  * @copyright     Copyright in 2006, 2007 by The OpenDocumentPHP Team
  44.  * @license     http://www.gnu.org/licenses/gpl.html GNU Public License 2.0 or above.
  45.  * @version        $Revision: 182 $
  46.  * @package        OpenDocumentPHP
  47.  * @since         0.5.0
  48.  */
  49. class OpenDocumentArchive extends ZipArchive {
  50.     /**
  51.      * Full path to manifest xml in the OpenDocument archive.
  52.      */
  53.     const PathToManifestXml = 'META-INF/manifest.xml';
  54.     /**
  55.      * No Manifest was found in Archive that where opened.
  56.      */
  57.     const NOMANIFEST = 1024;
  58.     /**
  59.      * The OpenDocument Manifest as DOM document.
  60.      * 
  61.      * @var ManifestDocument 
  62.      * @access private
  63.      */
  64.     private $manifest;
  65.     /**
  66.      * Constructor method.
  67.      * 
  68.      * @param         string mimetype Mime type of the achrive
  69.      * @since         0.5.0 - 08.02.2007
  70.      */
  71.     function __construct($mimetype ''{
  72.         $this->manifest new ManifestDocument($mimetype);
  73.     }
  74.     /**
  75.      * Adds a file to a ZIP archive from the given path.
  76.      * 
  77.      * @param         string localname The name of the entry to create.
  78.      * @param         string filename The path to the file to add.
  79.      * @param         string mimetype Mime type of the file.
  80.      * @access         public
  81.      * @since         0.5.0 - 08.02.2007
  82.      */
  83.     function addFile($filename$localname ''$mimetype 'text/text'{
  84.         // First add to ZipArchive
  85.         $ret parent :: addFile($filename$localname);
  86.         if ($ret === true{
  87.             // If successfully added to archive make a manifest entry                
  88.             if ($localname === ''{
  89.                 // If $localname not set, use $filename     
  90.                 $localname $filename;
  91.             }
  92.             // Add to manifest
  93.             $this->manifest->addFileEntry($localname$mimetype);
  94.         }
  95.         return $ret;
  96.     }
  97.     /**
  98.      * Add a file to a ZIP archive using its contents.
  99.      * 
  100.      * @param         string localname The name of the entry to create.
  101.      * @param         string contents The contents to use to create the entry. It is used in a binary safe mode.
  102.      * @param         string mimetype Mime type of the file.
  103.      * @access         public
  104.      * @since         0.5.0 - 08.02.2007
  105.      */
  106.     function addFromString($localname$contents$mimetype 'text/text'{
  107.         // First add to ZipArchive              
  108.         $ret parent :: addFromString($localname$contents);
  109.         if ($ret === true{
  110.             // If successfully added to archive make a manifest entry
  111.             $this->manifest->addFileEntry($localname$mimetype);
  112.         }
  113.         return $ret;
  114.     }
  115.     /**
  116.      * Retrieve mime type of the OpenDocument archive.
  117.      * 
  118.      * @return         string Current mime type of the OpenDocument archive.
  119.      * @access         public
  120.      * @since         0.5.0 - 08.02.2007
  121.      */
  122.     function getMimeType({
  123.         return $this->manifest->getMimeType();
  124.     }
  125.     /**
  126.      * Set mime type of the OpenDocument archive.
  127.      * 
  128.      * Therefor we delete the file 'mimetype' in the current OpenDocument archive
  129.      * and add a new file 'mimetype' with the new mime type given as parameter $mimetype.
  130.      * 
  131.      * @param         string $mimetype New mime type of the OpenDocument archive.
  132.      * @access         public
  133.      * @since         0.5.0 - 08.02.2007
  134.      */
  135.     function setMimeType($mimetype{
  136.         // We need to change the file 'mimetype' in the current archive as well:
  137.         // So we first remove the file ...
  138.         parent::deleteName('mimetype');
  139.         // ... and add a new one.
  140.         parent::addFormString('mimetype'$mimetype);
  141.                 
  142.         $this->manifest->setMimeType($mimetype);
  143.     }
  144.     /**
  145.      * Opens a new zip archive for reading, writing or modifying.
  146.      * 
  147.      * If we create a new archive, we add to files. First the manifest document
  148.      * stored in the ManifestDocument and second the file 'mimetype' which only
  149.      * include the
  150.      * 
  151.      * @param         string filename The file name of the ZIP archive to open.
  152.      * @param         int flags The mode to use to open the archive.
  153.      * @return        mixed 
  154.      * @access         public
  155.      * @since         0.5.0 - 08.02.2007
  156.      */
  157.     function open($filename$flags 0$mimetype ''{
  158.         $ret parent :: open($filename$flags);
  159.         if ($ret === true{
  160.             if (($flags && self :: CREATE0{
  161.                 // If archive was freshly made, we need a new ManifestDocument.
  162.                 unset ($this->manifest);
  163.                 $this->manifest new ManifestDocument($mimetype);
  164.                 // Add the file 'mimetype' with the current mimetype
  165.                 if ($mimetype != ''{
  166.                     parent::addFromString('mimetype'$mimetype);
  167.                 }
  168.             else {
  169.                 $mfxml parent :: getFromName(self :: PathToManifestXml);
  170.                 if ($mfxml === false{
  171.                     $ret self :: NOMANIFEST $mfxml;
  172.                 else {
  173.                     $this->manifest->loadXML($mfxml);
  174.                     // echo $this->manifest->saveXML();
  175.                 }
  176.             }
  177.         }
  178.         return $ret;
  179.     }
  180.     /**
  181.      * Delete an entry in the archive using its name.
  182.      * 
  183.      * @param         string name Name of the entry to delete.
  184.      * @access         public
  185.      * @since         0.5.0 - 08.02.2007
  186.      */
  187.     function deleteName($name{
  188.         $ret parent :: deleteName($name);
  189.         if ($ret === true{
  190.             // After the file is delete from the archive we also need to 
  191.             // remove it from the manifest file.
  192.             $ret $this->manifest->removeFileEntry($name);
  193.         }
  194.         return $ret;
  195.     }
  196.     /**
  197.      * Delete an entry in the archive using its index.
  198.      * 
  199.      * @param         int index Index of the entry to delete.
  200.      * @access         public
  201.      * @since         0.5.0 - 08.02.2007
  202.      */
  203.     function deleteIndex($index{
  204.         $ret false;
  205.         $stats parent :: statIndex($index);
  206.         if ($stats !== false{
  207.             $name $stats['name'];
  208.             $ret $this->deleteName($name);
  209.         }
  210.         return $ret;
  211.     }
  212.     /**
  213.      * Renames an entry defined by its index.
  214.      * 
  215.      * @param         int index Index of the entry to rename.
  216.      * @param         string newname New name.
  217.      * @access         public
  218.      * @since         0.5.0 - 08.02.2007
  219.      */
  220.     function renameIndex($index$newname{
  221.         $ret false;
  222.         $stats parent :: statIndex($index);
  223.         if ($stats !== false{
  224.             $name $stats['name'];
  225.             $ret $this->renameName($name$newname);
  226.         }
  227.         return $ret;
  228.     }
  229.     /**
  230.      * Renames an entry defined by its name.
  231.      * 
  232.      * @param         string name Name of the entry to rename.
  233.      * @param         string newname New name.
  234.      * @access         public
  235.      * @since         0.5.0 - 08.02.2007
  236.      */
  237.     function renameName($name$newname{
  238.         $ret parent :: renameName($name$newname);
  239.         if ($ret === true{
  240.             $ret $this->manifest->renameFileEntry($name$newname);
  241.         }
  242.         return $ret;
  243.     }
  244.     /**
  245.      * Get the details of an entry defined by its name.
  246.      * 
  247.      * The function obtains information about the entry defined by its name.
  248.      * 
  249.      * @param         string name Name of the entry.
  250.      * @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.
  251.      * @return         mixed Returns an array containing the entry details or <b>FALSE</b> on failure.
  252.      * @access         public
  253.      * @since         0.5.0 - 08.02.2007
  254.      */
  255.     function statName($name$flags 0{
  256.         $ret false;
  257.         $stat parent :: statName($name$flags);
  258.         if ($stat !== false{
  259.             $ret $stat;
  260.             $tmp $this->manifest->getFilelist();
  261.             foreach ($tmp as $element{
  262.                 if ($element['name'=== $name{
  263.                     $ret array_merge($stat$element);
  264.                     break;
  265.                 }
  266.             }
  267.         }
  268.         /*        echo "\nstat:\n";
  269.                 print_r($ret);
  270.                 echo "\n"; */
  271.         return $ret;
  272.     }
  273.     /**
  274.      * 
  275.      * @access         public
  276.      * @since         0.5.0 - 08.02.2007
  277.      */
  278.     function getFilelist({
  279.         return $this->manifest->getFilelist();
  280.     }
  281.     /**
  282.      * 
  283.      * @access         public
  284.      * @since         0.5.0 - 08.02.2007
  285.      */
  286.     function getDOMFromName($filename{
  287.         $ret new DOMDocument();
  288.         $ret->loadXML(parent :: getFromName($filename));
  289.         return $ret;
  290.     }
  291.     /**
  292.      * 
  293.      * @access         public
  294.      * @since         0.5.0 - 08.02.2007
  295.      */
  296.     function getManifest({
  297.         return $this->manifest;
  298.     }
  299.     /**
  300.      * Revert all changes done to an entry with the given name.
  301.      * Always returns <b>false</b>!
  302.      * 
  303.      * @param         string Name of the entry.
  304.      * @return         boolean Returns <b>true</b> if success, <b>false</b> if not.
  305.      * @access         public
  306.      * @since         0.5.0 - 08.02.2007
  307.      */
  308.     function unchangeName($index{
  309.         return false;
  310.     }
  311.     /**
  312.      * Revert all changes done to an entry at the given index.
  313.      * Always returns <b>false</b>!
  314.      * 
  315.      * @param         int index Index of the entry.
  316.      * @return         boolean Returns <b>true</b> if success, <b>false</b> if not.
  317.      * @access         public
  318.      * @since         0.5.0 - 08.02.2007
  319.      */
  320.     function unchangeIndex($index{
  321.         return false;
  322.     }
  323.     /**
  324.      * Undo all changes done in the archive. Always returns <b>false</b>!
  325.      * 
  326.      * @return         boolean Returns <b>true</b> if success, <b>false</b> if not.
  327.      * @access         public
  328.      * @since         0.5.0 - 08.02.2007
  329.      */
  330.     function unchangeAll({
  331.         return false;
  332.     }
  333.     /**
  334.      * Close opened or created archive and save changes. This method is
  335.      * automatically called at the end of the script.
  336.      * 
  337.      * @access         public
  338.      * @since         0.5.0 - 08.02.2007
  339.      */
  340.     function close({
  341.         // First we normalize the manifest document
  342.         $this->manifest->normalize();
  343.         // Delete old manifest.xml in the archive.
  344.         parent :: deleteName(self :: PathToManifestXml);
  345.         // We have to write the ManifestDocument    
  346.         parent :: addFromString(self :: PathToManifestXml$this->manifest->saveXML());
  347.         // Now we can close the OpenDocumentArchive
  348.         return parent :: close();
  349.     }
  350. }
  351. ?>

Documentation generated on Tue, 12 Jun 2007 10:00:27 +0200 by phpDocumentor 1.3.2