Hi Community,
i am developing FreePBX OSS Endpointman at the moment and need some help with pattern functions.
I want to extend the “Model” function of @tm1000 with an easier way to add config values for specific models.
This is the momentan function:
/**
* Simple Model if then statement, should be called before any parsing!
* @param string $file_contents Full Contents of the configuration file
* @return string Full Contents of the configuration file (After Parsing)
* @example {if model="6757*"}{/if}
* @author Andrew Nagy
*/
private function parse_conditional_model($file_contents) {
$pattern = "/{if model=\"(.*?)\"}(.*?){\/if}/si";
while (preg_match($pattern, $file_contents, $matches)) {
//This is exactly like the fnmatch function except it will work on POSIX compliant systems
//http://php.net/manual/en/function.fnmatch.php
if (preg_match("#^" . strtr(preg_quote($matches[1], '#'), array('\*' => '.*', '\?' => '.', '\[' => '[', '\]' => ']')) . "$#i", $this->model)) {
$file_contents = preg_replace($pattern, $matches[2], $file_contents, 1);
} else {
$file_contents = preg_replace($pattern, "", $file_contents, 1);
}
}
return($file_contents);
}
I want to create a new function which should look like this: @example {if models=“T56A,T58A,T58V”}{/if}
Does someone know this pattern things better and can help me integrating this into OSS Endpointman?
/**
* Simple Models if then statement, should be called before any parsing!
* @param string $file_contents Full Contents of the configuration file
* @return string Full Contents of the configuration file (After Parsing)
* @example {if models="T58V,T58A"}{/if}
* @author Andrew Nagy, Matthias Binder
*/
private function parse_conditional_models($file_contents) {
$pattern = "/{if models=\"(.*?)\"}(.*?){\/if}/si";
while (preg_match($pattern, $file_contents, $matches)) {
//This is exactly like the fnmatch function except it will work on POSIX compliant systems
//http://php.net/manual/en/function.fnmatch.php
$models = explode("," , $matches[1]);
if(in_array($this->model,$models)) {
$matches[1] = $this->model;
}
if (preg_match("#^" . strtr(preg_quote($matches[1], '#'), array('\*' => '.*', '\?' => '.', '\[' => '[', '\]' => ']')) . "$#i", $this->model)) {
$file_contents = preg_replace($pattern, $matches[2], $file_contents, 1);
} else {
$file_contents = preg_replace($pattern, "", $file_contents, 1);
}
}
return($file_contents);
}
I mean it is a string to strpos isn’t a bad thing… until you have two models “T56” and “T56A”… if you did a models match for T56 but used strpos T56A would match which would not be intended.
Yes i implemented your solution because it is strict and works prefect.
Already tested this with my new Yealink T5X Package.
I needed this Feature because i implemented the “Door Phone” features and only T58A/T58V Support this. Now i can implement this easier into one config file without text like {if model = T58[AV]}. I like to write {if models = T58A,T58V}