Module: Wpxf::WordPress::Login

Included in:
Module
Defined in:
lib/wpxf/wordpress/login.rb

Overview

Provides functionality required to login to WordPress.

Instance Method Summary collapse

Instance Method Details

#valid_wordpress_cookie?(cookies) ⇒ Boolean

Returns true if a valid WordPress cookie was found.

Parameters:

  • cookies (String)

    the session cookies to validate in string form.

Returns:

  • (Boolean)

    true if a valid WordPress cookie was found.



23
24
25
26
27
28
29
30
31
# File 'lib/wpxf/wordpress/login.rb', line 23

def valid_wordpress_cookie?(cookies)
  !(
    cookies =~ /wordpress(?:_sec)?_logged_in_[^=]+=[^;]+;/i ||
    # WordPress 2.0
    cookies =~ /wordpress(?:user|pass)_[^=]+=[^;]+;/i ||
    # WordPress 2.5
    cookies =~ /wordpress_[a-z0-9]+=[^;]+;/i
  ).nil?
end

#wordpress_login(user, pass) ⇒ Array?

Log in to WordPress and return the session cookies.

Parameters:

  • user (String)

    the username.

  • pass (String)

    the password.

Returns:

  • (Array, nil)

    an array of cookies if the login is successful, otherwise, returns nil.



38
39
40
41
42
43
44
45
46
# File 'lib/wpxf/wordpress/login.rb', line 38

def (user, pass)
  res = (user, pass)

  if res&.cookies
    return res.cookies if valid_wordpress_cookie?(res.cookies.to_s)
  end

  nil
end

#wordpress_login_post_body(user, pass) ⇒ String

Build a request body string for a WordPress login request.

Parameters:

  • user (String)

    the username.

  • pass (String)

    the password.

Returns:

  • (String)

    the request body.



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/wpxf/wordpress/login.rb', line 9

def (user, pass)
  redirect = Wpxf::Utility::Text.rand_alpha(10)
  builder = Wpxf::Utility::BodyBuilder.new
  builder.add_field('log', user)
  builder.add_field('pwd', pass)
  builder.add_field('redirect_to', normalize_uri(target_uri, redirect))
  builder.add_field('wp-submit', 'Login')
  builder.create do |body|
    return body
  end
end