| 1 |
<?php |
| 2 |
|
| 3 |
namespace WP_Defender\Model\Setting; |
| 4 |
|
| 5 |
use Calotes\Model\Setting; |
| 6 |
|
| 7 |
class Mask_Login extends Setting { |
| 8 |
/** |
| 9 |
* Option name. |
| 10 |
* @var string |
| 11 |
*/ |
| 12 |
public $table = 'wd_masking_login_settings'; |
| 13 |
/** |
| 14 |
* The URL we use to replace tradition wp-admin. |
| 15 |
* |
| 16 |
* @var string |
| 17 |
* @defender_property |
| 18 |
* @sanitize_mask_url |
| 19 |
*/ |
| 20 |
public $mask_url = ''; |
| 21 |
|
| 22 |
/** |
| 23 |
* Enable to redirect if user visit to tradition wp-admin or wp-login.php. |
| 24 |
* |
| 25 |
* @var string |
| 26 |
* @defender_property |
| 27 |
*/ |
| 28 |
public $redirect_traffic = 'off'; |
| 29 |
|
| 30 |
/** |
| 31 |
* The URL to redirect. |
| 32 |
* |
| 33 |
* @var string |
| 34 |
* @defender_property |
| 35 |
* @sanitize_text_field |
| 36 |
*/ |
| 37 |
public $redirect_traffic_url = ''; |
| 38 |
|
| 39 |
/** |
| 40 |
* The page/post id to redirect. |
| 41 |
* |
| 42 |
* @var int |
| 43 |
* @defender_property |
| 44 |
*/ |
| 45 |
public $redirect_traffic_page_id; |
| 46 |
|
| 47 |
/** |
| 48 |
* Main switch of this function. |
| 49 |
* |
| 50 |
* @var bool |
| 51 |
* @defender_property |
| 52 |
*/ |
| 53 |
public $enabled = false; |
| 54 |
|
| 55 |
/** |
| 56 |
* The ticket so anyone with this can bypass the mask login, we'll need this in certain cases. |
| 57 |
* Todo: need if express_tickets are not saved? |
| 58 |
* |
| 59 |
* @var array |
| 60 |
*/ |
| 61 |
public $express_tickets = []; |
| 62 |
|
| 63 |
/** |
| 64 |
* Backward compatibility with older version. |
| 65 |
* |
| 66 |
* @var array |
| 67 |
*/ |
| 68 |
protected $mapping = [ |
| 69 |
'express_tickets' => 'otps', |
| 70 |
]; |
| 71 |
|
| 72 |
/** |
| 73 |
* Define labels for settings key. |
| 74 |
* |
| 75 |
* @param string|null $key |
| 76 |
* |
| 77 |
* @return string|array|null |
| 78 |
*/ |
| 79 |
public function labels( $key = null ) { |
| 80 |
$labels = [ |
| 81 |
'enabled' => __( 'Mask Login Area', 'wpdef' ), |
| 82 |
'mask_url' => __( 'Masking URL', 'wpdef' ), |
| 83 |
'redirect_traffic' => __( 'Redirect traffic', 'wpdef' ), |
| 84 |
'redirect_traffic_url' => __( 'Redirection URL', 'wpdef' ), |
| 85 |
'redirect_traffic_page_id' => __( 'Select the page or post your default login URLs will redirect to', 'wpdef' ), |
| 86 |
]; |
| 87 |
|
| 88 |
if ( ! is_null( $key ) ) { |
| 89 |
return $labels[ $key ] ?? null; |
| 90 |
} |
| 91 |
|
| 92 |
return $labels; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* @return bool |
| 97 |
*/ |
| 98 |
public function is_active() { |
| 99 |
return apply_filters( 'wd_mask_login_enable', $this->enabled && '' !== $this->mask_url ); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* @param string|null $domain |
| 104 |
* |
| 105 |
* @return string |
| 106 |
*/ |
| 107 |
public function get_new_login_url( $domain = null ) { |
| 108 |
if ( empty( $this->mask_url ) ) { |
| 109 |
return ''; |
| 110 |
} |
| 111 |
|
| 112 |
if ( null === $domain ) { |
| 113 |
$domain = site_url(); |
| 114 |
} |
| 115 |
|
| 116 |
return $domain . '/' . ltrim( $this->mask_url, '/' ); |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* @return string |
| 121 |
*/ |
| 122 |
public function get_redirect_url() { |
| 123 |
if ( empty( $this->redirect_traffic_url ) ) { |
| 124 |
return ''; |
| 125 |
} |
| 126 |
|
| 127 |
if ( 'url' === $this->is_url_or_slug() ) { |
| 128 |
return $this->redirect_traffic_url; |
| 129 |
} else { |
| 130 |
return untrailingslashit( get_home_url( get_current_blog_id() ) ) . '/' . ltrim( $this->redirect_traffic_url, |
| 131 |
'/' ); |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Check the entered string is URL or slug of the internal link. |
| 137 |
* |
| 138 |
* @return string |
| 139 |
*/ |
| 140 |
public function is_url_or_slug() { |
| 141 |
$regex = '/^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[\d]{1,5})?(\/.*)?$/i'; |
| 142 |
|
| 143 |
if ( preg_match( $regex, $this->redirect_traffic_url ) ) { |
| 144 |
return 'url'; |
| 145 |
} else { |
| 146 |
return 'slug'; |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Custom validate: |
| 152 |
* 1. No forbidden URL |
| 153 |
* 2. No conflict with current post/page slug |
| 154 |
* 3. Mask URl should not same with redirect URL |
| 155 |
* |
| 156 |
* @return bool|void |
| 157 |
*/ |
| 158 |
public function after_validate() { |
| 159 |
if ( empty( $this->mask_url ) ) { |
| 160 |
return; |
| 161 |
} |
| 162 |
$forbidden = [ |
| 163 |
'login', |
| 164 |
'wp-admin', |
| 165 |
'admin', |
| 166 |
'dashboard', |
| 167 |
'wp-login', |
| 168 |
'wp-login.php', |
| 169 |
// For change '.' to '-'. |
| 170 |
'wp-login-php', |
| 171 |
]; |
| 172 |
// Remove the double slash. |
| 173 |
$this->mask_url = ltrim( $this->mask_url, '/\\' ); |
| 174 |
|
| 175 |
if ( in_array( $this->mask_url, $forbidden, true ) ) { |
| 176 |
$this->errors[] = __( 'The slug you have provided cannot be used for masking your login area. Please try a new one.', 'wpdef' ); |
| 177 |
|
| 178 |
return false; |
| 179 |
} |
| 180 |
$exits = get_page_by_path( $this->mask_url, OBJECT, [ 'post', 'page' ] ); |
| 181 |
if ( is_object( $exits ) ) { |
| 182 |
$this->errors[] = __( 'A page already exists at this URL, please pick a unique page for your new login area.', 'wpdef' ); |
| 183 |
|
| 184 |
return false; |
| 185 |
} |
| 186 |
|
| 187 |
if ( $this->mask_url === $this->redirect_traffic_url ) { |
| 188 |
$this->errors[] = __( 'Redirect URL must different from Login URL', 'wpdef' ); |
| 189 |
|
| 190 |
return false; |
| 191 |
} |
| 192 |
} |
| 193 |
} |
| 194 |
|