Created
July 13, 2010 18:34
-
-
Save avimar/474297 to your computer and use it in GitHub Desktop.
SipSorcery REST PHP Code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$user="USER"; | |
$pass="PASS"; | |
include("SipSorcery.inc.php"); | |
ini_set('session.use_only_cookies',1); | |
ini_set('session.gc_maxlifetime',31557600); | |
session_set_cookie_params(24*24*60); | |
session_start(); | |
if($_GET['do']==reset){ | |
$_SESSION['auth']=null; | |
} | |
define( 'url',"https://www.sipsorcery.com/provisioning.svc/rest/"); | |
function login($user, $pass) { | |
$url2=url."customer/login?username=$user&password=$pass"; | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $url2); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
$output = curl_exec($ch); | |
curl_close($ch); | |
echo $key; | |
if (preg_match('/(\w{96})/', $output,$matches)){ | |
echo "successfully logged in!"; | |
$_SESSION['auth']=$matches[1]; | |
//$time=time(); | |
$_SESSION['login_time']=time(); | |
return 1; | |
} | |
else { | |
echo "error logging in."; | |
return 0; | |
} | |
} | |
function getcalls(){ | |
$url2=url."getcalls"; | |
$url2.="?count=50"; | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $url2); | |
curl_setopt($ch, CURLOPT_COOKIE, "authid=".$_SESSION['auth']); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
$output = curl_exec($ch); | |
curl_close($ch); | |
if(strlen($output)>150){ | |
$xml=preg_replace ('~<(Inserted)\s*xmlns:a="http://schemas.datacontract.org/2004/07/System"><a:DateTime>([^<]*)\</a:DateTime>\<a:OffsetMinutes>0\</a:OffsetMinutes>\</\\1>~', "<$1>$2</$1>" , $output); | |
$xml=simplexml_load_string($xml); | |
$array=cleanArray($xml);//replace those empty arrays with nothing. | |
//$keys=array("AdminMemberId","BridgeId","CDRId","CSeq","CallDurationLimit","Direction","Id","Inserted","LocalTag","ProxySIPSocket","RemoteSDP","RemoteTag","RemoteTarget","RemoteUserField","SDP","TransferMode"); | |
$keys=array("RemoteUserField","RemoteTarget","Direction","BridgeId","CallDurationLimit","Inserted","Id","RemoteTag"); | |
$array=$array['SIPDialogueAsset'];//cut out the pesky extra container | |
$count=count($array);//total returned rows. | |
$size=count($keys);//keys that I'm actually going to show | |
echo "$count legs in progress<br><table border=1>"; | |
echo "<tr>"; | |
for($i=0;$i<$size;$i++){//all table headers | |
echo "<td>".$keys[$i]; | |
} | |
unset($bridgeid); | |
foreach ($array as $key => $row) { | |
$bridge[$key] = $row['BridgeId']; | |
$direction[$key] = $row['Direction']; | |
} | |
array_multisort($bridge, SORT_ASC, $direction, SORT_ASC, $array);//Keep bridgeIds together, sort IN to be on top. | |
for($i=0;$i<$count;$i++){ | |
echo "<tr>"; | |
if(isset($bridgeid) && $array[$i]['BridgeId']==$bridgeid){} | |
elseif(!isset($bridgeid)){} | |
else echo $xls ? "<br>" : "<td> <tr>"; | |
$bridgeid=$array[$i]['BridgeId']; | |
for($z=0;$z<$size;$z++){//each cell | |
if ($keys[$z]=='RemoteUserField'){ | |
if(preg_match("/<sip:([^@]*)@([^;]*)>/",$array[$i]['RemoteUserField'],$user)){ | |
preg_match("/\"([^\"]*)\"\s*\<sip/",$array[$i]['RemoteUserField'],$name); | |
echo "<td>$name[1] $user[1]@$user[2]"; | |
} | |
else echo "<td>".htmlentities($array[$i]['RemoteUserField']); | |
} | |
elseif($keys[$z]=='Inserted'){ | |
$time_stamp=strtotime($array[$i]['Inserted'])-60*60*7;//time adjust to my server | |
//echo $time_stamp."<br>"; | |
echo "<td>"; | |
if($time_stamp>0) echo ago($time_stamp); | |
else echo $time_stamp; | |
} | |
else echo "<td>".htmlentities($array[$i][$keys[$z]]); | |
} | |
} | |
echo "</table>"; | |
} | |
else echo "No calls in progress.<br><br>"; | |
} | |
if ((isset($_SESSION['auth']) && strlen($_SESSION['auth'])==96 && ((time()-$_SESSION['login_time'])<60*58)) || login($user,$pass)){//body of the script | |
getcalls(); | |
} | |
else echo "error authenticating"; | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function format_phone($phone){ | |
//$phone = preg_replace("/[^0-9]/", "", $phone); | |
$phone = preg_replace("/^\+/", "011", $phone); | |
if(strlen($phone) == 7) | |
return preg_replace("/([0-9]{3})([0-9]{4})/", "$1-$2", $phone); | |
elseif(strlen($phone) >= 8 && strlen($phone) <= 11) | |
return preg_replace("/1?([0-9]{1,3})([0-9]{3})([0-9]{4})/", "$1-$2-$3", $phone); | |
elseif(strlen($phone) == 14||strlen($phone) == 15) | |
return preg_replace("/([01]{3})([0-9]{3})([0-9]{1,2})([0-9]{3})([0-9]{4})/", "$1-$2-$3-$4-$5", $phone); | |
else | |
return $phone; | |
} | |
function ago($d){ | |
$chunks = array( | |
array(60 * 60 * 24 * 365 , 'year'), | |
array(60 * 60 * 24 * 30 , 'month'), | |
array(60 * 60 * 24 * 7, 'week'), | |
array(60 * 60 * 24 , 'day'), | |
array(60 * 60 , 'hour'), | |
array(60 , 'minute'), | |
array(1 , 'second') | |
); | |
$d = time() - $d; | |
if($d==0) echo "just now"; | |
for($i=0;$i<6;$i++){//check them from largest to smallest | |
//echo "checking if difference of $d seconds was more than {$chunks[$i][0]} seconds, which is a {$chunks[$i][1]}<br>"; | |
if($d>=$chunks[$i][0]) { | |
$unit=intval(floor($d/$chunks[$i][0])); | |
return $unit." ".$chunks[$i][1].(($unit==1)?'':'s')." ago"; | |
}//it's a match | |
}//loop the sizes | |
}//"x minutes ago" function | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment