independent-analytics
Last commit date
IAWP
1 year ago
dist
1 year ago
freemius
1 year ago
img
1 year ago
javascript-source
1 year ago
languages
1 year ago
vendor
1 year ago
views
1 year ago
iawp-bootstrap.php
1 year ago
iawp-click-endpoint.php
1 year ago
iawp.php
1 year ago
readme.txt
1 year ago
iawp-click-endpoint.php
185 lines
| 1 | <?php |
| 2 | |
| 3 | // Warning. This file is a standalone PHP file whose sole purpose is to sanitize input and store |
| 4 | // that input in a CSV for later processing. WordPress is not loaded and the classes from the |
| 5 | // plugin are not available. |
| 6 | |
| 7 | class Cache |
| 8 | { |
| 9 | private $attributes; |
| 10 | |
| 11 | public function __construct(array $attributes) |
| 12 | { |
| 13 | $this->attributes = $attributes; |
| 14 | } |
| 15 | |
| 16 | public function is_pro(): bool |
| 17 | { |
| 18 | return $this->attributes['is_pro'] === true; |
| 19 | } |
| 20 | |
| 21 | public function get_upload_directory(): string |
| 22 | { |
| 23 | return trim($this->attributes['upload_directory']); |
| 24 | } |
| 25 | |
| 26 | public function get_custom_ip_header(): ?string |
| 27 | { |
| 28 | if (array_key_exists('custom_ip_header', $this->attributes)) { |
| 29 | return trim($this->attributes['custom_ip_header']); |
| 30 | } |
| 31 | |
| 32 | return null; |
| 33 | } |
| 34 | |
| 35 | public function get_visitor_token_salt(): string |
| 36 | { |
| 37 | return trim($this->attributes['visitor_token_salt']); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | function get_cache(): ?Cache |
| 42 | { |
| 43 | $file = __DIR__ . "/iawp-click-config.php"; |
| 44 | |
| 45 | if (!is_readable($file)) { |
| 46 | return null; |
| 47 | } |
| 48 | |
| 49 | $contents = file_get_contents($file); |
| 50 | |
| 51 | if (false === $contents) { |
| 52 | return null; |
| 53 | } |
| 54 | |
| 55 | $lines = explode("\n", $contents); |
| 56 | |
| 57 | if (count($lines) !== 2) { |
| 58 | return null; |
| 59 | } |
| 60 | |
| 61 | $json = trim($lines[1]); |
| 62 | $data = json_decode($json, true); |
| 63 | |
| 64 | if (is_null($data)) { |
| 65 | return null; |
| 66 | } |
| 67 | |
| 68 | return new Cache($data); |
| 69 | |
| 70 | } |
| 71 | |
| 72 | function get_ip_address(Cache $cache): ?string |
| 73 | { |
| 74 | if (defined('IAWP_TEST_IP')) { |
| 75 | return IAWP_TEST_IP; |
| 76 | } |
| 77 | |
| 78 | $headers = [ |
| 79 | 'HTTP_X_FORWARDED_FOR', |
| 80 | 'HTTP_X_FORWARDED', |
| 81 | 'HTTP_FORWARDED_FOR', |
| 82 | 'HTTP_FORWARDED', |
| 83 | 'REMOTE_ADDR', |
| 84 | 'HTTP_CF_CONNECTING_IP', |
| 85 | 'HTTP_CLIENT_IP', |
| 86 | 'HTTP_INCAP_CLIENT_IP', |
| 87 | 'HTTP_CF_CONNECTING_IP', |
| 88 | ]; |
| 89 | |
| 90 | if (is_string($cache->get_custom_ip_header())) { |
| 91 | array_unshift($headers, $cache->get_custom_ip_header()); |
| 92 | } |
| 93 | |
| 94 | foreach ($headers as $header) { |
| 95 | if (isset($_SERVER[$header])) { |
| 96 | return explode(',', $_SERVER[$header])[0]; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | return null; |
| 101 | } |
| 102 | |
| 103 | function get_visitor_token(Cache $cache): ?string |
| 104 | { |
| 105 | return md5($cache->get_visitor_token_salt() . get_ip_address($cache) . $_SERVER['HTTP_USER_AGENT']); |
| 106 | } |
| 107 | |
| 108 | function trailing_slash(string $path): string |
| 109 | { |
| 110 | $path = rtrim($path, '/\\'); |
| 111 | |
| 112 | return $path . DIRECTORY_SEPARATOR; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Get the path to the click data file and create it if needed. |
| 117 | * |
| 118 | * @return string |
| 119 | */ |
| 120 | function get_click_data_file(): string |
| 121 | { |
| 122 | $text_file = trailing_slash(sys_get_temp_dir()) . "iawp-click-data.txt"; |
| 123 | |
| 124 | if (is_file($text_file)) { |
| 125 | return $text_file; |
| 126 | } |
| 127 | |
| 128 | if (file_put_contents($text_file, "") !== false && file_get_contents($text_file) !== false) { |
| 129 | return $text_file; |
| 130 | } |
| 131 | |
| 132 | $php_file = trailing_slash(__DIR__) . "iawp-click-data.php"; |
| 133 | |
| 134 | if (is_file($php_file)) { |
| 135 | return $php_file; |
| 136 | } |
| 137 | |
| 138 | if (file_put_contents($php_file, "<?php exit; ?>\n") !== false) { |
| 139 | return $php_file; |
| 140 | } |
| 141 | |
| 142 | // Unable to create either data file so there's nowhere to store the click data |
| 143 | exit; |
| 144 | } |
| 145 | |
| 146 | $cache = get_cache(); |
| 147 | |
| 148 | if (is_null($cache) || is_null(get_ip_address($cache))) { |
| 149 | exit; |
| 150 | } |
| 151 | |
| 152 | if (!$cache->is_pro()) { |
| 153 | exit; |
| 154 | } |
| 155 | |
| 156 | $json_body = file_get_contents('php://input'); |
| 157 | $body = json_decode($json_body, true); |
| 158 | |
| 159 | if (is_null($body)) { |
| 160 | exit; |
| 161 | } |
| 162 | |
| 163 | $click_data_file = get_click_data_file(); |
| 164 | $data = [ |
| 165 | 'href' => $body['href'], |
| 166 | 'classes' => $body['classes'], |
| 167 | 'payload' => json_encode($body['payload']), |
| 168 | 'signature' => $body['signature'], |
| 169 | 'visitor_token' => get_visitor_token($cache), |
| 170 | 'created_at' => time(), |
| 171 | ]; |
| 172 | |
| 173 | if(!is_readable($click_data_file) || !is_writable($click_data_file)) { |
| 174 | exit; |
| 175 | } |
| 176 | |
| 177 | $file_resource = fopen($click_data_file, 'a'); |
| 178 | |
| 179 | if (false === $file_resource) { |
| 180 | exit; |
| 181 | } |
| 182 | |
| 183 | fwrite($file_resource, json_encode($data) . "\n"); |
| 184 | fclose($file_resource); |
| 185 |