diff --git a/app/destinations/app_config.php b/app/destinations/app_config.php index 138a410c98..4cc868d413 100644 --- a/app/destinations/app_config.php +++ b/app/destinations/app_config.php @@ -109,6 +109,10 @@ $apps[$x]['db'][$y]['fields'][$z]['type'] = "text"; $apps[$x]['db'][$y]['fields'][$z]['description']['en-us'] = "Enter the number."; $z++; + $apps[$x]['db'][$y]['fields'][$z]['name'] = "destination_number_regex"; + $apps[$x]['db'][$y]['fields'][$z]['type'] = "text"; + $apps[$x]['db'][$y]['fields'][$z]['description']['en-us'] = "Regular Expression version of destination number"; + $z++; $apps[$x]['db'][$y]['fields'][$z]['name'] = "destination_caller_id_name"; $apps[$x]['db'][$y]['fields'][$z]['type'] = "text"; $apps[$x]['db'][$y]['fields'][$z]['description']['en-us'] = "Enter the caller id name."; diff --git a/app/destinations/destination_edit.php b/app/destinations/destination_edit.php index 410736a166..133ba059a7 100644 --- a/app/destinations/destination_edit.php +++ b/app/destinations/destination_edit.php @@ -17,7 +17,7 @@ The Initial Developer of the Original Code is Mark J Crane - Portions created by the Initial Developer are Copyright (C) 2013-2015 + Portions created by the Initial Developer are Copyright (C) 2013-2016 the Initial Developer. All Rights Reserved. Contributor(s): @@ -83,7 +83,6 @@ if (file_exists($_SERVER["PROJECT_ROOT"]."/app/billing/app_config.php")) { $destination_type = check_str($_POST["destination_type"]); $destination_number = check_str($_POST["destination_number"]); $db_destination_number = check_str($_POST["db_destination_number"]); - $regex_destination_number = str_replace("+", "\\+", $destination_number); $destination_caller_id_name = check_str($_POST["destination_caller_id_name"]); $destination_caller_id_number = check_str($_POST["destination_caller_id_number"]); $destination_cid_name_prefix = check_str($_POST["destination_cid_name_prefix"]); @@ -97,6 +96,9 @@ if (file_exists($_SERVER["PROJECT_ROOT"]."/app/billing/app_config.php")) { $currency_buy = check_str($_POST["currency_buy"]); $destination_accountcode = check_str($_POST["destination_accountcode"]); $destination_carrier = check_str($_POST["destination_carrier"]); + //convert the number to a regular expression + $destination_number_regex = string_to_regex($destination_number); + $_POST["destination_number_regex"] = $destination_number_regex; } //unset the db_destination_number @@ -221,7 +223,7 @@ if (count($_POST) > 0 && strlen($_POST["persistformvar"]) == 0) { $dialplan["dialplan_details"][$y]["domain_uuid"] = $domain_uuid; $dialplan["dialplan_details"][$y]["dialplan_detail_tag"] = "condition"; $dialplan["dialplan_details"][$y]["dialplan_detail_type"] = "destination_number"; - $dialplan["dialplan_details"][$y]["dialplan_detail_data"] = "(".$regex_destination_number.")"; + $dialplan["dialplan_details"][$y]["dialplan_detail_data"] = $destination_number_regex; $dialplan["dialplan_details"][$y]["dialplan_detail_order"] = $dialplan_detail_order; $y++; diff --git a/resources/functions.php b/resources/functions.php index 85f6c010a2..95154a55bb 100644 --- a/resources/functions.php +++ b/resources/functions.php @@ -1381,16 +1381,65 @@ function number_pad($number,$n) { //email validate function email_validate($strEmail){ - $validRegExp = '/^[a-zA-Z0-9\._-]+@[a-zA-Z0-9\._-]+\.[a-zA-Z]{2,3}$/'; - // search email text for regular exp matches - preg_match($validRegExp, $strEmail, $matches, PREG_OFFSET_CAPTURE); + $validRegExp = '/^[a-zA-Z0-9\._-]+@[a-zA-Z0-9\._-]+\.[a-zA-Z]{2,3}$/'; + // search email text for regular exp matches + preg_match($validRegExp, $strEmail, $matches, PREG_OFFSET_CAPTURE); + if (count($matches) == 0) { + return 0; + } + else { + return 1; + } + } - if (count($matches) == 0) { - return 0; - } - else { - return 1; - } -} +//converts a string to a regular expression + function string_to_regex($string) { + //escape the plus + if (substr($string, 0, 1) == "+") { + $string = "^\\+(".substr($string, 1).")$"; + } + //convert N,X,Z syntax to regex + $string = str_ireplace("N", "[2-9]", $string); + $string = str_ireplace("X", "[0-9]", $string); + $string = str_ireplace("Z", "[1-9]", $string); + //add ^ to the start of the string if missing + if (substr($string, 0, 1) != "^") { + $string = "^".$string; + } + //add $ to the end of the string if missing + if (substr($string, -1) != "$") { + $string = $string."$"; + } + //add the round brackgets ( and ) + if (!strstr($string, '(')) { + if (strstr($string, '^')) { + $string = str_replace("^", "^(", $string); + } + else { + $string = '^('.$string; + } + } + if (!strstr($string, ')')) { + if (strstr($string, '$')) { + $string = str_replace("$", ")$", $string); + } + else { + $string = $string.')$'; + } + } + //return the result + return $string; + } + //$string = "+12089068227"; echo $string." ".string_to_regex($string)."\n"; + //$string = "12089068227"; echo $string." ".string_to_regex($string)."\n"; + //$string = "2089068227"; echo $string." ".string_to_regex($string)."\n"; + //$string = "^(20890682[0-9][0-9])$"; echo $string." ".string_to_regex($string)."\n"; + //$string = "1208906xxxx"; echo $string." ".string_to_regex($string)."\n"; + //$string = "nxxnxxxxxxx"; echo $string." ".string_to_regex($string)."\n"; + //$string = "208906xxxx"; echo $string." ".string_to_regex($string)."\n"; + //$string = "^(2089068227"; echo $string." ".string_to_regex($string)."\n"; + //$string = "^2089068227)"; echo $string." ".string_to_regex($string)."\n"; + //$string = "2089068227$"; echo $string." ".string_to_regex($string)."\n"; + //$string = "2089068227)$"; echo $string." ".string_to_regex($string)."\n"; -?> +?> \ No newline at end of file