Commit 5b9461f5c8a8f3e6cdf612e868a7e7e190daecd2

Authored by Kevin Fourie
1 parent 068a9e28

KTS-1890

"Fatal error occurs in Workflow Administration, Security, Action Restrictions (by state) , Fatal error: Call to a member function on a non-object in C:\Program Files\ktdmsdev\knowledgetree\plugins\ktstandard\PDFGeneratorAction.php on line 136"
Updated and added some error checking.

Reviewed By: Conrad



git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/trunk@6512 c91229c3-7414-0410-bfa2-8a42b809f60b
bin/openoffice/pdfgen.py
1 1 #!/usr/bin/env python
2 2 #
  3 +#
  4 +# $Id: view.php 6203 2007-02-08 10:41:00Z kevin_fourie $
  5 +#
  6 +# The contents of this file are subject to the KnowledgeTree Public
  7 +# License Version 1.1 ("License"); You may not use this file except in
  8 +# compliance with the License. You may obtain a copy of the License at
  9 +# http://www.knowledgetree.com/KPL
3 10 #
4   -# Copyright (c) 2007 Jam Warehouse http://www.jamwarehouse.com
5   -#
6   -# This program is free software; you can redistribute it and/or modify
7   -# it under the terms of the GNU General Public License as published by
8   -# the Free Software Foundation; using version 2 of the License.
9   -#
10   -# This program is distributed in the hope that it will be useful,
11   -# but WITHOUT ANY WARRANTY; without even the implied warranty of
12   -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   -# GNU General Public License for more details.
14   -#
15   -# You should have received a copy of the GNU General Public License
16   -# along with this program; if not, write to the Free Software
17   -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18   -#
19   -# -------------------------------------------------------------------------
20   -#
21   -# You can contact the copyright owner regarding licensing via the contact
22   -# details that can be found on the KnowledgeTree web site:
  11 +# Software distributed under the License is distributed on an "AS IS"
  12 +# basis,
  13 +# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  14 +# for the specific language governing rights and limitations under the
  15 +# License.
23 16 #
24   -# http://www.knowledgetree.com/
  17 +# The Original Code is: KnowledgeTree Open Source
25 18 #
  19 +# The Initial Developer of the Original Code is The Jam Warehouse Software
  20 +# (Pty) Ltd, trading as KnowledgeTree.
  21 +# Portions created by The Jam Warehouse Software (Pty) Ltd are Copyright
  22 +# (C) 2007 The Jam Warehouse Software (Pty) Ltd;
  23 +# All Rights Reserved.
  24 +#
  25 +#
26 26  
27 27 import uno
28 28 import sys
... ... @@ -31,22 +31,47 @@ from com.sun.star.beans import PropertyValue
31 31 url_original = uno.systemPathToFileUrl(sys.argv[1])
32 32 url_save = uno.systemPathToFileUrl(sys.argv[2])
33 33  
34   -### Get Service Manager
35   -context = uno.getComponentContext()
36   -resolver = context.ServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", context)
37   -ctx = resolver.resolve("uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext")
38   -smgr = ctx.ServiceManager
  34 +try:
  35 + ### Get Service Manager
  36 + context = uno.getComponentContext()
  37 + resolver = context.ServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", context)
  38 + ctx = resolver.resolve("uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext")
  39 + smgr = ctx.ServiceManager
39 40  
40   -### Load document
41   -properties = []
42   -p = PropertyValue()
43   -p.Name = "Hidden"
44   -p.Value = True
45   -properties.append(p)
46   -properties = tuple(properties)
  41 + ### Load document
  42 + properties = []
  43 + p = PropertyValue()
  44 + p.Name = "Hidden"
  45 + p.Value = True
  46 + properties.append(p)
  47 + properties = tuple(properties)
  48 +
  49 + desktop = smgr.createInstanceWithContext("com.sun.star.frame.Desktop", ctx)
  50 +
  51 +except NoConnectException, e:
  52 + sys.stderr.write("OpenOffice process not found or not listening (" + e.Message + ")\n")
  53 + sys.exit(1)
  54 +except IllegalArgumentException, e:
  55 + sys.stderr.write("The url is invalid ( " + e.Message + ")\n")
  56 + sys.exit(1)
  57 +except RuntimeException, e:
  58 + sys.stderr.write("An unknown error occured: " + e.Message + "\n")
  59 +
  60 +try:
  61 + doc = desktop.loadComponentFromURL(url_original, "_blank", 0, properties)
  62 +except IOException, e:
  63 + sys.stderr.write("URL couldn't be found or was corrupt (" + e.Message + ")\n")
  64 + sys.exit(1)
  65 +except IllegalArgumentException, e:
  66 + sys.stderr.write("Given parameters doesn't conform to the specification ( " + e.Message + ")\n")
  67 + sys.exit(1)
  68 +except RuntimeException, e:
  69 + sys.stderr.write("An unknown error occured: " + e.Message + "\n")
47 70  
48   -desktop = smgr.createInstanceWithContext("com.sun.star.frame.Desktop", ctx)
49   -doc = desktop.loadComponentFromURL(url_original, "_blank", 0, properties)
  71 +if doc == None:
  72 + sys.stderr.write("Could not load doc.\n")
  73 + sys.exit(1)
  74 +
50 75  
51 76 ### Save File
52 77 properties = []
... ...
plugins/ktstandard/PDFGeneratorAction.php
1 1 <?php
2 2 /**
  3 + * $Id$
3 4 *
4   - * Copyright (c) 2007 Jam Warehouse http://www.jamwarehouse.com
  5 + * The contents of this file are subject to the KnowledgeTree Public
  6 + * License Version 1.1 ("License"); You may not use this file except in
  7 + * compliance with the License. You may obtain a copy of the License at
  8 + * http://www.knowledgetree.com/KPL
  9 + *
  10 + * Software distributed under the License is distributed on an "AS IS"
  11 + * basis,
  12 + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  13 + * for the specific language governing rights and limitations under the
  14 + * License.
  15 + *
  16 + * The Original Code is: KnowledgeTree Open Source
  17 + *
  18 + * The Initial Developer of the Original Code is The Jam Warehouse Software
  19 + * (Pty) Ltd, trading as KnowledgeTree.
  20 + * Portions created by The Jam Warehouse Software (Pty) Ltd are Copyright
  21 + * (C) 2007 The Jam Warehouse Software (Pty) Ltd;
  22 + * All Rights Reserved.
5 23 *
6   - * This program is free software; you can redistribute it and/or modify
7   - * it under the terms of the GNU General Public License as published by
8   - * the Free Software Foundation; using version 2 of the License.
9   - *
10   - * This program is distributed in the hope that it will be useful,
11   - * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   - * GNU General Public License for more details.
14   - *
15   - * You should have received a copy of the GNU General Public License
16   - * along with this program; if not, write to the Free Software
17   - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18   - *
19   - * -------------------------------------------------------------------------
20   - *
21   - * You can contact the copyright owner regarding licensing via the contact
22   - * details that can be found on the KnowledgeTree web site:
23   - *
24   - * http://www.knowledgetree.com/
25 24 */
26 25  
27 26 require_once(KT_LIB_DIR . '/actions/folderaction.inc.php');
... ... @@ -41,11 +40,11 @@ class PDFGeneratorAction extends KTDocumentAction {
41 40 // Note: 'asc' below seems to be a catchall for plain text docs.
42 41 // 'htm' and 'html' should work but are not so have been removed for now.
43 42 var $aAcceptedMimeTypes = array('doc', 'ods', 'odt', 'ott', 'txt', 'rtf', 'sxw', 'stw',
44   -// 'html', 'htm',
45   - 'xml' , 'pdb', 'psw', 'ods', 'ots', 'sxc',
46   - 'stc', 'dif', 'dbf', 'xls', 'xlt', 'slk', 'csv', 'pxl',
47   - 'odp', 'otp', 'sxi', 'sti', 'ppt', 'pot', 'sxd', 'odg',
48   - 'otg', 'std', 'asc');
  43 + // 'html', 'htm',
  44 + 'xml' , 'pdb', 'psw', 'ods', 'ots', 'sxc',
  45 + 'stc', 'dif', 'dbf', 'xls', 'xlt', 'slk', 'csv', 'pxl',
  46 + 'odp', 'otp', 'sxi', 'sti', 'ppt', 'pot', 'sxd', 'odg',
  47 + 'otg', 'std', 'asc');
49 48  
50 49 function getDisplayName() {
51 50 // We need to handle Windows differently - as usual ;)
... ... @@ -67,35 +66,35 @@ class PDFGeneratorAction extends KTDocumentAction {
67 66 }
68 67 return '';
69 68 }
70   -
  69 +
71 70 function form_main() {
72 71 $oForm = new KTForm;
73 72 $oForm->setOptions(array(
74   - 'label' => _kt('Convert Document to PDF'),
75   - 'action' => 'selectType',
76   - 'fail_action' => 'main',
77   - 'cancel_url' => KTBrowseUtil::getUrlForDocument($this->oDocument),
78   - 'submit_label' => _kt('Convert Document'),
79   - 'context' => &$this,
80   - ));
81   -
  73 + 'label' => _kt('Convert Document to PDF'),
  74 + 'action' => 'selectType',
  75 + 'fail_action' => 'main',
  76 + 'cancel_url' => KTBrowseUtil::getUrlForDocument($this->oDocument),
  77 + 'submit_label' => _kt('Convert Document'),
  78 + 'context' => &$this,
  79 + ));
  80 +
82 81 $oForm->setWidgets(array(
83   - array('ktcore.widgets.selection', array(
84   - 'label' => _kt("Type of conversion"),
85   - 'description' => _kt('The following are the types of conversions you can perform on this document.'),
86   - 'important_description' => _kt('QA NOTE: Permissions checks are required here...'),
87   - 'name' => 'convert_type',
88   - 'vocab' => array('Download as PDF', 'Duplicate as PDF', 'Replace as PDF'),
89   - 'simple_select' => true,
90   - 'required' => true,
91   - )),
92   - ));
93   -
  82 + array('ktcore.widgets.selection', array(
  83 + 'label' => _kt("Type of conversion"),
  84 + 'description' => _kt('The following are the types of conversions you can perform on this document.'),
  85 + 'important_description' => _kt('QA NOTE: Permissions checks are required here...'),
  86 + 'name' => 'convert_type',
  87 + 'vocab' => array('Download as PDF', 'Duplicate as PDF', 'Replace as PDF'),
  88 + 'simple_select' => true,
  89 + 'required' => true,
  90 + )),
  91 + ));
  92 +
94 93 return $oForm;
95 94 }
96 95  
97 96 function do_selectType() {
98   -
  97 +
99 98 switch($_REQUEST[data][convert_type]){
100 99 case '0':
101 100 $this->do_pdfdownload();
... ... @@ -120,20 +119,20 @@ class PDFGeneratorAction extends KTDocumentAction {
120 119 $oForm = $this->form_main();
121 120  
122 121 $oTemplate->setData(array(
123   - 'context' => &$this,
124   - 'form' => $oForm,
125   - ));
  122 + 'context' => &$this,
  123 + 'form' => $oForm,
  124 + ));
126 125 return $oTemplate->render();
127 126 }
128 127  
129 128 /**
130   - * Method for getting the MIME type extension for the current document.
131   - *
132   - * @return string mime time extension
133   - */
  129 + * Method for getting the MIME type extension for the current document.
  130 + *
  131 + * @return string mime time extension
  132 + */
134 133 function getMimeExtension() {
135 134  
136   - if($this->Document == null || $this->Document == "" || PEAR::isError($this->Document) ) return _kt('Unknown Type');
  135 + if($this->oDocument == null || $this->oDocument == "" || PEAR::isError($this->oDocument) ) return _kt('Unknown Type');
137 136  
138 137 $oDocument = $this->oDocument;
139 138 $iMimeTypeId = $oDocument->getMimeTypeID();
... ... @@ -153,10 +152,10 @@ class PDFGeneratorAction extends KTDocumentAction {
153 152 }
154 153  
155 154 /**
156   - * Method for downloading the document as a pdf.
157   - *
158   - * @return true on success else false
159   - */
  155 + * Method for downloading the document as a pdf.
  156 + *
  157 + * @return true on success else false
  158 + */
160 159 function do_pdfdownload() {
161 160  
162 161 $oDocument = $this->oDocument;
... ... @@ -165,33 +164,40 @@ class PDFGeneratorAction extends KTDocumentAction {
165 164  
166 165 //get the actual path to the document on the server
167 166 $sPath = sprintf("%s/%s", $oConfig->get('urls/documentRoot'), $oStorage->getPath($oDocument));
168   -
  167 +
169 168 if (file_exists($sPath)) {
170 169  
171   - # Get a tmp file
  170 +# Get a tmp file
172 171 $sTempFilename = tempnam('/tmp', 'ktpdf');
173   -
174   - # We need to handle Windows differently - as usual ;)
  172 +
  173 +# We need to handle Windows differently - as usual ;)
175 174 if (substr( PHP_OS, 0, 3) == 'WIN') {
176   -
177   - $cmd = "\"" . KT_DIR . "/../openoffice/openoffice/program/python.bat\" \"". KT_DIR . "/bin/openoffice/pdfgen.py\" \"" . $sPath . "\" \"" . $sTempFilename . "\"";
178   - $cmd = str_replace( '/','\\',$cmd);
179   -
  175 +
  176 + $cmd = "\"" . KT_DIR . "/../openoffice/openoffice/program/python.bat\" \"". KT_DIR . "/bin/openoffice/pdfgen.py\" \"" . $sPath . "\" \"" . $sTempFilename . "\"";
  177 + $cmd = str_replace( '/','\\',$cmd);
  178 +
180 179 // TODO: Check for more errors here
181 180 // SECURTIY: Ensure $sPath and $sTempFilename are safe or they could be used to excecute arbitrary commands!
182 181 // Excecute the python script. TODO: Check this works with Windows
183   - $res = `"$cmd" 2>&1`;
184   -
  182 + $res = `"$cmd" 2>&1`;
  183 + //print($res);
  184 + //print($cmd);
  185 + //exit;
  186 +
185 187 } else {
186 188  
187 189 // TODO: Check for more errors here
188 190 // SECURTIY: Ensure $sPath and $sTempFilename are safe or they could be used to excecute arbitrary commands!
189   - // Excecute the python script. TODO: Check this works with Windows
190   - $res = exec('../openoffice/program/python bin/openoffice/pdfgen.py ' . escapeshellcmd($sPath) . ' ' . escapeshellcmd($sTempFilename) );
191   -
  191 + // Excecute the python script.
  192 + $cmd = '../openoffice/program/python bin/openoffice/pdfgen.py ' . escapeshellcmd($sPath) . ' ' . escapeshellcmd($sTempFilename);
  193 + $res = shell_exec($cmd." 2>&1");
  194 + //print($res);
  195 + //print($cmd);
  196 + //exit;
  197 +
192 198 }
193   -
194   - # Check the tempfile exists and the python script did not return anything (which would indicate an error)
  199 +
  200 +# Check the tempfile exists and the python script did not return anything (which would indicate an error)
195 201 if (file_exists($sTempFilename) && $res == '') {
196 202  
197 203 $sUrlEncodedFileName = substr($oDocument->getFileName(), 0, strrpos($oDocument->getFileName(), '.') );
... ... @@ -207,27 +213,27 @@ class PDFGeneratorAction extends KTDocumentAction {
207 213 header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
208 214 header("Cache-Control: must-revalidate");
209 215  
210   - # Get a filelike object and send it to the browser
  216 +# Get a filelike object and send it to the browser
211 217 $oFile = new KTFSFileLike($sTempFilename);
212 218 KTFileLikeUtil::send_contents($oFile);
213   - # Remove the tempfile
  219 +# Remove the tempfile
214 220 unlink($sTempFilename);
215 221  
216   - # Create the document transaction
  222 +# Create the document transaction
217 223 $oDocumentTransaction = & new DocumentTransaction($oDocument, 'Document downloaded as PDF', 'ktcore.transactions.download', $aOptions);
218 224 $oDocumentTransaction->create();
219   - # Just stop here - the content has already been sent.
  225 +# Just stop here - the content has already been sent.
220 226 exit(0);
221   -
  227 +
222 228 } else {
223   - # Set the error messsage and redirect to view document
  229 +# Set the error messsage and redirect to view document
224 230 $this->addErrorMessage(_kt('An error occured generating the PDF - please contact the system administrator.'));
225 231 redirect(generateControllerLink('viewDocument',sprintf(_kt('fDocumentId=%d'),$oDocument->getId())));
226 232 exit(0);
227 233 }
228   -
  234 +
229 235 } else {
230   - # Set the error messsage and redirect to view document
  236 +# Set the error messsage and redirect to view document
231 237 $this->addErrorMessage(_kt('An error occured generating the PDF - please contact the system administrator.'));
232 238 redirect(generateControllerLink('viewDocument',sprintf(_kt('fDocumentId=%d'),$oDocument->getId())));
233 239 exit(0);
... ... @@ -237,9 +243,9 @@ class PDFGeneratorAction extends KTDocumentAction {
237 243 }
238 244  
239 245 /**
240   - * Method for duplicating the document as a pdf.
241   - *
242   - */
  246 + * Method for duplicating the document as a pdf.
  247 + *
  248 + */
243 249 function do_pdfduplicate() {
244 250  
245 251 $this->oPage->setBreadcrumbDetails(_kt('Generate PDF'));
... ... @@ -248,18 +254,18 @@ class PDFGeneratorAction extends KTDocumentAction {
248 254 $oForm = $this->form_main();
249 255  
250 256 $oTemplate->setData(array(
251   - 'context' => &$this,
252   - 'form' => $oForm,
253   - ));
  257 + 'context' => &$this,
  258 + 'form' => $oForm,
  259 + ));
254 260 $this->addErrorMessage(_kt('NOT IMPLEMENTED YET: This will create a pdf copy of the document as a new document.'));
255 261 return $oTemplate->render();
256   -
  262 +
257 263 }
258 264  
259 265 /**
260   - * Method for replacing the document as a pdf.
261   - *
262   - */
  266 + * Method for replacing the document as a pdf.
  267 + *
  268 + */
263 269 function do_pdfreplace() {
264 270  
265 271 $this->oPage->setBreadcrumbDetails(_kt('Generate PDF'));
... ... @@ -268,12 +274,12 @@ class PDFGeneratorAction extends KTDocumentAction {
268 274 $oForm = $this->form_main();
269 275  
270 276 $oTemplate->setData(array(
271   - 'context' => &$this,
272   - 'form' => $oForm,
273   - ));
  277 + 'context' => &$this,
  278 + 'form' => $oForm,
  279 + ));
274 280 $this->addErrorMessage(_kt('NOT IMPLEMENTED YET: This will replace the document with a pdf copy of the document.'));
275 281 return $oTemplate->render();
276   -
  282 +
277 283 }
278 284 }
279 285 ?>
... ...
plugins/ktstandard/PDFGeneratorPlugin.php
1 1 <?php
2 2 /**
  3 + * $Id$
3 4 *
4   - * Copyright (c) 2007 Jam Warehouse http://www.jamwarehouse.com
  5 + * The contents of this file are subject to the KnowledgeTree Public
  6 + * License Version 1.1 ("License"); You may not use this file except in
  7 + * compliance with the License. You may obtain a copy of the License at
  8 + * http://www.knowledgetree.com/KPL
  9 + *
  10 + * Software distributed under the License is distributed on an "AS IS"
  11 + * basis,
  12 + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  13 + * for the specific language governing rights and limitations under the
  14 + * License.
  15 + *
  16 + * The Original Code is: KnowledgeTree Open Source
  17 + *
  18 + * The Initial Developer of the Original Code is The Jam Warehouse Software
  19 + * (Pty) Ltd, trading as KnowledgeTree.
  20 + * Portions created by The Jam Warehouse Software (Pty) Ltd are Copyright
  21 + * (C) 2007 The Jam Warehouse Software (Pty) Ltd;
  22 + * All Rights Reserved.
5 23 *
6   - * This program is free software; you can redistribute it and/or modify
7   - * it under the terms of the GNU General Public License as published by
8   - * the Free Software Foundation; using version 2 of the License.
9   - *
10   - * This program is distributed in the hope that it will be useful,
11   - * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   - * GNU General Public License for more details.
14   - *
15   - * You should have received a copy of the GNU General Public License
16   - * along with this program; if not, write to the Free Software
17   - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18   - *
19   - * -------------------------------------------------------------------------
20   - *
21   - * You can contact the copyright owner regarding licensing via the contact
22   - * details that can be found on the KnowledgeTree web site:
23   - *
24   - * http://www.knowledgetree.com/
25 24 */
26 25  
27 26 require_once(KT_LIB_DIR . "/plugins/plugin.inc.php");
... ...