ExecutePHP.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. namespace Drupal\devel\Form;
  3. use Drupal\Core\Form\FormBase;
  4. use Drupal\Core\Form\FormStateInterface;
  5. /**
  6. * Defines a form that allows privileged users to execute arbitrary PHP code.
  7. */
  8. class ExecutePHP extends FormBase {
  9. /**
  10. * {@inheritdoc}
  11. */
  12. public function getFormId() {
  13. return 'devel_execute_form';
  14. }
  15. /**
  16. * {@inheritdoc}
  17. */
  18. public function buildForm(array $form, FormStateInterface $form_state) {
  19. $form = array(
  20. '#title' => $this->t('Execute PHP Code'),
  21. '#description' => $this->t('Execute some PHP code'),
  22. );
  23. $form['execute']['code'] = array(
  24. '#type' => 'textarea',
  25. '#title' => t('PHP code to execute'),
  26. '#description' => t('Enter some code. Do not use <code>&lt;?php ?&gt;</code> tags.'),
  27. '#default_value' => (isset($_SESSION['devel_execute_code']) ? $_SESSION['devel_execute_code'] : ''),
  28. '#rows' => 20,
  29. );
  30. $form['execute']['op'] = array('#type' => 'submit', '#value' => t('Execute'));
  31. $form['#redirect'] = FALSE;
  32. if (isset($_SESSION['devel_execute_code'])) {
  33. unset($_SESSION['devel_execute_code']);
  34. }
  35. return $form;
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function submitForm(array &$form, FormStateInterface $form_state) {
  41. ob_start();
  42. $code = $form_state->getValue('code');
  43. print eval($code);
  44. $_SESSION['devel_execute_code'] = $code;
  45. dpm(ob_get_clean());
  46. }
  47. }