_initialized ) { $this->_setVars(); $this->load(); load_plugin_textdomain( $this->_var, $this->_pluginRelativePath . '/lang' ); add_action( 'admin_menu', array( $this, 'addPages' ) ); $this->_initialized = true; } } } function addPages() { if ( function_exists( 'add_management_page' ) ) add_management_page( __( 'WP Easy Uploader', $this->_var ), __( 'Upload Files', $this->_var ), 'manage_options', __FILE__, array( $this, 'uploadsPage' ) ); } function _setVars() { $this->_class = get_class( $this ); $user = wp_get_current_user(); $this->_userID = $user->ID; // Thanks Ozh // http://planetozh.com/blog/2008/07/what-plugin-coders-must-know-about-wordpress-26/ if ( !defined( 'WP_CONTENT_URL' ) ) define( 'WP_CONTENT_URL', get_option( 'siteurl' ) . '/wp-content'); if ( !defined( 'WP_CONTENT_DIR' ) ) define( 'WP_CONTENT_DIR', ABSPATH . 'wp-content' ); $this->_pluginPath = WP_CONTENT_DIR . '/plugins/' . plugin_basename( dirname( __FILE__ ) ); $this->_pluginRelativePath = str_replace( ABSPATH, '', $this->_pluginPath ); $this->_pluginURL = WP_CONTENT_URL . '/plugins/' . plugin_basename( dirname( __FILE__ ) ); } // Options Storage //////////////////////////// function initializeOptions() { $this->_options['placeholder'] = 1; $this->save(); } function save() { $data = @get_option( $this->_var ); if ( isset( $data ) && ( $data === $this->_options ) ) return true; $data = $this->_options; return update_option( $this->_var, $data ); } function load() { $data = @get_option( $this->_var ); if ( is_array( $data ) ) $this->_options = $data; else $this->initializeOptions(); } // Pages ////////////////////////////////////// function uploadsPage() { $error = false; if ( ! empty( $_POST['upload'] ) ) { check_admin_referer( $this->_var . '-nonce' ); $uploads = array(); $file = array(); if ( 'plugin' == $_POST[$this->_var]['destinationSelection'] ) $uploads = array( 'path' => trailingslashit( WP_CONTENT_DIR ) . '/plugins/', 'url' => trailingslashit( WP_CONTENT_URL ) . 'plugins/', 'subdir' => '', 'error' => false ); elseif ( 'theme' == $_POST[$this->_var]['destinationSelection'] ) $uploads = array( 'path' => get_theme_root(), 'url' => get_theme_root_uri(), 'subdir' => '', 'error' => false ); elseif ( 'manual' == $_POST[$this->_var]['destinationSelection'] ) { if ( preg_match( '/^[\/\\\\]/', $_POST[$this->_var]['destinationPath'] ) ) $file['error'] = __( 'The Manual Path must be relative (cannot begin with \ or /).', $this->_var ); elseif ( preg_match( '/\.\./', $_POST[$this->_var]['destinationPath'] ) ) $file['error'] = __( 'Previous directory paths (..) are not permitted in the Manual Path.', $this->_var ); else { if ( empty( $_POST[$this->_var]['destinationPath'] ) ) { $path = ABSPATH; $url = get_option( 'siteurl' ); } else { $path = path_join( ABSPATH, $_POST[$this->_var]['destinationPath'] ); $url = trailingslashit( get_option( 'siteurl' ) ) . $_POST[$this->_var]['destinationPath']; } if ( ! wp_mkdir_p( $path ) ) $file['error'] = sprintf( __( 'Unable to create path %s. Ensure that the web server has permission to write to the parent of this folder.', $this->_var ), $path ); else $uploads = array( 'path' => $path, 'url' => $url, 'subdir' => '', 'error' => false ); } } $overwriteFile = ( ! empty( $_POST[$this->_var]['overwriteFile'] ) ) ? true : false; $renameIfExists = ( ! empty( $_POST[$this->_var]['renameIfExists'] ) ) ? true : false; if ( empty( $file['error'] ) ) { if ( ! empty( $_POST[$this->_var]['uploadURL'] ) ) $file = $this->getFileFromURL( $_POST[$this->_var]['uploadURL'], $uploads, $overwriteFile, $renameIfExists ); elseif ( ! empty( $_FILES['uploadFile']['name'] ) ) $file = $this->getFileFromPost( 'uploadFile', $uploads, $overwriteFile, $renameIfExists ); else $file['error'] = __( 'You must either provide a URL or a system file to upload.', $this->_var ); } if ( false === $file['error'] ) { $this->showStatusMessage( __( 'File successfully uploaded', $this->_var ) ); $extracted = false; if ( ! empty( $_POST[$this->_var]['extract'] ) ) { $forceExtractionFolder = ( ! empty( $_POST[$this->_var]['forceExtractionFolder'] ) ) ? true : false; $result = $this->extractArchive( $file, $forceExtractionFolder ); if ( true === $result['extracted'] ) { $path = str_replace( '/', '\\/', ABSPATH ); $destination = preg_replace( '/^' . $path . '/', '', $result['destination'] ); $this->showStatusMessage( sprintf( __( 'Archive successfully extracted to %s', $this->_var ), $destination ) ); $extracted = true; if ( ! empty( $_POST[$this->_var]['removeArchive'] ) ) { if ( unlink( $file['path'] ) ) $this->showStatusMessage( __( 'Archive removed', $this->_var ) ); else { $this->showErrorMessage( __( 'Unable to remove archive', $this->_var ) ); $error = true; } } } elseif ( false !== $result['error'] ) { $this->showErrorMessage( $result['error'] ); $error = true; } } if ( ! $extracted ) { ini_set( 'display_errors', '1' ); error_reporting( E_ALL ); $path = ABSPATH; $path = str_replace( '\\', '\\\\', $path ); $path = str_replace( '/', '\\/', $path ); $destination = preg_replace( '/^' . $path . '/', '', $file['path'] ); $message = '
' . sprintf( __( 'Path: %s', $this->_var ), $destination ) . '
'; $message .= '' . sprintf( __( 'URL: %s', $this->_var ), $file['url'], $file['url'] ) . '
'; $this->showStatusMessage( $message ); } } else { $this->showErrorMessage( $file['error'] ); $error = true; } } ?>upload_max_filesize directive in php.ini.", $this->_var ),
__( "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.", $this->_var ),
__( "The uploaded file was only partially uploaded.", $this->_var ),
__( "No file was uploaded.", $this->_var ),
__( "Missing a temporary folder.", $this->_var ),
__( "Failed to write file to disk.", $this->_var ) );
// All tests are on by default. Most can be turned off by $override[{test_name}] = false;
$test_form = true;
$test_size = true;
// If you override this, you must provide $ext and $type!!!!
$test_type = true;
$mimes = false;
// Customizable overrides
$uploads = wp_upload_dir();
$overwriteFile = false;
$renameIfExists = true;
$message = '';
// Install user overrides. Did we mention that this voids your warranty?
if ( is_array( $overrides ) )
extract( $overrides, EXTR_OVERWRITE );
// A correct form post will pass this test.
if ( $test_form && (!isset( $_POST['action'] ) || ($_POST['action'] != $action ) ) )
return $upload_error_handler( $file, __( 'Invalid form submission.', $this->_var ) );
// A successful upload will pass this test. It makes no sense to override this one.
if ( $file['error'] > 0 )
return $upload_error_handler( $file, $upload_error_strings[$file['error']] );
// A non-empty file will pass this test.
if ( $test_size && !($file['size'] > 0 ) )
return $upload_error_handler( $file, __( 'File is empty. Please upload something more substantial. This error could also be caused by uploads being disabled in your php.ini.', $this->_var ) );
// A properly uploaded file will pass this test. There should be no reason to override this one.
if (! @ is_uploaded_file( $file['tmp_name'] ) )
return $upload_error_handler( $file, __( 'Specified file failed upload test.', $this->_var ) );
// A correct MIME type will pass this test. Override $mimes or use the upload_mimes filter.
if ( $test_type ) {
$wp_filetype = wp_check_filetype( $file['name'], $mimes );
extract( $wp_filetype );
if ( ( !$type || !$ext ) && !current_user_can( 'unfiltered_upload' ) )
return $upload_error_handler( $file, __( 'File type does not meet security guidelines. Try another.', $this->_var ) );
if ( !$ext )
$ext = ltrim(strrchr($file['name'], '.'), '.');
if ( !$type )
$type = $file['type'];
}
// A writable uploads dir will pass this test. Again, there's no point overriding this one.
if ( false !== $uploads['error'] )
return $upload_error_handler( $file, $uploads['error'] );
$uploads['path'] = untrailingslashit( $uploads['path'] );
$uploads['path'] = preg_replace( '/\/+/', '/', $uploads['path'] );
$uploads['url'] = untrailingslashit( $uploads['url'] );
$file['name'] = preg_replace( '/\s+/', '_', $file['name'] );
$filename = $this->unique_filename( $uploads['path'], $file['name'] );
if ( file_exists( $uploads['path'] . '/' . $file['name'] ) ) {
if ( $overwriteFile )
$filename = $file['name'];
elseif ( ! $renameIfExists )
return $upload_error_handler( $file, __( 'The file already exists. Since overwriting and renaming are not permitted, the file was not added.', $this->_var ) );
}
if ( false === @ move_uploaded_file( $file['tmp_name'], $uploads['path'] . '/' . $filename ) ) {
if ( $overwriteFile ) {
$filename = $this->unique_filename( $uploads['path'], $file['name'] );
if ( false === @ move_uploaded_file( $file['tmp_name'], $uploads['path'] . '/' . $filename ) )
return $upload_error_handler( $file, sprintf( __( 'The uploaded file could not be moved to %s. Please check the folder and file permissions.', $this->_var ), $uploads['path'] ) );
else
$message = __( 'Unable to overwrite existing file. Since renaming is permitted, the file was saved with a new name.', $this->_var );
}
else
return $upload_error_handler( $file, sprintf( __( 'The uploaded file could not be moved to %s. Please check the folder and file permissions.', $this->_var ), $uploads['path'] ) );
}
$stat = stat( dirname( $uploads['path'] . '/' . $filename ) );
$perms = $stat['mode'] & 0000666;
@ chmod( $uploads['path'] . '/' . $filename, $perms );
// Compute the URL
$url = $uploads['url'] . '/' . $filename;
$return = apply_filters( 'wp_handle_upload', array( 'file' => $uploads['path'] . '/' . $filename, 'url' => $url, 'message' => $message, 'error' => false ) );
return $return;
}
function extractArchive( $file, $forceExtractionFolder = true ) {
$extensions = array( 'zip', 'tar', 'gz', 'tar.gz', 'tgz', 'tar.bz2', 'tbz' );
$extension = $this->getExtension( $file['path'] );
$originalIncludePath = ini_get( 'include_path' );
ini_set( 'include_path', dirname(__FILE__) . '/pear' );
if ( ! function_exists( 'file_archive_cleancache' ) )
require_once( 'File/Archive.php' );
$retval = array();
if ( in_array( $extension, (array) $extensions ) ) {
if ( is_callable( array( 'File_Archive', 'extract' ) ) && is_callable( array( 'File_Archive', 'read' ) ) ) {
$backupCWD = getcwd();
$path = dirname( $file['path'] );
chdir( $path );
$source = basename( $file['path'] ) . '/';
if ( $forceExtractionFolder )
$destination = basename( $file['path'], ".{$extension}" );
else
$destination = $path;
$error = File_Archive::extract( $source, $destination );
chdir( $backupCWD );
if ( PEAR::isError( $error ) )
$retval = array( 'extracted' => false, 'error' => sprintf( __( 'Extraction failed: %s', $this->_var ), $error->getMessage() ) );
else
$retval = array( 'destination' => path_join( $path, $destination ), 'extracted' => true, 'error' => false );
}
else
$retval = array( 'extracted' => false, 'error' => __( 'Unable to execute File_Archive::extract', $this->_var ) );
}
else
$retval = array( 'extracted' => false, 'error' => false );
ini_set( 'include_path', $originalIncludePath );
return $retval;
}
}
}
if ( class_exists( 'WPEasyUploader' ) ) {
$wpeasyuploader = new WPEasyUploader();
}
?>