<?php

	/**
	 * The page Settings.
	 *
	 * @since 1.0.0
	 */

	// Exit if accessed directly
	if( !defined('ABSPATH') ) {
		exit;
	}

	class WHLP_HideLoginPage extends Wbcr_FactoryClearfy206_PageBase {

		/**
		 * The id of the page in the admin menu.
		 *
		 * Mainly used to navigate between pages.
		 * @see FactoryPages410_AdminPage
		 *
		 * @since 1.0.0
		 * @var string
		 */
		public $id = "hlp_hide_login";

		/**
		 * @var string
		 */
		public $page_menu_dashicon = 'dashicons-testimonial';

		/**
		 * @var bool
		 */
		public $available_for_multisite = true;

		/**
		 * @var bool
		 */
		public $clearfy_collaboration = false;

		/**
		 * @var bool
		 */
		public $show_right_sidebar_in_options = false;

		/**
		 * @param Wbcr_Factory409_Plugin $plugin
		 */
		public function __construct(Wbcr_Factory409_Plugin $plugin)
		{
			$this->menu_title = __('Hide login page', 'hide-login-page');

			if( !defined('LOADING_HIDE_LOGIN_PAGE_AS_ADDON') ) {
				$this->internal = false;
				$this->menu_target = 'options-general.php';
				$this->add_link_to_plugin_actions = true;
			}

			if( is_multisite() && defined('WBCR_CLEARFY_PLUGIN_ACTIVE') ) {
				$clearfy_is_active_for_network = is_plugin_active_for_network(Wbcr_FactoryClearfy_Compatibility::getClearfyBasePath());

				if( WHLP_Plugin::app()->isNetworkActive() && $clearfy_is_active_for_network ) {
					$this->clearfy_collaboration = true;
				}
			} else if( defined('WBCR_CLEARFY_PLUGIN_ACTIVE') ) {
				$this->clearfy_collaboration = true;
			}

			parent::__construct($plugin);

			$this->plugin = $plugin;
		}

		/**
		 * @return string|void         *
		 */
		public function getPageTitle()
		{
			return $this->clearfy_collaboration ? __('"Hide login page" settings', 'hide-login-page') : __('General', 'hide-login-page');
		}

		/**
		 * Метод позволяет менять заголовок меню, в зависимости от сборки плагина.
		 * @return string|void
		 */
		public function getMenuTitle()
		{
			return $this->clearfy_collaboration ? __('General', 'hide-login-page') : __('Hide login page', 'hide-login-page');
		}

		/**
		 * Подменяем простраинство имен для меню плагина, если активирован плагин Clearfy
		 * Меню текущего плагина будет добавлено в общее меню Clearfy
		 * @return string
		 */
		public function getMenuScope()
		{
			if( $this->clearfy_collaboration ) {
				$this->internal = true;
				$this->page_parent_page = 'defence';

				return 'wbcr_clearfy';
			}

			return $this->plugin->getPluginName();
		}

		/**
		 * Requests assets (js and css) for the page.
		 *
		 * @see Wbcr_FactoryPages410_AdminPage
		 *
		 * @since 1.0.0
		 * @return void
		 */
		public function assets($scripts, $styles)
		{
			parent::assets($scripts, $styles);

			// Add Clearfy styles for HMWP pages
			if( defined('WBCR_CLEARFY_PLUGIN_ACTIVE') ) {
				$this->styles->add(WCL_PLUGIN_URL . '/admin/assets/css/general.css');
			}

			add_filter('wbcr/factory/pages/impressive/actions_notice', array($this, 'actionNotices'));
		}


		/**
		 * We register notifications for some actions
		 *
		 * @see libs\factory\pages\themplates\FactoryPages410_ImpressiveThemplate
		 * @param $notices
		 * @param Wbcr_Factory409_Plugin $plugin
		 * @return array
		 */
		public function actionNotices($notices)
		{

			$notices[] = array(
				'conditions' => array(
					'wbcr_hlp_login_path_incorrect' => 1,
				),
				'type' => 'danger',
				'message' => __('You entered an incorrect part of the path to your login page. The path to the login page can not consist only of digits, at least 3 characters, you must use only the characters [0-9A-z_-]!', 'hide-login-page')
			);
			$notices[] = array(
				'conditions' => array(
					'wbcr_hlp_login_path_exists' => 1,
				),
				'type' => 'danger',
				'message' => __('The entered login page name is already used for one of your pages. Try to choose a different login page name!', 'hide-login-page')
			);

			return $notices;
		}

		/**
		 * Permalinks options.
		 *
		 * @since 1.0.0
		 * @return mixed[]
		 */
		public function getPageOptions()
		{
			$options = array();

			$options[] = array(
				'type' => 'html',
				'html' => '<div class="wbcr-factory-page-group-header">' . __('<strong>Protect your admin login</strong>.', 'hide-login-page') . '<p>' . __('Dozens of bots attack your login page at /wp-login.php and /wp-admin/daily. Bruteforce and want to access your admin panel. Even if you\'re sure that you have created a complex and reliable password, this does not guarantee security and does not relieve your login page load. The easiest way is to protect the login page by simply changing its address to your own and unique.', 'hide-login-page') . '</p></div>'
			);

			$eventsOff = array();

			if( !WHLP_Plugin::app()->getPopulateOption('hide_wp_admin') && !WHLP_Plugin::app()->getPopulateOption('hide_login_path') ) {
				$eventsOff = array(
					'hide' => '.factory-control-wpadmin_and_login_access_error',
				);
			}

			$options[] = array(
				'type' => 'checkbox',
				'way' => 'buttons',
				'name' => 'hide_wp_admin',
				'title' => __('Hide wp-admin', 'hide-login-page'),
				'layout' => array('hint-type' => 'icon', 'hint-icon-color' => 'grey'),
				'hint' => __("Hides the /wp-admin directory for unauthorized users. If the option is disabled, when you request the page /wp-admin you will be redirected to the login page, even if you changed its address. Therefore, for protection purposes enable this option.", 'hide-login-page'),
				'eventsOn' => array(
					'show' => '.factory-control-wpadmin_and_login_access_error',
				),
				'eventsOff' => $eventsOff
			);

			$options[] = array(
				'type' => 'checkbox',
				'way' => 'buttons',
				'name' => 'hide_login_path',
				'title' => __('Hide Login Page', 'hide-login-page'),
				'layout' => array('hint-type' => 'icon', 'hint-icon-color' => 'red'),
				'hint' => __("Hides the wp-login.php and wp-signup.php pages.", 'hide-login-page') . '<br>--<br><span class="wbcr-factory-light-orange-color">' . __("Use this option carefully! If you forget the new login page address, you can not get into the admin panel.", 'hide-login-page') . '</span>',
				'eventsOn' => array(
					'show' => '.factory-control-wpadmin_and_login_access_error',
				),
				'eventsOff' => $eventsOff
			);

			$options[] = array(
				'type' => 'dropdown',
				'way' => 'buttons',
				'name' => 'wpadmin_and_login_access_error',
				'title' => __('Access error type', 'hide-login-page'),
				'data' => array(
					array('404', __('Page 404', 'hide-login-page')),
					array('redirect_to', __('Redirect to', 'hide-login-page')),
					array('forbidden', __('Forbidden 403', 'hide-login-page'))
				),
				'layout' => array('hint-type' => 'icon', 'hint-icon-color' => 'grey'),
				'hint' => __("Some Wordpress themes do not have page templates 404, if you see a php error (blank screen, error 500) when accessing the wp-admin page, wp-login.php, try set forbidden 403 error or set  a redirect to your custom page 404.", 'hide-login-page'),
				'default' => '404',
				'events' => array(
					'404' => array(
						'hide' => '.factory-control-wpadmin_and_login_access_redirect'
					),
					'redirect_to' => array(
						'show' => '.factory-control-wpadmin_and_login_access_redirect'
					),
					'forbidden' => array(
						'hide' => '.factory-control-wpadmin_and_login_access_redirect'
					)
				)
			);

			$options[] = array(
				'type' => 'textbox',
				'name' => 'wpadmin_and_login_access_redirect',
				'title' => __('Set custom page 404 url', 'hide-login-page'),
				'hint' => __('When you try to get to the hidden page wp-admin or wp-login.php, you will be redirected by the link you have set. Redirect will work only if you have chosen the required option above!', 'hide-login-page'),
				'layout' => array('hint-type' => 'icon', 'hint-icon-color' => 'grey'),
				'default' => site_url() . '/404'
			);

			$recovery_url = $this->getRecoveryUrl();
			$recovery_url = !empty($recovery_url) ? '<br><br>' . sprintf(__("If unable to access the login/admin section anymore, use the Recovery Link which reset links to default: \n%s", 'hide-login-page'), $recovery_url) : '';
			$new_login_url = $this->getNewLoginUrl();

			$options[] = array(
				'type' => 'textbox',
				'name' => 'login_path',
				'placeholder' => 'secure/auth.php',
				'title' => __('New login page', 'hide-login-page'),
				'hint' => __('Set a new login page name without slash. Example: mysecretlogin', 'hide-login-page') . '<br><span style="color:red">' . __("IMPORTANT! Be sure that you wrote down the new login page address", 'hide-login-page') . '</span>: <b><a href="' . $new_login_url . '" target="_blank">' . $new_login_url . '</a></b>' . $recovery_url,
				//'units' => '<i class="fa fa-unlock" title="' . __('This option will protect your blog against unauthorized access.', 'hide-login-page') . '"></i>',
				//'layout' => array('hint-type' => 'icon', 'hint-icon-color' => 'red')
			);

			$formOptions = array();

			$formOptions[] = array(
				'type' => 'form-group',
				'items' => $options,
				//'cssClass' => 'postbox'
			);

			return apply_filters('wbcr_hlp_general_form_options', $formOptions, $this);
		}

		/**
		 * Вызывается после сохранением опций формы, когда выполнен сброс кеша и совершен редирект
		 *
		 * @since 4.0.0
		 * @return void
		 */
		protected function afterFormSave()
		{
			$pages = array("hlp_hide_login", "defence");

			if( $this->plugin->getPluginName() == WHLP_Plugin::app()->getPluginName() && in_array($this->id, $pages) ) {

				$login_path = WHLP_Plugin::app()->getPopulateOption('login_path');
				$valid_path = !is_numeric($login_path) && preg_match('/^[0-9A-z_-]{3,}$/', $login_path);

				if( !empty($login_path) ) {
					if( !$valid_path ) {
						WHLP_Plugin::app()->deletePopulateOption('login_path');
						WHLP_Plugin::app()->deletePopulateOption('hide_login_path');

						$this->redirectToAction('index', array('wbcr_hlp_login_path_incorrect' => 1));
					}

					$login_path_exists = false;

					if( WHLP_Plugin::app()->isNetworkActive() ) {
						foreach(WHLP_Plugin::app()->getActiveSites() as $site) {

							switch_to_blog($site->blog_id);

							if( $this->loginPathExists($login_path) ) {
								$login_path_exists = true;
							}

							restore_current_blog();
						}
					} else {
						$login_path_exists = $this->loginPathExists($login_path);
					}

					if( !empty($login_path_exists) ) {
						WHLP_Plugin::app()->deletePopulateOption('login_path');
						WHLP_Plugin::app()->deletePopulateOption('hide_login_path');

						$this->redirectToAction('index', array('wbcr_hlp_login_path_exists' => 1));
					}

					$old_login_path = WHLP_Plugin::app()->getPopulateOption('old_login_path');

					if( !$old_login_path || $login_path != $old_login_path ) {

						$recovery_code = md5(rand(1, 9999) . microtime());

						$body = __("Hi,\nThis is %s plugin. Here is your new WordPress login address:\nURL: %s", 'hide-login-page') . PHP_EOL . PHP_EOL;
						$body .= __("IMPORTANT! Be sure that you wrote down the new login page address", 'hide-login-page') . '!' . PHP_EOL . PHP_EOL;
						$body .= __("If unable to access the login/admin section anymore, use the Recovery Link which reset links to default: \n%s", 'hide-login-page') . PHP_EOL . PHP_EOL;
						$body .= __("Best Regards,\n%s", 'hide-login-page') . PHP_EOL;

						$new_url = site_url('wp-login.php');

						$body = sprintf($body, WHLP_Plugin::app()->getPluginTitle(), $new_url, $this->getRecoveryUrl($recovery_code), WHLP_Plugin::app()->getPluginTitle()) . PHP_EOL;

						$subject = sprintf(__('[%s] Your New WP Login!', 'hide-login-page'), WHLP_Plugin::app()->getPluginTitle());

						$admin_email = WHLP_Plugin::app()->isNetworkActive() ? get_site_option('admin_email') : get_option('admin_email');

						wp_mail($admin_email, $subject, $body);

						WHLP_Plugin::app()->updatePopulateOption('old_login_path', $login_path);
						WHLP_Plugin::app()->updatePopulateOption('login_recovery_code', $recovery_code);
					}
				} else {

					// if new login path is empty
					WHLP_Plugin::app()->deletePopulateOption('old_login_path');
					WHLP_Plugin::app()->deletePopulateOption('login_recovery_code');
				}
			}
		}

		protected function loginPathExists($login_path)
		{
			$args = array(
				'name' => $login_path,
				'post_type' => array('page', 'post'),
				'numberposts' => 1
			);

			$posts = get_posts($args);

			if( !empty($posts) ) {
				return true;
			}

			return false;
		}

		/**
		 * Get the new address of the login page
		 *
		 * @return string
		 */

		protected function getNewLoginUrl()
		{
			$login_path = WHLP_Plugin::app()->getPopulateOption('login_path');

			if( empty($login_path) ) {
				return site_url('wp-login.php');
			}

			if( WbcrFactoryClearfy206_Helpers::isPermalink() ) {
				return WbcrFactoryClearfy206_Helpers::userTrailingslashit(home_url('/') . $login_path);
			} else {
				return add_query_arg($login_path, null, site_url());
			}
		}

		/**
		 * Allows you to get a link to reset settings
		 *
		 * @param string|null $recovery_code
		 * @return string
		 */
		protected function getRecoveryUrl($recovery_code = null)
		{
			$recovery_code = empty($recovery_code) ? WHLP_Plugin::app()->getPopulateOption('login_recovery_code') : $recovery_code;

			if( empty($recovery_code) ) {
				return '';
			}

			return add_query_arg('wbcr_hlp_login_recovery', $recovery_code, site_url());
		}
	}