| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package RSFirewall! |
| 4 |
* @copyright (c) 2018 RSJoomla! |
| 5 |
* @link https://www.rsjoomla.com |
| 6 |
* @license GNU General Public License http://www.gnu.org/licenses/gpl-3.0.en.html |
| 7 |
*/ |
| 8 |
|
| 9 |
if ( ! defined( 'WPINC' ) ) { |
| 10 |
die; |
| 11 |
} |
| 12 |
|
| 13 |
/** |
| 14 |
* Class RSFirewall_Helper_Fields |
| 15 |
*/ |
| 16 |
abstract class RSFirewall_Helper_Fields |
| 17 |
{ |
| 18 |
/** |
| 19 |
* Checkbox list. |
| 20 |
* |
| 21 |
* @param $args array containing: |
| 22 |
* 'section' => section name, |
| 23 |
* 'field' => JSimpleXMLElement, |
| 24 |
* 'value' => value set in the database or null |
| 25 |
*/ |
| 26 |
public static function checkboxes( $args ) |
| 27 |
{ |
| 28 |
$field = $args['field']; |
| 29 |
$section = $args['section']; |
| 30 |
$values = (array) $args['value']; |
| 31 |
$html = '<fieldset>'; |
| 32 |
$i = 0; |
| 33 |
|
| 34 |
|
| 35 |
foreach ($field->option as $option) |
| 36 |
{ |
| 37 |
$id = (string) $field->attributes()->name; |
| 38 |
$label = (string) $option; |
| 39 |
|
| 40 |
$attributes = array( |
| 41 |
'id' => $id.$i, |
| 42 |
'name' => sprintf('%s[%s][]', $section, $id), |
| 43 |
'value' => (string) $option->attributes()->value, |
| 44 |
); |
| 45 |
|
| 46 |
|
| 47 |
if (!strlen($attributes['value'])) |
| 48 |
{ |
| 49 |
$attributes['value'] = $label; |
| 50 |
} |
| 51 |
|
| 52 |
// handle the checked attribute |
| 53 |
if (in_array($attributes['value'], $values) || (!$values && $option->attributes()->checked)) { |
| 54 |
$attributes['checked'] = 'checked'; |
| 55 |
} |
| 56 |
|
| 57 |
// Handle the rest of the attributes |
| 58 |
foreach ($field->attributes() as $attr => $value) { |
| 59 |
// ignore the type |
| 60 |
if ($attr == 'type') continue; |
| 61 |
|
| 62 |
$value = (string) $value; |
| 63 |
if (!isset($attributes[$attr]) && !empty($value)) { |
| 64 |
$attributes[$attr] = $value; |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
$output_attrs = array(); |
| 69 |
foreach ($attributes as $attr => $value) { |
| 70 |
$output_attrs[] = esc_attr($attr).'="'.esc_attr($value).'"'; |
| 71 |
} |
| 72 |
|
| 73 |
$html .= ' |
| 74 |
<label for="' . esc_attr($id . $i) . '"> |
| 75 |
<input type="checkbox" '.implode(' ', $output_attrs).' autocomplete="off"/> |
| 76 |
' . esc_html__($label, 'rsfirewall') . ' |
| 77 |
</label> |
| 78 |
<br />'; |
| 79 |
|
| 80 |
$i++; |
| 81 |
} |
| 82 |
|
| 83 |
$html .= '</fieldset>'; |
| 84 |
|
| 85 |
echo $html; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Switchery - simple Yes/No radio. With callbacks if needed |
| 90 |
* |
| 91 |
* @param $args |
| 92 |
*/ |
| 93 |
public static function switchery( $args ) |
| 94 |
{ |
| 95 |
$field = $args['field']; |
| 96 |
$section = $args['section']; |
| 97 |
$value = empty($args['value']) ? '0' : 1; |
| 98 |
|
| 99 |
$id = (string) $field->attributes()->name; |
| 100 |
$name = sprintf('%s[%s]', $section, $id); |
| 101 |
$onchange = (string) $field->attributes()->onchange; |
| 102 |
|
| 103 |
if (isset($field->attributes()->check_before)) { |
| 104 |
$callback_data = explode('|', $field->attributes()->check_before); |
| 105 |
$callback = array_shift($callback_data); |
| 106 |
|
| 107 |
// add the necessary variables |
| 108 |
$additional_args = array($id, $value, $onchange, $name); |
| 109 |
$callback_data = array_merge($callback_data, $additional_args); |
| 110 |
|
| 111 |
if(method_exists('RSFirewall_Helper_Fields', $callback) && $html = call_user_func_array(array('RSFirewall_Helper_Fields', $callback), $callback_data)) { |
| 112 |
echo $html; |
| 113 |
return; |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
$html = ' |
| 119 |
<fieldset> |
| 120 |
<input class="rsfirewall-switch-field" data-id="' . esc_attr($id) . '" type="checkbox" ' . checked($value, 1, false) . ($onchange ? ' onchange="' . esc_attr($onchange) . '"' : '') . '/> |
| 121 |
<input class="rsfirewall-switch-value-holder" type="hidden" id="' . esc_attr($id) . '" name="' . esc_attr($name) . '" value="' . esc_attr($value) . '" /> |
| 122 |
</fieldset> |
| 123 |
'; |
| 124 |
|
| 125 |
|
| 126 |
echo $html; |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* We will not rely on the database to get the value |
| 131 |
* |
| 132 |
* @param $path , the path to the directory which needs to be checked |
| 133 |
* @param $id |
| 134 |
* @param $value |
| 135 |
* @param $onchange |
| 136 |
* @param $name |
| 137 |
* |
| 138 |
* @return string $html |
| 139 |
*/ |
| 140 |
protected static function is_harden($path, $id, $value, $onchange, $name) { |
| 141 |
// check if the path is hardened |
| 142 |
$path = rtrim(ABSPATH, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.$path; |
| 143 |
if (RSFirewall_Helper_Harden::is_hardened($path)) { |
| 144 |
$value = 1; |
| 145 |
} else { |
| 146 |
$value = '0'; |
| 147 |
} |
| 148 |
|
| 149 |
$html = ' |
| 150 |
<fieldset> |
| 151 |
<input class="rsfirewall-switch-field" data-id="' . esc_attr($id) . '" type="checkbox" ' . checked($value, 1, false) . ($onchange ? ' onchange="' . esc_attr($onchange) . '"' : '') . '/> |
| 152 |
<input class="rsfirewall-switch-value-holder" type="hidden" id="' . esc_attr($id) . '" name="' . esc_attr($name) . '" value="' . esc_attr($value) . '" /> |
| 153 |
</fieldset> |
| 154 |
'; |
| 155 |
|
| 156 |
return $html; |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Password field. |
| 161 |
* |
| 162 |
* @param $args |
| 163 |
*/ |
| 164 |
public static function password( $args ) |
| 165 |
{ |
| 166 |
$field = $args['field']; |
| 167 |
$section = $args['section']; |
| 168 |
$class = isset($args['class']) ? (string) $args['class'] : ''; |
| 169 |
|
| 170 |
$id = (string) $field->attributes()->name; |
| 171 |
$name = sprintf('%s[%s]', $section, $id); |
| 172 |
$class = strlen($class) ? ' class="' . esc_attr($class) . '"' : ''; |
| 173 |
|
| 174 |
|
| 175 |
echo '<input type="password"' . $class . ' autocomplete="off" id="' . esc_attr($id) . '" name="' . esc_attr($name) . '" />'; |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* TextBox Field |
| 180 |
* |
| 181 |
* @param $args |
| 182 |
*/ |
| 183 |
public static function textbox( $args ) |
| 184 |
{ |
| 185 |
$field = $args['field']; |
| 186 |
$section = $args['section']; |
| 187 |
$value = $args['value']; |
| 188 |
$class = isset($args['class']) ? (string) $args['class'] : ''; |
| 189 |
|
| 190 |
$id = (string) $field->attributes()->name; |
| 191 |
$name = sprintf('%s[%s]', $section, $id); |
| 192 |
$class = strlen($class) ? ' class="' . esc_attr($class) . '"' : ''; |
| 193 |
|
| 194 |
echo '<input type="text"' . $class . ' id="' . esc_attr($id) . '" name="' . esc_attr($name) . '" value="' . esc_attr($value) . '" />'; |
| 195 |
} |
| 196 |
|
| 197 |
/** |
| 198 |
* Textarea Field |
| 199 |
* |
| 200 |
* @param $args |
| 201 |
*/ |
| 202 |
public static function textarea( $args ) |
| 203 |
{ |
| 204 |
$field = $args['field']; |
| 205 |
$section = $args['section']; |
| 206 |
$value = $args['value']; |
| 207 |
$class = isset($args['class']) ? (string) $args['class'] : ''; |
| 208 |
|
| 209 |
$id = (string) $field->attributes()->name; |
| 210 |
$name = sprintf('%s[%s]', $section, $id); |
| 211 |
$rows = isset($field->attributes()->rows) ? ' rows="'.(string) $field->attributes()->rows.'"' : ''; |
| 212 |
$cols = isset($field->attributes()->cols) ? ' cols="'.(string) $field->attributes()->cols.'"' : ''; |
| 213 |
$class = strlen($class) ? ' class="' . esc_attr($class) . '"' : ''; |
| 214 |
|
| 215 |
echo '<textarea type="text" id="' . esc_attr($id) . '" name="' . esc_attr($name) . '"'.$rows.$cols.$class.'>' . esc_html($value) . '</textarea>'; |
| 216 |
} |
| 217 |
|
| 218 |
/** |
| 219 |
* Separator field. |
| 220 |
* |
| 221 |
* @param $args |
| 222 |
*/ |
| 223 |
public static function separator( $args ) |
| 224 |
{ |
| 225 |
echo '<hr />'; |
| 226 |
} |
| 227 |
|
| 228 |
public static function only_pro() { |
| 229 |
$html = '<div class="alert alert-info">'; |
| 230 |
$html .= ' <h4 style="margin-top:5px; margin-bottom:5px;">' . __('This feature is not available in the free version of RSFirewall!', 'rsfirewall') . '</h4>'; |
| 231 |
$html .= ' <p>' . esc_attr__('If you wish to use this feature please consider purchasing the full version of RSFirewall!', 'rsfirewall') . '</p>'; |
| 232 |
$html .= ' <p><a href="https://www.rsjoomla.com/wordpress-plugins/wordpress-security-plugin.html" class="button-primary">' . __('Purchase the full version of RSFirewall!', 'rsfirewall') . '</a></p>'; |
| 233 |
$html .= '</div>'; |
| 234 |
|
| 235 |
echo $html; |
| 236 |
} |
| 237 |
|
| 238 |
public static function only_pro_field() { |
| 239 |
$html = '<div class="alert alert-info" style="max-width:635px">'; |
| 240 |
$html .= ' <p>' . __('This feature is not available in the free version of RSFirewall!', 'rsfirewall') . ' <a href="https://www.rsjoomla.com/wordpress-plugins/wordpress-security-plugin.html" class="button-primary">' . __('Purchase the full version of RSFirewall!', 'rsfirewall') . '</a></p>'; |
| 241 |
$html .= '</div>'; |
| 242 |
|
| 243 |
echo $html; |
| 244 |
} |
| 245 |
|
| 246 |
/** |
| 247 |
* Selectize field. |
| 248 |
* |
| 249 |
* @param $args |
| 250 |
*/ |
| 251 |
public static function select( $args ) |
| 252 |
{ |
| 253 |
$field = $args['field']; |
| 254 |
$section = $args['section']; |
| 255 |
$values = (array) $args['value']; |
| 256 |
$class = isset($args['class']) ? (string) $args['class'] : ''; |
| 257 |
|
| 258 |
$id = (string) $field->attributes()->name; |
| 259 |
$name = sprintf('%s[%s]', $section, $id); |
| 260 |
$multiple = (string) $field->attributes()->multiple ? 'multiple="multiple"' : ''; |
| 261 |
$class = strlen($class) ? ' class="' . esc_attr($class) . '"' : ''; |
| 262 |
|
| 263 |
if ($multiple) |
| 264 |
{ |
| 265 |
$name .= '[]'; |
| 266 |
} |
| 267 |
|
| 268 |
$html = '<select ' . $multiple . $class . ' id="' . esc_attr($id) . '" name="' . esc_attr($name) . '">'; |
| 269 |
|
| 270 |
$options = array(); |
| 271 |
// Get options from the xml |
| 272 |
if ($field->option) |
| 273 |
{ |
| 274 |
$options_field = RSFirewall_Helper::select_options($field->option); |
| 275 |
$options = array_merge($options, $options_field); |
| 276 |
} |
| 277 |
|
| 278 |
// Get options from the callback |
| 279 |
if ($callback = (string) $field->attributes()->options) |
| 280 |
{ |
| 281 |
list($class, $function) = explode('::', $callback, 2); |
| 282 |
// remove the instance because it is always present and keep the clean function - legacy reasons (in case it is still present the old configuration.xml) |
| 283 |
$function = str_replace(array('get_instance()->', '()', ';'), '', $function); |
| 284 |
|
| 285 |
// remove any unwanted spaces |
| 286 |
$function = trim($function); |
| 287 |
|
| 288 |
$handler = RSFirewall_Helper::call_user_func_pro(array($class, 'get_instance')); |
| 289 |
if (is_callable(array($handler, $function))) { |
| 290 |
$options_callback = call_user_func(array($handler, $function)); |
| 291 |
$options = array_merge($options, $options_callback); |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
if (!empty($options)) { |
| 296 |
foreach ($options as $option) { |
| 297 |
$label = $option->label; |
| 298 |
$value = $option->value; |
| 299 |
|
| 300 |
$checked = in_array($value, $values) ? 'selected="selected"' : ''; |
| 301 |
|
| 302 |
$html .= '<option ' . $checked . ' value="' . esc_attr($value) . '">' . esc_html($label) . '</option>'; |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
$html .= '</select>'; |
| 307 |
|
| 308 |
echo $html; |
| 309 |
} |
| 310 |
|
| 311 |
/** |
| 312 |
* Upload Field |
| 313 |
* |
| 314 |
* @param $args |
| 315 |
*/ |
| 316 |
public static function upload( $args ) |
| 317 |
{ |
| 318 |
$check = array(); |
| 319 |
|
| 320 |
if ( ! empty( $args['action'] ) ) { |
| 321 |
$class_name = $args['action'][0]; |
| 322 |
$function_name = $args['action'][1]; |
| 323 |
if ( method_exists( $class_name, $function_name ) ) { |
| 324 |
$check = $class_name::$function_name(); |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
$html = '<input type="file" id="' . $args['field'] . '" name="' . $args['section'] . '[' . $args['field'] . ']" />'; |
| 329 |
if ( ! empty( $check['message'] ) ) { |
| 330 |
$html .= '<br /><small>' . $check['message'] . '</small>'; |
| 331 |
} |
| 332 |
echo $html; |
| 333 |
} |
| 334 |
|
| 335 |
/** |
| 336 |
* Textarea with filemanager |
| 337 |
* |
| 338 |
* @param $args |
| 339 |
*/ |
| 340 |
|
| 341 |
public static function textarea_filemanager( $args ) { |
| 342 |
$field = $args['field']; |
| 343 |
$section = $args['section']; |
| 344 |
$value = $args['value']; |
| 345 |
|
| 346 |
$id = (string) $field->attributes()->name; |
| 347 |
$name = sprintf('%s[%s]', $section, $id); |
| 348 |
|
| 349 |
$limit_to = is_null($field->attributes()->limit_to) ? '' : (string) $field->attributes()->limit_to; |
| 350 |
|
| 351 |
// the modal button |
| 352 |
echo '<button type="button" class="button-primary" data-filemanager="1"'.(!empty($limit_to) ? ' data-limitto="'.esc_attr($limit_to).'"' : '').' data-selection="#'.esc_attr($id).'" data-toggle="rsmodal" data-target="#rsmodal" data-title="'.__('File Manager', 'rsfirewall').'" data-usefooter="1" data-showclose="1" data-size="large">'.__('Open File Manager', 'rsfirewall').'</button>'; |
| 353 |
// separator |
| 354 |
echo '<br/><br/>'; |
| 355 |
// the actual textarea |
| 356 |
echo '<textarea type="text" id="' . esc_attr($id) . '" name="' . esc_attr($name) . '" style="width:100%; min-height:200px">' . esc_html($value) . '</textarea>'; |
| 357 |
} |
| 358 |
|
| 359 |
/* Handle the labels */ |
| 360 |
/** |
| 361 |
* Function to determine the custom label if exists |
| 362 |
* |
| 363 |
* @param $field |
| 364 |
* |
| 365 |
* @return string $label, the actual label |
| 366 |
* |
| 367 |
*/ |
| 368 |
public static function label_for($field) { |
| 369 |
$label = ''; |
| 370 |
$attr = $field->attributes(); |
| 371 |
$description = (isset($attr->description) && !empty($attr->description)) ? self::add_helptip_markup($attr->description) : ''; |
| 372 |
$required = (isset($attr->required ) && !empty($attr->required )) ? ' *' : ''; |
| 373 |
|
| 374 |
if (isset($attr->label)) { |
| 375 |
if (method_exists('RSFirewall_Helper_Fields', 'label_'.$attr->type)) { |
| 376 |
$label = call_user_func( array( 'RSFirewall_Helper_Fields', 'label_'.$attr->type ), $attr, $description, $required ); |
| 377 |
} |
| 378 |
|
| 379 |
if (empty($label)) { |
| 380 |
$label = $attr->label.$required.$description; |
| 381 |
} |
| 382 |
} else { |
| 383 |
$label = $attr->name.$required.$description; |
| 384 |
} |
| 385 |
|
| 386 |
|
| 387 |
return $label; |
| 388 |
|
| 389 |
} |
| 390 |
|
| 391 |
public static function add_helptip_markup($description = '') { |
| 392 |
if (!empty($description)) { |
| 393 |
$description = ' <span class="rsfirewall-help-tip" data-tip="'.esc_attr__($description, 'rsfirewall').'"></span>'; |
| 394 |
} |
| 395 |
|
| 396 |
return $description; |
| 397 |
} |
| 398 |
|
| 399 |
/** |
| 400 |
* Format the field separator label |
| 401 |
* |
| 402 |
* @param $attr, $description, $required |
| 403 |
* |
| 404 |
* @return string - the field separator label formatted |
| 405 |
*/ |
| 406 |
public static function label_separator($attr, $description = '', $required = '') { |
| 407 |
return '<h3>'.$attr->label.$required.$description.'</h3>'; |
| 408 |
} |
| 409 |
|
| 410 |
/** |
| 411 |
* Get the current admin slug |
| 412 |
* |
| 413 |
* @param $args |
| 414 |
* |
| 415 |
* @return string - the URL for the backend |
| 416 |
*/ |
| 417 |
public static function current_slug($args) { |
| 418 |
$blog_id = get_current_blog_id(); |
| 419 |
$admin_url = get_site_url($blog_id, 'wp-admin'); |
| 420 |
$slug = trim(RSFirewall_Config::get( 'admin_slug_text', '' )); |
| 421 |
|
| 422 |
if ((int) RSFirewall_Config::get( 'enable_admin_slug', 0 ) && strlen($slug) > 0) { |
| 423 |
$admin_url = get_site_url($blog_id) . '/' . htmlentities($slug, ENT_COMPAT, 'utf-8'); |
| 424 |
} |
| 425 |
|
| 426 |
echo $admin_url; |
| 427 |
} |
| 428 |
|
| 429 |
public static function label_only_pro($attr, $description = '', $required = '') { |
| 430 |
return '<h3>'.$attr->label.$required.$description.'</h3>'; |
| 431 |
} |
| 432 |
|
| 433 |
/** |
| 434 |
* Generate a modal button with functionality based on the callback, if any |
| 435 |
* |
| 436 |
* @param $args |
| 437 |
* |
| 438 |
* @return string - the field separator label formatted |
| 439 |
*/ |
| 440 |
public static function modal_custom($args) { |
| 441 |
$field = $args['field']; |
| 442 |
|
| 443 |
if (!isset($field->attributes()->callback)) { |
| 444 |
echo 'No callback defined'; |
| 445 |
} else { |
| 446 |
$callback_data = explode('|', $field->attributes()->callback); |
| 447 |
$callback = array_shift($callback_data); |
| 448 |
|
| 449 |
if(method_exists('RSFirewall_Helper_Fields', $callback) && $html = call_user_func_array(array('RSFirewall_Helper_Fields', $callback), $callback_data)) { |
| 450 |
echo $html; |
| 451 |
return; |
| 452 |
} |
| 453 |
} |
| 454 |
} |
| 455 |
|
| 456 |
// Modal Custom Callbacks |
| 457 |
|
| 458 |
protected static function add_whitelisted_php_files() { |
| 459 |
// check which directory is hardened |
| 460 |
$check_harden_folders = RSFirewall_Helper::check_hardened_directories(); |
| 461 |
|
| 462 |
if (in_array(true, $check_harden_folders)) { |
| 463 |
|
| 464 |
$files_count = RSFirewall_Helper_Harden::whitelisted_php_files(true); |
| 465 |
|
| 466 |
// the modal button |
| 467 |
echo '<button type="button" class="button-primary" data-whitelistfiles="1" data-toggle="rsmodal" data-target="#rsmodal" data-title="'.__('Safelist Blocked PHP Files', 'rsfirewall').'" data-usefooter="1" data-showclose="1" data-size="large">'.sprintf(esc_html__('Safelist Files (%s)', 'rsfirewall'), '<span id="rsf-whitelisted-count">'.$files_count.'</span>').'</button>'; |
| 468 |
} else { |
| 469 |
echo '<div class="alert alert-info">'.esc_html__('There are no folders hardened! You can safelist PHP files only if any of the folders above are hardened.', 'rsfirewall').'</div>'; |
| 470 |
} |
| 471 |
} |
| 472 |
} |