MyBB Community Forums

Full Version: preg_match(): Compilation failed: nothing to repeat at offset 1
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
preg_match(): Compilation failed: nothing to repeat at offset 1
if(!preg_match("#^".trim($exclude)."+[\d\w]*#i", $url)

define("IN_MYBB", 1);
define('THIS_SCRIPT', 'safelink.php');

require_once("./global.php");

$lang->load("safelink");

// Add link in breadcrumb
add_breadcrumb($lang->safelink, "safelink.php");
if($mybb->settings['safelink_enabled'] == 1)
{
	if($mybb->input['url'])
	{
		
		if(!filter_var($mybb->input['url'], FILTER_VALIDATE_URL))
		{
			$error = $lang->badurl;
		}
		else
		{
			$v = substr($_SERVER['REQUEST_URI'], strpos($_SERVER['REQUEST_URI'], "safelink.php?url=")); // these 2 lines filter out the url to continue to
			$url = str_replace('safelink.php?url=', '', $v); // only problem with these is it will replace all occurrencesof 'safelink.php?url=' in the url, and I don't know how to fix this (preg_replace perhaps?)
			$group = $mybb->user['usergroup'];
			$groups = explode(",", $mybb->settings['safelink_groups']);
			$excludes = explode("\n", $mybb->settings['safelink_urls']);
			foreach($excludes as $exclude)
			{
				if(!preg_match("#^".trim($exclude)."+[\d\w]*#i", $url) && !in_array($group, $groups)) // not an excluded site, go to safelink page and link intended URL
				{
					$warning = $lang->warning;
					$continue = $lang->continue;
				}
				else // site excluded from safelink OR user is in excluded usergroup
				{
					header("location:$url");
				}
			}
		}
	}
	else
	{
		$error = $lang->nourl;
	}
}
else
{
	$error = $lang->disabled;
}
eval("\$safelink = \"".$templates->get("safelink")."\";");

output_pag
I personally prefer not to use preg_match for something that you consider a crucial part of your security, there are exceptions of course, but you could get away with:

<?php
$url = parse_url($myUrl);
$targetHost = $url['host'];
if (in_array($targetHost, $excluded)) {
// This is an excluded URL
}

If you want to also block subdomains of a blocked host you can do:
<?php
$url = parse_url($myUrl);
$targetHost = $url['host'];
if (in_array($targetHost, $excluded)) {
// This is an excluded URL
}
foreach ($excluded as $blockedHost) {
if (strpos($targetHost, $blockedHost) === strlen($targetHost) - strlen($blockedHost) +1) {
// this is a sub of an excluded host
}
}