1<?php
2/***********************************************
3* File      :   compat.php
4* Project   :   Z-Push
5* Descr     :   Help function for files
6*
7* Created   :   01.10.2007
8*
9* Copyright 2007 - 2016 Zarafa Deutschland GmbH
10*
11* This program is free software: you can redistribute it and/or modify
12* it under the terms of the GNU Affero General Public License, version 3,
13* as published by the Free Software Foundation.
14*
15* This program is distributed in the hope that it will be useful,
16* but WITHOUT ANY WARRANTY; without even the implied warranty of
17* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18* GNU Affero General Public License for more details.
19*
20* You should have received a copy of the GNU Affero General Public License
21* along with this program.  If not, see <http://www.gnu.org/licenses/>.
22*
23* Consult LICENSE file for details
24************************************************/
25
26if (!function_exists("quoted_printable_encode")) {
27    /**
28     * Process a string to fit the requirements of RFC2045 section 6.7. Note that
29     * this works, but replaces more characters than the minimum set. For readability
30     * the spaces and CRLF pairs aren't encoded though.
31     *
32     * @param string    $string     string to be encoded
33     *
34     * @see http://www.php.net/manual/en/function.quoted-printable-decode.php#89417
35     */
36    function quoted_printable_encode($string) {
37        return preg_replace('/[^\r\n]{73}[^=\r\n]{2}/', "$0=\n", str_replace(array('%20', '%0D%0A', '%'), array(' ', "\r\n", '='), rawurlencode($string)));
38    }
39}
40
41if (!function_exists("apache_request_headers")) {
42    /**
43      * When using other webservers or using php as cgi in apache
44      * the function apache_request_headers() is not available.
45      * This function parses the environment variables to extract
46      * the necessary headers for Z-Push
47      */
48    function apache_request_headers() {
49        $headers = array();
50        foreach ($_SERVER as $key => $value)
51            if (substr($key, 0, 5) == 'HTTP_')
52                $headers[strtr(substr($key, 5), '_', '-')] = $value;
53
54        return $headers;
55    }
56}
57
58if (!function_exists("hex2bin")) {
59    /**
60     * Complementary function to bin2hex() which converts a hex entryid to a binary entryid.
61     * Since PHP 5.4 an internal hex2bin() implementation is available.
62     *
63     * @param string    $data   the hexadecimal string
64     *
65     * @returns string
66     */
67    function hex2bin($data) {
68        return pack("H*", $data);
69    }
70}
71
72if (!function_exists('http_response_code')) {
73    /**
74     * http_response_code does not exists in PHP < 5.4
75     * http://php.net/manual/en/function.http-response-code.php
76     */
77    function http_response_code($code = NULL) {
78        if ($code !== NULL) {
79            switch ($code) {
80                case 100: $text = 'Continue'; break;
81                case 101: $text = 'Switching Protocols'; break;
82                case 200: $text = 'OK'; break;
83                case 201: $text = 'Created'; break;
84                case 202: $text = 'Accepted'; break;
85                case 203: $text = 'Non-Authoritative Information'; break;
86                case 204: $text = 'No Content'; break;
87                case 205: $text = 'Reset Content'; break;
88                case 206: $text = 'Partial Content'; break;
89                case 300: $text = 'Multiple Choices'; break;
90                case 301: $text = 'Moved Permanently'; break;
91                case 302: $text = 'Moved Temporarily'; break;
92                case 303: $text = 'See Other'; break;
93                case 304: $text = 'Not Modified'; break;
94                case 305: $text = 'Use Proxy'; break;
95                case 400: $text = 'Bad Request'; break;
96                case 401: $text = 'Unauthorized'; break;
97                case 402: $text = 'Payment Required'; break;
98                case 403: $text = 'Forbidden'; break;
99                case 404: $text = 'Not Found'; break;
100                case 405: $text = 'Method Not Allowed'; break;
101                case 406: $text = 'Not Acceptable'; break;
102                case 407: $text = 'Proxy Authentication Required'; break;
103                case 408: $text = 'Request Time-out'; break;
104                case 409: $text = 'Conflict'; break;
105                case 410: $text = 'Gone'; break;
106                case 411: $text = 'Length Required'; break;
107                case 412: $text = 'Precondition Failed'; break;
108                case 413: $text = 'Request Entity Too Large'; break;
109                case 414: $text = 'Request-URI Too Large'; break;
110                case 415: $text = 'Unsupported Media Type'; break;
111                case 500: $text = 'Internal Server Error'; break;
112                case 501: $text = 'Not Implemented'; break;
113                case 502: $text = 'Bad Gateway'; break;
114                case 503: $text = 'Service Unavailable'; break;
115                case 504: $text = 'Gateway Time-out'; break;
116                case 505: $text = 'HTTP Version not supported'; break;
117                default:
118                    exit('Unknown http status code "' . htmlentities($code) . '"');
119                    break;
120            }
121
122            $protocol = (isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0');
123            header($protocol . ' ' . $code . ' ' . $text);
124
125            $GLOBALS['http_response_code'] = $code;
126        } else {
127            $code = (isset($GLOBALS['http_response_code']) ? $GLOBALS['http_response_code'] : 200);
128        }
129
130        return $code;
131    }
132}
133
134if (!function_exists('memory_get_peak_usage')) {
135    /**
136     * memory_get_peak_usage is not available prior to PHP 5.2.
137     * This complementary function will return the value of memory_get_usage();
138     * @see http://php.net/manual/en/function.memory-get-usage.php
139     * @see http://php.net/manual/en/function.memory-get-peak-usage.php
140     *
141     * @param boolean $real_usage
142     */
143    function memory_get_peak_usage($real_usage = false) {
144        ZLog::Write(LOGLEVEL_DEBUG, "memory_get_peak_usage() is not available on this system. The value of memory_get_usage() will be used.");
145        return memory_get_usage();
146    }
147
148}