Ejemplo n.º 1
0
 function checkExtensionStatus($curid, $type = 'list', $curhover)
 {
     global $db, $config;
     /* 
     	if type is list, then only check some specific extension
     	or else we get extension list from events
     */
     $panellist = array();
     $panelphones = array();
     if ($type == 'list') {
         $i = 0;
         foreach ($_SESSION['curuser']['extensions'] as $value) {
             $row = astercrm::getRecordByField('username', $value, 'astercrm_account');
             $panellist[$row['username']]['extension'] = $row['extension'];
             $panellist[$row['username']]['agent'] = $row['agent'];
             $panellist[$row['username']]['channel'] = $row['channel'];
             $panelphones[] = $row['extension'];
             $i++;
         }
         //$_SESSION['curuser']['extensions_session'] = $panellist;
     } else {
         $alluser = astercrm::getall('astercrm_account');
         while ($alluser->fetchinto($row)) {
             $panellist[$row['username']]['extension'] = $row['extension'];
             $panellist[$row['username']]['agent'] = $row['agent'];
             $panellist[$row['username']]['channel'] = $row['channel'];
             $panelphones[] = $row['extension'];
         }
     }
     if (!isset($_SESSION['extension_status'])) {
         $status = array();
         $callerid = array();
         $direction = array();
     } else {
         /*
         because there could be no all extension status data in events
         we need to inherit status from sessions
         */
         $status = $_SESSION['extension_status'];
         $callerid = $_SESSION['callerid'];
         $direction = $_SESSION['direction'];
         $srcchan = $_SESSION['srcchan'];
         $dstchan = $_SESSION['dstchan'];
     }
     if (!isset($panelphones) or $panelphones == '') {
         $panelphones = array();
     }
     if ($config['system']['eventtype'] == 'curcdr') {
         //read all peer status in table peerstatus and save to array $phone_status
         $events =& asterEvent::getPeerstatus(0);
         $phone_status = array();
         while ($events->fetchInto($list)) {
             list($tech, $peer) = split('/', $list['peername']);
             $phone_status[$peer] = $list['status'];
         }
         foreach ($panellist as $username => $phone) {
             $query = "SELECT * FROM curcdr WHERE (src = '" . $phone['extension'] . "' OR dst = '" . $phone['extension'] . "' OR srcchan = 'agent/" . $phone['agent'] . "' OR dstchan = 'agent/" . $phone['agent'] . "' OR srcchan LIKE '" . $phone['channel'] . "-%' OR dstchan LIKE '" . $phone['channel'] . "-%') AND dstchan != '' AND srcchan != '' AND dst != '' AND src != '' ORDER BY id ASC";
             $res = $db->query($query);
             if ($res->fetchInto($cdrrow)) {
                 if ($status[$username] == 1) {
                     continue;
                 }
                 //for check click to transfer
                 if (!strstr($cdrrow['dstchan'], $cdrrow['dst'])) {
                     $dst_tmp = trim($cdrrow['dst']);
                     $cdrrow['dst'] = '';
                 }
                 if ($status[$list['peer']] == 1) {
                     continue;
                 }
                 if (strstr($cdrrow['src'], $phone['extension']) or strstr($cdrrow['srcchan'], $phone['channel']) or $cdrrow['srcchan'] == "agent/" . $phone['agent']) {
                     // dial out
                     if ($cdrrow['didnumber'] != '') {
                         $callerid[$username] = trim($cdrrow['didnumber']);
                     } else {
                         if (trim($cdrrow['dst']) != '') {
                             $callerid[$username] = trim($cdrrow['dst']);
                         } else {
                             $callerid[$username] = $dst_tmp;
                         }
                     }
                     $direction[$username] = "dialout";
                     $status[$username] = 1;
                     $srcchan[$username] = trim($cdrrow['srcchan']);
                     $dstchan[$username] = trim($cdrrow['dstchan']);
                 } elseif (strstr($cdrrow['dst'], $phone['extension']) or strstr($cdrrow['dstchan'], $phone['channel']) or $cdrrow['dstchan'] == "agent/" . $phone['agent']) {
                     //dial in
                     $callerid[$username] = trim($cdrrow['src']);
                     $direction[$username] = "dialin";
                     $status[$username] = 1;
                     $srcchan[$username] = trim($cdrrow['srcchan']);
                     $dstchan[$username] = trim($cdrrow['dstchan']);
                 } else {
                     $callerid[$username] = '';
                     $direction[$username] = '';
                     $status[$username] = 0;
                 }
             } else {
                 if ($phone_status[$phone['extension']] == 'unknown' || $phone_status[$phone['extension']] == 'unreachable' || $phone_status[$phone['extension']] == '' || $phone_status[$phone['extension']] == 'unregistered') {
                     $status[$username] = 2;
                 } elseif ($phone_status[$phone['extension']] == 'reachable' || $phone_status[$phone['extension']] == 'registered' || strstr($phone_status[$phone['extension']], 'ok')) {
                     $status[$username] = 0;
                 }
                 $callerid[$username] = '';
                 $direction[$username] = '';
             }
         }
     } else {
         $events =& asterEvent::getEvents($curid);
         while ($events->fetchInto($list)) {
             $data = trim($list['event']);
             list($event, $event_val, $ev, $priv, $priv_val, $pv, $chan, $chan_val, $cv, $stat, $stat_val, $sv, $extra) = split(" ", $data, 13);
             //			if (strtolower(substr($chan_val,0,3)) != "sip" && strtolower(substr($chan_val,0,3)) != "iax") continue;	// also we check iax peer status
             if (strtolower(substr($chan_val, 0, 3)) != "sip") {
                 continue;
             }
             if (substr($event_val, 0, 10) == "PeerStatus") {
                 if (!in_array($chan_val, $phones)) {
                     $phones[] = $chan_val;
                 }
                 if (substr($stat_val, 0, 11) == "unreachable") {
                     $status[$chan_val] = 2;
                     continue;
                 }
                 if (substr($stat_val, 0, 12) == "unregistered") {
                     $status[$chan_val] = 2;
                     continue;
                 }
                 if (substr($stat_val, 0, 9) == "reachable") {
                     if ($status[$chan_val] == 1) {
                         continue;
                     }
                     $status[$chan_val] = 0;
                     continue;
                 }
                 if (substr($stat_val, 0, 12) == "registered") {
                     if ($status[$chan_val] == 1) {
                         continue;
                     }
                     $status[$chan_val] = 0;
                     continue;
                 }
                 if (!isset($status[$chan_val])) {
                     $status[$chan_val] = 0;
                 }
                 continue;
             }
             if (substr($event_val, 0, 10) == "Newchannel") {
                 $peer_val = split("-", $chan_val);
                 if (!in_array($peer_val[0], $panelphones)) {
                     $panelphones[] = $peer_val[0];
                 }
                 $status[$peer_val[0]] = 1;
                 //get unique id
                 //add by solo 2007-11-1
                 $extra = split("  ", $extra);
                 foreach ($extra as $temp) {
                     if (preg_match("/^Uniqueid:/", $temp)) {
                         $uniqueid = substr($temp, 9);
                         $callerid[$peer_val[0]] =& asterEvent::getCallerID($uniqueid);
                         $direction[$peer_val[0]] = "dialin";
                     }
                 }
                 if ($callerid[$peer_val[0]] == 0) {
                     // it's a dial out
                     $srcInfo =& asterEvent::getInfoBySrcID($uniqueid);
                     $callerid[$peer_val[0]] = $srcInfo['Extension'];
                     $direction[$peer_val[0]] = "dialout";
                 }
                 //**************************
                 continue;
             }
             if (substr($event_val, 0, 8) == "Newstate") {
                 $peer_val = split("-", $chan_val);
                 if (!in_array($peer_val[0], $panelphones)) {
                     $panelphones[] = $peer_val[0];
                 }
                 $status[$peer_val[0]] = 1;
                 continue;
             }
             if (substr($event_val, 0, 6) == "Hangup") {
                 $peer_val = split("-", $chan_val);
                 if (!in_array($peer_val[0], $panelphones)) {
                     $panelphones[] = $peer_val[0];
                 }
                 $status[$peer_val[0]] = 0;
                 $callerid[$peer_val[0]] = "";
                 continue;
             }
         }
     }
     if ($type == 'list') {
         if (!isset($_SESSION['curuser']['extensions']) or $_SESSION['curuser']['extensions'] == '') {
             $phones = array();
         } else {
             //$phones = $_SESSION['curuser']['extensions'];
             $phones = $panellist;
         }
         //print_r($phones);print_r($status);print_r($callerid);print_r($direction);exit;
         $action =& asterEvent::listStatus($phones, $status, $callerid, $direction, $srcchan, $dstchan);
     } else {
         //$_SESSION['curuser']['extensions_session'] = $phones;
         $action =& asterEvent::tableStatus($panellist, $status, $callerid, $direction, $srcchan, $dstchan, $curhover);
     }
     $_SESSION['extension_status'] = $status;
     $_SESSION['callerid'] = $callerid;
     $_SESSION['direction'] = $direction;
     $_SESSION['srcchan'] = $srcchan;
     $_SESSION['dstchan'] = $dstchan;
     $html .= $action;
     return $html;
 }