Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Package type #130

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/HTTP/ResponseBody/FileBody.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ class FileBody implements ResponseBody {
*/
public function __construct( string $filename ) {
$result = validate_file( $filename );

// if windows file path is found, ignore in
// in case wamp or such server is used
if ( 2 === $result ) {
$result = 0;

// normalize windows file path
$filename = wp_normalize_path( $filename );
}

if ( 0 !== $result ) {
throw InvalidFileName::withValidationCode( $filename, $result );
}
Expand Down
4 changes: 2 additions & 2 deletions src/Provider/RewriteRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ public function register_rewrite_rules() {
// Don't add a file extension. Some servers don't route file extensions
// through WordPress' front controller.
add_rewrite_rule(
'satispress/([^/]+)(/([^/]+))?$',
'index.php?satispress_route=download&satispress_params[slug]=$matches[1]&satispress_params[version]=$matches[3]',
'satispress/([^/]+)(/([^/]+))(/([^/]+))?$',
'index.php?satispress_route=download&satispress_params[slug]=$matches[3]&satispress_params[version]=$matches[5]&satispress_params[type]=$matches[1]',
'top'
);
}
Expand Down
18 changes: 15 additions & 3 deletions src/Release.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,13 @@ public function __construct( Package $package, string $version, string $source_u
* @return string
*/
public function get_download_url( array $args = [] ): string {

// un-prefix the package type
$package_type = str_replace( 'wordpress-', '', $this->get_package()->get_type() );

$url = sprintf(
'/satispress/%s/%s',
'/satispress/%s/%s/%s',
$package_type,
$this->get_package()->get_slug(),
$this->get_version()
);
Expand All @@ -79,11 +84,18 @@ public function get_download_url( array $args = [] ): string {
* @return string
*/
public function get_file_path(): string {
return sprintf(
'%1$s/%2$s',

// un-prefix the package type
$package_type = str_replace( 'wordpress-', '', $this->get_package()->get_type() );

$path = sprintf(
'%1$s/%2$s/%3$s',
$package_type,
$this->get_package()->get_slug(),
$this->get_file()
);

return $path;
}

/**
Expand Down
14 changes: 13 additions & 1 deletion src/Route/Download.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ class Download implements Route {
*/
const PACKAGE_VERSION_REGEX = '/[^0-9a-z.-]+/i';

/**
* Regex for sanitizing package package type.
*
* @var string
*/
const PACKAGE_TYPE_REGEX = '/[^A-Za-z0-9._\-]+/i';

/**
* Release manager.
*
Expand Down Expand Up @@ -103,7 +110,12 @@ public function handle( Request $request ): Response {
$version = preg_replace( self::PACKAGE_VERSION_REGEX, '', $request['version'] );
}

$package = $this->repository->first_where( [ 'slug' => $slug ] );
$type = '';
if ( ! empty( $request['type'] ) ) {
$type = preg_replace( self::PACKAGE_TYPE_REGEX, '', $request['type'] );
}

$package = $this->repository->first_where( [ 'slug' => $slug, 'type' => $type ] );

// Send a 404 response if the package doesn't exist.
if ( ! $package instanceof Package ) {
Expand Down
2 changes: 2 additions & 0 deletions src/Transformer/ComposerPackageTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ public function transform( Package $package ) {
$builder = $this->factory->create( 'composer' )->with_package( $package );

$vendor = apply_filters( 'satispress_vendor', 'satispress' );
$vendor = $vendor. '-' . $package->get_type();

$name = $this->normalize_package_name( $package->get_slug() );
$builder->set_name( $vendor . '/' . $name );

Expand Down