TYPO3 CMS  TYPO3_7-6
RequestBootstrap.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
21 {
25  public static function setGlobalVariables(array $requestArguments = null)
26  {
27  if (empty($requestArguments)) {
28  die('No JSON encoded arguments given');
29  }
30 
31  if (empty($requestArguments['documentRoot'])) {
32  die('No documentRoot given');
33  }
34 
35  if (empty($requestArguments['requestUrl']) || ($requestUrlParts = parse_url($requestArguments['requestUrl'])) === false) {
36  die('No valid request URL given');
37  }
38 
39  // Populating $_GET and $_REQUEST is query part is set:
40  if (isset($requestUrlParts['query'])) {
41  parse_str($requestUrlParts['query'], $_GET);
42  parse_str($requestUrlParts['query'], $_REQUEST);
43  }
44 
45  // Populating $_POST
46  $_POST = [];
47  // Populating $_COOKIE
48  $_COOKIE = [];
49 
50  // Setting up the server environment
51  $_SERVER = [];
52  $_SERVER['DOCUMENT_ROOT'] = $requestArguments['documentRoot'];
53  $_SERVER['HTTP_USER_AGENT'] = 'TYPO3 Functional Test Request';
54  $_SERVER['HTTP_HOST'] = $_SERVER['SERVER_NAME'] = isset($requestUrlParts['host']) ? $requestUrlParts['host'] : 'localhost';
55  $_SERVER['SERVER_ADDR'] = $_SERVER['REMOTE_ADDR'] = '127.0.0.1';
56  $_SERVER['SCRIPT_NAME'] = $_SERVER['PHP_SELF'] = '/index.php';
57  $_SERVER['SCRIPT_FILENAME'] = $_SERVER['_'] = $_SERVER['PATH_TRANSLATED'] = $requestArguments['documentRoot'] . '/index.php';
58  $_SERVER['QUERY_STRING'] = (isset($requestUrlParts['query']) ? $requestUrlParts['query'] : '');
59  $_SERVER['REQUEST_URI'] = $requestUrlParts['path'] . (isset($requestUrlParts['query']) ? '?' . $requestUrlParts['query'] : '');
60  $_SERVER['REQUEST_METHOD'] = 'GET';
61 
62  // Define HTTPS and server port:
63  if (isset($requestUrlParts['scheme'])) {
64  if ($requestUrlParts['scheme'] === 'https') {
65  $_SERVER['HTTPS'] = 'on';
66  $_SERVER['SERVER_PORT'] = '443';
67  } else {
68  $_SERVER['SERVER_PORT'] = '80';
69  }
70  }
71 
72  // Define a port if used in the URL:
73  if (isset($requestUrlParts['port'])) {
74  $_SERVER['SERVER_PORT'] = $requestUrlParts['port'];
75  }
76 
77  if (!is_dir($_SERVER['DOCUMENT_ROOT'])) {
78  die('Document root directory "' . $_SERVER['DOCUMENT_ROOT'] . '" does not exist');
79  }
80 
81  if (!is_file($_SERVER['SCRIPT_FILENAME'])) {
82  die('Script file "' . $_SERVER['SCRIPT_FILENAME'] . '" does not exist');
83  }
84  }
85 
89  public static function executeAndOutput()
90  {
91  global $TT, $TSFE, $TYPO3_CONF_VARS, $BE_USER, $TYPO3_MISC;
92 
93  $result = ['status' => 'failure', 'content' => null, 'error' => null];
94 
95  ob_start();
96  try {
97  chdir($_SERVER['DOCUMENT_ROOT']);
98  include($_SERVER['SCRIPT_FILENAME']);
99  $result['status'] = 'success';
100  $result['content'] = ob_get_contents();
101  } catch (\Exception $exception) {
102  $result['error'] = $exception->__toString();
103  }
104  ob_end_clean();
105 
106  echo json_encode($result);
107  }
108 }