#!/usr/bin/php -q
<?php
//Variablendeklarationen
// argv[1] contains the number of the callee
$NUMMER=$argv[1];
$IAC = "00"; // Vorwahlziffern für internationalen Anruf
$NAC = "0"; // Vorwahlziffern für nationalen Anruf long Distance
$CC = "49"; // eigener Ländercode (49 => Deutschland)
$NDC = "30"; // eigene Vorwahl ohne 0 ( 30 => Berlin)
$dbhost="localhost"; // MYSQL-Host
$dbuser="asterisk"; // MYSQL-User
$dbpass="asterisk"; // MYSQL Passwort
$dbschema="asterisk"; // MYSQL DB
function error($line,$file,$str,$mysql_err="") {
$str = "Fehler in Zeile ".$line." in ".$file.": ".$str;
if($mysql_err) $str .= " - MySQL meldet: ".$mysql_err;
print $str."\n";
connmysql(1);
exit;
}
function connmysql($mode,$host="",$user="",$pass="",$schema="") {
global $db, $dbhost, $dbuser, $dbpass, $dbschema;
if ($mode == 0 ) {
// connect to mysql
$db = mysqli_connect($host,$user,$pass,$schema);
if (mysqli_connect_errno()) {
error(__LINE__,__FILE__,"Verbindung zur Datenbank fehlgeschlagen: ".$user."@".$host);
}
if (!mysqli_set_charset($db,'utf8')) {
error(__LINE__,__FILE__,"Setzen des Zeichensatzes utf8 fehlgeschlagen: ".$user."@".$host);
}
mysqli_autocommit($db, FALSE);
} elseif ( $mode == 2 ) {
mysqli_commit($db);
mysqli_autocommit($db, TRUE);
mysqli_close($db);
unset($GLOBALS['db']);
} else {
mysqli_rollback($db);
mysqli_autocommit($db, TRUE);
mysqli_close($db);
unset($GLOBALS['db']);
}
}
function mysql_command($command) {
global $db;
if (mysqli_ping($db)) {
if (preg_match("/^select/i",$command)) {
// SELECT-Statement
if ($result=mysqli_query($db,$command)) {
if (mysqli_num_rows($result) == 0 ) {
mysqli_free_result($result);
$rowcount=-1;
} else {
$rowcount=mysqli_num_rows($result);
}
return array($rowcount,$result);
}
} else {
// Non-Select
if (mysqli_query($db,$command) === TRUE) {
return array(1,1);
}
}
}
return array(0,0);
}
function normalize_number() {
global $NUMMER,$IAC,$CC,$NDC,$NAC,$extch;
if ( preg_match('/^\+/',"$NUMMER") ) {
# nothing to do
$extch=preg_replace('/^\+/',$IAC,$NUMMER);
} elseif ( preg_match('/^'.$IAC.'/',$NUMMER) ) {
# international 00-Format
$extch=$NUMMER;
$NUMMER=preg_replace('/^'.$IAC.'/','+',$NUMMER);
} else {
# national calls
if ( preg_match('/^'.$NAC.'/',$NUMMER) ) {
# long distance
$extch=preg_replace('/^'.$NAC.'/',$IAC.$CC,$NUMMER);
$NUMMER=preg_replace('/^'.$NAC.'/','+'.$CC,$NUMMER);
} else {
# local call
$extch = $IAC.$CC.$NDC.$NUMMER;
$NUMMER = "+".$CC.$NDC.$NUMMER;
}
}
}
function check_exten($extch,$NUMMER) {
$i=15;
unset($COUN);
while ( $i >= 3 ) {
$skey=substr($extch,0,$i);
list($recs,$result) = mysql_command("SELECT iso3166, tz,netz,deutsch FROM exten WHERE exten = '" .$skey . "' LIMIT 1");
if ($recs == 1) {
$row = mysqli_fetch_array($result, MYSQLI_NUM);
$COUN = $row[0];
$TZ = $row[1];
$NET = $row[2];
$DESC = $row[3];
mysqli_free_result($result);
$i=0;
} else { $i--; }
}
if (isset($COUN)) {
print "Land : ".$COUN."\n";
print "Netz : ".$NET."\n";
print "Bezeichnung : ".$DESC."\n";
print "Zeitzone : ".$TZ."\n";
} else {
print "gesuchte Extension nicht gefunden\n";
}
}
// Hauptprogramm
# Connectierung
connmysql(0,$dbhost,$dbuser,$dbpass,$dbschema);
#Rufnummernnormalisierung
normalize_number();
#Daten zur Rufnummer ermitteln
check_exten($extch,$NUMMER);
#Disconnects
connmysql(2);
#End
exit(0);
?>