| 1 | <?php |
|---|
| 2 | /** |
|---|
| 3 | * The admin-specific functionality of the plugin |
|---|
| 4 | * |
|---|
| 5 | * @link https://neoslab.com |
|---|
| 6 | * @since 1.0.0 |
|---|
| 7 | * @package Simple_HTTPS |
|---|
| 8 | * @subpackage Simple_HTTPS/admin |
|---|
| 9 | */ |
|---|
| 10 | |
|---|
| 11 | /** |
|---|
| 12 | * Class `Simple_HTTPS_Admin` |
|---|
| 13 | * @package Simple_HTTPS |
|---|
| 14 | * @subpackage Simple_HTTPS/admin |
|---|
| 15 | * @author NeosLab <contact@neoslab.com> |
|---|
| 16 | */ |
|---|
| 17 | class Simple_HTTPS_Admin |
|---|
| 18 | { |
|---|
| 19 | /** |
|---|
| 20 | * The ID of this plugin |
|---|
| 21 | * @since 1.0.0 |
|---|
| 22 | * @access private |
|---|
| 23 | * @var string $pluginName the ID of this plugin |
|---|
| 24 | */ |
|---|
| 25 | private $pluginName; |
|---|
| 26 | |
|---|
| 27 | /** |
|---|
| 28 | * The version of this plugin |
|---|
| 29 | * @since 1.0.0 |
|---|
| 30 | * @access private |
|---|
| 31 | * @var string $version the current version of this plugin |
|---|
| 32 | */ |
|---|
| 33 | private $version; |
|---|
| 34 | |
|---|
| 35 | /** |
|---|
| 36 | * Initialize the class and set its properties |
|---|
| 37 | * @since 1.0.0 |
|---|
| 38 | * @param string $pluginName the name of this plugin |
|---|
| 39 | * @param string $version the version of this plugin |
|---|
| 40 | */ |
|---|
| 41 | public function __construct($pluginName, $version) |
|---|
| 42 | { |
|---|
| 43 | $this->pluginName = $pluginName; |
|---|
| 44 | $this->version = $version; |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | /** |
|---|
| 48 | * Register the stylesheets for the admin area |
|---|
| 49 | * @since 1.0.0 |
|---|
| 50 | */ |
|---|
| 51 | public function enqueue_styles() |
|---|
| 52 | { |
|---|
| 53 | wp_register_style($this->pluginName.'-fontawesome', plugin_dir_url(__FILE__).'assets/styles/fontawesome.min.css', array(), $this->version, 'all'); |
|---|
| 54 | wp_register_style($this->pluginName.'-dashboard', plugin_dir_url(__FILE__).'assets/styles/simple-https-admin.min.css', array(), $this->version, 'all'); |
|---|
| 55 | wp_enqueue_style($this->pluginName.'-fontawesome'); |
|---|
| 56 | wp_enqueue_style($this->pluginName.'-dashboard'); |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | /** |
|---|
| 60 | * Register the JavaScript for the admin area |
|---|
| 61 | * @since 1.0.0 |
|---|
| 62 | */ |
|---|
| 63 | public function enqueue_scripts() |
|---|
| 64 | { |
|---|
| 65 | wp_register_script($this->pluginName.'-script', plugin_dir_url(__FILE__).'assets/javascripts/simple-https-admin.min.js', array('jquery'), $this->version, false); |
|---|
| 66 | wp_enqueue_script($this->pluginName.'-script'); |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | /** |
|---|
| 70 | * Return the plugin header |
|---|
| 71 | */ |
|---|
| 72 | public function return_plugin_header() |
|---|
| 73 | { |
|---|
| 74 | $html = '<div class="wpbnd-header-plugin"><span class="header-icon"><i class="fas fa-sliders-h"></i></span> <span class="header-text">'.__('Simple HTTPS', 'simple-https').'</span></div>'; |
|---|
| 75 | return $html; |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | /** |
|---|
| 79 | * Return the tabs menu |
|---|
| 80 | */ |
|---|
| 81 | public function return_tabs_menu($tab) |
|---|
| 82 | { |
|---|
| 83 | $link = admin_url('options-general.php'); |
|---|
| 84 | $list = array |
|---|
| 85 | ( |
|---|
| 86 | array('tab1', 'simple-https-admin', 'fa-cogs', __('Settings', 'simple-https')) |
|---|
| 87 | ); |
|---|
| 88 | |
|---|
| 89 | $menu = null; |
|---|
| 90 | foreach($list as $item => $value) |
|---|
| 91 | { |
|---|
| 92 | $html = array('div' => array('class' => array()), 'a' => array('href' => array()), 'i' => array('class' => array()), 'p' => array(), 'span' => array()); |
|---|
| 93 | $menu ='<div class="tab-label '.$value[0].' '.(($tab === $value[0]) ? 'active' : '').'"><a href="'.$link.'?page='.$value[1].'"><p><i class="fas '.$value[2].'"></i><span>'.$value[3].'</span></p></a></div>'; |
|---|
| 94 | echo wp_kses($menu, $html); |
|---|
| 95 | } |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | /** |
|---|
| 99 | * Return .htaccess string |
|---|
| 100 | */ |
|---|
| 101 | private function return_htaccess_string() |
|---|
| 102 | { |
|---|
| 103 | $string = 'RewriteEngine On'.PHP_EOL; |
|---|
| 104 | $string.= 'RewriteCond %{HTTPS} !=on'.PHP_EOL; |
|---|
| 105 | $string.= 'RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]'.PHP_EOL; |
|---|
| 106 | $string.= 'Header always set Content-Security-Policy "upgrade-insecure-requests;"'; |
|---|
| 107 | return $string; |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | /** |
|---|
| 111 | * Return header strict transport security |
|---|
| 112 | */ |
|---|
| 113 | public function return_header_sts() |
|---|
| 114 | { |
|---|
| 115 | $simple_https = get_option('_simple_https'); |
|---|
| 116 | if((isset($simple_https['sts'])) && ($simple_https['sts'] === 'on')) |
|---|
| 117 | { |
|---|
| 118 | header("strict-transport-security: max-age=600"); |
|---|
| 119 | } |
|---|
| 120 | elseif((isset($simple_https['sts'])) && ($simple_https['sts'] === 'off')) |
|---|
| 121 | { |
|---|
| 122 | header("strict-transport-security: max-age=0"); |
|---|
| 123 | } |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | /** |
|---|
| 127 | * Update `Options` on form submit |
|---|
| 128 | */ |
|---|
| 129 | public function return_update_options() |
|---|
| 130 | { |
|---|
| 131 | if((isset($_POST['shs-update-option'])) && ($_POST['shs-update-option'] === 'true') |
|---|
| 132 | && check_admin_referer('shs-referer-form', 'shs-referer-option')) |
|---|
| 133 | { |
|---|
| 134 | $opts = array('ssl' => 'off', 'sts' => 'off'); |
|---|
| 135 | $filepath = ABSPATH."/.htaccess"; |
|---|
| 136 | if(isset($_POST['_simple_https']['ssl'])) |
|---|
| 137 | { |
|---|
| 138 | $opts['ssl'] = 'on'; |
|---|
| 139 | if(file_exists($filepath)) |
|---|
| 140 | { |
|---|
| 141 | $filetext = file_get_contents($filepath); |
|---|
| 142 | $htaccess = str_replace('RewriteEngine On', $this->return_htaccess_string(), $filetext); |
|---|
| 143 | $writefile = fopen($filepath, "w") or die("Unable to open file!"); |
|---|
| 144 | fwrite($writefile, $htaccess); |
|---|
| 145 | fclose($writefile); |
|---|
| 146 | } |
|---|
| 147 | } |
|---|
| 148 | else |
|---|
| 149 | { |
|---|
| 150 | if(file_exists($filepath)) |
|---|
| 151 | { |
|---|
| 152 | $filetext = file_get_contents($filepath); |
|---|
| 153 | $findtext = $this->return_htaccess_string(); |
|---|
| 154 | $htaccess = str_replace($findtext, 'RewriteEngine On', $filetext); |
|---|
| 155 | $writefile = fopen($filepath, "w") or die("Unable to open file!"); |
|---|
| 156 | fwrite($writefile, $htaccess); |
|---|
| 157 | fclose($writefile); |
|---|
| 158 | } |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | if(isset($_POST['_simple_https']['sts'])) |
|---|
| 162 | { |
|---|
| 163 | $opts['sts'] = 'on'; |
|---|
| 164 | } |
|---|
| 165 | |
|---|
| 166 | $data = update_option('_simple_https', $opts); |
|---|
| 167 | header('location:'.admin_url('options-general.php?page=simple-https-admin').'&output=updated'); |
|---|
| 168 | die(); |
|---|
| 169 | } |
|---|
| 170 | } |
|---|
| 171 | |
|---|
| 172 | /** |
|---|
| 173 | * Return the `Options` page |
|---|
| 174 | */ |
|---|
| 175 | public function return_options_page() |
|---|
| 176 | { |
|---|
| 177 | $opts = get_option('_simple_https'); |
|---|
| 178 | require_once plugin_dir_path(__FILE__).'partials/simple-https-admin-options.php'; |
|---|
| 179 | } |
|---|
| 180 | |
|---|
| 181 | /** |
|---|
| 182 | * Return Backend Menu |
|---|
| 183 | */ |
|---|
| 184 | public function return_admin_menu() |
|---|
| 185 | { |
|---|
| 186 | add_options_page('Simple HTTPS', 'Simple HTTPS', 'manage_options', 'simple-https-admin', array($this, 'return_options_page')); |
|---|
| 187 | remove_submenu_page('options-general.php', 'simple-https-about'); |
|---|
| 188 | } |
|---|
| 189 | } |
|---|
| 190 | |
|---|
| 191 | ?> |
|---|