/** * Serve the request to this endpoint. * * @param \ArrayAccess $get * @param \ArrayAccess $post * * @return Response * * @throws Exception|\Exception */ public function serve(\ArrayAccess $get, \ArrayAccess $post) { if (!isset($post['location'])) { throw new Exception(__("Activation location is required.", Plugin::SLUG), self::CODE_NO_LOCATION); } /** * Fires when the activate API endpoint is being validated. * * This occurs after authentication has taken place. You can return an error response by throwing an * \ITELIC\API\Exception in your callback. * * @since 1.0 * * @param \ArrayAccess $get * @param \ArrayAccess $post */ do_action('itelic_api_validate_activate_request', $get, $post); $location = sanitize_text_field($post['location']); $version = isset($post['version']) ? $post['version'] : ''; if (isset($post['track']) && in_array($post['track'], array('stable', 'pre-release'))) { $track = $post['track']; } else { $track = 'stable'; } if ($version) { $release = itelic_get_release_by_version($this->key->get_product()->ID, $version); } else { $release = null; } $activation = itelic_get_activation_by_location($location, $this->key); try { if ($activation) { if ($activation->get_status() == Activation::DEACTIVATED) { $activation->reactivate(); } $activation->update_meta('track', $track); if ($release) { $activation->set_release($release); } } else { $activation = itelic_activate_license_key($this->key, $location, null, $release, $track); } } catch (\LengthException $e) { throw new Exception($e->getMessage(), Endpoint::CODE_INVALID_LOCATION, $e); } catch (\OverflowException $e) { throw new Exception($e->getMessage(), Endpoint::CODE_MAX_ACTIVATIONS, $e); } /** * Fires when an activation is activated via the HTTP API. * * @since 1.0 * * @param Activation $activation * @param \ArrayAccess $get * @param \ArrayAccess $post */ do_action('itelic_api_activate_key', $activation, $get, $post); return new Response(array('success' => true, 'body' => $activation)); }
/** * Perform the activation. * * @since 1.0 * * @param Key $key * @param string $location * @param string $nonce * * @return Activation * * @throws \InvalidArgumentException|\UnexpectedValueException on error. */ public function do_activation(Key $key, $location, $nonce) { if (!wp_verify_nonce($nonce, "itelic-remote-activate-key-{$key->get_key()}")) { throw new \InvalidArgumentException(__("Sorry, this page has expired. Please refresh and try again.", Plugin::SLUG)); } if (!current_user_can('manage_options')) { throw new \InvalidArgumentException(__("Sorry, you don't have permission to do this.", Plugin::SLUG)); } $record = itelic_activate_license_key($key, $location); if (!$record instanceof Activation) { throw new \UnexpectedValueException(__("Something went wrong. Please refresh and try again.", Plugin::SLUG)); } return $record; }
/** * AJAX handler for remote activating a location. * * @internal * * @since 1.0 */ function account_licenses_activate() { if (!isset($_POST['location']) || !isset($_POST['nonce']) || !isset($_POST['key'])) { wp_send_json_error(array('message' => __("Invalid request format.", Plugin::SLUG))); } $location = sanitize_text_field($_POST['location']); $key = $_POST['key']; $nonce = $_POST['nonce']; if (!wp_verify_nonce($nonce, "itelic-remote-activate-{$key}")) { wp_send_json_error(array('message' => __("Request expired. Please refresh and try again.", Plugin::SLUG))); } try { $key = itelic_get_key($key); } catch (\Exception $e) { wp_send_json_error(array('message' => __("Invalid license key.", Plugin::SLUG))); die; } if (!$key) { wp_send_json_error(array('message' => __("Invalid license key.", Plugin::SLUG))); } if (!current_user_can('edit_user', $key->get_customer()->wp_user->ID)) { wp_send_json_error(array('message' => __("You don't have permission to do this.", Plugin::SLUG))); } try { itelic_activate_license_key($key, $location); } catch (\Exception $e) { wp_send_json_error(array('message' => $e->getMessage())); } wp_send_json_success(); }
/** * Activate a release. * * ## OPTIONS * * <location> * : Where the software is being activated. Typically a website. * * <key> * : The key being activated. * * [--when=<when>] * : Wen the activation occurred. Accepts strtotime compatible * value. GMT. * * [--version=<version>] * : The version of the software installed. Default: latest. * * [--track=<track>] * : Accepted values: stable, pre-release. Default: stable * * [--porcelain] * : Output just the new activation ID. * * @param $args * @param $assoc_args */ public function activate($args, $assoc_args) { list($location, $key) = $args; $key = itelic_get_key($key); if (!$key) { WP_CLI::error("Invalid key."); } if ($key->get_status() != ITELIC\Key::ACTIVE) { WP_CLI::error(sprintf("Key has a status of '%s' not 'active'.", $key->get_status())); } if (isset($assoc_args['when'])) { $when = \ITELIC\make_date_time($assoc_args['when']); } else { $when = null; } if (isset($assoc_args['version'])) { $release = itelic_get_release_by_version($key->get_product()->ID, $assoc_args['version']); if (!$release) { WP_CLI::error(sprintf("Invalid release ID %d.", $assoc_args['release'])); } } else { $release = null; } if (isset($assoc_args['track'])) { if (in_array($assoc_args['track'], array('stable', 'pre-release'))) { $track = $assoc_args['track']; } else { WP_CLI::error("Invalid value '%s' for track."); } } else { $track = 'stable'; } parent::_create($args, $assoc_args, function () use($location, $key, $when, $release, $track) { $a = itelic_activate_license_key($key, $location, $when, $release, $track); if ($a) { return $a->get_pk(); } return new WP_Error(); }); }