array('k', 'u', 'r', 'ti', 'tk', 'sw', 'sh', 'tp', 'tz', 'l', 'dw', 'hr', 'c', 'g', 'pl', 'v', 'o'), 'end' => array('tk', 'd', 's', 'cl', 'vt'), 'rp' => array('tk', 'q', 'p0', 'w', 'h', 'dh', 'e'), 'ev' => array('tk', 'n', 'p', 'v'), 'svs' => array('tk', 'id', 'a'), 'sva' => array('tk', 'id', 'r', 'q', 'v', 'x', 'ln'), 'err' => array('tk', 'm', 's', 'l'), 'fm' => array('tk', 'k', 's', 'd', 'lf', 'f'), ); $in = json_decode($raw, false, 8); if (!($in instanceof stdClass) || !isset($in->t) || !is_string($in->t) || !isset($fields[$in->t])) return ''; $type = $in->t; $out = array('t' => $type); foreach ($fields[$type] as $name) { if (!isset($in->$name)) continue; $value = $in->$name; if (is_string($value) && (($type === 'pv' && ($name === 'u' || $name === 'r')) || ($type === 'err' && $name === 's'))) { $out[$name] = esc_url_raw($value, array('http', 'https', 'android-app')); } elseif (is_string($value) && $type === 'sva' && $name === 'x') { $out[$name] = sanitize_textarea_field($value); } else { $out[$name] = userspy_clean_value($value); } } $json = wp_json_encode($out, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); return is_string($json) ? $json : ''; } /** Cleans one value of the message, including nested lists and objects. */ function userspy_clean_value($value) { if (is_int($value) || is_float($value) || is_bool($value) || $value === null) return $value; if (is_string($value)) return sanitize_text_field($value); if (is_array($value)) return array_map('userspy_clean_value', $value); $out = new stdClass(); if (is_object($value)) { foreach (get_object_vars($value) as $name => $item) { $name = sanitize_text_field((string) $name); if ($name !== '') $out->$name = userspy_clean_value($item); } } return $out; } /** POST: relay the visit data to the UserSpy service. The tracker script itself is part of this plugin. */ function userspy_proxy($host, $secret) { $method = isset($_SERVER['REQUEST_METHOD']) ? sanitize_key(wp_unslash($_SERVER['REQUEST_METHOD'])) : 'get'; if ($method === 'post') { // measurement data of the tracker (JSON): cleaned first, then relayed; never stored or printed here $body = userspy_clean_message((string) file_get_contents('php://input', false, null, 0, 65536)); if ($body === '') { wp_send_json(new stdClass(), 400); } $r = wp_remote_post($host . '/c', array( 'timeout' => 4, 'redirection' => 0, 'user-agent' => isset($_SERVER['HTTP_USER_AGENT']) ? substr(sanitize_text_field(wp_unslash($_SERVER['HTTP_USER_AGENT'])), 0, 400) : '', 'headers' => array( 'Content-Type' => 'text/plain', 'Origin' => isset($_SERVER['HTTP_ORIGIN']) ? esc_url_raw(wp_unslash($_SERVER['HTTP_ORIGIN'])) : untrailingslashit(home_url()), 'X-UserSpy-Proxy' => $secret, 'X-UserSpy-IP' => isset($_SERVER['REMOTE_ADDR']) ? (string) filter_var(sanitize_text_field(wp_unslash($_SERVER['REMOTE_ADDR'])), FILTER_VALIDATE_IP) : '', ), 'body' => $body, )); header('Cache-Control: no-store'); if (is_wp_error($r)) { wp_send_json(new stdClass(), 502); } $json = json_decode(wp_remote_retrieve_body($r)); wp_send_json($json === null ? new stdClass() : $json, (int) wp_remote_retrieve_response_code($r)); } status_header(405); exit; } /** True for a page request with a valid, unexpired view code signed by UserSpy (?usp_view=EXPIRY.SIGNATURE). */ function userspy_view_ok($secret) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- authenticated by the HMAC signature below $v = isset($_GET['usp_view']) && is_string($_GET['usp_view']) ? sanitize_text_field(wp_unslash($_GET['usp_view'])) : ''; if ($secret === '' || !preg_match('~^(\d{10})\.([a-f0-9]{20})$~', $v, $m)) return false; if ((int) $m[1] < time() || (int) $m[1] > time() + 7300) return false; return hash_equals(substr(hash_hmac('sha256', 'view|' . $m[1], $secret), 0, 20), $m[2]); } function userspy_embed_url(string $host, string $key, string $secret, string $domain, string $lang): string { $x = time() + 600; $n = bin2hex(random_bytes(8)); $d = strtolower(preg_replace('~^www\.~i', '', $domain)); $s = hash_hmac('sha256', 'embed|' . $key . '|' . $x . '|' . $n . '|' . $d, $secret); return rtrim($host, '/') . '/app/embed.php?' . http_build_query(array('k' => $key, 'x' => $x, 'n' => $n, 'd' => $d, 'l' => (strpos(strtolower($lang), 'en') === 0 ? 'en' : 'de'), 's' => $s)); } /** "key-secret" => array(key, secret) or null */ function userspy_parse_code(string $code) { return preg_match('~^([a-z0-9]{16})-([a-f0-9]{32})$~', trim($code), $m) ? array($m[1], $m[2]) : null; }