PHP NSUpdate Update of an TXT String - php

iam using NSUpdate to update DNS Records from PHP
all things working, but somthing going wrong when i want update/Add TXT Record
adding an Text Record is not so hard like SRV OF SPF
nope :) rlly
The Problem:
trying to add somthing like "This IS an TXT record"
after sending it from PHP to NSUPDATE (works with other Records)
Bind DNS Server get this :
Domain IN TXT "This" "IS" "an" "TXT" "Record"
but this is wrong ! inspected my code, looked arround..it dosent work ..
code :
<?php
if ($txt_true) {
$ttl2 = '36';
// TXT string could be almost anything, just make sure it's quoted.
// $txt_var = str_replace("'", "", $txt_var);
// $txt_var = str_replace('"', "", $txt_var);
//$txt_var = '"'. $txt_var.'"';
$data .= "update add ".$sub.".".$domain.". ".$ttl2." IN TXT ".$txt_var."\n";
}
this going to NSupdate ( data + nsupdate etc...) dotn follow block that is out commented
here:
function dns_update ($id_domain,$mydata) {
global $nsupdate;
if (domain_exists($id_domain)) {
$infos = domain_infos($id_domain);
$domain = $infos['domain'];
$dnsip = $infos['dnsip'];
$key = $infos['key'];
if ($key != '') { $add = ' -y '.$key; }
$data = "server $dnsip\n";
$data .= "zone $domain\n";
$data .= $mydata;
$data .= "\n";
$result = `echo "$data" | $nsupdate$add`;
return $result;
} else {
return false;
}
}
and Quote TXT Record happens from
HTML => INPUT => DO => Wirte DB & Function => NSUPDATE

You need quotes around the text data. If it contains any embedded quotes, they need to be escaped:
$txt_var = str_replace('"', '\"', $txt_var);
$data .= "update add $sub.$domain. $ttl2 IN TXT \"$txt_var\"\n";
Since this string contains double quotes, you should use single quotes in your echo statement:
$result = `echo '$data' | $nsupdate$add`;
However, a much better idea would be to use an API rather than doing this through the command line. See the Net_DNS2 library.

Related

Rename folder which contains special characters using PHP

I have the following names in my database and also in directories.
Zúñnga
Mariè
etc...
The problem is I tried to do this:
$oldname = '/home/website/public_html/profile/Zúñnga';
$newname = '/home/website/public_html/profile/Zuunga';
rename($oldname, $newname)
And it doesn't work...
The names are stored in a database.
This is the following script I've been using to get the names out of the database, convert them to UTF-8, but simply the rename function doesn't work...
...
foreach($query as $item)
{
$name_tags = utf8_encode($item['name_tags']);
$oldname = '/home/website/public_html/profile/'.$name_tags.'';
$text = iconv('UTF-8','ASCII//TRANSLIT', $name_tags);
$text_1 = preg_replace(array('/\s{2,}/', '/[\t\n]/'), ' ', $text);
$text_2 = preg_replace("/[^A-Za-z0-9- ]/", '', $text_1);
$name_tags_updated = str_replace(" ", "-", $text_2);
$newname = '/home/website/public_html/profile/'.$name_tags_updated.'';
rename($oldname, $newname)
$sql = 'UPDATE posts SET name_tags = "'.$name_tags_updated.'" where id = "'.$item['id'].'";
...
}
The query part is working fine; it's replacing the name with the good letters in the database, but when it comes to renaming the directory it simply doesn't work. Instead of changing the name to it's actual file, it just doesn't. I've tried to verify it this way
if(rename($oldname, $newname))
echo 'success';
else
echo 'fail';
it returns fail
On my website sometimes they are returned like this:
Mari�

How to use string variable that have special character "#" in select query statement mysql php

<?php
include('dbLink2.php');
$quizqr = $_GET['quizQR'];
$recordsID1 = $_GET['recordsID1'];
$recordsID2 = $_GET['recordsID2'];
$m_array1=array();
$m_array=array();
$sql = "SELECT quizQR, recordsID FROM `registertestactivity` WHERE (quizQR = '$quizqr' OR recordsID = '$recordsID1' OR recordsID = '$recordsID2') LIMIT 1";
$result = #mysqli_query($link, $sql) or die();
if (#mysqli_affected_rows($link) > 0) {
while($row = #mysqli_fetch_assoc($result))
{
$m_array[]=$row;
}
} else {
$m_array1 += ["quizQR" => "NoRecords"];
$m_array1 += ["recordsID" => "NoRecords"];
$m_array[0] = $m_array1;
}
echo json_encode($m_array);
#mysqli_free_result($result);
#mysqli_close($link);
?>
Can someone help me out, i have tried the mysqli_real_escape_string and it still doesnt work :(
The $quizqr value has a '#' character in the string and this is the error msg that pops when the ajax call this php:
Because you have a # in the URL you're dealing with a URL Fragment which means that everything past the # is not available in the query string. PHP offers a flag, PHP_URL_FRAGMENT for its parse_url() function which can help you get what you need from the string.
Here is one example using the URL you provided:
$fragment = parse_url($url, PHP_URL_FRAGMENT);
echo $fragment;
$fragmentSection = explode('&', $fragment);
print_r($fragmentSection);
foreach($fragmentSection AS $section) {
if(0 != strpos($section, '=')) {
$sectionParts = explode('=', $section);
$queryParts[$sectionParts[0]] = $sectionParts[1];
}
}
print_r($queryParts);
This ultimately returns two array members which could then be used in your query:
Array
(
[recordsID1] => records_001
[recordsID2] => records_002
)
The best thing to do would be to write a function to which you pass the URL to return the elements you need.
Keep in mind that this is not fool-proof. If the URL is in a different format then what I have done here will have to be modified to work as you would like it to.
Additionally you have been given some warnings and guidance in the comments you should follow to keep your code safe and efficient, so I will not repeat them here.

Posting Data To Mysql with IDs

I have a form with some input texts. It's counted with the name + an id. Like:
megnevezes_1
megnevezes_2
My form has also a counted id tag, called tid_1 and go on.
When I post my form i made a hidden input called darab, which counts how many id's I have.
Then I do the mysql query:
for($k=1; $k=$darab; $k++){
$command = <<<HTML
UPDATE
$dbtablename_template_tetelek
SET
vamtarifa_szj = '$vamtarifa_szj_$k',
megnevezes = '$megnevezes_$k',
me_egyseg = '$me_egyseg_$k',
mennyiseg = '$mennyiseg_$k',
afa = '$afa_$k',
egyseg_ar = 'str_replace(".","",$egyseg_ar_$k)'
WHERE template_id = '$tid_$k'
HTML;
mysql_query($command,$kapcsolat) or die(mysql_error(). $command);
}
But theres something wrong with it. How to attach to my strings the $k string with _? And how to make the str replace in the query?
Try this..
for($k=1; $k=$darab; $k++){
$blah = $egyseg_ar . '_' . $k;
$replace = str_replace(".", "", $blah);
$command = <<<HTML
UPDATE
$dbtablename_template_tetelek
SET
vamtarifa_szj = '$vamtarifa_szj_$k',
megnevezes = '$megnevezes_$k',
me_egyseg = '$me_egyseg_$k',
mennyiseg = '$mennyiseg_$k',
afa = '$afa_$k',
egyseg_ar = $replace
WHERE template_id = '$tid_$k'
HTML;
mysql_query($command,$kapcsolat) or die(mysql_error(). $command);
}
You should let PHP know precisely what it needs to parse. To help it you can use curly brackets like in '{$var1}_{$var2}'
So e.g. mennyiseg = '$mennyiseg_$k' might need to be mennyiseg = '{$mennyiseg}_{$k}' if you already have a variable named $mennyiseg in your code above the loop.

Insert data from custom PHP function into MySQL database

I'm having problems inserting a particular line of code into my MySQL database. It inserts three rows just fine, but the "html_href" row isn't going in for whatever reason. Here is my code:
function html_path() {
$title = strtolower($_POST['title']); // convert title to lower case
$filename = str_replace(" ", "-", $title); // replace spaces with dashes
$html_href = $filename . ".html"; // add the extension
}
And my MySQL query code:
$query = "INSERT INTO work (title, logline, html_href, synopsis) VALUES";
$query .= "('".mysql_real_escape_string($_POST['title'])."',";
$query .= "'".mysql_real_escape_string($_POST['logline'])."',";
$query .= "'".html_path()."',";
$query .= "'".mysql_real_escape_string($_POST['synopsis'])."')";
$result = mysql_query($query);
The title, logline, and synopsis values go in just fine, but the html_href() function inserts a blank row.
It looks like your html_path() function isn't returning anything.
Try:
function html_path() {
$title = strtolower($_POST['title']); // convert title to lower case
$filename = str_replace(" ", "-", $title); // replace spaces with dashes
$html_href = $filename . ".html"; // add the extension
return $html_href;
}
Your html_path() does not return the $html_href variable. Add
return $html_href;
before you close it, and it should work perfectly.

mysql real escape on array that contains objects?

I have an array that I need to sanitize before putting it in a cell on a mysql database. The code i'm trying seems to work. But as soon as there are characters like ' it throws errors and thats not good. Here's what i've tried, any ideas whats wrong?
function submitLogDb($array,$id,$title)
{
function mysql_real_escape_array($var)
{
foreach($var as $line)
{
mysql_real_escape_string($line['msg']);
}
return $var;
}
$title=mysql_real_escape_string($title);
$array=mysql_real_escape_array($array);
return mysql_query("INSERT INTO logs (text,id,title) VALUES ('".serialize($array)."','$id','$title')");
}
EDIT:
Just incase it helps, heres what some of the objects might look like in the array:
[1]
icon = ""
msg = "this is a test"
name = "Them: "
systemMsg = 0
[2]
icon = ""
msg = "yep it sure is"
name = "You: "
systemMsg = 0
mysql_real_escape_string the output of serialization of the array.
$data_to_insert = mysql_real_escape_string(serialize($array));

Categories