Skip to content

Commit

Permalink
load up admin functions. format for better legibility
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.wp-plugins.org/wordpress-console/trunk@260251 b8457f37-d9ea-0310-8a92-e5e31aec5664
  • Loading branch information
sant0sk1 committed Jul 4, 2010
1 parent ffad816 commit ee65426
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 72 deletions.
43 changes: 22 additions & 21 deletions common.php
@@ -1,30 +1,31 @@
<?php
require_once dirname(__FILE__) . "/../../../wp-load.php";
require_once( dirname( __FILE__ ) . "/../../../wp-load.php" );
require_once( ABSPATH . 'wp-admin/includes/admin.php' );

if (!session_id()) {
if ( !session_id() ) {
session_start();
}

@ob_end_clean();
error_reporting(E_ALL);
set_time_limit(0);
ob_end_clean();
error_reporting( E_ALL );
set_time_limit( 0 );

if (!function_exists('json_encode')) {
function json_encode($value) {
@require_once('lib/FastJSON.class.php');
if ( !function_exists( 'json_encode' ) ) {
function json_encode( $value ) {
require_once( 'lib/FastJSON.class.php' );
return FastJSON::encode($value);
}
}

function console_error_handler($errno,$errorstr) {
error($errorstr);
function console_error_handler( $errno, $errorstr ) {
error( $errorstr );
}

function error($error) {
exit(json_encode(array('error' => $error)));
function error( $error ) {
exit( json_encode( array( 'error' => $error ) ) );
}

function logit($msg) {
function logit( $msg ) {
$file = "/tmp/console.log";
$fh = fopen($file,'a');
fwrite($fh,$msg);
Expand All @@ -34,29 +35,29 @@ function logit($msg) {

// saves newly defined variables to session.
// somebody please refactor this!
function save_variables($existing, $current, $ignore) {
$new_vars = array_diff(array_keys($current),array_keys($existing));
$user_vars = array_diff($new_vars,$ignore);
function save_variables( $existing, $current, $ignore ) {
$new_vars = array_diff( array_keys( $current ), array_keys( $existing ) );
$user_vars = array_diff( $new_vars, $ignore );

$save_vars = array();

foreach($current as $key => $value) {
if (in_array($key,$user_vars)) {
foreach( $current as $key => $value ) {
if ( in_array( $key, $user_vars ) ) {
$save_vars[$key] = $value;
}
}

// purge any references to stdClass::__set_state() as advised here:
// http://drupal.org/node/215375
$export = var_export($save_vars,true);
$final = preg_replace("/stdClass::__set_state\((.*)\)/Ums",'$1',$export);
$export = var_export( $save_vars, true );
$final = preg_replace( "/stdClass::__set_state\((.*)\)/Ums", '$1', $export );
$_SESSION['console_vars'] = $final;
}

// this function was yoinked (and adjusted) from the 'php shell' project. See:
// http://jan.kneschke.de/projects/php-shell
// return int 0 if a executable statement is in the session buffer, non-zero otherwise
function parse($code) {
function parse( $code ) {
## remove empty lines
if (trim($code) == '') return 1;

Expand Down
102 changes: 51 additions & 51 deletions query.php
@@ -1,70 +1,70 @@
<?php
require('common.php');
require_once( 'common.php' );

set_error_handler('console_error_handler');
set_error_handler( 'console_error_handler' );

$secret = get_option('wordpress-console-secret');
if ( !$secret )
$secret = get_option( 'wordpress-console-secret' );
if ( !$secret ) {
return;
}

if ( !isset( $_POST['signature'] ) || !$_POST['signature'] ) {
return;
}

if ( !isset($_POST['signature']) || !$_POST['signature'] )
if ( !isset( $_POST['query'] ) || !$_POST['query'] ) {
return;
}

if (isset($_POST['query'])) {
$query = stripslashes( $_POST['query'] );

if ( hash_hmac('sha1', stripslashes($_POST['query']), $secret) != $_POST['signature'] )
return;
if ( hash_hmac( 'sha1', $query, $secret ) != $_POST['signature'] ) {
return;
}

$existing_vars = get_defined_vars();
$existing_vars = get_defined_vars();

// restore session variables if they exist
if (isset($_SESSION['console_vars'])) {
extract(eval("return " . $_SESSION['console_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;
}

// 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();

try {
if (parse($query) == 0) {
$response = array();

ob_start(); // start output buffer (to capture prints)
$rval = eval($_SESSION['code']);
$response['output'] = ob_get_contents();
ob_end_clean(); // quietly discard buffered output

if ($rval != NULL) {
ob_start(); // do it again, this time for the return value
print_r($rval);
$response['rval'] = ob_get_contents();
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());
}
ob_start(); // start output buffer (to capture prints)
$rval = eval( $_SESSION['code'] );
$response['output'] = ob_get_contents();
ob_end_clean(); // quietly discard buffered output

// store variables to session
$current_vars = get_defined_vars();
if ( $rval != NULL ) {
ob_start(); // do it again, this time for the return value
print_r( $rval );
$response['rval'] = ob_get_contents();
ob_end_clean();
}

save_variables($existing_vars,
$current_vars,
array('query','response','rval','existing_vars','current_vars','_SESSION'));
// clear the code buffer
$_SESSION['code'] = '';
$_SESSION['partial'] = '';

} else {
error('Error initializing session.');
print json_encode( $response );
} else {
print json_encode( array( 'output' => 'partial' ) );
}
} catch( Exception $exception ) {
error( $exception->getMessage() );
}

// store variables to session
$current_vars = get_defined_vars();
$ignore = array( 'query','response','rval','existing_vars','current_vars','_SESSION' );

save_variables( $existing_vars, $current_vars, $ignore );
?>

0 comments on commit ee65426

Please sign in to comment.