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
137 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_file($file)) { |
| 46 | return null; |
| 47 | } |
| 48 | |
| 49 | $contents = file_get_contents($file); |
| 50 | $lines = explode("\n", $contents); |
| 51 | |
| 52 | if (count($lines) !== 2) { |
| 53 | return null; |
| 54 | } |
| 55 | |
| 56 | $json = trim($lines[1]); |
| 57 | $data = json_decode($json, true); |
| 58 | |
| 59 | if (is_null($data)) { |
| 60 | return null; |
| 61 | } |
| 62 | |
| 63 | return new Cache($data); |
| 64 | |
| 65 | } |
| 66 | |
| 67 | function get_ip_address(Cache $cache): ?string |
| 68 | { |
| 69 | if (defined('IAWP_TEST_IP')) { |
| 70 | return IAWP_TEST_IP; |
| 71 | } |
| 72 | |
| 73 | $headers = [ |
| 74 | 'HTTP_X_FORWARDED_FOR', |
| 75 | 'HTTP_X_FORWARDED', |
| 76 | 'HTTP_FORWARDED_FOR', |
| 77 | 'HTTP_FORWARDED', |
| 78 | 'REMOTE_ADDR', |
| 79 | 'HTTP_CF_CONNECTING_IP', |
| 80 | 'HTTP_CLIENT_IP', |
| 81 | 'HTTP_INCAP_CLIENT_IP', |
| 82 | 'HTTP_CF_CONNECTING_IP', |
| 83 | ]; |
| 84 | |
| 85 | if (is_string($cache->get_custom_ip_header())) { |
| 86 | array_unshift($headers, $cache->get_custom_ip_header()); |
| 87 | } |
| 88 | |
| 89 | foreach ($headers as $header) { |
| 90 | if (isset($_SERVER[$header])) { |
| 91 | return explode(',', $_SERVER[$header])[0]; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | return null; |
| 96 | } |
| 97 | |
| 98 | function get_visitor_token(Cache $cache): ?string |
| 99 | { |
| 100 | return md5($cache->get_visitor_token_salt() . get_ip_address($cache) . $_SERVER['HTTP_USER_AGENT']); |
| 101 | } |
| 102 | |
| 103 | $cache = get_cache(); |
| 104 | |
| 105 | if (is_null($cache) || is_null(get_ip_address($cache))) { |
| 106 | exit; |
| 107 | } |
| 108 | |
| 109 | if (!$cache->is_pro()) { |
| 110 | exit; |
| 111 | } |
| 112 | |
| 113 | $json_body = file_get_contents('php://input'); |
| 114 | $body = json_decode($json_body, true); |
| 115 | |
| 116 | if (is_null($body)) { |
| 117 | exit; |
| 118 | } |
| 119 | |
| 120 | $clicks_file = __DIR__ . "/iawp-click-data.php"; |
| 121 | |
| 122 | if (!is_file($clicks_file)) { |
| 123 | file_put_contents($clicks_file, "<?php exit; ?>\n"); |
| 124 | } |
| 125 | |
| 126 | $data = [ |
| 127 | 'href' => $body['href'], |
| 128 | 'classes' => $body['classes'], |
| 129 | 'payload' => json_encode($body['payload']), |
| 130 | 'signature' => $body['signature'], |
| 131 | 'visitor_token' => get_visitor_token($cache), |
| 132 | 'created_at' => time(), |
| 133 | ]; |
| 134 | |
| 135 | $file_resource = fopen($clicks_file, 'a'); |
| 136 | fwrite($file_resource, json_encode($data) . "\n"); |
| 137 |