sant0sk1 / wordpress-console

An interactive console for WordPress developers

This URL has Read+Write access

wordpress-console / query.php
100644 71 lines (53 sloc) 1.676 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
require('common.php');
 
set_error_handler('console_error_handler');
 
$secret = get_option('wordpress-console-secret');
if ( !$secret )
return;
 
if ( !isset($_POST['signature']) || !$_POST['signature'] )
return;
 
if (isset($_POST['query'])) {
 
  if ( hash_hmac('sha1', stripslashes($_POST['query']), $secret) != $_POST['signature'] )
    return;
 
  $existing_vars = get_defined_vars();
 
  // restore session variables if they exist
  if (isset($_SESSION['console_vars'])) {
    extract(eval("return " . $_SESSION['console_vars'] . ";"));
  }
 
  $query = stripslashes($_POST['query']);
  
  // append query to current partial query if there is one
  if (isset($_SESSION['partial'])) {
    $query = $_SESSION['partial'] . $query;
  }
 
  try {
    if (parse($query) == 0) {
      $response = array();
 
      // start output buffer (to capture prints)
      ob_start();
      $rval = eval($_SESSION['code']);
 
      // eval'd code had a return value
      if ($rval != NULL) {
        $response['rval'] = $rval;
      }
      $response['output'] = ob_get_contents();
 
      // quietly discard buffered output
      ob_end_clean();
      print json_encode($response);
      // clear the code buffer
      $_SESSION['code'] = '';
      $_SESSION['partial'] = '';
    } else {
      print json_encode(array('output' => 'partial'));
    }
  } catch(Exception $exception) {
    error($exception->getMessage());
  }
 
  // store variables to session
  $current_vars = get_defined_vars();
 
  save_variables($existing_vars,
    $current_vars,
    array('query','response','rval','existing_vars','current_vars','_SESSION'));
 
} else {
  error('Error initializing session.');
}
 
?>