function sblamchallange() { $serveruid = _sblamserveruid(); $magic = dechex(mt_rand()) . ';' . dechex(time()) . ';' . $_SERVER['REMOTE_ADDR']; $magic = addslashes(md5($serveruid . $magic) . $magic); if (!headers_sent()) { header("Content-Type: text/javascript;charset=UTF-8"); header("Cache-Control: private,max-age=3600"); setcookie('sblam_', md5($magic . $serveruid), time() + 3600); } $fieldname = 'sc' . abs(crc32($serveruid)); echo <<<JS (function(){ var f = document.getElementsByTagName('form'); f = f && f.length && f[f.length-1] if (!f || f.{$fieldname}) return setTimeout(function(){ var i = document.createElement('input') i.setAttribute('type','hidden') i.setAttribute('name','{$fieldname}') i.setAttribute('value','{$magic};' + (new Date()/1000).toFixed()) f.appendChild(i) /*@cc_on @*/ /*@if (@_jscript_version < 5.9) \ti.name = '{$fieldname}'; \ti.parentNode.removeChild(i); f.innerHTML += (''+i.outerHTML).replace(/>/,' name="{$fieldname}">'); /*@end @*/ var dclick,o = f.onsubmit f.onsubmit = function() { \tif (dclick) return false \tif (this.elements.{$fieldname}) this.elements.{$fieldname}.value += ';' + (new Date()/1000).toFixed() \tif (!o || false !== o()) {dclick=true;setTimeout(function(){dclick=false},4000); return true} \treturn false; } },1000) })() JS; }
/** Wysyła pola z $_POST do sprawdzenia na serwerze Sblam!. * @param fieldnames tablica zawierająca *nazwy* pól w kolejności: treść, autor, e-mail autora, www autora. Może być NULL zamiast nazwy, jeśli nie ma takiego pola w formularzu. * @param apikey klucz API wygenerowany na http://sblam.com/key.html */ function sblamtestpost($fieldnames = NULL, $apikey = NULL) { global $_sblam_last_id, $_sblam_last_error; $_sblam_last_id = $_sblam_last_error = NULL; if (!count($_POST)) { return NULL; } if (NULL === $apikey) { $apikey = "default"; } $in = array('uid' => _sblamserveruid(), 'uri' => empty($_SERVER['REQUEST_URI']) ? $_SERVER['SCRIPT_NAME'] . '?' . $_SERVER['QUERY_STRING'] : $_SERVER['REQUEST_URI'], 'host' => empty($_SERVER['HTTP_HOST']) ? $_SERVER['SERVER_NAME'] : $_SERVER['HTTP_HOST'], 'ip' => $_SERVER['REMOTE_ADDR'], 'time' => time(), 'cookies' => count($_COOKIE) ? 1 : 0, 'session' => isset($_COOKIE[session_name()]) ? 1 : 0, 'sblamcookie' => isset($_COOKIE['sblam_']) ? $_COOKIE['sblam_'] : '', 'salt' => 'x' . mt_rand() . time()); if (is_array($fieldnames)) { foreach ($fieldnames as $key => $val) { $in['field_' . $key] = $val; } } foreach ($_POST as $key => $val) { $in['POST_' . $key] = stripslashes(is_array($val) ? implode("\n", $val) : $val); } if (function_exists("getallheaders")) { foreach (getallheaders() as $header => $val) { $in['HTTP_' . strtr(strtoupper($header), "-", "_")] = $val; } } else { foreach ($_SERVER as $key => $val) { if (substr($key, 0, 5) !== 'HTTP_') { continue; } $in[$key] = stripslashes($val); } } unset($in['HTTP_COOKIE']); unset($in['HTTP_AUTHORIZATION']); $data = ''; foreach ($in as $key => $val) { $data .= strtr($key, "", " ") . "" . strtr($val, "", " ") . ""; } if (strlen($data) > 300000) { return 0; } if ($compress = strlen($data) > 5000 && function_exists('gzcompress')) { $data = gzcompress($data, 1); } if (function_exists('fsockopen')) { $hosts = array('api.sblam.com', 'api2.sblam.com', 'spamapi.geekhood.net'); foreach ($hosts as $host) { $request = "POST / HTTP/1.1\r\n" . "Host:{$host}\r\n" . "Connection:close\r\n" . "Content-Type:application/x-sblam;sig=" . md5("^&\$@\$2\n{$apikey}@@") . md5($apikey . $data) . ($compress ? ";compress=gzip" : '') . "\r\n" . "Content-Length:" . strlen($data) . "\r\n" . "\r\n" . $data; $fs = @fsockopen($host, 80, $errn, $errs, 5); if ($fs !== false && function_exists('stream_set_timeout')) { stream_set_timeout($fs, 15); } if ($fs !== false && fwrite($fs, $request)) { $response = ''; while (!feof($fs)) { $response .= fread($fs, 1024); if (preg_match('!\\r\\n\\r\\n.*\\n!', $response)) { break; } } fclose($fs); if (preg_match('!HTTP/1\\..\\s+(\\d+\\s+[^\\r\\n]+)\\r?\\n((?:[^\\r\\n]+\\r?\\n)+)\\r?\\n(.+)!s', $response, $out)) { if (intval($out[1]) == 200) { if (preg_match('!^(-?\\d+):([a-z0-9-]{0,42}):([a-z0-9]{32})!', $out[3], $res)) { if (md5($apikey . $res[1] . $in['salt']) === $res[3]) { $_sblam_last_id = $res[2]; return $res[1]; } else { trigger_error($_sblam_last_error .= "Sblam: Rezultat od serwera {$host} ma niepoprawny podpis\n"); } } else { trigger_error($_sblam_last_error .= "Sblam: Awaria serwera {$host}. Otrzymany rezultat ma niepoprawny format " . htmlspecialchars($out[3]) . "\n"); } } else { trigger_error($_sblam_last_error .= "Sblam: Komunikat serwera {$host}: " . htmlspecialchars(substr($out[1], 0, 80)) . "\n"); } } else { trigger_error($_sblam_last_error .= "Sblam: Niepoprawny rezultat otrzymany od serwera {$host}\n"); } } else { trigger_error($_sblam_last_error .= "Sblam: Problem komunikacji z serwerem {$host} - {$errn}:{$errs}\n"); } } } else { trigger_error($_sblam_last_error .= "Sblam: Brak wymaganego rozszerzenia sockets (fsockopen)\n"); } return 0; }