php decoder function not working? - php

I have some data in raw email format with base64 encoding, but when I put that encoding into a string variable, and then pass it to the echo base64_decode($str) function, it prints out the above.
Here is a snippet of my code. I didn't post the entire base64 encoding string for obvious reasons.
$str = "JVBERi0xLjcKJeTjz9IKNCAwIG9iago8PC9MZW5ndGggMyAwIFIvRmlsdGVy
L0ZsYXRlRGVjb2RlPj4Kc3RyZWFtCnicvVndb9s2ECdgYB28AlnQFGv6ARhY
mg8gVUXxQ1K3vQyO46YGHDt23Dbe07YOGJqH5nF//X53pGXJllzZ8QbZEilK
d7873h2PJ9kKcbySOMVp1Pr9thkGkUILFx1LvijZuvur+aUp+VnZ0iH98Oiv";
echo base64_decode($str);

Related

How to encode a string in base64 to be used in a URL?

I need to attach a confirmation code that can hold several parts and almost random characters, to an email.
The idea is to print the URL with that code in the message body (in HTML).
Is the base64_encode() function enough to make it safe to be parsed by the browser ?
Base64 encoding is to take binary data and make it suitable for being treated as text.
That isn't your problem (you said you had random characters) so you shouldn't use Base64.
You have text and want to insert it into a URL. You need to URL encode it. That is what the urlencode() function is for.
You then want to insert that URL in an HTML document. That is what the htmlspecialchars() function is for.
$data = function_to_get_random_data();
$url_safe_data = urlencode($data);
$url = "http://example.com/$url_safe_data";
$html_safe_url = htmlspecialchars($url);
$html = "$html_safe_url";

print cyrillic string from json php

I receive json answer from another script. Next I used $json = json_decode($json) and die(json['message']) for show specific string, and this value contains Cyrillic data.
Function mb_detect_encoding() shows that string in UTF-8.
Ok, I use charset="utf-8" in html file, but
I see this output "Пользователь с этим адресом электронной почты уже существует" in my browser.
I used mb_convert_encoding(json['message'], 'UTF-8'), without any effect/
Only var_dump($json) shows me decoded string.
Maybe I wrong to access data in json?
Use mb_convert_encoding(json['message'], "utf-8", "windows-1251"); to properly convert string.

Decode large string base64

I have to decode a large string base64 encoded:
<?php
$str ='base64code';
echo base64_decode($str);
?>
The link contains the base64 encoded string: http://www.interwebmedia.nl/dataxi/base64.txt
Online decoders give the right result but this php function doesn't. Is there a solution?
base64_decode outputs exactly what was encoded before. It does not adapt any contained values.
You are writing everything out in HTML context. And there any <tags> will not be shown in the browser window. Use show source. Or htmlspecialchars.

Escaping in base64 encode php

I'm trying to encode my script using base64 in php, but why my code automatic adds backslashes \
at single quotes ' or double quotes "
this is the code I am using to encode
$encode = $_POST ['encode'];
$encod = base64_encode ($encode);
echo "<form action='?' method='post'><textarea name='encode'></textarea><input type='submit' value='Encode'></form><textarea> ".$encod." </textarea>";
I use code at above, then I try to encode this script:
echo "just test";
echo 'or just test';
and result
PD9waHAgZWNobyBcImp1c3QgdGVzdFwiOw0KZWNobyBcJ29yIGp1c3QgdGVzdFwnOyA/Pg==
when I decode, result
echo \"just test\";
echo \'or just test\';
how to delete backslashes??
I've tried using str_replace on $encod, and stripslashes($encod) but it does not work.
I have tried decoding your base64 encoded string and it yielded the same result. I tried encoding it again, slashes and all, and it yielded the same encoded string.
The problem is not that you are somehow getting unknown slashes out of the decoding process, but that the slashes exist there before you ever encoded the string.
The same string without slashes is this:
PD9waHAgZWNobyAianVzdCB0ZXN0IjsNCmVjaG8gJ29yIGp1c3QgdGVzdCc7ID8+
Try decoding it.
I believe that the real solution to your problem can be found here:
Why are $_POST variables getting escaped in PHP?
I believe the string you are encoding in base64 was already encoded in json (because the POST has to be done with a json string, I assume).
Json's way to escape the quotes is what you describe to be your problem:
See: json_encode PHP
So the solution here would be to first decode from base64 (base64_decode [PHP]) then decode from json (jsoN_decode() [PHP]) or (JSON.parse() [JS])

PHP quoted_printable_decode not working on Fandango plain/text emails

I'm working to clean up emails before they get stored in a database. A fandango email was sent as being encoded as 4 (quoted-printable). Here is part of the message without decoding...
=0A=0A=A0=0AJohn=0A(800) 123-4567=0A=0A----- Forwarded Message =
=20=0ASent:=20Thursday,=20July=204,=202013=204:14=20PM=0ASubject:=20Your=20Despicab=
le=20Me=202=20iTunes=20Download=0A=20=0A=0A=0ADespicable=20Me=202=20=0A=20=20=0A=20Your=20purchase=20=
of=20tickets=20for=20Despicable=20Me=202=20has=20earned=20you=20a=20complimentary=20download=20of=20t= he=20song=20'Just=20a=20Cloud=20Away'=20by=20Pharrell=20from=20the=20Original=20Motion=20Picture=20So=
undtrack=20on=20iTunes.=20=0AWe=20hope=20you=20enjoy=20the=20song=20and=20the=20film!=0AIf=20you=20ha=
ve=20iTunes=20installed,=20click=20here=20to=20start=20your=20complimentary=20download.=0AIF=20=
YOU=20DO=20NOT=20HAVE=20iTunes=20INSTALLED:=0A=0A1.=20Download=20iTunes=20for=20Mac=20or=20Window=
s,=20free=20of=20charge=20at=20www.iTunes.com.=20=0A2.=20Open=20iTunes=20and=20click=20iTunes=20Sto=
re.=20=0A3.=20Click=20Redeem=20under=20Quick=20Links.=20=0A4.=20Enter=20the=20code=20below.=20Your=20= download=20will=20start=20immediately.=20Enjoy.=20=0ADownload=20Code:=20FML6H34XXTMJ=20=0AC=
But when I use quoted_printable_decode() on the variable it produces no text.
This url has a decoder that works, albeit in ASP/VB...
http://www.motobit.com/util/quoted-printable-decoder.asp
I'm guessing the code here is relevant...
http://www.motobit.com/tips/detpg_quoted-printable-decode/
It decodes the quote-printable HTML above correctly. Hopefully this will help someone trying to help me. I'm sure I'm not the only one to encounter broken quote-printable email messages.
It looks like there are some spaces in the quoted-printable encoded string that you posted. That's probably what's causing the problem - if it's truly quoted-printable, than the encoded string should not contain any spaces. Spaces are =20 in quoted printable. If you use a replace function (e.g. PHP's str_replace) to replace the spaces in the encoded string with =20, then you get the following quoted-printable encoded string:
John=0D=0A(800)=20123-4567=0D=0A=0D=0A-----=20Forwarded=20Message=20
Then, this string can be decoded using PHP's quoted_printable_decode() function.
If you copy the quoted-printable encoded text above to a file, then run the following PHP script (which reads the quoted-printable text from the file, gets rid of the spaces using the str_replace function, then decodes the quoted-printable text using the quoted_printable_decode function), you should see that it produces the correct decoded output:
<?
$filename="./qp.txt";
$file = fopen($filename,"r");
$qp = fread($file,filesize($filename));
fclose($file);
$qp=str_replace(" ", "", $qp);
print "<plaintext>";
print quoted_printable_decode($qp);
?>

Categories