Last active
May 22, 2016 17:02
-
-
Save jankal/8ad40cfe245ecd85f6a8 to your computer and use it in GitHub Desktop.
pack a anonimous function into a string (for transmissen)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-----BEGIN PGP SIGNED MESSAGE----- | |
Hash: SHA256 | |
<?php | |
class TestClass { | |
function getClosure() { | |
return function () { | |
var_dump($this); | |
}; | |
} | |
} | |
$abc = "testABC"; | |
$xyz = new TestClass(); | |
$test = true; | |
$x = function () use ($test, $xyz, $abc) { | |
echo $abc; | |
var_dump($test, $xyz); | |
var_dump($this); | |
}; | |
$test = new TestClass(); | |
echo packAnonFunction($x, $test, $xyz, $abc); | |
echo packAnonFunction($test->getClosure()); | |
function packAnonFunction($payload, ...$args) { | |
$func = new ReflectionFunction($payload); | |
$filename = $func->getFileName(); | |
$start_line = $func->getStartLine() - 1; | |
$end_line = $func->getEndLine(); | |
$length = $end_line - $start_line; | |
$source = file($filename); | |
$body = implode("", array_slice($source, $start_line, $length)); | |
$body = preg_replace('/(?:(?:(\$[a-z]+)\s\=)|return)\sfunction/', '\\$payload = function', $body); | |
if(preg_match('/use\s\((\$[a-zA-Z0-9]+(?:,\s\$[a-zA-Z0-9]+)*)\)/', $body, $matches)) { | |
$vars = $matches[1]; | |
if(strpos($vars, ', ') !== false) { | |
$parts = explode(', ', $vars); | |
} else { | |
$parts = [$vars]; | |
} | |
$body = str_replace(" use (" . $vars . ")", "", $body); | |
} else { | |
$parts = []; | |
} | |
if(strpos($body, '$this') !== false) { | |
$parts[] = '$_this'; | |
$args[count($parts) - 1] = $func->getClosureThis(); | |
$body = str_replace('$this', '$_this', $body); | |
} | |
$return = []; | |
foreach($parts as $key => $variable) { | |
$return[$variable] = $args[$key]; | |
} | |
$variableString = ""; | |
foreach($return as $var => $value) { | |
$value = serialize($value); | |
$variableString .= "\t{$var} = unserialize('{$value}');\n"; | |
} | |
$body = str_replace("{\n", "{\n" . $variableString, $body); | |
$body = str_replace(" ", "\t", $body); | |
$body = preg_replace('/\t+/', "\t", $body); | |
$body = preg_replace('/\t\$payload\s=\sfunction/', '$payload = function', $body); | |
$body = preg_replace('/\t\}\;/s', '};', $body); | |
return $body; | |
} | |
-----BEGIN PGP SIGNATURE----- | |
Version: GnuPG v2 | |
iQIcBAEBCAAGBQJXQeYhAAoJEC4SA5pJ49cLKncP/1OGUMVOQbsHkiZQNzB3ARNZ | |
EmfYIfOAQFCUS3w+lfHuGXaCU1nvM6hZOWTA648aMzHCUKsIXfRqtNWLXcGMLKWW | |
qkAZU0ccPQriS910zS/FSr+1dUikIfRpfN4+VJwd5ragNs5W4OmAik+VYETkpmMy | |
xiupcn4GfH0yLB1rym8mQkKYlytxDnVnehQpQwzAjOOJquTqnZHClKZiRwMfcGBc | |
pOPCqjjWsvjAJKPgpMTmN9tMa8w9x905sC2djRsJcLiBF35IP5qNn1fAVrVfyN+f | |
4COTb8NZmtbikuDWvCJreYRPu2281FyIduwVIXuuGb5vAam+HSoQbx812kAAfW9i | |
OJarvilnzc74cxNyO/wTRYz1u6uwVRRa2eBZGS/hHNA9bDGsrh8fhsUKDjqja02l | |
DXhEA7XTqtRx8hKX9ru90Csx9wsLrz8iXMDa70i53YYOZX5xHvFU5KDaX80/Ux4Z | |
FbooaF/0nlBXfakpAUwdlT7LwKjTVeTkZl2NHpQrzKQdXZlgUtwFWR0mje/C7bvP | |
A9Cg3APaDt6Dgsq5DBjEXwD43y0bnXVcHBA05YwQElurwTQE68kBkyB+SUzhPug7 | |
7BD8MmmPewQs6VRVJo/ClP5hFSDP30o27zFjlRgPffbz3Kf/HxFnUl+RVYpYSQJM | |
3E8ZMWpLB4c3OgzphXh2 | |
=dks0 | |
-----END PGP SIGNATURE----- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment