Looking for feedback to the best solution to do this.
I’m really digging the idea of API behind freepbx upcoming versions.
Following this guide: PBX API > RESTful I was able to create a quick php script to call pjsip extension status (based off of asterisk info contacts)
<?php
namespace FreePBX\Api\Rest;
use FreePBX\modules\Api\Rest\Base;
class Status extends Base {
public static function getScopes() {
return [
'read:status' => [
'description' => _('Read Extenson status'),
]
];
}
public function setupRoutes($app) {
$app->get('/status', function ($request, $response, $args) {
$data = array();
$online = array();
$offline = array();
$resp = shell_exec('asterisk -rx "pjsip show endpoints" | egrep "Unavailable|Avail | grep -v dpma_endpoint"');
$resp = preg_replace('/ +/', ' ', $resp);
$arr = explode("\n", $resp);
foreach ($arr as $ext) {
$type = substr($ext,0,10);
if ( $type == " Contact: "){
$pos = strpos($ext,'/');
$ext = substr($ext,10,$pos-10);
array_push($online, $ext);
}elseif ( $type == " Endpoint:") {
$pos = strpos($ext,'/');
$ext = substr($ext,11,$pos-11);
array_push($offline, $ext);
}
}
$data = [
"total" => array( "online" => count($online), "offline" => count($offline)),
"online" => $online,
"offline" => $offline
];
return $response->withJson($data);
})->add($this->checkReadScopeMiddleware('status'));
}
}
Few questions.
Would calling asterisk -rx like this be problematic or vulnerable?
Would there be an easier way to do this?
And if this method is fine. Any tips on making the php code cleaner? I’m just a tinkerer and not a developer.
Postman returns json data as so
{ "total": { "online": 1, "offline": 3 }, "online": [ "72741" ], "offline": [ "2", "3", "727412" ] }