userAgentImages = $userAgentImages; $this->filters = $filters; $this->allowedDomains = $allowedDomains; list($ip,$port) = explode(":",$dispatcher); $this->dispatcher_ip = $ip; if ($port) $this->dispatcher_port = $port; return $this->getSessions(); } function getSessions () { if (!$this->dispatcher_ip) return false; if (!$this->dispatcher_port) return false; if ($fp = fsockopen ($this->dispatcher_ip, $this->dispatcher_port, $errno, $errstr, $this->timeout)) { printf ("

Connected to MediaProxy2 dispatcher %s:%s",$this->dispatcher_ip, $this->dispatcher_port); if (!count($this->allowedDomains)) { fputs($fp, "summary\r\n"); $line = fgets($fp); $this->relays = json_decode($line); } fputs($fp, "sessions\r\n"); $line = fgets($fp); $_sessions=json_decode($line); if (count($this->allowedDomains)) { foreach ($_sessions as $_session) { list($user1,$domain1)=explode("@",$_session->from_uri); list($user2,$domain2)=explode("@",$_session->to_uri); if (!in_array($domain1,$this->allowedDomains) && !in_array($domain2,$this->allowedDomains)) { continue; } if (strlen($this->filters['user'])) { $user=$this->filters['user']; if (preg_match("/$user/",$_session->from_uri) || preg_match("/$user/",$_session->to_uri) ) { $this->sessions[] = $_session; } } } } else { $this->sessions = $_sessions; } fclose($fp); return true; } else { printf ("

Error connecting to %s:%s: %s (%s) \n",$this->dispatcher_ip,$this->dispatcher_port,$errstr,$errno); return false; } } function showSearch() { if (!count($this->sessions)) return; printf ("

", $_SERVER['PHP_SELF'], $_REQUEST['user'] ); } function showHeader() { print " Media sessions "; } function showFooter() { } function show() { $this->showHeader(); print "

Media sessions

"; $this->showSearch(); if (!count($this->allowedDomains)) { $this->showRelays(); } $this->showSessions(); $this->showFooter(); } function showRelays() { if (!count($this->sessions)) return; print " "; $i = 1; foreach ($this->relays as $relay) { unset($media_types); foreach ($relay->stream_count as $key => $value) { $media_types++; } if ($media_types > 1) { $streams = "
Address Version Uptime Relayed traffic Sessions Streams Status
"; foreach ($relay->stream_count as $key => $value) { $streams .= sprintf("",$key,$value); } $streams .= "
%s%s
"; } else { foreach ($relay->stream_count as $key => $value) { $streams=sprintf("%s %s",$key,$value); } } printf (" %d %s %s %s %s %d %s %s ", $i, $relay->ip, $relay->version, $this->normalizeTime($relay->uptime), $this->normalizeTraffic($relay->bps_relayed), $relay->session_count, $streams, ucfirst($relay->status) ); $i++; } print "
"; } function showSessions () { if (!count($this->sessions)) return; print " "; $i = 1; foreach ($this->sessions as $session) { $from = $session->from_uri; $to = $session->to_uri; $fromAgent = $session->caller_ua; $toAgent = $session->callee_ua; $fromImage = $this->getImageForUserAgent($fromAgent); $toImage = $this->getImageForUserAgent($toAgent); $sc = count($session->streams); print " "; $duration = $this->normalizeTime($session->duration); foreach ($session->streams as $streamInfo) { $status = $streamInfo->status; if ($status=="idle" || $status=='hold') { $idletime = $this->normalizeTime($streamInfo->timeout_wait); $status = sprintf("%s %s", $status, $idletime); } $caller = $streamInfo->caller_remote; $callee = $streamInfo->callee_remote; $relay_caller = $streamInfo->caller_local; $relay_callee = $streamInfo->callee_local; $codec = $streamInfo->caller_codec; $type = $streamInfo->media_type; if ($caller == '?.?.?.?:?') { $caller = '–'; // a dash $align1 = 'center'; } else { $align1 = 'left'; } if ($callee == '?.?.?.?:?') { $callee = '–'; // a dash $align2 = 'center'; } else { $align2 = 'left'; } if ($codec == 'Unknown') $codec = '–'; // a dash if ($type == 'Unknown') $type = '–'; // a dash $bytes_in1 = $this->normalizeBytes($streamInfo->caller_bytes); $bytes_in2 = $this->normalizeBytes($streamInfo->callee_bytes); print " "; } $i++; } print "
  Callers Phones Media Streams
Caller address Relay caller Relay callee Callee address Status Codec Type Duration Bytes
Caller
Bytes
Called
$i From: $from
To: $to
\"$fromAgent\" \"$toAgent\" $caller $relay_caller $relay_callee $callee $status $codec $type $duration $bytes_in1 $bytes_in2

"; } function normalizeBytes($bytes) { $mb = $bytes/1024/1024.0; $kb = $bytes/1024.0; if ($mb >= 0.95) { return sprintf("%.2fM", $mb); } else if ($kb >= 1) { return sprintf("%.2fk", $kb); } else { return sprintf("%d", $bytes); } } function normalizeTime($period) { $sec = $period % 60; $min = floor($period/60); $h = floor($min/60); $min = $min % 60; if ($h >= 1) { return sprintf('%dh%02d\'%02d"', $h, $min, $sec); } else { return sprintf('%d\'%02d"', $min, $sec); } } function normalizeTraffic($traffic) { // input is in bytes/second $mb = $traffic/1024/1024.0; $kb = $traffic/1024.0; if ($mb >= 0.95) { return sprintf("%.2fMbps", $mb); } else if ($kb >= 1) { return sprintf("%.2fkbps",$kb); } else { return sprintf("%dbps",$traffic); } } function getImageForUserAgent($agent) { foreach ($this->userAgentImages as $agentRegexp => $image) { if (preg_match("/$agentRegexp/i", $agent)) { return $image; } } return "unknown.png"; } } class MediaSessions1 { function MediaSessions1 ($servers=array(),$allowedDomains=array()) { $this->servers = $servers; $this->allowedDomains = $allowedDomains; global $userAgentImages; require_once("phone_images.php"); $this->userAgentImages = $userAgentImages; } function isDomainAllowed($from,$to) { $els = explode("@",$from); $fromDomain = $els[1]; $els = explode("@",$to); $toDomain = $els[1]; if (count($this->allowedDomains)) { if (in_array($fromDomain,$this->allowedDomains) || in_array($toDomain,$this->allowedDomains)) { return 1; } else { return 0; } } else { return 1; } } function normalizeBytes($bytes) { $mb = $bytes/1024/1024.0; $kb = $bytes/1024.0; if ($mb >= 0.95) { return sprintf("%.2fM", $mb); } else if ($kb >= 1) { return sprintf("%.2fk", $kb); } else { return sprintf("%d", $bytes); } } function normalizeTime($period) { $sec = $period % 60; $min = floor($period/60); $h = floor($min/60); $min = $min % 60; if ($h >= 1) { return sprintf('%dh%02d\'%02d"', $h, $min, $sec); } else { return sprintf('%d\'%02d"', $min, $sec); } } function normalizeTraffic($traffic) { // input is in bytes/second $traffic = $traffic * 8; $mb = $traffic/1024/1024.0; $kb = $traffic/1024.0; if ($mb >= 0.95) { return sprintf("%.2fMbps", $mb); } else if ($kb >= 1) { return sprintf("%.2fkbps",$kb); } else { return sprintf("%dbps",$traffic); } } function getImageForUserAgent($agent) { foreach ($this->userAgentImages as $agentRegexp => $image) { if (preg_match("/$agentRegexp/i", $agent)) { return $image; } } return "unknown.png"; } function getRTPSessions($ip, $port) { if ($fp = fsockopen ($ip, $port, $errno, $errstr, "3") ) { fputs($fp, "status\n"); $proxy = array('status' => 'Ok'); $crtSession = 'None'; while (!feof($fp)) { $line = fgets($fp, 2048); $elements = explode(" ", $line); if ($elements[0] == 'version' && count($elements)==2) { $proxy['version'] = $elements[1]; } else if ($elements[0] == 'proxy' && count($elements)==3) { $proxy['sessionCount'] = $elements[1]; $traffic = explode("/", $elements[2]); $proxy['traffic'] = array('caller' => $traffic[0], 'called' => $traffic[1], 'relayed' => $traffic[2]); $proxy['sessions'] = array(); } else if ($elements[0]=='session' && count($elements)==7) { if ($this->isDomainAllowed($elements[2],$elements[3])) { $crtSession = $elements[1]; $info = array('from' => $elements[2], 'to' => $elements[3], 'fromAgent' => quoted_printable_decode($elements[4]), 'toAgent' => quoted_printable_decode($elements[5]), 'duration' => $elements[6], 'streams' => array()); $proxy['sessions'][$crtSession] = $info; $allowed_session=1; } else { unset($allowed_session); } } else if ($elements[0] == 'stream' && count($elements)==9) { if (!$allowed_session) continue; $stream = array('caller' => $elements[1], 'called' => $elements[2], 'via' => $elements[3], 'bytes' => explode("/", $elements[4]), 'status' => $elements[5], 'codec' => $elements[6], 'type' => $elements[7], 'idletime' => $elements[8]); $proxy['sessions'][$crtSession]['streams'][] = $stream; } else { //print "Invalid line: '$line'
\n"; } } fclose($fp); if (!isset($proxy['version'])) { if ($fp = fsockopen ($ip, $port, $errno, $errstr, "2") ) { fputs($fp, "version\n"); $line = fgets($fp, 2048); $version = trim($line); if (!$version) $version = 'unknown'; $proxy['version'] = $version; fclose($fp); } } return $proxy; } else { return array('status' => "$errstr"); } } function haveSessions() { foreach ($this->servers as $server) { if ($this->sessions[$server]['sessionCount'] > 0) { return True; } } return False; } function showSummary() { // IE seems to ignore border on elements // that's why we used bordertb on and print " "; $i = 1; foreach ($this->servers as $server) { list($ip, $port) = explode(":", $server); $sessionInfo = $this->sessions[$server]; $status = $sessionInfo['status']; if ($status=='Ok') $version = $sessionInfo['version']; else $version = " "; if ($status!='Ok' || $sessionInfo['sessionCount'] == 0) { $caller = " "; $called = " "; $relayed = " "; $sessionCount = " "; } else { $caller = $this->normalizeTraffic($sessionInfo['traffic']['caller']); $called = $this->normalizeTraffic($sessionInfo['traffic']['called']); $relayed = $this->normalizeTraffic($sessionInfo['traffic']['relayed']); $sessionCount = $sessionInfo['sessionCount']; } print " "; $i++; } print "
Server Version Caller traffic Called traffic Relayed traffic Sessions Status
$i $ip $version $caller $called $relayed $sessionCount $status

"; } function showSessions() { if ($this->haveSessions($this->servers, $sessions)) { print " "; $i = 1; foreach ($this->servers as $server) { $serverSessions = $this->sessions[$server]['sessions']; foreach ($serverSessions as $id => $sessionInfo) { $sc = count($sessionInfo['streams']); $from = $sessionInfo['from']; $to = $sessionInfo['to']; $fromAgent = $sessionInfo['fromAgent']; $toAgent = $sessionInfo['toAgent']; $fromImage = $this->getImageForUserAgent($fromAgent); $toImage = $this->getImageForUserAgent($toAgent); print " "; $duration = $this->normalizeTime($sessionInfo['duration']); foreach ($sessionInfo['streams'] as $streamInfo) { $status = $streamInfo['status']; if ($status=="idle" || $status=='hold') { $idletime = $this->normalizeTime($streamInfo['idletime']); $status = sprintf("%s %s", $status, $idletime); } $caller = $streamInfo['caller']; $called = $streamInfo['called']; $via = $streamInfo['via']; $codec = $streamInfo['codec']; $type = $streamInfo['type']; if ($caller == '?.?.?.?:?') { $caller = '–'; // a dash $align1 = 'center'; } else { $align1 = 'left'; } if ($called == '?.?.?.?:?') { $called = '–'; // a dash $align2 = 'center'; } else { $align2 = 'left'; } if ($codec == 'Unknown') $codec = '–'; // a dash if ($type == 'Unknown') $type = '–'; // a dash $bytes_in1 = $this->normalizeBytes($streamInfo['bytes'][0]); $bytes_in2 = $this->normalizeBytes($streamInfo['bytes'][1]); $bytes_rel = $this->normalizeBytes($streamInfo['bytes'][2]); print " "; } $i++; } } print "
  Call Phones Media Streams
Caller address Called address Via address Status Codec Type Duration Bytes
Caller
Bytes
Called
Bytes
Relayed
$i From: $from
To: $to
\"$fromAgent\" \"$toAgent\" $caller $called $via $status $codec $type $duration $bytes_in1 $bytes_in2 $bytes_rel

"; } } function showHeader() { print " Media Sessions

Media Sessions

"; } function show() { $this->sessions = array(); foreach ($this->servers as $server) { list($ip, $port) = explode(":", $server); if (!$port) $port = "25060"; $this->sessions[$server] = $this->getRTPSessions($ip, $port); } print "

Media sessions

"; $this->showSessions(); if (!count($this->allowedDomains)) { $this->showSummary(); } } } ?>