~xibo-maintainers/xibo/tempel

« back to all changes in this revision

Viewing changes to lib/Controller/Fault.php

  • Committer: Dan Garner
  • Date: 2016-02-18 16:07:16 UTC
  • mfrom: (454.4.137)
  • Revision ID: git-v1:8867f12675bc9e0e67e7e622c80da7471b9f294a
Merge pull request #139 from dasgarner/feature/nested-display-groups

Feature/nested display groups

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
namespace Xibo\Controller;
22
22
 
23
23
use Xibo\Exception\AccessDeniedException;
24
 
use Xibo\Factory\DisplayFactory;
25
24
use Xibo\Factory\LogFactory;
26
 
use Xibo\Helper\Random;
27
 
use Xibo\Service\ConfigServiceInterface;
28
 
use Xibo\Service\DateServiceInterface;
29
 
use Xibo\Service\LogServiceInterface;
30
 
use Xibo\Service\SanitizerServiceInterface;
31
 
use Xibo\Storage\StorageServiceInterface;
 
25
use Xibo\Helper\Config;
32
26
 
33
 
/**
34
 
 * Class Fault
35
 
 * @package Xibo\Controller
36
 
 */
37
27
class Fault extends Base
38
28
{
39
 
    /** @var  StorageServiceInterface */
40
 
    private $store;
41
 
 
42
 
    /**
43
 
     * @var LogFactory
44
 
     */
45
 
    private $logFactory;
46
 
 
47
 
    /** @var  DisplayFactory */
48
 
    private $displayFactory;
49
 
 
50
 
    /**
51
 
     * Set common dependencies.
52
 
     * @param LogServiceInterface $log
53
 
     * @param SanitizerServiceInterface $sanitizerService
54
 
     * @param \Xibo\Helper\ApplicationState $state
55
 
     * @param \Xibo\Entity\User $user
56
 
     * @param \Xibo\Service\HelpServiceInterface $help
57
 
     * @param DateServiceInterface $date
58
 
     * @param ConfigServiceInterface $config
59
 
     * @param StorageServiceInterface $store
60
 
     * @param LogFactory $logFactory
61
 
     * @param DisplayFactory $displayFactory
62
 
     */
63
 
    public function __construct($log, $sanitizerService, $state, $user, $help, $date, $config, $store, $logFactory, $displayFactory)
64
 
    {
65
 
        $this->setCommonDependencies($log, $sanitizerService, $state, $user, $help, $date, $config);
66
 
 
67
 
        $this->store = $store;
68
 
        $this->logFactory = $logFactory;
69
 
        $this->displayFactory = $displayFactory;
70
 
    }
71
 
 
72
29
    function displayPage()
73
30
    {
74
 
        $config = $this->getConfig();
 
31
        $config = new Config();
75
32
        $data = [
76
33
            'environmentCheck' => $config->CheckEnvironment(),
77
34
            'environmentFault' => $config->envFault,
78
 
            'environmentWarning' => $config->envWarning,
79
 
            'binLogError' => ($config->checkBinLogEnabled() && !$config->checkBinLogFormat())
 
35
            'environmentWarning' => $config->envWarning
80
36
        ];
81
37
 
82
38
        $this->getState()->template = 'fault-page';
85
41
 
86
42
    public function collect()
87
43
    {
 
44
        $out = fopen('php://output', 'w');
 
45
        fputcsv($out, ['logId', 'runNo', 'logDate', 'channel', 'page', 'function', 'message', 'display.display', 'type']);
 
46
 
 
47
        // Do some post processing
 
48
        foreach (LogFactory::query(['logId'], ['fromDt' => (time() - (60 * 10))]) as $row) {
 
49
            /* @var \Xibo\Entity\LogEntry $row */
 
50
            fputcsv($out, [$row->logId, $row->runNo, $row->logDate, $row->channel, $row->page, $row->function, $row->message, $row->display, $row->type]);
 
51
        }
 
52
 
 
53
        fclose($out);
 
54
 
 
55
        // We want to output a load of stuff to the browser as a text file.
 
56
        $app = $this->getApp();
 
57
        $app->response()->header('Content-Type', 'text/csv');
 
58
        $app->response()->header('Content-Disposition', 'attachment; filename="troubleshoot.csv"');
 
59
        $app->response()->header('Content-Transfer-Encoding', 'binary"');
 
60
        $app->response()->header('Accept-Ranges', 'bytes');
88
61
        $this->setNoOutput(true);
89
 
 
90
 
        // Create a ZIP file
91
 
        $tempFileName = $this->getConfig()->GetSetting('LIBRARY_LOCATION') . 'temp/' . Random::generateString();
92
 
        $zip = new \ZipArchive();
93
 
 
94
 
        $result = $zip->open($tempFileName, \ZipArchive::CREATE | \ZipArchive::OVERWRITE);
95
 
        if ($result !== true)
96
 
            throw new \InvalidArgumentException(__('Can\'t create ZIP. Error Code: ' . $result));
97
 
 
98
 
        // Decide what we output based on the options selected.
99
 
        $outputVersion = $this->getSanitizer()->getCheckbox('outputVersion') == 1;
100
 
        $outputLog = $this->getSanitizer()->getCheckbox('outputLog') == 1;
101
 
        $outputEnvCheck = $this->getSanitizer()->getCheckbox('outputEnvCheck') == 1;
102
 
        $outputSettings = $this->getSanitizer()->getCheckbox('outputSettings') == 1;
103
 
        $outputDisplays = $this->getSanitizer()->getCheckbox('outputDisplays') == 1;
104
 
        $outputDisplayProfile = $this->getSanitizer()->getCheckbox('outputDisplayProfile') == 1;
105
 
 
106
 
        if (!$outputVersion && !$outputLog && !$outputEnvCheck && !$outputSettings && !$outputDisplays && !$outputDisplayProfile)
107
 
            throw new \InvalidArgumentException(__('Please select at least one option'));
108
 
 
109
 
        // Should we output the version?
110
 
        if ($outputVersion) {
111
 
            $zip->addFromString('version.json', json_encode($this->store->select('SELECT * FROM `version`', []), JSON_PRETTY_PRINT));
112
 
        }
113
 
 
114
 
        // Should we output a log?
115
 
        if ($outputLog) {
116
 
            $tempLogFile = $this->getConfig()->GetSetting('LIBRARY_LOCATION') . 'temp/log_' . Random::generateString();
117
 
            $out = fopen($tempLogFile, 'w');
118
 
            fputcsv($out, ['logId', 'runNo', 'logDate', 'channel', 'page', 'function', 'message', 'display.display', 'type']);
119
 
 
120
 
            // Do some post processing
121
 
            foreach ($this->logFactory->query(['logId'], ['fromDt' => (time() - (60 * 10))]) as $row) {
122
 
                /* @var \Xibo\Entity\LogEntry $row */
123
 
                fputcsv($out, [$row->logId, $row->runNo, $row->logDate, $row->channel, $row->page, $row->function, $row->message, $row->display, $row->type]);
124
 
            }
125
 
 
126
 
            fclose($out);
127
 
 
128
 
            $zip->addFile($tempLogFile, 'log.csv');
129
 
        }
130
 
 
131
 
        // Output ENV Check
132
 
        if ($outputEnvCheck) {
133
 
            $zip->addFromString('environment.json', json_encode(array_map(function ($element) {
134
 
                unset($element['advice']);
135
 
                return $element;
136
 
            }, $this->getConfig()->CheckEnvironment()), JSON_PRETTY_PRINT));
137
 
        }
138
 
 
139
 
        // Output Settings
140
 
        if ($outputSettings) {
141
 
            $zip->addFromString('settings.json', json_encode(array_map(function($element) {
142
 
                return [$element['setting'] => $element['value']];
143
 
            }, $this->store->select('SELECT setting, `value` FROM `setting`', [])), JSON_PRETTY_PRINT));
144
 
        }
145
 
 
146
 
        // Output Displays
147
 
        if ($outputDisplays) {
148
 
 
149
 
            $displays = $this->displayFactory->query(['display']);
150
 
 
151
 
            // Output Profiles
152
 
            if ($outputDisplayProfile) {
153
 
                foreach ($displays as $display) {
154
 
                    /** @var \Xibo\Entity\Display $display */
155
 
                    $display->settingProfile = array_map(function ($element) {
156
 
                        unset($element['helpText']);
157
 
                        return $element;
158
 
                    }, $display->getSettings());
159
 
                }
160
 
            }
161
 
 
162
 
            $zip->addFromString('displays.json', json_encode($displays, JSON_PRETTY_PRINT));
163
 
        }
164
 
 
165
 
        // Close the ZIP file
166
 
        $zip->close();
167
 
 
168
 
        // Prepare the download
169
 
        if (ini_get('zlib.output_compression')) {
170
 
            ini_set('zlib.output_compression', 'Off');
171
 
        }
172
 
 
173
 
        header('Content-Type: application/octet-stream');
174
 
        header("Content-Transfer-Encoding: Binary");
175
 
        header("Content-disposition: attachment; filename=troubleshoot.zip");
176
 
        header('Content-Length: ' . filesize($tempFileName));
177
 
 
178
 
        // Send via Apache X-Sendfile header?
179
 
        if ($this->getConfig()->GetSetting('SENDFILE_MODE') == 'Apache') {
180
 
            header("X-Sendfile: $tempFileName");
181
 
            $this->getApp()->halt(200);
182
 
        }
183
 
        // Send via Nginx X-Accel-Redirect?
184
 
        if ($this->getConfig()->GetSetting('SENDFILE_MODE') == 'Nginx') {
185
 
            header("X-Accel-Redirect: /download/temp/" . basename($tempFileName));
186
 
            $this->getApp()->halt(200);
187
 
        }
188
 
 
189
 
        // Return the file with PHP
190
 
        // Disable any buffering to prevent OOM errors.
191
 
        readfile($tempFileName);
192
62
    }
193
63
 
194
64
    public function debugOn()
196
66
        if ($this->getUser()->userTypeId != 1)
197
67
            throw new AccessDeniedException();
198
68
 
199
 
        $this->getConfig()->ChangeSetting('audit', 'DEBUG');
200
 
        $this->getConfig()->ChangeSetting('ELEVATE_LOG_UNTIL', $this->getDate()->parse()->addMinutes(30)->format('U'));
 
69
        Config::ChangeSetting('audit', 'DEBUG');
201
70
 
202
71
        // Return
203
72
        $this->getState()->hydrate([
210
79
        if ($this->getUser()->userTypeId != 1)
211
80
            throw new AccessDeniedException();
212
81
 
213
 
        $this->getConfig()->ChangeSetting('audit', $this->getConfig()->GetSetting('RESTING_LOG_LEVEL'));
214
 
        $this->getConfig()->ChangeSetting('ELEVATE_LOG_UNTIL', '');
 
82
        Config::ChangeSetting('audit', 'EMERGENCY');
215
83
 
216
84
        // Return
217
85
        $this->getState()->hydrate([