Not able to access POSTed variables using /dev/stdin in bash - linux

How can I access values sent with the help of xmlHttpReq.send in bash?
In perl I can access data sent from an html/javascript file as:
#!/usr/bin/perl -w
use CGI;
$query = new CGI;
$secretword = $query->param('w');
print $query->header;
print "<p>The secret word is <b>$secretword</b></p>"
I am trying to access 'w' in a sh script but reading from /dev/stdin is not working. Nothing gets displayed in browser.
#!/bin/sh
echo "Content-type: text/html"
echo ""
echo $(</dev/stdin)
How can I access the data sent via POST in sh?
Here is the html/javascript file being used:
<html>
<head>
<title>Ajax Example</title>
<script language="Javascript">
function xmlhttpPost(strURL) {
var xmlHttpReq = false;
var self = this;
if (window.XMLHttpRequest) {
self.xmlHttpReq = new XMLHttpRequest();
}
else if (window.ActiveXObject) {
self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
}
self.xmlHttpReq.open('POST', strURL, true);
self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
self.xmlHttpReq.onreadystatechange = function() {
if (self.xmlHttpReq.readyState == 4) {
updatepage(self.xmlHttpReq.responseText);
}
}
self.xmlHttpReq.send(getquerystring());
}
function getquerystring() {
var form = document.forms['f1'];
var word = form.word.value;
qstr = 'w=' + escape(word);
return qstr;
}
function updatepage(str){
document.getElementById("result").innerHTML = str;
}
</script>
</head>
<body>
<form name="f1">
<p>word: <input name="word" type="text">
<input value="Go" type="button" onclick='JavaScript:xmlhttpPost("./cgi-bin/test_ajax_bash.sh")'></p>
<div id="result"></div>
</form>
</body>
</html>

Use read shell builtin command to read lines from standard input:
#!/bin/sh
echo "Content-type: text/plain"
echo
while read line; do
echo $line
done
Also you don't need "" for echoing new line. And also you can use text/plain mime type if you output a plain text instead of html.

First step : make sure your actually seeing something by echoing on standard output :
echo "Content-type: text/html"
echo ""
echo "Some HTML code"
Then use cat
echo "Content-type: text/html"
echo ""
cat
Instead of debugging in the browser, you can also sen the sript output in a file.
echo "Content-type: text/html"
echo ""
cat > post_data.txt

Related

Showing Error Message

When I run this program and enter Dan Dan inside of it, it works. Now when I enter http://sftpgamblerlotteryclub/www it doesn't work. I would like for it to catch the sftp, /, www and return back to the form and inform the user " this is not a username, please resubmit". Thanks you.
<?php
// define variables and set to empty values
$nameErr = $emailErr = "";
$name = $email = $subject = "";
if ($_SERVER["REQUEST_METHOD"] == "POST"){
if (empty($_POST["name"])) {
$nameErr = "Name is required";
} else {
$name = test_input($_POST["name"]);
// check if name only contains letters and whitespace
if (preg_match("/^[a-zA-Z-']*$/",$name)) {
$nameErr = "Only letters and white space allowed";
}
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<html>
<head>
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Contact Form</title>
</head>
<body>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Name:
<input type="text" name="name">
<span class="error">* <?php echo $nameErr; ?> </span>
<br><br>
<input name="submit_btn" type="submit" id="submit-btn" value="Send Mail">
</form>
</div>
</body>
</html>
<?php
echo $name;
?>
Your test_input function is striping out the / so that is removed before you do your preg_match. As such you will never catch the /.
If I understand your question you want to make sure the user name is not in a list of prohibited names. For that I would create an array of prohibited names and then do a stripos on the json_encoded output of the array within a if condition. This makes it easy to expand the list of prohibited names.
This is based on your code snip-it.
$array = ["sftp", "http", "www"];
if (stripos(json_encode($array),$name) !== false) {
$nameErr = "This is not a valid user name"
}

Multiple URLs in manifest.json file

I am trying to build a Chrome plugin. In the main folder, I have a popup.html which runs by default and uses the following syntax in manifest.json
"browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
},
the popup.html is working absolutely fine
what is my popup.html is doing?
It is inputting email from the user and storing it in local phpmyadmin.
Following is the code of popup.html
<!doctype html>
<html style="min-width:350px;">
<head>
<title>Welcome</title>
</head>
<body>
<h3> Enter email </h3>
<form action=”info.php” method=”post”>
Enter email: <input type=”email” name=”email” />
<input type="submit" value="Submit" >
</form>
</body>
</html>
The form action is linked to info.php where the php connects the database and inserts the data into the table in phpMyAdmin.
Following is the info.php code
<html>
<body>
<?php
$con = mysql_connect('127.0.0.1','root','');
if (!$con)
{
echo'Could not connect to the server';
}
if (!mysqli_select_db($con,'test'))
{
echo 'Database Not Selected';
}
$Email = $_POST[email];
$sql = "INSERT INTO test_table(Email) VALUES ('$Email')";
if(!mysqli_query($con,$sql))
{
echo 'Could not add to database';
}
else
{
echo 'Thank you the data is added';
}
header("refresh:2; url=popup.html");
?>
</body>
</html>
What problem am I facing?
After I enter the email in the input field it gives an error that Your file was not found It may have been moved or deleted.
ERR_FILE_NOT_FOUND
Maybe I am getting this error because info.php has to added in the manifest file? If this is the problem then how can I add multiple urls in the manifest.json file?
Your header won't work, since you have already echoed output.
Instead of outputting immediately, stick the output into an $html variable.
This might not completely fix your issue? But it will fix the header.
<?php
$html = '';
$con = mysql_connect('127.0.0.1','root','');
if (!$con)
{
$html .= 'Could not connect to the server';
}
if (!mysqli_select_db($con,'test'))
{
$html .= 'Database Not Selected';
}
$Email = $_POST[email];
$sql = "INSERT INTO test_table(Email) VALUES ('$Email')";
if(!mysqli_query($con,$sql))
{
$html .= 'Could not add to database';
}
if (empty($html)) {
header("refresh:2; url=popup.html");
}
echo $html;
with #wOxxOm help the problem was solved.
In the popup.html instead of giving info.html directly, it would have been through the server such as http://localhost/foldername/info.php

How to stop my telegram bot replying to a message multiple times

My bot reply's to a message repeatedly how can I stop it? It is reading data from /getupdates. I need some sort of reply sent variable but do not know how to implement it
<?php
$page = $_SERVER['PHP_SELF'];
$sec = "60";
$bottoken = "218567218:AAGQMx6lYCOhRapUXxIG5b0EkXTQOJ5y3uw";
$website = "https://api.telegram.org/bot".$bottoken;
$update = file_get_contents($website."/getupdates");
$updatearray = json_decode($update, TRUE);
$length = count($updatearray["result"]);
$chatid = $updatearray["result"][$length-1]["message"]["chat"]["id"];
$text = $updatearray["result"][$length-1]["message"]["text"];
if($text == 'hi'){
file_get_contents($website."/sendmessage?chat_id=".$chatid."&text=hello");
}
elseif($text == 'bye'){
file_get_contents($website."/sendmessage?chat_id=".$chatid."&text=piss off");
}
?>
<html>
<head>
<meta http-equiv="refresh" content="<?php echo $sec?>;URL='<?php echo $page?>'">
</head>
<body>
</body>
</html>
You can use memcache in PHP or simply use a database.
Store the last update_id in memcache or your database. That way you know what message has already been answered. For the next time you can get the last update_id, increment it, and then get new message for response.
According to Telegram api document, You can pass last update_id+1 as offset param to getUpdates.

Pass values from url to another redirected url

zero.php
<?php
$q = $_GET['id'];
echo = $q
?>
this code is not working, Help me!
I am not using .htaccess in this.
Try this.
<?php
$q = $_GET['value'];
echo $q;
Your syntax is significantly off.
Have you tried?
<?php
$q = $_GET['id'];
echo $q;
?>
and of course this is assuming that there is a GET parameter called id it is being passed so a even better solution would be to check to see if it is defined first.
<?php
if (isset($_GET['id'])) {
$q = $_GET['id'];
echo $q;
} else {
echo "ID PARAMETER NOT SET";
}
?>

phpmailer for php variables

I am running phpmailer and very much new to it.
Problem definition: Not able to see php variables data in the received email while html content can seen properly.
Below is some of the code:
require 'PHPMailer/class.phpmailer.php';
$mail = new PHPMailer;
$mail->WordWrap = 50; // Set word wrap to 50 characters
$mail->IsHTML(true); // Set email format to HTML
$mail->Subject = 'Message';
$mail->Body = '<body>
<div align="center"><p7><strong>HELLO WORLD</strong></p7></div>
<h9><u>Details</u></h9><br/>
<h9><strong>NAME:</strong> <?php echo "$name";?> <?php echo "$place";?>
</body>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->Send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}
echo 'Message has been sent';
Not able to see data defined under php.
Also $name and $place is the dynamic data for the every mail.
Please help.
You cannot put php statements in single quotes. use the following instead:
$mail->Body = '<body>
<div align="center"><p7><strong>HELLO WORLD</strong></p7></div>
<h9><u>Details</u></h9><br/>
<h9><strong>NAME:</strong>' . $name . ' ' . $place . '
</body>';
You need to put the text between " instead of '.
PHP only replaces variables in double quoted strings.
Also you shouldn't use php code in the strings.
$mail->Body = "
<body>
<div align=\"center\"><p7><strong>HELLO WORLD</strong></p7></div>
<h9><u>Details</u></h9><br/>
<h9><strong>NAME:</strong> $name $place
</body>
";
try instead :
$mail->Body = "<body>
<div align=\"center\"><p7><strong>HELLO WORLD</strong></p7></div>
<h9><u>Details</u></h9><br/>
<h9><strong>NAME:</strong>$name $place
</body>";
You don't have to put php tags in your string since you are already in a php context !
Replace Body with :
$mail->Body = '<body>
<div align="center"><p7><strong>HELLO WORLD</strong></p7></div>
<h9><u>Details</u></h9><br/>
<h9><strong>NAME:</strong>'.$name.' '.$place.'</body>';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$message .= "Phone: 0181-4606260.<br>";
$message .="Mobile :09417608422.<br>";
Send_Mail('abc#example.com','Welcome',$message,$headers);
try this...

Resources