Skip to content

Instantly share code, notes, and snippets.

@rmccue
Last active March 28, 2017 21:22
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rmccue/ec6a7d79349ea7e9b206 to your computer and use it in GitHub Desktop.
Save rmccue/ec6a7d79349ea7e9b206 to your computer and use it in GitHub Desktop.
WordPress test server - front controller for testing WP. *****NEVER USE THIS ON PRODUCTION******
<?php
/**
* WordPress Test Server
*
* This serves up a full WordPress site via the PHP test server (`php -S`), and
* is intended purely for serving WP for UI testing. Please do not ever use this
* in production.
*/
// Path to WP install
$basedir = realpath( __DIR__ . '/../..' );
$wpdir = $basedir . '/wordpress';
// Ensure we can't load files outside of our WP install
ini_set( 'open_basedir', $basedir );
$path = null;
if ( !empty( $_SERVER['REQUEST_URI'] ) && $_SERVER['REQUEST_URI'] !== '/' ) {
$requested = parse_url( $_SERVER['REQUEST_URI'], PHP_URL_PATH );
$search = array(
$basedir, // /wordpress/wp-includes/a.jpg
$wpdir, // /wp-includes/a.jpg
);
foreach ( $search as $dir ) {
$full = realpath( $dir . $requested );
if ( file_exists( $full ) ) {
$path = $full;
}
}
}
if ( empty( $path ) ) {
require $basedir . '/index.php';
return;
}
$info = new finfo( FILEINFO_SYMLINK );
$mime_type = $info->file($path, FILEINFO_MIME_TYPE);
if ( $mime_type === 'text/x-php' ) {
include $path;
return;
}
$mime_header = $info->file($path, FILEINFO_MIME_ENCODING);
header( sprintf( 'Content-Type: %s', $mime_header ) );
readfile( $path );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment