summaryrefslogtreecommitdiff
path: root/public_html/debug.php
blob: af03921590e7937d97a0a3675e96bb8e4a6e10ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php

/**
 * Kolab WebDAV/CalDAV/CardDAV Debug Pipe
 *
 * @author Thomas Bruederli <bruederli@kolabsys.com>
 *
 * Copyright (C) 2013, Kolab Systems AG <contact@kolabsys.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

// define some environment variables used throughout the app and libraries
define('KOLAB_DAV_ROOT', realpath('../'));
define('KOLAB_DAV_START', microtime(true));

define('RCUBE_INSTALL_PATH', KOLAB_DAV_ROOT . '/');
define('RCUBE_CONFIG_DIR',   KOLAB_DAV_ROOT . '/config/');

ini_set('error_reporting', E_ALL &~ E_NOTICE &~ E_STRICT);

require_once KOLAB_DAV_ROOT . '/vendor/autoload.php';
require_once KOLAB_DAV_ROOT . '/lib/Roundcube/bootstrap.php';

// Roundcube framework initialization
$rcube = rcube::get_instance();
$rcube->config->load_from_file(RCUBE_CONFIG_DIR . 'dav.inc.php');

$base_uri = $rcube->config->get('base_uri', slashify(substr(dirname($_SERVER['SCRIPT_FILENAME']), strlen($_SERVER['DOCUMENT_ROOT']))));

// quick & dirty request debugging
$http_headers = array();
foreach (apache_request_headers() as $hdr => $value) {
    if ($hdr == 'Destination')
        $value = str_replace($base_uri, $base_uri . 'index.php/', $value);
    $http_headers[$hdr] = "$hdr: $value";
}
// read HTTP request body
$in = fopen('php://input', 'r');
$http_body = stream_get_contents($in);
fclose($in);

$rcube->write_log('davdebug', $_SERVER['REQUEST_METHOD'] . ' ' . $_SERVER['REQUEST_URI'] . ' ' . $_SERVER['SERVER_PROTOCOL'] . "\n" .
    join("\n", $http_headers) . "\n\n" . $http_body);

// fix URIs in request body
$http_body = preg_replace("!(<d:href>$base_uri)!i", '\\1index.php/', $http_body);
$http_headers['Content-Length'] = "Content-Length: " . strlen($http_body);

// forward the full request to index.php
$rel_url = substr($_SERVER['REQUEST_URI'], strlen($base_uri));
$host = $_SERVER['HTTP_HOST'];
$port = 80;
$path = $base_uri . 'index.php/' . $rel_url;

// remove Host: header
unset($http_headers['Host']);
$response_headers = array();

// re-send using curl
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,            "http://$host:$port$path");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST,  $_SERVER['REQUEST_METHOD']);
curl_setopt($ch, CURLOPT_POSTFIELDS,     $http_body);
curl_setopt($ch, CURLOPT_HTTPHEADER,     array_values($http_headers));
curl_setopt($ch, CURLOPT_HEADER,         0);
curl_setopt($ch, CURLOPT_HEADERFUNCTION, function($ch, $header) use (&$response_headers){
    list($key, $val) = explode(": ", rtrim($header), 2);
    if (!empty($val)) $response_headers[$key] = $val;
    return strlen($header);
});

$result = str_replace('index.php/', '', curl_exec($ch));
$info = curl_getinfo($ch);

// send recieved HTTP status code
$result_headers = $_SERVER['SERVER_PROTOCOL'] . " " . $info['http_code'] . " " . http_response_phrase($info['http_code']);
header($result_headers, true);

// forward response headers
unset($response_headers['Transfer-Encoding']);
foreach ($response_headers as $hdr => $value) {
    $result_headers .= "\n$hdr: " . $value;
    header("$hdr: " . $value, true);
}

// log response
$rcube->write_log('davdebug', "RESPONSE:\n" . $result_headers . "\n\n" . (strpos($info['content_type'], 'image/') === false ? $result : ''));

// send response body back to client
echo $result;


/**
 * Derived from HTTP_Request2_Response::getDefaultReasonPhrase()
 */
function http_response_phrase($code)
{
    static $phrases = array(

        // 1xx: Informational - Request received, continuing process
        100 => 'Continue',
        101 => 'Switching Protocols',

        // 2xx: Success - The action was successfully received, understood and
        // accepted
        200 => 'OK',
        201 => 'Created',
        202 => 'Accepted',
        203 => 'Non-Authoritative Information',
        204 => 'No Content',
        205 => 'Reset Content',
        206 => 'Partial Content',
        207 => 'Multi-Status',

        // 3xx: Redirection - Further action must be taken in order to complete
        // the request
        300 => 'Multiple Choices',
        301 => 'Moved Permanently',
        302 => 'Found',  // 1.1
        303 => 'See Other',
        304 => 'Not Modified',
        305 => 'Use Proxy',
        307 => 'Temporary Redirect',

        // 4xx: Client Error - The request contains bad syntax or cannot be
        // fulfilled
        400 => 'Bad Request',
        401 => 'Unauthorized',
        402 => 'Payment Required',
        403 => 'Forbidden',
        404 => 'Not Found',
        405 => 'Method Not Allowed',
        406 => 'Not Acceptable',
        407 => 'Proxy Authentication Required',
        408 => 'Request Timeout',
        409 => 'Conflict',
        410 => 'Gone',
        411 => 'Length Required',
        412 => 'Precondition Failed',
        413 => 'Request Entity Too Large',
        414 => 'Request-URI Too Long',
        415 => 'Unsupported Media Type',
        416 => 'Requested Range Not Satisfiable',
        417 => 'Expectation Failed',

        // 5xx: Server Error - The server failed to fulfill an apparently
        // valid request
        500 => 'Internal Server Error',
        501 => 'Not Implemented',
        502 => 'Bad Gateway',
        503 => 'Service Unavailable',
        504 => 'Gateway Timeout',
        505 => 'HTTP Version Not Supported',
        509 => 'Bandwidth Limit Exceeded',
    );

    return $phrases[$code];
}