Beispiel #1
0
function ajaxShutdown()
{
    $isError = false;
    if ($error = error_get_last()) {
        switch ($error['type']) {
            case E_ERROR:
            case E_CORE_ERROR:
            case E_COMPILE_ERROR:
            case E_USER_ERROR:
            case E_PARSE:
                $log_file = ini_get("error_log");
                if (file_exists($log_file)) {
                    $number_of_characters_to_get = 5000;
                    $bf = new BigFile($log_file);
                    $text = $bf->getFromEnd($number_of_characters_to_get);
                    $lines = array_reverse(explode("\n", $text));
                    $backtrace = "";
                    foreach ($lines as $i => $line) {
                        if (strstr($line, "Fatal")) {
                            for ($j = $i; $j > 0; $j--) {
                                $backtrace .= $lines[$j] . "\n";
                            }
                            break;
                        }
                    }
                    if ($backtrace == "") {
                        $backtrace = "No Fatal error found in the last {$number_of_characters_to_get} lines of {$log_file}";
                    }
                } else {
                    $backtrace = "No error log found at " . $log_file;
                }
                $isError = true;
                break;
        }
    }
    if ($isError) {
        send_http_error(new Exception("E_ERROR " . $error['message'] . "\n file: " . $error['file'] . " (" . $error['line'] . ")", E_FATAL_ERROR), "FATAL ERROR. shutdown function tried to restore backtrace from php error log ({$log_file}):\n" . $backtrace, true);
        $response = new HttpResponse();
        $response->setStatus("400 Bad Request");
        $response->write("E_ERROR " . $error['message'] . "\n file: " . $error['file'] . " (" . $error['line'] . ")");
        $response->flush();
    }
}
 public static function curl($url, $httpMethod = "GET", $postFields = null, $headers = null)
 {
     $ch = curl_init();
     curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $httpMethod);
     if (ENABLE_HTTP_PROXY) {
         curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_BASIC);
         curl_setopt($ch, CURLOPT_PROXY, HTTP_PROXY_IP);
         curl_setopt($ch, CURLOPT_PROXYPORT, HTTP_PROXY_PORT);
         curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
     }
     curl_setopt($ch, CURLOPT_URL, $url);
     curl_setopt($ch, CURLOPT_FAILONERROR, false);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($ch, CURLOPT_POSTFIELDS, is_array($postFields) ? self::getPostHttpBody($postFields) : $postFields);
     if (self::$readTimeout) {
         curl_setopt($ch, CURLOPT_TIMEOUT, self::$readTimeout);
     }
     if (self::$connectTimeout) {
         curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, self::$connectTimeout);
     }
     //https request
     if (strlen($url) > 5 && strtolower(substr($url, 0, 5)) == "https") {
         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
         curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
     }
     if (is_array($headers) && 0 < count($headers)) {
         $httpHeaders = self::getHttpHearders($headers);
         curl_setopt($ch, CURLOPT_HTTPHEADER, $httpHeaders);
     }
     $httpResponse = new HttpResponse();
     $httpResponse->setBody(curl_exec($ch));
     $httpResponse->setStatus(curl_getinfo($ch, CURLINFO_HTTP_CODE));
     if (curl_errno($ch)) {
         throw new ClientException("Speicified endpoint or uri is not valid.", "SDK.ServerUnreachable");
     }
     curl_close($ch);
     return $httpResponse;
 }
 /**
  *	Parses the raw HTTP response and returns a response object
  **/
 protected function parseResponse($output, $ch = null)
 {
     $response = new HttpResponse();
     if ($output) {
         $lines = explode("\n", $output);
         $isHeader = true;
         $buffer = array();
         foreach ($lines as $line) {
             if ($isHeader) {
                 if (preg_match('/^\\s*$/', $line)) {
                     // Header/body separator
                     $isHeader = false;
                 } else {
                     // This is a real HTTP header
                     if (preg_match('/^([^:]+)\\:(.*)$/', $line, $matches)) {
                         //echo "HEADER: [", $matches[1], ']: [', $matches[2], "]\n";
                         $name = trim($matches[1]);
                         $value = trim($matches[2]);
                         $response->addHeader($name, $value);
                     } else {
                         // This is the status response
                         //echo "HEADER: ", trim($line), "\n";
                         if (preg_match('/^(HTTP\\/\\d\\.\\d) (\\d*) (.*)$/', trim($line), $matches)) {
                             $response->setStatus($matches[2]);
                             $response->setStatusMsg($matches[3]);
                             $response->setVersion($matches[1]);
                         }
                     }
                 }
             } else {
                 $buffer[] = $line;
             }
         }
         // The buffer is the HTTP Entity Body
         $response->setBody(implode("\n", $buffer));
     } else {
         $statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
         if ($statusCode == 0) {
             $response->setStatus(502);
             $response->setStatusMsg('CURL Error');
         } else {
             $response->setStatus($statusCode);
             $response->setStatusMsg('CURL Response');
         }
     }
     return $response;
 }
Beispiel #4
0
 public function handleRequest(AjaxRequestObject $ajaxRequestObject, AjaxResponseObject $ajaxResponseObject)
 {
     $response = new HttpResponse();
     $extension = ExtensionMaster::getInstance()->getExtensionForNamespace($ajaxRequestObject->getNamespace());
     if (isset($extension)) {
         if ($extension instanceof ICommandAdapter) {
             if ($ajaxRequestObject->getCommand() != "") {
                 if ($ajaxRequestObject->getCommand() == "databinding") {
                     $command = new Databinding();
                 } else {
                     if ($extension->isValidCommand($ajaxRequestObject->getCommand())) {
                         $command = $extension->getCommand($ajaxRequestObject->getCommand());
                     }
                 }
                 if ($command instanceof IAjaxCommand && $command->validateData($ajaxRequestObject)) {
                     try {
                         $command->processData($ajaxRequestObject);
                         $ajaxResponseObject = $command->ajaxResponse($ajaxResponseObject);
                     } catch (Exception $e) {
                         $response->setStatus("400 Bad Request");
                         $response->write("Command processing error: \"{$e->getMessage()}\"");
                         $response->flush();
                         if (DEVELOPMENT_MODE) {
                             throw $e;
                         }
                         exit;
                     }
                     if ($ajaxResponseObject instanceof AjaxResponseObject) {
                         $data = \Widgets\Widget::getData($ajaxResponseObject->getWidgets());
                         $stat = "";
                         if ($_SESSION["STATISTICS_LEVEL"] > 0) {
                             $stat = "console.log('Serveranfragen: " . $GLOBALS["STEAM"]->get_request_count() . " / " . $GLOBALS["STEAM"]->get_globalrequest_count() . "');";
                         }
                         if ($_SESSION["STATISTICS_LEVEL"] > 1 && isset($GLOBALS["page_time_start"])) {
                             $requestMap = $GLOBALS["STEAM"]->get_globalrequest_map();
                             $requestTime = $GLOBALS["STEAM"]->get_globalrequest_time();
                             $requestString = "";
                             foreach ($requestMap as $method => $count) {
                                 $requestString .= "console.log('Methode {$method} -> {$count} mal in " . round($requestTime[$method] * 1000) . " ms');";
                             }
                             $stat .= "console.log('Zeit: " . round((microtime(TRUE) - $GLOBALS["page_time_start"]) * 1000) . " ms');" . $requestString;
                         }
                         header("Content-type: text/plain");
                         $response->write(json_encode(array("status" => $ajaxResponseObject->getStatus(), "html" => $data["html"], "data" => $ajaxResponseObject->getData(), "css" => $data["css"], "js" => $data["js"] . $stat, "postjs" => $data["postjs"])));
                         $response->flush();
                         exit;
                     } else {
                         $response->setStatus("400 Bad Request");
                         $response->write("Wrong response type for \"{$ajaxRequestObject->getCommand()}\"");
                         $response->flush();
                         exit;
                     }
                 } else {
                     $response->setStatus("400 Bad Request");
                     $response->write("Command \"{$ajaxRequestObject->getCommand()}\" not valid.");
                     $response->flush();
                     exit;
                 }
             } else {
                 $response->setStatus("400 Bad Request");
                 $response->write("Command parameter missing.");
                 $response->flush();
                 exit;
             }
         } else {
             $response->setStatus("400 Bad Request");
             $response->write("Extension doesn't support commands.");
             $response->flush();
             exit;
         }
     } else {
         $response->setStatus("400 Bad Request");
         $response->write("Not extension found for url");
         $response->flush();
         exit;
     }
 }
 /**
  * Rejects the request.
  *
  * @param string $message
  * @param int $httpResponseCode
  */
 public function reject($message = "Rejected", $httpResponseCode = 400)
 {
     $this->httpResponse->setStatus($httpResponseCode . ' ' . $message);
 }
Beispiel #6
0
<?php

require_once "errors.php";
require_once "Exceptions.php";
require_once "db.php";
require_once 'HttpRequest.php';
require_once 'HttpResponse.php';
require_once 'Authenticator.php';
require_once 'Router.php';
require_once 'Trips.php';
$resp = new HttpResponse();
try {
    $db = new mysqli($dbhost, $dbuser, $dbpw, $dbdb);
    $req = new HttpRequest();
    $auth = new Authenticator($db, $req);
    //the router handles all of the other stuff (loading controller etc.)
    $router = new Router($req, $resp, $db, $auth);
    $content = $router->executeRoute();
    $resp->write($content);
    $resp->flush();
} catch (Exception $e) {
    $resp->setStatus(400);
    $resp->addHeader('error', $e->getMessage());
    $resp->write('{"exception" : "' . $e->getMessage() . ' [' . $e->getFile() . '#' . $e->getLine() . ']"}');
    $resp->flush();
}