Initial revision
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_svbase.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 1999-2003 Kasper Skårhøj (kasper@typo3.com)
6 * All rights reserved
7 *
8 * This script is part of the Typo3 project. The Typo3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27 /**
28 * Parent class for "Services" classes
29 *
30 * TODO: temp files are not removed
31 *
32 * @author René Fritz <r.fritz@colorcube.de>
33 */
34
35 define ('T3_ERR_SV_GENERAL', -1); // General error - something went wrong
36 define ('T3_ERR_SV_NOT_AVAIL', -2); // During execution it showed that the service is not available and should be ignored. The service itself should call $this->setNonAvailable()
37 define ('T3_ERR_SV_WRONG_SUBTYPE', -3); // passed subtype is not possible with this service
38 define ('T3_ERR_SV_NO_INPUT', -4); // passed subtype is not possible with this service
39
40
41 define ('T3_ERR_SV_FILE_NOT_FOUND', -20); // File not found which the service should process
42 define ('T3_ERR_SV_FILE_READ', -21); // File not readable
43 define ('T3_ERR_SV_FILE_WRITE', -22); // File not writeable
44
45 define ('T3_ERR_SV_PROG_NOT_FOUND', -40); // passed subtype is not possible with this service
46 define ('T3_ERR_SV_PROG_FAILED', -41); // passed subtype is not possible with this service
47
48 // define ('T3_ERR_SV_serviceType_myerr, 100); // All errors with prefix T3SV_ERR_serviceType_ and beginning with number 100 are service type dependent error
49
50
51 require_once(PATH_t3lib.'class.t3lib_exec.php');
52
53 class t3lib_svbase {
54
55 /**
56 * service description array
57 */
58 var $info=array();
59
60 /**
61 * error stack
62 */
63 var $error=array();
64
65
66
67
68 /**
69 * The output content.
70 * That's what the services produced as result.
71 */
72 var $out = '';
73
74 /**
75 * The file that should be processed.
76 */
77 var $inputFile = '';
78
79 /**
80 * The content that should be processed.
81 */
82 var $inputContent = '';
83
84 /**
85 * The type of the input content (or file). Might be the same as the service subtypes.
86 */
87 var $inputType = '';
88
89 /**
90 * The file where the output should be written to.
91 */
92 var $outputFile = '';
93
94
95
96
97 /***************************************
98 *
99 * Get service meta information
100 *
101 ***************************************/
102
103
104 /**
105 * @return array service description array
106 */
107 function getServiceInfo() {
108 return $this->info;
109 }
110
111
112 /**
113 * @return string service key
114 */
115 function getServiceKey() {
116 return $this->info['serviceKey'];
117 }
118
119
120 /**
121 * @return string service title
122 */
123 function getServiceTitle() {
124 return $this->info['title'];
125 }
126
127
128
129
130 /***************************************
131 *
132 * Error handling
133 *
134 ***************************************/
135
136
137 /**
138 * Puts an error on the error stack. Calling without parameter adds a general error.
139 *
140 * @param string error message
141 * @param string error number (see T3_ERR_SV_* constants)
142 * @return void
143 */
144 function errorPush($errNum=T3_ERR_SV_GENERAL, $errMsg='Unspecified error occured') {
145 array_push($this->error, array('nr'=>$errNum, 'msg'=>$errMsg));
146
147 if (is_object($GLOBALS["TT"])) {
148 $GLOBALS['TT']->setTSlogMessage($errMsg,2);
149 }
150
151 }
152
153
154 /**
155 * Removes the last error from the error stack.
156 *
157 * @return void
158 */
159 function errorPull() {
160 array_pop($this->error);
161
162 // pop for $GLOBALS['TT']->setTSlogMessage is not supported
163 }
164
165
166 /**
167 * Returns the last error number from the error stack.
168 *
169 * @return string error number
170 */
171 function getLastError() {
172 if(count($this->error)) {
173 $error = end($this->error);
174 return $error['nr'];
175 } else {
176 return TRUE; // means all is ok - no error
177 }
178 }
179
180
181 /**
182 * Returns the last message from the error stack.
183 *
184 * @return string error message
185 */
186 function getLastErrorMsg() {
187 if(count($this->error)) {
188 $error = end($this->error);
189 return $error['msg'];
190 } else {
191 return '';
192 }
193 }
194
195
196 /**
197 * Returns all error messages as array.
198 *
199 * @return array error messages
200 */
201 function getErrorMsgArray() {
202 $errArr = array();
203
204 if(count($this->error)) {
205 reset($this->error);
206 foreach($this->error as $error) {
207 $errArr[] = $error['msg'];
208 }
209 }
210 return $errArr;
211 }
212
213
214 /**
215 * Returns the last array from the error stack.
216 *
217 * @return array error nr and message
218 */
219 function getLastErrorArray() {
220 return end($this->error);
221 }
222
223 /**
224 * Reset the error stack.
225 *
226 * @return void
227 */
228 function resetErrors() {
229 $this->error=array();
230 }
231
232
233
234 /***************************************
235 *
236 * General service functions
237 *
238 ***************************************/
239
240
241
242 /**
243 * check the availability of external programs
244 *
245 * @param string comma list of programs 'perl,python,pdftotext'
246 * @return boolean return FALSE if one program was not found
247 */
248 function checkExec($progList) {
249 $ret = TRUE;
250
251 require_once(PATH_t3lib."class.t3lib_exec.php");
252
253 $progList = t3lib_div::trimExplode(',', $progList, 1);
254 foreach($progList as $prog) {
255 if (!t3lib_exec::checkCommand($prog)) {
256 // program not found
257 $this->errorPush('External program not found: '.$prog, T3_ERR_SV_PROG_NOT_FOUND);
258 $ret = FALSE;
259 }
260 }
261 return $ret;
262 }
263
264
265 /**
266 * Deactivate the service. Use this if the service fails at runtime and will not be available.
267 *
268 * @return void
269 */
270 function deactivateService() {
271 t3lib_extMgm::deactivateService($this->info['serviceType'], $this->info['serviceKey']);
272 }
273
274
275 /**
276 function available() {
277 global $AB,$BE_USER,$LANG,$BACK_PATH,$TCA_DESCR,$TCA,$HTTP_GET_VARS,$HTTP_POST_VARS,$CLIENT,$TYPO3_CONF_VARS;
278
279 // check if the service is available at runtime
280 // the sense of this method is that the service might need some more information to check the availablity
281
282 / *
283
284 $excludeServiceKeys='';
285 while (is_object($serviceObj = t3lib_div::makeInstanceService('anyService','', $excludeServiceKeys))) {
286 if ($serviceObj->available('some special parm to check availablity')) {
287 break;
288 }
289 $excludeServiceKeys .= ','.$serviceObj->getServiceKey;
290 unset($serviceObj);
291 }
292
293 * /
294
295 return TRUE;
296 }
297 */
298
299
300 /***************************************
301 *
302 * IO tools
303 *
304 ***************************************/
305
306
307
308 /**
309 * Check if a file exists and is readable.
310 *
311 * @param string File name with absolute path.
312 * @return string File name or FALSE.
313 */
314 function checkInputFile ($absFile) {
315 if(@is_file($absFile)) {
316 if(@is_readable($absFile)) {
317 return $absFile;
318 } else {
319 $this->errorPush(T3_ERR_SV_FILE_READ, 'File is not readable: '.$absFile);
320 }
321 } else {
322 $this->errorPush(T3_ERR_SV_FILE_NOT_FOUND, 'File not found: '.$absFile);
323 }
324 return FALSE;
325 }
326
327
328 /**
329 * Read content from a file a file.
330 *
331 * @param string File name to read from.
332 * @param integer Maximum length to read. If empty the whole file will be read.
333 * @return string $content or FALSE
334 */
335 function readFile ($absFile, $length=0) {
336 $out = FALSE;
337
338 if ($this->checkInputFile ($absFile)) {
339 if ($fd = fopen ($absFile, 'rb')) {
340 $length = intval($length) ? intval($length) : filesize ($absFile);
341 $out = fread ($fd, $length);
342 fclose ($fd);
343 } else {
344 $this->errorPush(T3_ERR_SV_FILE_READ, 'Can not read from file: '.$absFile);
345 }
346 }
347 return $out;
348 }
349
350
351 /**
352 * Write content to a file.
353 *
354 * @param string Content to write to the file
355 * @param string File name to write into. If empty a temp file will be created.
356 * @return string File name or FALSE
357 */
358 function writeFile (&$content, $absFile='') {
359 $ret = TRUE;
360
361 if (!$absFile) {
362 $absFile = tempnam ('', $this->prefixId);
363 if(!$absFile) {
364 $this->errorPush(T3_ERR_SV_FILE_WRITE, 'Can not create temp file.');
365 $ret = FALSE;
366 }
367 }
368
369 if($absFile) {
370 if ($fd = @fopen($absFile,'wb')) {
371 @fwrite($fd, $content);
372 @fclose($fd);
373 } else {
374 $this->errorPush(T3_ERR_SV_FILE_WRITE, 'Can not write to file: '.$absFile);
375 $ret = FALSE;
376 }
377 }
378
379 return ($ret ? $absFile : FALSE);
380 }
381
382
383
384
385 /***************************************
386 *
387 * IO input
388 *
389 ***************************************/
390
391
392 /**
393 * Set the input content for service processing.
394 *
395 * @param mixed
396 */
397 function setInput ($content, $type='') {
398 $this->inputContent = $content;
399 $this->inputFile = '';
400 $this->inputType = $type;
401 }
402
403
404 /**
405 * Set the input file name for service processing.
406 *
407 * @param string file name
408 */
409 function setInputFile ($absFile, $type='') {
410 $this->inputContent = '';
411 $this->inputFile = $absFile;
412 $this->inputType = $type;
413 }
414
415
416 /**
417 * Get the input content.
418 * Will be read from input file if needed.
419 *
420 * @return mixed
421 */
422 function getInput () {
423 if ($this->inputContent=='') {
424 $this->inputContent = $this->readFile($this->inputFile);
425 }
426 return $this->inputContent;
427 }
428
429
430 /**
431 * Get the input file name.
432 * If the content was set by setContent a file will be created.
433 *
434 * @param string File name. If empty a temp file will be created.
435 * @return string File name or FALSE if no input or file error.
436 */
437 function getInputFile ($absFile='') {
438 if($this->inputFile) {
439 $this->inputFile = $this->checkInputFile($this->inputFile);
440 } elseif ($this->inputContent) {
441 $this->inputFile = $this->writeFile($this->inputContent, $absFile);
442 }
443 return $this->inputFile;
444 }
445
446
447
448
449 /***************************************
450 *
451 * IO output
452 *
453 ***************************************/
454
455
456 /**
457 * Set the output file name.
458 *
459 * @param string file name
460 */
461 function setOutputFile ($absFile) {
462 $this->outputFile = $absFile;
463 }
464
465
466 /**
467 * Get the output content.
468 *
469 * @return mixed
470 */
471 function getOutput () {
472 if ($this->outputFile) {
473 $this->out = $this->readFile($this->outputFile);
474 }
475 return $this->out;
476 }
477
478
479 /**
480 * Get the output file name.
481 *
482 * @return mixed
483 */
484 function getOutputFile ($absFile='') {
485 if (!$this->outputFile) {
486 $this->outputFile = $this->writeFile($this->out, $absFile);
487 }
488 return $this->outputFile;
489 }
490
491
492
493
494 /***************************************
495 *
496 * Service implementation
497 *
498 ***************************************/
499
500 /**
501 * Initialization of the service.
502 *
503 * The class have to do a strict check if the service is available.
504 * example: check if the perl interpreter is available which is needed to run an extern perl script.
505 *
506 * @return boolean TRUE if the service is available
507 */
508 function init() {
509 $this->reset();
510
511 // check for external programs which are defined by $info['exec']
512 if (trim($this->info['exec'])) {
513 if (!$this->checkExec($this->info['exec'])) {
514 }
515 }
516
517 return $this->getLastError();
518 }
519
520
521 /**
522 * Resets the service.
523 * Will be called by init(). Should be used before every use if a service instance is used multiple times.
524 *
525 * @return void
526 */
527 function reset() {
528 $this->resetErrors();
529 $this->out = '';
530 $this->inputFile = '';
531 $this->inputContent = '';
532 $this->inputType = '';
533 $this->outputFile = '';
534 }
535
536
537 /* every service type has it's own API
538 function process($content='', $type='', $conf=array()) {
539 }
540 */
541
542 }
543
544 /**
545 // Does not make sense, because this class is always extended by the service classes..
546 if (defined("TYPO3_MODE") && $TYPO3_CONF_VARS[TYPO3_MODE]["XCLASS"]["t3lib/class.t3lib_svbase.php"]) {
547 include_once($TYPO3_CONF_VARS[TYPO3_MODE]["XCLASS"]["t3lib/class.t3lib_svbase.php"]);
548 }
549 */
550
551 ?>
552