PHP giving a trailing "=" on each line after reading from stdin - php

The contents of stdin is getting corrupted with word wrapping and trailing "=" throughout which obviously breaks the URL that I need to post.
I need to extract a URL/link from an email then post the URL. So, I'm piping my email to a php script in cpanel using a standard code snip I've seen all over the internet:
$fd = fopen("php://stdin", "r");
$email = ""; // This will be the variable holding the data.
while (!feof($fd)) { $email .= trim(fread($fd, 1024)); }
fclose($fd);
Then dumping the contents of the email to a file "pipemail.txt" for now to inspect it and make sure it's all working properly.
$fdw = fopen("pipemail.txt", "w+");
fwrite($fdw, $email);
fclose($fdw);
The output is looking like this:
...
<table style=3D"width:100%" cellpadding=3D"0" cellspacing=3D"0" border=3D"0=
"><tbody><tr><td><table style=3D"background-color:#ffffff;color:#3c445a;fon=
t-family:arial;font-size:10px;font-weight:bold;width:100%" cellpadding=3D"0=
" cellspacing=3D"0">
...
I have been working on this for over a day now and I'm completely stumped. I've tried trimming the trailing "=" from incoming lines and it does not give me the expected result. Instead it seems to remove random "=" from seemingly random locations in the content. I am guessing that it is not random but it only seems so because it's not what I expect. It's probably only removing it if it happens to be the last character of the 1024 k/char line but if that is true then where else is the wordwrapping coming from? I don't know enough about how this works to trouble shoot this myself.
Why is it wrapping? Where are the "=" coming from? Does anyone have any suggestions?

Emails are commonly encoded in the quoted printable format (http://en.wikipedia.org/wiki/Quoted-printable)
You can decode it using quoted_printable_decode() - this is done automatically by your email client, which is why it looks like php is adding those character.
http://www.php.net/manual/en/function.quoted-printable-decode.php

from an email
and
trailing “=” on each line
means that the email is probably quoted-printable encoded. You need to parse the message properly and run the body part through quoted_printable_decode().
There are Content-Type headers that will tell you what kind of encoding is used, and probably MIME parts and boundaries that you'll need to deal with.

Your data is in quoted-printable format, use quoted_printable_decode to decode it:
$email = quoted_printable_decode(file_get_contents("php:://stdin"));

Related

Clear angle quotes on phpMalier from parameter

My message on from parameter comming with angles quotes, like that:
MyFromName <New message!>
I try $mail->ClearAllRecipients(), but not work.
Any tips?
Nope. You can't do this. It's part of the email specification, which says a mailbox (which is the thing you're talking about) is made up of:
mailbox = name-addr / addr-spec
name-addr = [display-name] angle-addr
angle-addr = [CFWS] "<" addr-spec ">" [CFWS] /
obs-angle-addr
That means an address can be in one of two forms when it appears in a header; as a name and address:
User Name <user#example.com>
Or as just an address:
user#example.com
Nearly everything uses the former, and usually if the name is given, that is displayed in preference to the address part, however, that is entirely up to the client application you're using to display the message, over which you have no control at all.
If you remove the angle brackets and keep the name, your message will never arrive because it's an invalid format.
You can use a replace function, for example;
function removeAngleBrackets($from)
{
// Create an array of things to replace
// Use both standard and html encoded to cover basis (you could use the ASCII too)
$replace = array("<", ">", "<", ">");
// Create the variable to replave the strings with
$replace_with = "";
// Replace this in the string and return the value when done
return str_replace($replace, $replace_with, $from);
}
Calling this on your string will remove the angle braces

Opening an encoded file with PHP

I am opening a file on the server with PHP. The file seems ordinary. It opens in Notepad and Textedit on a PC. Even PHP can display it without any issue in a web browser when we echo out.
But when I try searching it with strpos() it can’t find anything except single characters. if i search for a string with 2 or more characters, it doesn’t find anything.
I have tried encoding it to UTF-8, and it detects it as ASCII. so everything seems right there.
I have also isolated the part of the file that I am trying to read down to only 250 characters. They all look fine on the screen.
But strpos can’t find it. I’ve run tests on every part of my code and I believe everything is fine with my code. The problem I believe derives from that the characters I see on the screen are not exactly matching what those characters really are.
My last resort is to write a function which converts each character into an integer array (if that’s even possible), and then convert all that back to a string. This way, we’ll know 100% that the characters we see are real.
Hoping that somebody has a better approach or perhaps an idea for something I missed?
I'll post the code below:
$content = file_get_contents($file->getPathname()); // get the file contents
$content = substr($content, 30, 300); // reduce the large file to just the first few lines
$content = htmlspecialchars($content); // try to remove any special characters from the file
$content = iconv('ASCII', 'UTF-8//IGNORE', $content); // encode to a friendly format
$string = "JobName"; // this is the string i'm searching for
if (strpos($content, $string) !== false) {
echo "bingo";
}
else {
echo " not found ";
}
Just to be clear, the file I'm opening is generated from a PC program that stores its data in .DAT format. Like I said, I can see and read the content very easily using any program, including PHP. but when I try to search, its as if it doesn't recognize the content at all.
I am not aware of how to upload a file on StackOverflow, but if someone can tell me how to do it then I will gladly post the file itself.
Thank you very much for your help ARKASCHA. I was able to find an online HexEditor and when I saw the characters, it seems there is a NUL character between every single character in this file. that's probably why I couldn't see it with a regular view. I just had to run an additional function to remove NUL characters from the file, and then it works as its supposed. Thanks again.

After Encodeing json i have and extra whitespace, how can i remove that?

i have the following array whitch i want to encode to jSon:
$output = array("success" =>0,"msg" => "The e-mail address is already in use");
I am useing the following method to encode:
echo json_encode($output);
As a result I get back the following string:
{"success":0,"msg":"The e-mail address is already in use"}
I seems perfectly okey but I have an extra whitespace at the end witch prevent me for useing the jSon string any further. Can anyone help me how to remove this whitespace from the end?
Likely you have some content after an ending PHP tag. As an example:
<?php
$output = array("success" =>0,"msg" => "The e-mail address is already in use");
echo json_encode($output);
?>
\n
Now I cant represent a new line in the code block but assume the \n is a line return. Now after the PHP tags it will echo the json, and then after PHP tags it echos any content such as that new line. You can however just remove the end tag. This is also true of the open tag and any content there will likewise be output, but that one is much easier to spot if its at the top of the file.
Or at least that's my best guess if you have {...}\n as your json output.
It's an extremely easy mistake to make that can be hard to spot. It may seem strange, but it does happen like this.

\r\n showing up in <textarea> after pulled from database

the data is captured from a textarea to begin with.
Line1
Line2
Line3
etc
It is sent through this function before being stored in the DB (i'm open to better solutions, but if PDO is one of them, I don't understand it and have yet to get it to work)
function test_input($data) {
global $conn;
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
$data = mysqli_real_escape_string($conn, $data);
return $data;
}
Which is my way of preventing injections (not my way but a way I found that has worked great until now its giving me this problem with line breaks and textareas)
I try to extract the data from the DB and display it in a textarea, and it shows \r\n instead of doing a line break. It is stored in the DB with the line breaks (i do not see the \r\n but instead i see the data on a new line)
I've tried nl2br, i've tried html_entity_decode, i've tried str_replace \r\n to br (and then it just shows br literal instead of \r\n).
from the research I've found on this site, its the stuff i'm doing to it before i store it in the DB that is causing this but none of the solutions have worked for me.
help.
Replace the \r\n in the text with
before putting it into to the textarea and showing it to the user.
It worked for me.
Try This May be help full
<?php
function nl2br2($string) {
$string = str_replace(array("\r\n", "\r", "\n"), "<br />", $string);
return $string;
}
?>
Html versus php and windows (carriage return and newline). Le
rning what happens
when you have a field of values in a buffer. A buffer can be coming from or going to an input/output device
What is inside the buffer may need to be substituted for a string appropriate to the device.
Pulling data from a DB line by line or from a SQL api query will require a regular expression and substitution operation repeating until all
expressions are changed. Bullet proofing the input before it goes into the DB field is always a good idea.
I encountered a print problem that was caused by extra data (escape sequences) that caused the printers
to stop and wait for a reset sequence. No one understood why print jobs failed to print for something like
12 months. I wrote a filter and added it to the printers interface. PRoblem solved

String too long | using chunk_split

I have a heredoc var. in php and sending it via POST to an mail.php - everything worked fine, but since web.de and gmx.net are such tools they won´t accept my mails "String too Long 500..." - using chunk_split solved the problem:
$text1 = chunk_split($text1, 1212,"\r\n");
But now the layout is screwed up sometimes. So instead of Hello there...
It makes an empty space Hello t here...
Any ideas?
It depends on the type of content.
If your content is HTML, one way to handle it is to run the content through tidy::repairString.
If your content is text (without a structure and formatting language), breaking long lines during mail transfer is one of the things quoted-printable encoding does. Run your content through that. Send your message with a header named Content-Transfer-Encoding with value QUOTED-PRINTABLE. Mail clients will automatically put the content back together before displaying it.

Categories