Location: PHPKode > scripts > floSIP Packet > flosip-packet/floSIP_Packet.php
<?
class floSIP_Packet {
/* 
Level 1: SIP Syntax

NOTE: The "" header will be transmitted as the body.  The "SIP" header will be 
transmitted on the first line without a header name.  

I.E. 

$message_array = array("" => "this is the body\r\nbody line 2",
	"SIP" => "INVITE hide@address.com SIP/2.0",
	"From" => "\"Me Somebody\"<hide@address.com>");

will be translated to using to_stream():

$message_stream = "INVITE hide@address.com SIP/2.0\r\nFrom: \"Me Somebody\"<hide@address.com>\r\n\r\nthis is the body\r\nbody line 2\r\n";

and viceversa with to_array()

CRLF's are automatically generated between 
headers, and 2 before & 2 after the body.  

$this->header_update($header_name, $header_value, $where)
This is pretty self explainatory, except the $where part.  If $where == 0,
the header by that name is replaced if it exists or added otherwise.

If $where > 0 or < 0, the header is inserted after or before the existing
headers by that name respectively or added if none exist.  
*/
	var $message_stream = "";
	var $message_array = array("headers" => array(), "body" => "");
	var $parse_error = "";
	function floSIPPacket($message = null) {
		if (is_array($message)) {
			// Incoming parse, create stream,
			$this->to_stream($message);
		} elseif($message) {
			// Incoming parse, create associative array
			$this->to_array($message);
		}
	}
	function to_stream($message_array = false) {
		$message_array = $message_array?$message_array:$this->message_array;
		$message_stream = "";
		foreach ($message_array["headers"] as $key => $value) {
			if ($this->header_validate($key, $value)) {
				if (strtolower($value["name"]) == "sip") {
					$message_stream = $value["value"]."\r\n".$message_stream;
				} else {
					$message_stream .= $value["name"].": ".$value["value"]."\r\n";
				}
			} else {
				$this->parse_error = "Unable to validate header '$key'.";
				return false;
			}
		}
		$message_stream .= "\r\n";
		if (isset($message_array["body"])) {
			$message_stream .= $message_array["body"]."\r\n\r\n";
		}
		$this->message_array = $message_array;
		$this->message_stream = $message_stream;
		return $message_stream;
	}
	function to_array($message_stream = false) {
		$message_stream = $message_stream?$message_stream:$this->message_stream;
		$message_explode = explode("\r\n", $message_stream);
		$this->message_array = array("headers" => array());
		while($message_header = array_shift($message_explode)) {
			$message_header_array = $this->header_read($message_header);
			$this->header_update($message_header_array["name"], $message_header_array["value"], 1);
		}
		$msg_body = implode("\r\n", $message_explode);
		if (substr($msg_body, -4) == "\r\n\r\n") {
			$msg_body = substr($msg_body, 0, strlen($msg_body) - 4);
		}
		$this->header_update("", $msg_body);
		return $message;
	}
	function header_update($header_name = false, $header_value = false, $where = 0) {
		$one_only_list = array("SIP", "From", "to", "Contact", "Content-Length");
		foreach($one_only_list as $one_only) {
			if (strtolower($one_only) == $header_name) {
				$where = 0;
			}
		}
		if ($header_name == "") {
			$this->message_array["body"] = $header_value;
			$this->header_update("Content-Length", strlen($header_value));
		} elseif ($this->header_validate($header_name, $header_value)) {
			if ($where == 0) {
				foreach ($this->message_array["headers"] as $key => $value) {
					if (strtolower($value["name"]) == strtolower($header_name)) {
						unset($this->message_array["headers"][$key]);
					}
				}
			} else {
				if ($where > 0) {
					$this->message_array["headers"] = array_reverse($this->message_array["headers"]);
				}
				$headers_array_temp = array();
				$foundinsertpoint = false;
				foreach ($this->message_array["headers"] as $key => $value) {
					if (strtolower($value["name"]) == strtolower($header_name) && !$foundinsertpoint) {
						$foundinsertpoint = true;
						$headers_array_temp[] = array("name" => $header_name, "value" => $header_value);
					}
					$headers_array_temp[] = $value;
				}
				if ($where > 0) {
					$this->message_array["headers"] = array_reverse($headers_array_temp);
				} else {
					$this->message_array["headers"] = $headers_array_temp;
				}
			}
			if (!$where || !$foundinsertpoint) {
				$this->message_array["headers"][] = array("name" => $header_name, "value" => $header_value);
			}
		} else {
			$this->parse_error = "Unable to validate header.";
			return false;
		}
		$this->to_stream();
	}
	function header_validate($header_name = false, $header_value = false) {
		if (strpos($header_name, ":") !== false) {
			return false;
		}
		return true;
	}
	function header_read($header_string) {
		if (strpos($header_string, ":") !== false) {
			$header_explode = explode(":", $header_string);
			$header_name = array_shift($header_explode);
			$header_value = trim(implode(":", $header_explode));
			return array("name" => $header_name, "value" => $header_value);
		} else {
			return array("name" => "SIP", "value" => $header_string);
		}
	}
}
?>
Return current item: floSIP Packet