import torrus 1.0.9
[freeside.git] / fs_selfservice / fri / includes / asi.php
1 <?php
2
3 /**
4  * @file
5  * Asterisk manager interface for access to asterisk api (astdb)
6  */
7
8 /**
9   * Asterisk Manager Interface
10   */
11 class AsteriskManagerInterface {
12
13   var $socket;
14
15   /**
16    * constructor 
17    */
18   function AsteriskManagerInterface() {
19   }
20
21   /*
22    * Reloads Asterisk Configuration
23    *
24    * @param $username
25    *   asterisk manager interface username
26    * @param $password
27    *   asterisk manager interface password 
28    */
29   function connect($host,$username,$password) {
30
31     // connect
32     $fp = fsockopen($host, 5038, $errno, $errstr, 10);
33     if (!$fp) {
34       return FALSE;
35     } 
36     else {
37       $buffer='';
38       if(version_compare(phpversion(), '4.3', '>=')) {
39         stream_set_timeout($fp, 5);
40       } 
41       else {
42         socket_set_timeout($fp, 5);
43       }
44       $buffer = fgets($fp);
45       if (!preg_match('/Asterisk Call Manager/i', $buffer)) {
46         $_SESSION['ari_error'] = _("Asterisk Call Manager not responding") . "<br />\n";
47         return FALSE;
48       }
49       else {
50         $out="Action: Login\r\nUsername: ".$username."\r\nSecret: ".$password."\r\n\r\n";
51         fwrite($fp,$out);
52         $buffer=fgets($fp);
53         if ($buffer!="Response: Success\r\n") {
54           $_SESSION['ari_error'] =  _("Asterisk authentication failed:") . "<br />" . $buffer . "<br />\n";
55           return FALSE;
56         }
57         else {
58           $buffers=fgets($fp); // get rid of Message: Authentication accepted
59
60           // connected
61           $this->socket = $fp;
62         }
63       }
64     }
65     return TRUE;
66   }
67
68   /*
69    * Reloads Asterisk Configuration
70    */
71   function disconnect() {
72
73     if ($this->socket) {
74       fclose($this->socket);
75     }
76   }
77
78   /*
79    * Reloads Asterisk Configuration
80    *
81    * @param $command
82    *   Command to be sent to the asterisk manager interface 
83    * @return $ret
84    *   response from asterisk manager interface 
85    */
86   function command($command) {
87
88     $response = '';
89
90     fwrite($this->socket,$command);
91
92     $count = 0;
93     while (($buffer = fgets($this->socket)) && (!preg_match('/Response: Follows/i', $buffer))) {
94
95       if ($count>100) {
96         $_SESSION['ari_error'] =  _("Asterisk command not understood") . "<br />" . $buffer . "<br />\n";
97         return FALSE;
98       }
99       $count++;
100     }
101
102     $count = 0;
103     while (($buffer = fgets($this->socket)) && (!preg_match('/END COMMAND/i', $buffer))) {
104
105       if (preg_match('/Value/',$buffer)) {
106         $parts = split(' ',trim($buffer));
107         $response = $parts[1];
108       }
109
110       if ($count>100) {
111         $_SESSION['ari_error'] =  _("Asterisk command not understood") . "<br />" . $buffer . "<br />\n";
112         return;
113       }
114       $count++;
115     }
116
117     return $response;
118   }
119
120   function command2($command) {
121
122     $response = '';
123
124     fwrite($this->socket,$command);
125
126     $count = 0;
127     while (($buffer = fgets($this->socket)) && (!preg_match('/Response: Follows/i', $buffer))) {
128
129       if ($count>100) {
130         $_SESSION['ari_error'] =  _("Asterisk command not understood") . "<br />" . $buffer . "<br />\n";
131         return FALSE;
132       }
133       $count++;
134     }
135
136     $count = 0;
137     while (($buffer = fgets($this->socket)) && (!preg_match('/END COMMAND/i', $buffer))) {
138
139       if (preg_match('/Value:/',$buffer)) {
140         $parts = split('Value:',trim($buffer));
141         $response = $parts[1];
142       }
143       if ($count>100) {
144         $_SESSION['ari_error'] =  _("Asterisk command not understood") . "<br />" . $buffer . "<br />\n";
145         return;
146       }
147       $count++;
148     }
149
150     return $response;
151   }
152
153 }  
154
155
156 ?>