Need some PHP help for this script



  • I am not now, and have never claimed to be, a PHP developer.

    What I am wanting to do is create a webpage to drop on my FreePBX system that will let me reboot or reload Yealink phones.

    I have worked out the backend connectivity. Now I jsut need something to parse the output and create the buttons that will send the commands.

    I started this in PHP because that is what the examples I previously had from another script as well as the new stuff I found all used. I absolutely do not care about it remaining in PHP.

    The script will connect to Asterisk and get a list of all the extensions with the command pjsip list endpoints. I have this done.

    The page now needs to parse the result into an array of extensions. I guess a little logic can be used to determine what is really an extension. Here is what is returned. In this example, only the 101 - 110 are valid. At another site it would be 5100 - 5499, and yet another it would be 200-299.
    0_1532284361667_6f76d738-f0f9-4a00-8350-8d99c87f626f-image.png

    I envision this dumping into a list view with a checkbox to select the ones I want to reload or reboot and then two buttons labeled Reload and Reboot respectively.

    The page will then parse for the selected extensions and send the appropriate action based on the button clicked. Potentially an "are you sure" warning.

    The action to send is a simple string with all the extensions tacked on the end space delimited.
    Such as pjsip send notify reload-yealink endpoints 101 102 103 110 or pjsip send notify reboot-yealink endpoints 101 102 103 110

    The code in it's current form is on my github if anyone wants to work on it that way.
    https://github.com/sorvani/freepbx-helper-scripts/blob/master/yealink.reload.php

    <?php
    
    // Some initial sample code found at https://www.voip-info.org/asterisk-manager-example:-php
    // amportal.conf code modified from https://raw.githubusercontent.com/sorvani/freepbx-helper-scripts/master/yl.php
    
        // Open /etc/amportal.conf and get the Asterisk Manager connection information
        define("AMP_CONF", "/etc/amportal.conf");
        $file = file(AMP_CONF);
        if (is_array($file)) {
            foreach ($file as $line) {
                if (preg_match("/^\s*([a-zA-Z0-9_]+)=([a-zA-Z0-9 .&[email protected]=_!<>\"\']+)\s*$/",$line,$matches)) {
                    $amp_conf[ $matches[1] ] = $matches[2];
                }
            }
        }
    
        require_once('DB.php'); //php-pear-db must first be installed on a new FreePBX 14 system
        $db_user = $amp_conf["AMPMGRUSER"];
        $db_pass = $amp_conf["AMPMGRPASS"];
        $db_host = $amp_conf["ASTMANAGERHOST"];
        $db_port = $amp_conf["ASTMANAGERPORT"];
        $db_timeout = $amp_conf["ASTMGRWRITETIMEOUT"];
    
        // Connect to Asterisk Manager and run a command
        $socket = fsockopen($db_host, $db_port, $errno, $errstr, $db_timeout);
        fputs($socket, "Action: Login\r\n");
        fputs($socket, "UserName: $db_user\r\n");
        fputs($socket, "Secret: $db_pass\r\n\r\n");
        fputs($socket, "Action: Command\r\n");
        fputs($socket, "Command: pjsip list endpoints\r\n\r\n");
        fputs($socket, "Action: Logoff\r\n\r\n");
        while (!feof($socket)) {
            $wrets .= fread($socket, 8192);
        }
        fclose($socket);
        echo <<<ASTERISKMANAGEREND
    <pre>
    ASTERISK MANAGER OUTPUT:<br />
    $wrets
    </pre>
    ASTERISKMANAGEREND;
    
    ?>
    


  • I'm not a PHP dev either, but I've been slowly learning it and this seems like a fun challenge to help with that process.

    So, if you don't mind... I'm going to take a stab at it as I have time... or at least until someone else whose more experienced provides a working solution first.



  • It's hard to work without having data to use. Could you copy all that data from your screenshot to a text file so I can copy / paste that to use?



  • @obsolesce said in Need some PHP help for this script:

    It's hard to work without having data to use. Could you copy all that data from your screenshot to a text file so I can copy / paste that to use?

    Sorry I went offline after that to play a game of Life with the kids.



  • For anyone interested. Thanks to @Obsolesce this is progressing nicely.

    Current code is on my github.

    Have extensions being parsed into an array. Now just to wrap things into a pretty form.

    0_1532312957331_94eb9591-cf64-4559-a125-e4b13f99aba7-image.png



  • I added AstMan.php to github and it contains all the connection stuff wrapped in functions with error handling. But I am having a hell of a time trying to use them.
    https://github.com/sorvani/freepbx-helper-scripts/blob/master/AstMan.php

    @dafyre .........



  • @JaredBusch -- Check my pull request.



  • @dafyre said in Need some PHP help for this script:

    @JaredBusch -- Check my pull request.

    That worked, minus a syntax correction.
    0_1532355947016_b1a85472-4c44-4a38-8139-9ef3c7661356-image.png



  • Added in @Obsolesce's last commit for the form.
    0_1532357038561_129fcd20-34a4-4889-adb5-108779e6150c-image.png

    Now to make the form actually submit.



  • next bit. I have this being returned by a function.

    string(3771) "Asterisk Call Manager/4.0.3
    Response: Success
    Message: Authentication accepted
    
    Event: FullyBooted
    Privilege: system,all
    Uptime: 151132
    LastReload: 40568
    Status: Fully Booted
    
    Response: Success
    EventList: start
    Message: Following are Events for each object associated with the Endpoint
    
    Event: EndpointDetail
    <snip not needed>
    
    Event: AuthDetail
    <snip not needed>
    
    Event: AorDetail
    <snip not needed>
    
    Event: ContactStatusDetail
    AOR: 5120
    URI: sip:[email protected]:5060
    UserAgent: Yealink SIP-T42G 29.83.0.35
    RegExpire: 1532368885
    ViaAddress: 10.202.0.150:5060
    CallID: 0_12383642[email protected]
    Status: Reachable
    RoundtripUsec: 15857
    EndpointName: 5120
    ID: 5120;@9e6900eb84ddc2a05701b109000ae8e5
    AuthenticateQualify: 0
    OutboundProxy: 
    Path: 
    QualifyFrequency: 60
    QualifyTimeout: 3.000
    
    
    Event: EndpointDetailComplete
    EventList: Complete
    ListItems: 4
    
    Response: Goodbye
    Message: Thanks for all the fish.
    
    

    I do not need the Event responses except for ContactStatusDetail.
    I want to get that section dropped into an array in PHP.

    if I am somehow getting each line of text in the variable $line, I can use explode I believe?

    $split =  explode(":",$line,2);
    

    I would then need to add each line to an array?



  • Frustration and finally progress. Buttons do nothing yet, but @Obsolesce has been working on that in his spare time.

    0_1532404624691_3aae8246-d367-48a9-83bb-35e9f7ca5f6b-image.png

    Currently requires 2 files from my github. AstMan.php and yealink.reload.php



  • @jaredbusch said in Need some PHP help for this script:

    0_1532404624691_3aae8246-d367-48a9-83bb-35e9f7ca5f6b-image.png

    What you're building looks a little like what you have in 3CX. They also show ip address, mac address and the user name and some other minor stuff. As options besides reload (called reprovision) and reboot, they also "firmware update" and also a direct link to the phones webserver.
    Maybe some of that would make sense to add to your php script?

    PS. Checking the input from the form is relatively easy. Make sure the form tag in the generated html uses post <form method="post"> . Then when you receive the values into the php script you can do print_r($_POST); and you'll see how everything looks. Then it's just a matter of going through the $_POST array and picking out the extensions you selected.

    It's the name of the checkboxes in html that will appear in the $_POST array. So when the html table is generated use the extension number as name for the checkboxes, for instance ext_101, ext_102 etc.



  • This is now working at a basic level.

    Code on GitHub.



  • github updated. files renamed a bit. Still a work in progress, but things working much better.

    0_1533163657304_1cb7dbcf-38f3-48da-a102-fc1f7348fe17-image.png