"Require all denied\nDeny from all\n", 'web.config' => '', 'index.php' => " $body ) { if ( ! is_file( $dir . $name ) ) { file_put_contents( $dir . $name, $body ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents -- PressHelm's own private folder. } } return $dir; } /** * Whether a name has an extension that could run on the server. * * @param string $name File name. * @return bool */ public static function executable( $name ) { $name = strtolower( (string) $name ); foreach ( explode( '.', $name ) as $i => $part ) { if ( $i > 0 && in_array( $part, self::EXEC_EXT, true ) ) { return true; // Also catches "x.php.csv" style double extensions. } } return '.' === substr( basename( $name ), 0, 1 ) || '.user.ini' === basename( $name ); } /** * Type of a file, from its bytes first (never only from the name). * * @param string $path File. * @param string $name Name. * @return string zip|image|pdf|csv|json|xml|text|binary */ public static function kind( $path, $name ) { $head = (string) file_get_contents( $path, false, null, 0, 4096 ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- local file in the box. $ext = strtolower( pathinfo( (string) $name, PATHINFO_EXTENSION ) ); if ( 0 === strpos( $head, "PK\x03\x04" ) || 0 === strpos( $head, "PK\x05\x06" ) ) { return 'zip'; } if ( 0 === strpos( $head, '%PDF-' ) ) { return 'pdf'; } if ( preg_match( '/^(\xFF\xD8\xFF|\x89PNG|GIF8|RIFF....WEBP)/s', $head ) ) { return 'image'; } if ( preg_match( '/^(MZ|\x7FELF|#!)/', $head ) || false !== stripos( $head, ' self::MAX_SIZE ) { wp_delete_file( $tmp ); return new \WP_Error( 'presshelm_file_size', 'The file is larger than 200 MB.' ); } $kind = self::kind( $tmp, $name ); if ( 'zip' === $kind ) { $code = self::zip_code( $tmp ); if ( '' !== $code ) { wp_delete_file( $tmp ); return new \WP_Error( 'presshelm_file_type', $code ); } } if ( 'binary' === $kind && ! in_array( strtolower( pathinfo( $name, PATHINFO_EXTENSION ) ), array( 'xlsx', 'xls', 'docx', 'doc', 'odt', 'ods', 'mp4', 'mp3', 'woff', 'woff2', 'ttf', 'otf', 'ico' ), true ) ) { wp_delete_file( $tmp ); return new \WP_Error( 'presshelm_file_type', 'This looks like a program or script, not a data file, so it was not stored.' ); } $id = 'f_' . bin2hex( random_bytes( 6 ) ); $dest = self::dir() . $id . '__' . $name; if ( ! @rename( $tmp, $dest ) ) { // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged, WordPress.WP.AlternativeFunctions.rename_rename -- falls back to copy below. if ( ! copy( $tmp, $dest ) ) { return new \WP_Error( 'presshelm_file_store', 'The file could not be stored on the site.' ); } wp_delete_file( $tmp ); } $meta = (array) get_option( self::OPTION . '_index', array() ); $meta[ $id ] = array( 'name' => $name, 'kind' => $kind, 'size' => $size, 'sha256' => hash_file( 'sha256', $dest ), 'origin' => substr( (string) $origin, 0, 200 ), 'at' => time(), ); update_option( self::OPTION . '_index', $meta, false ); return array( 'file_id' => $id ) + $meta[ $id ]; } /** * Path of a stored file. * * @param string $id File id. * @return array{path:string,record:array}|\WP_Error */ public static function get( $id ) { $meta = (array) get_option( self::OPTION . '_index', array() ); if ( ! preg_match( '/^f_[a-f0-9]{12}$/', (string) $id ) || empty( $meta[ $id ] ) ) { return new \WP_Error( 'presshelm_file_missing', 'No such file in PressHelm\'s file box (files are kept 7 days). See file_list.' ); } $path = self::dir() . $id . '__' . $meta[ $id ]['name']; if ( ! is_file( $path ) ) { return new \WP_Error( 'presshelm_file_missing', 'The file is no longer on the site.' ); } return array( 'path' => $path, 'record' => array( 'file_id' => $id ) + $meta[ $id ], ); } /** * Stored files, newest first. * * @return array */ public static function all() { self::cleanup(); $out = array(); foreach ( (array) get_option( self::OPTION . '_index', array() ) as $id => $r ) { $out[] = array( 'file_id' => $id ) + $r; } usort( $out, static function ( $a, $b ) { return $b['at'] <=> $a['at']; } ); return $out; } /** * Removes a stored file. * * @param string $id File id. * @return bool */ public static function remove( $id ) { $f = self::get( $id ); if ( ! is_wp_error( $f ) ) { wp_delete_file( $f['path'] ); } $meta = (array) get_option( self::OPTION . '_index', array() ); unset( $meta[ $id ] ); update_option( self::OPTION . '_index', $meta, false ); return true; } /** * Deletes expired files. * * @return void */ public static function cleanup() { $meta = (array) get_option( self::OPTION . '_index', array() ); $changed = false; foreach ( $meta as $id => $r ) { if ( (int) ( $r['at'] ?? 0 ) < time() - self::TTL_DAYS * DAY_IN_SECONDS ) { $path = self::dir() . $id . '__' . ( $r['name'] ?? '' ); if ( is_file( $path ) ) { wp_delete_file( $path ); } unset( $meta[ $id ] ); $changed = true; } } if ( $changed ) { update_option( self::OPTION . '_index', $meta, false ); } } }