Need Help with Pattern Check function (OSS Endpointman Development)

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?

Thanks for any help.

Try using http://www.phpliveregex.com/ to generate something that will work for you

Here’s the work for you:

https://www.phpliveregex.com/p/oqv

then

$models = explode(",",$output_array[1]);

1 Like

Thank your very much!

And this function should do what it should do :slight_smile:

Will be added into the next Pull Request.

/**
 * 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);
}

You could also explode $matches. This way it’s more strict.

$models = explode(",",$matches[1]);
if(in_array($this->model,$models
2 Likes

Ohhh yeppa yes :slight_smile:

I already had it exploded, than i had a mistake with thinking what i should do with the array :smiley: I made a foreach command and got a loop in my script.

However now it works :slight_smile:

Thank you!

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}

:star_struck::star_struck: it works ^^

Dev Yealink T5X
image
(Yealink T58V IP Phone Review - YouTube)

Dev. Grandstream GDS3710 Video Door System:


1 Like

provisioner_net.tgz (13,0 KB)

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.