/**
  * Init an object.
  *
  * @since 1.0
  *
  * @param \stdClass $data
  */
 protected function init(\stdClass $data)
 {
     $this->ID = $data->ID;
     $this->activation = itelic_get_activation($data->activation);
     $this->release = itelic_get_release($data->release_id);
     $this->update_date = make_date_time($data->update_date);
     $this->previous_version = $data->previous_version;
 }
 public function test_activation_release_updates_when_update_record_created()
 {
     $product = $this->product_factory->create_and_get();
     $file = $this->factory->attachment->create_object('file.zip', $product->ID, array('post_mime_type' => 'application/zip'));
     /** @var Release $r1 */
     $r1 = $this->release_factory->create_and_get(array('product' => $product->ID, 'file' => $file, 'version' => '1.0', 'type' => Release::TYPE_MAJOR));
     /** @var Release $r2 */
     $r2 = $this->release_factory->create_and_get(array('product' => $product->ID, 'file' => $file, 'version' => '1.1', 'type' => Release::TYPE_MAJOR));
     $key = $this->key_factory->create_and_get(array('product' => $product->ID, 'customer' => 1));
     $activation = $this->activation_factory->create(array('key' => $key, 'release' => $r1->get_pk()));
     $this->update_factory->create_and_get(array('release' => $r2, 'activation' => $activation));
     $this->assertEquals($r2->get_pk(), itelic_get_activation($activation)->get_release()->get_pk());
 }
 /**
  * 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['id'])) {
         throw new Exception(__("Activation ID is required.", Plugin::SLUG), self::CODE_NO_LOCATION_ID);
     }
     /**
      * Fires when the deactivate 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_deactivate_request', $get, $post);
     $activation_id = absint($post['id']);
     $activation = itelic_get_activation($activation_id);
     if ($activation) {
         if ($activation->get_key()->get_key() !== $this->key->get_key()) {
             throw new Exception(__("Activation record ID does not match license key.", Plugin::SLUG), self::CODE_INVALID_LOCATION);
         } else {
             $activation->deactivate();
         }
     } else {
         throw new Exception(__("Activation record could not be found.", Plugin::SLUG), self::CODE_INVALID_LOCATION);
     }
     /**
      * Fires when an activation is deactivated via the HTTP API.
      *
      * @since 1.0
      *
      * @param Activation   $activation
      * @param \ArrayAccess $get
      * @param \ArrayAccess $post
      */
     do_action('itelic_api_deactivate_key', $activation, $get, $post);
     return new Response(array('success' => true, 'body' => $activation));
 }
/**
 * Create an update record.
 *
 * @api
 *
 * @since 1.0
 *
 * @param array $args
 *
 * @return \ITELIC\Update|WP_Error
 */
function itelic_create_update($args)
{
    $defaults = array('activation' => '', 'release' => '', 'update_date' => '', 'previous_version' => '');
    $args = ITUtility::merge_defaults($args, $defaults);
    $activation = is_numeric($args['activation']) ? itelic_get_activation($args['activation']) : $args['activation'];
    if (!$activation) {
        return new WP_Error('invalid_activation', __("Invalid activation record.", \ITELIC\Plugin::SLUG));
    }
    $release = is_numeric($args['release']) ? itelic_get_release($args['release']) : $args['release'];
    if (!$release) {
        return new WP_Error('invalid_release', __("Invalid release object.", \ITELIC\Plugin::SLUG));
    }
    if (!empty($args['update_date'])) {
        $update_date = is_string($args['update_date']) ? \ITELIC\make_date_time($args['update_date']) : $args['update_date'];
        if (!$update_date instanceof DateTime) {
            return new WP_Error("invalid_update_date", __("Invalid update date.", \ITELIC\Plugin::SLUG));
        }
    } else {
        $update_date = null;
    }
    return \ITELIC\Update::create($activation, $release, $update_date, $args['previous_version']);
}
 function get_object_by_id($object_id)
 {
     return itelic_get_activation($object_id);
 }
 /**
  * Handle deactivating a location installation.
  *
  * @since 1.0
  */
 public function handle_ajax_delete()
 {
     if (!isset($_POST['id']) || !isset($_POST['nonce'])) {
         wp_send_json_error(array('message' => __("Invalid request format.", Plugin::SLUG)));
     }
     $id = abs($_POST['id']);
     $nonce = $_POST['nonce'];
     try {
         $this->do_delete(itelic_get_activation($id), $nonce);
     } catch (\Exception $e) {
         wp_send_json_error(array('message' => $e->getMessage()));
     }
     wp_send_json_success();
 }
 /**
  * Serve the request to this endpoint.
  *
  * @param \ArrayAccess $get
  * @param \ArrayAccess $post
  *
  * @return Response
  */
 public function serve(\ArrayAccess $get, \ArrayAccess $post)
 {
     /**
      * Fires before the download query args are validated.
      *
      * If add-ons are completely overriding how product updates
      * are delivered. They should use this action.
      *
      * @since 1.0
      *
      * @param \ArrayAccess $get
      * @param \ArrayAccess $post
      */
     do_action('itelic_pre_validate_download', $get, $post);
     if (!\ITELIC\validate_query_args($get)) {
         $this->logger->notice('Invalid download link used.', array('get' => $get, 'post' => $post));
         status_header(403);
         _e("This download link is invalid or has expired.", Plugin::SLUG);
         if (!defined('DOING_TESTS') || !DOING_TESTS) {
             die;
         } else {
             return;
         }
     }
     $activation = itelic_get_activation($get['activation']);
     $key = $get['key'];
     if (!$activation || $activation->get_key()->get_key() != $key) {
         $key_object = itelic_get_key($key);
         if ($key_object && $key_object->get_customer()) {
             $user = $key_object->get_customer()->id;
         } elseif ($activation->get_key()->get_customer()) {
             $user = $activation->get_key()->get_customer()->id;
         } else {
             $user = 0;
         }
         $this->logger->notice('Invalid download link used. Key activation mismatch.', array('get' => $get, 'post' => $post, '_user' => $user));
         status_header(403);
         _e("This download link is invalid or has expired.", Plugin::SLUG);
         if (!defined('DOING_TESTS') || !DOING_TESTS) {
             die;
         } else {
             return;
         }
     }
     $release = $activation->get_key()->get_product()->get_latest_release_for_activation($activation);
     $file = $release->get_download();
     itelic_create_update(array('activation' => $activation, 'release' => $release));
     $this->logger->info('Download for {product} delivered.', array('product' => $activation->get_key()->get_product()->post_title, '_user' => $activation->get_key()->get_customer()->id, 'get' => $get, 'post' => $post));
     /**
      * Fires before a download is served.
      *
      * Download links are only generated from the Product endpoint
      * if both the license key and activation records are valid.
      *
      * If you are generating download links differently, you should
      * probably validate the activation status and key status again.
      *
      * @since 1.0
      *
      * @param \WP_Post   $file       WordPress attachment object for the software update.
      * @param Key        $key        License key used for validation.
      * @param Activation $activation Activation the download is being delivered to.
      */
     do_action('itelic_pre_serve_download', $file, $key, $activation);
     \ITELIC\serve_download(wp_get_attachment_url($file->ID));
 }
/**
 * AJAX handler for deactivating a location.
 *
 * @internal
 *
 * @since 1.0
 */
function account_licenses_deactivate_location()
{
    if (!isset($_POST['id']) || !isset($_POST['nonce'])) {
        wp_send_json_error(array('message' => __("Invalid request format.", Plugin::SLUG)));
    }
    $id = absint($_POST['id']);
    $nonce = $_POST['nonce'];
    if (!wp_verify_nonce($nonce, "itelic-deactivate-{$id}")) {
        wp_send_json_error(array('message' => __("Request expired. Please refresh and try again.", Plugin::SLUG)));
    }
    try {
        $record = itelic_get_activation($id);
    } catch (\Exception $e) {
        wp_send_json_error(array('message' => __("Invalid install location.", Plugin::SLUG)));
        die;
    }
    if (!$record) {
        wp_send_json_error(array('message' => __("Invalid install location.", Plugin::SLUG)));
    }
    if (!current_user_can('edit_user', $record->get_key()->get_customer()->wp_user->ID)) {
        wp_send_json_error(array('message' => __("You don't have permission to do this.", Plugin::SLUG)));
    }
    $record->deactivate();
    wp_send_json_success();
}
 /**
  * Check authentication, keeping the mode in mind.
  *
  * @since 1.0
  *
  * @param Authenticatable $endpoint
  *
  * @return bool
  */
 protected function handle_auth(Authenticatable $endpoint)
 {
     if (!isset($_SERVER['PHP_AUTH_USER']) || trim($_SERVER['PHP_AUTH_USER']) == '') {
         return false;
     }
     $license_key = $_SERVER['PHP_AUTH_USER'];
     try {
         $key = itelic_get_key($license_key);
     } catch (\Exception $e) {
         return false;
     }
     if (!$key) {
         return false;
     }
     $this->current_user = $key->get_customer() ? $key->get_customer()->wp_user : null;
     if (!empty($_SERVER['PHP_AUTH_PW'])) {
         $activation = itelic_get_activation($_SERVER['PHP_AUTH_PW']);
     } else {
         $activation = null;
     }
     $endpoint->set_auth_license_key($key);
     $endpoint->set_auth_activation($activation);
     switch ($endpoint->get_auth_mode()) {
         case Authenticatable::MODE_ACTIVE:
             return $key->get_status() == Key::ACTIVE;
         case Authenticatable::MODE_EXISTS:
             return true;
         case Authenticatable::MODE_VALID_ACTIVATION:
             if (!$activation) {
                 return false;
             }
             if ($key->get_status() != Key::ACTIVE) {
                 return false;
             }
             if ($activation->get_status() != Activation::ACTIVE) {
                 return false;
             }
             if ($activation->get_key()->get_key() != $key->get_key()) {
                 return false;
             }
             return true;
         default:
             return false;
     }
 }