root/contributed_modules/modules/extcfg/astman.inc

Revision 11299, 8.5 kB (checked in by pnlarsson, 1 year ago)

Added instead of hardcoded settings. Added some images, re #4688

Line 
1 <?php
2 class AstMan {
3   var $socket = false;
4   var $error = "";
5   var $_events = false;
6  
7   function AstMan($events = false){
8     if ($events == true)
9       $this->_events = true;   
10   }
11
12   function Login($host="localhost", $username="admin", $password="amp111"){
13     $this->socket = @fsockopen($host, "5038", $errno, $errstr, 1);
14     if (!$this->socket) {
15       $this->error =  "Could not connect - $errstr ($errno)";
16       return FALSE;
17     }else{
18       stream_set_timeout($this->socket, 3);
19      
20       if ($this->_events)
21         $query = "Action: Login\r\nUserName: $username\r\nSecret: $password\r\n\r\n";
22       else
23         $query = "Action: Login\r\nUserName: $username\r\nSecret: $password\r\nEvents: off\r\n\r\n";
24      
25       $wrets = $this->Query($query);
26  
27       if (strpos($wrets, "Message: Authentication accepted") != FALSE){
28         return true;
29       }else{
30         $this->error = "Could not login - Authentication failed";
31         fclose($this->socket);
32         $this->socket = FALSE;
33         return FALSE;
34       }
35     }
36   }
37  
38   function Logout(){
39     $wrets = "";
40     if ($this->socket){
41       fputs($this->socket, "Action: Logoff\r\n\r\n");
42       while (!feof($this->socket)) {
43         $wrets .= fread($this->socket, 8192);
44       }
45       fclose($this->socket);
46       $this->socket = "FALSE";
47     }
48     return;
49   }
50  
51   function Query($query){
52     $wrets = "";
53    
54     if ($this->socket === FALSE)
55       return FALSE;
56      
57     fputs($this->socket, $query);
58     do
59     {
60       $line = fgets($this->socket, 4096);
61       $wrets .= $line;
62       $info = stream_get_meta_data($this->socket);
63     }while ($line != "\r\n" && $info['timed_out'] == false );
64     return $wrets;
65   }
66  
67   function GetError(){
68     return $this->error;
69   }
70  
71   function GetDB($family, $key){
72     $value = "";
73  
74     $wrets = $this->Query("Action: Command\r\nCommand: database get $family $key\r\n\r\n");
75  
76     if ($wrets){
77       $value_start = strpos($wrets, "Value: ") + 7;
78       $value_stop = strpos($wrets, "\n", $value_start);
79       if ($value_start > 8){
80         $value = substr($wrets, $value_start, $value_stop - $value_start);
81       }
82     }
83     return $value;
84   }
85  
86   function PutDB($family, $key, $value){
87     $wrets = $this->Query("Action: Command\r\nCommand: database put $family $key $value\r\n\r\n");
88  
89     if (strpos($wrets, "Updated database successfully") != FALSE){
90       return TRUE;
91     }
92     $this->error =  "Could not updated database";
93     return FALSE;
94   }
95  
96   function DelDB($family, $key){
97     $wrets = $this->Query("Action: Command\r\nCommand: database del $family $key\r\n\r\n");
98
99     if (strpos($wrets, "Database entry removed.") != FALSE){
100       return TRUE;
101     }
102     $this->error =  "Database entry does not exist";
103     return FALSE;
104   }
105    
106   function GetFamilyDB($family){
107     $wrets = $this->Query("Action: Command\r\nCommand: database show $family\r\n\r\n");
108     if ($wrets){
109       $value_start = strpos($wrets, "Response: Follows\r\n") + 19;
110       $value_stop = strpos($wrets, "--END COMMAND--\r\n", $value_start);
111       if ($value_start > 18){
112         $wrets = substr($wrets, $value_start, $value_stop - $value_start);
113       }
114       $lines = explode("\n", $wrets);
115       foreach($lines as $line){
116         if (strlen($line) > 4){
117           $value_start = strpos($line, ": ") + 2;
118           $value_stop = strpos($line, " ", $value_start);
119           $key = trim(substr($line, strlen($family) + 2, strpos($line, " ") - strlen($family) + 2));     
120           $value[$key] = trim(substr($line, $value_start));
121         }
122       }
123       return $value;
124     }
125     return FALSE;
126   }   
127  
128   function GetChannel($account, $callerid){
129     $account_s = "Account: " . $account;
130     $callerid_s = "CallerID: " . $callerid;
131     $value_stop = 0;
132    
133     $wrets = $this->Query2("Action: Status\r\n\r\n", "Event: StatusComplete\r\n");
134     if ($wrets){
135       $w_len = strlen($wrets);
136       while ($value_stop <= $w_len){
137         $value_start = strpos($wrets, "Event: Status", $value_stop);
138         $value_stop = strpos($wrets, "Event:", $value_start + 6);
139         if (($value_stop - $value_start) > 0){
140           $status = substr($wrets, $value_start, $value_stop - $value_start);
141           if (strpos($status, $account_s) !== false){
142             if (strpos($status, $callerid_s) !== false){
143               $channel_start = strpos($status, "Channel: ") + 9;
144               $channel_slut = strpos($status, "\r\n", $channel_start);
145               $channel = substr($status, $channel_start, $channel_slut - $channel_start);
146               return $channel;
147             }
148           }
149         }else{
150           return FALSE;
151         }
152       }
153     }
154     return FALSE;   
155   }
156
157   function GetAllChannel($account, $callerid, $cid_name){
158     $account_s = "Account: " . $account;
159     $callerid_s = "CallerID: " . $callerid;  // Not in use
160     $cid_name_s = "CallerIDName: " . $cid_name;
161     $value_stop = 0;
162     $res = array();
163    
164     $wrets = $this->Query2("Action: Status\r\n\r\n", "Event: StatusComplete\r\n");
165     if ($wrets){
166       $w_len = strlen($wrets);
167       while ($value_stop <= $w_len){
168         $value_start = strpos($wrets, "Event: Status", $value_stop);
169         $value_stop = strpos($wrets, "Event:", $value_start + 6);
170         if (($value_stop - $value_start) > 0){
171           $status = substr($wrets, $value_start, $value_stop - $value_start);
172           if (strpos($status, $account_s) !== false || strpos($status, $cid_name_s) !== false){
173             $channel_start = strpos($status, "Channel: ") + 9;
174             $channel_slut = strpos($status, "\r\n", $channel_start);
175             $channel = substr($status, $channel_start, $channel_slut - $channel_start);
176             $res[] = $channel;
177           }
178         }else{
179           return $res;
180         }
181       }
182     }
183     return $res;   
184   }
185    
186   function Redirect($channel, $exten, $context = 'from-internal', $priority = 1){
187     $wrets = $this->Query2("Action: Redirect\r\nChannel: $channel\r\nExten: $exten\r\nContext: $context\r\nPriority: $priority\r\n\r\n", "Message: Redirect successful\r\n");
188     if (strpos('Redirect successful', $wrets) !== false)
189       return true;
190     else
191       return false;
192   }
193  
194   function GetChannelActionID($actionid){
195     $actionid_s = "ActionID: " . $actionid;
196    
197     $value_stop = 0;
198    
199     $wrets = $this->Query2("Action: Status\r\n$actionid_s\r\n\r\n", "Event: StatusComplete\r\n");
200     if ($wrets){
201       $w_len = strlen($wrets);
202       $value_start = strpos($wrets, "Event: Status");
203       $value_stop = strpos($wrets, "Event:", $value_start + 6);
204       if (($value_stop - $value_start) > 6){
205         $status = substr($wrets, $value_start, $value_stop - $value_start);
206         if (strpos($status, $actionid_s) !== false){
207           $channel_start = strpos($status, "Channel: ") + 9;
208           $channel_slut = strpos($status, "\r\n", $channel_start);
209           $channel = substr($status, $channel_start, $channel_slut - $channel_start);
210           return $channel;
211         }
212       }else{
213         return FALSE;
214       }
215     }
216     return FALSE;       
217   }
218  
219   function Hangup($channel){
220     $wrets = $this->Query("Action: Hangup\r\nChannel: $channel\r\n\r\n");
221
222     if (strpos($wrets, "Message: Channel Hungup") != FALSE){
223       return TRUE;
224     }
225     $this->error =  "Could not hangup $channel";
226     return FALSE;
227   }
228  
229   function Query2($query, $end_str){
230     $wrets = "";
231    
232     if ($this->socket === FALSE)
233       return FALSE;
234      
235     fputs($this->socket, $query);
236     do{
237       $line = fgets($this->socket, 4096);
238       $wrets .= $line;
239       $info = stream_get_meta_data($this->socket);
240     }while ($line != $end_str && $info['timed_out'] == false );
241     return $wrets;
242   }
243  
244   function ExtensionState($exten, $context = 'ext-local'){
245 /*
246 Action: ExtensionState
247 Context: ext-local
248 Exten: 8546
249
250 Response: Success
251 Message: Extension Status
252 Exten: 8546
253 Context: ext-local
254 Hint: SIP/8546
255 Status: 0
256
257 */
258     $wrets = $this->Query("Action: ExtensionState\r\nContext: $context\r\nExten: $exten\r\n\r\n");
259     $status = (int) trim(substr($wrets, strpos($wrets, 'Status: ') + 8));
260     return $status;
261   }
262  
263   function get_my_stuff($str, $start_str, $slut_str = ""){
264     if ($start_str == false){
265       $start = 0;
266     }else{
267       $start = strpos($str, $start_str);
268       if ($start === false)
269         return "";
270       $start += strlen($start_str);
271     }
272     if ($slut_str == ""){
273       $slut = strlen($str);
274     }else{
275       $slut = strpos($str, $slut_str, $start); 
276     }
277     $res = trim(substr($str, $start, $slut - $start));
278     return $res;
279   }
280 }
Note: See TracBrowser for help on using the browser.