/** * * @param Exception|Throwable $exception */ private function reportAndThrow($exception) { if (headers_sent() === false && DebugR::isEnabled()) { if ($exception->getFile()) { $file = preg_replace('/^' . preg_quote(dirname(\Sledgehammer\VENDOR_DIR), '/') . '/', '', $exception->getFile()); $location = "\nin " . $file . ' on line ' . $exception->getLine(); } else { $location = ''; } DebugR::warning(get_class($exception) . "\n" . $exception->getMessage() . $location); } throw $exception; }
/** * Send a response based on the request. */ public function handleRequest() { // Build document $document = $this->generateDocument(); if (!defined('Sledgehammer\\GENERATED')) { define('Sledgehammer\\GENERATED', microtime(true)); } // Send headers $headers = $document->getHeaders(); \Sledgehammer\send_headers($headers['http']); // Send the sledgehammer-statusbar as DebugR header. if (DebugR::isEnabled()) { ob_start(); statusbar(); DebugR::send('sledgehammer-statusbar', ob_get_clean(), true); } // Send the contents $document->render(); }
/** * Inspect the worpress database and. */ public static function initialize() { static $initialized = false; if ($initialized) { return; } $initialized = true; if (function_exists('the_post') === false) { throw new Exception('Wordpress is not yet initialized'); } // Lazy database connection Connection::$instances['wordpress'] = function () { if (defined('DB_USER') === false) { throw new Exception('No database configured'); } $connection = new Connection('mysql://' . DB_USER . ':' . DB_PASSWORD . '@' . DB_HOST . '/' . DB_NAME); if (empty(Logger::$instances['Database[wordpress]'])) { $index = array_search($connection->logger, Logger::$instances); unset(Logger::$instances[$index]); Logger::$instances['Database[wordpress]'] = $connection->logger; } return $connection; }; if (empty(Connection::$instances['default'])) { Connection::$instances['default'] = 'wordpress'; } // Sledgehammer ORM configuration Repository::configureDefault(function ($repo) { $repo->registerBackend(new WordpressRepositoryBackend()); }); if (defined('Sledgehammer\\WEBPATH') === false) { $url = new Url(WP_HOME); $path = $url->path === '/' ? '/' : $url->path . '/'; define('Sledgehammer\\WEBPATH', $path); define('Sledgehammer\\WEBROOT', $path); } define('WEBPATH', \Sledgehammer\WEBPATH); if (WP_DEBUG || !empty($_GET[\Sledgehammer\DEBUG_VAR])) { add_action('admin_enqueue_scripts', function () { wp_enqueue_style('sh-debug', '/../core/css/debug.css'); }); add_action('admin_footer', [self::class, 'statusbar']); add_action('wp_footer', [self::class, 'statusbar']); add_action('send_headers', function () { if (DebugR::isEnabled()) { ob_start(); echo $_SERVER['REQUEST_METHOD'] . ' ' . $_SERVER['REQUEST_URI']; \Sledgehammer\statusbar(); DebugR::send('sledgehammer-statusbar', ob_get_clean(), true); } }, PHP_INT_MAX); add_filter('template_include', function ($template) { if (empty(ErrorHandler::$instances['default'])) { ErrorHandler::enable(); } if (defined('Sledgehammer\\GENERATED') === false) { define('Sledgehammer\\GENERATED', microtime(true)); } return $template; }, PHP_INT_MAX); } }
/** * De fout afhandelen volgens de afhandel opties (email, log, html). * * @param int $type E_USER_NOTICE, E_USER_ERROR, enz * @param string $message De foutmelding * @param mixed $information Extra informatie voor de fout. bv array('Tip' => 'Controleer ...') */ private function process($type, $message, $information = null) { if ($this->isProcessing) { echo '<span style="color:red">[ErrorHandler failure]'; if ($this->html && is_string($message)) { // show error? if ($message === '__UNCAUGHT_EXCEPTION__' && $this->isThrowable($type)) { echo ' ', htmlentities($type->getMessage() . ' in ' . $type->getFile() . ' on line ' . $type->getLine()); } else { echo ' ', htmlentities($message); } } echo ' </span>'; error_log('ErrorHandler failure: ' . $message); return; } $this->isProcessing = true; if ($this->debugR) { $this->debugR = headers_sent() === false; // Update debugr setting. } if ($this->log || $this->cli || $this->debugR) { if (self::isThrowable($type)) { $error_message = get_class($type) . ': ' . $type->getMessage(); } else { $error_message = $this->error_types[$type] . ': ' . $message; } $location = $this->resolveLocation(); if ($location) { $error_message .= ' in ' . $location['file'] . ' on line ' . $location['line']; } if ($this->log) { error_log($error_message); } if ($this->debugR) { if (class_exists('Sledgehammer\\Core\\Debug\\DebugR', false) === false) { require_once __DIR__ . '/DebugR.php'; } if (class_exists('Sledgehammer\\Core\\Json', false) === false) { require_once __DIR__ . '/../Json.php'; } if (self::isThrowable($type) || in_array($type, array(E_USER_ERROR, E_ERROR, 'EXCEPTION'))) { DebugR::error($error_message); } else { DebugR::warning($error_message); } } } // Limiet contoleren if ($this->email) { // Een foutrapport via email versturen? $limit = $this->importEmailLimit(); if ($limit < 1) { // Is de limiet bereikt? $this->email = false; // Niet emailen } } $buffer = $this->email || $this->debugR; if ($buffer) { // store the html of the error-report in a buffer ob_start(); } elseif (!$this->html) { // No html version required? $this->isProcessing = false; return; // Processing is complete } self::render($type, $message, $information); if ($buffer) { $html = ob_get_clean(); if ($this->debugR) { DebugR::html($html); } if ($this->email) { // email versturen? // Headers voor de email met HTML indeling $headers = "MIME-Version: 1.0\n"; $headers .= 'Content-type: text/html; charset=' . Framework::$charset . "\n"; $headers .= 'From: ' . $this->fromEmail() . "\n"; if (self::isThrowable($type)) { $subject = get_class($type) . ': ' . $type->getMessage(); if ($message === '__UNCAUGHT_EXCEPTION__') { $subject = ' Uncaught ' . $subject; } } else { $subject = $this->error_types[$type] . ': ' . $message; } if (function_exists('mail') && !mail($this->email, $subject, '<html><body style="background-color: #fcf8e3">' . $html . "</body></html>\n", $headers)) { error_log('The ' . self::class . ' was unable to email the report.'); } } if ($this->html) { if ($this->headers && headers_sent() === false) { if (function_exists('http_response_code')) { // PHP 5.4 if (http_response_code() === 200) { http_response_code(500); } } header('Content-Type: text/html; charset=' . Framework::$charset); } echo $html; // buffer weergeven if (ob_get_level() === 0) { flush(); } } } if ($this->cli) { echo '[', date('Y-m-d H:i:s'), '] ', $error_message, "\n"; } $this->isProcessing = false; }
define('Sledgehammer\\TMP_DIR', $__TMP_DIR); unset($__TMP_DIR); } // Case insensitive "natural order" sorting method for natcasesort() in Collection->orderBy() define('Sledgehammer\\SORT_NATURAL_CI', -2); // Regex for supported operators for the compare() function define('Sledgehammer\\COMPARE_OPERATORS', '==|!=|<|<=|>|>=|IN|NOT IN|LIKE|NOT LIKE'); /** * 2. Declare public functions. */ require_once __DIR__ . '/functions.php'; // Namespaced functions require_once __DIR__ . '/helpers.php'; // Global functions (but not guaranteed) \Sledgehammer\mkdirs(\Sledgehammer\TMP_DIR); /* * 3. Configure and register the AutoLoader */ require_once __DIR__ . '/Object.php'; require_once __DIR__ . '/Debug/Autoloader.php'; spl_autoload_register('Sledgehammer\\Core\\Debug\\AutoLoader::lazyRegister'); /* * 4. Set DebugR statusbar header. */ if (headers_sent() === false) { DebugR::send('sledgehammer-statusbar', 'no statusbar data.', true); } /* * Timestamp (in microseconds) for when the Sledgehammer Framework was initialized. */ define('Sledgehammer\\INITIALIZED', microtime(true));
/** * Reports the error/exception to the ErrorHandler and returns the error as Json object. * The javascript client should detect and report the error to the user: * if (result.success !== true) { alert(result.error); }. * * @param string|Exception $error The error message or Exception * @param int $http The HTTP status code (defaults to 400 Bad Request) * * @return Json */ public static function error($error, $http = 400) { if (headers_sent() === false && DebugR::isEnabled()) { ErrorHandler::instance()->html = false; } if ($error instanceof Exception) { \Sledgehammer\report_exception($error); $error = $error->getMessage(); } else { \Sledgehammer\warning($error); } return new self(array('success' => false, 'error' => $error), array('http' => array('Status' => $http . ' ' . Framework::$statusCodes[$http], 'Content-Type' => ErrorHandler::instance()->html ? 'text/html; charset=utf-8' : 'application/json'))); }