Example #1
0
function siemens_push_str($phone_ip, $postdata)
{
    $prov_host = gs_get_conf('GS_PROV_HOST');
    $data = "POST /server_push.html/ServerPush HTTP/1.1\r\n";
    $data .= "User-Agent: Gemeinschaft\r\n";
    $data .= "Host: {$phone_ip}:8085\r\n";
    $data .= "Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2\r\n";
    $data .= "Connection: keep-alive\r\n";
    $data .= "Content-Type: application/x-www-form-urlencoded\r\n";
    $data .= "Content-Length: " . strLen($postdata) . "\r\n\r\n";
    $data .= $postdata;
    $socket = @fSockOpen($phone_ip, 8085, $error_no, $error_str, 4);
    if (!$socket) {
        gs_log(GS_LOG_NOTICE, "Siemens: Failed to open socket - IP: {$phone_ip}");
        return 0;
    }
    stream_set_timeout($socket, 4);
    $bytes_written = (int) @fWrite($socket, $data, strLen($data));
    @fFlush($socket);
    $response = @fGetS($socket);
    @fClose($socket);
    if (strPos($response, '200') === false) {
        gs_log(GS_LOG_WARNING, "Siemens: Failed to push to phone {$phone_ip}");
        return 0;
    }
    gs_log(GS_LOG_DEBUG, "Siemens: Pushed {$bytes_written} bytes to phone {$phone_ip}");
    return $bytes_written;
}
Example #2
0
File: util.php Project: rkania/GS3
function gs_write_error($data)
{
    if (!defined('STDERR')) {
        define('STDERR', @fOpen('php://stderr', 'wb'));
    }
    if (php_sapi_name() === 'cli' && STDERR) {
        @fWrite(STDERR, $data, strLen($data));
        @fFlush(STDERR);
    } else {
        echo $data;
    }
}
Example #3
0
function gif_outputAsPng($gif, $lpszFileName, $bgColor = -1)
{
    if (!isset($gif) || @get_class($gif) != "cgif" || !$gif->loaded() || $lpszFileName == "") {
        return false;
    }
    $fd = $gif->getPng($bgColor);
    if (strlen($fd) <= 0) {
        return false;
    }
    if (!($fh = @fOpen($lpszFileName, "wb"))) {
        return false;
    }
    @fWrite($fh, $fd, strlen($fd));
    @fFlush($fh);
    @fClose($fh);
    return true;
}
Example #4
0
 public function ami_send_command($command)
 {
     @fWrite($this->_socket, $command, strLen($command));
     @fFlush($this->_socket);
     $data = array();
     while (!fEof($this->_socket)) {
         $tmp = @fRead($this->_socket, 8192);
         list($first, $last) = explode(':', $tmp);
         $data[trim($first)] = trim($last);
         if (@preg_match('/\\r\\n\\r\\n/S', $tmp)) {
             break;
         }
         usleep(1000);
         # sleep 0.001 secs
     }
     return $data;
 }
Example #5
0
 public function ami_send_command($command)
 {
     @fWrite($this->_socket, $command, strLen($command));
     @fFlush($this->_socket);
     $data = array();
     while (!fEof($this->_socket)) {
         $tmp = @fgets($this->_socket, 8192);
         if (strlen(trim($tmp)) <= 0) {
             break;
         }
         list($first, $last) = explode(':', $tmp);
         $data[trim($first)] = trim($last);
         usleep(1000);
         # sleep 0.001 secs
     }
     return $data;
 }
Example #6
0
function aastra_push_str($phone_ip, $xml)
{
    $prov_host = gs_get_conf('GS_PROV_HOST');
    //FIXME - call wget or something. this function should not block
    // for so long!
    // see _gs_prov_phone_checkcfg_by_ip_do_aastra() in
    // opt/gemeinschaft/inc/gs-fns/gs_prov_phone_checkcfg.php
    //$xml = utf8_decode($xml);
    if (subStr($xml, 0, 5) !== '<' . '?xml') {
        $xmlpi = '<' . '?xml version="1.0" encoding="UTF-8"?' . '>' . "\n";
    } else {
        $xmlpi = '';
    }
    $data = "POST / HTTP/1.1\r\n";
    $data .= "Host: {$phone_ip}\r\n";
    $data .= "Referer: {$prov_host}\r\n";
    $data .= "Connection: Close\r\n";
    $data .= "Content-Type: text/xml; charset=utf-8\r\n";
    $data .= "Content-Length: " . (strLen('xml=') + strLen($xmlpi) + strLen($xml)) . "\r\n";
    $data .= "\r\n";
    $data .= 'xml=' . $xmlpi . $xml;
    $socket = @fSockOpen($phone_ip, 80, $error_no, $error_str, 4);
    if (!$socket) {
        gs_log(GS_LOG_NOTICE, "Aastra: Failed to open socket - IP: {$phone_ip}");
        return 0;
    }
    stream_set_timeout($socket, 4);
    $bytes_written = (int) @fWrite($socket, $data, strLen($data));
    @fFlush($socket);
    $response = @fGetS($socket);
    @fClose($socket);
    if (strPos($response, '200') === false) {
        gs_log(GS_LOG_WARNING, "Aastra: Failed to push XML to phone {$phone_ip}");
        return 0;
    }
    gs_log(GS_LOG_DEBUG, "Aastra: Pushed {$bytes_written} bytes to phone {$phone_ip}");
    return $bytes_written;
}
function gs_extstate($host, $exts)
{
    static $hosts = array();
    if (!is_array($exts)) {
        $exts = array($exts);
        $return_single = true;
    } else {
        $return_single = false;
    }
    if (gs_get_conf('GS_INSTALLATION_TYPE_SINGLE')) {
        $host = '127.0.0.1';
    }
    if (!isset($hosts[$host])) {
        $hosts[$host] = array('sock' => null, 'lasttry' => 0);
    }
    if (!is_resource($hosts[$host]['sock'])) {
        if ($hosts[$host]['lasttry'] > time() - 60) {
            # we have tried less than a minute ago
            $hosts[$host]['lasttry'] = time();
            return $return_single ? AST_MGR_EXT_UNKNOWN : array();
        }
        $hosts[$host]['lasttry'] = time();
        $sock = @fSockOpen($host, 5038, $err, $errMsg, 2);
        if (!is_resource($sock)) {
            gs_log(GS_LOG_WARNING, 'Connection to AMI on ' . $host . ' failed');
            return $return_single ? AST_MGR_EXT_UNKNOWN : array();
        }
        $data = _sock_read($sock, 3, '/[\\r\\n]/');
        if (!preg_match('/^Asterisk [^\\/]+\\/(\\d(?:\\.\\d)?)/mis', $data, $m)) {
            gs_log(GS_LOG_WARNING, 'Incompatible Asterisk manager interface on ' . $host);
            $m = array(1 => '0.0');
        } else {
            if ($m[1] > '1.1') {
                # Asterisk 1.4: manager 1.0
                # Asterisk 1.6: manager 1.1
                gs_log(GS_LOG_NOTICE, 'Asterisk manager interface on ' . $host . ' speaks a new protocol version (' . $m[1] . ')');
                # let's try anyway and hope to understand it
            }
        }
        $hosts[$host]['sock'] = $sock;
        $req = "Action: Login\r\n" . "Username: "******"gscc" . "\r\n" . "Secret: " . "gspass" . "\r\n" . "Events: off\r\n" . "\r\n";
        @fWrite($sock, $req, strLen($req));
        @fFlush($sock);
        $data = _sock_read($sock, 3, '/\\r\\n\\r\\n/S');
        if ($data === false) {
            gs_log(GS_LOG_WARNING, 'Authentication to AMI on ' . $host . ' failed (timeout)');
            $hosts[$host]['sock'] = null;
            return $return_single ? AST_MGR_EXT_UNKNOWN : array();
        } elseif (!preg_match('/Authentication accepted/i', $data)) {
            gs_log(GS_LOG_WARNING, 'Authentication to AMI on ' . $host . ' failed');
            $hosts[$host]['sock'] = null;
            return $return_single ? AST_MGR_EXT_UNKNOWN : array();
        }
    } else {
        $sock = $hosts[$host]['sock'];
    }
    $states = array();
    foreach ($exts as $ext) {
        $req = "Action: ExtensionState\r\n" . "Context: to-internal-users\r\n" . "Exten: " . $ext . "\r\n" . "\r\n";
        @fWrite($sock, $req, strLen($req));
        @fFlush($sock);
        $resp = trim(_sock_read($sock, 3, '/\\r\\n\\r\\n/S'));
        //echo "\n$resp\n\n";
        $states[$ext] = AST_MGR_EXT_UNKNOWN;
        if (!preg_match('/^Response:\\s*Success/is', $resp)) {
            continue;
        }
        if (!preg_match('/^Exten:\\s*([\\da-z]+)/mis', $resp, $m)) {
            continue;
        }
        $resp_ext = $m[1];
        if (!preg_match('/^Status:\\s*(-?\\d+)/mis', $resp, $m)) {
            continue;
        }
        $resp_state = (int) $m[1];
        $states[$resp_ext] = $resp_state;
    }
    if ($return_single) {
        return array_key_exists($exts[0], $states) ? $states[$exts[0]] : AST_MGR_EXT_UNKNOWN;
    } else {
        return $states;
    }
}
Example #8
0
function gs_queue_status($host, $ext, $getMembers, $getCallers)
{
    static $hosts = array();
    if (gs_get_conf('GS_INSTALLATION_TYPE_SINGLE')) {
        $host = '127.0.0.1';
    }
    if (!isset($hosts[$host])) {
        $hosts[$host] = array('sock' => null, 'lasttry' => 0);
    }
    if (!is_resource($hosts[$host]['sock'])) {
        if ($hosts[$host]['lasttry'] > time() - 60) {
            # we have tried less than a minute ago
            $hosts[$host]['lasttry'] = time();
            return false;
        }
        $hosts[$host]['lasttry'] = time();
        $sock = @fSockOpen($host, 5038, $err, $errMsg, 2);
        if (!is_resource($sock)) {
            gs_log(GS_LOG_WARNING, 'Connection to AMI on ' . $host . ' failed');
            return false;
        }
        $data = _sock_read($sock, 3, '/[\\r\\n]/');
        if (!preg_match('/^Asterisk [^\\/]+\\/(\\d(?:\\.\\d)?)/mis', $data, $m)) {
            gs_log(GS_LOG_WARNING, 'Incompatible Asterisk manager interface on ' . $host);
            $m = array(1 => '0.0');
        } else {
            if ($m[1] > '1.1') {
                # Asterisk 1.4: manager 1.0
                # Asterisk 1.6: manager 1.1
                gs_log(GS_LOG_NOTICE, 'Asterisk manager interface on ' . $host . ' speaks a new protocol version (' . $m[1] . ')');
                # let's try anyway and hope to understand it
            }
        }
        $hosts[$host]['sock'] = $sock;
        $req = "Action: Login\r\n" . "Username: "******"gscc" . "\r\n" . "Secret: " . "gspass" . "\r\n" . "Events: off\r\n" . "\r\n";
        @fWrite($sock, $req, strLen($req));
        @fFlush($sock);
        $data = _sock_read2($sock, 5, '/\\r\\n\\r\\n/S');
        if (!preg_match('/Authentication accepted/i', $data)) {
            gs_log(GS_LOG_WARNING, 'Authentication to AMI on ' . $host . ' failed');
            $hosts[$host]['sock'] = null;
            return false;
        }
    } else {
        $sock = $hosts[$host]['sock'];
    }
    $queue_stats = array('maxlen' => null, 'calls' => null, 'holdtime' => null, 'completed' => null, 'abandoned' => null, 'sl' => null, 'slp' => null);
    if ($getMembers) {
        $queue_stats['members'] = array();
    }
    if ($getCallers) {
        $queue_stats['callers'] = array();
    }
    $default_member = array('dynamic' => null, 'calls' => null, 'lastcall' => null, 'devstate' => null, 'paused' => null);
    $default_caller = array('channel' => null, 'cidnum' => null, 'cidname' => null, 'wait' => null);
    $req = "Action: QueueStatus\r\n" . "Queue: " . $ext . "\r\n" . "\r\n";
    @fWrite($sock, $req, strLen($req));
    @fFlush($sock);
    $resp = trim(_sock_read2($sock, 2, '/Event:\\s*QueueStatusComplete\\r\\n\\r\\n/i'));
    //echo "\n$resp\n\n";
    if (!preg_match('/^Response:\\s*Success/is', $resp)) {
        return false;
    }
    $resp = preg_split('/\\r\\n\\r\\n/S', $resp);
    /*
    echo "<pre>";
    print_r($resp);
    echo "</pre>";
    */
    $manager_ok = false;
    foreach ($resp as $pkt) {
        $pkt = lTrim($pkt);
        if (preg_match('/^Event:\\s*QueueParams/is', $pkt)) {
            if (!preg_match('/^Queue:\\s*' . $ext . '/mis', $pkt)) {
                continue;
            }
            //echo $pkt, "\n\n";
            if (preg_match('/^Max:\\s*(\\d+)/mis', $pkt, $m)) {
                $queue_stats['maxlen'] = (int) $m[1] > 0 ? (int) $m[1] : null;
            }
            if (preg_match('/^Calls:\\s*(\\d+)/mis', $pkt, $m)) {
                $queue_stats['calls'] = (int) $m[1];
            }
            if (preg_match('/^Holdtime:\\s*(\\d+)/mis', $pkt, $m)) {
                $queue_stats['holdtime'] = (int) $m[1];
            }
            if (preg_match('/^Completed:\\s*(\\d+)/mis', $pkt, $m)) {
                $queue_stats['completed'] = (int) $m[1];
            }
            if (preg_match('/^Abandoned:\\s*(\\d+)/mis', $pkt, $m)) {
                $queue_stats['abandoned'] = (int) $m[1];
            }
            if (preg_match('/^ServiceLevel:\\s*(\\d+)/mis', $pkt, $m)) {
                $queue_stats['sl'] = (int) $m[1];
            }
            if (preg_match('/^ServiceLevelPerf:\\s*(\\d+?(\\.\\d+)?)/mis', $pkt, $m)) {
                $queue_stats['slp'] = (double) $m[1];
            }
            $manager_ok = true;
        } elseif ($getMembers && preg_match('/^Event:\\s*QueueMember/is', $pkt)) {
            if (!preg_match('/^Queue:\\s*' . $ext . '/mis', $pkt)) {
                continue;
            }
            if (!preg_match('/^Location:\\s*([A-Z\\d\\/]+)/mis', $pkt, $m)) {
                continue;
            }
            $loc = $m[1];
            $queue_stats['members'][$loc] = $default_member;
            //echo $pkt, "\n\n";
            if (preg_match('/^Membership:\\s*([a-z]+)/mis', $pkt, $m)) {
                $queue_stats['members'][$loc]['dynamic'] = $m[1] != 'static';
            }
            if (preg_match('/^CallsTaken:\\s*(\\d+)/mis', $pkt, $m)) {
                $queue_stats['members'][$loc]['calls'] = (int) $m[1];
            }
            if (preg_match('/^LastCall:\\s*(\\d+)/mis', $pkt, $m)) {
                $queue_stats['members'][$loc]['lastcall'] = (int) $m[1];
            }
            if (preg_match('/^Status:\\s*(\\d+)/mis', $pkt, $m)) {
                $queue_stats['members'][$loc]['devstate'] = (int) $m[1];
            }
            if (preg_match('/^Paused:\\s*(\\d+)/mis', $pkt, $m)) {
                $queue_stats['members'][$loc]['paused'] = (int) $m[1] > 0;
            }
        } elseif ($getCallers && preg_match('/^Event:\\s*QueueEntry/is', $pkt)) {
            if (!preg_match('/^Queue:\\s*' . $ext . '/mis', $pkt)) {
                continue;
            }
            if (!preg_match('/^Position:\\s*(\\d+)/mis', $pkt, $m)) {
                continue;
            }
            $pos = (int) $m[1];
            $queue_stats['callers'][$pos] = $default_caller;
            //echo $pkt, "\n\n";
            if (preg_match('/^Channel:\\s*([^\\n\\r]+)/mis', $pkt, $m)) {
                $queue_stats['callers'][$pos]['dynamic'] = trim($m[1]);
            }
            if (preg_match('/^CallerID:\\s*([^\\n\\r]+)/mis', $pkt, $m)) {
                $queue_stats['callers'][$pos]['cidnum'] = strToLower(trim($m[1])) != 'unknown' ? trim($m[1]) : null;
            }
            if (preg_match('/^CallerIDName:\\s*([^\\n\\r]+)/mis', $pkt, $m)) {
                $queue_stats['callers'][$pos]['cidname'] = strToLower(trim($m[1])) != 'unknown' ? trim($m[1]) : null;
            }
            if (preg_match('/^Wait:\\s*(\\d+)/mis', $pkt, $m)) {
                $queue_stats['callers'][$pos]['wait'] = (int) $m[1];
            }
        }
    }
    if (!$manager_ok && $getMembers) {
        # failed to get information about the queue from the manager
        # interface. this happens after a reload of Asterisk when
        # no call has entered the queue using Queue() yet
        $queue_stats['calls'] = 0;
        $queue_stats['completed'] = 0;
        $queue_stats['abandoned'] = 0;
        $queue_stats['holdtime'] = 0;
        include_once GS_DIR . 'inc/db_connect.php';
        $db = @gs_db_slave_connect();
        if (!$db) {
            return $queue_stats;
        }
        $maxlen = (int) $db->executeGetOne('SELECT `maxlen` FROM `ast_queues` WHERE `name`=\'' . $db->escape($ext) . '\'');
        $queue_stats['maxlen'] = $maxlen > 0 ? $maxlen : null;
        $rs = $db->execute('SELECT `interface` FROM `ast_queue_members` WHERE `queue_name`=\'' . $db->escape($ext) . '\'');
        $queue_members = array();
        while ($r = $rs->fetchRow()) {
            if (strToUpper(subStr($r['interface'], 0, 4)) == 'SIP/') {
                $queue_members[] = subStr($r['interface'], 4);
            } else {
                $queue_members[] = $r['interface'];
            }
        }
        if (count($queue_members) < 1) {
            return $queue_stats;
        }
        foreach ($queue_members as $queue_member) {
            $queue_stats['members']['SIP/' . $queue_member] = $default_member;
        }
        $ext_states = @gs_extstate($host, $queue_members);
        if (!is_array($ext_states)) {
            return $queue_stats;
        }
        foreach ($queue_members as $queue_member) {
            $queue_stats['members']['SIP/' . $queue_member]['devstate'] = extstate_to_devstate(@$ext_states[$queue_member]);
        }
    }
    /*
    echo "<pre>";
    print_r($queue_stats);
    echo "</pre>";
    */
    return $queue_stats;
}
Example #9
0
function gs_agi_do($cmd)
{
    global $agi_basename;
    $fail = array('code' => 500, 'result' => -1, 'data' => '');
    $response = array('code' => null, 'result' => null, 'data' => null);
    $cmd = trim($cmd);
    if ($cmd === '') {
        trigger_error("Empty AGI command.", E_USER_NOTICE);
        return $fail;
    }
    if (@fWrite(STDOUT, $cmd . "\n", strLen($cmd) + 1) === false) {
        trigger_error("AGI command \"{$cmd}\" failed! Could not write to StdOut.", E_USER_WARNING);
        return $fail;
    }
    gs_agi_log(GS_LOG_DEBUG, 'Tx << ' . $cmd);
    /*
    if (! in_array(php_sapi_name(), array('cgi'), true)) {
    	# the correct way
    */
    if (!@fFlush(STDOUT)) {
        gs_agi_log(GS_LOG_WARNING, 'Failed to flush StdOut!');
        gs_log(GS_LOG_WARNING, "Failed to flush StdOut in AGI script {$agi_basename}!");
        @ob_flush();
        @flush();
        uSleep(1000);
    }
    /*
    } else {
    	# STDOUT is not defined in the "cgi" version of PHP.
    	# However, the RedHat/Centos way of running shell scripts in
    	# php-cgi instead of php-cli is simply wrong.
    	uSleep(10);
    }
    */
    stream_set_blocking(STDIN, true);
    $count = 0;
    $str = '';
    do {
        uSleep(1000);
        stream_set_timeout(STDIN, 1);
        $str .= trim(fGetS(STDIN, 4096));
    } while ($str == '' && $count++ < 5);
    gs_agi_log(GS_LOG_DEBUG, 'Rx >> ' . $str);
    if ($count >= 5) {
        trigger_error("AGI command \"{$cmd}\" failed! Could not read response.", E_USER_WARNING);
        return $fail;
    }
    $response['code'] = subStr($str, 0, 3);
    $str = trim(subStr($str, 3));
    if (subStr($str, 0, 1) === '-') {
        # multi-line response
        $count = 0;
        $str = subStr($str, 1) . "\n";
        $line = fGetS(STDIN, 4096);
        gs_agi_log(GS_LOG_DEBUG, 'Rx >> ' . $line);
        while (subStr($line, 0, 3) !== $response['code'] && $count < 5) {
            $str .= $line;
            $line = fGetS(STDIN, 4096);
            gs_agi_log(GS_LOG_DEBUG, 'Rx >> ' . $line);
            $count = trim($line) == '' ? $count + 1 : 0;
        }
        if ($count >= 5) {
            trigger_error("AGI command \"{$cmd}\" failed! Could not read multi-line response.", E_USER_WARNING);
            return $fail;
        }
    }
    $response['result'] = null;
    $response['data'] = '';
    if ($response['code'] !== '200') {
        $response['data'] = $str;
    } else {
        $parse = explode(' ', trim($str));
        $in_token = false;
        foreach ($parse as $token) {
            if ($in_token) {
                $response['data'] .= ' ' . trim($token, '() ');
                if (subStr($token, strLen($token) - 1, 1) === ')') {
                    $in_token = false;
                }
            } elseif (subStr($token, 0, 1) === '(') {
                if (subStr($token, strLen($token) - 1, 1) !== ')') {
                    $in_token = true;
                }
                $response['data'] .= ' ' . trim($token, '() ');
            } elseif (strPos($token, '=') !== false) {
                $token = explode('=', $token);
                $response[$token[0]] = $token[1];
            } elseif ($token != '') {
                $response['data'] .= ' ' . $token;
            }
        }
        $response['data'] = trim($response['data']);
    }
    return $response;
}
Example #10
0
function _fake_agi_send($fd, $line)
{
    $bytes = fWrite($fd, $line . "\n", strLen($line) + 1);
    fFlush($fd);
    return $bytes;
}