| 1 |
<?php |
| 2 |
|
| 3 |
namespace WP_Defender\Component; |
| 4 |
|
| 5 |
use Calotes\Base\Component; |
| 6 |
use WP_Defender\Model\Setting\Security_Headers; |
| 7 |
|
| 8 |
abstract class Security_Header extends Component { |
| 9 |
|
| 10 |
/** |
| 11 |
* @var string |
| 12 |
*/ |
| 13 |
static $rule_slug; |
| 14 |
|
| 15 |
/** |
| 16 |
* Use for cache. |
| 17 |
* |
| 18 |
* @var Security_Headers |
| 19 |
*/ |
| 20 |
public $model; |
| 21 |
|
| 22 |
/** |
| 23 |
* @return array |
| 24 |
*/ |
| 25 |
public function get_misc_data() { |
| 26 |
return array(); |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* @return mixed |
| 31 |
*/ |
| 32 |
abstract function check(); |
| 33 |
|
| 34 |
/** |
| 35 |
* @return string |
| 36 |
*/ |
| 37 |
abstract function get_title(); |
| 38 |
|
| 39 |
/** |
| 40 |
* @return mixed |
| 41 |
*/ |
| 42 |
abstract function add_hooks(); |
| 43 |
|
| 44 |
/** |
| 45 |
* Safe way to get cached model. |
| 46 |
* |
| 47 |
* @return Security_Headers |
| 48 |
*/ |
| 49 |
protected function get_model() { |
| 50 |
if ( is_object( $this->model ) ) { |
| 51 |
return $this->model; |
| 52 |
} |
| 53 |
|
| 54 |
return $this->model = new Security_Headers(); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Check if the header is out or not. |
| 59 |
* |
| 60 |
* @param $header |
| 61 |
* @param $somewhere |
| 62 |
* |
| 63 |
* @return bool |
| 64 |
*/ |
| 65 |
protected function maybe_submit_header( $header, $somewhere ) { |
| 66 |
if ( false === $somewhere ) { |
| 67 |
return true; |
| 68 |
} |
| 69 |
$list = headers_list(); |
| 70 |
$match = false; |
| 71 |
foreach ( $list as $item ) { |
| 72 |
if ( stristr( $item, $header ) ) { |
| 73 |
$match = true; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
return $match; |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
/** |
| 82 |
* @param $url |
| 83 |
* @param $origin |
| 84 |
* @param $ttl |
| 85 |
* |
| 86 |
* @return array|mixed |
| 87 |
*/ |
| 88 |
protected function head_request( $url, $origin, $ttl = null ) { |
| 89 |
$model = $this->get_model(); |
| 90 |
$cached = $model->get_data_values( 'head_requests' ); |
| 91 |
if ( ! is_array( $cached ) ) { |
| 92 |
$cached = array(); |
| 93 |
} |
| 94 |
if ( isset( $cached[ $url ] ) ) { |
| 95 |
$cache = $cached[ $url ]; |
| 96 |
if ( $cache['ttl'] > time() ) { |
| 97 |
|
| 98 |
return $cache['data']; |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
// No cache or cache expired. |
| 103 |
$request = wp_remote_head( |
| 104 |
$url, |
| 105 |
array( |
| 106 |
'user-agent' => 'WP Defender self ping - ' . $origin, |
| 107 |
) |
| 108 |
); |
| 109 |
if ( ! is_wp_error( $request ) ) { |
| 110 |
$headers = wp_remote_retrieve_headers( $request ); |
| 111 |
$headers = $headers->getAll(); |
| 112 |
if ( null === $ttl ) { |
| 113 |
$ttl = strtotime( '+1 day' ); |
| 114 |
} |
| 115 |
$headers['response_code'] = wp_remote_retrieve_response_code( $request ); |
| 116 |
$cached[ $url ] = array( |
| 117 |
'ttl' => apply_filters( 'wd_head_request_ttl', $ttl ), |
| 118 |
'data' => $headers, |
| 119 |
); |
| 120 |
$model->set_data_values( 'head_requests', $cached ); |
| 121 |
$this->log( sprintf( 'Fetched header for %s into cache', $url ) ); |
| 122 |
|
| 123 |
return $headers; |
| 124 |
} |
| 125 |
|
| 126 |
return $request; |
| 127 |
} |
| 128 |
} |
| 129 |
|