Skip to content

Commit

Permalink
Issue #71 - re-enable tracing with all trace files written into the t…
Browse files Browse the repository at this point in the history
…race files directory
  • Loading branch information
bobbingwide committed May 21, 2018
1 parent e131c3d commit bb3a166
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 47 deletions.
7 changes: 5 additions & 2 deletions admin/class-oik-trace-summary.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,9 @@ function get_summary_file_prefix() {
$file_prefix = trim( $file_prefix );
if ( empty( $file_prefix ) ) {
$file_prefix = "bwtrace.vt";
gob();

}

return $file_prefix;

}
Expand Down Expand Up @@ -179,10 +180,12 @@ function bw_record_vt( $vnoisy=false ) {
* @return string Fully qualified file name
*/
function bw_trace_vt_file() {
global $bw_trace;
$fq_trace_files_directory = $bw_trace->trace_files_directory->get_fq_trace_files_directory();

$bwtracevt = $this->get_summary_file_prefix();

$file = ABSPATH . $bwtracevt . "." . date( "Ymd" );
$file = $fq_trace_files_directory . $bwtracevt . "." . date( "Ymd" );
global $blog_id;
bw_trace2( $blog_id, "blog_id !$blog_id!", false, BW_TRACE_VERBOSE );
if ( $blog_id != 1 ) {
Expand Down
13 changes: 7 additions & 6 deletions admin/oik-bwtrace.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ function oik_trace_options() {
stag( 'table class="form-table"' );
bw_flush();
settings_fields('bw_trace_options_options');

BW_::bw_textfield_arr( "bw_trace_options", __( "Trace files directory", "oik-bwtrace" ), $options, 'trace_directory', 60 );
bw_tablerow( array( __( "General browser requests", "oik-bwtrace" ) ), "tr", "th" );
BW_::bw_textfield_arr( "bw_trace_options", __( "Trace file", "oik-bwtrace" ), $options, 'file', 60 );
bw_checkbox_arr( "bw_trace_options", __( "Trace enabled", "oik-bwtrace" ), $options, 'trace' );
Expand Down Expand Up @@ -184,7 +186,6 @@ function oik_trace_options() {

// Does this need includes/bwtrace.php?
bw_tablerow( array( __( "Trace records", "oik-bwtrace" ) ), "tr", "th" );
BW_::bw_textfield_arr( "bw_trace_options", __( "Trace files directory", "oik-bwtrace" ), $options, 'trace_directory', 60 );
$trace_levels = bw_list_trace_levels();
// Do we need to default this after upgrade?
//$options['level'] = bw_trace_level();
Expand Down Expand Up @@ -226,15 +227,15 @@ function oik_trace_notes() {
BW_::p( __( "The oik-bwtrace plugin should <b>not</b> need to be activated on a live site.", "oik-bwtrace" ) );
BW_::p( __( "If you do need to activate it, only do so for a short period of time.", "oik-bwtrace" ) );

BW_::p( __( "You will need to specify the trace file name (e.g. bwtrace.loh )", "oik-bwtrace" ) );
BW_::p( __( "You will need to specify the trace files directory and trace file names.", "oik-bwtrace" ) );
BW_::p( __( "When you want to trace processing check 'Trace enabled'", "oik-bwtrace" ) );
//BW_::p( __( "Check 'Reset trace file every transaction' to cause the trace file to be cleared for every request, including AJAX requests.", "oik-bwtrace" ) );
BW_::p( __( "Specifying a different file name and limit for each transaction can help you to trace multiple concurrent requests." ) );

BW_::p( __( "You may find the most recent browser trace output at...", "oik-bwtrace" ) );
$bw_trace_url = bw_trace_url();
//BW_::p( __( "You may find the most recent browser trace output at...", "oik-bwtrace" ) );
//$bw_trace_url = bw_trace_url();

BW_::alink( NULL, $bw_trace_url, $bw_trace_url, __( "View trace output in your browser.", "oik-bwtrace" ) );
//BW_::alink( NULL, $bw_trace_url, $bw_trace_url, __( "View trace output in your browser.", "oik-bwtrace" ) );

BW_::p( __( "If you want to trace processing within some content you can use two shortcodes: [bwtron] to turn trace on and [bwtroff] to turn it off", "oik-bwtrace" ) );

Expand Down Expand Up @@ -349,7 +350,7 @@ function bw_trace_validate_directory( $array, $key ) {
$trace_files_directory->validate_trace_files_directory( $directory );
$valid = $trace_files_directory->is_valid();
if ( !$valid ) {
add_settings_error( $key, $key, "Invalid trace files directory." );
add_settings_error( $key, $key, "Invalid Trace files directory." );
}

}
Expand Down
21 changes: 4 additions & 17 deletions includes/class-trace-file-selector.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,22 +59,9 @@ public function __construct() {
* @param object $trace_files_directory
*/
public function set_trace_files_directory( $trace_files_directory=null ) {
$this->trace_files_directory = null;
$this->trace_files_directory = $trace_files_directory;
}

/**
* Validates the trace directory
*
* Trace files can contain sensitive data so should not be accessible to the general public.
* This can be achieved by placing the files outside of the web root directory
* or in a folder protected by .htaccess
*
* @param string $directory
* @return bool validity
*/
public function validate_trace_directory( $directory ) {

}

/**
* Sets the limit
Expand Down Expand Up @@ -134,9 +121,9 @@ public function get_generation() {
* @param string $file_path fully qualified location including trailing slash
*/
public function set_file_path( $file_path=null ) {
if ( !$file_path ) {
$file_path = $this->get_abspath();
}
//if ( !$file_path ) {
// $file_path = $this->get_abspath();
//}
$this->file_path = trailingslashit( $file_path );
}

Expand Down
172 changes: 150 additions & 22 deletions includes/class-trace-files-directory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,79 +20,141 @@
* ```
*
* Note: If running locally this logic doesn't matter so much.
* We can't easily check we're running locally so we'll defer to the production strength logic.
* But, since we can't easily check we're running locally we'll defer to the production strength logic...
* even though we don't expect tracing to be run in production.
*
*
* We should also ensure that the path is not a WordPress folder.
*
*
* $directory | Processing
* ---------- | ------------
* null | Don't support trace
* 0 | Don't support trace. Note: empty() returns true for "0"
* starts / | Treat as fully qualified
* starts C:/ | Treat as fully qualified - Windows only - where C is a drive letter
* directory | Prepend ABSPATH and check directory exists
* starts . | ?
* starts ../ | ?
*
*
*
*/

class trace_files_directory {

/**
* Value entered by user.
* May be set as a constant, in wp-config.php, for when tracing at startup is required.
*/
public $trace_files_directory;
public $options;
public $valid = false;

/**
* Fully qualified trace files directory, with trailing slash
* Trace files are created in this directory, either directly or within subdirectories.
*/
public $fq_trace_files_directory;

/**
* Fully qualified prefix
*
* Prefix for a fully qualified file name.
*/
private $fq_prefix;

function __construct() {
$this->valid = false;
$this->default_options();
$this->set_trace_files_directory();
$this->set_fq_trace_files_directory();
$this->set_fq_prefix();

}

/**
* Validates the trace files directory
*
* Sets the FQ trace files directory if valid
*
* Trace files can contain sensitive data so should not be accessible to the general public.
* This can be achieved by placing the files outside of the web root directory.
*
* A less secure method is to place the files in a folder protected by .htaccess
*
* $directory | Processing
* ---------- | ------------
* null | Don't support trace
* 0 | Don't support trace. Note: empty() returns true for "0"
* starts / | Treat as fully qualified
* starts C:/ | Treat as fully qualified - Windows only - where C is a drive letter
* directory | Prepend ABSPATH and check directory exists
* starts . | ?
* starts ../ | ?
*
*
* @param string $directory
* @return bool validity
*/
function validate_trace_files_directory() {

$this->valid = false;
$fq_directory = $this->set_fq_prefix();
$fq_directory = null;
$fq_directory .= $this->trace_files_directory;
if ( file_exists( $fq_directory ) ) {
if ( is_dir( $fq_directory ) ) {
$this->set_fq_trace_files_directory( $fq_directory );
$this->valid = true;
} else {
echo "File is not a directory";
}
} else {
//echo "Directory does not exist";
$this->valid = wp_mkdir_p( $fq_directory );
if ( !$this->valid ) {
echo "Cannot create directory";
}
}
return $this->valid;

}


function default_options() {
$this->options = array();
$this->options[ 'trace_directory' ] = null;
}

/**
* Sets the trace files directory
*/
function set_trace_files_directory( $directory=null ) {
$this->trace_files_directory = null;
$this->trace_files_directory = $directory;
}



/**
* Sets local variables from the options array
*/
function set_options( $options ) {
$this->trace_files_directory = bw_array_get( $options, 'trace_directory' );

$this->set_trace_files_directory( bw_array_get( $options, 'trace_directory' ) );
}

function validate_trace_files_directory() {
$this->valid = false;
/**
*/
public function validate_trace_directory( $directory ) {
}

function is_valid() {
return $this->valid;
}

/**
*
* Stored with a trailing '/'
*/
function set_fq_trace_files_directory( $directory=null ) {
$this->fq_trace_files_directory = $directory;
$this->fq_trace_files_directory = trailingslashit( $directory );
}

/**
* Returns the fully qualified trace files directory
*
*
* Returned with a trailing slash - like ABSPATH
*/
function get_fq_trace_files_directory() {
if ( !$this->fq_trace_files_directory ) {
gob();
}
return $this->fq_trace_files_directory;
}

Expand All @@ -115,6 +177,72 @@ public function get_abspath() {
}
return $abspath;
}

/**
* Builds the fully qualified prefix for a file
*
* Takes the operating system into account
*/
function set_fq_prefix() {
$new_file = $this->query_external_dir();
if ( PHP_OS == "WINNT" ) {
$new_file = $this->query_windows_homedrive();
$new_file .= '/';
} else {
$new_file = '/';
}
$this->fq_prefix = $new_file;
return $this->fq_prefix;

}

/**
* Builds the external directory name
*
* For non Windows servers (e.g. Linux) we need to find the "home" directory
* e.g.
* If [DOCUMENT_ROOT] => /home/t10scom/public_html
* and $dir parameter is '/zipdir/'
* then external_directory will become "/home/t10scom/zipdir/"
*
* @param string - required external directory name with leading and trailing slashes
* @return string - external directory with "home" directory prepended
*/
function query_external_dir() {
$external_dir = dirname( $_SERVER['DOCUMENT_ROOT'] );
//print_r( $_SERVER['DOCUMENT_ROOT'] );
//print_r( $_SERVER );
return $external_dir;
}

/**
* Query Windows System Drive
*
* Obtains the drive letter to prefix a fully qualified file name in a Windows environment
*
* Notes: Variable settings vary depending on the invocation
*
* Value | CLI | Server
* -------------------------- | ---- | ----
* $_ENV | null | set
* getenv( "HOMEDRIVE" ) | set | null
* getenv( "ServerDrive" ) | set | set
* $_SERVER[ 'DOCUMENT_ROOT'] | null | set
*
*/
function query_windows_homedrive() {
//print_r( $_ENV );
//$home = getenv();
//print_r( $home );
//$homedrive = getenv( "HOMEDRIVE" );
$systemdrive = getenv( "SystemDrive" );
//print_r( $homedrive );
//print_r( $systemdrive);
//$homepath = getenv( "HOMEPATH" );
//print_r( $homepath );
return $systemdrive;
}

}

Expand Down

0 comments on commit bb3a166

Please sign in to comment.