– Kamailio SIP Server –

FIFO Client PHP Example

A sample implementation of a FIFO client in PHP you can integrate it in your web applications.

  <head>
  <title>OpenSER FIFO Client</title>
  </head>
   <body>

  <table border="1">
  <tr><th colspan="2">OpenSER FIFO Client</th></tr>
  <form action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="POST">
  <tr>
    <td><textarea name="cmd" cols=40 rows=6></textarea></td>
    <td><input type="submit" name="action" value="Send"></td>
  </tr>
  </form>
  <tr align="center">
    <td colspan="2">
       <font size="-1">Please do not enter the leading ":" or the trailing ":REPLY_FILE_NAME"</font>
    </td>
  </tr>
  </table>

  <?php
  $cmd = NULL;
  if (isset($_POST['cmd'])) {
    $cmd = $_POST['cmd'];
  }
  if (isset($_POST['action']) && ($_POST['action'] == "Send") && (trim(strlen($_POST['cmd'])) > 0)) {
    $host="192.168.1.1";  # IP Address of the OpenSER (FIFO) Server
    $port = 12345;        # Port that OpenSER (FIFO) is listening on
    $pos = strpos($cmd,"\n");
    if ($pos == NULL) {
      $fifo_cmd = ":$cmd:REPLY_FILE_NAME\n";
    } else {
      $t1 = substr($cmd,0,$pos-1);
      $t2 = substr($cmd,$pos);
      $fifo_cmd = ":$t1:REPLY_FILE_NAME$t2\n";
    }
    $timeout = 5;
    $fp = fsockopen ($host, $port, $errno, $errstr, $timeout);
    if (!$fp) {
      die("Error opening socket: $errno $errstr");
    } else {
      echo "<br>FIFO Command: <b><pre>$fifo_cmd</pre></b><br>";
    // write the user string to the socket
    fputs ($fp, $fifo_cmd);
    // get the result
    while ($result = fgets ($fp, 1024)) {
      echo "Response: <b>$result</b><br>";
    }
    // close the connection
    fclose ($fp);
    }
  }
  ?>

  </body>
  </html>

Here is another example using the function write2fifo taken from serweb. It should work as it with a default openser installation. This script comes with no warranty… Type which to list all available commands.

<?php
/* This script has part of it taken from "serweb". 
   serweb talks to SER via FIFO -- this is FIFO's name,
   it must have the same value as SER's fifo config param
*/
$config->fifo_server="/tmp/openser_fifo";

/* values used for names of reply fifos -- they change randomly 
   this values shouldn't be changed unless you well know what are
   you doing
*/
$config->reply_fifo_filename="openserwebfifo_".rand();
$config->reply_fifo_path="/tmp/".$config->reply_fifo_filename;

/* This function is taken from "serweb" */
function write2fifo($fifo_cmd, &$errors, &$status){
	global $config;

	/* check if fifo is running */
	if (!file_exists($config->fifo_server) or 
	     filetype($config->fifo_server)!="fifo"){

		/*log_errors(PEAR::raiseError("FIFO not running or bad path to it", NULL, NULL, 
		           NULL, "fifo path:".$config->fifo_server), $errors);*/
		echo "FIFO not running or bad path to it";
		return false;
	}


	/* open fifo now */
	$fifo_handle=fopen( $config->fifo_server, "w" );
	if (!$fifo_handle) {
		$errors[]="sorry -- cannot open write fifo"; return false;
	}

	/* create fifo for replies */
	@system("mkfifo -m 666 ".$config->reply_fifo_path );


	/* add command separator */
	$fifo_cmd=$fifo_cmd."\n";

	/* write fifo command */
	if (fwrite( $fifo_handle, $fifo_cmd)==-1) {
	    @unlink($config->reply_fifo_path);
	    @fclose($fifo_handle);
		$errors[]="sorry -- fifo writing error"; return false;
	}
	@fclose($fifo_handle);

	/* read output now */
	@$fp = fopen( $config->reply_fifo_path, "r");
	if (!$fp) {
	    @unlink($config->reply_fifo_path);
		$errors[]="sorry -- reply fifo opening error"; return false;
	}

	$status=fgets($fp,256);
	if (!$status) {
	    @unlink($config->reply_fifo_path);
		$errors[]="sorry -- reply fifo reading error"; return false;
	}
	

	$rd="";
	while (!feof($fp)) {
		$rd.=fread($fp,8192);
	}
	@unlink($config->reply_fifo_path);
	
	return $rd;
}
?>

<html>
<head>
  <title>OpenSER FIFO Client</title>
</head>
<body>

  <table border="1">
    <tr><th colspan="2">OpenSER FIFO Client</th></tr>
    <form action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="POST">
      <tr>
        <td><textarea name="cmd" cols=40 rows=6></textarea></td>
        <td><input type="submit" name="action" value="Send"></td>
      </tr>
    </form>
    <tr align="center">
      <td colspan="2">
      <font size="-1">Please do not enter the leading ":" or the trailing ":REPLY_FILE_NAME"</font>
      </td>
    </tr>
  </table>

<?php
$cmd = NULL;
if (isset($_POST['cmd'])) {
  $cmd = $_POST['cmd'];
  }
if (isset($_POST['action']) && ($_POST['action'] == "Send") && (trim(strlen($_POST['cmd'])) > 0)) {
  $pos = strpos($cmd,"\n");
  if ($pos == NULL) {
    $fifo_cmd = ":$cmd:".$config->reply_fifo_filename."\n";
  } else {
    $t1 = substr($cmd,0,$pos-1);
    $t2 = substr($cmd,$pos);
    $fifo_cmd = ":$t1:".$config->reply_fifo_filename."$t2\n";
  }

  $fifo_out=write2fifo($fifo_cmd, $err, $status);

  if ($err) {
    $errors=array_merge($errors, $err); 
    echo "Error 1: $errors";
    die();
  }

  if (substr($status,0,1)!="2" and substr($status,0,3)!="404") {
    $errors[]=$status; 
    echo "Error 2: $status";
    die();
  }
  echo "<br>FIFO Command: <b><pre>$fifo_cmd</pre></b><br>";
  echo "Response: <b>$fifo_out</b><br>";
}
?>

</body>
</html>