I'm trying to compile a python script and record any syntax errors
I have a script called test.py that looks like
#!/usr/bin/python
import py_compile
try:
py_compile.compile("answer.py")
except py_compile.PyCompileError as e:
print(e)
answer.py is just a self contained script that doesn't do anything except print something(for now)
i also have a php exec command in a php file that runs test.py, problem is php exec doesn't get returned anything by
print(e)
I'm guessing because e here in PyCompileError isn't a string or something weird with the format, the result simply comes back empty "". When I played around with it it seems to be an object, not sure how to convert that to a string.
I tried putting it into a string but it's not working, if I change e into a string like print("You have errors") I get the correct output, so it's not my php but I want the syntax errors on the lines. Not even sure if I should be using compile() for this
I figured it out, basically I needed doraise=True and then get print(e.exc_value)
I did initially have doraise=True before people think I was lazy and not reading the documentation, but I didn't realize exc_value was the variable that had the message(I was trying to get e.msg) so I turned it off since it said by default it prints to stderr with it off, and I tried to work from there.
Please ignore
Related
I'm having a problem where my PHP scripts are returning my json encoded array with a number above and a number below it. Like follows:
26
[0,"edited_token_string"]
0
I have not changed any of the scripts that I'm encountering this on, but it is happening with all of them. I don't have any other echos other than the one used to echo the array. Our server was returning "null" from all of the scripts all morning and now is returning the correct array, with these numbers surrounding it. Is it possible something was updated on the server that accidentally turned on some type of debugging? I've called our hosting service, but they are incredibly unhelpful.
Thanks in advance,
Max
Try adding header('Content-type: application/json'); directly above the line that outputs your JSON. If something else has already outputted something, you will get an error telling you where in your code that happened.
For the undesired output after the JSON, could it be that there is also an extra space after the closing ?> tag? A quick and dirty fix would be to just add die; after the last intentional echo;
I have a PHP script, which attempts to read a file and output it, but none of the ways I know are working. It's kind of a weird case, since I can WRITE to the file, but not read it.
Posting any code would probably be unnecessary, I simply tried reading the file, first with file_get_contents, and then with fopen. None of them rises an error, they simply return an empty string. Even filesize returns 0 as the length. The only thing, that works is filemtime.
Any ideas why writing would be possible, but not reading?
EDIT:
It seems like file_get_contents works, when I point to the file, directly, using FTP, although a new weird issue came up: before the while-loop in my original code, it does return the file's text, but IN the while loop, it doesn't. And it's printing anything else with no problem, so it's not an echo-related issue..
I'm playing with some PHP obfuscation/encoding tool that works without any server-side loader, and I surprisingly found out solution that I cannot decode.
I consider myself generally well informed about different solutions for 'protecting' php code by encoding and obfuscation, and I know 2 fundamental rules:
if PHP can read and understand your code, no matter how much obfuscated and encoded, you also can
encoding solutions based on usage of eval() language construct are breakable trough outputting code that is to be passed as argument in eval construct (replacing eval with echo or print)
Well, that principles worked perfectly until I found this tool, encoded exactly this code
<?php
echo "Hello World!";
?>
and got this:
<?php $_F=__FILE__;$_C0='cl92S3IqSnY0aVlwKWFwNTVwYVl8fEtyKkp2NDlZcClhb2EodWFZNDdySnYnOXNpV0xyOCA4fU9yPksgTFY4IHNKSg0gSjo+ck9KNz05dHNpOXNPIHRpU31Pcj5LOCA+T0lLSn1LSjcgYzhyDQAgS0xKICxqLCBrY1ZPNyBXT3JWJiBuSk84ckkNIFZPSiBdViZyNyBfSU8gbyggTEljTzggSQ0mQj0nNEc/aTk/Pkw+MCFKfUxJICJqSiYmSSAySU8mN0UiRzAhP2k5Pz5MPiByX3ZMSlY3Sk84pzhKDUt2NDRKfUxJdnNWOEpGKKc3Sn1JN0p2JyxrWD43cjNtN2omOHFXJQ19a2FtVi1YPnNvKEZRMmhtc286WTdrYHU3byZaN2sARk5kYlR9agB1c2tuKjdkSVkuI2B1N2thVDYrZlR9agB1czK0QnFvJmMKMjomcQ1iRgpXZlkuajMoNnAzTHFrWD5zKn1GNmozKDZwM0xxa1g+cyp9S3Nrbio3ZEklNi0zKDZvaF19KlgmfStJOn1qAAB9b2E4VjJiAGZtWik2V0tyUTJOT3FwaF03MilaNnJOKnErWgA3LWg4Z2tMJTdqQUYKQmFwN3B9Y31rTFRxcG5MfSpiY3MqbiUKcE4rfSomVDcjWgBzKjVLfSpuVHEytCVmZGZvfWoAAE5wMyg2b3Fdcw1iS3EqtEtWMjopNiZxJn0qWExzKk04WG9uY3EtcUwKak5Mcw0uS31vbkJWMlF1cSphYzcjWW1WLT4mNitNQn1qAHVRb2E4c3BmRmZtUW9OKzgNZmthY1FvOj5RbzhhaHBYSVYtLmN9cFgpc2tgY3FrJm19azpMSlclcnMqYWNxU2Z1aG0pYFZrJm1mak4rfSomVDcjM3BRLS4AfWpoXTdrbis3a25aZmpubVYyKQ1malhJcVNBNVFTM0l9Km4qLFM3STdqWFQ2cjVdN3A3cAoNM0l9azdZUS1oWgoqKSY3I30AfXBYKXNrYGFob05dc2thQjZyLlROKi51N2tuKDcjWVpxMk5dfSq0JVYyYWM2DW5jcWtuQnNrJmNxV0sqc28pJQotNyZWMjdJN2Q+cnNvOlpoQjMlUS1oDXEtYmFoWWFyc2u0Y1ZCfWVgTUxiZk03WVEtaFosI2FMLHIzYH0qJkxzIzMycS1obVYyYWMKK1RdcWsmbywAUCc0NEc/aQ=';$_D=strrev('edoced_46esab');eval($_D('JF9DMD1iYXNlNjRfZGVjb2RlKCRfQzApOyRfQzA9c3RydHIoJF9DMCwnKEEuZ1B0TzZyDVF+p1khLHszTHcwOkhxKVpXSjFkbURYbioKOGBoZkktZbQjNENOUzJhNXZbb2lda007X3hianNsY0IAdSY3eXpVVCtwOT4lRy9GCVZLPX1SRTwnLCc0QU1LPS9yT2luWbRfMQpQfkJoOg14Jlo1a1RlKkR6XVJWbUxzVUpJb1grRkMpYE5TVzk4KH0yPnZHRSVmLVFIYlt1eWc3bGRxpyx3ajM8cDA7IzZ7YXQuYwkhACcpOyRfUj1zdHJfcmVwbGFjZSgnX19GSUxFX18nLCInIi4kX0YuIiciLCRfQzApO2V2YWwoJF9SKTskX1I9MDskX0MwPTA7'));?>
There is really nothing special about given code except one thing I cannot explain:
when I output code that is to be passed in eval() as argument, that code generates parse error.
Naturally, PHP would not accept this:
$_C0=base64_decode($_C0);$_C0=strtr($_C0,'(A.gPtO6r
Q~§Y!,{3Lw0:Hq)ZWJ1dmDXn*
8`hfI-e´#4CNS2a5v[oi]kM;_xbjslcBu&7yzUT+p9>%G/F VK=}RE<','4AMK=/rOinY´_1
P~Bh:
x&Z5kTe*Dz]RVmLsUJIoX+FC)`NSW98(}2>vGE%f-QHb[uyg7ldq§,wj3
but that is that very code that is passed in eval() construct in encoded script above. And everything works fine.
How can that be? Eval construct is nothing more - as much as I know - then interpreting string as regular PHP code, and if that string contains something that will be incorrect for PHP in eval() -it sure will be same if that would be passed directly to script without eval.
What is the mistery here?
The result of base64_decode that you are printing contains some control characters that are tricking the console into hiding some parts of the code. The actual result is this:
$_C0=base64_decode($_C0);$_C0=strtr($_C0,'(A.gPtO6r^MQ~§Y!,{3Lw0:Hq)ZWJ1dmDXn*
8`hfI-e´#4CNS2a5v[oi]kM;_xbjslcB^#u&7yzUT+p9>%G/F VK=}RE<','4AMK=/rOinY´_1
P~Bh:^Mx&Z5kTe*Dz]RVmLsUJIoX+FC)`NSW98(}2>vGE%f-QHb[uyg7ldq§,wj3<p0;#6{at.c !^#');$_R=str_replace('__FILE__',"'".$_F."'",$_C0);eval($_R);$_R=0;$_C0=0;
Which decodes into this:
if(time()>1359388391||time()<1359294791)die('<b>This script has been expired.</b><br />Scripts protected using the PHP Guard Trial Version are valid for 24 hours only.');?><?php
echo "Hello World!";
?><?php if(headers_sent())echo(base64_decode('PGRpdiBzdHlsZT0ncG9zaXRpb246YWJzb2x1dGU7d2lkdGg6NDQwcHg7bGVmdDo1MCU7dG9wOjIwcHg7bWFyZ2luLWxlZnQ6LTI1MHB4O3BhZGRpbmc6OHB4O3BhZGRpbmctbGVmdDo0OXB4O2JvcmRlcjoxcHggc29saWQgIzk5OTtiYWNrZ3JvdW5kOiNmZjkgdXJsKGh0dHA6Ly93d3cucGhwZ3VhcmQubmV0L3NjcmlwdCkgbm8tcmVwZWF0IDI2cHggN3B4O2ZvbnQtZmFtaWx5OlZlcmRhbmEsR2VuZXZhLHNhbnMtc2VyaWY7Zm9udC1zaXplOjEycHg7Y29sb3I6IzY2NjsnIG9uY2xpY2s9J3RoaXMuc3R5bGUuZGlzcGxheT0ibm9uZSI7Jz5UaGlzIHNjcmlwdCB3YXMgcHJvdGVjdGVkIHVzaW5nIHRoZSA8YSBocmVmPSdodHRwOi8vd3d3LnBocGd1YXJkLm5ldCcgc3R5bGU9J2NvbG9yOiMwNmM7dGV4dC1kZWNvcmF0aW9uOnVuZGVybGluZTtmb250LXdlaWdodDpib2xkJyB0YXJnZXQ9J19ibGFuayc+UEhQIEd1YXJkPC9hPiBUcmlhbCBWZXJzaW9uLjwvZGl2Pg='));?>
Which decodes into this:
<div style='position:absolute;width:440px;left:50%;top:20px;margin-left:-250px;padding:8px;padding-left:49px;border:1px solid #999;background:#ff9 url(http://www.phpguard.net/script) no-repeat 26px 7px;font-family:Verdana,Geneva,sans-serif;font-size:12px;color:#666;' onclick='this.style.display="none";'>This script was protected using the <a href='http://www.phpguard.net' style='color:#06c;text-decoration:underline;font-weight:bold' target='_blank'>PHP Guard</a> Trial Version.</div>
From the PHP manual...
eval() returns NULL unless return is called in the evaluated code, in
which case the value passed to return is returned. If there is a parse
error in the evaluated code, eval() returns FALSE and execution of the
following code continues normally. It is not possible to catch a parse
error in eval() using set_error_handler().
PHP manual - eval()
I'm writing PHP code that uses a database. To do so, I use an array as a hash-map.
Every time content is added or removed from my DB, I save it to file.
I'm forced by my DB structure to use this method and can't use mysql or any other standard DB (School project, so structure stays as is).
I built two functions:
function saveDB($db){
$json_db = json_encode($db);
file_put_contents("wordsDB.json", $json_db);
} // saveDB
function loadDB(){
$json_db = file_get_contents("wordsDB.json");
return json_decode($json_db, true);
} // loadDB
When echo-ing the string I get after the encoding or after loading from file, I get a valid json (Tested it on a json viewer) Whenever I try to decode the string using json_decode(), I get null (Tested it with var_dump()).
The json string itself is very long (~200,000 characters, and that's just for testing).
I tried the following:
Replacing single/double-quotes with double/single-quotes (Without any backslashes, with one backslash and three backslashes. And any combination I could think of with a different number of backslashes in the original and replaced string), both manually and using str_replace().
Adding quotes before and after the json string.
Changing the page's encoding.
Decoding without saving to file (Right after encoding).
Checked for slashes and backslashes. None to be found.
Tried addslashes().
Tried using various "Escape String" variants.
json_last_error() doesn't work. I get no error number (Get null, not 0).
It's not my server, so I'm not sure what PHP version is used, and I can't upgrade/downgrade/install anything.
I believe the size has something to do with it, because small strings seem to work fine.
Thanks Everybody :)
In your JSON file change null to "null" and it will solve the problem.
Check if your file is UTF8 encoded. json_decode works with UTF8 encoded data only.
EDIT:
After I saw uploaded JSON data, I did some digging and found that there are 'null' key. Search for:
"exceeding":{"S01E01.html":{"2217":1}},null:{"S01E01.html":
Change that null to be valid property name and json_decode will do the job.
I had a similar problem last week. my json was valid according to jsonlint.com.
My json string contained a # and a & and those two made json_decode fail and return null.
by using var_dump(json_decode($myvar)) which stops right where it fails I managed to figure out where the problem was coming from.
I suggest var_dumping and using find dunction to look for these king of characters.
Just on the off chance.. and more for anyone hitting this thread rather than the OP's issue...I missed the following, someone had htmlentities($json) way above me in the call stack. Just ensure you haven't been bitten by the same and check the html source.
Kickself #124
I am trying to parse the json output from
http://www.nyc.gov/portal/apps/311_contentapi/services/all.json
And my php json_decode returns a NULL
I am not sure where the issue is, I tried running a small subset of the data through JSONLint and it validated the json.
Any Ideas?
The error is in this section:
{
"id":"2002-12-05-22-24-56_000010083df0188b4001eb56",
"service_name":"Outdoor Electric System Complaint",
"expiration":"2099-12-31T00:00:00Z",
"brief_description":"Report faulty Con Edison equipment, including dangling or corroded power lines or "hot spots.""
}
See where it says "hot spots." in an already quoted string. Those "'s should've been escaped. Since you don't have access to edit the JSON perhaps you could do a search for "hot spots."" and replace it with \"hot spots.\"" like str_replace('"hot spots.""', '\\"hot spots.\\""\, $str); for as long as that's in there. Of course that only helps if this is a one time thing. If the site continues to make errors in their JSON output you'll have to come up with something more complex.
What I did to identify the errors in the JSON ...
Since faulty quoting is the first thing to look for, I downloaded the JSON to a text file, opened in a text editor (I used vim but any full featured editor would do), ran a search and replace that removed all characters except double-quote and looked at the result. It was clear that correct lines should have 4 double-quotes so I simply searched for 5 double-quotes together and found the first bad line. I noted the line number and then undid the search and replace to get the original file back and looked at that line. This gives you what you need to get the developers of the API to fix the JSON.
Writing code to automatically fix the bad JSON before giving it to json_decode() would be quite a bit harder but doable using techniques like those in another answer.
According to the PHP manual:
In the event of a failure to decode, json_last_error() can be used to determine the exact nature of the error.
Try calling it to see where the error is.