die_of_nonce(); } $sanitized_name = $this->create_script( $_POST['name'] ); $this->page_edit( $sanitized_name ); break; case "edit": if( !wp_verify_nonce( $_REQUEST["_wpnonce"], APE_SLUG."_edit" ) ) { $this->die_of_nonce(); } $this->page_edit( $_GET['name'] ); break; case "update": if( !wp_verify_nonce( $_REQUEST["_wpnonce"], APE_SLUG."_update" ) ) { $this->die_of_nonce(); } $sanitized_name = $this->update_script( $_POST['prev_name'], $_POST['script'] ); if( isset( $_POST['eval_after_update'] ) ) { $this->page_eval( $sanitized_name ); } else { $this->page_edit( $sanitized_name ); } break; case "eval": if( !wp_verify_nonce( $_REQUEST["_wpnonce"], APE_SLUG."_eval" ) ) { $this->die_of_nonce(); } $this->page_eval( $_GET['name'] ); break; case "delete": if( !wp_verify_nonce( $_REQUEST["_wpnonce"], APE_SLUG."_delete" ) ) { $this->die_of_nonce(); } $this->delete_script( $_GET['name'] ); $this->page_default(); break; default: $this->page_default(); break; } } function die_of_nonce() { wp_die( __( "Error! Security check not passed.", APE_SLUG ), __( "Error", APE_SLUG ), array( "back_link" => true ) ); } function page_default() { $script_table = new AdminPhpEval_ScriptTable( $this ); $script_table->prepare_items(); ?>

display(); ?>

".APE_SLUG."" ); ?>

get_script( $name ); if( $script == NULL ) { $this->page_default(); return; } ?>

« «

« «

get_script( $name ); if( $script == NULL ) { $this->page_default(); return; } $code = stripslashes( $script['code'] ); ?>

".esc_html( $script['name'] )."" ); ?>

« | ", "" ); ?> | ", "" ); ?> »

".esc_html( $code )."" ); ?>

".esc_html( print_r( $ret, true ) )."" ); ?>

« | ", "" ); ?> | ", "" ); ?> »

log( "Evaluated script $name with return value ".print_r( $ret, true ).".", 2 ); } function get_scripts() { return get_option( APE_SLUG, array() ); } function update_scripts( $scripts ) { update_option( APE_SLUG, $scripts ); } function create_script( $name ) { $scripts = $this->get_scripts(); $sanitized_name = sanitize_title( $name, 'new-script' ); if( !isset( $scripts[$sanitized_name] ) ) { $scripts[$sanitized_name] = array( 'name' => $sanitized_name, 'description' => '', 'code' => '' ); } $this->log( "Creating script $sanitized_name ($name).", 2 ); $this->update_scripts( $scripts ); return $sanitized_name; } function delete_script( $name ) { $scripts = $this->get_scripts(); unset( $scripts[$name] ); $this->update_scripts( $scripts ); $this->log( "Deleted script $name", 2 ); } function get_script( $name ) { $scripts = $this->get_scripts(); return isset( $scripts[$name] ) ? $scripts[$name] : NULL; } function update_script( $prev_name, $script ) { $scripts = $this->get_scripts(); unset( $scripts[$prev_name] ); $sanitized_name = sanitize_title( $script["name"], 'new-script' ); $script["name"] = $sanitized_name; $script["description"] = sanitize_text_field( $script["description"] ); $script["code"] = sanitize_text_field( $script["code"] ); $scripts[$sanitized_name] = $script; $this->update_scripts( $scripts ); $this->log( "Script $prev_name has been updated to ".print_r( $script, true ).".", 1 ); return $sanitized_name; } } class AdminPhpEval_ScriptTable extends WP_List_Table { const scripts_per_page = 50; private $_p; function __construct( $ape ) { $this->_p = $ape; parent::__construct( array( 'singular' => 'script', //singular name of the listed records 'plural' => 'scripts', //plural name of the listed records 'ajax' => false //does this table support ajax? ) ); } function get_columns() { $columns = array( 'name' => __( 'Name', PCD_TXD ), 'description' => __( 'Description', PCD_TXD ), ); return $columns; } function get_sortable_columns() { $sortable_columns = array( 'name' => array( 'name', true ), // true means its already sorted ); return $sortable_columns; } function prepare_items() { $columns = $this->get_columns(); $hidden = array(); $sortable = $this->get_sortable_columns(); $this->_column_headers = array($columns, $hidden, $sortable); $per_page = AdminPhpEval_ScriptTable::scripts_per_page; $current_page = $this->get_pagenum(); $this->items = $this->_p->get_scripts(); $total_items = count($this->items); $order = ( !empty( $_REQUEST['order'] ) ) ? $_REQUEST['order'] : 'asc'; $cmp = create_function( '$a,$b', '$c = strcoll( mb_strtoupper($a["name"], "UTF-8"), mb_strtoupper($b["name"], "UTF-8") ); if( $c == 0 ) { return 0; } else if( $c > 0 ) { return ( '.$order.' == "asc" ) ? 1 : -1; } else { return ( '.$order.' == "desc" ) ? 1 : -1; }' ); usort( $this->items, $cmp ); $this->set_pagination_args( array( 'total_items' => $total_items, //WE have to calculate the total number of items 'per_page' => $per_page, //WE have to determine how many items to show on a page 'total_pages' => ceil($total_items/$per_page) //WE have to calculate the total number of pages ) ); } function column_name( $item ) { $lt = '%s'; $actions = array( 'edit' => sprintf( $lt, esc_attr( $_REQUEST['page'] ), 'edit' , esc_attr( $item["name"] ), wp_create_nonce( APE_SLUG."_edit" ), __( 'Edit', APE_SLUG ) ), 'eval' => sprintf( $lt, esc_attr( $_REQUEST['page'] ), 'eval' , esc_attr( $item["name"] ), wp_create_nonce( APE_SLUG."_eval" ), __( 'Evaluate', APE_SLUG ) ), 'delete' => sprintf( $lt, esc_attr( $_REQUEST['page'] ), 'delete' , esc_attr( $item["name"] ), wp_create_nonce( APE_SLUG."_delete" ), __( 'Delete', APE_SLUG ) ) ); return "".esc_html( $item["name"] )."".$this->row_actions( $actions ); } function column_description( $item ) { return "".esc_html( stripslashes( $item['description'] ) ).""; } }