Documentation, format class, no modification. (#7629)

This commit is contained in:
frytimo
2025-11-19 12:48:36 -04:00
committed by GitHub
parent 0ea256fce8
commit 34821bed7e
36 changed files with 12982 additions and 11551 deletions

View File

@@ -37,6 +37,7 @@ class buffer {
return $tmp;
}
}
//$b = new buffer;
//$b->append("hello\nworld\n");
//print($b->read_line());
@@ -44,23 +45,24 @@ class buffer {
/**
* Subscribes to the event socket of the FreeSWITCH (c) Event Socket Server
*
* @depends buffer::class
*/
class event_socket {
private static $socket = null;
public $fp;
/**
* Used as a flag to determine if the socket should be created automatically
*
* @var bool
*/
protected $auto_create;
private $buffer;
public $fp;
private static $socket = null;
private $config;
/**
* Create a new connection to the socket
*
* @param resource|false $fp
*/
public function __construct($fp = false, ?config $config = null) {
@@ -71,15 +73,59 @@ class event_socket {
}
/**
* Ensures a closed connection on destruction of object
* Sends an API command on the socket
*
* @param string $api_cmd
*
* @return string|false Response from server or false if failed
*/
public function __destructor() {
$this->close();
public static function api(string $api_cmd) {
return self::command('api ' . $api_cmd);
}
/**
* Sends a command on the socket blocking for a response
*
* @param string $cmd
*
* @return string|false Response from server or false if failed
*/
public static function command(string $cmd) {
return self::create()->request($cmd);
}
/**
* Send a command to the FreeSWITCH Event Socket Server
* <p>Multi-line commands can be sent when separated by '\n'</p>
*
* @param string $cmd Command to send through the socket
*
* @return mixed Returns the response from FreeSWITCH or false if not connected
* @depends read_event()
*/
public function request($cmd) {
if (!$this->connected()) {
return false;
}
$cmd_array = explode("\n", $cmd);
foreach ($cmd_array as $value) {
fputs($this->fp, $value . "\n");
}
fputs($this->fp, "\n"); //second line feed to end the headers
$event = $this->read_event();
if (array_key_exists('$', $event)) {
return $event['$'];
}
return $event;
}
/**
* Read the event body from the socket
* @return string|false Content body or false if not connected or empty message
*
* @return mixed Content body or false if not connected or empty message
* @depends buffer::class
*/
public function read_event() {
@@ -88,7 +134,7 @@ class event_socket {
}
$b = $this->buffer;
$content = array();
$content = [];
while (true) {
$line = $b->read_line();
@@ -96,7 +142,7 @@ class event_socket {
if ($line === '') {
break;
}
list($key, $value) = explode(':', $line, 2);
[$key, $value] = explode(':', $line, 2);
$content[trim($key)] = trim($value);
}
@@ -132,14 +178,41 @@ class event_socket {
return $content;
}
/**
* Create uses a singleton design to return a connected socket to the FreeSWITCH Event Socket Layer
*
* @param string $host Host or IP address of FreeSWITCH event socket server. Defaults to 127.0.0.1
* @param string $port Port number of FreeSWITCH event socket server. Defaults to 8021
* @param string $password Password of FreeSWITCH event socket server. Defaults to ClueCon
* @param int $timeout_microseconds Number of microseconds before timeout is triggered on socket
*
* @return self
* @global array $conf Global configuration used in config.conf
*/
public static function create($host = null, $port = null, $password = null, $timeout_microseconds = 30000): self {
//create the event socket object
if (self::$socket === null) {
self::$socket = new event_socket();
}
//attempt to connect it
if (!self::$socket->connected()) {
self::$socket->connect($host, $port, $password, $timeout_microseconds);
}
return self::$socket;
}
/**
* Connect to the FreeSWITCH (c) event socket server
* <p>If the configuration is not loaded then the defaults of
* host 127.0.0.1, port of 8021, and default password of ClueCon will be used</p>
* @param null|string $host Host or IP address of FreeSWITCH event socket server. Defaults to 127.0.0.1
* @param null|string|int $port Port number of FreeSWITCH event socket server. Defaults to 8021
* @param null|string $password Password of FreeSWITCH event socket server. Defaults to ClueCon
* @param int $timeout_microseconds Number of microseconds before timeout is triggered on socket. Defaults to 30,000
*
* @param null|string $host Host or IP address of FreeSWITCH event socket server. Defaults to
* 127.0.0.1
* @param null|string|int $port Port number of FreeSWITCH event socket server. Defaults to 8021
* @param null|string $password Password of FreeSWITCH event socket server. Defaults to ClueCon
* @param int $timeout_microseconds Number of microseconds before timeout is triggered on socket.
* Defaults to 30,000
*
* @return bool Returns true on success or false if not connected
*/
public function connect($host = null, $port = null, $password = null, $timeout_microseconds = 30000) {
@@ -165,7 +238,7 @@ class event_socket {
//wait auth request and send a response
while ($this->connected()) {
$event = $this->read_event();
if(($event['Content-Type'] ?? '') === 'auth/request'){
if (($event['Content-Type'] ?? '') === 'auth/request') {
fputs($this->fp, "auth $password\n\n");
break;
}
@@ -186,8 +259,60 @@ class event_socket {
return $this->connected();
}
/**
* Sends an API command to FreeSWITCH using asynchronous (non-blocking) mode
*
* @param string $cmd API command to send
*
* @returns string $job_id the Job ID for tracking completion status
*/
public static function async(string $cmd) {
return self::command('bgapi ' . $cmd);
}
/**
* Ensures a closed connection on destruction of object
*/
public function __destructor() {
$this->close();
}
/**
* Close the socket connection with the FreeSWITCH Event Socket Server.
*
* @return void
*/
public function close() {
//fp is public access so ensure it is a resource before closing it
if (is_resource($this->fp)) {
try {
fclose($this->fp);
} catch (Exception $t) {
//report it
trigger_error("event_socket failed to close socket", E_USER_WARNING);
}
} else {
//log an error if fp was set to something other than a resource
if ($this->fp !== false) {
trigger_error("event_socket not a resource", E_USER_ERROR);
}
}
//force fp to be false
$this->fp = false;
}
/**
* alias of connected
*
* @return bool
*/
public function is_connected(): bool {
return $this->connected();
}
/**
* Tests if connected to the FreeSWITCH Event Socket Server
*
* @return bool Returns true when connected or false when not connected
*/
public function connected(): bool {
@@ -203,124 +328,20 @@ class event_socket {
return true;
}
/**
* alias of connected
* @return bool
*/
public function is_connected(): bool {
return $this->connected();
}
/**
* Send a command to the FreeSWITCH Event Socket Server
* <p>Multi-line commands can be sent when separated by '\n'</p>
* @param string $cmd Command to send through the socket
* @return mixed Returns the response from FreeSWITCH or false if not connected
* @depends read_event()
*/
public function request($cmd) {
if (!$this->connected()) {
return false;
}
$cmd_array = explode("\n", $cmd);
foreach ($cmd_array as $value) {
fputs($this->fp, $value."\n");
}
fputs($this->fp, "\n"); //second line feed to end the headers
$event = $this->read_event();
if (array_key_exists('$', $event)) {
return $event['$'];
}
return $event;
}
/**
* Sets the current socket resource returning the old
*
* @param resource|bool $fp Sets the current FreeSWITCH resource
*
* @return mixed Returns the original resource
* @deprecated since version 5.1
*/
public function reset_fp($fp = false){
public function reset_fp($fp = false) {
$tmp = $this->fp;
$this->fp = $fp;
return $tmp;
}
/**
* Closes the socket
*/
public function close() {
//fp is public access so ensure it is a resource before closing it
if (is_resource($this->fp)) {
try {
fclose($this->fp);
} catch (\Exception $t) {
//report it
trigger_error("event_socket failed to close socket", E_USER_WARNING);
}
} else {
//log an error if fp was set to something other than a resource
if ($this->fp !== false) {
trigger_error("event_socket not a resource", E_USER_ERROR);
}
}
//force fp to be false
$this->fp = false;
}
/**
* Create uses a singleton design to return a connected socket to the FreeSWITCH Event Socket Layer
* @global array $conf Global configuration used in config.conf
* @param string $host Host or IP address of FreeSWITCH event socket server. Defaults to 127.0.0.1
* @param string $port Port number of FreeSWITCH event socket server. Defaults to 8021
* @param string $password Password of FreeSWITCH event socket server. Defaults to ClueCon
* @param int $timeout_microseconds Number of microseconds before timeout is triggered on socket
* @return self
*/
public static function create($host = null, $port = null, $password = null, $timeout_microseconds = 30000): self {
//create the event socket object
if (self::$socket === null) {
self::$socket = new event_socket();
}
//attempt to connect it
if(!self::$socket->connected()) {
self::$socket->connect($host, $port, $password, $timeout_microseconds);
}
return self::$socket;
}
/**
* Sends a command on the socket blocking for a response
* @param string $cmd
* @return string|false Response from server or false if failed
*/
public static function command(string $cmd) {
return self::create()->request($cmd);
}
/**
* Sends an API command on the socket
* @param string $api_cmd
* @return string|false Response from server or false if failed
*/
public static function api(string $api_cmd) {
return self::command('api '.$api_cmd);
}
/**
* Sends an API command to FreeSWITCH using asynchronous (non-blocking) mode
* @param string $cmd API command to send
* @returns string $job_id the Job ID for tracking completion status
*/
public static function async(string $cmd) {
return self::command('bgapi '.$cmd);
}
}
// $esl = event_socket::create('127.0.0.1', 8021, 'ClueCon');
// print($esl->request('api sofia status'));
?>