Created
October 4, 2015 14:59
-
-
Save smddzcy/842f9d58d9b82d412f4d to your computer and use it in GitHub Desktop.
System command execute function, tries a bunch of funcs
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
/** | |
* @param string $cmd Command to execute | |
* @return string Result | |
*/ | |
public function _exec($cmd) | |
{ | |
$res = ''; | |
if (!empty($cmd)) { | |
if (@function_exists('exec')) { | |
exec($cmd, $res); | |
$res = join("\n", $res); | |
} elseif (@function_exists('shell_exec')) { | |
$res = @shell_exec($cmd); | |
} elseif (@function_exists('system')) { | |
@ob_start(); | |
system($cmd); | |
$res = ob_get_contents(); | |
@ob_end_clean(); | |
} elseif (@function_exists('passthru')) { | |
@ob_start(); | |
passthru($cmd); | |
$res = ob_get_contents(); | |
@ob_end_clean(); | |
} elseif (@is_resource($f = @popen($cmd, "r"))) { | |
$res = ""; | |
if (@function_exists('fread') && @function_exists('feof')) { | |
while (!@feof($f)) { | |
$res .= @fread($f, 1024); | |
} | |
} else if (@function_exists('fgets') && @function_exists('feof')) { | |
while (!@feof($f)) { | |
$res .= @fgets($f, 1024); | |
} | |
} | |
@pclose($f); | |
} elseif (@is_resource($f = @proc_open($cmd, array(1 => array("pipe", "w")), $pipes))) { | |
$res = ""; | |
if (@function_exists('fread') && @function_exists('feof')) { | |
while (!@feof($pipes[1])) { | |
$res .= @fread($pipes[1], 1024); | |
} | |
} else if (@function_exists('fgets') && @function_exists('feof')) { | |
while (!@feof($pipes[1])) { | |
$res .= @fgets($pipes[1], 1024); | |
} | |
} | |
@proc_close($f); | |
} | |
} | |
return $res; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment