Skip to content
ExecutePHP.php 1.47 KiB
Newer Older
<?php

namespace Drupal\devel\Form;

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;

/**
 * Defines a form that allows privileged users to execute arbitrary PHP code.
 */
class ExecutePHP extends FormBase {

  /**
   * {@inheritdoc}
   */
    return 'devel_execute_form';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form = array(
      '#title' => $this->t('Execute PHP Code'),
      '#description' => $this->t('Execute some PHP code'),
    );
    $form['execute']['code'] = array(
      '#type' => 'textarea',
      '#title' => t('PHP code to execute'),
      '#description' => t('Enter some code. Do not use <code>&lt;?php ?&gt;</code> tags.'),
      '#default_value' => (isset($_SESSION['devel_execute_code']) ? $_SESSION['devel_execute_code'] : ''),
      '#rows' => 20,
    );
    $form['execute']['actions'] = ['#type' => 'actions'];
    $form['execute']['actions']['op'] = [
      '#type' => 'submit',
      '#value' => t('Execute'),
    ];
    $form['#redirect'] = FALSE;
    if (isset($_SESSION['devel_execute_code'])) {
      unset($_SESSION['devel_execute_code']);
    }

  public function submitForm(array &$form, FormStateInterface $form_state) {
    $code = $form_state->getValue('code');
    print eval($code);
    $_SESSION['devel_execute_code'] = $code;