class.tx_ter_pi1.php 4.85 KB
Newer Older
1
<?php
2
/*
3 4 5 6 7 8 9 10 11 12 13
 * This file is part of the TYPO3 CMS project.
 *
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
 *
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
 *
 * The TYPO3 project - inspiring people to share!
 */
14

15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
/**
 * SOAP server for the TYPO3 Extension Repository
 *
 * $Id$
 *
 * @author    Robert Lemke <robert@typo3.org>
 */
/**
 * [CLASS/FUNCTION INDEX of SCRIPT]
 *
 *
 *
 *   54: class tx_ter_pi1 extends tslib_pibase
 *   61:     function main ($content, $conf)
 *
 * TOTAL FUNCTIONS: 1
 * (This index is automatically created/updated by the extension "extdeveval")
 */
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
use TYPO3\CMS\Frontend\Plugin\AbstractPlugin;

require_once(ExtensionManagementUtility::extPath('ter') . 'class.tx_ter_api.php');

/**
 * TYPO3 Extension Repository, frontend plugin for SOAP service
 *
 * @author    Robert Lemke <robert@typo3.org>
 */
class tx_ter_pi1 extends AbstractPlugin
{
Jens Jacobsen's avatar
Jens Jacobsen committed
45 46 47 48 49
    public $cObj;          // Standard cObj (parent)
    public $extensionsPID; // Start page for extension records
    public $wsdlNamespace; // Namespace
    public $repositoryDir; // Absolute path to extension repository directory
    public $conf;          // The FE Plugin's TS configuration
50

51
    public function main($content, $conf)
52 53 54 55 56 57 58
    {
        global $TSFE;

        $this->pi_initPIflexForm();
        $this->conf = $conf;

        $this->extensionsPID = $conf['pid'];
Jens Jacobsen's avatar
Jens Jacobsen committed
59
        $this->wsdlNamespace = $conf['wsdlNamespace'];
60 61 62 63 64 65 66 67 68
        $staticConfArr = unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf']['ter']);
        if (is_array($staticConfArr)) {
            $this->repositoryDir = $staticConfArr['repositoryDir'];
            if (substr($this->repositoryDir, -1, 1) != '/') {
                $this->repositoryDir .= '/';
            }
        }

        try {
69
            $server = new SoapServer(null, ['uri' => $this->wsdlNamespace, 'trace' => true, 'exceptions' => true]);
Jens Jacobsen's avatar
Jens Jacobsen committed
70
            $server->setClass(\tx_ter_api::class, $this);
71
            $server->handle(file_get_contents('php://input'));
72 73
            // Due to different behaviour on stage and prod server we need this exit to prevent wrong XML response
            exit();
74
        } catch (\T3o\Ter\Exception\Exception $e) {
75 76 77 78 79 80 81 82 83 84 85
            /**
             * @author Christian Zenker <christian.zenker@599media.de>
             * @see http://forge.typo3.org/issues/44135
             *
             * SoapServer is a little nasty. When you throw a SoapFault (extends Exception) inside a soap call,
             * you won't have any possible chance of catching and handling that exception. SoapServer will instantly
             * return a 500 InternalServerError without any way of intervention.
             *
             * That's why there is an own set of exceptions defined that basically stand for a status code.
             */
            $statusCode = 404;
86
            if ($e instanceof \T3o\Ter\Exception\UnauthorizedException) {
87
                $statusCode = 401;
88
            } elseif ($e instanceof \T3o\Ter\Exception\NotFoundException) {
89
                $statusCode = 404;
90
            } elseif ($e instanceof \T3o\Ter\Exception\FailedDependencyException) {
91
                $statusCode = 424;
92
            } elseif ($e instanceof \T3o\Ter\Exception\InternalServerErrorException) {
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
                $statusCode = 500;
                error_log(sprintf('TER Server internal error occurred. Error message is: "%s"', $e->getMessage()));
            }
            header(' ', true, $statusCode);
            /**
             * Using $server->fault will cause a http 500 status code to be sent which in turn
             * will trigger the web server's error page to be shown, instead of the SOAP XML
             * The header sent above will just be ignored.
             * Because of that, we forge a SOAP Fault XML ourselves and just echo it.
             *
             * flush() to prevent SoapServer to change the status code obviously also does not work
             * This fails when some other kind of output buffering is in place (e.g. for gzip compression)
             * in the web server.
             */
            // flush to prevent Soap to change the status code (does not work)
            // flush();
            // $server->fault($e->getCode(), $e->getMessage());
            $faultStringXmlTemplate = '<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
	<SOAP-ENV:Body>
		<SOAP-ENV:Fault>
			<faultcode>%s</faultcode>
			<faultstring>%s</faultstring>
		</SOAP-ENV:Fault>
	</SOAP-ENV:Body>
</SOAP-ENV:Envelope>';
            echo sprintf($faultStringXmlTemplate, $e->getCode(), $e->getMessage());
            exit;
        }

        return '';
    }
}