...

View Full Version : extracting PHP from PHP



Phil Jackson
10-18-2009, 09:11 AM
Morning all. I have a problem in the form of i need to extract php code from a php and html (.php) file.

firstly i tried :



$file_contents = str_replace("<?php", "[ACTphpOpen]", file_get_contents("../www.cms.actwebdesigns.co.uk2/logged.php"));
$file_contents = preg_replace("#\?\s*>#is", "[ACTphpClose]", $file_contents);

if(preg_match_all("#\[ACTphpOpen\]((?!\[ACTphpClose\]).)*#is", $file_contents, $matches))
{
foreach($matches as $code)
{
echo "<pre>".$code."</pre>";
}
}


but this lead to, well nothing.

so i dug a bit deeper and wrote this:



$file_contents = token_get_all(file_get_contents("../www.cms.actwebdesigns.co.uk2/logged.php"));
$start=0;
$end=0;

$segmentArray = array();
foreach($file_contents as $key => $token)
{
$tokenName = token_name($key);
if(!is_array($token))
{
if($start==0 && $end==0 && $tokenName=="T_OPEN_TAG")
{
$start=1;
}
if($start==1 && $end==0 && $tokenName!=="T_CLOSE_TAG")
{
$entryNo = count($segmentArray);
$segmentArray[$entryNo][] = $token;
}
if($tokenName=="T_CLOSE_TAG")
{
$start=0;
}
}
else
{
foreach($token as $key2 => $token2)
{
if(!is_string($token2))
{
if(!is_array($token2))
{
$tokenName2 = token_name($token2);
#echo "<pre>".$tokenName2."</pre>";
if($start==0 && $end==0 && $tokenName2=="T_OPEN_TAG")
{
$start=1;
}
if($start==1 && $end==0 && $tokenName2!=="T_CLOSE_TAG")
{
$entryNo = count($segmentArray);
$segmentArray[$entryNo][] = $token2;
}
if($tokenName2=="T_CLOSE_TAG")
{
$start=0;
}
}
}
else
{
if($start==1 && $end==0 && $tokenName2!=="T_CLOSE_TAG")
{
$entryNo = count($segmentArray);
$segmentArray[$entryNo][] = $token2;
}
}
}
}
}

print_r($segmentArray);


Problem being, i dont know how deep any given matrix is going to be a secondly how to convert the tokens back into php as i think i would get the same problems as before.

Anyone know of a way to process php without it actualy "processing" the code?

seco
10-18-2009, 10:09 AM
i dont think you can extract php, if you could i think there would be problems all over the net. have you tried curl? or naming that file logged.phps if no one else will be accessing it direct?

Phil Jackson
10-18-2009, 01:13 PM
Thats not the problem at hand. i can gather all php data into a variable but trying to process it though php functions results in errors. so at the min im trying to encrypt the data, process it that way then decrypt it.. sigh...

Phil Jackson
10-18-2009, 01:55 PM
Could someone do me a huge favour.



<?php

$str = base64_encode(preg_replace("#\s|\r|\t|\n#", " ", file_get_contents("../path/to/file.php")));
if(preg_match("#(PD9waHAg)((?!(Pz4g)).)*#is", $str, $matches))
{
#print_r($matches);
echo "<xmp>".base64_decode($matches[0]."Pz4g")."</xmp>";
}

?>

The above code works as planned but as soon as i use preg_match_all i get a browser error not php error.

could someone test the code using preg_match_all and see what happens please!

seco
10-18-2009, 03:29 PM
i get a few odd characters

Phil Jackson
10-18-2009, 05:39 PM
This one is updated, but still have a problem: (apparently wamp and xamp have problems with preg_match_all when subject is more than 250 chars long)



$dir = "../www.cms.actwebdesigns.co.uk2/logged.php";
#$dir = "../../logged.php";

$str = base64_encode(htmlspecialchars(preg_replace("#(?:\s\s+)|(?:\n)|(?:\t)|(?:\r)#", " ", file_get_contents($dir))));
$dataToAdd = array();

for($x=0; true; $x++)
{
if(preg_match("#(".base64_encode("&lt;?php ").")((?!(".base64_encode("?&gt; ").")).)*#is", $str, $match))
{
$dataToAdd[] = $match[0].base64_encode("?&gt; ");
$PQmatch = preg_quote($match[0].base64_encode("?&gt; "), "#");
$str = preg_replace("#".$PQmatch."#is", "", $str, 1);
}
else
{
break;
}
}

$z=1;

foreach($dataToAdd as $data)
{
echo "<xmp>".$z."----->>> ".base64_decode($data)."<br /></xmp>";
$z++;
}

//1----->>> &lt;?php include(&quot;scripts/php/auth.php&quot;); include(&quot;scripts/php/sessions.php&quot;); ?&gt;<br />
//2----->>> &lt;?php if($VERIFICATION!==1) { ?&gt;<br />
//3----->>> &lt;?php echo $WEBSITE; ?&gt;<br />
//4----->>> &lt;?php } ?&gt; &lt;/p&gt; &lt;/div&gt; &lt;div class=&quot;contentBlock&quot;&gt; &lt;h2&gt;ACT Web Designs Group&lt;/h2&gt; &lt;p&gt;&lt;a href=&quot;http://www.actwebdesigns.co.uk&quot; title=&quot;link to ACT Web Designs Home&quot;&gt;www.actwebdesigns.co.uk&lt;/a&gt;&lt;br /&gt; &lt;a href=&quot;http://hosting.actwebdesigns.co.uk&quot; title=&quot;link to ACT Web Designs Hosting Solutions&quot;&gt;www.hosting.actwebdesigns.co.uk&lt;/a&gt; &lt;a href=&quot;http://www.plugnplaycms.co.uk.co.uk&quot; title=&quot;link to the home of plug n play cms&quot;&gt;www.plugnplaycms.co.uk&lt;/a&gt;&lt;/p&gt; &lt;/div&gt; &lt;/div&gt; &lt;div id=&quot;mainBodyRight&quot;&gt; &lt;?php if(isset($_GET['msg']) &amp;&amp; !empty($_GET['msg'])) { echo &quot;&lt;div class=\&quot;contentBlock\&quot;&gt;\n&quot;; echo &quot;&lt;h2&gt;&quot;.$_GET['h2'].&quot;&lt;/h2&gt;&quot;; echo &quot; &lt;p style=\&quot;color:&quot;.$_GET['color'].&quot;\&quot;&gt;&quot;.$_GET['msg'].&quot;&lt;/p&gt;\n&quot;; echo &quot;&lt;/div&gt;\n&quot;; } if($VERIFICATION==0) { issetJava(&quot;Your account needs verifying.&quot;, &quot;javascript&quot;, &quot;Authorisation&quot;, &quot;red&quot;); } elseif($VERIFICATION==1) { include(&quot;pageIncludes/install.php&quot;); } elseif($VERIFICATION==2) { include(&quot;pageIncludes/mainPage.php&quot;); } ?&gt;<br />

?>


If you look at the 4th output it is not correct but i cant find out why. any ideas? (should be split up into about 4 or 5 separate outputs)

Phil Jackson
10-18-2009, 06:51 PM
god damn... i've been working on something all day that was only not working cause of something wrong with wamp...

working:



<?php

#$dir = "../www.cms.actwebdesigns.co.uk2/logged.php";
$dir = "../../logged.php";

$str = preg_replace("#(?:\s\s+)|(?:\n)|(?:\t)|(?:\r)#", " ", file_get_contents($dir));
$dataToAdd = array();

for($x=0; true; $x++)
{
if(preg_match("#<\?php((?!\?>).)*#is", $str, $match))
{
$dataToAdd[] = $match[0]."?>";
$PQmatch = preg_quote($match[0]."?>", "#");
$str = preg_replace("#".$PQmatch."#is", "", $str, 1);
}
else
{
break;
}
}

$z=1;

foreach($dataToAdd as $data)
{
echo "<xmp>".$z."----->>> ".trim($data)."<br /></xmp>";
$z++;
}

?>

seco
10-18-2009, 07:05 PM
so what exactly is this for?

funnymoney
10-18-2009, 07:16 PM
Anyone know of a way to process php without it actualy "processing" the code?

umm, why do you want PHP that isn't processing.. there should be another way of what you are doing, but, there is only one mistery.. "what are you doing?" :)

Phil Jackson
10-18-2009, 07:26 PM
my cms installs and takes on "any website" (...ahem, supposedly) within 1 minute. It works and runs well on static .html sites now im knotching it up a level to work on php files/sites and keep everything in tact. which means creating a placeholder for the php snippets and storing seperatly. when you publish your amended work it will then bind back together.

as so:



[ACTphp-b7075] = <?php include("scripts/php/auth.php"); include("scripts/php/sessions.php"); ?>
[ACTphp-6f055] = <?php if($VERIFICATION==1) { echo "<script type=\"text/javascript\" src=\"https://web111.secure-secure.co.uk/snowyswebsolutions.co.uk/scripts/js/stopwatch.js\"></script>\n"; echo "<script type=\"text/javascript\" src=\"https://web111.secure-secure.co.uk/snowyswebsolutions.co.uk/scripts/js/stopwatchHandlers.js\"></script>\n"; echo "<script type=\"text/javascript\" src=\"https://web111.secure-secure.co.uk/snowyswebsolutions.co.uk/scripts/js/uncompressed/installation.js\"></script>\n"; } else { ?>
[ACTphp-078c3] = <?php } ?>
[ACTphp-66979] = <?php if($VERIFICATION!==1) { ?>
[ACTphp-7a8c2] = <?php $getPagesQuery = mysql_query("SELECT * FROM `page_names` ORDER BY page_name"); if(mysql_num_rows($getPagesQuery)!=0) { while($getPagesArray = mysql_fetch_array($getPagesQuery)) { echo "<option>".$getPagesArray["page_name"]."</option>"; } } else { echo "<option>You courently have no pages set up</option>\n"; } ?>
[ACTphp-57cf0] = <?php } ?>
[ACTphp-54824] = <?php if($VERIFICATION==1) { ?>
[ACTphp-23913] = <?php } else { ?>
[ACTphp-c1efd] = <?php echo $WEBSITE; ?>
[ACTphp-95ab1] = <?php } ?>
[ACTphp-7b852] = <?php if(isset($_GET['msg']) && !empty($_GET['msg'])) { echo "<div class=\"contentBlock\">\n"; echo "<h2>".$_GET['h2']."</h2>"; echo " <p style=\"color:".$_GET['color']."\">".$_GET['msg']."</p>\n"; echo "</div>\n"; } if($VERIFICATION==0) { issetJava("Your account needs verifying.", "javascript", "Authorisation", "red"); } elseif($VERIFICATION==1) { include("pageIncludes/install.php"); } elseif($VERIFICATION==2) { include("pageIncludes/mainPage.php"); } ?>
[ACTphp-b7075] <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Plug n Play CMS</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="verify-v1" content="KLvuxC+643AQ8NYGn5woBYCU8z962CJXI8B0DVGXKbY=" /> <meta name="description" content="Web Design - ACT Web Designs - Mansfield. Giving a new lease of life to standard compliant web sites." /> <meta name="keywords" content="Web Design, ACT Web Designs, Mansfield, designsers, design" /> <meta name="language" content="en" /> <meta name="author" content="ACT Web Designs" /> <meta name="copyright" content="ACT Web Designs" /> <meta name="publisher" content="ACT Web Designs" /> <meta name="country" content="United Kingdom" /> <meta name="city" content="Mansfield" /> <meta http-equiv="X-UA-Compatible" content="IE=8" /> <link href="https://web111.secure-secure.co.uk/snowyswebsolutions.co.uk/stylesheets/cssReset.css" rel="stylesheet" type="text/css" /> <link href="https://web111.secure-secure.co.uk/snowyswebsolutions.co.uk/stylesheets/main.css" rel="stylesheet" type="text/css" /> <link href="https://web111.secure-secure.co.uk/snowyswebsolutions.co.uk/stylesheets/thickbox.css" rel="stylesheet" type="text/css" /> <script type="text/javascript" src="https://web111.secure-secure.co.uk/snowyswebsolutions.co.uk/scripts/js/jquery-1.3.1.js"></script> <script type="text/javascript" src="https://web111.secure-secure.co.uk/snowyswebsolutions.co.uk/scripts/js/content.acord.act.js"></script> [ACTphp-6f055] <script type="text/javascript" src="https://web111.secure-secure.co.uk/snowyswebsolutions.co.uk/scripts/js/main.js"></script> <script type="text/javascript" src="https://web111.secure-secure.co.uk/snowyswebsolutions.co.uk/scripts/js/uncompressed/populateList.js"></script> <script type="text/javascript" src="https://web111.secure-secure.co.uk/snowyswebsolutions.co.uk/scripts/js/uncompressed/navSetupPopulateList.js"></script> <script type="text/javascript" src="https://web111.secure-secure.co.uk/snowyswebsolutions.co.uk/scripts/js/thickbox.js"></script> [ACTphp-078c3] </head> <body id="aboutusPage"> <div id="wrapper"> <div id="header"> <div id="headerLeft"> <img src="https://web111.secure-secure.co.uk/snowyswebsolutions.co.uk/images/for_web/ACTwebDesignsPanelBanner.jpg" width="278" height="122" alt="Plug n Play CMS - Logo" /> </div> <div id="headerRight"> <ul id="navigationBar"> <li><a href="https://web111.secure-secure.co.uk/snowyswebsolutions.co.uk/index.html" title="" id="aboutus" accesskey="a" tabindex="">Contact</a></li> <li><a href="https://web111.secure-secure.co.uk/snowyswebsolutions.co.uk/web-design-mansfield/plug-n-play-cms.html" title="" id="plugNplay" accesskey="p" tabindex="">Terms</a></li> <li><a href="https://web111.secure-secure.co.uk/snowyswebsolutions.co.uk/web-design-mansfield/services.php" title="" id="services" accesskey="s" tabindex="">Manual</a></li> <li><a href="https://web111.secure-secure.co.uk/snowyswebsolutions.co.uk/web-design-mansfield/portfolio.html" title="" id="portfolio" accesskey="o" tabindex="">F&amp;Q</a></li> <li><a href="https://web111.secure-secure.co.uk/snowyswebsolutions.co.uk/signout.php" title="" id="contact" accesskey="c" tabindex="">Signout</a></li> </ul> <div class="clear"></div> <p class="email">info@actwebdesigns.co.uk</p> <p class="contactNumber">0844 504 9959</p> </div> <div class="clear"></div> </div> <div id="mainBody"> <div id="mainBodyLeft"> <div class="contentBlock"> <div id="reportWrapper"> <h2>Create a new page</h2> <p>The form below will enable you to create a new page using your existing pages as a template.</p> <form action="https://web111.secure-secure.co.uk/snowyswebsolutions.co.uk/creatNewPage.php" method="post" id="creatNewPageForm"> <div class="dropDiv"> <p><a class="open"><small>Create new page</small></a></p> [ACTphp-66979] <div id='insertPageAjaxDiv'></div> <div id='insertPageAjaxDiv2'></div> <p class="alignRight"><label for="templateFrom">Template Based On: </label> <select name="tempFor" id="tempFor"> [ACTphp-7a8c2] </select></p> <p class="alignRight"><label for="titleBox" id="labelTitleBox">Title: </label><input type="text" name="titleBox" id="titleBox" class="edit_input" /></p> <p class="alignRight"><label for="fileNameBox" id="labelFileNameBox">File Name: </label><input type="text" name="fileNameBox" id="fileNameBox" class="edit_input" /></p> <p class="alignRight"><label for="keywordsBox" id="labelKeywordBox">Page Keywords: </label><input type="text" name="keywordBox" id="keywordBox" class="edit_input" /></p> <p class="alignRight"><label for="descriptionBox" id="labelDescriptionBox">Page Description: </label><input type="text" name="descriptionBox" id="descriptionBox" class="edit_input" /></p> <p id="alignLeft"><input type="button" name="createPage" id="createPage" value="Create page" class="submitButton" /></p> <p><a class="close"><small>Close</small></a></p> [ACTphp-57cf0] </div> </form> </div> </div> <div class="contentBlock"> <h2>Publish your site</h2> <p>Once you have amended pages, you can simply submit the button below to make all you edited pages active on your site.</p> <div id="publishResult">&nbsp;</div> <p id="alignLeft"> [ACTphp-54824] <input disabled="disabled" type="submit" name="publishButton" id="publishButton" class="submitButton" value="Publish" /> [ACTphp-23913] <input type="submit" name="publishButton" id="publishButton" class="submitButton" value="Publish" /> <input type="hidden" name="websiteAddress" id="websiteAddress" value="[ACTphp-c1efd]" /> [ACTphp-95ab1] </p> </div> <div class="contentBlock"> <h2>ACT Web Designs Group</h2> <p><a href="http://www.actwebdesigns.co.uk" title="link to ACT Web Designs Home">www.actwebdesigns.co.uk</a><br /> <a href="http://hosting.actwebdesigns.co.uk" title="link to ACT Web Designs Hosting Solutions">www.hosting.actwebdesigns.co.uk</a> <a href="http://www.plugnplaycms.co.uk.co.uk" title="link to the home of plug n play cms">www.plugnplaycms.co.uk</a></p> </div> </div> <div id="mainBodyRight"> [ACTphp-7b852] </div> <div class="clear"></div> <form><input type="hidden" name="javascript" id="javascript" value="yes" /></form> </div> <div id="footer"> <p>Created by ACT <strong class="fade">Web Design</strong>s 2009 &copy; - All rights reserved - info@ACTweb<strong class="fade">design</strong>s.co.uk - <strong class="fade">Mansfield</strong></p> <p>W3C Standards: <a href="http://jigsaw.w3.org/css-validator/check/referer" tabindex="16">CSS</a> &amp; <a href="http://validator.w3.org/check?uri=referer" tabindex="17" title="Web Design - Link to W3C validator">XHTML</a> | <a href="terms-and-conditions.html" title="Web Design - link to terms and conditions page" accesskey="g" tabindex="">Terms &amp; Conditions</a> | <a href="site-map.html" title="Web Design - link to site map page" accesskey="l" tabindex="">Site Map</a> </p> </div> </div> </body> </html>

funnymoney
10-18-2009, 10:41 PM
hmm.. well very nice CMS option, sry can't help you much though, i thought it's gonna be something else :)

Phil Jackson
10-18-2009, 10:51 PM
hehe you have my ear... what was you thinking?

by the way if anyone wants it, finished code is:



$localRoot = "../path/to/file.php";
$str = preg_replace("#(?:\s\s+)|(?:\n)|(?:\t)|(?:\r)#", " ", file_get_contents($localRoot));
$dataToAdd = array();

for($x=0; true; $x++)
{
if(preg_match("#<\?php((?!\?>).)*#is", $str, $match))
{
$stamp = microtime();
$PQmatch = preg_quote($match[0]."?>", "#");
$str = preg_replace("#".$PQmatch."#is", "[ACTphp-".substr(md5($stamp),0, 5)."]", $str, 1);
$dataToAdd['data'][$x] = $match[0]."?>";
$dataToAdd['placeHolder'][$x] = "[ACTphp-".substr(md5($stamp),0, 5)."]";
}
else
{
break;
}
}

foreach($dataToAdd['data'] as $key => $data)
{
$placeHolder = $dataToAdd['placeHolder'][$key];
$data = trim($data);
//use data as you wish
}

funnymoney
10-18-2009, 11:40 PM
well, i thought you were really really off mostly :) but looks like you have a valid usage for your script.. i guess :D



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum