Problem with ENUM Webservice API
|
| Author |
Message |
npereira
Joined: Jun 01, 2006
Posts: 14
Status: Offline
|
| Posted:
Mar 17, 2009 - 07:26 PM |
|
|
Hi,
I found this script and built an ENUM server as explained by Dean Elwood, yet the API does not seem to function correctly, when you try to update a record based on the ENUM, looks like it works as the page displays 1, but the record in the DB is not updated.
The Add and Delete functions work fine.
Has anyone worked with this API and probebly updated it with mor functions?
BTW, i'm not a php guru... |
|
|
|
 |
dean
Site Admin
Joined: Dec 13, 2003
Posts: 7867
Location: London
Status: Offline
|
| Posted:
Mar 18, 2009 - 08:19 AM |
|
|
Hi there,
It works fine for me. I'll go refresh my memory on it and see if there's something obvious that might go wrong.
Dean |
|
|
|
 |
dean
Site Admin
Joined: Dec 13, 2003
Posts: 7867
Location: London
Status: Offline
|
| Posted:
Mar 18, 2009 - 10:18 AM |
|
|
I can't see anything wrong. Can you post your actual code so we can take a look ? |
|
|
|
 |
npereira
Joined: Jun 01, 2006
Posts: 14
Status: Offline
|
| Posted:
Mar 18, 2009 - 11:31 AM |
|
| dean : | | I can't see anything wrong. Can you post your actual code so we can take a look ? |
| Code: |
<?php
///////////////////////////////////////////////////////////////////////////////////////////////
//
// ENUM webservices API functions
//
// Version 0.1 by Dean Elwood 1st August 2008
//
// A set of functions to add, delete and update records in a central ENUM database.
//
// Released under GNU Public licence http://www.gnu.org/licenses/gpl.txt
//
///////////////////////////////////////////////////////////////////////////////////////////////
// EDIT THIS
$logfile_path = "/var/log/syslog";
// AND DON'T FORGET TO CHANGE ALL REFERENCES TO mydomain.net TO YOUR OWN DOMAIN
function getEnumDatabaseConnection () {
$connection = mysql_connect("localhost", "pdns", "pdns") or die ("Cannot make the connection");
$db = mysql_selectdb("pdns", $connection) or die ("Cannot connect to the database");
return $db;
}
function logThis($text) {
if (!$fp = fopen($logfile_path, 'a')) {
$result = "0";
} else {
flock ($fp, LOCK_SH);
$result = fwrite($fp, date("y-m-d G.i:s", time())." ".$text."\n");
flock($fp, LOCK_UN);
fclose($fp);
}
return $result;
}
function cleanUpNumber($number) {
// Strip leading chars
$number = trim($number);
// Strip non-numerics
$number = preg_replace ("/[^0-9]/", "", $number);
// Strip leading zeros
while ( $number[0] == "0" ) {
$number = ltrim($number, "0");
}
return $number;
}
function enumFormat($number) {
// Takes an e164 formatted number and returns ENUM formatted
$number = cleanUpNumber($number);
$e164 = ereg_replace("[^0-9]", "", $number);
// Reverse the order of our e164 number and put dots in between
$rev_e164 = "";
for ($i = strlen($e164); $i >= 0; $i-- ) {
$rev_e164 .= $e164[$i];
if ( $i != strlen($e164) ) { $rev_e164 .= "."; }
}
// Shove our private tree tail on the end
$rev_e164 .= "e164.siptest.com";
return $rev_e164;
}
function getEnumRecord($number) {
$enum = enumFormat($number);
$db = getEnumDatabaseConnection();
$sql = "SELECT * FROM `records` WHERE `name` = '$enum' LIMIT 1";
$result = mysql_query($sql);
if ($result) {
$row = mysql_fetch_row($result);
$response = $row['0'];
} else {
$response = False;
}
return $response;
}
function addEnumRecord($sipUri, $number) {
// Check it doesn't already exist...
logThis("Trying to add $sipUri as destination for $number");
if ( getEnumRecord($number) ) {
$response = False;
} else {
$enum = enumFormat($number);
$content = "100 10 \"u\" \"E2U+sip\" \"!^.*$!sip:".$sipUri."!\" .";
// Shove it in the DB as a new NAPTR record
$db = getEnumDatabaseConnection();
$sql = "INSERT INTO `records` values('', '10', '$enum', 'NAPTR', '$content', '600', NULL, UNIX_TIMESTAMP());";
$response = mysql_query($sql);
if (!$response) {
logThis("Database insert failed trying to add enum record.");
} else {
logThis("Added $number to ENUM routing to $sipUri");
}
}
return $response;
}
function updateEnumRecord($number, $sipUri) {
$enum = enumFormat($number);
$content = "100 10 \"u\" \"E2U+sip\" \"!^.*$!sip:".$sipUri."!\"";
$db = getEnumDatabaseConnection();
$sql = "UPDATE `records` SET `content` = '$content' WHERE `name` ='$enum' LIMIT 1";
$response = mysql_query($sql);
if (!$response) {
logThis("Database insert failed trying to update enum record.");
}
return $response;
}
function deleteEnumRecord($recordId) {
$db = getEnumDatabaseConnection();
$sql = "DELETE FROM `records` WHERE `id` = '$recordId'";
$response = mysql_query($sql);
if (!$response) {
logThis("Database insert failed trying to delete enum record.");
}
return $response;
}
?>
|
|
|
|
|
 |
npereira
Joined: Jun 01, 2006
Posts: 14
Status: Offline
|
| Posted:
Mar 18, 2009 - 01:52 PM |
|
Here is the call.php file:
| Code: | <?php
///////////////////////////////////////////////////////////////////////////////////////////////
//
// ENUM Webservice API
//
// Version 0.1 by Dean Elwood 1st August 2008
//
// Released under GNU Public licence http://www.gnu.org/licenses/gpl.txt
//
//
///////////////////////////////////////////////////////////////////////////////////////////////
// EDIT THIS LINE
include_once('webservice.inc');
$todo = $_GET['todo'];
$number = $_GET['number'];
$sipuri = $_GET['sipuri'];
$success = false;
switch ($todo) {
case "add" :
$success = addEnumRecord($sipuri, $number);
break;
case "delete" :
$recordId = getEnumRecord($number);
if ( $recordId > 0 ) {
$success = deleteEnumRecord($recordId);
} else {
$success = False;
}
break;
case "update" :
$success = updateEnumRecord($sipuri, $number);
break;
default :
echo "Nothing to do?\n";
break;
}
if ( $success ) {
echo $success;
} else {
print_r($_GET);
}
?>
|
|
|
|
|
 |
npereira
Joined: Jun 01, 2006
Posts: 14
Status: Offline
|
| Posted:
Mar 19, 2009 - 11:29 AM |
|
|
can anyone figure out why the get and update functions dont work? |
|
|
|
 |
mazilo
Moderator
Joined: Feb 09, 2005
Posts: 2279
Location: USA
Status: Offline
|
| Posted:
Mar 19, 2009 - 11:41 AM |
|
| npereira : | | can anyone figure out why the get and update functions dont work? |
I don't see any get nor update functions on your codes. If you meant the function updateEnumRecord($number, $sipUri), then you should call this function with variables in respective order, i.e. updateEnumRecord($number, $sipuri) and not updateEnumRecord($sipuri, $number). Do you see the difference? |
|
|
|
 |
dean
Site Admin
Joined: Dec 13, 2003
Posts: 7867
Location: London
Status: Offline
|
| Posted:
Mar 19, 2009 - 12:20 PM |
|
| Quote: | | I don't see any get nor update functions on your codes. |
Exactly. Can you show us how you're calling these functions? |
|
|
|
 |
npereira
Joined: Jun 01, 2006
Posts: 14
Status: Offline
|
| Posted:
Mar 19, 2009 - 01:01 PM |
|
ok, so I added a get function in the call.php
| Code: |
case "get" :
$success = getEnumRecord($number);
break;
|
Also modifued the getEnumRecord function a bit to echo what I want to see...
| Code: |
function getEnumRecord($number) {
$enum = enumFormat($number);
$db = getEnumDatabaseConnection();
$sql = "SELECT * FROM `records` WHERE `name` = '$enum' LIMIT 1";
$result = mysql_query($sql);
if ($result) {
$row = mysql_fetch_row($result);
$response = $row['0'];
[b] echo $row['2'];
echo $row['4'];[/b]
} else {
$response = False;
}
return $response;
}
|
I'm far from being a php coder, but playing around seems to do some stuff...
The problem now, is the print out on the page looks like:
| Code: |
7.5.0.0.5.3.5.2.3.4.1.e164.siptest.com100 10 "u" "E2U+sip" "!^.*$!sip:14325350057@10.98.7.198!" .34
|
I would like the print out to show/format the screen output as:
1-432-535-0057 sip:14325350057@10.98.7.198
Also, if the $number is not in the DB, I want it to print out that this record does not exist. Right now if the $number does not exist, it shows:
| Code: |
Array ( [todo] => get [number] => 1100 )
|
How can I do this? Also, the LogThis function is not logging anything at all... |
|
|
|
 |
dean
Site Admin
Joined: Dec 13, 2003
Posts: 7867
Location: London
Status: Offline
|
| Posted:
Mar 19, 2009 - 01:56 PM |
|
| Quote: | I would like the print out to show/format the screen output as:
1-432-535-0057 sip:14325350057@10.98.7.198
Also, if the $number is not in the DB, I want it to print out that this record does not exist. Right now if the $number does not exist, it shows: |
You'll need to adapt the code to do that. The output from the DB is in NAPTR format.
| Quote: | | Also, the LogThis function is not logging anything at all... |
| Quote: | | $logfile_path = "/var/log/syslog"; |
You've set the path to syslog which I suspect your Apache instance doesn't have permission to write to. Syslog is the system logger. Use a different logfile and make sure that Apache has the requisite permissions to write to it. |
|
|
|
 |
mazilo
Moderator
Joined: Feb 09, 2005
Posts: 2279
Location: USA
Status: Offline
|
| Posted:
Mar 19, 2009 - 03:09 PM |
|
| npereira : | | Code: | $todo = $_GET['todo'];
$number = $_GET['number'];
$sipuri = $_GET['sipuri'];
$success = false;
:
.
if ( $success ) {
echo $success;
} else {
print_r($_GET);
} |
|
I, too, am not a PHP programmer. However, I am just curious if the above print_r($_GET); statement is responsible to print out as shown below.
| npereira : | The problem now, is the print out on the page looks like:
| Code: |
7.5.0.0.5.3.5.2.3.4.1.e164.siptest.com100 10 "u" "E2U+sip" "!^.*$!sip:14325350057@10.98.7.198!" .34
|
|
|
|
|
|
 |
|
| Forum Rules and Guidelines |
About VoIP User |
Privacy Policy
|
All logos and trademarks in this site are property of their respective owner. Comments and posts are property of the poster, all the rest (c) 2003-2008 VoIP User Limited.
VoIP User Limited is incorporated in England and Wales under Company Number 6694577.
No part of this site may be reproduced without our prior consent.
|
|