annotate program/lib/Roundcube/rcube_utils.php @ 11:aff04b06b685 default tip

various small fixes from upgrades to PHP and/or hangover from fix to apt-get overwrite at beginning of the year somehow
author Charlie Root
date Sun, 26 Jan 2025 13:09:03 -0500
parents 4681f974d28b
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1 <?php
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4 +-----------------------------------------------------------------------+
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
5 | This file is part of the Roundcube Webmail client |
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
6 | Copyright (C) 2008-2012, The Roundcube Dev Team |
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
7 | Copyright (C) 2011-2012, Kolab Systems AG |
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
8 | |
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
9 | Licensed under the GNU General Public License version 3 or |
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
10 | any later version with exceptions for skins & plugins. |
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
11 | See the README file for a full license statement. |
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
12 | |
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
13 | PURPOSE: |
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
14 | Utility class providing common functions |
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
15 +-----------------------------------------------------------------------+
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
16 | Author: Thomas Bruederli <roundcube@gmail.com> |
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
17 | Author: Aleksander Machniak <alec@alec.pl> |
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
18 +-----------------------------------------------------------------------+
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
19 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
20
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
21 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
22 * Utility class providing common functions
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
23 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
24 * @package Framework
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
25 * @subpackage Utils
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
26 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
27 class rcube_utils
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
28 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
29 // define constants for input reading
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
30 const INPUT_GET = 1;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
31 const INPUT_POST = 2;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
32 const INPUT_COOKIE = 4;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
33 const INPUT_GP = 3; // GET + POST
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
34 const INPUT_GPC = 7; // GET + POST + COOKIE
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
35
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
36
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
37 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
38 * Helper method to set a cookie with the current path and host settings
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
39 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
40 * @param string Cookie name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
41 * @param string Cookie value
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
42 * @param string Expiration time
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
43 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
44 public static function setcookie($name, $value, $exp = 0)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
45 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
46 if (headers_sent()) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
47 return;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
48 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
49
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
50 $cookie = session_get_cookie_params();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
51 $secure = $cookie['secure'] || self::https_check();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
52
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
53 setcookie($name, $value, $exp, $cookie['path'], $cookie['domain'], $secure, true);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
54 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
55
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
56 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
57 * E-mail address validation.
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
58 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
59 * @param string $email Email address
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
60 * @param boolean $dns_check True to check dns
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
61 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
62 * @return boolean True on success, False if address is invalid
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
63 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
64 public static function check_email($email, $dns_check=true)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
65 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
66 // Check for invalid characters
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
67 if (preg_match('/[\x00-\x1F\x7F-\xFF]/', $email)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
68 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
69 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
70
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
71 // Check for length limit specified by RFC 5321 (#1486453)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
72 if (strlen($email) > 254) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
73 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
74 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
75
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
76 $email_array = explode('@', $email);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
77
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
78 // Check that there's one @ symbol
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
79 if (count($email_array) < 2) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
80 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
81 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
82
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
83 $domain_part = array_pop($email_array);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
84 $local_part = implode('@', $email_array);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
85
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
86 // from PEAR::Validate
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
87 $regexp = '&^(?:
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
88 ("\s*(?:[^"\f\n\r\t\v\b\s]+\s*)+")| #1 quoted name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
89 ([-\w!\#\$%\&\'*+~/^`|{}=]+(?:\.[-\w!\#\$%\&\'*+~/^`|{}=]+)*)) #2 OR dot-atom (RFC5322)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
90 $&xi';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
91
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
92 if (!preg_match($regexp, $local_part)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
93 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
94 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
95
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
96 // Validate domain part
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
97 if (preg_match('/^\[((IPv6:[0-9a-f:.]+)|([0-9.]+))\]$/i', $domain_part, $matches)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
98 return self::check_ip(preg_replace('/^IPv6:/i', '', $matches[1])); // valid IPv4 or IPv6 address
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
99 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
100 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
101 // If not an IP address
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
102 $domain_array = explode('.', $domain_part);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
103 // Not enough parts to be a valid domain
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
104 if (count($domain_array) < 2) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
105 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
106 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
107
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
108 foreach ($domain_array as $part) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
109 if (!preg_match('/^((xn--)?([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]))$/', $part)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
110 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
111 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
112 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
113
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
114 // last domain part
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
115 $last_part = array_pop($domain_array);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
116 if (strpos($last_part, 'xn--') !== 0 && preg_match('/[^a-zA-Z]/', $last_part)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
117 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
118 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
119
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
120 $rcube = rcube::get_instance();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
121
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
122 if (!$dns_check || !$rcube->config->get('email_dns_check')) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
123 return true;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
124 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
125
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
126 // find MX record(s)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
127 if (!function_exists('getmxrr') || getmxrr($domain_part, $mx_records)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
128 return true;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
129 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
130
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
131 // find any DNS record
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
132 if (!function_exists('checkdnsrr') || checkdnsrr($domain_part, 'ANY')) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
133 return true;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
134 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
135 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
136
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
137 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
138 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
139
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
140 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
141 * Validates IPv4 or IPv6 address
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
142 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
143 * @param string $ip IP address in v4 or v6 format
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
144 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
145 * @return bool True if the address is valid
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
146 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
147 public static function check_ip($ip)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
148 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
149 return filter_var($ip, FILTER_VALIDATE_IP) !== false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
150 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
151
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
152 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
153 * Check whether the HTTP referer matches the current request
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
154 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
155 * @return boolean True if referer is the same host+path, false if not
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
156 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
157 public static function check_referer()
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
158 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
159 $uri = parse_url($_SERVER['REQUEST_URI']);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
160 $referer = parse_url(self::request_header('Referer'));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
161
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
162 return $referer['host'] == self::request_header('Host') && $referer['path'] == $uri['path'];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
163 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
164
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
165 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
166 * Replacing specials characters to a specific encoding type
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
167 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
168 * @param string Input string
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
169 * @param string Encoding type: text|html|xml|js|url
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
170 * @param string Replace mode for tags: show|remove|strict
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
171 * @param boolean Convert newlines
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
172 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
173 * @return string The quoted string
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
174 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
175 public static function rep_specialchars_output($str, $enctype = '', $mode = '', $newlines = true)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
176 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
177 static $html_encode_arr = false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
178 static $js_rep_table = false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
179 static $xml_rep_table = false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
180
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
181 if (!is_string($str)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
182 $str = strval($str);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
183 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
184
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
185 // encode for HTML output
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
186 if ($enctype == 'html') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
187 if (!$html_encode_arr) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
188 $html_encode_arr = get_html_translation_table(HTML_SPECIALCHARS);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
189 unset($html_encode_arr['?']);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
190 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
191
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
192 $encode_arr = $html_encode_arr;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
193
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
194 if ($mode == 'remove') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
195 $str = strip_tags($str);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
196 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
197 else if ($mode != 'strict') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
198 // don't replace quotes and html tags
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
199 $ltpos = strpos($str, '<');
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
200 if ($ltpos !== false && strpos($str, '>', $ltpos) !== false) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
201 unset($encode_arr['"']);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
202 unset($encode_arr['<']);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
203 unset($encode_arr['>']);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
204 unset($encode_arr['&']);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
205 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
206 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
207
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
208 $out = strtr($str, $encode_arr);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
209
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
210 return $newlines ? nl2br($out) : $out;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
211 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
212
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
213 // if the replace tables for XML and JS are not yet defined
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
214 if ($js_rep_table === false) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
215 $js_rep_table = $xml_rep_table = array();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
216 $xml_rep_table['&'] = '&amp;';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
217
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
218 // can be increased to support more charsets
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
219 for ($c=160; $c<256; $c++) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
220 $xml_rep_table[chr($c)] = "&#$c;";
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
221 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
222
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
223 $xml_rep_table['"'] = '&quot;';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
224 $js_rep_table['"'] = '\\"';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
225 $js_rep_table["'"] = "\\'";
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
226 $js_rep_table["\\"] = "\\\\";
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
227 // Unicode line and paragraph separators (#1486310)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
228 $js_rep_table[chr(hexdec('E2')).chr(hexdec('80')).chr(hexdec('A8'))] = '&#8232;';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
229 $js_rep_table[chr(hexdec('E2')).chr(hexdec('80')).chr(hexdec('A9'))] = '&#8233;';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
230 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
231
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
232 // encode for javascript use
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
233 if ($enctype == 'js') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
234 return preg_replace(array("/\r?\n/", "/\r/", '/<\\//'), array('\n', '\n', '<\\/'), strtr($str, $js_rep_table));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
235 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
236
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
237 // encode for plaintext
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
238 if ($enctype == 'text') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
239 return str_replace("\r\n", "\n", $mode == 'remove' ? strip_tags($str) : $str);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
240 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
241
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
242 if ($enctype == 'url') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
243 return rawurlencode($str);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
244 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
245
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
246 // encode for XML
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
247 if ($enctype == 'xml') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
248 return strtr($str, $xml_rep_table);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
249 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
250
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
251 // no encoding given -> return original string
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
252 return $str;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
253 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
254
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
255 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
256 * Read input value and convert it for internal use
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
257 * Performs stripslashes() and charset conversion if necessary
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
258 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
259 * @param string Field name to read
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
260 * @param int Source to get value from (see self::INPUT_*)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
261 * @param boolean Allow HTML tags in field value
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
262 * @param string Charset to convert into
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
263 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
264 * @return string Field value or NULL if not available
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
265 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
266 public static function get_input_value($fname, $source, $allow_html = false, $charset = null)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
267 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
268 $value = null;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
269
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
270 if (($source & self::INPUT_GET) && isset($_GET[$fname])) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
271 $value = $_GET[$fname];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
272 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
273
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
274 if (($source & self::INPUT_POST) && isset($_POST[$fname])) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
275 $value = $_POST[$fname];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
276 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
277
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
278 if (($source & self::INPUT_COOKIE) && isset($_COOKIE[$fname])) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
279 $value = $_COOKIE[$fname];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
280 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
281
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
282 return self::parse_input_value($value, $allow_html, $charset);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
283 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
284
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
285 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
286 * Parse/validate input value. See self::get_input_value()
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
287 * Performs stripslashes() and charset conversion if necessary
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
288 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
289 * @param string Input value
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
290 * @param boolean Allow HTML tags in field value
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
291 * @param string Charset to convert into
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
292 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
293 * @return string Parsed value
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
294 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
295 public static function parse_input_value($value, $allow_html = false, $charset = null)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
296 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
297 global $OUTPUT;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
298
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
299 if (empty($value)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
300 return $value;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
301 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
302
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
303 if (is_array($value)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
304 foreach ($value as $idx => $val) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
305 $value[$idx] = self::parse_input_value($val, $allow_html, $charset);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
306 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
307 return $value;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
308 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
309
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
310 // remove HTML tags if not allowed
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
311 if (!$allow_html) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
312 $value = strip_tags($value);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
313 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
314
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
315 $output_charset = is_object($OUTPUT) ? $OUTPUT->get_charset() : null;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
316
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
317 // remove invalid characters (#1488124)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
318 if ($output_charset == 'UTF-8') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
319 $value = rcube_charset::clean($value);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
320 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
321
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
322 // convert to internal charset
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
323 if ($charset && $output_charset) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
324 $value = rcube_charset::convert($value, $output_charset, $charset);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
325 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
326
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
327 return $value;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
328 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
329
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
330 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
331 * Convert array of request parameters (prefixed with _)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
332 * to a regular array with non-prefixed keys.
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
333 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
334 * @param int $mode Source to get value from (GPC)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
335 * @param string $ignore PCRE expression to skip parameters by name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
336 * @param boolean $allow_html Allow HTML tags in field value
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
337 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
338 * @return array Hash array with all request parameters
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
339 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
340 public static function request2param($mode = null, $ignore = 'task|action', $allow_html = false)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
341 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
342 $out = array();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
343 $src = $mode == self::INPUT_GET ? $_GET : ($mode == self::INPUT_POST ? $_POST : $_REQUEST);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
344
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
345 foreach (array_keys($src) as $key) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
346 $fname = $key[0] == '_' ? substr($key, 1) : $key;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
347 if ($ignore && !preg_match('/^(' . $ignore . ')$/', $fname)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
348 $out[$fname] = self::get_input_value($key, $mode, $allow_html);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
349 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
350 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
351
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
352 return $out;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
353 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
354
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
355 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
356 * Convert the given string into a valid HTML identifier
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
357 * Same functionality as done in app.js with rcube_webmail.html_identifier()
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
358 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
359 public static function html_identifier($str, $encode=false)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
360 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
361 if ($encode) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
362 return rtrim(strtr(base64_encode($str), '+/', '-_'), '=');
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
363 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
364 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
365 return asciiwords($str, true, '_');
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
366 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
367 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
368
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
369 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
370 * Replace all css definitions with #container [def]
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
371 * and remove css-inlined scripting, make position style safe
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
372 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
373 * @param string CSS source code
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
374 * @param string Container ID to use as prefix
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
375 * @param bool Allow remote content
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
376 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
377 * @return string Modified CSS source
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
378 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
379 public static function mod_css_styles($source, $container_id, $allow_remote = false)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
380 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
381 $last_pos = 0;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
382 $replacements = new rcube_string_replacer;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
383
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
384 // ignore the whole block if evil styles are detected
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
385 $source = self::xss_entity_decode($source);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
386 $stripped = preg_replace('/[^a-z\(:;]/i', '', $source);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
387 $evilexpr = 'expression|behavior|javascript:|import[^a]' . (!$allow_remote ? '|url\((?!data:image)' : '');
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
388
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
389 if (preg_match("/$evilexpr/i", $stripped)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
390 return '/* evil! */';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
391 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
392
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
393 $strict_url_regexp = '!url\s*\([ "\'](https?:)//[a-z0-9/._+-]+["\' ]\)!Uims';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
394
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
395 // cut out all contents between { and }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
396 while (($pos = strpos($source, '{', $last_pos)) && ($pos2 = strpos($source, '}', $pos))) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
397 $nested = strpos($source, '{', $pos+1);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
398 if ($nested && $nested < $pos2) // when dealing with nested blocks (e.g. @media), take the inner one
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
399 $pos = $nested;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
400 $length = $pos2 - $pos - 1;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
401 $styles = substr($source, $pos+1, $length);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
402
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
403 // Convert position:fixed to position:absolute (#5264)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
404 $styles = preg_replace('/position:[\s\r\n]*fixed/i', 'position: absolute', $styles);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
405
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
406 // check every line of a style block...
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
407 if ($allow_remote) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
408 $a_styles = preg_split('/;[\r\n]*/', $styles, -1, PREG_SPLIT_NO_EMPTY);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
409
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
410 for ($i=0, $len=count($a_styles); $i < $len; $i++) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
411 $line = $a_styles[$i];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
412 $stripped = preg_replace('/[^a-z\(:;]/i', '', $line);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
413
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
414 // allow data:image uri, join with continuation
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
415 if (stripos($stripped, 'url(data:image')) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
416 $a_styles[$i] .= ';' . $a_styles[$i+1];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
417 unset($a_styles[$i+1]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
418 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
419 // allow strict url() values only
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
420 else if (stripos($stripped, 'url(') && !preg_match($strict_url_regexp, $line)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
421 $a_styles = array('/* evil! */');
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
422 break;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
423 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
424 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
425
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
426 $styles = join(";\n", $a_styles);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
427 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
428
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
429 $key = $replacements->add($styles);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
430 $repl = $replacements->get_replacement($key);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
431 $source = substr_replace($source, $repl, $pos+1, $length);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
432 $last_pos = $pos2 - ($length - strlen($repl));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
433 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
434
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
435 // remove html comments and add #container to each tag selector.
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
436 // also replace body definition because we also stripped off the <body> tag
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
437 $source = preg_replace(
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
438 array(
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
439 '/(^\s*<\!--)|(-->\s*$)/m',
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
440 // (?!##str) below is to not match with ##str_replacement_0##
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
441 // from rcube_string_replacer used above, this is needed for
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
442 // cases like @media { body { position: fixed; } } (#5811)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
443 '/(^\s*|,\s*|\}\s*|\{\s*)((?!##str)[a-z0-9\._#\*][a-z0-9\.\-_]*)/im',
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
444 '/'.preg_quote($container_id, '/').'\s+body/i',
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
445 ),
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
446 array(
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
447 '',
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
448 "\\1#$container_id \\2",
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
449 $container_id,
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
450 ),
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
451 $source);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
452
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
453 // put block contents back in
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
454 $source = $replacements->resolve($source);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
455
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
456 return $source;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
457 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
458
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
459 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
460 * Generate CSS classes from mimetype and filename extension
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
461 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
462 * @param string $mimetype Mimetype
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
463 * @param string $filename Filename
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
464 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
465 * @return string CSS classes separated by space
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
466 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
467 public static function file2class($mimetype, $filename)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
468 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
469 $mimetype = strtolower($mimetype);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
470 $filename = strtolower($filename);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
471
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
472 list($primary, $secondary) = explode('/', $mimetype);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
473
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
474 $classes = array($primary ?: 'unknown');
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
475
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
476 if ($secondary) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
477 $classes[] = $secondary;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
478 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
479
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
480 if (preg_match('/\.([a-z0-9]+)$/', $filename, $m)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
481 if (!in_array($m[1], $classes)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
482 $classes[] = $m[1];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
483 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
484 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
485
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
486 return join(" ", $classes);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
487 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
488
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
489 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
490 * Decode escaped entities used by known XSS exploits.
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
491 * See http://downloads.securityfocus.com/vulnerabilities/exploits/26800.eml for examples
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
492 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
493 * @param string CSS content to decode
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
494 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
495 * @return string Decoded string
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
496 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
497 public static function xss_entity_decode($content)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
498 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
499 $callback = function($matches) { return chr(hexdec($matches[1])); };
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
500
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
501 $out = html_entity_decode(html_entity_decode($content));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
502 $out = trim(preg_replace('/(^<!--|-->$)/', '', trim($out)));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
503 $out = preg_replace_callback('/\\\([0-9a-f]{4})/i', $callback, $out);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
504 $out = preg_replace('#/\*.*\*/#Ums', '', $out);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
505 $out = strip_tags($out);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
506
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
507 return $out;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
508 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
509
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
510 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
511 * Check if we can process not exceeding memory_limit
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
512 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
513 * @param integer Required amount of memory
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
514 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
515 * @return boolean True if memory won't be exceeded, False otherwise
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
516 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
517 public static function mem_check($need)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
518 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
519 $mem_limit = parse_bytes(ini_get('memory_limit'));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
520 $memory = function_exists('memory_get_usage') ? memory_get_usage() : 16*1024*1024; // safe value: 16MB
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
521
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
522 return $mem_limit > 0 && $memory + $need > $mem_limit ? false : true;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
523 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
524
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
525 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
526 * Check if working in SSL mode
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
527 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
528 * @param integer $port HTTPS port number
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
529 * @param boolean $use_https Enables 'use_https' option checking
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
530 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
531 * @return boolean
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
532 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
533 public static function https_check($port=null, $use_https=true)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
534 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
535 if (!empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) != 'off') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
536 return true;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
537 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
538 if (!empty($_SERVER['HTTP_X_FORWARDED_PROTO'])
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
539 && strtolower($_SERVER['HTTP_X_FORWARDED_PROTO']) == 'https'
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
540 && in_array($_SERVER['REMOTE_ADDR'], rcube::get_instance()->config->get('proxy_whitelist', array()))
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
541 ) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
542 return true;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
543 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
544 if ($port && $_SERVER['SERVER_PORT'] == $port) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
545 return true;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
546 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
547 if ($use_https && rcube::get_instance()->config->get('use_https')) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
548 return true;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
549 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
550
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
551 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
552 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
553
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
554 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
555 * Replaces hostname variables.
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
556 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
557 * @param string $name Hostname
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
558 * @param string $host Optional IMAP hostname
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
559 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
560 * @return string Hostname
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
561 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
562 public static function parse_host($name, $host = '')
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
563 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
564 if (!is_string($name)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
565 return $name;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
566 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
567
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
568 // %n - host
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
569 $n = preg_replace('/:\d+$/', '', $_SERVER['SERVER_NAME']);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
570 // %t - host name without first part, e.g. %n=mail.domain.tld, %t=domain.tld
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
571 $t = preg_replace('/^[^\.]+\./', '', $n);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
572 // %d - domain name without first part
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
573 $d = preg_replace('/^[^\.]+\./', '', $_SERVER['HTTP_HOST']);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
574 // %h - IMAP host
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
575 $h = $_SESSION['storage_host'] ?: $host;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
576 // %z - IMAP domain without first part, e.g. %h=imap.domain.tld, %z=domain.tld
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
577 $z = preg_replace('/^[^\.]+\./', '', $h);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
578 // %s - domain name after the '@' from e-mail address provided at login screen.
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
579 // Returns FALSE if an invalid email is provided
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
580 if (strpos($name, '%s') !== false) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
581 $user_email = self::get_input_value('_user', self::INPUT_POST);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
582 $user_email = self::idn_convert($user_email, true);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
583 $matches = preg_match('/(.*)@([a-z0-9\.\-\[\]\:]+)/i', $user_email, $s);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
584 if ($matches < 1 || filter_var($s[1]."@".$s[2], FILTER_VALIDATE_EMAIL) === false) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
585 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
586 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
587 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
588
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
589 return str_replace(array('%n', '%t', '%d', '%h', '%z', '%s'), array($n, $t, $d, $h, $z, $s[2]), $name);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
590 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
591
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
592 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
593 * Returns remote IP address and forwarded addresses if found
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
594 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
595 * @return string Remote IP address(es)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
596 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
597 public static function remote_ip()
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
598 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
599 $address = $_SERVER['REMOTE_ADDR'];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
600
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
601 // append the NGINX X-Real-IP header, if set
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
602 if (!empty($_SERVER['HTTP_X_REAL_IP'])) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
603 $remote_ip[] = 'X-Real-IP: ' . $_SERVER['HTTP_X_REAL_IP'];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
604 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
605
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
606 // append the X-Forwarded-For header, if set
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
607 if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
608 $remote_ip[] = 'X-Forwarded-For: ' . $_SERVER['HTTP_X_FORWARDED_FOR'];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
609 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
610
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
611 if (!empty($remote_ip)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
612 $address .= '(' . implode(',', $remote_ip) . ')';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
613 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
614
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
615 return $address;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
616 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
617
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
618 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
619 * Returns the real remote IP address
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
620 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
621 * @return string Remote IP address
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
622 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
623 public static function remote_addr()
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
624 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
625 // Check if any of the headers are set first to improve performance
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
626 if (!empty($_SERVER['HTTP_X_FORWARDED_FOR']) || !empty($_SERVER['HTTP_X_REAL_IP'])) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
627 $proxy_whitelist = rcube::get_instance()->config->get('proxy_whitelist', array());
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
628 if (in_array($_SERVER['REMOTE_ADDR'], $proxy_whitelist)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
629 if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
630 foreach (array_reverse(explode(',', $_SERVER['HTTP_X_FORWARDED_FOR'])) as $forwarded_ip) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
631 if (!in_array($forwarded_ip, $proxy_whitelist)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
632 return $forwarded_ip;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
633 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
634 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
635 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
636
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
637 if (!empty($_SERVER['HTTP_X_REAL_IP'])) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
638 return $_SERVER['HTTP_X_REAL_IP'];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
639 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
640 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
641 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
642
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
643 if (!empty($_SERVER['REMOTE_ADDR'])) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
644 return $_SERVER['REMOTE_ADDR'];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
645 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
646
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
647 return '';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
648 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
649
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
650 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
651 * Read a specific HTTP request header.
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
652 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
653 * @param string $name Header name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
654 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
655 * @return mixed Header value or null if not available
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
656 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
657 public static function request_header($name)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
658 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
659 if (function_exists('getallheaders')) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
660 $hdrs = array_change_key_case(getallheaders(), CASE_UPPER);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
661 $key = strtoupper($name);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
662 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
663 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
664 $key = 'HTTP_' . strtoupper(strtr($name, '-', '_'));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
665 $hdrs = array_change_key_case($_SERVER, CASE_UPPER);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
666 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
667
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
668 return $hdrs[$key];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
669 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
670
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
671 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
672 * Explode quoted string
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
673 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
674 * @param string Delimiter expression string for preg_match()
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
675 * @param string Input string
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
676 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
677 * @return array String items
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
678 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
679 public static function explode_quoted_string($delimiter, $string)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
680 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
681 $result = array();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
682 $strlen = strlen($string);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
683
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
684 for ($q=$p=$i=0; $i < $strlen; $i++) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
685 if ($string[$i] == "\"" && $string[$i-1] != "\\") {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
686 $q = $q ? false : true;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
687 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
688 else if (!$q && preg_match("/$delimiter/", $string[$i])) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
689 $result[] = substr($string, $p, $i - $p);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
690 $p = $i + 1;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
691 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
692 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
693
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
694 $result[] = (string) substr($string, $p);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
695
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
696 return $result;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
697 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
698
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
699 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
700 * Improved equivalent to strtotime()
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
701 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
702 * @param string $date Date string
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
703 * @param DateTimeZone $timezone Timezone to use for DateTime object
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
704 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
705 * @return int Unix timestamp
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
706 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
707 public static function strtotime($date, $timezone = null)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
708 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
709 $date = self::clean_datestr($date);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
710 $tzname = $timezone ? ' ' . $timezone->getName() : '';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
711
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
712 // unix timestamp
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
713 if (is_numeric($date)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
714 return (int) $date;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
715 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
716
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
717 // if date parsing fails, we have a date in non-rfc format.
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
718 // remove token from the end and try again
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
719 while ((($ts = @strtotime($date . $tzname)) === false) || ($ts < 0)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
720 $d = explode(' ', $date);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
721 array_pop($d);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
722 if (!$d) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
723 break;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
724 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
725 $date = implode(' ', $d);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
726 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
727
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
728 return (int) $ts;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
729 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
730
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
731 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
732 * Date parsing function that turns the given value into a DateTime object
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
733 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
734 * @param string $date Date string
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
735 * @param DateTimeZone $timezone Timezone to use for DateTime object
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
736 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
737 * @return DateTime instance or false on failure
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
738 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
739 public static function anytodatetime($date, $timezone = null)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
740 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
741 if ($date instanceof DateTime) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
742 return $date;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
743 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
744
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
745 $dt = false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
746 $date = self::clean_datestr($date);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
747
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
748 // try to parse string with DateTime first
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
749 if (!empty($date)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
750 try {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
751 $dt = $timezone ? new DateTime($date, $timezone) : new DateTime($date);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
752 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
753 catch (Exception $e) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
754 // ignore
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
755 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
756 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
757
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
758 // try our advanced strtotime() method
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
759 if (!$dt && ($timestamp = self::strtotime($date, $timezone))) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
760 try {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
761 $dt = new DateTime("@".$timestamp);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
762 if ($timezone) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
763 $dt->setTimezone($timezone);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
764 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
765 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
766 catch (Exception $e) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
767 // ignore
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
768 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
769 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
770
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
771 return $dt;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
772 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
773
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
774 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
775 * Clean up date string for strtotime() input
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
776 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
777 * @param string $date Date string
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
778 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
779 * @return string Date string
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
780 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
781 public static function clean_datestr($date)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
782 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
783 $date = trim($date);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
784
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
785 // check for MS Outlook vCard date format YYYYMMDD
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
786 if (preg_match('/^([12][90]\d\d)([01]\d)([0123]\d)$/', $date, $m)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
787 return sprintf('%04d-%02d-%02d 00:00:00', intval($m[1]), intval($m[2]), intval($m[3]));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
788 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
789
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
790 // Clean malformed data
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
791 $date = preg_replace(
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
792 array(
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
793 '/GMT\s*([+-][0-9]+)/', // support non-standard "GMTXXXX" literal
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
794 '/[^a-z0-9\x20\x09:+-\/]/i', // remove any invalid characters
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
795 '/\s*(Mon|Tue|Wed|Thu|Fri|Sat|Sun)\s*/i', // remove weekday names
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
796 ),
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
797 array(
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
798 '\\1',
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
799 '',
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
800 '',
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
801 ), $date);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
802
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
803 $date = trim($date);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
804
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
805 // try to fix dd/mm vs. mm/dd discrepancy, we can't do more here
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
806 if (preg_match('/^(\d{1,2})[.\/-](\d{1,2})[.\/-](\d{4})(\s.*)?$/', $date, $m)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
807 $mdy = $m[2] > 12 && $m[1] <= 12;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
808 $day = $mdy ? $m[2] : $m[1];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
809 $month = $mdy ? $m[1] : $m[2];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
810 $date = sprintf('%04d-%02d-%02d%s', $m[3], $month, $day, $m[4] ?: ' 00:00:00');
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
811 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
812 // I've found that YYYY.MM.DD is recognized wrong, so here's a fix
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
813 else if (preg_match('/^(\d{4})\.(\d{1,2})\.(\d{1,2})(\s.*)?$/', $date, $m)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
814 $date = sprintf('%04d-%02d-%02d%s', $m[1], $m[2], $m[3], $m[4] ?: ' 00:00:00');
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
815 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
816
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
817 return $date;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
818 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
819
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
820 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
821 * Turns the given date-only string in defined format into YYYY-MM-DD format.
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
822 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
823 * Supported formats: 'Y/m/d', 'Y.m.d', 'd-m-Y', 'd/m/Y', 'd.m.Y', 'j.n.Y'
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
824 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
825 * @param string $date Date string
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
826 * @param string $format Input date format
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
827 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
828 * @return strin Date string in YYYY-MM-DD format, or the original string
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
829 * if format is not supported
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
830 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
831 public static function format_datestr($date, $format)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
832 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
833 $format_items = preg_split('/[.-\/\\\\]/', $format);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
834 $date_items = preg_split('/[.-\/\\\\]/', $date);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
835 $iso_format = '%04d-%02d-%02d';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
836
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
837 if (count($format_items) == 3 && count($date_items) == 3) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
838 if ($format_items[0] == 'Y') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
839 $date = sprintf($iso_format, $date_items[0], $date_items[1], $date_items[2]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
840 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
841 else if (strpos('dj', $format_items[0]) !== false) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
842 $date = sprintf($iso_format, $date_items[2], $date_items[1], $date_items[0]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
843 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
844 else if (strpos('mn', $format_items[0]) !== false) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
845 $date = sprintf($iso_format, $date_items[2], $date_items[0], $date_items[1]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
846 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
847 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
848
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
849 return $date;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
850 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
851
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
852 /*
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
853 * Idn_to_ascii wrapper.
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
854 * Intl/Idn modules version of this function doesn't work with e-mail address
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
855 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
856 public static function idn_to_ascii($str)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
857 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
858 return self::idn_convert($str, true);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
859 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
860
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
861 /*
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
862 * Idn_to_ascii wrapper.
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
863 * Intl/Idn modules version of this function doesn't work with e-mail address
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
864 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
865 public static function idn_to_utf8($str)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
866 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
867 return self::idn_convert($str, false);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
868 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
869
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
870 public static function idn_convert($input, $is_utf = false)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
871 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
872 if ($at = strpos($input, '@')) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
873 $user = substr($input, 0, $at);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
874 $domain = substr($input, $at+1);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
875 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
876 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
877 $domain = $input;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
878 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
879
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
880 $domain = $is_utf ? idn_to_ascii($domain) : idn_to_utf8($domain);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
881
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
882 if ($domain === false) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
883 return '';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
884 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
885
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
886 return $at ? $user . '@' . $domain : $domain;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
887 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
888
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
889 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
890 * Split the given string into word tokens
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
891 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
892 * @param string Input to tokenize
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
893 * @param integer Minimum length of a single token
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
894 * @return array List of tokens
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
895 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
896 public static function tokenize_string($str, $minlen = 2)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
897 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
898 $expr = array('/[\s;,"\'\/+-]+/ui', '/(\d)[-.\s]+(\d)/u');
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
899 $repl = array(' ', '\\1\\2');
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
900
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
901 if ($minlen > 1) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
902 $minlen--;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
903 $expr[] = "/(^|\s+)\w{1,$minlen}(\s+|$)/u";
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
904 $repl[] = ' ';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
905 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
906
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
907 return array_filter(explode(" ", preg_replace($expr, $repl, $str)));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
908 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
909
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
910 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
911 * Normalize the given string for fulltext search.
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
912 * Currently only optimized for ISO-8859-1 and ISO-8859-2 characters; to be extended
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
913 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
914 * @param string Input string (UTF-8)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
915 * @param boolean True to return list of words as array
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
916 * @param integer Minimum length of tokens
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
917 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
918 * @return mixed Normalized string or a list of normalized tokens
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
919 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
920 public static function normalize_string($str, $as_array = false, $minlen = 2)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
921 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
922 // replace 4-byte unicode characters with '?' character,
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
923 // these are not supported in default utf-8 charset on mysql,
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
924 // the chance we'd need them in searching is very low
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
925 $str = preg_replace('/('
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
926 . '\xF0[\x90-\xBF][\x80-\xBF]{2}'
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
927 . '|[\xF1-\xF3][\x80-\xBF]{3}'
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
928 . '|\xF4[\x80-\x8F][\x80-\xBF]{2}'
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
929 . ')/', '?', $str);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
930
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
931 // split by words
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
932 $arr = self::tokenize_string($str, $minlen);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
933
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
934 // detect character set
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
935 if (utf8_encode(utf8_decode($str)) == $str) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
936 // ISO-8859-1 (or ASCII)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
937 preg_match_all('/./u', 'äâàåáãæçéêëèïîìíñöôòøõóüûùúýÿ', $keys);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
938 preg_match_all('/./', 'aaaaaaaceeeeiiiinoooooouuuuyy', $values);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
939
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
940 $mapping = array_combine($keys[0], $values[0]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
941 $mapping = array_merge($mapping, array('ß' => 'ss', 'ae' => 'a', 'oe' => 'o', 'ue' => 'u'));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
942 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
943 else if (rcube_charset::convert(rcube_charset::convert($str, 'UTF-8', 'ISO-8859-2'), 'ISO-8859-2', 'UTF-8') == $str) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
944 // ISO-8859-2
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
945 preg_match_all('/./u', 'ąáâäćçčéęëěíîłľĺńňóôöŕřśšşťţůúűüźžżý', $keys);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
946 preg_match_all('/./', 'aaaaccceeeeiilllnnooorrsssttuuuuzzzy', $values);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
947
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
948 $mapping = array_combine($keys[0], $values[0]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
949 $mapping = array_merge($mapping, array('ß' => 'ss', 'ae' => 'a', 'oe' => 'o', 'ue' => 'u'));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
950 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
951
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
952 foreach ($arr as $i => $part) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
953 $part = mb_strtolower($part);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
954
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
955 if (!empty($mapping)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
956 $part = strtr($part, $mapping);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
957 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
958
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
959 $arr[$i] = $part;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
960 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
961
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
962 return $as_array ? $arr : join(" ", $arr);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
963 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
964
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
965 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
966 * Compare two strings for matching words (order not relevant)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
967 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
968 * @param string Haystack
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
969 * @param string Needle
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
970 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
971 * @return boolean True if match, False otherwise
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
972 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
973 public static function words_match($haystack, $needle)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
974 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
975 $a_needle = self::tokenize_string($needle, 1);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
976 $_haystack = join(" ", self::tokenize_string($haystack, 1));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
977 $valid = strlen($_haystack) > 0;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
978 $hits = 0;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
979
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
980 foreach ($a_needle as $w) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
981 if ($valid) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
982 if (stripos($_haystack, $w) !== false) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
983 $hits++;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
984 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
985 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
986 else if (stripos($haystack, $w) !== false) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
987 $hits++;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
988 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
989 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
990
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
991 return $hits >= count($a_needle);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
992 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
993
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
994 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
995 * Parse commandline arguments into a hash array
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
996 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
997 * @param array $aliases Argument alias names
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
998 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
999 * @return array Argument values hash
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1000 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1001 public static function get_opt($aliases = array())
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1002 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1003 $args = array();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1004 $bool = array();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1005
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1006 // find boolean (no value) options
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1007 foreach ($aliases as $key => $alias) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1008 if ($pos = strpos($alias, ':')) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1009 $aliases[$key] = substr($alias, 0, $pos);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1010 $bool[] = $key;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1011 $bool[] = $aliases[$key];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1012 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1013 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1014
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1015 for ($i=1; $i < count($_SERVER['argv']); $i++) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1016 $arg = $_SERVER['argv'][$i];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1017 $value = true;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1018 $key = null;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1019
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1020 if ($arg[0] == '-') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1021 $key = preg_replace('/^-+/', '', $arg);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1022 $sp = strpos($arg, '=');
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1023
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1024 if ($sp > 0) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1025 $key = substr($key, 0, $sp - 2);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1026 $value = substr($arg, $sp+1);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1027 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1028 else if (in_array($key, $bool)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1029 $value = true;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1030 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1031 else if (strlen($_SERVER['argv'][$i+1]) && $_SERVER['argv'][$i+1][0] != '-') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1032 $value = $_SERVER['argv'][++$i];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1033 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1034
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1035 $args[$key] = is_string($value) ? preg_replace(array('/^["\']/', '/["\']$/'), '', $value) : $value;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1036 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1037 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1038 $args[] = $arg;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1039 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1040
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1041 if ($alias = $aliases[$key]) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1042 $args[$alias] = $args[$key];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1043 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1044 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1045
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1046 return $args;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1047 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1048
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1049 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1050 * Safe password prompt for command line
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1051 * from http://blogs.sitepoint.com/2009/05/01/interactive-cli-password-prompt-in-php/
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1052 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1053 * @return string Password
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1054 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1055 public static function prompt_silent($prompt = "Password:")
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1056 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1057 if (preg_match('/^win/i', PHP_OS)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1058 $vbscript = sys_get_temp_dir() . 'prompt_password.vbs';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1059 $vbcontent = 'wscript.echo(InputBox("' . addslashes($prompt) . '", "", "password here"))';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1060 file_put_contents($vbscript, $vbcontent);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1061
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1062 $command = "cscript //nologo " . escapeshellarg($vbscript);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1063 $password = rtrim(shell_exec($command));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1064 unlink($vbscript);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1065
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1066 return $password;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1067 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1068 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1069 $command = "/usr/bin/env bash -c 'echo OK'";
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1070 if (rtrim(shell_exec($command)) !== 'OK') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1071 echo $prompt;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1072 $pass = trim(fgets(STDIN));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1073 echo chr(8)."\r" . $prompt . str_repeat("*", strlen($pass))."\n";
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1074 return $pass;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1075 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1076
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1077 $command = "/usr/bin/env bash -c 'read -s -p \"" . addslashes($prompt) . "\" mypassword && echo \$mypassword'";
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1078 $password = rtrim(shell_exec($command));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1079 echo "\n";
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1080 return $password;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1081 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1082 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1083
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1084 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1085 * Find out if the string content means true or false
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1086 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1087 * @param string $str Input value
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1088 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1089 * @return boolean Boolean value
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1090 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1091 public static function get_boolean($str)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1092 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1093 $str = strtolower($str);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1094
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1095 return !in_array($str, array('false', '0', 'no', 'off', 'nein', ''), true);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1096 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1097
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1098 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1099 * OS-dependent absolute path detection
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1100 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1101 public static function is_absolute_path($path)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1102 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1103 if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1104 return (bool) preg_match('!^[a-z]:[\\\\/]!i', $path);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1105 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1106 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1107 return $path[0] == '/';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1108 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1109 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1110
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1111 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1112 * Resolve relative URL
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1113 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1114 * @param string $url Relative URL
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1115 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1116 * @return string Absolute URL
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1117 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1118 public static function resolve_url($url)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1119 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1120 // prepend protocol://hostname:port
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1121 if (!preg_match('|^https?://|', $url)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1122 $schema = 'http';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1123 $default_port = 80;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1124
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1125 if (self::https_check()) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1126 $schema = 'https';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1127 $default_port = 443;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1128 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1129
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1130 $prefix = $schema . '://' . preg_replace('/:\d+$/', '', $_SERVER['HTTP_HOST']);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1131 if ($_SERVER['SERVER_PORT'] != $default_port) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1132 $prefix .= ':' . $_SERVER['SERVER_PORT'];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1133 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1134
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1135 $url = $prefix . ($url[0] == '/' ? '' : '/') . $url;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1136 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1137
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1138 return $url;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1139 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1140
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1141 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1142 * Generate a random string
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1143 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1144 * @param int $length String length
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1145 * @param bool $raw Return RAW data instead of ascii
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1146 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1147 * @return string The generated random string
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1148 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1149 public static function random_bytes($length, $raw = false)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1150 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1151 $hextab = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1152 $tabsize = strlen($hextab);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1153
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1154 // Use PHP7 true random generator
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1155 if ($raw && function_exists('random_bytes')) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1156 return random_bytes($length);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1157 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1158
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1159 if (!$raw && function_exists('random_int')) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1160 $result = '';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1161 while ($length-- > 0) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1162 $result .= $hextab[random_int(0, $tabsize - 1)];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1163 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1164
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1165 return $result;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1166 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1167
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1168 $random = openssl_random_pseudo_bytes($length);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1169
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1170 if ($random === false && $length > 0) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1171 throw new Exception("Failed to get random bytes");
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1172 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1173
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1174 if (!$raw) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1175 for ($x = 0; $x < $length; $x++) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1176 $random[$x] = $hextab[ord($random[$x]) % $tabsize];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1177 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1178 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1179
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1180 return $random;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1181 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1182
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1183 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1184 * Convert binary data into readable form (containing a-zA-Z0-9 characters)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1185 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1186 * @param string $input Binary input
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1187 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1188 * @return string Readable output (Base62)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1189 * @deprecated since 1.3.1
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1190 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1191 public static function bin2ascii($input)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1192 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1193 $hextab = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1194 $result = '';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1195
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1196 for ($x = 0; $x < strlen($input); $x++) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1197 $result .= $hextab[ord($input[$x]) % 62];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1198 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1199
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1200 return $result;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1201 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1202
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1203 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1204 * Format current date according to specified format.
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1205 * This method supports microseconds (u).
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1206 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1207 * @param string $format Date format (default: 'd-M-Y H:i:s O')
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1208 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1209 * @return string Formatted date
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1210 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1211 public static function date_format($format = null)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1212 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1213 if (empty($format)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1214 $format = 'd-M-Y H:i:s O';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1215 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1216
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1217 if (strpos($format, 'u') !== false) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1218 $dt = number_format(microtime(true), 6, '.', '');
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1219 $dt .= '.' . date_default_timezone_get();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1220
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1221 if ($date = date_create_from_format('U.u.e', $dt)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1222 return $date->format($format);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1223 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1224 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1225
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1226 return date($format);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1227 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1228
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1229 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1230 * Parses socket options and returns options for specified hostname.
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1231 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1232 * @param array &$options Configured socket options
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1233 * @param string $host Hostname
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1234 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1235 public static function parse_socket_options(&$options, $host = null)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1236 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1237 if (empty($host) || empty($options)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1238 return $options;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1239 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1240
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1241 // get rid of schema and port from the hostname
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1242 $host_url = parse_url($host);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1243 if (isset($host_url['host'])) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1244 $host = $host_url['host'];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1245 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1246
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1247 // find per-host options
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1248 if (array_key_exists($host, $options)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1249 $options = $options[$host];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1250 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1251 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1252
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1253 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1254 * Get maximum upload size
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1255 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1256 * @return int Maximum size in bytes
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1257 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1258 public static function max_upload_size()
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1259 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1260 // find max filesize value
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1261 $max_filesize = parse_bytes(ini_get('upload_max_filesize'));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1262 $max_postsize = parse_bytes(ini_get('post_max_size'));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1263
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1264 if ($max_postsize && $max_postsize < $max_filesize) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1265 $max_filesize = $max_postsize;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1266 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1267
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1268 return $max_filesize;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1269 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1270 }