PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 3.0.1
Defender Security – Malware Scanner, Login Security & Firewall v3.0.1
6.3.0 6.2.3 6.2.4 6.2.0 6.2.1 6.2.2 6.1.0 5.3.1 5.4.0 5.4.1 5.5.0 5.5.1 5.6.0 5.6.1 5.6.2 5.7.0 5.7.1 5.7.2 5.8.0 5.8.1 5.9.0 6.0.0 6.0.1 3.0.1 3.1.0 All 141 releases
defender-security / src / component / security-header.php

security-header.php in Defender Security – Malware Scanner, Login Security & Firewall 3.0.1, at src/component/security-header.php

129 lines 2.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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