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