newbtophp Posted December 1, 2009 Share Posted December 1, 2009 Lets say I have a file: <?php $var = "test file"; $var2 = base64_decode('ZXhhbXBsZQ=='); $test = array(base64_decode('ZXhwZXJpbWVudA==') => 'example'); ?> How would i replace each encoded base64 with its decoded (loop it with php?) so it would become: <?php $var = "test file"; $var2 = 'example'; $test = array('experiment' => 'example'); ?> All help is greatly apreciated. Quote Link to comment Share on other sites More sharing options...
JonnoTheDev Posted December 1, 2009 Share Posted December 1, 2009 By creating a new array <?php $test = array('ZXhwZXJpbWVudA==' => 'example'); $new = array(); foreach($test as $key => $val) { $new[base64_decode($key)] = $val; } print_r($new); ?> Quote Link to comment Share on other sites More sharing options...
newbtophp Posted December 1, 2009 Author Share Posted December 1, 2009 By creating a new array <?php $test = array('ZXhwZXJpbWVudA==' => 'example'); $new = array(); foreach($test as $key => $val) { $new[base64_decode($key)] = $val; } print_r($new); ?> Thanks, how would i integrate that so it replaces the original encoded with the base64_decoded within the actual file (without placing the decoded in an array)? :-\ Quote Link to comment Share on other sites More sharing options...
JonnoTheDev Posted December 1, 2009 Share Posted December 1, 2009 Thanks, how would i integrate that so it replaces the original encoded with the base64_decoded within the actual file (without placing the decoded in an array)? Does not make sense. Please elaborate. Quote Link to comment Share on other sites More sharing options...
newbtophp Posted December 1, 2009 Author Share Posted December 1, 2009 Does not make sense. Please elaborate. Heres my attempt: <?php //Don't work - just used my attempt as example $input = file_get_contents('test.php'); function recursive64($input) { $regex = "/base64_decode\('([^']+)'\)/Umis"; preg_match("/base64_decode\('([^']+)'\)/Umis", $input, $var); $input = base64_decode($var[1]); return preg_replace_callback($regex, 'recursive64', $input); } $output = recursive64($input); echo($output); ?> Lets say I have this code (within file.php): <?php $var = "test file"; $var2 = base64_decode('ZXhhbXBsZQ=='); $test = array(base64_decode('ZXhwZXJpbWVudA==') => 'example'); ?> Theirfore when its echo'd (ran throught my function) the output would be: <?php $var = "test file"; $var2 = 'example'; $test = array('experiment' => 'example'); ?> My aim is, to replace each base64_decode('string') with 'decodedstring', without effecting the actual file (so only the base64 is replaced). Quote Link to comment Share on other sites More sharing options...
newbtophp Posted December 1, 2009 Author Share Posted December 1, 2009 Neil's offline, i dont want to sound rude, but can possibly someone else take alook at this in the mean time? Quote Link to comment Share on other sites More sharing options...
newbtophp Posted December 1, 2009 Author Share Posted December 1, 2009 I tried simply doing the following: <?php $file = file_get_contents('test.php'); $file = preg_replace("~base64_decode\('([^']+)'\)~s", base64_decode($1), $file); highlight_string($file); ?> but that returns me with an error: Parse error: syntax error, unexpected T_LNUMBER, expecting T_VARIABLE or '$' in /home/user/public_html/file.php on line 6 Quote Link to comment Share on other sites More sharing options...
Alex Posted December 1, 2009 Share Posted December 1, 2009 One solution would be to use preg_replace_callback. Ex: $file = <<<FILE \$var = "test file"; \$var2 = base64_decode('ZXhhbXBsZQ=='); \$test = array(base64_decode('ZXhwZXJpbWVudA==') => 'example'); FILE; $file = preg_replace_callback("~base64_decode\('(.+?)'\)~", create_function('$arg', 'return "\'" . base64_decode($arg[1]) . "\'";'), $file); echo '<pre>' . $file . '</pre>'; Output: $var = "test file"; $var2 = 'example'; $test = array('experiment' => 'example'); Quote Link to comment Share on other sites More sharing options...
salathe Posted December 1, 2009 Share Posted December 1, 2009 [ot]For the lazy and/or time-short folks, scroll down to the first code block. That's all that's important. With AlexWD's post, even the code isn't important… serves me right for taking over half an hour to format a post (amongst doing other things).[/ot] I'm not sure why you would need this to be recursive (as shown by your code attempt, but not in your description) so I'm ignoring that for the moment. The tasks that we need to accomplish follow the general sequence of: 1. Find the parts of the string (the file contents) that we want to deal with. 2. Extract the Base64 encoded values from those parts. 3. Decode those encoded values. 4. Replace the whole part with the decoded value wrapped in quotes (since we're writing PHP code). Number 1 is easy, we all know that regular expressions can do anything[1]. This counts as anything, so lets use them. We need an expression to match base64_decode('gobbledygook') where the gobbledygook is a Base64 encoded value. Matching the literal parts is easy, matching the Base64 encoded value is also easy since we can just say to match the characters available for Base64 (a-z, A-Z, 0-9, +, / and some trailing = characters). I'm going to assume a working knowledge of regex, so won't delve too deeply into it (in fact, I won't delve at all into it; just present you with the code). The part of regular expression matching the Base64 encoded value is simplified in my example below but since we're replacing existing (hopefully working) PHP code we shouldn't have to worry about things which look like Base64 encoded values yet aren't. Number 2 is also easy with regex, we just use a capturing group ((…)) around the Base64 encoded value. For number 3 we have the super-convenient base64_decode function available to change the encoded value into a decoded one. For number 4, we'll use a function in conjunction with preg_replace_callback to take the entire match and replace it with what we want. The code should really explain itself (it's only 4 lines!). To put it all into code we simply grab the file contents and perform a regex replacement doing all that stuff described above. This example just echoes out the "decoded" file contents, you can do whatever with it (write it back to a file, perhaps). <?php $encoded = file_get_contents('encoded.php'); function callback($match) { return "'" . base64_decode($match[1]) . "'"; } echo preg_replace_callback("~base64_decode\('([a-zA-Z0-9+/]+={0,3})'\)~", 'callback', $encoded); ?> Given the file encoded.php with the following contents: <?php $var = "test file"; $var2 = base64_decode('ZXhhbXBsZQ=='); $test = array(base64_decode('ZXhwZXJpbWVudA==') => 'example'); ?> The output from the example script is something like: <?php $var = "test file"; $var2 = 'example'; $test = array('experiment' => 'example'); ?> -- [1] Yes, literally anything. Quote Link to comment Share on other sites More sharing options...
newbtophp Posted December 1, 2009 Author Share Posted December 1, 2009 Thanks Alex and Salathe! Salathe, the info was very useful, really appreciate it ! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.