Changeset 2732

Show
Ignore:
Timestamp:
10/16/06 03:33:26 (7 years ago)
Author:
gregmac
Message:

Trunk dial patterns now support + and | in the same pattern

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • freepbx/trunk/amp_conf/agi-bin/fixlocalprefix

    r2616 r2732  
    110110} 
    111111 
     112function fixNumber($pattern, $number, &$agi) { 
     113  // valid chars in a pattern are 0-9XNZwW#*\.\[\]\-\+\| 
     114  $chars = '0-9XNZwW#*\.\[\]\-'; //escaped pcre-ready 
     115   
     116  // sanitize the pattern - remove any non-pattern chars 
     117  $regex = preg_replace("/[^0-9XNZwW#*\.\[\]\-\+\|]/", "", $regex); 
     118  // Also kill the '-' characters outside of groups 
     119  $regex = preg_replace("/((?:\[[^\]]*\])*)([^\[\]\-]*)-?/", "$1$2", $regex); 
     120 
     121  // attempt to grab the pieces of the pattern 
     122  if (preg_match('/^(([0-9XNZwW#*\.\[\]\-]+)\|)?(([0-9XNZwW#*\.\[\]\-]+)\+)?([0-9XNZwW#*\.\[\]\-]+)$/', $pattern, $matches)) { 
     123    // one of NXXXXXX, 613|NXXXXXX   1+NXXXXXX    613|1+NXXXXXX,   
     124    // matches[2] = drop (eg 613),  matches[4] = prefix (eg 1),  matches[5] = rest of number (eg NXXXXX) 
     125     
     126    $drop = $matches[2]; 
     127    $prefix = $matches[4]; 
     128    $static = $matches[5]; 
     129  } else if (preg_match('/^(([0-9XNZwW#*\.\[\]\-]+)\+)?(([0-9XNZwW#*\.\[\]\-]+)\|)?([0-9XNZwW#*\.\[\]\-]+)$/', $pattern, $matches)) { 
     130    // one of NXXXXXX,  613|NXXXXXX   1+NXXXXXX    1+613|NXXXXXX 
     131    // matches[2] = prefix (eg 1),  matches[4] = drop (eg 613),  matches[5] = rest of number (eg NXXXXX) 
     132     
     133    $drop = $matches[4]; 
     134    $prefix = $matches[2]; 
     135    $static = $matches[5]; 
     136  } else { 
     137    if (!is_null($agi)) { 
     138      $agi->verbose('Could not understand pattern "'.$pattern.'"', 3); 
     139    } 
     140    return $false; 
     141  } 
     142   
     143  // convert asterisk pattern matching into perl regular expression 
     144  $regex = str_replace( 
     145      array( 
     146        "X", 
     147        "Z", 
     148        "N", 
     149        ".", 
     150      ), 
     151      array( 
     152        "[0-9]", 
     153        "[1-9]", 
     154        "[2-9]", 
     155        "[0-9#*]+", 
     156      ), 
     157      // note, we're doing a subpattern match on the static portion so it can be extracted later 
     158      $drop.'('.$static.')'); 
     159   
     160  if (preg_match('/^'.$regex.'$/', $number, $matches)) { 
     161    return $prefix.$matches[1]; 
     162  } 
     163  return false; 
     164} 
     165 
    112166/**********************************************************************************************************************/ 
    113167 
     
    148202      // $rule is a dial rule 
    149203       
    150       $regex = $rule; 
    151       $prefix = ""; 
    152  
    153       // Remove all non-pattern characters from $regex except for '+' and '|'. 
    154       // Allow groups like "[0-9]" to remain. 
    155       $regex = preg_replace("/[^0-9XNZwW#*\.\[\]\-\+\|]/", "", $regex); 
    156        
    157       // Also kill the '-' characters outside of groups 
    158       $regex = preg_replace("/((?:\[[^\]]*\])*)([^\[\]\-]*)-?/", "$1$2", $regex); 
    159  
    160       if (false !== ($pos = strpos($regex,"+"))) { 
    161         $prefix = substr($regex,0,$pos); 
    162         $regex  = substr($regex,$pos); 
    163       } 
    164       if (false !== ($pos = strpos($regex,"|"))) { 
    165         // we're removing digits 
    166         $type  = "remove"; 
    167         $regex = substr($regex,0,$pos) . "(" . substr($regex, $pos) . ")"; 
    168       } else { 
    169         $pos = 0; 
    170         $type = "asis"; 
    171       } 
    172  
    173       // Remove any remaining '+' and '|' characters from $regex. 
    174       // Note that there will be parenthesis in the regex if the type 
    175       // is "remove". 
    176       $regex = preg_replace("/[\+\|]/", "", $regex); 
    177  
    178       // convert asterisk pattern matching into perl regular expression 
    179       $regex = str_replace( 
    180           array( 
    181             "X", 
    182             "Z", 
    183             "N", 
    184             ".", 
    185           ), 
    186           array( 
    187             "[0-9]", 
    188             "[1-9]", 
    189             "[2-9]", 
    190             "[0-9#*]+", 
    191           ), 
    192           $regex); 
    193  
    194       // $agi->conlog("Trying to match number '$number' with regex '$regex' ($rule)"); 
    195       if (preg_match("/^$regex$/",$number)) { 
    196         // it matched 
    197         if ($type == "remove") { 
    198           $number = preg_replace("/^$regex$/", "$1", $number); 
    199           $agi->verbose("Removed prefix.  New number: $number"); 
    200           $agi->set_variable("DIAL_NUMBER", $number); 
    201         } 
    202         if ($prefix != '') { 
    203           // we're adding digits 
    204           $number = $prefix . $number; 
    205           $agi->verbose("Added prefix.  New number: $number"); 
    206           $agi->set_variable("DIAL_NUMBER", $number); 
    207         } 
     204      if ($newnum = fixNumber($rule, $number, $agi)) { 
     205        $agi->verbose('Dialpattern '.$rule.' matched. '.$number.' -> '.$newnum, 1); 
     206        $agi->set_variable("DIAL_NUMBER", $newnum); 
    208207         
    209208        // reverted back form r2080 (r2081 on trac) so that any pattern match will end 
     
    211210        // patterns are used to avoid matching substitution rules for exception cases. 
    212211        exit(0); 
    213          
    214212      } // else, it didn't match this rule 
    215213    } // else, this isn't a rule