<?php
/*
OpenOffice.php, functions for converting XHTML to OpenOffice.org documents
and back
Copyright (C) 2003-2004 Arend van Beelen, Auton Rijnsburg
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at your option)
any later version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
For any questions, comments or whatever, you may mail me at: hide@address.com
*/
require_once('URI.php');
/**
* @brief Class for converting and creating OpenOffice.org documents.
*/
class OpenOffice
{
/**
* Converts an XHTML document to an OpenOffice.org document. Any images
* in a Pictures/ subdirectory in the source directory of the XHTML
* document will be assumed to be part of the document and will be
* included in the OpenOffice.org document.
*
* @note Saving OpenOffice.org documents is still highly experimental.
*
* @param xhtmlDoc The XHTML document used as source for the
* conversion.
* @param openofficeDoc The filename for the resulting OpenOffice.org
* document.
* @return @p true on success, @p false otherwise.
*/
static function convertXHTML2SXW($xhtmlDoc, $openofficeDoc)
{
// check the access permissions
if(!((URI::permissions($xhtmlDoc) & PERMISSION_READ) &&
((!URI::fileExists($openofficeDoc) && URI::permissions(dirname($openofficeDoc)) & PERMISSION_APPEND) ||
(URI::fileExists($openofficeDoc) && URI::permissions($openofficeDoc) & PERMISSION_MODIFY))))
{
return false;
}
// create a temporary directory
$tmpdir = tempnam('/tmp', 'OpenOffice');
unlink($tmpdir);
mkdir($tmpdir);
// copy images
$picturesDir = dirname($xhtmlDoc).'/Pictures';
if(URI::fileExists($picturesDir))
{
mkdir("Pictures");
foreach(URI::entries($picturesDir) as $entry)
{
if($entry == '.' || $entry == '..')
{
continue;
}
copy("$picturesDir/$entry", "$tmpdir/Pictures/$entry");
}
}
// convert the XHTML file using the XSLT stylesheet into an OOo XML file
$xmlDoc = new DOMDocument();
$xmlDoc->load($xhtmlDoc);
$xsltDoc = new DOMDocument();
$xsltDoc->load(AUKYLA_DIR.'/data/base/OpenOffice.php/xhtml2sxw.xslt');
$proc = new XSLTProcessor();
if($proc->hasExsltSupport() == false)
{
die('EXSLT support in PHP is required for converting OpenOffice.org files!');
}
$proc->importStyleSheet($xsltDoc);
$proc->setParameter('', 'imagePrefix', '#Pictures/');
if($proc->transformToURI($xmlDoc, "$tmpdir/content.xml") == false)
{
return false;
}
// create an OpenOffice.org zip file from the temporary directory
exec("cd $tmpdir; zip -r - * > document.sxw", $output, $return);
if($return != 0)
{
return false;
}
copy("$tmpdir/document.sxw", $openofficeDoc);
exec("rm -R $tmpdir");
return true;
}
/**
* Converts an OpenOffice.org document to an XHTML document. Any images
* contained in the OpenOffice.org document will be placed in a
* Pictures/ subdirectory in the destination directory of the XHTML
* document.
*
* @param openofficeDoc The OpenOffice.org document used as source for
* the conversion.
* @param xhtmlDoc The filename for the resulting XHTML document.
* @return @p true on success, @p false otherwise.
*/
static function convertSXW2XHTML($openofficeDoc, $xhtmlDoc)
{
// check the access permissions
if(!((URI::permissions($openofficeDoc) & PERMISSION_READ) &&
(URI::permissions(dirname($xhtmlDoc)) & PERMISSION_APPEND) &&
(!URI::fileExists($xhtmlDoc) ||
URI::permissions($xhtmlDoc) & PERMISSION_MODIFY)))
{
return false;
}
// create a temporary directory and extract the OOo document to it
$tmpdir = tempnam('/tmp', 'OpenOffice');
unlink($tmpdir);
mkdir($tmpdir);
copy($openofficeDoc, "$tmpdir/document.sxw");
exec("unzip $tmpdir/document.sxw -d $tmpdir", $output, $return);
if($return != 0)
{
return false;
}
// copy images
if(file_exists("$tmpdir/Pictures"))
{
$picturesDir = dirname($xhtmlDoc).'//Pictures';
if(!URI::fileExists($picturesDir))
{
URI::mkdir($picturesDir);
}
foreach(glob("$tmpdir/Pictures/*") as $entry)
{
if($entry == '.' || $entry == '..')
{
continue;
}
$basename = basename($entry);
copy($entry, "$picturesDir/$basename");
}
}
// convert the OOo XML file using the XSLT stylesheet into an XHTML file
$xmlDoc = new DOMDocument();
if($xmlDoc->load("$tmpdir/content.xml") == false)
{
return false;
}
$xsltDoc = new DOMDocument();
$xsltDoc->load(AUKYLA_DIR.'/data/base/OpenOffice.php/sxw2xhtml.xslt');
$proc = new XSLTProcessor();
if($proc->hasExsltSupport() == false)
{
die('EXSLT support in PHP is required for converting OpenOffice.org files!');
}
$proc->importStyleSheet($xsltDoc);
$proc->setParameter('', 'imagePrefix', Config::globals('downloadURL').'?file='.dirname($xhtmlDoc).'/Pictures/');
if($proc->transformToURI($xmlDoc, $xhtmlDoc) == false)
{
return false;
}
// cleanup temporary directory
exec("rm -R $tmpdir");
return true;
}
/**
* Scales all images in an OpenOffice.org document to a maximum resolution.
*
* @param openofficeDoc The document to scale the images from.
* @param maxWidth The maximum width of the images.
* @param maxHeight The maximum height of the images.
* @return @p true on success, @p false on error.
*/
static function scaleImages($openofficeDoc, $maxWidth, $maxHeight)
{
// check the access permissions
if(!(URI::permissions($openofficeDoc) & PERMISSION_MODIFY))
{
return false;
}
// create a temporary directory and extract the OOo document to it
$tmpdir = tempnam('/tmp', 'OpenOffice');
unlink($tmpdir);
mkdir($tmpdir);
copy($openofficeDoc, "$tmpdir/document.sxw");
exec("unzip $tmpdir/document.sxw -d $tmpdir", $output, $return);
if($return != 0)
{
return false;
}
$images = glob("$tmpdir/Pictures/*");
if($images !== false)
{
foreach($images as $image)
{
unset($output);
exec("identify $image", $output, $return);
if($return != 0)
{
exec("rm -R $tmpdir");
return false;
}
$properties = explode(' ', $output[0]);
$geometry = $properties[2];
list($width, $height) = explode('x', $geometry);
if($width > $maxWidth || $height > $maxHeight)
{
exec("convert -size {$maxWidth}x{$maxHeight} $image -scale {$maxWidth}x{$maxHeight} $image", $output, $return);
if($return != 0)
{
exec("rm -R $tmpdir");
return false;
}
}
}
// zip the document again
exec("cd $tmpdir; rm document.sxw; zip -r - * > document.sxw", $output, $return);
if($return != 0)
{
return false;
}
copy("$tmpdir/document.sxw", $openofficeDoc);
}
// cleanup temporary directory
exec("rm -R $tmpdir");
return true;
}
}
?>