Location: PHPKode > projects > Aukyla Platform > aukyla/doc/html/a00228.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
  <head>
    <title>Aukyla PHP Framework</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <link rel="StyleSheet" href="auton.css" type="text/css" media="screen">
  </head>

  <body>
    <div id="mainmenu">
      <a href="http://www.auton.nl/" class="home">Home</a>
    </div>
    <div id="submenu">
      <a class="logo" href="http://www.auton.nl/"></a>

      <div class="head">Ads</div>
      <a href="http://sourceforge.net/"><img src="http://sourceforge.net/sflogo.php?group_id=116358&amp;type=1" alt="SourceForge.net Logo" style="padding: 2px 4px; width: 88px; height: 31px; border: 0px"></a>
      <a href="http://getfirefox.com/" title="Get Firefox - The Browser, Reloaded."><img src="http://www.mozilla.org/products/firefox/buttons/getfirefox_small.png" alt="Get Firefox" style="width: 110px; height: 32px; border: 0px"></a>
    </div>
    <div id="main">
<!-- Generated by Doxygen 1.3.9.1 -->
<div class="qindex"><a class="qindex" href="index.html">Main&nbsp;Page</a> | <a class="qindex" href="hierarchy.html">Class&nbsp;Hierarchy</a> | <a class="qindex" href="classes.html">Alphabetical&nbsp;List</a> | <a class="qindex" href="annotated.html">Class&nbsp;List</a> | <a class="qindex" href="files.html">File&nbsp;List</a> | <a class="qindex" href="functions.html">Class&nbsp;Members</a> | <a class="qindex" href="pages.html">Related&nbsp;Pages</a></div>
<div class="nav">
<a class="el" href="dir_000000.html">base</a></div>
<h1>OpenOffice.php</h1><div class="fragment"><pre class="fragment">00001 &lt;?php
00002 <span class="comment">/*</span>
00003 <span class="comment">     OpenOffice.php, functions for converting XHTML to OpenOffice.org documents</span>
00004 <span class="comment">                     and back</span>
00005 <span class="comment">     Copyright (C) 2003-2004 Arend van Beelen, Auton Rijnsburg</span>
00006 <span class="comment"></span>
00007 <span class="comment">     This program is free software; you can redistribute it and/or modify it</span>
00008 <span class="comment">     under the terms of the GNU General Public License as published by the Free</span>
00009 <span class="comment">     Software Foundation; either version 2 of the License, or (at your option)</span>
00010 <span class="comment">     any later version.</span>
00011 <span class="comment"></span>
00012 <span class="comment">     This program is distributed in the hope that it will be useful, but WITHOUT</span>
00013 <span class="comment">     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or</span>
00014 <span class="comment">     FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for</span>
00015 <span class="comment">     more details.</span>
00016 <span class="comment"></span>
00017 <span class="comment">     You should have received a copy of the GNU General Public License along</span>
00018 <span class="comment">     with this program; if not, write to the Free Software Foundation, Inc.,</span>
00019 <span class="comment">     59 Temple Place, Suite 330, Boston, MA 02111-1307 USA</span>
00020 <span class="comment"></span>
00021 <span class="comment">     For any questions, comments or whatever, you may mail me at: hide@address.com</span>
00022 <span class="comment">*/</span>
00023 
00024 require_once('<a class="code" href="a00163.html">URI</a>.php');
00025 
<a name="l00029"></a><a class="code" href="a00133.html">00029</a> <span class="keyword">class </span><a class="code" href="a00133.html">OpenOffice</a>
00030 {
<a name="l00045"></a><a class="code" href="a00133.html#e0">00045</a>         <span class="keyword">static</span> function convertXHTML2SXW($xhtmlDoc, $openofficeDoc)
00046         {
00047                 <span class="comment">// check the access permissions</span>
00048                 <span class="keywordflow">if</span>(!((<a class="code" href="a00163.html#e15">URI::permissions</a>($xhtmlDoc) &amp; PERMISSION_READ) &amp;&amp;
00049                      ((!<a class="code" href="a00163.html#e29">URI::fileExists</a>($openofficeDoc) &amp;&amp; <a class="code" href="a00163.html#e15">URI::permissions</a>(dirname($openofficeDoc)) &amp; PERMISSION_APPEND) ||
00050                       (<a class="code" href="a00163.html#e29">URI::fileExists</a>($openofficeDoc) &amp;&amp; <a class="code" href="a00163.html#e15">URI::permissions</a>($openofficeDoc) &amp; PERMISSION_MODIFY))))
00051                 {
00052                         <span class="keywordflow">return</span> <span class="keyword">false</span>;
00053                 }
00054 
00055                 <span class="comment">// create a temporary directory</span>
00056                 $tmpdir = tempnam('/tmp', '<a class="code" href="a00133.html">OpenOffice</a>');
00057                 unlink($tmpdir);
00058                 mkdir($tmpdir);
00059 
00060                 <span class="comment">// copy images</span>
00061                 $picturesDir = dirname($xhtmlDoc).'/Pictures';
00062                 <span class="keywordflow">if</span>(<a class="code" href="a00163.html#e29">URI::fileExists</a>($picturesDir))
00063                 {
00064                         mkdir(<span class="stringliteral">"Pictures"</span>);
00065                         foreach(URI::entries($picturesDir) as $entry)
00066                         {
00067                                 <span class="keywordflow">if</span>($entry == <span class="charliteral">'.'</span> || $entry == '..')
00068                                 {
00069                                         <span class="keywordflow">continue</span>;
00070                                 }
00071 
00072                                 copy(<span class="stringliteral">"$picturesDir/$entry"</span>, <span class="stringliteral">"$tmpdir/Pictures/$entry"</span>);
00073                         }
00074                 }
00075 
00076                 <span class="comment">// convert the XHTML file using the XSLT stylesheet into an OOo XML file</span>
00077                 $xmlDoc = <span class="keyword">new</span> DOMDocument();
00078                 $xmlDoc-&gt;load($xhtmlDoc);
00079 
00080                 $xsltDoc = <span class="keyword">new</span> DOMDocument();
00081                 $xsltDoc-&gt;load(AUKYLA_DIR.'/data/base/<a class="code" href="a00133.html">OpenOffice</a>.php/xhtml2sxw.xslt');
00082 
00083                 $proc = <span class="keyword">new</span> XSLTProcessor();
00084                 <span class="keywordflow">if</span>($proc-&gt;hasExsltSupport() == <span class="keyword">false</span>)
00085                 {
00086                         die('EXSLT support in PHP is required <span class="keywordflow">for</span> converting <a class="code" href="a00133.html">OpenOffice</a>.org files!');
00087                 }
00088                 $proc-&gt;importStyleSheet($xsltDoc);
00089                 $proc-&gt;setParameter('', 'imagePrefix', '#Pictures/');
00090                 <span class="keywordflow">if</span>($proc-&gt;transformToURI($xmlDoc, <span class="stringliteral">"$tmpdir/content.xml"</span>) == <span class="keyword">false</span>)
00091                 {
00092                         <span class="keywordflow">return</span> <span class="keyword">false</span>;
00093                 }
00094 
00095                 <span class="comment">// create an OpenOffice.org zip file from the temporary directory</span>
00096                 exec(<span class="stringliteral">"cd $tmpdir; zip -r - * &gt; document.sxw"</span>, $output, $<span class="keywordflow">return</span>);
00097                 <span class="keywordflow">if</span>($return != 0)
00098                 {
00099                         <span class="keywordflow">return</span> <span class="keyword">false</span>;
00100                 }
00101                 copy(<span class="stringliteral">"$tmpdir/document.sxw"</span>, $openofficeDoc);
00102                 exec(<span class="stringliteral">"rm -R $tmpdir"</span>);
00103 
00104                 <span class="keywordflow">return</span> <span class="keyword">true</span>;
00105         }
00106 
<a name="l00118"></a><a class="code" href="a00133.html#e1">00118</a>         <span class="keyword">static</span> function convertSXW2XHTML($openofficeDoc, $xhtmlDoc)
00119         {
00120                 <span class="comment">// check the access permissions</span>
00121                 <span class="keywordflow">if</span>(!((<a class="code" href="a00163.html#e15">URI::permissions</a>($openofficeDoc) &amp; PERMISSION_READ) &amp;&amp;
00122                      (<a class="code" href="a00163.html#e15">URI::permissions</a>(dirname($xhtmlDoc)) &amp; PERMISSION_APPEND) &amp;&amp;
00123                      (!<a class="code" href="a00163.html#e29">URI::fileExists</a>($xhtmlDoc) ||
00124                       <a class="code" href="a00163.html#e15">URI::permissions</a>($xhtmlDoc) &amp; PERMISSION_MODIFY)))
00125                 {
00126                         <span class="keywordflow">return</span> <span class="keyword">false</span>;
00127                 }
00128 
00129                 <span class="comment">// create a temporary directory and extract the OOo document to it</span>
00130                 $tmpdir = tempnam('/tmp', '<a class="code" href="a00133.html">OpenOffice</a>');
00131                 unlink($tmpdir);
00132                 mkdir($tmpdir);
00133                 copy($openofficeDoc, <span class="stringliteral">"$tmpdir/document.sxw"</span>);
00134                 exec(<span class="stringliteral">"unzip $tmpdir/document.sxw -d $tmpdir"</span>, $output, $<span class="keywordflow">return</span>);
00135                 <span class="keywordflow">if</span>($return != 0)
00136                 {
00137                         <span class="keywordflow">return</span> <span class="keyword">false</span>;
00138                 }
00139 
00140                 <span class="comment">// copy images</span>
00141                 <span class="keywordflow">if</span>(file_exists(<span class="stringliteral">"$tmpdir/Pictures"</span>))
00142                 {
00143                         $picturesDir = dirname($xhtmlDoc).'<span class="comment">//Pictures';</span>
00144                         <span class="keywordflow">if</span>(!<a class="code" href="a00163.html#e29">URI::fileExists</a>($picturesDir))
00145                         {
00146                                 <a class="code" href="a00163.html#e18">URI::mkdir</a>($picturesDir);
00147                         }
00148                         foreach(glob(<span class="stringliteral">"$tmpdir/Pictures/*"</span>) as $entry)
00149                         {
00150                                 <span class="keywordflow">if</span>($entry == <span class="charliteral">'.'</span> || $entry == '..')
00151                                 {
00152                                         <span class="keywordflow">continue</span>;
00153                                 }
00154 
00155                                 $basename = basename($entry);
00156                                 copy($entry, <span class="stringliteral">"$picturesDir/$basename"</span>);
00157                         }
00158                 }
00159 
00160                 <span class="comment">// convert the OOo XML file using the XSLT stylesheet into an XHTML file</span>
00161                 $xmlDoc = <span class="keyword">new</span> DOMDocument();
00162                 <span class="keywordflow">if</span>($xmlDoc-&gt;load(<span class="stringliteral">"$tmpdir/content.xml"</span>) == <span class="keyword">false</span>)
00163                 {
00164                         <span class="keywordflow">return</span> <span class="keyword">false</span>;
00165                 }
00166 
00167                 $xsltDoc = <span class="keyword">new</span> DOMDocument();
00168                 $xsltDoc-&gt;load(AUKYLA_DIR.'/data/base/<a class="code" href="a00133.html">OpenOffice</a>.php/sxw2xhtml.xslt');
00169 
00170                 $proc = <span class="keyword">new</span> XSLTProcessor();
00171                 <span class="keywordflow">if</span>($proc-&gt;hasExsltSupport() == <span class="keyword">false</span>)
00172                 {
00173                         die('EXSLT support in PHP is required <span class="keywordflow">for</span> converting <a class="code" href="a00133.html">OpenOffice</a>.org files!');
00174                 }
00175                 $proc-&gt;importStyleSheet($xsltDoc);
00176                 $proc-&gt;setParameter('', 'imagePrefix', Config::globals('downloadURL').'?file='.dirname($xhtmlDoc).'/Pictures/');
00177                 <span class="keywordflow">if</span>($proc-&gt;transformToURI($xmlDoc, $xhtmlDoc) == <span class="keyword">false</span>)
00178                 {
00179                         <span class="keywordflow">return</span> <span class="keyword">false</span>;
00180                 }
00181 
00182                 <span class="comment">// cleanup temporary directory</span>
00183                 exec(<span class="stringliteral">"rm -R $tmpdir"</span>);
00184 
00185                 <span class="keywordflow">return</span> <span class="keyword">true</span>;
00186         }
00187 
00196         <span class="keyword">static</span> function scaleImages($openofficeDoc, $maxWidth, $maxHeight)
00197         {
00198                 <span class="comment">// check the access permissions</span>
00199                 <span class="keywordflow">if</span>(!(<a class="code" href="a00163.html#e15">URI::permissions</a>($openofficeDoc) &amp; PERMISSION_MODIFY))
00200                 {
00201                         <span class="keywordflow">return</span> <span class="keyword">false</span>;
00202                 }
00203 
00204                 <span class="comment">// create a temporary directory and extract the OOo document to it</span>
00205                 $tmpdir = tempnam('/tmp', '<a class="code" href="a00133.html">OpenOffice</a>');
00206                 unlink($tmpdir);
00207                 mkdir($tmpdir);
00208                 copy($openofficeDoc, <span class="stringliteral">"$tmpdir/document.sxw"</span>);
00209                 exec(<span class="stringliteral">"unzip $tmpdir/document.sxw -d $tmpdir"</span>, $output, $<span class="keywordflow">return</span>);
00210                 <span class="keywordflow">if</span>($return != 0)
00211                 {
00212                         <span class="keywordflow">return</span> <span class="keyword">false</span>;
00213                 }
00214 
00215                 $images = glob(<span class="stringliteral">"$tmpdir/Pictures/*"</span>);
00216                 <span class="keywordflow">if</span>($images !== <span class="keyword">false</span>)
00217                 {
00218                         foreach($images as $image)
00219                         {
00220                                 unset($output);
00221                                 exec(<span class="stringliteral">"identify $image"</span>, $output, $<span class="keywordflow">return</span>);
00222                                 <span class="keywordflow">if</span>($return != 0)
00223                                 {
00224                                         exec(<span class="stringliteral">"rm -R $tmpdir"</span>);
00225                                         <span class="keywordflow">return</span> <span class="keyword">false</span>;
00226                                 }
00227 
00228                                 $properties = explode(<span class="charliteral">' '</span>, $output[0]);
00229                                 $geometry = $properties[2];
00230                                 list($width, $height) = explode(<span class="charliteral">'x'</span>, $geometry);
00231                                 <span class="keywordflow">if</span>($width &gt; $maxWidth || $height &gt; $maxHeight)
00232                                 {
00233                                         exec(<span class="stringliteral">"convert -size {$maxWidth}x{$maxHeight} $image -scale {$maxWidth}x{$maxHeight} $image"</span>, $output, $<span class="keywordflow">return</span>);
00234                                         <span class="keywordflow">if</span>($return != 0)
00235                                         {
00236                                                 exec(<span class="stringliteral">"rm -R $tmpdir"</span>);
00237                                                 <span class="keywordflow">return</span> <span class="keyword">false</span>;
00238                                         }
00239                                 }
00240                         }
00241 
00242                         <span class="comment">// zip the document again</span>
00243                         exec(<span class="stringliteral">"cd $tmpdir; rm document.sxw; zip -r - * &gt; document.sxw"</span>, $output, $<span class="keywordflow">return</span>);
00244                         <span class="keywordflow">if</span>($return != 0)
00245                         {
00246                                 <span class="keywordflow">return</span> <span class="keyword">false</span>;
00247                         }
00248                         copy(<span class="stringliteral">"$tmpdir/document.sxw"</span>, $openofficeDoc);
00249                 }
00250 
00251                 <span class="comment">// cleanup temporary directory</span>
00252                 exec(<span class="stringliteral">"rm -R $tmpdir"</span>);
00253 
00254                 <span class="keywordflow">return</span> <span class="keyword">true</span>;
00255         }
00256 }
00257 
00258 ?&gt;
</pre></div>    </div>
  </body>
</html>
Return current item: Aukyla Platform