PageRenderTime 44ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/php/idc_manager/cidc-read-only/library/Etao/Console/Exec.php

http://timoseven.googlecode.com/
PHP | 149 lines | 109 code | 21 blank | 19 comment | 12 complexity | b09fef8ed4f1b98f8ad145116b048bf2 MD5 | raw file
Possible License(s): MIT, LGPL-2.1, MPL-2.0-no-copyleft-exception, GPL-3.0, AGPL-1.0
  1. <?php
  2. /*
  3. [Discuz!] (C)2001-2011 Comsenz Inc.
  4. This is NOT a freeware, use is subject to license terms
  5. y109
  6. Exec.php
  7. 2011-4-22
  8. */
  9. class Etao_Console_Exec
  10. {
  11. public static function exec($command, array &$output = null, &$return_var = null)
  12. {
  13. exec($command, $output, $return_var);
  14. return $return_var;
  15. }
  16. public static function execBase64($command, array &$output = null, &$return_var = null)
  17. {
  18. $command = base64_decode($command);
  19. exec($command, $output, $return_var);
  20. return $return_var;
  21. }
  22. /**
  23. *
  24. * ?exec????????,?????
  25. * @param string $cmd ??
  26. * @param int $retcode ??
  27. */
  28. public static function runCommand($cmd, $nohup=TRUE)
  29. {
  30. set_time_limit(0);
  31. ignore_user_abort(TRUE);
  32. if($nohup) {
  33. $cmd = 'nohup ' . $cmd . ' > /dev/null 2>&1 &';
  34. }
  35. $logger = Zend_Registry::get('logger');
  36. $logger->debug(__METHOD__ . $cmd);
  37. global $disablefunc;
  38. $disablefunc = @ini_get("disable_functions");
  39. if(!empty($disablefunc)) {
  40. $disablefunc = str_replace(" ", "", $disablefunc);
  41. $disablefunc = explode(",", $disablefunc);
  42. } else {
  43. $disablefunc = array();
  44. }
  45. $result = "";
  46. if(!empty($cmd)) {
  47. if(is_callable("exec") and !in_array("exec", $disablefunc)) {
  48. exec($cmd, $result);
  49. $result = implode("\n", $result);
  50. } elseif(($result = `$cmd`) !== false) {
  51. } elseif(is_callable("system") and !in_array("system", $disablefunc)) {
  52. $v = @ob_get_contents();
  53. @ob_clean();
  54. system($cmd);
  55. $result = @ob_get_contents();
  56. @ob_clean();
  57. echo $v;
  58. } elseif(is_callable("passthru") and !in_array("passthru", $disablefunc)) {
  59. $v = @ob_get_contents();
  60. @ob_clean();
  61. passthru($cmd);
  62. $result = @ob_get_contents();
  63. @ob_clean();
  64. echo $v;
  65. } elseif(is_resource($fp = popen($cmd, "r"))) {
  66. $result = "null";
  67. while(!feof($fp))
  68. {
  69. $result .= fread($fp, 1024);
  70. }
  71. pclose($fp);
  72. }
  73. }
  74. return $result;
  75. }
  76. /**
  77. *
  78. * ?exec????????
  79. * @param string $cmd ??
  80. * @param int $retcode ??
  81. */
  82. public static function runCommands($cmd, $nohup = TRUE, $mayReturnNothing = FALSE)
  83. {
  84. set_time_limit(0);
  85. ignore_user_abort(TRUE);
  86. if($nohup) {
  87. $cmd = 'nohup ' . $cmd . ' > /dev/null 2>&1 &';
  88. }
  89. $logger = Zend_Registry::get('logger');
  90. $logger->debug(__METHOD__ . $cmd);
  91. global $lang;
  92. $output = array();
  93. $err = false;
  94. $c = $cmd;
  95. $descriptorspec = array(0 => array('pipe', 'r'), 1 => array('pipe', 'w'), 2 => array('pipe', 'w'));
  96. $resource = proc_open($c, $descriptorspec, $pipes);
  97. $error = '';
  98. if(!is_resource($resource)) {
  99. exit;
  100. }
  101. $handle = $pipes[1];
  102. $firstline = true;
  103. while(!feof($handle))
  104. {
  105. $line = fgets($handle);
  106. if($firstline && empty($line) && !$mayReturnNothing) {
  107. $err = true;
  108. }
  109. $firstline = false;
  110. $output[] = rtrim($line);
  111. }
  112. while(!feof($pipes[2]))
  113. {
  114. $error .= fgets($pipes[2]);
  115. }
  116. $error = trim($error);
  117. fclose($pipes[0]);
  118. fclose($pipes[1]);
  119. fclose($pipes[2]);
  120. proc_close($resource);
  121. if(!$err) {
  122. return $output;
  123. } else {
  124. throw new Exception($err);
  125. }
  126. }
  127. }
  128. ?>