*/ class helper_plugin_sentry extends DokuWiki_Plugin { /** * Parse the DSN configuration into its parts * * @return array */ protected function parseDSN() { $parts = parse_url($this->getConf('dsn')); $dsn = []; $dsn['protocol'] = $parts['scheme']; $dsn['public'] = $parts['user']; $dsn['secret'] = $parts['pass']; $dsn['project'] = (int)basename($parts['path']); $dsn['url'] = $parts['host']; if (!empty($parts['port'])) $dsn['url'] .= ':' . $parts['port']; $path = dirname($parts['path']); $path = trim($path, '/'); if (!empty($path)) { $path = '/' . $path; } $dsn['path'] = $path; return $dsn; } /** * Return the API endpoint to store messages * * @return string */ protected function storeAPI() { $dsn = $this->parseDSN(); return $dsn['protocol'] . '://' . $dsn['url'] . $dsn['path'] . '/api/' . $dsn['project'] . '/store/'; } /** * Return the X-Sentry-Auth header * * @return string */ protected function storeAuthHeader() { $dsn = $this->parseDSN(); $header[] = 'Sentry sentry_version=7'; $header[] = 'sentry_client=' . Event::CLIENT . Event::VERSION; $header[] = 'sentry_timestamp=' . time(); $header[] = 'sentry_key=' . $dsn['public']; $header[] = 'sentry_secret=' . $dsn['secret']; return join(', ', $header); } /** * Log an exception * * If you need more control over the logged Event, use logEvent() * * @param \Throwable|\Exception $e */ public function logException($e) { $this->logEvent(Event::fromException($e)); } /** * Log an event * * @param Event $event */ public function logEvent(Event $event) { $this->saveEvent($event); if ($this->sendEvent($event)) $this->deleteEvent($event->getID()); } /** * Log a message and optionally some data to sentry * * @param string $message the raw message string * @param array $extra */ public function logMessage($message, array $extra = []) { $backtrace = debug_backtrace(); array_shift($backtrace); // remove this logMessage method $eventData = [ 'sentry.interfaces.Message' => [ 'message' => $message, ], 'stacktrace' => ['frames' => Event::backTraceFrames($backtrace)], 'extra' => $extra, ]; $event = new Event($eventData); $event->setLogLevel('info'); $this->logEvent($event); } /** * Format an exception for the user in HTML * * @param \Throwable|\Exception $e * @return string the HTML */ public function formatException($e) { global $conf; $html = '
' . hsc(get_class($e)) . ': ' . $e->getMessage() . '
'; if ($conf['allowdebug']) { $html .= '' . hsc($e->getFile()) . ':' . hsc($e->getLine()) . '
' . hsc($e->getTraceAsString()) . ''; } $html .= '
The error has been logged.
'; $html .= '