<?php
// **************************************************************************
// Phonebook Server for Gigaset IP Phones by TimoLage
// History of changes
// V1.5 20/06/12 by TimoLage
// - support for LDAP servers
// **************************************************************************
// Search request Lastname (ln=L*)
// http://<IP-Server>/xmlMysql/xmlMysql.php?command=get_list&type=pb&fn=*&ln=L*&st=*&ct=*&hm=*&nr=*&zc=*&sip=*&mb=*&first=1&count=10&reqsrc=user&limit=3072&mac=7C2F800803F1&lang=3
// Detail Search request Firstname (fn=T*) and Lastname (ln=L*)
// http://<IP-Server>/xmlMysql/xmlMysql.php?command=get_list&type=pb&fn=T*&ln=L*&st=*&ct=*&hm=*&nr=*&zc=*&sip=*&mb=*&first=1&count=10&reqsrc=user&limit=3072&mac=7C2F800803F1&lang=3
// Search request for autolook up (hm=02871*)
// http://<IP-Server>/xmlMysql/xmlMysql.php?command=get_list&type=pb&fn=*&ln=*&st=*&ct=*&hm=02871*&nr=*&zc=*&sip=*&mb=*&first=1&count=10&reqsrc=user&limit=3072&mac=7C2F800803F1&lang=3
// **************************************************************************
// get the search criteria from the HTTP GET call
// **************************************************************************
// last name
if (isset($_GET["ln"])){
$sc_ln = $_GET["ln"];
$sc_ln = str_replace('*','%',$sc_ln);
} else {
$sc_ln = "%";
}
// first name
if (isset($_GET["fn"])){
$sc_fn = $_GET["fn"];
$sc_fn = str_replace('*','%',$sc_fn);
} else {
$sc_fn = "%";
}
// city
if (isset($_GET["ct"])){
$sc_ct = $_GET["ct"];
} else {
$sc_ct = "%";
}
// Get directory type
if (isset($_GET["type"])){
$sc_type = $_GET["type"];
} else {
$sc_ct = "%";
}
// number - support for reverse search
if (isset($_GET["hm"])){
$sc_hm = $_GET["hm"];
$sc_hm = str_replace('*','%',$sc_hm);
} else {
$sc_hm = "%";
}
// in $sc_hm we have to replace the leading double zeros by a + eg. 0049 has to be +49 as this is the format I choosed for the DB
//if (strpos($sc_hm, '00') === 0) {
// $sc_hm = substr_replace($sc_hm, "+", 0,2);
//}
// **************************************************************************
// get the result parameters from the HTTP GET call
// **************************************************************************
// first - which is the first entry of the query we should send? (scrolling down)
if (isset($_GET["first"])){
$sc_first = $_GET["first"];
} else {
$sc_first = "0";
}
// count - what is the number of results we should return
if (isset($_GET["count"])){
$sc_count = $_GET["count"];
} else {
$sc_count = "1"; // send 1 result by default
}
// last - what is the last entry we should return (scrolling up)
if (isset($_GET["last"])){
$sc_last = $_GET["last"];
} else {
$sc_last = "0";
}
// either we have a value for first or for last; in case of last we have to calculate first
if ($sc_last <> "0"){
$sc_first = $sc_last-$sc_count+1;
}
elseif ($sc_first == "0"){ // if first and last are zero, set first=1 in order to avoid an SQL error
$sc_first = "1";
}
// **************************************************************************
// create the XML DOM document root and attributes
// **************************************************************************
$dom = new DomDocument('1.0');
$dom->encoding="UTF-8";
//add root - <list>
$list = $dom->appendChild($dom->createElement('list'));
// create list attributes
$response = $dom->createAttribute("response"); // response type? "get list", what else?
$list->appendChild($response);
$type = $dom->createAttribute("type"); // obviously "pb" and "yp"
$list->appendChild($type);
$total = $dom->createAttribute("total"); // total results of query?
$list->appendChild($total);
$first = $dom->createAttribute("first"); // number of first result in list
$list->appendChild($first);
$last = $dom->createAttribute("last"); // number of last result in list
$list->appendChild($last);
$reqid = $dom->createAttribute("reqid"); // is this used at all???
$list->appendChild($reqid);
// define attribute values
$responseValue= $dom->createTextNode("get list");
$response->appendChild($responseValue);
// currently we only support white pages
$typeValue= $dom->createTextNode("$sc_type");
$type->appendChild($typeValue);
// don't know the exact usage of this - probably not used?
$reqidValue= $dom->createTextNode("fortytwo");
$reqid->appendChild($reqidValue);
// **************************************************************************
// connect to MySQL server and send the query
// **************************************************************************
// Verbindung aufbauen, auswählen einer Datenbank
$mysqlhost="localhost"; // Insert MySQL-Host
$mysqluser="root"; // Insert MySQL-User
$mysqlpwd="gigaset"; // Insert Passwort
$mysqldatabase="Directory"; // Insert Database
$mysqltabell="Gigaset"; // Insert Tabell
$link = mysql_connect($mysqlhost, $mysqluser, $mysqlpwd)
or die("Keine Verbindung möglich: " . mysql_error());
//echo "Verbindung zum Datenbankserver erfolgreich";
mysql_select_db($mysqldatabase) or die("Auswahl der Datenbank fehlgeschlagen");
// Ausführen einer SQL-Anfrage
$query = "";
if ($_GET["hm"]!="*")
{
$query = "SELECT * FROM $mysqltabell WHERE HomePhone LIKE '$sc_hm' or OfficePhone LIKE '$sc_hm' or Mobile LIKE '$sc_hm'or Intern LIKE '$sc_hm' ";
}
else
{
if($_GET["fn"]!="*")
{
$query = "SELECT * FROM $mysqltabell WHERE Name LIKE '$sc_ln' and Firstname LIKE '$sc_fn' and Type LIKE '$sc_type' order by Name asc";
}
else
{
$query = "SELECT * FROM $mysqltabell WHERE Name LIKE '$sc_ln' and Type LIKE '$sc_type' order by Name asc";
}
}
$result = mysql_query($query) or die("Anfrage fehlgeschlagen: " . mysql_error());
$numTotalResults = mysql_num_rows($result);
//$info = mysql_fetch_array($result, MYSQL_ASSOC);
$info=array();
while ($line = mysql_fetch_assoc($result))
{
$info[] = array($line);
}
// **************************************************************************
// calculate result counters
// **************************************************************************
// first get number of total results (probably this can be done in a "nicer" way?
//$numTotalResults = $info["count"]; // count number of results
// all results are the arrray, send only those to the phone which where requested
// check boundaries of result window
// if first is beyond TotalResults
if ($sc_first > $numTotalResults)
{
$sc_first=0; // return empty result
$sc_last=0;
}
else // return the requested result window
{
$sc_last = $sc_first + $sc_count-1;
if ($sc_last > $numTotalResults) $sc_last=$numTotalResults;
$sc_count = $sc_last - $sc_first;
}
// generate total attribute for XML reply
$totalValue = $dom->createTextNode($numTotalResults);
$total->appendChild($totalValue);
// generate first and last attributes for the XML reply
$lastValue= $dom->createTextNode($sc_last);
$firstValue= $dom->createTextNode($sc_first);
$last->appendChild($lastValue);
$first->appendChild($firstValue);
// iterate over array and insert the found entries into the XML reply
for ($i=$sc_first; $i<=$sc_last; $i++)
{
//add entry
$entry = $list->appendChild($dom->createElement('entry'));
//add <ln> if available
if ($info[$i-1][0]["Name"])
{
$ln = $entry->appendChild($dom->createElement('ln'));
$ln->appendChild($dom->createTextNode($info[$i-1][0]["Name"]));
}
//add <fn> if available
if ($info[$i-1][0]["Firstname"]) {
$fn = $entry->appendChild($dom->createElement('fn'));
$fn->appendChild($dom->createTextNode($info[$i-1][0]["Firstname"]));
}
//add <st> if available
if ($info[$i-1][0]["Street"])
{
$st = $entry->appendChild($dom->createElement('st'));
$st->appendChild($dom->createTextNode($info[$i-1][0]["Street"]));
}
//add <nr> if available
if ($info[$i-1][0]["Streetnr"])
{
$nr = $entry->appendChild($dom->createElement('nr'));
$nr->appendChild($dom->createTextNode($info[$i-1][0]["Streetnr"]));
}
//add <zc> if available
if ($info[$i-1][0]["ZipCode"])
{
$zc = $entry->appendChild($dom->createElement('zc'));
$zc->appendChild($dom->createTextNode($info[$i-1][0]["ZipCode"]));
}
if ($info[$i-1][0]["City"])
{
//add <l> if available
$ct = $entry->appendChild($dom->createElement('ct'));
$ct->appendChild($dom->createTextNode($info[$i-1][0]["City"]));
}
//add <sip> -- don't know the attribute for LDAP?
if ($info[$i-1][0]["HomePhone"])
{
$sip = $entry->appendChild($dom->createElement('sip'));
$sip->appendChild($dom->createTextNode($info[$i-1][0]["HomePhone"]));
}
//add <hm> if available
if ($info[$i-1][0]["OfficePhone"])
{
$hm = $entry->appendChild($dom->createElement('hm'));
$hm->appendChild($dom->createTextNode($info[$i-1][0]["OfficePhone"]));
}
//add <mb> if available
if ($info[$i-1][0]["Mobile"])
{
$mb = $entry->appendChild($dom->createElement('mb'));
$mb->appendChild($dom->createTextNode($info[$i-1][0]["Mobile"]));
}
//add <in> if available
if ($info[$i-1][0]["Intern"])
{
$mn = $entry->appendChild($dom->createElement('in'));
$mn->appendChild($dom->createTextNode($info[$i-1][0]["Intern"]));
}
//add <em> if available
if ($info[$i-1][0]["email"])
{
$mn = $entry->appendChild($dom->createElement('em'));
$mn->appendChild($dom->createTextNode($info[$i-1][0]["email"]));
}
//add <bi> if available
if ($info[$i-1][0]["Birthday"])
{
$mn = $entry->appendChild($dom->createElement('bi'));
$mn->appendChild($dom->createTextNode($info[$i-1][0]["Birthday"]));
}
//add <cpn> if available
if ($info[$i-1][0]["Company"])
{
$mn = $entry->appendChild($dom->createElement('cpn'));
$mn->appendChild($dom->createTextNode($info[$i-1][0]["Company"]));
}
}
// Freigeben des Resultsets
mysql_free_result($result);
// Schließen der Verbinung
mysql_close($link);
// **************************************************************************
// create XML with DB query results and send back to client
// **************************************************************************
//generate xml
$dom->formatOutput = true; // set the formatOutput attribute of domDocument to true
echo utf8_encode($dom->saveXML()); // output the XML document
?>