PageRenderTime 25ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/dmAdminPlugin/modules/dmConsole/actions/actions.class.php

http://github.com/diem-project/diem
PHP | 107 lines | 79 code | 20 blank | 8 comment | 7 complexity | 11c3e3f8280668538535786387c6e7aa MD5 | raw file
Possible License(s): BSD-3-Clause, MIT, ISC
  1. <?php
  2. /**
  3. * dmMedia actions.
  4. *
  5. * @package diem
  6. * @subpackage dmMedia
  7. * @author Your name here
  8. * @version SVN: $Id: actions.class.php 12474 2008-10-31 10:41:27Z fabien $
  9. */
  10. class dmConsoleActions extends dmAdminBaseActions
  11. {
  12. protected static
  13. $defaultCommands = 'sf man ll ls pwd cat mkdir rm cp mv touch chmod free df find clear php';
  14. protected function getCommands()
  15. {
  16. return explode(' ', sfConfig::get('dm_console_commands', self::$defaultCommands));
  17. }
  18. protected function getAliases()
  19. {
  20. return sfConfig::get('dm_console_aliases', array('ll' => 'ls -l'));
  21. }
  22. public function executeCommand(sfWebRequest $request)
  23. {
  24. $command = trim($request->getParameter("dm_command"));
  25. if (substr($command, 0, 2) == "sf")
  26. {
  27. $command = substr($command, 3);
  28. $exec = sprintf('%s "%s" %s --color', sfToolkit::getPhpCli(), dmProject::getRootDir().'/symfony', $command);
  29. }
  30. else
  31. {
  32. $options = substr(trim($command), 0, 2) == 'll' || substr(trim($command), 0, 2) == 'ls' ? '--color' : '' ;
  33. $parts = explode(" ", $command);
  34. $parts[0] = dmArray::get($this->getAliases(), $parts[0], $parts[0]);
  35. $command = implode(" ", $parts);
  36. $parts = explode(" ", $command);
  37. $command = dmArray::get($this->getAliases(), $command, $command);
  38. if (!in_array($parts[0], $this->getCommands()))
  39. return $this->renderText(sprintf(
  40. "%s<li>This command is not available. You can do: <strong>%s</strong></li>",
  41. $this->renderCommand($command), implode(' ', $this->getCommands())
  42. ));
  43. $exec = sprintf("%s $options", $command);
  44. }
  45. ob_start();
  46. passthru($exec.' 2>&1', $return);
  47. $raw = dmAnsiColorFormatHtmlRenderer::render(ob_get_clean());
  48. $arr = explode("\n", $raw);
  49. $res = $this->renderCommand($command);
  50. foreach($arr as $a)
  51. $res .= "<li class='dm_result_command'><pre>".$a."</pre></li>";
  52. return $this->renderText($res);
  53. }
  54. protected function renderCommand($command)
  55. {
  56. return '<li class="dm_command_user">'.$this->getUser()->getAttribute('dm_console_prompt').$command.'</li>';
  57. }
  58. public function executeIndex(sfWebRequest $request)
  59. {
  60. $this->commands = implode(' ', $this->getCommands());
  61. $this->uname = php_uname();
  62. $filesystem = $this->context->getFilesystem();
  63. if ($filesystem->exec('whoami'))
  64. {
  65. $this->whoami = $filesystem->getLastExec('output');
  66. }
  67. else
  68. {
  69. $this->whoami = 'unknown_user';
  70. }
  71. $this->pwd = getcwd();
  72. $this->prompt = $this->whoami.'@'.php_uname('n').':'.'~/'.dmProject::unRootify($this->pwd).'$&nbsp;';
  73. $this->getUser()->setAttribute('dm_console_prompt', $this->prompt);
  74. $this->form = $this->getCommandForm();
  75. }
  76. protected function getCommandForm()
  77. {
  78. $form = new BaseForm;
  79. $form->setWidgetSchema(new sfWidgetFormSchema(array(
  80. 'dm_command' => new sfWidgetFormInputText
  81. )));
  82. $form->setValidatorSchema(new sfValidatorSchema(array(
  83. 'dm_command' => new sfValidatorString
  84. )));
  85. return $form;
  86. }
  87. }