Skip to content
This repository has been archived by the owner on Aug 14, 2023. It is now read-only.

Indicate when the license key is valid, but the domain isn't registered for the license key #24

Open
danielbachhuber opened this issue Dec 29, 2016 · 2 comments

Comments

@danielbachhuber
Copy link

When a user has provided a valid license key, they'll see a prompt to update the plugin when there's a new version available.

However, if the domain isn't registered to the license key, they'll see this cryptic error when they try to update:

image

It would be better to intercept the user earlier so they don't try the update process and end up in this state.

Similarly, "Download Failed: Unauthorized" would be better as "Download Failed: License key expired or not activated for URL".

@Spreeuw
Copy link

Spreeuw commented Jul 7, 2017

EDD SL actually returns a body with the 401 (Unauthorized) that is much more informative (if you copy the link and open it in your browser, you'll see). But cUrl doesn't take a body for 40X errors so it gets lost with a generic 'Unauthorized' (more about the cUrl options here)

I guess one solution could be to use the http_response filter to check for 401's for your own license server and then make an additional request to get the body (although you'd need to use a different URL to circumvent that same 401 issue).

Here's a somewhat more simple solution, that at least overrides the generic 'Unauthorized':

add_filter( 'http_response', 'http_response_debug', 10, 3 );
function http_response_debug($response, $args, $url) {
	if (isset($response['response']) && is_array($response['response'])) {
		if (isset($response['response']['code']) && $response['response']['code'] == 401 ) {
			// we have a 401 response, check if it's ours
			$license_server = 'https://www.yoursite.com';
			if (strpos($url, $license_server) !== false && strpos($url, 'package_download') !== false) {
				// this is our request

				// extract values from token
				$url_parts = parse_url( $url );
				$paths     = array_values( explode( '/', $url_parts['path'] ) );
				$token  = end( $paths );
				$values = explode( ':', base64_decode( $token ) );
				if ( count( $values ) !== 6 ) {
					$response['response']['message'] = __( 'Invalid token supplied', 'edd_sl' );
					return $response;
				}
				$expires        = $values[0];
				$license_key    = $values[1];
				$download_id    = (int) $values[2];
				$url            = str_replace( '@', ':', $values[4] );
				$download_beta  = (bool) $values[5];

				// This could also be an actual check_license response with the above vars!
				$message = __( 'License key expired or not activated for URL', 'edd_sl' );

				$response['response']['message'] = $message;
				return $response;

			}
		}
	}
	return $response;
}

Replace $license_server with your own and change the textdomain of the gettext calls to yours too.

@Spreeuw
Copy link

Spreeuw commented Jul 8, 2017

Just adding that if you're not interested in checking the license to show a more detailed message, you can use a shorter version without all the token extraction code:

add_filter( 'http_response', 'http_response_debug', 10, 3 );
function http_response_debug($response, $args, $url) {
	if (isset($response['response']) && is_array($response['response'])) {
		if (isset($response['response']['code']) && $response['response']['code'] == 401 ) {
			// we have a 401 response, check if it's ours
			$license_server = 'https://www.yoursite.com';
			if (strpos($url, $license_server) !== false && strpos($url, 'package_download') !== false) {
				// this is our request
				$response['response']['message'] = __( 'License key expired or not activated for URL', 'edd_sl' );
				return $response;
			}
		}
	}
	return $response;
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants