21
21
namespace Xibo\Controller;
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;
35
* @package Xibo\Controller
37
27
class Fault extends Base
39
/** @var StorageServiceInterface */
47
/** @var DisplayFactory */
48
private $displayFactory;
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
63
public function __construct($log, $sanitizerService, $state, $user, $help, $date, $config, $store, $logFactory, $displayFactory)
65
$this->setCommonDependencies($log, $sanitizerService, $state, $user, $help, $date, $config);
67
$this->store = $store;
68
$this->logFactory = $logFactory;
69
$this->displayFactory = $displayFactory;
72
29
function displayPage()
74
$config = $this->getConfig();
31
$config = new Config();
76
33
'environmentCheck' => $config->CheckEnvironment(),
77
34
'environmentFault' => $config->envFault,
78
'environmentWarning' => $config->envWarning,
79
'binLogError' => ($config->checkBinLogEnabled() && !$config->checkBinLogFormat())
35
'environmentWarning' => $config->envWarning
82
38
$this->getState()->template = 'fault-page';
86
42
public function collect()
44
$out = fopen('php://output', 'w');
45
fputcsv($out, ['logId', 'runNo', 'logDate', 'channel', 'page', 'function', 'message', 'display.display', 'type']);
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]);
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);
91
$tempFileName = $this->getConfig()->GetSetting('LIBRARY_LOCATION') . 'temp/' . Random::generateString();
92
$zip = new \ZipArchive();
94
$result = $zip->open($tempFileName, \ZipArchive::CREATE | \ZipArchive::OVERWRITE);
96
throw new \InvalidArgumentException(__('Can\'t create ZIP. Error Code: ' . $result));
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;
106
if (!$outputVersion && !$outputLog && !$outputEnvCheck && !$outputSettings && !$outputDisplays && !$outputDisplayProfile)
107
throw new \InvalidArgumentException(__('Please select at least one option'));
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));
114
// Should we output a log?
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']);
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]);
128
$zip->addFile($tempLogFile, 'log.csv');
132
if ($outputEnvCheck) {
133
$zip->addFromString('environment.json', json_encode(array_map(function ($element) {
134
unset($element['advice']);
136
}, $this->getConfig()->CheckEnvironment()), JSON_PRETTY_PRINT));
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));
147
if ($outputDisplays) {
149
$displays = $this->displayFactory->query(['display']);
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']);
158
}, $display->getSettings());
162
$zip->addFromString('displays.json', json_encode($displays, JSON_PRETTY_PRINT));
165
// Close the ZIP file
168
// Prepare the download
169
if (ini_get('zlib.output_compression')) {
170
ini_set('zlib.output_compression', 'Off');
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));
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);
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);
189
// Return the file with PHP
190
// Disable any buffering to prevent OOM errors.
191
readfile($tempFileName);
194
64
public function debugOn()