public function test_is_debug_log()
 {
     if (!defined('WP_DEBUG_LOG')) {
         define('WP_DEBUG_LOG', true);
     }
     $this->assertEquals(WP_DEBUG_LOG, $this->facade->is_debug_log());
 }
 /**
  * Add links to additional actions to the actions links in the plugins list
  *
  * @param $links
  *
  * @return array
  *
  * @since 1.0.0
  */
 public function add_action_links($links)
 {
     static $template = '<a href="%s">%s</a>';
     $links[] = sprintf($template, $this->get_settings_page(true), $this->wp_facade->__('Setup Wizard', $this->language_domain));
     $links[] = sprintf($template, $this->get_settings_page(), $this->wp_facade->__('Settings', $this->language_domain));
     return $links;
 }
 /**
  * @return string
  */
 private function get_settings_page($wizard = false)
 {
     $page = $this->is_multi_site ? 'network/settings.php' : 'options-general.php';
     $page .= '?page=launchkey-settings';
     $page .= $wizard ? '#wizard-1' : '';
     return $this->wp_facade->admin_url($page);
 }
 /**
  * Is the current session index registered. If so, this is a replay
  * @return bool Registered
  * @throws Exception DB errors throw exceptions
  */
 public function is_session_index_registered()
 {
     $db = $this->facade->get_wpdb();
     $query = $db->prepare("SELECT COUNT(*) FROM {$db->prefix}launchkey_sso_sessions WHERE id = %s", $this->get_session_index());
     $count = $db->get_var($query);
     if ($db->last_error) {
         throw new Exception(sprintf("Database Error: %s", $db->last_error));
     }
     return $count > 0;
 }
 /**
  * @param array $input
  * @param array $errors
  * @param array $options
  */
 private function process_standard_options(&$input, &$errors, &$options)
 {
     if (empty($input[LaunchKey_WP_Options::OPTION_ROCKET_KEY])) {
         $errors[] = $this->wp_facade->__('Rocket Key is a required field', $this->language_domain);
     } else {
         $rocket_key = trim($input[LaunchKey_WP_Options::OPTION_ROCKET_KEY]);
         if (!is_numeric($rocket_key)) {
             $errors[] = $this->wp_facade->__('Rocket Key must be numeric', $this->language_domain);
         } elseif (strlen($rocket_key) !== 10) {
             $errors[] = $this->wp_facade->__('Rocket Key must be 10 digits', $this->language_domain);
         } else {
             $options[LaunchKey_WP_Options::OPTION_ROCKET_KEY] = $rocket_key;
         }
     }
     if (empty($input[LaunchKey_WP_Options::OPTION_SECRET_KEY]) && empty($options[LaunchKey_WP_Options::OPTION_SECRET_KEY])) {
         $errors[] = $this->wp_facade->__('Secret Key is a required field', $this->language_domain);
     } else {
         if (!empty($input[LaunchKey_WP_Options::OPTION_SECRET_KEY])) {
             $secret_key = trim($input[LaunchKey_WP_Options::OPTION_SECRET_KEY]);
             if (!ctype_alnum($secret_key)) {
                 $errors[] = $this->wp_facade->__('Secret Key must be alphanumeric', $this->language_domain);
             } elseif (strlen($secret_key) !== 32) {
                 $errors[] = $this->wp_facade->__('Secret Key must be 32 characters', $this->language_domain);
             } else {
                 $options[LaunchKey_WP_Options::OPTION_SECRET_KEY] = $secret_key;
             }
         }
     }
     $app_display_name = isset($input[LaunchKey_WP_Options::OPTION_APP_DISPLAY_NAME]) ? trim($input[LaunchKey_WP_Options::OPTION_APP_DISPLAY_NAME]) : null;
     if ('LaunchKey' !== $app_display_name && LaunchKey_WP_Implementation_Type::WHITE_LABEL !== $options[LaunchKey_WP_Options::OPTION_IMPLEMENTATION_TYPE]) {
         $errors[] = $this->wp_facade->__('App Display Name can only be modified for White Label implementations', $this->language_domain);
         $options[LaunchKey_WP_Options::OPTION_APP_DISPLAY_NAME] = 'LaunchKey';
     } else {
         $options[LaunchKey_WP_Options::OPTION_APP_DISPLAY_NAME] = $app_display_name ?: null;
     }
     if (empty($_FILES['private_key']['tmp_name']) && empty($options[LaunchKey_WP_Options::OPTION_PRIVATE_KEY]) && isset($options[LaunchKey_WP_Options::OPTION_IMPLEMENTATION_TYPE]) && LaunchKey_WP_Implementation_Type::requires_private_key($options[LaunchKey_WP_Options::OPTION_IMPLEMENTATION_TYPE])) {
         $errors[] = $this->wp_facade->__('Private Key is required', $this->language_domain);
     } else {
         if (!empty($_FILES['private_key']['tmp_name'])) {
             $private_key = @file_get_contents($_FILES['private_key']['tmp_name']);
             $rsa = new Crypt_RSA();
             if (@$rsa->loadKey($private_key)) {
                 if ($rsa->getPrivateKey($rsa->privateKeyFormat)) {
                     $options[LaunchKey_WP_Options::OPTION_PRIVATE_KEY] = $private_key;
                 } else {
                     $errors[] = $this->wp_facade->__('The Key file provided was a valid RSA key file but did not contain a private key.  Did you mistakenly supply the public key file?', $this->language_domain);
                 }
             } else {
                 $errors[] = $this->wp_facade->__('The Private Key provided was invalid', $this->language_domain);
             }
         }
     }
 }
 /**
  * Method to handle redirects for logout of the LaunchKey SSO service
  *
  * '@since 1.1.0
  */
 public function logout()
 {
     if ($user = $this->wp_facade->wp_get_current_user()) {
         // And that user has logged in with LaunchKey SSO
         if (!empty($user->launchkey_sso_session)) {
             // Reset the SSO session
             $this->wp_facade->update_user_meta($user->ID, 'launchkey_sso_session', null);
             // Redirect to SSO logout
             $this->wp_facade->wp_redirect($this->logout_url);
             exit;
         }
     }
 }
 /**
  * @param $launchkey_user_hash
  */
 private function prepare_for_launchkey_pair($launchkey_user_hash)
 {
     // Set the pair cookie with the LaunchKey user hash
     $this->wp_facade->setcookie('launchkey_user', $launchkey_user_hash, $this->wp_facade->current_time('timestamp', true) + 300, COOKIEPATH, COOKIE_DOMAIN);
     // Redirect to finish pairing
     if (!$this->wp_facade->current_user_can('manage_options')) {
         //not previously logged in
         $this->wp_facade->wp_redirect($this->wp_facade->wp_login_url() . "?launchkey_pair=1");
     } else {
         //previously authenticated
         $this->wp_facade->wp_redirect($this->wp_facade->admin_url("profile.php?launchkey_admin_pair=1&updated=1"));
     }
 }
 /**
  * @param $user_id
  *
  * @return boolean
  */
 private function get_user_authorized($user_id)
 {
     $db = $this->wp_facade->get_wpdb();
     $value = $db->get_var($db->prepare("SELECT meta_value FROM {$db->usermeta} WHERE user_id = %s AND meta_key = 'launchkey_authorized' LIMIT 1", $user_id));
     if ('true' === $value) {
         $authorized = true;
     } elseif ('false' === $value) {
         $authorized = false;
     } else {
         $authorized = null;
     }
     return $authorized;
 }
 public function wizard_submit_ajax()
 {
     if (isset($_POST['nonce'])) {
         if ($this->wp_facade->wp_verify_nonce($_POST['nonce'], static::WIZARD_NONCE_KEY)) {
             list($options, $errors) = $this->admin->check_option($_POST);
             if ($errors) {
                 $response["errors"] = $errors;
             } else {
                 $this->wp_facade->update_option(LaunchKey_WP_Admin::OPTION_KEY, $options);
             }
             $response['nonce'] = $this->wp_facade->wp_create_nonce(static::WIZARD_NONCE_KEY);
         } else {
             $response["errors"] = $this->wp_facade->__("An error occurred submitting the page.  Please refresh the page and submit again.");
         }
         $this->wp_facade->wp_send_json($response);
     }
 }
 private function debug_log($level, $message, array $context = array())
 {
     if ($this->wp_facade->is_debug_log()) {
         $this->log($level, $message, $context);
     }
 }
 /**
  * @return mixed
  */
 private function get_option()
 {
     return $this->is_multi_site ? $this->wp_facade->get_site_option(LaunchKey_WP_Admin::OPTION_KEY) : $this->wp_facade->get_option(LaunchKey_WP_Admin::OPTION_KEY);
 }
 private function launchkey_is_activated()
 {
     return $this->facade->is_plugin_active($this->plugin_file) || $this->launchkey_is_mu_plugin();
 }
 public function wizard_easy_setup_callback()
 {
     $headers = array();
     array_walk($_SERVER, function ($value, $key) use(&$headers) {
         if (preg_match('/^HTTP\\_(.+)$/', $key, $matches)) {
             $headers[str_replace('_', '-', $matches[1])] = $value;
         }
     });
     preg_match('/^[^\\/]+\\/(.*)$/', $_SERVER['SERVER_PROTOCOL'], $matches);
     $protocol_version = $matches ? $matches[1] : null;
     $request = new Request($_SERVER['REQUEST_METHOD'], $_SERVER['REQUEST_URI'], $headers, $this->wp_facade->fopen('php://input', 'rb'), $protocol_version);
     $http_response = new Response();
     if ($request->hasHeader('signature')) {
         try {
             // Have the SDK client handle the callback
             $response = $this->launchkey_client->serverSentEvent()->handleEvent($request, $http_response);
             if ($response instanceof \LaunchKey\SDK\Domain\RocketCreated) {
                 $config = $this->get_option(LaunchKey_WP_Configuration_Wizard::EASY_SETUP_OPTION);
                 if (empty($config['nonce']) || !$config['nonce'] instanceof \LaunchKey\SDK\Domain\NonceResponse) {
                     throw new \LaunchKey\SDK\Service\Exception\InvalidRequestError(sprintf('Easy config request with no valid "nonce" in option "%s"', LaunchKey_WP_Configuration_Wizard::EASY_SETUP_OPTION));
                 }
                 // Delete the option, valid or not.
                 $this->wp_facade->delete_option(LaunchKey_WP_Configuration_Wizard::EASY_SETUP_OPTION);
                 // Check for expiration of the nonce
                 $expires = $config['nonce']->getExpiration();
                 if ($expires <= new DateTime("now", new DateTimeZone("UTC"))) {
                     throw new \LaunchKey\SDK\Service\Exception\InvalidRequestError('Easy config "nonce" has expired');
                 }
                 $rocketConfig = $response->getRocketConfig($this->crypt_service, $config['nonce']->getNonce());
                 $expected_callback_url = $this->wp_facade->admin_url('admin-ajax.php?action=' . LaunchKey_WP_Native_Client::CALLBACK_AJAX_ACTION);
                 // Verify the callback URL before attempting to decrypt the data
                 $actual_callback_url = $rocketConfig->getCallbackURL();
                 if ($actual_callback_url !== $expected_callback_url) {
                     throw new \LaunchKey\SDK\Service\Exception\InvalidRequestError(sprintf('Easy config is not for this site based on callback. Expected: %s, Actual: %s.', $expected_callback_url, $actual_callback_url));
                 }
                 $options = $this->get_option(LaunchKey_WP_Admin::OPTION_KEY);
                 $rocket_type = $rocketConfig->isWhiteLabel() ? LaunchKey_WP_Implementation_Type::WHITE_LABEL : LaunchKey_WP_Implementation_Type::NATIVE;
                 // Update options from server sent event service response
                 $options[LaunchKey_WP_Options::OPTION_IMPLEMENTATION_TYPE] = $rocket_type;
                 $options[LaunchKey_WP_Options::OPTION_ROCKET_KEY] = $rocketConfig->getKey();
                 $options[LaunchKey_WP_Options::OPTION_SECRET_KEY] = $rocketConfig->getSecret();
                 $options[LaunchKey_WP_Options::OPTION_PRIVATE_KEY] = $rocketConfig->getPrivateKey();
                 $this->update_option(LaunchKey_WP_Admin::OPTION_KEY, $options);
                 $response_string = "";
                 $body = $http_response->getBody();
                 $body->rewind();
                 while ($segment = $body->read(256)) {
                     $response_string .= $segment;
                 }
                 $this->wp_facade->header("Content-Type: text/plain", true, $http_response->getStatusCode());
                 $this->wp_facade->wp_die($response_string);
             }
         } catch (\Exception $e) {
             if ($this->wp_facade->is_debug_log()) {
                 $this->wp_facade->error_log('Callback Exception: ' . $e->getMessage());
             }
             if ($e instanceof \LaunchKey\SDK\Service\Exception\InvalidRequestError) {
                 $this->wp_facade->http_response_code(400);
                 $this->wp_facade->wp_die('Invalid Request');
             } else {
                 $this->wp_facade->http_response_code(500);
                 $this->wp_facade->wp_die('Server Error');
             }
         }
     }
 }
 /**
  * @return mixed
  */
 private function update_option($key)
 {
     return $this->is_multi_site ? $this->wp_facade->update_site_option($key) : $this->wp_facade->update_option($key);
 }
 /**
  * @param $user_id
  */
 private function reset_auth($user_id)
 {
     $this->wp_facade->update_user_meta($user_id, 'launchkey_sso_session', null);
     $this->wp_facade->update_user_meta($user_id, 'launchkey_authorized', null);
 }
 /**
  * @return string
  */
 private function get_login_post_url()
 {
     return $this->wp_facade->site_url('wp-login.php', 'login_post');
 }