Beispiel #1
0
 public static function sendResponse($data, $status = 200, $body = '', $content_type = 'text/html')
 {
     logAPIAction($data, $status);
     $status_header = 'HTTP/1.1 ' . $status . ' ' . rest_utils::getStatusCodeMessage($status);
     // set the status
     header($status_header);
     // set the content type
     header('Content-type: ' . $content_type);
     // pages with body are easy
     if ($body != '') {
         // send the body
         echo $body . PHP_EOL;
         exit;
     } else {
         // we need to create the body if none is passed
         // create some body messages
         $message = '';
         // this is purely optional, but makes the pages a little nicer to read
         // for your users.  Since you won't likely send a lot of different status codes,
         // this also shouldn't be too ponderous to maintain
         switch ($status) {
             case 401:
                 $message = self::$MESSAGE_401;
                 break;
             case 404:
                 $message = sprintf(self::$MESSAGE_404, $_SERVER['REQUEST_URI']);
                 break;
             case 500:
                 $message = self::$MESSAGE_500;
                 break;
             case 501:
                 $message = self::$MESSAGE_501;
                 break;
         }
         // servers don't always have a signature turned on (this is an apache directive "ServerSignature On")
         $signature = $_SERVER['SERVER_SIGNATURE'] == '' ? $_SERVER['SERVER_SOFTWARE'] . ' Server at ' . $_SERVER['SERVER_NAME'] . ' Port ' . $_SERVER['SERVER_PORT'] : $_SERVER['SERVER_SIGNATURE'];
         $status_code_message = rest_utils::getStatusCodeMessage($status);
         ob_start();
         require LIBRARY . '/view/rest.phtml';
         $body = ob_get_clean();
         echo $body;
         exit;
     }
 }
function logAPIAction($data = false, $status)
{
    if (is_int($data)) {
        $method = $data;
        $vs = array();
    } else {
        $method = $data->getMethod();
        $vs = $data->getRequestVars();
    }
    $out = sprintf("[%s] %s %s %s %s [%s]\n", date('Y-m-d H:i:s', time()), strtoupper($method), $_SERVER['REQUEST_URI'], $status, rest_utils::getStatusCodeMessage($status), implode(',', array_keys($vs)));
    file_put_contents(ROOT . '/log/api.log', $out, FILE_APPEND);
}