Jump to content

PHP to C++ Command - Slashes escaping & a few errors


cwarn23

Recommended Posts

Hi and I have been writing a script to convert php code (and php-gtk code) into a string that can be used by c++. The only thing is that when I use the string that my script outputs, php fails to execute the code and because it is done via command line (and c++ system function), the error "The system cannot find the file specified." is with at least one script reported. So somewhere in my script I must be adding too many escape slashes or something and perhaps an extra pair of eyes will help as I have been working at this for 2 weeks. But I do know that the classic hello world example works on this script though. Since my php to string/c++ converter is short I shall post it and is as follows:

<?
$_POST['code']=stripslashes($_POST['code']);
echo "This form will convert yur php-gtk code into a c++ program that if you place in the same directory as the official php-gtk viewer, it will allow you to view your php-gtk application. Note: You cannot use new lines when placing code into c++. Instead you must use \n.<br><form method='post' style='padding:0px; margin:0px;'><textarea name='code' style='width:100%; height:480px;'>".$_POST['code']."</textarea><br><input type='submit' value='Convert PHP-GTK Code into C++ exe command'></form>";
if (isset($_POST['code']))
{
//http://www.weanswer.it/blog/optimize-css-javascript-remove-comments-php/
$_POST['code']='
'.$_POST['code'];
while ($_POST['code']!=preg_replace("/(\n|\n*\;[^\'\"]+)((?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:\/\/.*))(\n)?/", "
$1
", $_POST['code']) ) {
$_POST['code']=preg_replace("/(\n|\n*\;[^\'\"]+)((?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:\/\/.*))(\n)?/", "
$1
", $_POST['code']); }
while($_POST['code']!=preg_replace("/(\n|\n[^\'\"]+)((?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:\/\/.*))(\n)?/", "
$1
", $_POST['code'])) {
$_POST['code']=preg_replace("/(\n|\n[^\'\"]+)((?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:\/\/.*))(\n)?/", "
$1
", $_POST['code']);
}

$_POST['code']=str_replace('\\r','\r',$_POST['code']);
$_POST['code']=str_replace('\\n','\n',$_POST['code']);
$_POST['code']=str_replace("
",'',$_POST['code']);
$_POST['code']=str_replace("\r",'',$_POST['code']);
$_POST['code']=str_replace("\n",'',$_POST['code']);

$_POST['code']=preg_replace("/<\?(php)?/",'',$_POST['code']);
$_POST['code']=str_replace('?>','',$_POST['code']);
$_POST['code']=str_replace('\\','\\\\',$_POST['code']);
$_POST['code']=str_replace('\"','\\"',$_POST['code']);
$_POST['code']=str_replace('"','\\\\\"',$_POST['code']);
$_POST['code']=str_replace('{','{ ',$_POST['code']);
$_POST['code']=str_replace('}',' }',$_POST['code']);
$_POST['code']='#include <cstdio>
#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{
    //int argc, char *argv[]
    //http://ubuntuforums.org/showthread.php?t=588559
    
    string code = "'.$_POST['code'].'";
    std::string var = "php -r \" "+code+" \"";
    system (var.c_str());
    //system("PAUSE");
    return EXIT_SUCCESS;
}';
echo "<table border=0 cellpadding=0 cellspacing=0 width=800><tr><td><xmp style='width:800;'>
".$_POST['code']."
</xmp></td></tr></table><hr>";
} else {
echo "<p>";
}
echo "Please note this exe file must be placed in the same folder as the php.exe file (and other php binary files) as can be downloaded from <a href='http://gtk.php.net/download.php'>http://gtk.php.net/download.php</a> (use the binary download).";
?>

With the c++ code the above script outputs, if you place the executable the c++ code makes in the same directory as php.exe then it will submit the php code to php.exe via command line. The only problem is that sometimes it is reporting an error saying "The system cannot find the file specified.". But with that error, the command line doesn't have anything to do with files other than the fact it uses the php command line. But when I devide my code into smaller bits and put it through the above script, somehow it is able to escape the quotation marks for the system command and cause the standard dos error saying file or command not recognised. Below is an example of the c++ command (no different to how the above script makes it) and can anybody see in the string code line how the middle double quotation mark might escape the quotation mark on the line below it.

#include <cstdio>
#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{
    string code = "var_dump(preg_split('/(href\\=\'|href\\=\\\\\"|href\\=)/is','adslkfajal;sdfjhref=test.com/test.htm'));";
    std::string var = "echo \" "+code+" \"";
    system (var.c_str());
    system("PAUSE");
    return EXIT_SUCCESS;
}

Please reply.

I have just checked the php.ini file and it is a rather simple php.ini file containing only extension entries and one other uncommented line (102 lines including comments) and there seems to be no mention of stripslashes nor magic-quotes so I would assume the interpreter (php.exe part of php-gtk and regular php) would use the defaults.

 

Its a bit of a mind-twister to work out why the section of code href\\=\\\\\" can escape the quotes after the word echo. I have tried the following code and I still get the error " 'href\' is not recognized as an internal or external command, operable program or batch file."

#include <cstdio>
#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{  
    string code = "var_dump(preg_split('/(href\\=\\'|href\\=\"|href\\=)/is','adslkfajal;sdfjhref=test.com/test.htm'));";
    std::string var = "php -r \" "+code+" \"";
    system (var.c_str());
    system("PAUSE");
    return EXIT_SUCCESS;
}

Even though in the above code, I replaced href\\=\\\\\" with href\\=\"

 


Thanks for the reply as on some of my topics I don't get a single reply.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.