includes/functions.php
changeset 38 ed06961e54dd
parent 32 4d87aad3c4c0
child 40 723bb7acf914
equal deleted inserted replaced
37:7267c2a67a93 38:ed06961e54dd
  2195  * @return string
  2195  * @return string
  2196  */
  2196  */
  2197 
  2197 
  2198 function dirtify_page_id($page_id)
  2198 function dirtify_page_id($page_id)
  2199 {
  2199 {
       
  2200   global $db, $session, $paths, $template, $plugins; // Common objects
  2200   // First, replace spaces with underscores  
  2201   // First, replace spaces with underscores  
  2201   $page_id = str_replace(' ', '_', $page_id);
  2202   $page_id = str_replace(' ', '_', $page_id);
       
  2203   
       
  2204   // Exception for userpages for IP addresses
       
  2205   if ( preg_match('/^' . preg_quote($paths->nslist['User']) . '/', $page_id) )
       
  2206   {
       
  2207     $ip = preg_replace('/^' . preg_quote($paths->nslist['User']) . '/', '', $page_id);
       
  2208     if ( is_valid_ip($ip) )
       
  2209       return $page_id;
       
  2210   }
  2202   
  2211   
  2203   preg_match_all('/\.[A-Fa-f0-9][A-Fa-f0-9]/', $page_id, $matches);
  2212   preg_match_all('/\.[A-Fa-f0-9][A-Fa-f0-9]/', $page_id, $matches);
  2204   
  2213   
  2205   foreach ( $matches[0] as $id => $char )
  2214   foreach ( $matches[0] as $id => $char )
  2206   {
  2215   {
  2267   $pos++;
  2276   $pos++;
  2268   $str2 = substr($haystack, $pos);
  2277   $str2 = substr($haystack, $pos);
  2269   return "{$str1}{$needle}{$str2}";
  2278   return "{$str1}{$needle}{$str2}";
  2270 }
  2279 }
  2271 
  2280 
       
  2281 /**
       
  2282  * Tells if a given IP address is valid.
       
  2283  * @param string suspected IP address
       
  2284  * @return bool true if valid, false otherwise
       
  2285  */
       
  2286  
       
  2287 function is_valid_ip($ip)
       
  2288 {
       
  2289   // These came from phpBB3.
       
  2290   $ipv4 = '(?:(?:\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.){3}(?:\d{1,2}|1\d\d|2[0-4]\d|25[0-5])';
       
  2291   $ipv6 = '(?:(?:(?:[\dA-F]{1,4}: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g_match("/^{$ipv4}$/", $ip) || preg_match("/^{$ipv6}$/", $ip) )
       
  2294     return true;
       
  2295   else
       
  2296     return false;
       
  2297 }
       
  2298 
  2272 //die('<pre>Original:  01010101010100101010100101010101011010'."\nProcessed: ".uncompress_bitfield(compress_bitfield('01010101010100101010100101010101011010')).'</pre>');
  2299 //die('<pre>Original:  01010101010100101010100101010101011010'."\nProcessed: ".uncompress_bitfield(compress_bitfield('01010101010100101010100101010101011010')).'</pre>');
  2273 
  2300 
  2274 ?>
  2301 ?>