@@ -32,50 +32,50 @@ |
||
32 | 32 | use OCP\Log\IWriter; |
33 | 33 | |
34 | 34 | class LogFactory implements ILogFactory { |
35 | - /** @var IServerContainer */ |
|
36 | - private $c; |
|
37 | - /** @var SystemConfig */ |
|
38 | - private $systemConfig; |
|
35 | + /** @var IServerContainer */ |
|
36 | + private $c; |
|
37 | + /** @var SystemConfig */ |
|
38 | + private $systemConfig; |
|
39 | 39 | |
40 | - public function __construct(IServerContainer $c, SystemConfig $systemConfig) { |
|
41 | - $this->c = $c; |
|
42 | - $this->systemConfig = $systemConfig; |
|
43 | - } |
|
40 | + public function __construct(IServerContainer $c, SystemConfig $systemConfig) { |
|
41 | + $this->c = $c; |
|
42 | + $this->systemConfig = $systemConfig; |
|
43 | + } |
|
44 | 44 | |
45 | - /** |
|
46 | - * @throws \OCP\AppFramework\QueryException |
|
47 | - */ |
|
48 | - public function get(string $type):IWriter { |
|
49 | - switch (strtolower($type)) { |
|
50 | - case 'errorlog': |
|
51 | - return new Errorlog(); |
|
52 | - case 'syslog': |
|
53 | - return $this->c->resolve(Syslog::class); |
|
54 | - case 'systemd': |
|
55 | - return $this->c->resolve(Systemdlog::class); |
|
56 | - case 'file': |
|
57 | - return $this->buildLogFile(); |
|
45 | + /** |
|
46 | + * @throws \OCP\AppFramework\QueryException |
|
47 | + */ |
|
48 | + public function get(string $type):IWriter { |
|
49 | + switch (strtolower($type)) { |
|
50 | + case 'errorlog': |
|
51 | + return new Errorlog(); |
|
52 | + case 'syslog': |
|
53 | + return $this->c->resolve(Syslog::class); |
|
54 | + case 'systemd': |
|
55 | + return $this->c->resolve(Systemdlog::class); |
|
56 | + case 'file': |
|
57 | + return $this->buildLogFile(); |
|
58 | 58 | |
59 | - // Backwards compatibility for old and fallback for unknown log types |
|
60 | - case 'owncloud': |
|
61 | - case 'nextcloud': |
|
62 | - default: |
|
63 | - return $this->buildLogFile(); |
|
64 | - } |
|
65 | - } |
|
59 | + // Backwards compatibility for old and fallback for unknown log types |
|
60 | + case 'owncloud': |
|
61 | + case 'nextcloud': |
|
62 | + default: |
|
63 | + return $this->buildLogFile(); |
|
64 | + } |
|
65 | + } |
|
66 | 66 | |
67 | - public function getCustomLogger(string $path):ILogger { |
|
68 | - $log = $this->buildLogFile($path); |
|
69 | - return new Log($log, $this->systemConfig); |
|
70 | - } |
|
67 | + public function getCustomLogger(string $path):ILogger { |
|
68 | + $log = $this->buildLogFile($path); |
|
69 | + return new Log($log, $this->systemConfig); |
|
70 | + } |
|
71 | 71 | |
72 | - protected function buildLogFile(string $logFile = ''):File { |
|
73 | - $defaultLogFile = $this->systemConfig->getValue('datadirectory', \OC::$SERVERROOT.'/data').'/nextcloud.log'; |
|
74 | - if($logFile === '') { |
|
75 | - $logFile = $this->systemConfig->getValue('logfile', $defaultLogFile); |
|
76 | - } |
|
77 | - $fallback = $defaultLogFile !== $logFile ? $defaultLogFile : ''; |
|
72 | + protected function buildLogFile(string $logFile = ''):File { |
|
73 | + $defaultLogFile = $this->systemConfig->getValue('datadirectory', \OC::$SERVERROOT.'/data').'/nextcloud.log'; |
|
74 | + if($logFile === '') { |
|
75 | + $logFile = $this->systemConfig->getValue('logfile', $defaultLogFile); |
|
76 | + } |
|
77 | + $fallback = $defaultLogFile !== $logFile ? $defaultLogFile : ''; |
|
78 | 78 | |
79 | - return new File($logFile, $fallback, $this->systemConfig); |
|
80 | - } |
|
79 | + return new File($logFile, $fallback, $this->systemConfig); |
|
80 | + } |
|
81 | 81 | } |
@@ -33,17 +33,17 @@ |
||
33 | 33 | * @since 14.0.0 |
34 | 34 | */ |
35 | 35 | interface ILogFactory { |
36 | - /** |
|
37 | - * @param string $type - one of: file, errorlog, syslog, systemd |
|
38 | - * @return IWriter |
|
39 | - * @since 14.0.0 |
|
40 | - */ |
|
41 | - public function get(string $type): IWriter; |
|
36 | + /** |
|
37 | + * @param string $type - one of: file, errorlog, syslog, systemd |
|
38 | + * @return IWriter |
|
39 | + * @since 14.0.0 |
|
40 | + */ |
|
41 | + public function get(string $type): IWriter; |
|
42 | 42 | |
43 | - /** |
|
44 | - * @param string $path |
|
45 | - * @return ILogger |
|
46 | - * @since 14.0.0 |
|
47 | - */ |
|
48 | - public function getCustomLogger(string $path): ILogger; |
|
43 | + /** |
|
44 | + * @param string $path |
|
45 | + * @return ILogger |
|
46 | + * @since 14.0.0 |
|
47 | + */ |
|
48 | + public function getCustomLogger(string $path): ILogger; |
|
49 | 49 | } |
@@ -36,167 +36,167 @@ |
||
36 | 36 | |
37 | 37 | class Manage extends Command implements CompletionAwareInterface { |
38 | 38 | |
39 | - const DEFAULT_BACKEND = 'file'; |
|
40 | - const DEFAULT_LOG_LEVEL = 2; |
|
41 | - const DEFAULT_TIMEZONE = 'UTC'; |
|
42 | - |
|
43 | - /** @var IConfig */ |
|
44 | - protected $config; |
|
45 | - |
|
46 | - public function __construct(IConfig $config) { |
|
47 | - $this->config = $config; |
|
48 | - parent::__construct(); |
|
49 | - } |
|
50 | - |
|
51 | - protected function configure() { |
|
52 | - $this |
|
53 | - ->setName('log:manage') |
|
54 | - ->setDescription('manage logging configuration') |
|
55 | - ->addOption( |
|
56 | - 'backend', |
|
57 | - null, |
|
58 | - InputOption::VALUE_REQUIRED, |
|
59 | - 'set the logging backend [file, syslog, errorlog, systemd]' |
|
60 | - ) |
|
61 | - ->addOption( |
|
62 | - 'level', |
|
63 | - null, |
|
64 | - InputOption::VALUE_REQUIRED, |
|
65 | - 'set the log level [debug, info, warning, error]' |
|
66 | - ) |
|
67 | - ->addOption( |
|
68 | - 'timezone', |
|
69 | - null, |
|
70 | - InputOption::VALUE_REQUIRED, |
|
71 | - 'set the logging timezone' |
|
72 | - ) |
|
73 | - ; |
|
74 | - } |
|
75 | - |
|
76 | - protected function execute(InputInterface $input, OutputInterface $output) { |
|
77 | - // collate config setting to the end, to avoid partial configuration |
|
78 | - $toBeSet = []; |
|
79 | - |
|
80 | - if ($backend = $input->getOption('backend')) { |
|
81 | - $this->validateBackend($backend); |
|
82 | - $toBeSet['log_type'] = $backend; |
|
83 | - } |
|
84 | - |
|
85 | - $level = $input->getOption('level'); |
|
86 | - if ($level !== null) { |
|
87 | - if (is_numeric($level)) { |
|
88 | - $levelNum = $level; |
|
89 | - // sanity check |
|
90 | - $this->convertLevelNumber($levelNum); |
|
91 | - } else { |
|
92 | - $levelNum = $this->convertLevelString($level); |
|
93 | - } |
|
94 | - $toBeSet['loglevel'] = $levelNum; |
|
95 | - } |
|
96 | - |
|
97 | - if ($timezone = $input->getOption('timezone')) { |
|
98 | - $this->validateTimezone($timezone); |
|
99 | - $toBeSet['logtimezone'] = $timezone; |
|
100 | - } |
|
101 | - |
|
102 | - // set config |
|
103 | - foreach ($toBeSet as $option => $value) { |
|
104 | - $this->config->setSystemValue($option, $value); |
|
105 | - } |
|
106 | - |
|
107 | - // display configuration |
|
108 | - $backend = $this->config->getSystemValue('log_type', self::DEFAULT_BACKEND); |
|
109 | - $output->writeln('Enabled logging backend: '.$backend); |
|
110 | - |
|
111 | - $levelNum = $this->config->getSystemValue('loglevel', self::DEFAULT_LOG_LEVEL); |
|
112 | - $level = $this->convertLevelNumber($levelNum); |
|
113 | - $output->writeln('Log level: '.$level.' ('.$levelNum.')'); |
|
114 | - |
|
115 | - $timezone = $this->config->getSystemValue('logtimezone', self::DEFAULT_TIMEZONE); |
|
116 | - $output->writeln('Log timezone: '.$timezone); |
|
117 | - } |
|
118 | - |
|
119 | - /** |
|
120 | - * @param string $backend |
|
121 | - * @throws \InvalidArgumentException |
|
122 | - */ |
|
123 | - protected function validateBackend($backend) { |
|
124 | - if (!class_exists('OC\\Log\\'.ucfirst($backend))) { |
|
125 | - throw new \InvalidArgumentException('Invalid backend'); |
|
126 | - } |
|
127 | - } |
|
128 | - |
|
129 | - /** |
|
130 | - * @param string $timezone |
|
131 | - * @throws \Exception |
|
132 | - */ |
|
133 | - protected function validateTimezone($timezone) { |
|
134 | - new \DateTimeZone($timezone); |
|
135 | - } |
|
136 | - |
|
137 | - /** |
|
138 | - * @param string $level |
|
139 | - * @return int |
|
140 | - * @throws \InvalidArgumentException |
|
141 | - */ |
|
142 | - protected function convertLevelString($level) { |
|
143 | - $level = strtolower($level); |
|
144 | - switch ($level) { |
|
145 | - case 'debug': |
|
146 | - return 0; |
|
147 | - case 'info': |
|
148 | - return 1; |
|
149 | - case 'warning': |
|
150 | - case 'warn': |
|
151 | - return 2; |
|
152 | - case 'error': |
|
153 | - case 'err': |
|
154 | - return 3; |
|
155 | - } |
|
156 | - throw new \InvalidArgumentException('Invalid log level string'); |
|
157 | - } |
|
158 | - |
|
159 | - /** |
|
160 | - * @param int $levelNum |
|
161 | - * @return string |
|
162 | - * @throws \InvalidArgumentException |
|
163 | - */ |
|
164 | - protected function convertLevelNumber($levelNum) { |
|
165 | - switch ($levelNum) { |
|
166 | - case 0: |
|
167 | - return 'Debug'; |
|
168 | - case 1: |
|
169 | - return 'Info'; |
|
170 | - case 2: |
|
171 | - return 'Warning'; |
|
172 | - case 3: |
|
173 | - return 'Error'; |
|
174 | - } |
|
175 | - throw new \InvalidArgumentException('Invalid log level number'); |
|
176 | - } |
|
177 | - |
|
178 | - /** |
|
179 | - * @param string $optionName |
|
180 | - * @param CompletionContext $context |
|
181 | - * @return string[] |
|
182 | - */ |
|
183 | - public function completeOptionValues($optionName, CompletionContext $context) { |
|
184 | - if ($optionName === 'backend') { |
|
185 | - return ['file', 'syslog', 'errorlog', 'systemd']; |
|
186 | - } else if ($optionName === 'level') { |
|
187 | - return ['debug', 'info', 'warning', 'error']; |
|
188 | - } else if ($optionName === 'timezone') { |
|
189 | - return \DateTimeZone::listIdentifiers(); |
|
190 | - } |
|
191 | - return []; |
|
192 | - } |
|
193 | - |
|
194 | - /** |
|
195 | - * @param string $argumentName |
|
196 | - * @param CompletionContext $context |
|
197 | - * @return string[] |
|
198 | - */ |
|
199 | - public function completeArgumentValues($argumentName, CompletionContext $context) { |
|
200 | - return []; |
|
201 | - } |
|
39 | + const DEFAULT_BACKEND = 'file'; |
|
40 | + const DEFAULT_LOG_LEVEL = 2; |
|
41 | + const DEFAULT_TIMEZONE = 'UTC'; |
|
42 | + |
|
43 | + /** @var IConfig */ |
|
44 | + protected $config; |
|
45 | + |
|
46 | + public function __construct(IConfig $config) { |
|
47 | + $this->config = $config; |
|
48 | + parent::__construct(); |
|
49 | + } |
|
50 | + |
|
51 | + protected function configure() { |
|
52 | + $this |
|
53 | + ->setName('log:manage') |
|
54 | + ->setDescription('manage logging configuration') |
|
55 | + ->addOption( |
|
56 | + 'backend', |
|
57 | + null, |
|
58 | + InputOption::VALUE_REQUIRED, |
|
59 | + 'set the logging backend [file, syslog, errorlog, systemd]' |
|
60 | + ) |
|
61 | + ->addOption( |
|
62 | + 'level', |
|
63 | + null, |
|
64 | + InputOption::VALUE_REQUIRED, |
|
65 | + 'set the log level [debug, info, warning, error]' |
|
66 | + ) |
|
67 | + ->addOption( |
|
68 | + 'timezone', |
|
69 | + null, |
|
70 | + InputOption::VALUE_REQUIRED, |
|
71 | + 'set the logging timezone' |
|
72 | + ) |
|
73 | + ; |
|
74 | + } |
|
75 | + |
|
76 | + protected function execute(InputInterface $input, OutputInterface $output) { |
|
77 | + // collate config setting to the end, to avoid partial configuration |
|
78 | + $toBeSet = []; |
|
79 | + |
|
80 | + if ($backend = $input->getOption('backend')) { |
|
81 | + $this->validateBackend($backend); |
|
82 | + $toBeSet['log_type'] = $backend; |
|
83 | + } |
|
84 | + |
|
85 | + $level = $input->getOption('level'); |
|
86 | + if ($level !== null) { |
|
87 | + if (is_numeric($level)) { |
|
88 | + $levelNum = $level; |
|
89 | + // sanity check |
|
90 | + $this->convertLevelNumber($levelNum); |
|
91 | + } else { |
|
92 | + $levelNum = $this->convertLevelString($level); |
|
93 | + } |
|
94 | + $toBeSet['loglevel'] = $levelNum; |
|
95 | + } |
|
96 | + |
|
97 | + if ($timezone = $input->getOption('timezone')) { |
|
98 | + $this->validateTimezone($timezone); |
|
99 | + $toBeSet['logtimezone'] = $timezone; |
|
100 | + } |
|
101 | + |
|
102 | + // set config |
|
103 | + foreach ($toBeSet as $option => $value) { |
|
104 | + $this->config->setSystemValue($option, $value); |
|
105 | + } |
|
106 | + |
|
107 | + // display configuration |
|
108 | + $backend = $this->config->getSystemValue('log_type', self::DEFAULT_BACKEND); |
|
109 | + $output->writeln('Enabled logging backend: '.$backend); |
|
110 | + |
|
111 | + $levelNum = $this->config->getSystemValue('loglevel', self::DEFAULT_LOG_LEVEL); |
|
112 | + $level = $this->convertLevelNumber($levelNum); |
|
113 | + $output->writeln('Log level: '.$level.' ('.$levelNum.')'); |
|
114 | + |
|
115 | + $timezone = $this->config->getSystemValue('logtimezone', self::DEFAULT_TIMEZONE); |
|
116 | + $output->writeln('Log timezone: '.$timezone); |
|
117 | + } |
|
118 | + |
|
119 | + /** |
|
120 | + * @param string $backend |
|
121 | + * @throws \InvalidArgumentException |
|
122 | + */ |
|
123 | + protected function validateBackend($backend) { |
|
124 | + if (!class_exists('OC\\Log\\'.ucfirst($backend))) { |
|
125 | + throw new \InvalidArgumentException('Invalid backend'); |
|
126 | + } |
|
127 | + } |
|
128 | + |
|
129 | + /** |
|
130 | + * @param string $timezone |
|
131 | + * @throws \Exception |
|
132 | + */ |
|
133 | + protected function validateTimezone($timezone) { |
|
134 | + new \DateTimeZone($timezone); |
|
135 | + } |
|
136 | + |
|
137 | + /** |
|
138 | + * @param string $level |
|
139 | + * @return int |
|
140 | + * @throws \InvalidArgumentException |
|
141 | + */ |
|
142 | + protected function convertLevelString($level) { |
|
143 | + $level = strtolower($level); |
|
144 | + switch ($level) { |
|
145 | + case 'debug': |
|
146 | + return 0; |
|
147 | + case 'info': |
|
148 | + return 1; |
|
149 | + case 'warning': |
|
150 | + case 'warn': |
|
151 | + return 2; |
|
152 | + case 'error': |
|
153 | + case 'err': |
|
154 | + return 3; |
|
155 | + } |
|
156 | + throw new \InvalidArgumentException('Invalid log level string'); |
|
157 | + } |
|
158 | + |
|
159 | + /** |
|
160 | + * @param int $levelNum |
|
161 | + * @return string |
|
162 | + * @throws \InvalidArgumentException |
|
163 | + */ |
|
164 | + protected function convertLevelNumber($levelNum) { |
|
165 | + switch ($levelNum) { |
|
166 | + case 0: |
|
167 | + return 'Debug'; |
|
168 | + case 1: |
|
169 | + return 'Info'; |
|
170 | + case 2: |
|
171 | + return 'Warning'; |
|
172 | + case 3: |
|
173 | + return 'Error'; |
|
174 | + } |
|
175 | + throw new \InvalidArgumentException('Invalid log level number'); |
|
176 | + } |
|
177 | + |
|
178 | + /** |
|
179 | + * @param string $optionName |
|
180 | + * @param CompletionContext $context |
|
181 | + * @return string[] |
|
182 | + */ |
|
183 | + public function completeOptionValues($optionName, CompletionContext $context) { |
|
184 | + if ($optionName === 'backend') { |
|
185 | + return ['file', 'syslog', 'errorlog', 'systemd']; |
|
186 | + } else if ($optionName === 'level') { |
|
187 | + return ['debug', 'info', 'warning', 'error']; |
|
188 | + } else if ($optionName === 'timezone') { |
|
189 | + return \DateTimeZone::listIdentifiers(); |
|
190 | + } |
|
191 | + return []; |
|
192 | + } |
|
193 | + |
|
194 | + /** |
|
195 | + * @param string $argumentName |
|
196 | + * @param CompletionContext $context |
|
197 | + * @return string[] |
|
198 | + */ |
|
199 | + public function completeArgumentValues($argumentName, CompletionContext $context) { |
|
200 | + return []; |
|
201 | + } |
|
202 | 202 | } |
@@ -30,30 +30,30 @@ |
||
30 | 30 | use OCP\Log\IWriter; |
31 | 31 | |
32 | 32 | class Syslog implements IWriter { |
33 | - protected $levels = [ |
|
34 | - ILogger::DEBUG => LOG_DEBUG, |
|
35 | - ILogger::INFO => LOG_INFO, |
|
36 | - ILogger::WARN => LOG_WARNING, |
|
37 | - ILogger::ERROR => LOG_ERR, |
|
38 | - ILogger::FATAL => LOG_CRIT, |
|
39 | - ]; |
|
33 | + protected $levels = [ |
|
34 | + ILogger::DEBUG => LOG_DEBUG, |
|
35 | + ILogger::INFO => LOG_INFO, |
|
36 | + ILogger::WARN => LOG_WARNING, |
|
37 | + ILogger::ERROR => LOG_ERR, |
|
38 | + ILogger::FATAL => LOG_CRIT, |
|
39 | + ]; |
|
40 | 40 | |
41 | - public function __construct(IConfig $config) { |
|
42 | - openlog($config->getSystemValue('syslog_tag', 'Nextcloud'), LOG_PID | LOG_CONS, LOG_USER); |
|
43 | - } |
|
41 | + public function __construct(IConfig $config) { |
|
42 | + openlog($config->getSystemValue('syslog_tag', 'Nextcloud'), LOG_PID | LOG_CONS, LOG_USER); |
|
43 | + } |
|
44 | 44 | |
45 | - public function __destruct() { |
|
46 | - closelog(); |
|
47 | - } |
|
45 | + public function __destruct() { |
|
46 | + closelog(); |
|
47 | + } |
|
48 | 48 | |
49 | - /** |
|
50 | - * write a message in the log |
|
51 | - * @param string $app |
|
52 | - * @param string $message |
|
53 | - * @param int $level |
|
54 | - */ |
|
55 | - public function write(string $app, $message, int $level) { |
|
56 | - $syslog_level = $this->levels[$level]; |
|
57 | - syslog($syslog_level, '{'.$app.'} '.$message); |
|
58 | - } |
|
49 | + /** |
|
50 | + * write a message in the log |
|
51 | + * @param string $app |
|
52 | + * @param string $message |
|
53 | + * @param int $level |
|
54 | + */ |
|
55 | + public function write(string $app, $message, int $level) { |
|
56 | + $syslog_level = $this->levels[$level]; |
|
57 | + syslog($syslog_level, '{'.$app.'} '.$message); |
|
58 | + } |
|
59 | 59 | } |
@@ -43,36 +43,36 @@ |
||
43 | 43 | // Syslog compatibility fields |
44 | 44 | |
45 | 45 | class Systemdlog implements IWriter { |
46 | - protected $levels = [ |
|
47 | - ILogger::DEBUG => 7, |
|
48 | - ILogger::INFO => 6, |
|
49 | - ILogger::WARN => 4, |
|
50 | - ILogger::ERROR => 3, |
|
51 | - ILogger::FATAL => 2, |
|
52 | - ]; |
|
46 | + protected $levels = [ |
|
47 | + ILogger::DEBUG => 7, |
|
48 | + ILogger::INFO => 6, |
|
49 | + ILogger::WARN => 4, |
|
50 | + ILogger::ERROR => 3, |
|
51 | + ILogger::FATAL => 2, |
|
52 | + ]; |
|
53 | 53 | |
54 | - protected $syslogId; |
|
54 | + protected $syslogId; |
|
55 | 55 | |
56 | - public function __construct(IConfig $config) { |
|
57 | - if(!function_exists('sd_journal_send')) { |
|
58 | - throw new HintException( |
|
59 | - 'PHP extension php-systemd is not available.', |
|
60 | - 'Please install and enable PHP extension systemd if you wish to log to the Systemd journal.'); |
|
56 | + public function __construct(IConfig $config) { |
|
57 | + if(!function_exists('sd_journal_send')) { |
|
58 | + throw new HintException( |
|
59 | + 'PHP extension php-systemd is not available.', |
|
60 | + 'Please install and enable PHP extension systemd if you wish to log to the Systemd journal.'); |
|
61 | 61 | |
62 | - } |
|
63 | - $this->syslogId = $config->getSystemValue('syslog_tag', 'Nextcloud'); |
|
64 | - } |
|
62 | + } |
|
63 | + $this->syslogId = $config->getSystemValue('syslog_tag', 'Nextcloud'); |
|
64 | + } |
|
65 | 65 | |
66 | - /** |
|
67 | - * Write a message to the log. |
|
68 | - * @param string $app |
|
69 | - * @param string $message |
|
70 | - * @param int $level |
|
71 | - */ |
|
72 | - public function write(string $app, $message, int $level) { |
|
73 | - $journal_level = $this->levels[$level]; |
|
74 | - sd_journal_send('PRIORITY='.$journal_level, |
|
75 | - 'SYSLOG_IDENTIFIER='.$this->syslogId, |
|
76 | - 'MESSAGE={'.$app.'} '.$message); |
|
77 | - } |
|
66 | + /** |
|
67 | + * Write a message to the log. |
|
68 | + * @param string $app |
|
69 | + * @param string $message |
|
70 | + * @param int $level |
|
71 | + */ |
|
72 | + public function write(string $app, $message, int $level) { |
|
73 | + $journal_level = $this->levels[$level]; |
|
74 | + sd_journal_send('PRIORITY='.$journal_level, |
|
75 | + 'SYSLOG_IDENTIFIER='.$this->syslogId, |
|
76 | + 'MESSAGE={'.$app.'} '.$message); |
|
77 | + } |
|
78 | 78 | } |
@@ -54,7 +54,7 @@ |
||
54 | 54 | protected $syslogId; |
55 | 55 | |
56 | 56 | public function __construct(IConfig $config) { |
57 | - if(!function_exists('sd_journal_send')) { |
|
57 | + if (!function_exists('sd_journal_send')) { |
|
58 | 58 | throw new HintException( |
59 | 59 | 'PHP extension php-systemd is not available.', |
60 | 60 | 'Please install and enable PHP extension systemd if you wish to log to the Systemd journal.'); |
@@ -46,340 +46,340 @@ |
||
46 | 46 | */ |
47 | 47 | class OC_Template extends \OC\Template\Base { |
48 | 48 | |
49 | - /** @var string */ |
|
50 | - private $renderAs; // Create a full page? |
|
51 | - |
|
52 | - /** @var string */ |
|
53 | - private $path; // The path to the template |
|
54 | - |
|
55 | - /** @var array */ |
|
56 | - private $headers = array(); //custom headers |
|
57 | - |
|
58 | - /** @var string */ |
|
59 | - protected $app; // app id |
|
60 | - |
|
61 | - protected static $initTemplateEngineFirstRun = true; |
|
62 | - |
|
63 | - /** |
|
64 | - * Constructor |
|
65 | - * |
|
66 | - * @param string $app app providing the template |
|
67 | - * @param string $name of the template file (without suffix) |
|
68 | - * @param string $renderAs If $renderAs is set, OC_Template will try to |
|
69 | - * produce a full page in the according layout. For |
|
70 | - * now, $renderAs can be set to "guest", "user" or |
|
71 | - * "admin". |
|
72 | - * @param bool $registerCall = true |
|
73 | - */ |
|
74 | - public function __construct( $app, $name, $renderAs = "", $registerCall = true ) { |
|
75 | - // Read the selected theme from the config file |
|
76 | - self::initTemplateEngine($renderAs); |
|
77 | - |
|
78 | - $theme = OC_Util::getTheme(); |
|
79 | - |
|
80 | - $requestToken = (OC::$server->getSession() && $registerCall) ? \OCP\Util::callRegister() : ''; |
|
81 | - |
|
82 | - $parts = explode('/', $app); // fix translation when app is something like core/lostpassword |
|
83 | - $l10n = \OC::$server->getL10N($parts[0]); |
|
84 | - /** @var \OCP\Defaults $themeDefaults */ |
|
85 | - $themeDefaults = \OC::$server->query(\OCP\Defaults::class); |
|
86 | - |
|
87 | - list($path, $template) = $this->findTemplate($theme, $app, $name); |
|
88 | - |
|
89 | - // Set the private data |
|
90 | - $this->renderAs = $renderAs; |
|
91 | - $this->path = $path; |
|
92 | - $this->app = $app; |
|
93 | - |
|
94 | - parent::__construct($template, $requestToken, $l10n, $themeDefaults); |
|
95 | - } |
|
96 | - |
|
97 | - /** |
|
98 | - * @param string $renderAs |
|
99 | - */ |
|
100 | - public static function initTemplateEngine($renderAs) { |
|
101 | - if (self::$initTemplateEngineFirstRun){ |
|
102 | - |
|
103 | - //apps that started before the template initialization can load their own scripts/styles |
|
104 | - //so to make sure this scripts/styles here are loaded first we use OC_Util::addScript() with $prepend=true |
|
105 | - //meaning the last script/style in this list will be loaded first |
|
106 | - if (\OC::$server->getSystemConfig()->getValue ('installed', false) && $renderAs !== 'error' && !\OCP\Util::needUpgrade()) { |
|
107 | - if (\OC::$server->getConfig ()->getAppValue ( 'core', 'backgroundjobs_mode', 'ajax' ) == 'ajax') { |
|
108 | - OC_Util::addScript ( 'backgroundjobs', null, true ); |
|
109 | - } |
|
110 | - } |
|
111 | - |
|
112 | - OC_Util::addStyle('css-variables', null, true); |
|
113 | - OC_Util::addStyle('server', null, true); |
|
114 | - OC_Util::addStyle('jquery-ui-fixes',null,true); |
|
115 | - OC_Util::addVendorStyle('jquery-ui/themes/base/jquery-ui',null,true); |
|
116 | - OC_Util::addVendorStyle('select2/select2', null, true); |
|
117 | - OC_Util::addStyle('jquery.ocdialog'); |
|
118 | - OC_Util::addTranslations("core", null, true); |
|
119 | - OC_Util::addStyle('search', 'results'); |
|
120 | - OC_Util::addScript('search', 'search', true); |
|
121 | - OC_Util::addScript('search', 'searchprovider'); |
|
122 | - OC_Util::addScript('merged-template-prepend', null, true); |
|
123 | - OC_Util::addScript('jquery-ui-fixes'); |
|
124 | - OC_Util::addScript('files/fileinfo'); |
|
125 | - OC_Util::addScript('files/client'); |
|
126 | - OC_Util::addScript('contactsmenu'); |
|
127 | - |
|
128 | - if (\OC::$server->getConfig()->getSystemValue('debug')) { |
|
129 | - // Add the stuff we need always |
|
130 | - // following logic will import all vendor libraries that are |
|
131 | - // specified in core/js/core.json |
|
132 | - $fileContent = file_get_contents(OC::$SERVERROOT . '/core/js/core.json'); |
|
133 | - if($fileContent !== false) { |
|
134 | - $coreDependencies = json_decode($fileContent, true); |
|
135 | - foreach(array_reverse($coreDependencies['vendor']) as $vendorLibrary) { |
|
136 | - //remove trailing ".js" as addVendorScript will append it |
|
137 | - OC_Util::addVendorScript( |
|
138 | - substr($vendorLibrary, 0, -3),null,true); |
|
139 | - } |
|
140 | - } else { |
|
141 | - throw new \Exception('Cannot read core/js/core.json'); |
|
142 | - } |
|
143 | - } else { |
|
144 | - // Import all (combined) default vendor libraries |
|
145 | - OC_Util::addVendorScript('core', null, true); |
|
146 | - } |
|
147 | - |
|
148 | - if (\OC::$server->getRequest()->isUserAgent([\OC\AppFramework\Http\Request::USER_AGENT_IE])) { |
|
149 | - // polyfill for btoa/atob for IE friends |
|
150 | - OC_Util::addVendorScript('base64/base64'); |
|
151 | - // shim for the davclient.js library |
|
152 | - \OCP\Util::addScript('files/iedavclient'); |
|
153 | - } |
|
154 | - |
|
155 | - self::$initTemplateEngineFirstRun = false; |
|
156 | - } |
|
157 | - |
|
158 | - } |
|
159 | - |
|
160 | - |
|
161 | - /** |
|
162 | - * find the template with the given name |
|
163 | - * @param string $name of the template file (without suffix) |
|
164 | - * |
|
165 | - * Will select the template file for the selected theme. |
|
166 | - * Checking all the possible locations. |
|
167 | - * @param string $theme |
|
168 | - * @param string $app |
|
169 | - * @return string[] |
|
170 | - */ |
|
171 | - protected function findTemplate($theme, $app, $name) { |
|
172 | - // Check if it is a app template or not. |
|
173 | - if( $app !== '' ) { |
|
174 | - $dirs = $this->getAppTemplateDirs($theme, $app, OC::$SERVERROOT, OC_App::getAppPath($app)); |
|
175 | - } else { |
|
176 | - $dirs = $this->getCoreTemplateDirs($theme, OC::$SERVERROOT); |
|
177 | - } |
|
178 | - $locator = new \OC\Template\TemplateFileLocator( $dirs ); |
|
179 | - $template = $locator->find($name); |
|
180 | - $path = $locator->getPath(); |
|
181 | - return array($path, $template); |
|
182 | - } |
|
183 | - |
|
184 | - /** |
|
185 | - * Add a custom element to the header |
|
186 | - * @param string $tag tag name of the element |
|
187 | - * @param array $attributes array of attributes for the element |
|
188 | - * @param string $text the text content for the element. If $text is null then the |
|
189 | - * element will be written as empty element. So use "" to get a closing tag. |
|
190 | - */ |
|
191 | - public function addHeader($tag, $attributes, $text=null) { |
|
192 | - $this->headers[]= array( |
|
193 | - 'tag' => $tag, |
|
194 | - 'attributes' => $attributes, |
|
195 | - 'text' => $text |
|
196 | - ); |
|
197 | - } |
|
198 | - |
|
199 | - /** |
|
200 | - * Process the template |
|
201 | - * @return boolean|string |
|
202 | - * |
|
203 | - * This function process the template. If $this->renderAs is set, it |
|
204 | - * will produce a full page. |
|
205 | - */ |
|
206 | - public function fetchPage($additionalParams = null) { |
|
207 | - $data = parent::fetchPage($additionalParams); |
|
208 | - |
|
209 | - if( $this->renderAs ) { |
|
210 | - $page = new TemplateLayout($this->renderAs, $this->app); |
|
211 | - |
|
212 | - if(is_array($additionalParams)) { |
|
213 | - foreach ($additionalParams as $key => $value) { |
|
214 | - $page->assign($key, $value); |
|
215 | - } |
|
216 | - } |
|
217 | - |
|
218 | - // Add custom headers |
|
219 | - $headers = ''; |
|
220 | - foreach(OC_Util::$headers as $header) { |
|
221 | - $headers .= '<'.\OCP\Util::sanitizeHTML($header['tag']); |
|
222 | - if ( strcasecmp($header['tag'], 'script') === 0 && in_array('src', array_map('strtolower', array_keys($header['attributes']))) ) { |
|
223 | - $headers .= ' defer'; |
|
224 | - } |
|
225 | - foreach($header['attributes'] as $name=>$value) { |
|
226 | - $headers .= ' '.\OCP\Util::sanitizeHTML($name).'="'.\OCP\Util::sanitizeHTML($value).'"'; |
|
227 | - } |
|
228 | - if ($header['text'] !== null) { |
|
229 | - $headers .= '>'.\OCP\Util::sanitizeHTML($header['text']).'</'.\OCP\Util::sanitizeHTML($header['tag']).'>'; |
|
230 | - } else { |
|
231 | - $headers .= '/>'; |
|
232 | - } |
|
233 | - } |
|
234 | - |
|
235 | - $page->assign('headers', $headers); |
|
236 | - |
|
237 | - $page->assign('content', $data); |
|
238 | - return $page->fetchPage($additionalParams); |
|
239 | - } |
|
240 | - |
|
241 | - return $data; |
|
242 | - } |
|
243 | - |
|
244 | - /** |
|
245 | - * Include template |
|
246 | - * |
|
247 | - * @param string $file |
|
248 | - * @param array|null $additionalParams |
|
249 | - * @return string returns content of included template |
|
250 | - * |
|
251 | - * Includes another template. use <?php echo $this->inc('template'); ?> to |
|
252 | - * do this. |
|
253 | - */ |
|
254 | - public function inc( $file, $additionalParams = null ) { |
|
255 | - return $this->load($this->path.$file.'.php', $additionalParams); |
|
256 | - } |
|
257 | - |
|
258 | - /** |
|
259 | - * Shortcut to print a simple page for users |
|
260 | - * @param string $application The application we render the template for |
|
261 | - * @param string $name Name of the template |
|
262 | - * @param array $parameters Parameters for the template |
|
263 | - * @return boolean|null |
|
264 | - */ |
|
265 | - public static function printUserPage( $application, $name, $parameters = array() ) { |
|
266 | - $content = new OC_Template( $application, $name, "user" ); |
|
267 | - foreach( $parameters as $key => $value ) { |
|
268 | - $content->assign( $key, $value ); |
|
269 | - } |
|
270 | - print $content->printPage(); |
|
271 | - } |
|
272 | - |
|
273 | - /** |
|
274 | - * Shortcut to print a simple page for admins |
|
275 | - * @param string $application The application we render the template for |
|
276 | - * @param string $name Name of the template |
|
277 | - * @param array $parameters Parameters for the template |
|
278 | - * @return bool |
|
279 | - */ |
|
280 | - public static function printAdminPage( $application, $name, $parameters = array() ) { |
|
281 | - $content = new OC_Template( $application, $name, "admin" ); |
|
282 | - foreach( $parameters as $key => $value ) { |
|
283 | - $content->assign( $key, $value ); |
|
284 | - } |
|
285 | - return $content->printPage(); |
|
286 | - } |
|
287 | - |
|
288 | - /** |
|
289 | - * Shortcut to print a simple page for guests |
|
290 | - * @param string $application The application we render the template for |
|
291 | - * @param string $name Name of the template |
|
292 | - * @param array|string $parameters Parameters for the template |
|
293 | - * @return bool |
|
294 | - */ |
|
295 | - public static function printGuestPage( $application, $name, $parameters = array() ) { |
|
296 | - $content = new OC_Template( $application, $name, "guest" ); |
|
297 | - foreach( $parameters as $key => $value ) { |
|
298 | - $content->assign( $key, $value ); |
|
299 | - } |
|
300 | - return $content->printPage(); |
|
301 | - } |
|
302 | - |
|
303 | - /** |
|
304 | - * Print a fatal error page and terminates the script |
|
305 | - * @param string $error_msg The error message to show |
|
306 | - * @param string $hint An optional hint message - needs to be properly escape |
|
307 | - * @param int $statusCode |
|
308 | - * @suppress PhanAccessMethodInternal |
|
309 | - */ |
|
310 | - public static function printErrorPage( $error_msg, $hint = '', $statusCode = 500) { |
|
311 | - if (\OC::$server->getAppManager()->isEnabledForUser('theming') && !\OC_App::isAppLoaded('theming')) { |
|
312 | - \OC_App::loadApp('theming'); |
|
313 | - } |
|
314 | - |
|
315 | - |
|
316 | - if ($error_msg === $hint) { |
|
317 | - // If the hint is the same as the message there is no need to display it twice. |
|
318 | - $hint = ''; |
|
319 | - } |
|
320 | - |
|
321 | - http_response_code($statusCode); |
|
322 | - try { |
|
323 | - $content = new \OC_Template( '', 'error', 'error', false ); |
|
324 | - $errors = array(array('error' => $error_msg, 'hint' => $hint)); |
|
325 | - $content->assign( 'errors', $errors ); |
|
326 | - $content->printPage(); |
|
327 | - } catch (\Exception $e) { |
|
328 | - $logger = \OC::$server->getLogger(); |
|
329 | - $logger->error("$error_msg $hint", ['app' => 'core']); |
|
330 | - $logger->logException($e, ['app' => 'core']); |
|
331 | - |
|
332 | - header('Content-Type: text/plain; charset=utf-8'); |
|
333 | - print("$error_msg $hint"); |
|
334 | - } |
|
335 | - die(); |
|
336 | - } |
|
337 | - |
|
338 | - /** |
|
339 | - * print error page using Exception details |
|
340 | - * @param Exception|Throwable $exception |
|
341 | - * @param int $statusCode |
|
342 | - * @return bool|string |
|
343 | - * @suppress PhanAccessMethodInternal |
|
344 | - */ |
|
345 | - public static function printExceptionErrorPage($exception, $statusCode = 503) { |
|
346 | - http_response_code($statusCode); |
|
347 | - try { |
|
348 | - $request = \OC::$server->getRequest(); |
|
349 | - $content = new \OC_Template('', 'exception', 'error', false); |
|
350 | - $content->assign('errorClass', get_class($exception)); |
|
351 | - $content->assign('errorMsg', $exception->getMessage()); |
|
352 | - $content->assign('errorCode', $exception->getCode()); |
|
353 | - $content->assign('file', $exception->getFile()); |
|
354 | - $content->assign('line', $exception->getLine()); |
|
355 | - $content->assign('trace', $exception->getTraceAsString()); |
|
356 | - $content->assign('debugMode', \OC::$server->getSystemConfig()->getValue('debug', false)); |
|
357 | - $content->assign('remoteAddr', $request->getRemoteAddress()); |
|
358 | - $content->assign('requestID', $request->getId()); |
|
359 | - $content->printPage(); |
|
360 | - } catch (\Exception $e) { |
|
361 | - try { |
|
362 | - $logger = \OC::$server->getLogger(); |
|
363 | - $logger->logException($exception, ['app' => 'core']); |
|
364 | - $logger->logException($e, ['app' => 'core']); |
|
365 | - } catch (Throwable $e) { |
|
366 | - // no way to log it properly - but to avoid a white page of death we send some output |
|
367 | - header('Content-Type: text/plain; charset=utf-8'); |
|
368 | - print("Internal Server Error\n\n"); |
|
369 | - print("The server encountered an internal error and was unable to complete your request.\n"); |
|
370 | - print("Please contact the server administrator if this error reappears multiple times, please include the technical details below in your report.\n"); |
|
371 | - print("More details can be found in the server log.\n"); |
|
372 | - |
|
373 | - // and then throw it again to log it at least to the web server error log |
|
374 | - throw $e; |
|
375 | - } |
|
376 | - |
|
377 | - header('Content-Type: text/plain; charset=utf-8'); |
|
378 | - print("Internal Server Error\n\n"); |
|
379 | - print("The server encountered an internal error and was unable to complete your request.\n"); |
|
380 | - print("Please contact the server administrator if this error reappears multiple times, please include the technical details below in your report.\n"); |
|
381 | - print("More details can be found in the server log.\n"); |
|
382 | - } |
|
383 | - die(); |
|
384 | - } |
|
49 | + /** @var string */ |
|
50 | + private $renderAs; // Create a full page? |
|
51 | + |
|
52 | + /** @var string */ |
|
53 | + private $path; // The path to the template |
|
54 | + |
|
55 | + /** @var array */ |
|
56 | + private $headers = array(); //custom headers |
|
57 | + |
|
58 | + /** @var string */ |
|
59 | + protected $app; // app id |
|
60 | + |
|
61 | + protected static $initTemplateEngineFirstRun = true; |
|
62 | + |
|
63 | + /** |
|
64 | + * Constructor |
|
65 | + * |
|
66 | + * @param string $app app providing the template |
|
67 | + * @param string $name of the template file (without suffix) |
|
68 | + * @param string $renderAs If $renderAs is set, OC_Template will try to |
|
69 | + * produce a full page in the according layout. For |
|
70 | + * now, $renderAs can be set to "guest", "user" or |
|
71 | + * "admin". |
|
72 | + * @param bool $registerCall = true |
|
73 | + */ |
|
74 | + public function __construct( $app, $name, $renderAs = "", $registerCall = true ) { |
|
75 | + // Read the selected theme from the config file |
|
76 | + self::initTemplateEngine($renderAs); |
|
77 | + |
|
78 | + $theme = OC_Util::getTheme(); |
|
79 | + |
|
80 | + $requestToken = (OC::$server->getSession() && $registerCall) ? \OCP\Util::callRegister() : ''; |
|
81 | + |
|
82 | + $parts = explode('/', $app); // fix translation when app is something like core/lostpassword |
|
83 | + $l10n = \OC::$server->getL10N($parts[0]); |
|
84 | + /** @var \OCP\Defaults $themeDefaults */ |
|
85 | + $themeDefaults = \OC::$server->query(\OCP\Defaults::class); |
|
86 | + |
|
87 | + list($path, $template) = $this->findTemplate($theme, $app, $name); |
|
88 | + |
|
89 | + // Set the private data |
|
90 | + $this->renderAs = $renderAs; |
|
91 | + $this->path = $path; |
|
92 | + $this->app = $app; |
|
93 | + |
|
94 | + parent::__construct($template, $requestToken, $l10n, $themeDefaults); |
|
95 | + } |
|
96 | + |
|
97 | + /** |
|
98 | + * @param string $renderAs |
|
99 | + */ |
|
100 | + public static function initTemplateEngine($renderAs) { |
|
101 | + if (self::$initTemplateEngineFirstRun){ |
|
102 | + |
|
103 | + //apps that started before the template initialization can load their own scripts/styles |
|
104 | + //so to make sure this scripts/styles here are loaded first we use OC_Util::addScript() with $prepend=true |
|
105 | + //meaning the last script/style in this list will be loaded first |
|
106 | + if (\OC::$server->getSystemConfig()->getValue ('installed', false) && $renderAs !== 'error' && !\OCP\Util::needUpgrade()) { |
|
107 | + if (\OC::$server->getConfig ()->getAppValue ( 'core', 'backgroundjobs_mode', 'ajax' ) == 'ajax') { |
|
108 | + OC_Util::addScript ( 'backgroundjobs', null, true ); |
|
109 | + } |
|
110 | + } |
|
111 | + |
|
112 | + OC_Util::addStyle('css-variables', null, true); |
|
113 | + OC_Util::addStyle('server', null, true); |
|
114 | + OC_Util::addStyle('jquery-ui-fixes',null,true); |
|
115 | + OC_Util::addVendorStyle('jquery-ui/themes/base/jquery-ui',null,true); |
|
116 | + OC_Util::addVendorStyle('select2/select2', null, true); |
|
117 | + OC_Util::addStyle('jquery.ocdialog'); |
|
118 | + OC_Util::addTranslations("core", null, true); |
|
119 | + OC_Util::addStyle('search', 'results'); |
|
120 | + OC_Util::addScript('search', 'search', true); |
|
121 | + OC_Util::addScript('search', 'searchprovider'); |
|
122 | + OC_Util::addScript('merged-template-prepend', null, true); |
|
123 | + OC_Util::addScript('jquery-ui-fixes'); |
|
124 | + OC_Util::addScript('files/fileinfo'); |
|
125 | + OC_Util::addScript('files/client'); |
|
126 | + OC_Util::addScript('contactsmenu'); |
|
127 | + |
|
128 | + if (\OC::$server->getConfig()->getSystemValue('debug')) { |
|
129 | + // Add the stuff we need always |
|
130 | + // following logic will import all vendor libraries that are |
|
131 | + // specified in core/js/core.json |
|
132 | + $fileContent = file_get_contents(OC::$SERVERROOT . '/core/js/core.json'); |
|
133 | + if($fileContent !== false) { |
|
134 | + $coreDependencies = json_decode($fileContent, true); |
|
135 | + foreach(array_reverse($coreDependencies['vendor']) as $vendorLibrary) { |
|
136 | + //remove trailing ".js" as addVendorScript will append it |
|
137 | + OC_Util::addVendorScript( |
|
138 | + substr($vendorLibrary, 0, -3),null,true); |
|
139 | + } |
|
140 | + } else { |
|
141 | + throw new \Exception('Cannot read core/js/core.json'); |
|
142 | + } |
|
143 | + } else { |
|
144 | + // Import all (combined) default vendor libraries |
|
145 | + OC_Util::addVendorScript('core', null, true); |
|
146 | + } |
|
147 | + |
|
148 | + if (\OC::$server->getRequest()->isUserAgent([\OC\AppFramework\Http\Request::USER_AGENT_IE])) { |
|
149 | + // polyfill for btoa/atob for IE friends |
|
150 | + OC_Util::addVendorScript('base64/base64'); |
|
151 | + // shim for the davclient.js library |
|
152 | + \OCP\Util::addScript('files/iedavclient'); |
|
153 | + } |
|
154 | + |
|
155 | + self::$initTemplateEngineFirstRun = false; |
|
156 | + } |
|
157 | + |
|
158 | + } |
|
159 | + |
|
160 | + |
|
161 | + /** |
|
162 | + * find the template with the given name |
|
163 | + * @param string $name of the template file (without suffix) |
|
164 | + * |
|
165 | + * Will select the template file for the selected theme. |
|
166 | + * Checking all the possible locations. |
|
167 | + * @param string $theme |
|
168 | + * @param string $app |
|
169 | + * @return string[] |
|
170 | + */ |
|
171 | + protected function findTemplate($theme, $app, $name) { |
|
172 | + // Check if it is a app template or not. |
|
173 | + if( $app !== '' ) { |
|
174 | + $dirs = $this->getAppTemplateDirs($theme, $app, OC::$SERVERROOT, OC_App::getAppPath($app)); |
|
175 | + } else { |
|
176 | + $dirs = $this->getCoreTemplateDirs($theme, OC::$SERVERROOT); |
|
177 | + } |
|
178 | + $locator = new \OC\Template\TemplateFileLocator( $dirs ); |
|
179 | + $template = $locator->find($name); |
|
180 | + $path = $locator->getPath(); |
|
181 | + return array($path, $template); |
|
182 | + } |
|
183 | + |
|
184 | + /** |
|
185 | + * Add a custom element to the header |
|
186 | + * @param string $tag tag name of the element |
|
187 | + * @param array $attributes array of attributes for the element |
|
188 | + * @param string $text the text content for the element. If $text is null then the |
|
189 | + * element will be written as empty element. So use "" to get a closing tag. |
|
190 | + */ |
|
191 | + public function addHeader($tag, $attributes, $text=null) { |
|
192 | + $this->headers[]= array( |
|
193 | + 'tag' => $tag, |
|
194 | + 'attributes' => $attributes, |
|
195 | + 'text' => $text |
|
196 | + ); |
|
197 | + } |
|
198 | + |
|
199 | + /** |
|
200 | + * Process the template |
|
201 | + * @return boolean|string |
|
202 | + * |
|
203 | + * This function process the template. If $this->renderAs is set, it |
|
204 | + * will produce a full page. |
|
205 | + */ |
|
206 | + public function fetchPage($additionalParams = null) { |
|
207 | + $data = parent::fetchPage($additionalParams); |
|
208 | + |
|
209 | + if( $this->renderAs ) { |
|
210 | + $page = new TemplateLayout($this->renderAs, $this->app); |
|
211 | + |
|
212 | + if(is_array($additionalParams)) { |
|
213 | + foreach ($additionalParams as $key => $value) { |
|
214 | + $page->assign($key, $value); |
|
215 | + } |
|
216 | + } |
|
217 | + |
|
218 | + // Add custom headers |
|
219 | + $headers = ''; |
|
220 | + foreach(OC_Util::$headers as $header) { |
|
221 | + $headers .= '<'.\OCP\Util::sanitizeHTML($header['tag']); |
|
222 | + if ( strcasecmp($header['tag'], 'script') === 0 && in_array('src', array_map('strtolower', array_keys($header['attributes']))) ) { |
|
223 | + $headers .= ' defer'; |
|
224 | + } |
|
225 | + foreach($header['attributes'] as $name=>$value) { |
|
226 | + $headers .= ' '.\OCP\Util::sanitizeHTML($name).'="'.\OCP\Util::sanitizeHTML($value).'"'; |
|
227 | + } |
|
228 | + if ($header['text'] !== null) { |
|
229 | + $headers .= '>'.\OCP\Util::sanitizeHTML($header['text']).'</'.\OCP\Util::sanitizeHTML($header['tag']).'>'; |
|
230 | + } else { |
|
231 | + $headers .= '/>'; |
|
232 | + } |
|
233 | + } |
|
234 | + |
|
235 | + $page->assign('headers', $headers); |
|
236 | + |
|
237 | + $page->assign('content', $data); |
|
238 | + return $page->fetchPage($additionalParams); |
|
239 | + } |
|
240 | + |
|
241 | + return $data; |
|
242 | + } |
|
243 | + |
|
244 | + /** |
|
245 | + * Include template |
|
246 | + * |
|
247 | + * @param string $file |
|
248 | + * @param array|null $additionalParams |
|
249 | + * @return string returns content of included template |
|
250 | + * |
|
251 | + * Includes another template. use <?php echo $this->inc('template'); ?> to |
|
252 | + * do this. |
|
253 | + */ |
|
254 | + public function inc( $file, $additionalParams = null ) { |
|
255 | + return $this->load($this->path.$file.'.php', $additionalParams); |
|
256 | + } |
|
257 | + |
|
258 | + /** |
|
259 | + * Shortcut to print a simple page for users |
|
260 | + * @param string $application The application we render the template for |
|
261 | + * @param string $name Name of the template |
|
262 | + * @param array $parameters Parameters for the template |
|
263 | + * @return boolean|null |
|
264 | + */ |
|
265 | + public static function printUserPage( $application, $name, $parameters = array() ) { |
|
266 | + $content = new OC_Template( $application, $name, "user" ); |
|
267 | + foreach( $parameters as $key => $value ) { |
|
268 | + $content->assign( $key, $value ); |
|
269 | + } |
|
270 | + print $content->printPage(); |
|
271 | + } |
|
272 | + |
|
273 | + /** |
|
274 | + * Shortcut to print a simple page for admins |
|
275 | + * @param string $application The application we render the template for |
|
276 | + * @param string $name Name of the template |
|
277 | + * @param array $parameters Parameters for the template |
|
278 | + * @return bool |
|
279 | + */ |
|
280 | + public static function printAdminPage( $application, $name, $parameters = array() ) { |
|
281 | + $content = new OC_Template( $application, $name, "admin" ); |
|
282 | + foreach( $parameters as $key => $value ) { |
|
283 | + $content->assign( $key, $value ); |
|
284 | + } |
|
285 | + return $content->printPage(); |
|
286 | + } |
|
287 | + |
|
288 | + /** |
|
289 | + * Shortcut to print a simple page for guests |
|
290 | + * @param string $application The application we render the template for |
|
291 | + * @param string $name Name of the template |
|
292 | + * @param array|string $parameters Parameters for the template |
|
293 | + * @return bool |
|
294 | + */ |
|
295 | + public static function printGuestPage( $application, $name, $parameters = array() ) { |
|
296 | + $content = new OC_Template( $application, $name, "guest" ); |
|
297 | + foreach( $parameters as $key => $value ) { |
|
298 | + $content->assign( $key, $value ); |
|
299 | + } |
|
300 | + return $content->printPage(); |
|
301 | + } |
|
302 | + |
|
303 | + /** |
|
304 | + * Print a fatal error page and terminates the script |
|
305 | + * @param string $error_msg The error message to show |
|
306 | + * @param string $hint An optional hint message - needs to be properly escape |
|
307 | + * @param int $statusCode |
|
308 | + * @suppress PhanAccessMethodInternal |
|
309 | + */ |
|
310 | + public static function printErrorPage( $error_msg, $hint = '', $statusCode = 500) { |
|
311 | + if (\OC::$server->getAppManager()->isEnabledForUser('theming') && !\OC_App::isAppLoaded('theming')) { |
|
312 | + \OC_App::loadApp('theming'); |
|
313 | + } |
|
314 | + |
|
315 | + |
|
316 | + if ($error_msg === $hint) { |
|
317 | + // If the hint is the same as the message there is no need to display it twice. |
|
318 | + $hint = ''; |
|
319 | + } |
|
320 | + |
|
321 | + http_response_code($statusCode); |
|
322 | + try { |
|
323 | + $content = new \OC_Template( '', 'error', 'error', false ); |
|
324 | + $errors = array(array('error' => $error_msg, 'hint' => $hint)); |
|
325 | + $content->assign( 'errors', $errors ); |
|
326 | + $content->printPage(); |
|
327 | + } catch (\Exception $e) { |
|
328 | + $logger = \OC::$server->getLogger(); |
|
329 | + $logger->error("$error_msg $hint", ['app' => 'core']); |
|
330 | + $logger->logException($e, ['app' => 'core']); |
|
331 | + |
|
332 | + header('Content-Type: text/plain; charset=utf-8'); |
|
333 | + print("$error_msg $hint"); |
|
334 | + } |
|
335 | + die(); |
|
336 | + } |
|
337 | + |
|
338 | + /** |
|
339 | + * print error page using Exception details |
|
340 | + * @param Exception|Throwable $exception |
|
341 | + * @param int $statusCode |
|
342 | + * @return bool|string |
|
343 | + * @suppress PhanAccessMethodInternal |
|
344 | + */ |
|
345 | + public static function printExceptionErrorPage($exception, $statusCode = 503) { |
|
346 | + http_response_code($statusCode); |
|
347 | + try { |
|
348 | + $request = \OC::$server->getRequest(); |
|
349 | + $content = new \OC_Template('', 'exception', 'error', false); |
|
350 | + $content->assign('errorClass', get_class($exception)); |
|
351 | + $content->assign('errorMsg', $exception->getMessage()); |
|
352 | + $content->assign('errorCode', $exception->getCode()); |
|
353 | + $content->assign('file', $exception->getFile()); |
|
354 | + $content->assign('line', $exception->getLine()); |
|
355 | + $content->assign('trace', $exception->getTraceAsString()); |
|
356 | + $content->assign('debugMode', \OC::$server->getSystemConfig()->getValue('debug', false)); |
|
357 | + $content->assign('remoteAddr', $request->getRemoteAddress()); |
|
358 | + $content->assign('requestID', $request->getId()); |
|
359 | + $content->printPage(); |
|
360 | + } catch (\Exception $e) { |
|
361 | + try { |
|
362 | + $logger = \OC::$server->getLogger(); |
|
363 | + $logger->logException($exception, ['app' => 'core']); |
|
364 | + $logger->logException($e, ['app' => 'core']); |
|
365 | + } catch (Throwable $e) { |
|
366 | + // no way to log it properly - but to avoid a white page of death we send some output |
|
367 | + header('Content-Type: text/plain; charset=utf-8'); |
|
368 | + print("Internal Server Error\n\n"); |
|
369 | + print("The server encountered an internal error and was unable to complete your request.\n"); |
|
370 | + print("Please contact the server administrator if this error reappears multiple times, please include the technical details below in your report.\n"); |
|
371 | + print("More details can be found in the server log.\n"); |
|
372 | + |
|
373 | + // and then throw it again to log it at least to the web server error log |
|
374 | + throw $e; |
|
375 | + } |
|
376 | + |
|
377 | + header('Content-Type: text/plain; charset=utf-8'); |
|
378 | + print("Internal Server Error\n\n"); |
|
379 | + print("The server encountered an internal error and was unable to complete your request.\n"); |
|
380 | + print("Please contact the server administrator if this error reappears multiple times, please include the technical details below in your report.\n"); |
|
381 | + print("More details can be found in the server log.\n"); |
|
382 | + } |
|
383 | + die(); |
|
384 | + } |
|
385 | 385 | } |
@@ -37,48 +37,48 @@ |
||
37 | 37 | |
38 | 38 | try { |
39 | 39 | |
40 | - require_once __DIR__ . '/lib/base.php'; |
|
40 | + require_once __DIR__ . '/lib/base.php'; |
|
41 | 41 | |
42 | - OC::handleRequest(); |
|
42 | + OC::handleRequest(); |
|
43 | 43 | |
44 | 44 | } catch(\OC\ServiceUnavailableException $ex) { |
45 | - \OC::$server->getLogger()->logException($ex, array('app' => 'index')); |
|
45 | + \OC::$server->getLogger()->logException($ex, array('app' => 'index')); |
|
46 | 46 | |
47 | - //show the user a detailed error page |
|
48 | - OC_Template::printExceptionErrorPage($ex, 503); |
|
47 | + //show the user a detailed error page |
|
48 | + OC_Template::printExceptionErrorPage($ex, 503); |
|
49 | 49 | } catch (\OC\HintException $ex) { |
50 | - try { |
|
51 | - OC_Template::printErrorPage($ex->getMessage(), $ex->getHint(), 503); |
|
52 | - } catch (Exception $ex2) { |
|
53 | - try { |
|
54 | - \OC::$server->getLogger()->logException($ex, array('app' => 'index')); |
|
55 | - \OC::$server->getLogger()->logException($ex2, array('app' => 'index')); |
|
56 | - } catch (Throwable $e) { |
|
57 | - // no way to log it properly - but to avoid a white page of death we try harder and ignore this one here |
|
58 | - } |
|
50 | + try { |
|
51 | + OC_Template::printErrorPage($ex->getMessage(), $ex->getHint(), 503); |
|
52 | + } catch (Exception $ex2) { |
|
53 | + try { |
|
54 | + \OC::$server->getLogger()->logException($ex, array('app' => 'index')); |
|
55 | + \OC::$server->getLogger()->logException($ex2, array('app' => 'index')); |
|
56 | + } catch (Throwable $e) { |
|
57 | + // no way to log it properly - but to avoid a white page of death we try harder and ignore this one here |
|
58 | + } |
|
59 | 59 | |
60 | - //show the user a detailed error page |
|
61 | - OC_Template::printExceptionErrorPage($ex, 500); |
|
62 | - } |
|
60 | + //show the user a detailed error page |
|
61 | + OC_Template::printExceptionErrorPage($ex, 500); |
|
62 | + } |
|
63 | 63 | } catch (\OC\User\LoginException $ex) { |
64 | - OC_Template::printErrorPage($ex->getMessage(), $ex->getMessage(), 403); |
|
64 | + OC_Template::printErrorPage($ex->getMessage(), $ex->getMessage(), 403); |
|
65 | 65 | } catch (Exception $ex) { |
66 | - \OC::$server->getLogger()->logException($ex, array('app' => 'index')); |
|
66 | + \OC::$server->getLogger()->logException($ex, array('app' => 'index')); |
|
67 | 67 | |
68 | - //show the user a detailed error page |
|
69 | - OC_Template::printExceptionErrorPage($ex, 500); |
|
68 | + //show the user a detailed error page |
|
69 | + OC_Template::printExceptionErrorPage($ex, 500); |
|
70 | 70 | } catch (Error $ex) { |
71 | - try { |
|
72 | - \OC::$server->getLogger()->logException($ex, array('app' => 'index')); |
|
73 | - } catch (Error $e) { |
|
74 | - http_response_code(500); |
|
75 | - header('Content-Type: text/plain; charset=utf-8'); |
|
76 | - print("Internal Server Error\n\n"); |
|
77 | - print("The server encountered an internal error and was unable to complete your request.\n"); |
|
78 | - print("Please contact the server administrator if this error reappears multiple times, please include the technical details below in your report.\n"); |
|
79 | - print("More details can be found in the webserver log.\n"); |
|
71 | + try { |
|
72 | + \OC::$server->getLogger()->logException($ex, array('app' => 'index')); |
|
73 | + } catch (Error $e) { |
|
74 | + http_response_code(500); |
|
75 | + header('Content-Type: text/plain; charset=utf-8'); |
|
76 | + print("Internal Server Error\n\n"); |
|
77 | + print("The server encountered an internal error and was unable to complete your request.\n"); |
|
78 | + print("Please contact the server administrator if this error reappears multiple times, please include the technical details below in your report.\n"); |
|
79 | + print("More details can be found in the webserver log.\n"); |
|
80 | 80 | |
81 | - throw $e; |
|
82 | - } |
|
83 | - OC_Template::printExceptionErrorPage($ex, 500); |
|
81 | + throw $e; |
|
82 | + } |
|
83 | + OC_Template::printExceptionErrorPage($ex, 500); |
|
84 | 84 | } |