]> CyberLeo.Net >> Repos - Github/YOURLS.git/blob - includes/functions-formatting.php
Better URL sanitization. Fixes Issue 1102.
[Github/YOURLS.git] / includes / functions-formatting.php
1 <?php\r
2 /*\r
3  * YOURLS\r
4  * Function library for anything related to formatting / validating / sanitizing\r
5  */\r
6 \r
7 // function to convert an integer (1337) to a string (3jk).\r
8 function yourls_int2string( $num, $chars = null ) {\r
9         if( $chars == null )\r
10                 $chars = yourls_get_shorturl_charset();\r
11         $string = '';\r
12         $len = strlen( $chars );\r
13         while( $num >= $len ) {\r
14                 $mod = bcmod( $num, $len );\r
15                 $num = bcdiv( $num, $len );\r
16                 $string = $chars[$mod] . $string;\r
17         }\r
18         $string = $chars[$num] . $string;\r
19         \r
20         return yourls_apply_filter( 'int2string', $string, $num, $chars );\r
21 }\r
22 \r
23 // function to convert a string (3jk) to an integer (1337)\r
24 function yourls_string2int( $string, $chars = null ) {\r
25         if( $chars == null )\r
26                 $chars = yourls_get_shorturl_charset();\r
27         $integer = 0;\r
28         $string = strrev( $string  );\r
29         $baselen = strlen( $chars );\r
30         $inputlen = strlen( $string );\r
31         for ($i = 0; $i < $inputlen; $i++) {\r
32                 $index = strpos( $chars, $string[$i] );\r
33                 $integer = bcadd( $integer, bcmul( $index, bcpow( $baselen, $i ) ) );\r
34         }\r
35 \r
36         return yourls_apply_filter( 'string2int', $integer, $string, $chars );\r
37 }\r
38 \r
39 // return a unique(ish) hash for a string to be used as a valid HTML id\r
40 function yourls_string2htmlid( $string ) {\r
41         return yourls_apply_filter( 'string2htmlid', 'y'.abs( crc32( $string ) ) );\r
42 }\r
43 \r
44 // Make sure a link keyword (ie "1fv" as in "site.com/1fv") is valid.\r
45 function yourls_sanitize_string( $string ) {\r
46         // make a regexp pattern with the shorturl charset, and remove everything but this\r
47         $pattern = yourls_make_regexp_pattern( yourls_get_shorturl_charset() );\r
48         $valid = substr(preg_replace('![^'.$pattern.']!', '', $string ), 0, 199);\r
49         \r
50         return yourls_apply_filter( 'sanitize_string', $valid, $string );\r
51 }\r
52 \r
53 // Alias function. I was always getting it wrong.\r
54 function yourls_sanitize_keyword( $keyword ) {\r
55         return yourls_sanitize_string( $keyword );\r
56 }\r
57 \r
58 // Sanitize a page title. No HTML per W3C http://www.w3.org/TR/html401/struct/global.html#h-7.4.2\r
59 function yourls_sanitize_title( $title ) {\r
60         // TODO: make stronger Implement KSES?\r
61         $title = strip_tags( $title );\r
62         // Remove extra white space\r
63         $title = preg_replace( "/\s+/", ' ', trim( $title ) );\r
64         return $title;\r
65 }\r
66 \r
67 // A few sanity checks on the URL\r
68 function yourls_sanitize_url( $url, $force_protocol = true, $force_lowercase = true ) {\r
69         // make sure there's only one 'http://' at the beginning (prevents pasting a URL right after the default 'http://')\r
70         $url = str_replace( \r
71                 array( 'http://http://', 'http://https://' ),\r
72                 array( 'http://',        'https://'        ),\r
73                 $url\r
74         );\r
75 \r
76         if( $force_protocol ) {\r
77                 // make sure there's a protocol, add http:// if not\r
78                 if ( !preg_match('!^([a-zA-Z]+://)!', $url ) )\r
79                         $url = 'http://'.$url;\r
80         }\r
81         \r
82         if( $force_lowercase ) {\r
83                 // force scheme and domain to lowercase - see issue 591\r
84                 preg_match( '!^([a-zA-Z]+://([^/]+))(.*)$!', $url, $matches );\r
85                 if( isset( $matches[1] ) && isset( $matches[3] ) )\r
86                         $url = strtolower( $matches[1] ) . $matches[3];\r
87         }\r
88         \r
89         // clean and shave\r
90         $url = yourls_clean_url( $url );\r
91         return substr( $url, 0, 1999 );\r
92 }\r
93 \r
94 // Function to filter all invalid characters from a URL. Stolen from WP's clean_url()\r
95 function yourls_clean_url( $url ) {\r
96         $url = preg_replace( '|[^a-z0-9-~+_.?\[\]\^#=!&;,/:%@$\|*`\'<>"()\\x80-\\xff\{\}]|i', '', $url );\r
97         $strip = array( '%0d', '%0a', '%0D', '%0A' );\r
98         $url = yourls_deep_replace( $strip, $url );\r
99         $url = str_replace( ';//', '://', $url );\r
100         $url = str_replace( '&amp;', '&', $url ); // Revert & not to break query strings\r
101         \r
102         return $url;\r
103 }\r
104 \r
105 // Perform a replacement while a string is found, eg $subject = '%0%0%0DDD', $search ='%0D' -> $result =''\r
106 // Stolen from WP's _deep_replace\r
107 function yourls_deep_replace($search, $subject){\r
108         $found = true;\r
109         while($found) {\r
110                 $found = false;\r
111                 foreach( (array) $search as $val ) {\r
112                         while(strpos($subject, $val) !== false) {\r
113                                 $found = true;\r
114                                 $subject = str_replace($val, '', $subject);\r
115                         }\r
116                 }\r
117         }\r
118         \r
119         return $subject;\r
120 }\r
121 \r
122 // Make sure an integer is a valid integer (PHP's intval() limits to too small numbers)\r
123 // TODO FIXME FFS: unused ?\r
124 function yourls_sanitize_int($in) {\r
125         return ( substr(preg_replace('/[^0-9]/', '', strval($in) ), 0, 20) );\r
126 }\r
127 \r
128 // Make sure a integer is safe\r
129 // Note: this is not checking for integers, since integers on 32bits system are way too limited\r
130 // TODO: find a way to validate as integer\r
131 function yourls_intval($in) {\r
132         return yourls_escape($in);\r
133 }\r
134 \r
135 // Escape a string\r
136 function yourls_escape( $in ) {\r
137         return mysql_real_escape_string($in);\r
138 }\r
139 \r
140 // Sanitize an IP address\r
141 function yourls_sanitize_ip( $ip ) {\r
142         return preg_replace( '/[^0-9a-fA-F:., ]/', '', $ip );\r
143 }\r
144 \r
145 // Make sure a date is m(m)/d(d)/yyyy, return false otherwise\r
146 function yourls_sanitize_date( $date ) {\r
147         if( !preg_match( '!^\d{1,2}/\d{1,2}/\d{4}$!' , $date ) ) {\r
148                 return false;\r
149         }\r
150         return $date;\r
151 }\r
152 \r
153 // Sanitize a date for SQL search. Return false if malformed input.\r
154 function yourls_sanitize_date_for_sql( $date ) {\r
155         if( !yourls_sanitize_date( $date ) )\r
156                 return false;\r
157         return date('Y-m-d', strtotime( $date ) );\r
158 }\r
159 \r
160 // Return word or words if more than one\r
161 function yourls_plural( $word, $count=1 ) {\r
162         return $word . ($count > 1 ? 's' : '');\r
163 }\r
164 \r
165 // Return trimmed string\r
166 function yourls_trim_long_string( $string, $length = 60, $append = '[...]' ) {\r
167         $newstring = $string;\r
168         if( function_exists('mb_substr') ) {\r
169                 if ( mb_strlen( $newstring ) > $length ) {\r
170                         $newstring = mb_substr( $newstring, 0, $length - mb_strlen( $append ), 'UTF-8' ) . $append;     \r
171                 }\r
172         } else {\r
173                 if ( strlen( $newstring ) > $length ) {\r
174                         $newstring = substr( $newstring, 0, $length - strlen( $append ) ) . $append;    \r
175                 }\r
176         }\r
177         return yourls_apply_filter( 'trim_long_string', $newstring, $string, $length, $append );\r
178 }\r
179 \r
180 // Sanitize a version number (1.4.1-whatever -> 1.4.1)\r
181 function yourls_sanitize_version( $ver ) {\r
182         return preg_replace( '/[^0-9.]/', '', $ver );\r
183 }\r
184 \r
185 // Sanitize a filename (no Win32 stuff)\r
186 function yourls_sanitize_filename( $file ) {\r
187         $file = str_replace( '\\', '/', $file ); // sanitize for Win32 installs\r
188         $file = preg_replace( '|/+|' ,'/', $file ); // remove any duplicate slash\r
189         return $file;\r
190 }\r
191 \r
192 // Check if a string seems to be UTF-8. Stolen from WP.\r
193 function yourls_seems_utf8($str) {\r
194         $length = strlen($str);\r
195         for ($i=0; $i < $length; $i++) {\r
196                 $c = ord($str[$i]);\r
197                 if ($c < 0x80) $n = 0; # 0bbbbbbb\r
198                 elseif (($c & 0xE0) == 0xC0) $n=1; # 110bbbbb\r
199                 elseif (($c & 0xF0) == 0xE0) $n=2; # 1110bbbb\r
200                 elseif (($c & 0xF8) == 0xF0) $n=3; # 11110bbb\r
201                 elseif (($c & 0xFC) == 0xF8) $n=4; # 111110bb\r
202                 elseif (($c & 0xFE) == 0xFC) $n=5; # 1111110b\r
203                 else return false; # Does not match any model\r
204                 for ($j=0; $j<$n; $j++) { # n bytes matching 10bbbbbb follow ?\r
205                         if ((++$i == $length) || ((ord($str[$i]) & 0xC0) != 0x80))\r
206                                 return false;\r
207                 }\r
208         }\r
209         return true;\r
210 }\r
211 \r