function ask_to_ufdb($datatosend, $PARAMS, $ToUfdbKey) { if (!isset($GLOBALS["UFDB_SOCKET_ERROR"])) { $GLOBALS["UFDB_SOCKET_ERROR"] = 0; } if (!isset($GLOBALS["UfdbgclientMaxSockTimeOut"])) { $GLOBALS["UfdbgclientMaxSockTimeOut"] = 0; } if ($GLOBALS["UfdbgclientMaxSockTimeOut"] == 0) { $GLOBALS["UfdbgclientMaxSockTimeOut"] = 6; } if (intval($GLOBALS["UFDB_SOCKET_ERROR"]) > intval($GLOBALS["UfdbgclientMaxSockTimeOut"])) { if (!isset($GLOBALS["UFDB_SOCKET_ERROR_TIME"])) { $GLOBALS["UFDB_SOCKET_ERROR_TIME"] = time(); return false; } if ($GLOBALS["UFDB_SOCKET_ERROR_TIME"] == 0) { $GLOBALS["UFDB_SOCKET_ERROR_TIME"] = time(); return false; } $ask_to_ufdb_time_sec = ask_to_ufdb_time_sec($GLOBALS["UFDB_SOCKET_ERROR_TIME"]); if ($ask_to_ufdb_time_sec == 10) { ufdbg_admin_mysql(1, "Web filtering Current 10 seconds to wait retry working with webfiltering in 80 seconds...", null, __FILE__, __LINE__); } if ($ask_to_ufdb_time_sec == 30) { ufdbg_admin_mysql(1, "Web filtering Current 30 seconds to wait retry working with webfiltering in 60 seconds...", null, __FILE__, __LINE__); } if ($ask_to_ufdb_time_sec == 60) { ufdbg_admin_mysql(1, "Web filtering Current 60 seconds to wait retry working with webfiltering in 30 seconds...", null, __FILE__, __LINE__); } if ($ask_to_ufdb_time_sec == 80) { ufdbg_admin_mysql(1, "Web filtering Current 80 seconds to wait retry working with webfiltering in 10 seconds...", null, __FILE__, __LINE__); } if ($ask_to_ufdb_time_sec < 89) { return false; } ufdbg_admin_mysql(1, "Web filtering retry working with webfiltering", null, __FILE__, __LINE__); $GLOBALS["UFDB_SOCKET_ERROR"] = 0; $GLOBALS["UFDB_SOCKET_ERROR_TIME"] = 0; } $prefix = "http"; $sitename = $PARAMS["host"]; $uri = $PARAMS["URI"]; $IP = $PARAMS["IP"]; $userid = $PARAMS["userid"]; $PROTO = $PARAMS["PROTO"]; if ($PROTO == "CONNECT") { $prefix = "https"; } if ($userid == "-") { $userid = null; } if (!preg_match("#^http#", $uri)) { $uri = "{$prefix}://{$uri}"; } if (preg_match("#([0-9\\.]+)\\/(.*)#", $IP, $re)) { $hostname = $re[2]; $IP = $re[1]; } $uri = urlencode($uri); if ($GLOBALS["DEBUG_WEBFILTERING"]) { events("ask_to_ufdb: [{$GLOBALS["UFDB_SERVER"]}] Sitename: {$sitename} uri={$uri},IP={$IP},userid={$userid},PROTO={$PROTO}"); } if ($GLOBALS["DEBUG_WEBFILTERING"]) { events("ask_to_ufdb: [{$GLOBALS["UFDB_SERVER"]}] Send \"{$datatosend}\""); } if ($GLOBALS["DEBUG_PROTOCOL"]) { events("[{$GLOBALS["UFDB_SERVER"]}] Send \"{$datatosend}\""); } $socket = @socket_create(AF_INET, SOCK_STREAM, 0); if (!is_resource($socket)) { $GLOBALS["UFDB_SOCKET_ERROR"]++; $error = @socket_strerror(socket_last_error()); events("FATAL!!!: Web filtering socket error {$error} on {$GLOBALS["UFDB_SERVER"]}:{$GLOBALS["UFDB_PORT"]} [" . __LINE__ . "]"); ufdbg_admin_mysql(1, "Web filtering socket error {$error} on {$GLOBALS["UFDB_SERVER"]}:{$GLOBALS["UFDB_PORT"]} ", null, __FILE__, __LINE__); return false; } $ret = @socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec' => $GLOBALS["UfdbgclientSockTimeOut"], 'usec' => 0)); $ret = socket_set_option($socket, SOL_SOCKET, SO_SNDTIMEO, array('sec' => $GLOBALS["UfdbgclientSockTimeOut"], 'usec' => 0)); $ret = @socket_get_option($socket, SOL_SOCKET, SO_RCVTIMEO); if ($ret === false) { $error = socket_strerror(socket_last_error()); $GLOBALS["UFDB_SOCKET_ERROR"]++; events("FATAL!!!: Web filtering socket error {$GLOBALS["UFDB_SOCKET_ERROR"]}/{$GLOBALS["UfdbgclientMaxSockTimeOut"]} socket_get_option SO_RCVTIMEO {$error} on {$GLOBALS["UFDB_SERVER"]}:{$GLOBALS["UFDB_PORT"]} [" . __LINE__ . "]"); ufdbg_admin_mysql(1, "Web filtering socket error {$GLOBALS["UFDB_SOCKET_ERROR"]}/{$GLOBALS["UfdbgclientMaxSockTimeOut"]} socket_get_option SO_RCVTIMEO {$error} on {$GLOBALS["UFDB_SERVER"]}:{$GLOBALS["UFDB_PORT"]} ", null, __FILE__, __LINE__); return false; } if (!@socket_connect($socket, $GLOBALS["UFDB_SERVER"], $GLOBALS["UFDB_PORT"])) { $GLOBALS["UFDB_SOCKET_ERROR"]++; $socket_last_error = socket_last_error($socket); $socketerror = socket_strerror(socket_last_error($socket)); events("ask_to_ufdb: [{$GLOBALS["UFDB_SERVER"]}:{$GLOBALS["UFDB_PORT"]}] "); events("FATAL!!!: Web filtering socket error [{$socket_last_error}] {$GLOBALS["UFDB_SOCKET_ERROR"]}/{$GLOBALS["UfdbgclientMaxSockTimeOut"]} {$socketerror} on {$GLOBALS["UFDB_SERVER"]}:{$GLOBALS["UFDB_PORT"]} [" . __LINE__ . "]"); ufdbg_admin_mysql(1, "Web filtering socket error [{$socket_last_error}] {$GLOBALS["UFDB_SOCKET_ERROR"]}/{$GLOBALS["UfdbgclientMaxSockTimeOut"]} {$socketerror} on {$GLOBALS["UFDB_SERVER"]}:{$GLOBALS["UFDB_PORT"]} ", null, __FILE__, __LINE__); @socket_close($socket); return false; } if ($GLOBALS["DEBUG_PROTOCOL"]) { events("ask_to_ufdb: * * * SEND * * * \"{$datatosend}\"", __LINE__); } @socket_write($socket, $datatosend, strlen($datatosend)); $buf = @socket_read($socket, 1024); if (!$buf) { $socketerror = socket_strerror(socket_last_error($socket)); $GLOBALS["UFDB_SOCKET_ERROR"]++; events("ask_to_ufdb: [{$GLOBALS["UFDB_SERVER"]} Socket error:{$socketerror}"); events("FATAL!!!: Web filtering socket error {$GLOBALS["UFDB_SOCKET_ERROR"]}/{$GLOBALS["UfdbgclientMaxSockTimeOut"]} {$socketerror} on {$GLOBALS["UFDB_SERVER"]}:{$GLOBALS["UFDB_PORT"]} [" . __LINE__ . "]"); ufdbg_admin_mysql(1, "Web filtering socket error {$GLOBALS["UFDB_SOCKET_ERROR"]}/{$GLOBALS["UfdbgclientMaxSockTimeOut"]} {$socketerror} on {$GLOBALS["UFDB_SERVER"]}:{$GLOBALS["UFDB_PORT"]} ", null, __FILE__, __LINE__); @socket_close($socket); return false; } @socket_close($socket); $GLOBALS["UFDB_SOCKET_ERROR"] = 0; $buf = str_replace("\r\n", "", $buf); $buf = str_replace("\r", "", $buf); $buf = str_replace("\n", "", $buf); $buf = trim($buf); if ($GLOBALS["DEBUG_PROTOCOL"]) { events("[{$GLOBALS["UFDB_SERVER"]}] RECEIVE \"{$buf}\""); } if ($GLOBALS["DEBUG_WEBFILTERING"]) { events("ask_to_ufdb:[{$GLOBALS["UFDB_SERVER"]}"); } if ($GLOBALS["DEBUG_WEBFILTERING"]) { events("ask_to_ufdb:[{$GLOBALS["UFDB_SERVER"]} **********************"); } if ($GLOBALS["DEBUG_WEBFILTERING"]) { events("ask_to_ufdb:[{$GLOBALS["UFDB_SERVER"]} [proto={$PROTO}]"); } if ($GLOBALS["DEBUG_WEBFILTERING"]) { events("ask_to_ufdb:[{$GLOBALS["UFDB_SERVER"]} Receive \"{$buf}\""); } if ($GLOBALS["DEBUG_WEBFILTERING"]) { events("ask_to_ufdb:[{$GLOBALS["UFDB_SERVER"]} **********************"); } if ($GLOBALS["DEBUG_WEBFILTERING"]) { events("ask_to_ufdb:[{$GLOBALS["UFDB_SERVER"]}"); } if (strpos($buf, "loading-database") > 0) { return $buf; } if (strpos($buf, "fatalerror") > 0) { return $buf; } if ($buf == "OK") { return $buf; } if ($PROTO == "CONNECT") { $url_matched = ask_to_ufdb_parse_response($buf); if ($url_matched != null) { //if(!preg_match("#^https:#", $url_matched)){$url_matched="https://$url_matched";} $url_matched = str_replace("&url=%u", "&url={$uri}", $url_matched); $url_matched = str_replace("&clientaddr=%a", "&clientaddr={$IP}", $url_matched); $url_matched = str_replace("&clientname=%n", "&clientname={$hostname}", $url_matched); $url_matched = str_replace("&clientuser=%i", "&clientuser={$userid}", $url_matched); $url_matched = str_replace("&clientgroup=%s&targetgroup=%t", "&rule-id=0&clientgroup=generic&targetgroup=https-locked", $url_matched); return $url_matched; } } if (strpos(" {$buf}", "GET") > 0) { events_failed("{$buf}"); } if (strpos(" {$buf}", "HTTP/") > 0) { events_failed("{$buf}"); } return $buf; }
function ask_to_ufdb($datatosend){ $CachedZ=ask_to_ufdb_cache_get($datatosend); if($CachedZ[0]){return $CachedZ[1];} $socket = @socket_create(AF_INET, SOCK_STREAM, 0); if(!@socket_connect($socket, $GLOBALS["UFDB_SERVER"], $GLOBALS["UFDB_PORT"])){ $socketerror=socket_strerror(socket_last_error($socket)); events("ask_to_ufdb: [{$GLOBALS["UFDB_SERVER"]}:{$GLOBALS["UFDB_PORT"]}] "); ufdbg_admin_mysql(1, "Web filtering socket error $socketerror on {$GLOBALS["UFDB_SERVER"]}:{$GLOBALS["UFDB_PORT"]} ", null,__FILE__,__LINE__); @socket_close($socket); return false; } //if($GLOBALS["VERBOSE"]){events("ask_to_ufdb:\"$datatosend\"");} @socket_write($socket, $datatosend, strlen($datatosend)); $buf = @socket_read($socket, 1024); if(!$buf){ $socketerror=socket_strerror(socket_last_error($socket)); events("ask_to_ufdb: Socket error:$socketerror"); ufdbg_admin_mysql(1, "Web filtering socket error $socketerror on {$GLOBALS["UFDB_SERVER"]}:{$GLOBALS["UFDB_PORT"]} ", null,__FILE__,__LINE__); @socket_close($socket); return false; } @socket_close($socket); $buf=str_replace("\r\n", "", $buf); $buf=str_replace("\r", "", $buf); $buf=str_replace("\n", "", $buf); if($GLOBALS["VERBOSE"]){events("ask_to_ufdb: Receive \"$buf\"");} ask_to_ufdb_cache_set($datatosend,$buf); return $buf; }