Completed
Push — transform-to-laminas3 ( 1cf951 )
by
unknown
10:00
created

LegacyController::runScript()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 8.8817
c 0
b 0
f 0
cc 6
nc 5
nop 1
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 Laminas\Http\Response;
11
use Laminas\Mvc\Controller\AbstractActionController;
12
use Laminas\View\Model\ViewModel;
13
use MaglLegacyApplication\Application\MaglLegacy;
14
use MaglLegacyApplication\Options\LegacyControllerOptions;
15
16
class LegacyController extends AbstractActionController
17
{
18
19
    /**
20
     *
21
     * @var LegacyControllerOptions
22
     */
23
    private $options;
24
25
    /**
26
     *
27
     * @var MaglLegacy
28
     */
29
    private $legacy;
30
31
    public function __construct(LegacyControllerOptions $options, MaglLegacy $legacy)
32
    {
33
        $this->options = $options;
34
        $this->legacy = $legacy;
35
    }
36
37
    public function indexAction()
38
    {
39
        $docRoots = $this->options->getDocRoots();
40
        foreach ($docRoots as $key => $docRoot) {
41
            $docRoots[$key] = rtrim(getcwd() . '/' . $docRoot);
42
        }
43
44
        $scriptName = $this->params('script');
45
46
        if (empty($scriptName)) {
47
            $path = $this->params(('path')) ? $this->params('path') : '';
48
            foreach ($this->options->getIndexFiles() as $indexFile) {
49
                foreach ($docRoots as $docRoot) {
50
                    if (is_file($docRoot . '/' . $path . $indexFile)) {
51
                        $this->legacy->setLegacyScriptName($path . $indexFile);
52
                        return $this->runScript($docRoot . '/' . $path . $indexFile);
53
                    }
54
                }
55
            }
56
        }
57
58
        $scriptUri = '/' . ltrim($scriptName, '/'); // force leading '/'
59
        foreach ($docRoots as $docRoot) {
60
            $legacyScriptFilename = $docRoot . $scriptUri;
61
            if (is_file($legacyScriptFilename)) {
62
                //inform the application about the used script
63
                $this->legacy->setLegacyScriptName($scriptUri);
64
65
                //inject get and request variables
66
                $this->setGetVariables();
67
68
                return $this->runScript($legacyScriptFilename);
69
            }
70
        }
71
72
        /** @var Response $response */
73
        $response = $this->getResponse();
74
        $response->setStatusCode(404);
75
    }
76
77
    private function setGetVariables()
78
    {
79
        $globals_options = $this->options->getGlobals();
80
81
        // if we should not set any global vars, we can return safely
82
        if (!$globals_options['get'] && !$globals_options['request']) {
83
            return;
84
        }
85
86
        // check if $_GET is used at all (ini - variables_order ?)
87
        // check if $_GET is written to $_REQUEST (ini - variables_order / request_order)
88
        // depending on request_order, check if $_REQUEST is already written and decide if we are allowed to override
89
90
        $request_order = ini_get('request_order');
91
92
        if ($request_order === false) {
93
            $request_order = ini_get('variables_order');
94
        }
95
96
        $get_prio = stripos($request_order, 'g');
97
        $post_prio = stripos($request_order, 'p');
98
99
        $forceOverrideRequest = $get_prio > $post_prio;
100
101
        $routeParams = $this->getEvent()->getRouteMatch()->getParams();
102
103
        foreach ($routeParams as $paramName => $paramValue) {
104
            if ($globals_options['get'] && !isset($_GET[$paramName])) {
105
                $_GET[$paramName] = $paramValue;
106
            }
107
108
            if ($globals_options['request'] && ($forceOverrideRequest || !isset($_REQUEST[$paramName]))) {
109
                $_REQUEST[$paramName] = $paramValue;
110
            }
111
        }
112
    }
113
114
    private function runScript($scriptFileName)
115
    {
116
        $this->legacy->setLegacyScriptFilename($scriptFileName);
117
118
        ob_start();
119
        $result = include $scriptFileName;
120
        $output = ob_get_clean();
121
122
        if ($result instanceof ViewModel || $result instanceof Response) {
123
            if ($this->options->getPrependOutputBufferToResponse()) {
124
                echo $output;
125
            }
126
            return $result;
127
        }
128
129
        $result = $this->getEventManager()->trigger(MaglLegacy::EVENT_SHORT_CIRCUIT_RESPONSE, $this);
130
        if ($result->stopped()) {
131
            if ($this->options->getPrependOutputBufferToResponse()) {
132
                $result->last()->setContent($output . $result->last()->getContent());
133
            }
134
            return $result->last();
135
        }
136
137
        $this->getResponse()->setContent($output);
138
        return $this->getResponse();
139
    }
140
}
141