1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @author Matthias Glaub <[email protected]> |
5
|
|
|
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace MaglLegacyApplication\Controller; |
9
|
|
|
|
10
|
|
|
use MaglLegacyApplication\Application\MaglLegacy; |
11
|
|
|
use MaglLegacyApplication\Options\LegacyControllerOptions; |
12
|
|
|
use Zend\Http\Response; |
13
|
|
|
use Zend\Mvc\Controller\AbstractActionController; |
14
|
|
|
|
15
|
|
|
class LegacyController extends AbstractActionController |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* |
20
|
|
|
* @var LegacyControllerOptions |
21
|
|
|
*/ |
22
|
|
|
private $options; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* |
26
|
|
|
* @var MaglLegacy |
27
|
|
|
*/ |
28
|
|
|
private $legacy; |
29
|
|
|
|
30
|
4 |
|
public function __construct(LegacyControllerOptions $options, MaglLegacy $legacy) |
31
|
|
|
{ |
32
|
4 |
|
$this->options = $options; |
33
|
4 |
|
$this->legacy = $legacy; |
34
|
4 |
|
} |
35
|
|
|
|
36
|
4 |
|
public function indexAction() |
37
|
|
|
{ |
38
|
4 |
|
$docRoots = $this->options->getDocRoots(); |
39
|
4 |
|
foreach($docRoots as $key => $docRoot) { |
|
|
|
|
40
|
4 |
|
$docRoots[$key] = rtrim(getcwd() . '/' . $docRoot); |
41
|
4 |
|
} |
42
|
|
|
|
43
|
4 |
|
$scriptName = $this->params('script'); |
44
|
|
|
|
45
|
4 |
|
if (empty($scriptName)) { |
46
|
|
|
$path = $this->params(('path')) ? $this->params('path') : ''; |
47
|
|
|
foreach ($this->options->getIndexFiles() as $indexFile) { |
48
|
|
|
foreach($docRoots as $docRoot) { |
|
|
|
|
49
|
|
|
if (file_exists($docRoot . '/' . $path . $indexFile)) { |
50
|
|
|
return $this->runScript($docRoot . '/' . $path . $indexFile); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
|
57
|
4 |
|
$scriptUri = '/' . ltrim($scriptName, '/'); // force leading '/' |
58
|
4 |
|
foreach($docRoots as $docRoot) { |
|
|
|
|
59
|
4 |
|
$legacyScriptFilename = $docRoot . $scriptUri; |
60
|
4 |
|
if(file_exists($legacyScriptFilename)) { |
|
|
|
|
61
|
|
|
//inform the application about the used script |
62
|
3 |
|
$this->legacy->setLegacyScriptFilename($legacyScriptFilename); |
63
|
3 |
|
$this->legacy->setLegacyScriptName($scriptUri); |
64
|
|
|
|
65
|
|
|
//inject get and request variables |
66
|
3 |
|
$this->setGetVariables(); |
67
|
|
|
|
68
|
3 |
|
return $this->runScript($legacyScriptFilename); |
69
|
|
|
} |
70
|
2 |
|
} |
71
|
|
|
|
72
|
|
|
// if we're here, the file doesn't really exist and we do not know what to do |
73
|
1 |
|
$response = $this->getResponse(); |
74
|
|
|
|
75
|
|
|
/* @var $response Response */ //<-- this one for netbeans (WHY, NetBeans, WHY??) |
76
|
|
|
/** @var Response $response */ // <-- this one for other IDEs and code analyzers :) |
77
|
1 |
|
$response->setStatusCode(404); |
78
|
1 |
|
return $response; |
|
|
|
|
79
|
|
|
} |
80
|
|
|
|
81
|
3 |
|
private function setGetVariables() |
82
|
|
|
{ |
83
|
3 |
|
$globals_options = $this->options->getGlobals(); |
84
|
|
|
|
85
|
|
|
// if we should not set any global vars, we can return safely |
86
|
3 |
|
if (!$globals_options['get'] && !$globals_options['request']) { |
87
|
|
|
return; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
// check if $_GET is used at all (ini - variables_order ?) |
91
|
|
|
// check if $_GET is written to $_REQUEST (ini - variables_order / request_order) |
92
|
|
|
// depending on request_order, check if $_REQUEST is already written and decide if we are allowed to override |
93
|
|
|
|
94
|
3 |
|
$request_order = ini_get('request_order'); |
95
|
|
|
|
96
|
3 |
|
if ($request_order === false) { |
97
|
|
|
$request_order = ini_get('variables_order'); |
98
|
|
|
} |
99
|
|
|
|
100
|
3 |
|
$get_prio = stripos($request_order, 'g'); |
101
|
3 |
|
$post_prio = stripos($request_order, 'p'); |
102
|
|
|
|
103
|
3 |
|
$forceOverrideRequest = $get_prio > $post_prio; |
104
|
|
|
|
105
|
3 |
|
$routeParams = $this->getEvent()->getRouteMatch()->getParams(); |
106
|
|
|
|
107
|
3 |
|
foreach ($routeParams as $paramName => $paramValue) { |
|
|
|
|
108
|
|
|
|
109
|
3 |
|
if ($globals_options['get'] && !isset($_GET[$paramName])) { |
110
|
3 |
|
$_GET[$paramName] = $paramValue; |
111
|
3 |
|
} |
112
|
|
|
|
113
|
3 |
|
if ($globals_options['request'] && ($forceOverrideRequest || !isset($_REQUEST[$paramName]))) { |
114
|
3 |
|
$_REQUEST[$paramName] = $paramValue; |
115
|
3 |
|
} |
116
|
3 |
|
} |
117
|
3 |
|
} |
118
|
|
|
|
119
|
3 |
|
private function runScript($scriptFileName) |
120
|
|
|
{ |
121
|
3 |
|
ob_start(); |
122
|
3 |
|
include $scriptFileName; |
123
|
3 |
|
$output = ob_get_clean(); |
124
|
|
|
|
125
|
3 |
|
$result = $this->getEventManager()->trigger(MaglLegacy::EVENT_SHORT_CIRCUIT_RESPONSE, $this); |
126
|
3 |
|
if ($result->stopped()) { |
127
|
|
|
return $result->last(); |
128
|
|
|
} |
129
|
|
|
|
130
|
3 |
|
$this->getResponse()->setContent($output); |
131
|
3 |
|
return $this->getResponse(); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|