Skip to main content
Topic: General Coding Discussion? (Read 6882 times) previous topic - next topic
0 Members and 1 Guest are viewing this topic.

Re: General Coding Discussion?

Reply #15

A million thanks @emanuele . I am glad that is settled for now. The updates are now fully working. Attached is the working ip_updater.php file.

However, Till @ howtoforge said this way is not advisable.

QuoteThe procedure that you use above will not result in a proper IP update for several reasons:

1) Data in sys_datalog may never be modified. The data there is transaction data, so you can not modify a existing transaction.[1]
2) You dont update the serial of the zone, so your change will get ignored by all external dns servers and clients.

To do a dynamic IP update corrrectly, use the ispconfig remote apt function dns_a_update.

There seems to be two files that I need to use but I am not so clear with. [2]For using remote apt function, I have created a remote user that I put into the soap_config file.

The API is as follows:
Code: [Select]
dns_a_update($session_id, $client_id, $primary_id, $params);

Description:
Updates an IPv4 record if type is a.

Input Variables:
$session_id, $client_id, $primary_id, $params

Parameters (in $params):
server_id  (int(11))
zone  (int(11))
name  (varchar(64))
type  (enum('a','aaaa','alias','cname','hinfo','mx''naptr','ns','ptr','rp','srv','txt'))
data  (varchar(255))
aux  (int(11))
ttl  (int(11))
active  (enum('n','y'))
stamp  (timestamp)
serial  (int(10))

Output:
Returns the number of affected rows.

But, then, how do I supposed to add my working code inside dns_a_update file? :(

Edited: The attached ip_updater.php is removed as it is not the latest. Do get it from the github.
Data is updated as I checked it. Hmmm...
Both sample files are attached.

Re: General Coding Discussion?

Reply #16

The latest working code is already updated in the github. Change server_id (two occurrence to chosen server id). Copy two other files mentioned in that code and make changes as stated (ask if you are not sure). ip_updater.php should be place in tools folder.

Re: General Coding Discussion?

Reply #17

Glad you worked it out! :D
Bugs creator.
Features destroyer.
Template killer.

Re: General Coding Discussion?

Reply #18

I have updated my github on ip_updater with details of how to guides for those who have created or wish to try / create their own server using dynamic IP. Just follow howtoforge guides on creating your preferred ispconfig perfect server and use this ip_updater after following all its guides. Instructions, link and code for ddclient settings are in reply#7 above. Feel free to ask.
Last Edit: December 03, 2015, 07:28:35 pm by ahrasis

Re: General Coding Discussion?

Reply #19

Hi, I need some help again. This time is to convert this code to mysqli[1]:

Code: [Select]
// Get access by using ispconfig default
require_once '/usr/local/ispconfig/interface/lib/config.inc.php';
// Create and check connection to proceed
$ip_updater = mysql_connect($conf['db_host'], $conf['db_user'], $conf['db_password']);
if(!$ip_updater) {
 echo "Connection failed! \r\n"; die(mysql_connect_error());
} else { // Connection is fine. Select the database
 $db_selection = mysql_select_db($conf['db_database'], $ip_updater);
 if(!$db_selection) {
 echo "Can\'t use selected database! \r\n"; die(mysql_error());
 } else { // Database is selected. Get updated public ip from reliable source OR
 $public_ip = file_get_contents('http://ip.sch.my/'); //  Get it from your own updated nameserver ip e.g. $public_ip = gethostbyname('ns1.sch.my');
 if(!filter_var($public_ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) === true) {
 echo "Filtering failed!";  die (mysql_error());
 } else { // IPv4 is true. Important! Change to your intended server_id.
 $select_ip = "SELECT ip_address FROM server_ip WHERE server_id =1";
 $query_ip = mysql_query($select_ip);
 list ($stored_ip) = mysql_fetch_row($query_ip); // Get current stored ip
 if($public_ip == $stored_ip) { // Compare the ip addresses
 echo "Same IP address. No changes is made." . "\r\n"; die (mysql_error());
 } else { // Update database if there is a change
 $update_ip = mysql_query("UPDATE [icode]dns_rr[/icode] SET [icode]data[/icode] = replace([icode]data[/icode], '$stored_ip', '$public_ip')");
 $update_ip3 = mysql_query("UPDATE [icode]server_ip[/icode] SET [icode]ip_address[/icode] = replace([icode]ip_address[/icode], '$stored_ip', '$public_ip')");// Check the above process updates
 // Check the above process updates
 if(!($update_ip || $update_ip3)) {
 echo "Public ip should now be the same with stored ip. :( \r\n"; die (mysql_error());
 } // Now we update ip address in soa zone files
 foreach (glob("/etc/bind/pri.*") as $filename) {
 $file = file_get_contents($filename);
 file_put_contents($filename, preg_replace("/$stored_ip/","$public_ip",$file));
 }
 } // Resynce data. Important! Copy resync.php to ipu_resync and app.inc.php to ipu_app.inc.php
 // Disable admin check and tpl in ipu_resync.php and start_session() in ipu_app.inc.php.
 // Change require once in ipu_resync.php is now changed to ipu_app.inc.php.
 require_once 'ipu_resync.php';
 echo "Check and resync database and soa zone files. Restart apache. \r\n";
 // Double check to confirm stored ipis updated  as update mysql_query always returns successful :(
 $select_new_ip = "SELECT ip_address FROM server_ip WHERE server_id =1"; // Define this similar as above
 $query_new_ip = mysql_query($select_new_ip);
 list ($new_stored_ip) = mysql_fetch_row($query_new_ip); // Get currrent stored_ip
 if ($new_stored_ip != $public_ip) { echo "Updates failed! \r\n"; die (mysql_error()); }
 else { echo "Updates are successful! Thanks God." . "\r\n"; }
 }
 }
}
// Lastly, we close connection and restart apache.
mysql_close($ip_updater);
echo "Closing connection... \r\n";
exec('service apache2 restart'); // Smile...
?>
Preparing to comply with php7, sighed...

Re: General Coding Discussion?

Reply #20

I feel like giving you some reference would be better than re-writing the script for you. O:-)
http://php.net/manual/en/mysqli.summary.php
In general, you just replace "mysql" with "mysqli". In some cases (like mysqli_query) the order of the parameters changed.
Honestly I don't remember mysql all the changes, so your best bet is to have a look at the functions in the php manual and compare. :)
Bugs creator.
Features destroyer.
Template killer.


Re: General Coding Discussion?

Reply #22

It has been two months and I finally get my hands on this, again. I managed to convert the code to mysqli which supposedly should run fine in php 5.6 and 7. The following is the latest code:

Code: [Select]
<?php
/*
Copyright (c) 2015, Ahmad Rasyid Ismail, ahrasis@gmail.com
Project IP Updater for ubuntu, ispconfig 3 and dynamic ip users.
BSD3 License. All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice,
 this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
 this list of conditions and the following disclaimer in the documentation
 and/or other materials provided with the distribution.
* Neither the name of ISPConfig nor the names of its contributors
 may be used to endorse or promote products derived from this software without
 specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/* Get database access by using ispconfig default configuration so no
   user and its password are disclosed. Exit if its connection failed */

require_once 'config.inc.php';
$ip_updater = mysqli_connect($conf['db_host'], $conf['db_user'], $conf['db_password'], $conf['db_database']);
if (mysqli_connect_errno()) {
    printf("Connection failed! \r\n", mysqli_connect_error());
    exit();
}

/* Else, it works. Now get public ip from a reliable source.
   We are using this but you can define your own. But We just
   need ipv4. So we exit if its filetering failed */

$public_ip = file_get_contents('http://ip.sch.my/');
if(!filter_var($public_ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) === true) {
    printf("IPV4 for public IP filtering failed! \r\n");
    exit();
}

/* Else, it's truly ipv4. Now we obtain the server ip address,
   based on server id. So change your server id accordingly.
   Exit if the server public ip address matches i.e. the same.  */

$query_ip = mysqli_query($ip_updater, 'SELECT ip_address FROM server_ip WHERE server_id =1');
list($stored_ip) = mysqli_fetch_row($query_ip);

if($public_ip == $stored_ip) {
    printf("\r\nThe server and its public ip addresses match. \r\nNo changes is therefore necesary.\r\n\r\n");
    exit();
}

/* Else, update database and soa zone files with the new ip address */

$update1 = mysqli_query($ip_updater, 'UPDATE dns_rr SET data = replace(data, $stored_ip, $public_ip)');
$update2 = mysqli_query($ip_updater, 'UPDATE server_ip SET ip_address = replace(ip_address, $stored_ip, $public_ip)');
foreach (glob('/etc/bind/pri.*') as $filename) {
$file = file_get_contents($filename);
file_put_contents($filename, preg_replace('/$stored_ip/', '$public_ip', $file));
}

/* Now resync so that above changes updated properly. Important!
   Refer to http://ipupdater.sch.my and download or copy
   resync.php to ipu_resync,php and app.inc.php to
   ipu_app.inc.php. Disable admin check and tpl in ipu_resync.php
   and start_session() in  ipu_app.inc.php and change require
   once in ipu_resync.php to ipu_app.inc.php.
   Then double check if the update truly works */

require_once 'ipu_resync.php';
$query_new_ip = mysqli_query($ip_updater, 'SELECT ip_address FROM server_ip WHERE server_id =1');
list($new_stored_ip) = mysqli_fetch_row($query_new_ip);
if ($new_stored_ip != $public_ip) {
    printf("\r\nUpdates failed! \r\nUpdates failed! \r\nUpdates failed! \r\n\r\n");
    exit();
} else { printf("\r\n Database updates are successful! Thanks God. \r\n\r\n"); }

/* Lastly, close database connection and restart apache. */
printf("\r\nClosing connection and restarting apache... \r\n\r\n");
mysqli_close($ip_updater);
exec('service apache2 restart');
?>

Partly the code works except to this part:
Code: [Select]
$update1 = mysqli_query($ip_updater, 'UPDATE dns_rr SET data = replace(data, $stored_ip, $public_ip)');
$update2 = mysqli_query($ip_updater, 'UPDATE server_ip SET ip_address = replace(ip_address, $stored_ip, $public_ip)');
foreach (glob('/etc/bind/pri.*') as $filename) {
$file = file_get_contents($filename);
file_put_contents($filename, preg_replace('/$stored_ip/', '$public_ip', $file));
}

So far that I have checked, based on what I know,  ::) it seems like the above code is correct, but it does not work.  :(  I would highly appreciate if anybody could give me some clues or ideas on how to find what is wrong with the code and fix it.  O:-)

Re: General Coding Discussion?

Reply #23

Managed to fix the above. Tested with php5.6 and 7.0. The new working code, if anybody is interested, can be obtained from the above mentioned links which are as follows:

Code: [Select]
<?php
/*
Copyright (c) 2015, Ahmad Rasyid Ismail, ahrasis@gmail.com
Project IP Updater for ubuntu, ispconfig 3 and dynamic ip users.
BSD3 License. All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice,
  this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
  this list of conditions and the following disclaimer in the documentation
  and/or other materials provided with the distribution.
* Neither the name of ISPConfig nor the names of its contributors
  may be used to endorse or promote products derived from this software without
  specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/* Get database access by using ispconfig default configuration so no
   user and its password are disclosed. Exit if its connection failed */

require_once 'config.inc.php';
$ip_updater = mysqli_connect($conf['db_host'], $conf['db_user'], $conf['db_password'], $conf['db_database']);
if (mysqli_connect_errno()) {
    printf("\r\nConnection failed! \r\n\r\n", mysqli_connect_error());
    exit();
}

/* Else, it works. Now get public ip from a reliable source.
   We are using this but you can define your own. But We just
   need ipv4. So we exit if its filetering failed */

$public_ip = file_get_contents('http://ip.sch.my/');
if(!filter_var($public_ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) === true) {
    printf("\r\nIPV4 for public IP filtering failed! \r\nYou may need to use other ip source.\r\n\r\n");
    exit();
}

/* Else, it's truly ipv4. Now we obtain the server ip address,
   based on server id. So change your server id accordingly.
   Exit if the server public ip address matches i.e. the same.  */

$query_ip = mysqli_query($ip_updater, 'SELECT ip_address FROM server_ip WHERE server_id =1');
list($stored_ip) = mysqli_fetch_row($query_ip);
if($public_ip == $stored_ip) {
    printf("\r\nThe server and its public ip addresses match. \r\nNo changes is deemed necesary.\r\n\r\n");
    exit();
}

/* Else, update database with the new ip address */

$update1 = mysqli_query($ip_updater, "UPDATE dns_rr SET data = replace(data, '$stored_ip', '$public_ip')");
$update2 = mysqli_query($ip_updater, "UPDATE server_ip SET ip_address = replace(ip_address, '$stored_ip', '$public_ip')");
$query_new_ip = mysqli_query($ip_updater, 'SELECT ip_address FROM server_ip WHERE server_id =1');
list($new_stored_ip) = mysqli_fetch_row($query_new_ip);

if ($new_stored_ip != $public_ip) {
    printf("\r\nDatabase updates failed! \r\nDatabase updating code may need a fix or update. \r\n\r\n");
    exit();
}

/* Next we change soa zone files with the new ip address
   i.e. only when the above updates are successful */

foreach (glob('/etc/bind/pri.*') as $filename) {
        $file = file_get_contents($filename);
        file_put_contents($filename, preg_replace("/$stored_ip/", "$public_ip", $file));

        /* Exit if SOA zone files are not updated */
        foreach(file($filename) as $fli=>$fl) {
                if(strpos($fl, "/$stored_ip/")!==false) {
                        printf("\r\nSOA zone files updates failed! \r\nZone files updating code may need a fix or update. \r\n\r\n");
                        exit();
                }
        }
}

/* Now resync so that above changes updated properly. Important!
   Do refer to http://ipupdater.sch.my and download or copy
   resync.php to ipu_resync,php and app.inc.php to
   ipu_app.inc.php. Disable admin check and tpl in ipu_resync.php
   and start_session() in ipu_app.inc.php and change require
   once in ipu_resync.php to ipu_app.inc.php. */

require_once 'ipu_resync.php';

/* Lastly, congratulations! All updates are successful.
   Log it then close database connection and restart apache. */
printf("\r\nDatabase and SOA zone files updates are successful! \r\nThanks God. Closing connection and restarting apache. \r\n\r\n");
mysqli_close($ip_updater);
exec('service apache2 restart');
?>
Last Edit: August 03, 2016, 11:22:12 pm by ahrasis