PageRenderTime 52ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/bash.inc.php

http://wordpress-cli.googlecode.com/
PHP | 125 lines | 99 code | 17 blank | 9 comment | 17 complexity | 2b44dc3ef0aae63f7b4028160159d17a MD5 | raw file
  1. <?php
  2. /* bash.inc.php */
  3. require_once(CLI_DIR.'/lib/cat.inc.php');
  4. if(!isset($_POST['cancel'])){
  5. if(!isset($_SESSION['current'])){ // set current post if not set already
  6. /* Cheers, Sim */
  7. $q="SELECT ID AS c
  8. FROM `".$wpdb->posts."`
  9. WHERE `post_date` IN
  10. (SELECT MAX(`post_date`)
  11. FROM `".$wpdb->posts."`
  12. WHERE (`post_status`='publish' OR `post_status`='static'))";
  13. $r=mysql_query($q);
  14. $_SESSION['current']=mysql_result($r,0,'c');
  15. }
  16. /* command line parsing */
  17. $prompt="(taint)";
  18. $full_line=trim(stripslashes($_POST['c']));
  19. $full_line=do_escape($full_line);
  20. /* hash quoted strings (" > ') */
  21. $stdnoi = stringThatDoesNotOccurIn($full_line);
  22. $quotedstrings=array();
  23. if (preg_match_all('/(".*?")/',$full_line,$qs)){
  24. foreach($qs[1] as $q){
  25. $hash=md5($q);
  26. $full_line=str_replace($q,$stdnoi.$hash,$full_line);
  27. $quotedstrings[$hash]=$q;
  28. }
  29. }
  30. if (preg_match_all("/('.*?')/",$full_line,$qs)){
  31. foreach($qs[1] as $q){
  32. $hash=md5($q);
  33. $full_line=str_replace($q,$stdnoi.$hash,$full_line);
  34. $quotedstrings[$hash]=$q;
  35. }
  36. }
  37. $line=$full_line;
  38. $chaincmd=false;
  39. $chains=explode(';',$full_line);
  40. $line=array_shift($chains);
  41. $chaincmd=implode(';',$chains);
  42. $chaincmd=unpackquotes($chaincmd,$quotedstrings);
  43. $chaincmd=un_escape($chaincmd);
  44. $pipecmd=false;
  45. $pipes=explode('|',$line);
  46. $line=array_shift($pipes);
  47. $pipecmd=implode('|',$pipes);
  48. $pipecmd=unpackquotes($pipecmd,$quotedstrings);
  49. $pipecmd=un_escape($pipecmd);
  50. /* at this point $line should just have command [switches] [operands] */
  51. $tokens=preg_split('/\s+/',$line);
  52. $cmd=strtolower(array_shift($tokens));
  53. $params=unpackquotes(implode(' ',$tokens),$quotedstrings);;
  54. $params=un_escape($params);
  55. $line=unpackquotes($line, $quotedstrings);
  56. $line=un_escape($line);
  57. $switches=array();
  58. $tmptokens=array();
  59. foreach($tokens as $tk){
  60. if($tk{0}=='-'){
  61. if($tk{1}=='-'){ // it's a --option type switch
  62. $ex=explode('=',substr($tk,2),2);
  63. $switches[$ex[0]]=($ex[1]?$ex[1]:true);
  64. }else{ //it's a -laR buncha switches
  65. for($i=1; $i < strlen($tk); $i++){
  66. $switches[$tk{$i}]=true;
  67. }
  68. }
  69. }else{
  70. $tmptokens[]=un_escape(unpackquotes($tk,$quotedstrings));
  71. }
  72. }
  73. $tokens=$tmptokens;
  74. array_unshift($tokens,$cmd);
  75. /* aliases */
  76. include(CLI_DIR.'/lib/bash.aliases.inc.php');
  77. if(isset($aliases[$cmd])) $cmd=$aliases[$cmd];
  78. if($params=='--help' || $params=='-?' || $params=='-h'){
  79. $tokens[1] = $cmd;
  80. $cmd='help';
  81. }
  82. $cmd=un_escape($cmd);
  83. if(strpos($cmd,'/')){ // Fudging a little. Oh stop whining, it's not *really* bash after all
  84. $cmd=substr($cmd,strrpos($cmd,'/')+1);
  85. }
  86. /* say $PATH=/usr/bin:/bin */
  87. /* and no, the user can't change that!*/
  88. if(file_exists(CLI_DIR.'/usr/bin/command-'.$cmd.'.inc.php')){
  89. include(CLI_DIR.'/usr/bin/command-'.$cmd.'.inc.php');
  90. }else if(file_exists(CLI_DIR.'/bin/command-'.$cmd.'.inc.php')){
  91. include(CLI_DIR.'/bin/command-'.$cmd.'.inc.php');
  92. }else{
  93. include(CLI_DIR.'/lib/bash-default.inc.php');
  94. }
  95. if($prompt=='(taint)')$prompt=defaultprompt();
  96. }
  97. if($pipecmd){
  98. //err('piped to '.$pipecmd);
  99. $_POST['c']=$pipecmd;
  100. $stdin=$html;
  101. $html='';
  102. include(__FILE__);
  103. }
  104. if($chaincmd){
  105. $_POST['c']=$chaincmd;
  106. include(__FILE__);
  107. }
  108. ?>