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

Source for file OpenDocumentArchive.php

Documentation is available at OpenDocumentArchive.php

  1. <?php
  2.  
  3. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  4.  
  5. /*
  6.  * Created on 29. Dec. 2006 by Norman Markgraf (nmarkgraf(at)user.sourceforge.net)
  7.  */
  8.  
  9. /**
  10.  * OpenDocumentArchive class file.
  11.  * 
  12.  * This is a extension of the class ZipArchive to handle OpenDocumentArchives.
  13.  * That is a file that, like jar archives in java, have meta informations.
  14.  * Unlike in jar files, this meta informations are stored in a XML file named
  15.  * 'manifest.xml' in the 'META-INF' directory of the ZIP archive.
  16.  * 
  17.  * If you have got any problems with the <i>ZipArchive</i> class, please read the
  18.  * information on the class-level documentation first.
  19.  * 
  20.  * PHP versions 5
  21.  *   
  22.  * LICENSE:
  23.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  24.  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  25.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  26.  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  27.  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  28.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  29.  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  30.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  31.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  32.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  33.  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34.  *
  35.  * This software consists of voluntary contributions made by many individuals
  36.  * and is licensed under the GPL. For more information please see
  37.  * <http://opendocumentphp.org>.
  38.  * 
  39.  * $Id: OpenDocumentArchive.php 256 2007-08-01 14:16:10Z nmarkgraf $
  40.  * 
  41.  * @category    File Formats
  42.  * @package        OpenDocumentPHP
  43.  * @author         Norman Markgraf (nmarkgraf(at)user.sourceforge.net)
  44.  * @copyright     Copyright in 2006, 2007 by The OpenDocumentPHP Team
  45.  * @license     http://www.gnu.org/licenses/gpl.html GNU General Public License 2.0.
  46.  * @version        SVN: $Id: OpenDocumentArchive.php 256 2007-08-01 14:16:10Z nmarkgraf $
  47.  * @link           http://opendocumentphp.org
  48.  * @since         0.5.0 - 08. Feb. 2007
  49.  */
  50.  
  51. /*
  52.  * Check for useable PHP version:
  53.  */
  54. if (version_compare(PHP_VERSION"5.2.0"0{
  55.     die("You need at least PHP 5.2.0 to use OpenDocumentArchive!");
  56. }
  57. /*
  58.  * Check if ZipArchive is installed properly
  59.  */
  60.  
  61. if (!class_exists('ZipArchive')) {
  62.     die("You need ZipArchive to be enabled. On Linux systems use '--enable-zip' option at compile time. On Windows systems enable 'php_zip.dll' inside of 'php.ini'.");
  63. }
  64.  
  65. /**
  66.  *
  67.  */
  68. require_once 'OpenDocumentPHP/manifest/ManifestDocument.php';
  69.  
  70. /**
  71.  * OpenDocumentArchive class.
  72.  * 
  73.  * This is a extension of the class ZipArchive to handle OpenDocumentArchives.
  74.  * That is a file that, like jar archives in java, have meta informations.
  75.  * Unlike in jar files, this meta informations are stored in a XML file named
  76.  * 'manifest.xml' in the 'META-INF' directory of the ZIP archive.
  77.  * 
  78.  * This class will handle everything needed.
  79.  * 
  80.  * <i>But be aware, that you have at least PHP 5.2.0 and enabled zip support.</i>
  81.  * 
  82.  * <i> Linux systems (general) </i>
  83.  *  
  84.  * In order to use these functions you must compile PHP with zip support by using
  85.  * the <i>--with-zip[=DIR]</i> configure option, where <i>[DIR]</i> is the prefix
  86.  * of the ZZIPlib library install.
  87.  * 
  88.  * <i> Linux systems (debian) </i>
  89.  * 
  90.  * The easiest way is to use <i>apt-get install php5-zip</i> on command line. This
  91.  * will do it in most cases. Please ensure that you run PHP 5.2.1 (or better) on your
  92.  * linux box.
  93.  * 
  94.  * <i> Windows systems </i>
  95.  * Windows users need to enable <i>php_zip.dll</i> inside of <i>php.ini</i> in order to
  96.  * use the <i>ZipArchive</i> class.
  97.  * 
  98.  * YOU NEED AT LEAST PHP 5.2.0 !!!
  99.  *  
  100.  * @category    File Formats
  101.  * @package        OpenDocumentPHP
  102.  * @author         Norman Markgraf (nmarkgraf(at)user.sourceforge.net)
  103.  * @copyright     Copyright in 2006, 2007 by The OpenDocumentPHP Team
  104.  * @license     http://www.gnu.org/licenses/gpl.html GNU General Public License 2.0.
  105.  * @version     Release: @package_version@
  106.  * @link           http://opendocumentphp.org
  107.  * @link        http://php.net/zip
  108.  * @since         0.5.0 - 08. Feb. 2007
  109.  */
  110. class OpenDocumentArchive extends ZipArchive {
  111.     /**
  112.      * Full path to manifest xml in the OpenDocument archive.
  113.      */
  114.     const PathToManifestXml = 'META-INF/manifest.xml';
  115.     /**
  116.      * No Manifest was found in Archive that where opened.
  117.      */
  118.     const NOMANIFEST = 1024;
  119.     /**
  120.      * The OpenDocument Manifest document as object.
  121.      * 
  122.      * @var ManifestDocument 
  123.      * @access private
  124.      */
  125.     private $manifest;
  126.     /**
  127.      * Constructor method.
  128.      * 
  129.      * @param         string $mimetype Mime type of the achrive
  130.      * @since         0.5.0 - 08. Feb. 2007
  131.      */
  132.     function __construct($mimetype ''{
  133.         $this->manifest new ManifestDocument($mimetype);
  134.     }
  135.     /**
  136.      * Adds a file to a ZIP archive from the given path.
  137.      * 
  138.      * @param         string $localname The name of the entry to create.
  139.      * @param         string $filename The path to the file to add.
  140.      * @param         string $mimetype Mime type of the file.
  141.      * @access         public
  142.      * @since         0.5.0 - 08. Feb. 2007
  143.      */
  144.     function addFile($filename$localname ''$mimetype 'text/text'{
  145.         /*
  146.          * First add to ZipArchive ...
  147.          */
  148.         $ret parent :: addFile($filename$localname);
  149.         if ($ret === true{
  150.             /*
  151.              * If successfully added to archive make a manifest entry
  152.              */
  153.             if ($localname === ''{
  154.                 /*
  155.                  *  If $localname not set, use $filename instead
  156.                  */     
  157.                 $localname $filename;
  158.             }
  159.             /*
  160.              * add new file-entry to manifest document 
  161.              */
  162.             $this->manifest->addFileEntry($localname$mimetype);
  163.         }
  164.         return $ret;
  165.     }
  166.     /**
  167.      * Add a file to a ZIP archive using its contents.
  168.      * 
  169.      * @param         string $localname The name of the entry to create.
  170.      * @param         string $contents The contents to use to create the entry. It is used in a binary safe mode.
  171.      * @param         string $mimetype Mime type of the file.
  172.      * @access         public
  173.      * @since         0.5.0 - 08. Feb. 2007
  174.      */
  175.     function addFromString($localname$contents$mimetype 'text/text'{
  176.         /*
  177.          * First add to ZipArchive ...
  178.          */
  179.         $ret parent :: addFromString($localname$contents);
  180.         if ($ret === true{
  181.             /*
  182.              * If successfully added to archive make a manifest entry
  183.              */
  184.             $this->manifest->addFileEntry($localname$mimetype);
  185.         }
  186.         return $ret;
  187.     }
  188.     /**
  189.      * Retrieve mime type of the OpenDocument archive.
  190.      * 
  191.      * @return         string Current mime type of the OpenDocument archive.
  192.      * @access         public
  193.      * @since         0.5.0 - 08. Feb. 2007
  194.      */
  195.     function getMimeType({
  196.         return $this->manifest->getMimeType();
  197.     }
  198.     /**
  199.      * Set mime type of the OpenDocument archive.
  200.      * 
  201.      * Therefor we delete the file 'mimetype' in the current OpenDocument archive
  202.      * and add a new file 'mimetype' with the new mime type given as parameter $mimetype.
  203.      * 
  204.      * @param         string $mimetype New mime type of the OpenDocument archive.
  205.      * @access         public
  206.      * @since         0.5.0 - 08. Feb. 2007
  207.      */
  208.     function setMimeType($mimetype{
  209.         /*
  210.          * We need to change the file 'mimetype' in the current archive as well:
  211.          * So we first remove the file ...
  212.          */
  213.         parent::deleteName('mimetype');
  214.         // ... and add a new one.
  215.         parent::addFormString('mimetype'$mimetype);
  216.         // Set mime type also in the manfifest document.
  217.         $this->manifest->setMimeType($mimetype);
  218.     }
  219.     /**
  220.      * Opens a new zip archive for reading, writing or modifying.
  221.      * 
  222.      * If we create a new archive, we add to files. First the manifest document
  223.      * stored in the ManifestDocument and second the file 'mimetype' which only
  224.      * include the
  225.      * 
  226.      * @param         string $filename The file name of the ZIP archive to open.
  227.      * @param         int $flags The mode to use to open the archive.
  228.      * @return        mixed 
  229.      * @access         public
  230.      * @since         0.5.0 - 08. Feb. 2007
  231.      */
  232.     function open($filename$flags 0$mimetype ''{
  233.         $ret parent :: open($filename$flags);
  234.         if ($ret === true{
  235.             if (($flags && self :: CREATE0{
  236.                 // If archive was freshly made, we need a new ManifestDocument.
  237.                 unset ($this->manifest);
  238.                 $this->manifest new ManifestDocument($mimetype);
  239.                 // Add the file 'mimetype' with the current mimetype
  240.                 if ($mimetype != ''{
  241.                     parent::addFromString('mimetype'$mimetype);
  242.                 }
  243.             else {
  244.                 $mfxml parent :: getFromName(self :: PathToManifestXml);
  245.                 if ($mfxml === false{
  246.                     $ret self :: NOMANIFEST $mfxml;
  247.                 else {
  248.                     $this->manifest->loadXML($mfxml);
  249.                 }
  250.             }
  251.         }
  252.         return $ret;
  253.     }
  254.     /**
  255.      * Delete an entry in the archive using its name.
  256.      * 
  257.      * @param         string $name Name of the entry to delete.
  258.      * @access         public
  259.      * @since         0.5.0 - 08. Feb. 2007
  260.      */
  261.     function deleteName($name{
  262.         $ret parent :: deleteName($name);
  263.         if ($ret === true{
  264.             // After the file is delete from the archive we also need to 
  265.             // remove it from the manifest file.
  266.             $ret $this->manifest->removeFileEntry($name);
  267.         }
  268.         return $ret;
  269.     }
  270.     /**
  271.      * Delete an entry in the archive using its index.
  272.      * 
  273.      * @param         int $index Index of the entry to delete.
  274.      * @access         public
  275.      * @since         0.5.0 - 08. Feb. 2007
  276.      */
  277.     function deleteIndex($index{
  278.         $ret false;
  279.         $stats parent :: statIndex($index);
  280.         if ($stats !== false{
  281.             $name $stats['name'];
  282.             $ret $this->deleteName($name);
  283.         }
  284.         return $ret;
  285.     }
  286.     /**
  287.      * Renames an entry defined by its index.
  288.      * 
  289.      * @param         int $index Index of the entry to rename.
  290.      * @param         string $newname New name.
  291.      * @access         public
  292.      * @since         0.5.0 - 08. Feb. 2007
  293.      */
  294.     function renameIndex($index$newname{
  295.         $ret false;
  296.         $stats parent :: statIndex($index);
  297.         if ($stats !== false{
  298.             $name $stats['name'];
  299.             $ret $this->renameName($name$newname);
  300.         }
  301.         return $ret;
  302.     }
  303.     /**
  304.      * Renames an entry defined by its name.
  305.      * 
  306.      * @param         string $name Name of the entry to rename.
  307.      * @param         string $newname New name.
  308.      * @access         public
  309.      * @since         0.5.0 - 08. Feb. 2007
  310.      */
  311.     function renameName($name$newname{
  312.         $ret parent :: renameName($name$newname);
  313.         if ($ret === true{
  314.             $ret $this->manifest->renameFileEntry($name$newname);
  315.         }
  316.         return $ret;
  317.     }
  318.     /**
  319.      * Get the details of an entry defined by its name.
  320.      * 
  321.      * The function obtains information about the entry defined by its name.
  322.      * 
  323.      * @param         string $name Name of the entry.
  324.      * @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.
  325.      * @return         array|boolReturns an array containing the entry details or <b>FALSE</b> on failure.
  326.      * @access         public
  327.      * @since         0.5.0 - 08. Feb. 2007
  328.      */
  329.     function statName($name$flags 0{
  330.         $ret false;
  331.         $stat parent :: statName($name$flags);
  332.         if ($stat !== false{
  333.             $ret $stat;
  334.             $tmp $this->manifest->getFilelist();
  335.             foreach ($tmp as $element{
  336.                 if ($element['name'=== $name{
  337.                     $ret array_merge($stat$element);
  338.                     break;
  339.                 }
  340.             }
  341.         }
  342.         return $ret;
  343.     }
  344.     /**
  345.      * Retrieve file list.
  346.      * 
  347.      * @access         public
  348.      * @since         0.5.0 - 08. Feb. 2007
  349.      */
  350.     function getFilelist({
  351.         return $this->manifest->getFilelist();
  352.     }
  353.     /**
  354.      * Retrieve file from archive as a DOM document.
  355.      * 
  356.      * @return        DOMDocument The file as a DOM document.
  357.      * @param        string $filename 
  358.      * @access         public
  359.      * @since         0.5.0 - 08. Feb. 2007
  360.      */
  361.     function getDOMFromName($filename{
  362.         $ret new DOMDocument();
  363.         $ret->loadXML(parent :: getFromName($filename));
  364.         return $ret;
  365.     }
  366.     /**
  367.      * Retreive the manifest document as a ManifestDocument object.
  368.      * 
  369.      * @return        ManifestDocument The manifest document.
  370.      * @access         public
  371.      * @since         0.5.0 - 08. Feb. 2007
  372.      */
  373.     function getManifest({
  374.         return $this->manifest;
  375.     }
  376.     /**
  377.      * Revert all changes done to an entry with the given name.
  378.      * Always returns <b>false</b>!
  379.      * 
  380.      * @param         string Name of the entry.
  381.      * @return         boolean Returns <b>true</b> if success, <b>false</b> if not.
  382.      * @access         public
  383.      * @since         0.5.0 - 08. Feb. 2007
  384.      */
  385.     function unchangeName($index{
  386.         return false;
  387.     }
  388.     /**
  389.      * Revert all changes done to an entry at the given index.
  390.      * Always returns <b>false</b>!
  391.      * 
  392.      * @param         int $index Index of the entry.
  393.      * @return         boolean Returns <b>true</b> if success, <b>false</b> if not.
  394.      * @access         public
  395.      * @since         0.5.0 - 08. Feb. 2007
  396.      */
  397.     function unchangeIndex($index{
  398.         return false;
  399.     }
  400.     /**
  401.      * Undo all changes done in the archive. Always returns <b>false</b>!
  402.      * 
  403.      * @return         boolean Returns <b>true</b> if success, <b>false</b> if not.
  404.      * @access         public
  405.      * @since         0.5.0 - 08. Feb. 2007
  406.      */
  407.     function unchangeAll({
  408.         return false;
  409.     }
  410.     /**
  411.      * Close opened or created archive and save changes. This method is
  412.      * automatically called at the end of the script.
  413.      * 
  414.      * @param        boolean $write Should we write to archive? - Default is true.
  415.      * @access         public
  416.      * @since         0.5.0 - 08. Feb. 2007
  417.      */
  418.     function close($write=true{
  419.         if ($write{
  420.             /*
  421.              * We want to write all changes back to the archive, so first we normalize 
  422.              * the manifest document ...
  423.              */
  424.             $this->manifest->normalize();
  425.             /*
  426.              * ... than we delete old manifest.xml in the archive ..
  427.              */
  428.             parent :: deleteName(self :: PathToManifestXml);
  429.             /*
  430.              *  ... and at last we write the ManifestDocument
  431.              */
  432.             parent :: addFromString(self :: PathToManifestXml$this->manifest->saveXML());
  433.         else {
  434.             /*
  435.              * So we don't want to change a thing in the archive, 
  436.              * thats why we revert all done work first.
  437.              */
  438.             parent::unchangeAll();
  439.         }
  440.         /*
  441.          * Now we can close the OpenDocumentArchive: 
  442.          */
  443.         return parent :: close();
  444.     }
  445. }
  446. ?>

Documentation generated on Wed, 18 Jun 2008 06:30:55 +0200 by phpDocumentor 1.3.2