summaryrefslogtreecommitdiff
path: root/fs_selfservice/fri/includes/asi.php
blob: 62f221e2f75a73a34760299862461266d0d46d76 (plain)
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php

/**
 * @file
 * Asterisk manager interface for access to asterisk api (astdb)
 */

/**
  * Asterisk Manager Interface
  */
class AsteriskManagerInterface {

  var $socket;

  /**
   * constructor 
   */
  function AsteriskManagerInterface() {
  }

  /*
   * Reloads Asterisk Configuration
   *
   * @param $username
   *   asterisk manager interface username
   * @param $password
   *   asterisk manager interface password 
   */
  function connect($host,$username,$password) {

    // connect
    $fp = fsockopen($host, 5038, $errno, $errstr, 10);
    if (!$fp) {
      return FALSE;
    } 
    else {
      $buffer='';
      if(version_compare(phpversion(), '4.3', '>=')) {
        stream_set_timeout($fp, 5);
      } 
      else {
        socket_set_timeout($fp, 5);
      }
      $buffer = fgets($fp);
      if (!preg_match('/Asterisk Call Manager/i', $buffer)) {
        $_SESSION['ari_error'] = _("Asterisk Call Manager not responding") . "<br />\n";
        return FALSE;
      }
      else {
        $out="Action: Login\r\nUsername: ".$username."\r\nSecret: ".$password."\r\n\r\n";
        fwrite($fp,$out);
        $buffer=fgets($fp);
        if ($buffer!="Response: Success\r\n") {
          $_SESSION['ari_error'] =  _("Asterisk authentication failed:") . "<br />" . $buffer . "<br />\n";
          return FALSE;
        }
        else {
          $buffers=fgets($fp); // get rid of Message: Authentication accepted

          // connected
          $this->socket = $fp;
        }
      }
    }
    return TRUE;
  }

  /*
   * Reloads Asterisk Configuration
   */
  function disconnect() {

    if ($this->socket) {
      fclose($this->socket);
    }
  }

  /*
   * Reloads Asterisk Configuration
   *
   * @param $command
   *   Command to be sent to the asterisk manager interface 
   * @return $ret
   *   response from asterisk manager interface 
   */
  function command($command) {

    $response = '';

    fwrite($this->socket,$command);

    $count = 0;
    while (($buffer = fgets($this->socket)) && (!preg_match('/Response: Follows/i', $buffer))) {

      if ($count>100) {
        $_SESSION['ari_error'] =  _("Asterisk command not understood") . "<br />" . $buffer . "<br />\n";
        return FALSE;
      }
      $count++;
    }

    $count = 0;
    while (($buffer = fgets($this->socket)) && (!preg_match('/END COMMAND/i', $buffer))) {

      if (preg_match('/Value/',$buffer)) {
        $parts = split(' ',trim($buffer));
        $response = $parts[1];
      }

      if ($count>100) {
        $_SESSION['ari_error'] =  _("Asterisk command not understood") . "<br />" . $buffer . "<br />\n";
        return;
      }
      $count++;
    }

    return $response;
  }

  function command2($command) {

    $response = '';

    fwrite($this->socket,$command);

    $count = 0;
    while (($buffer = fgets($this->socket)) && (!preg_match('/Response: Follows/i', $buffer))) {

      if ($count>100) {
        $_SESSION['ari_error'] =  _("Asterisk command not understood") . "<br />" . $buffer . "<br />\n";
        return FALSE;
      }
      $count++;
    }

    $count = 0;
    while (($buffer = fgets($this->socket)) && (!preg_match('/END COMMAND/i', $buffer))) {

      if (preg_match('/Value:/',$buffer)) {
        $parts = split('Value:',trim($buffer));
        $response = $parts[1];
      }
      if ($count>100) {
        $_SESSION['ari_error'] =  _("Asterisk command not understood") . "<br />" . $buffer . "<br />\n";
        return;
      }
      $count++;
    }

    return $response;
  }

}  


?>