-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSipContact.php
129 lines (113 loc) · 2.6 KB
/
SipContact.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
class SipContact
{
public $dispname;
public $scheme = 'sip';
public $username;
public $password;
public $domain; // 可包含 port
public $parameters = array();
function __construct($username='', $domain=''){
$this->username = $username;
$this->domain = $domain;
}
static function from_str($str){
$ret = new SipContact();
$ret->decode($str);
return $ret;
}
function equals($dst){
return ($this->username === $dst->username) && ($this->tag() == $dst->tag());
}
function address(){
return "{$this->username}@{$this->domain}";
}
function set_tag($tag){
$this->set_parameter('tag', $tag);
}
function del_tag(){
$this->del_parameter('tag');
}
function tag(){
return $this->get_parameter('tag');
}
function get_parameter($k){
return $this->parameters[$k];
}
function set_parameter($k, $v){
$this->parameters[$k] = $v;
}
function del_parameter($k){
unset($this->parameters[$k]);
}
function encode(){
$ret = '';
if($this->dispname){
$ret .= "\"{$this->dispname}\" ";
}
$ret .= "<{$this->scheme}:{$this->username}";
if(strlen($this->password) > 0){
$ret .= ":{$this->password}";
}
$ret .= "@{$this->domain}>";
foreach($this->parameters as $k=>$v){
$ret .= ";{$k}";
if(strlen($v)){
$ret .= "={$v}";
}
}
return $ret;
}
function decode($str){
$pos = strpos($str, '>');
if($pos === false){
$address = $str;
$parameters = '';
}else{
$address = substr($str, 0, $pos + 1);
$parameters = substr($str, $pos + 1);
}
$this->parse_address($address);
$ps = explode(';', $parameters);
foreach($ps as $p){
$p = trim($p);
if(strlen($p) == 0){
continue;
}
$kv = explode('=', $p);
$k = $kv[0];
$v = isset($kv[1])? $kv[1] : '';
$this->set_parameter($k, $v);
}
}
private function parse_address($str){
$pos = strpos($str, '<');
if(count($pos) !== false){
$this->dispname = trim(substr($str, 0, $pos), '" ');
$uri = substr($str, $pos);
}else{
$uri = $str;
}
$uri = trim($uri, '<>');
$a = new SipUri();
$a->decode($uri);
$this->scheme = $a->scheme;
$this->username = $a->username;
$this->password = $a->password;
$this->domain = $a->domain;
return;
// $ts = explode('@', $uri);
// if(count($ts) == 2){
// $this->domain = $ts[1]; // TODO: 可能包含 port 和 tag,如 '127.0.0.1:1234;ob'
// $ps = explode(':', $ts[0]);
// if(count($ps) == 2){
// $this->scheme = $ps[0];
// $this->username = $ps[1];
// }else if(count($ts) == 3){
// $this->scheme = $ps[0];
// $this->username = $ps[1];
// $this->password = $ps[2];
// }
// }
}
}