/**
   * Checks for, and downloads, any premium features from TCT servers
   *
   * @uses $wpdb
   * @since 3.0
   *
   */
  function check_for_premium_features($return = false) {
    global $wpi_settings;

    $blogname = get_bloginfo('url');
    $blogname = urlencode(str_replace(array('http://', 'https://'), '', $blogname));
    $system = 'wpi';
    $wpi_version = WP_INVOICE_VERSION_NUM;

    $api_key = WPI_Functions::get_api_key(array('force_check' => true, 'return' => true));

    if(empty($api_key) || strlen($api_key) != 40) {
      if($return) {
        if(empty($api_key)) {
          $api_key = __("The API key could not be generated.", WPI);
        }
        return sprintf(__('An error occured during premium feature check: <b>%s</b>.',WPI), $api_key);
      } else {
        return;
      }
    }

    $check_url = "http://updates.usabilitydynamics.com/?system={$system}&site={$blogname}&system_version={$wpi_version}&api_key={$api_key}";

    $response = @wp_remote_get($check_url);

    if (!$response) {
      return;
    }

    // Check for errors
    if (is_object($response) && !empty($response->errors)) {

      foreach ($response->errors as $update_errrors) {
        $error_string .= implode(",", $update_errrors);
        WPI_Functions::log("Feature Update Error: " . $error_string);
      }

      if ($return) {
        return sprintf(__('An error occured during premium feature check: <b> %s </b>.', WPI), $error_string);
      }

      return;
    }

    //** Quit if failure */
    if ($response['response']['code'] != '200') {
      return;
    }

    $response = @json_decode($response['body']);

    if (is_object($response->available_features)) {

      $response->available_features = WPI_Functions::objectToArray($response->available_features);

      //** Update the database */
      $wpi_settings = get_option('wpi_options');

      $wpi_settings['available_features'] = WPI_Functions::objectToArray($response->available_features);
      update_option('wpi_options', $wpi_settings);
    } // available_features


    if ($response->features == 'eligible' && $wpi_settings['disable_automatic_feature_update'] != 'true') {

      // Try to create directory if it doesn't exist
      if (!is_dir(WPI_Premium)) {
        @mkdir(WPI_Premium, 0755);
      }

      // If didn't work, we quit
      if (!is_dir(WPI_Premium)) {
        continue;
      }

      // Save code
      if (is_object($response->code)) {
        foreach ($response->code as $code) {

          $filename = $code->filename;
          $php_code = $code->code;
          $version = $code->version;

          //** Check version */

          $default_headers = array(
              'Name' => __('Feature Name', WPI),
              'Version' => __('Version', WPI),
              'Description' => __('Description', WPI)
          );

          $current_file = @get_file_data(WPI_Premium . "/" . $filename, $default_headers, 'plugin');

          if (@version_compare($current_file[Version], $version) == '-1') {
            $this_file = WPI_Premium . "/" . $filename;
            $fh = @fopen($this_file, 'w');
            if ($fh) {
              fwrite($fh, $php_code);
              fclose($fh);

              if ($current_file[Version]) {
                //UD_F::log(sprintf(__('WP-Invoice Premium Feature: %s updated to version %s from %s.', WPI), $code->name, $version, $current_file[Version]));
              } else {
                //UD_F::log(sprintf(__('WP-Invoice Premium Feature: %s updated to version %s.', WPI), $code->name, $version));
              }

              $updated_features[] = $code->name;
            }
          } else {

          }
        }
      }
    }

    // Update settings
    //WPI_Functions::settings_action(true);

    if ($return && $wpi_settings['disable_automatic_feature_update'] == 'true') {
      return __('Update ran successfully but no features were downloaded because the setting is disabled. Enable in the "Developer" tab.', WPI);
    } elseif ($return) {
      return __('Update ran successfully.', WPI);
    }
  }
Esempio n. 2
0
 /**
  * Checks for, and downloads, any premium features from TCT servers
  *
  * @uses $wpdb
  * @since 3.0
  *
  */
 static function check_for_premium_features($return = false)
 {
     global $wpi_settings;
     $updates = array();
     try {
         $blogname = get_bloginfo('url');
         $blogname = urlencode(str_replace(array('http://', 'https://'), '', $blogname));
         $system = 'wpi';
         $wpi_version = WP_INVOICE_VERSION_NUM;
         $api_key = WPI_Functions::get_api_key(array('force_check' => true, 'return' => true));
         if (empty($api_key)) {
             throw new Exception(__('The API key could not be generated.', WPI));
         }
         if (strlen($api_key) != 40) {
             throw new Exception(sprintf(__('An error occurred during premium feature check. API Key \'<b>%s</b>\' is incorrect.', WPI), $api_key));
         }
         $check_url = "http://updates.usabilitydynamics.com/?system={$system}&site={$blogname}&system_version={$wpi_version}&api_key={$api_key}";
         $response = @wp_remote_get($check_url, array('timeout' => 30));
         if (empty($response)) {
             throw new Exception(__('Could not do remote request.', WPI));
         }
         if (is_wp_error($response)) {
             throw new Exception($response->get_error_message());
         }
         if ($response['response']['code'] != '200') {
             throw new Exception(sprintf(__('Response code from requested server is %s.', WPI), $response['response']['code']));
         }
         $response = @json_decode($response['body']);
         if (empty($response)) {
             throw new Exception(__('Requested server returned empty result or timeout was exceeded. Please, try again later.', WPI));
         }
         if (is_object($response->available_features)) {
             $response->available_features = WPI_Functions::objectToArray($response->available_features);
             //** Update the database */
             $wpi_settings = get_option('wpi_options');
             $wpi_settings['available_features'] = WPI_Functions::objectToArray($response->available_features);
             update_option('wpi_options', $wpi_settings);
         }
         if ($response->features != 'eligible') {
             throw new Exception(__('There are no available premium features.', WPI));
         }
         if (isset($wpi_settings['disable_automatic_feature_update']) && $wpi_settings['disable_automatic_feature_update'] == 'true') {
             throw new Exception(__('No premium features were downloaded because the setting is disabled. Enable in the "Main" tab.', WPI));
         }
         // Try to create directory if it doesn't exist
         if (!is_dir(WPI_Premium)) {
             @mkdir(WPI_Premium, 0755);
         }
         // If didn't work, we quit
         if (!is_dir(WPI_Premium)) {
             throw new Exception(__('Specific directory for uploading premium features can not be created.', WPI));
         }
         // Save code
         if (is_object($response->code)) {
             foreach ($response->code as $code) {
                 $filename = $code->filename;
                 $php_code = $code->code;
                 $version = $code->version;
                 //** Check version */
                 $default_headers = array('Name' => 'Name', 'Version' => 'Version', 'Description' => 'Description');
                 $current_file = @get_file_data(WPI_Premium . "/" . $filename, $default_headers, 'plugin');
                 if (@version_compare($current_file['Version'], $version) == '-1') {
                     $this_file = WPI_Premium . "/" . $filename;
                     $fh = @fopen($this_file, 'w');
                     if ($fh) {
                         fwrite($fh, $php_code);
                         fclose($fh);
                         $res = '';
                         if ($current_file['Version']) {
                             $res = sprintf(__('<b>%s</b> updated from version %s to %s .', WPI), $code->name, $current_file['Version'], $version);
                         } else {
                             $res = sprintf(__('<b>%s</b> %s has been installed.', WPI), $code->name, $version);
                         }
                         if (!empty($res)) {
                             WPI_Functions::log(sprintf(__('WP-Invoice Premium Feature: %s', 'wpp'), $res));
                             $updates[] = $res;
                         }
                     } else {
                         throw new Exception(__('There are no file permissions to upload or update premium features.', WPI));
                     }
                 }
             }
         } else {
             throw new Exception(__('There are no available premium features. Check your licenses for the current domain', WPI));
         }
     } catch (Exception $e) {
         WPI_Functions::log("Feature Update Error: " . $e->getMessage());
         return new WP_Error('error', $e->getMessage());
     }
     $result = __('Update ran successfully.', 'wpp');
     if (!empty($updates)) {
         $result .= '<ul>';
         foreach ($updates as $update) {
             $result .= "<li>{$update}</li>";
         }
         $result .= '</ul>';
     } else {
         $result .= '<br/>' . __('You have the latest premium features versions.', 'wpp');
     }
     $result = apply_filters('wpi::feature_check::result', $result, $updates);
     return $result;
 }