public function QUERY_SQL($sql, $database = null, $called = null, $unbuffered = false) { $TableDropped_blacklist["ufdb_smtp"] = true; $database = trim($database); if ($database == "artica_backup") { $database = $this->database; } if ($database == "artica_events") { $database = $this->database; } if ($database == "ocsweb") { $database = $this->database; } if ($database == "postfixlog") { $database = $this->database; } if ($database == "powerdns") { $database = $this->database; } if ($database == "zarafa") { $database = $this->database; } if ($database == "syslogstore") { $database = $this->database; } if ($database == "metaclient") { $database = $this->database; } if ($database == null) { $database = $this->database; } $this->last_id = 0; $this->sql = $sql; $CLASS = __CLASS__; $FUNCTION = __FUNCTION__; $FILENAME = basename(__FILE__); $LOGPRF = "{$FILENAME}::{$CLASS}/{$FUNCTION}"; $this->ok = false; if (isset($GLOBALS["HOTSPOT_DEBUG"])) { if ($GLOBALS["HOTSPOT_DEBUG"]) { if (function_exists("wifidog_logs")) { wifidog_logs("{$sql}", __FUNCTION__, __LINE__); } } } $sql = trim($sql); if ($called == null) { if (function_exists("debug_backtrace")) { $trace = @debug_backtrace(); if (isset($trace[1])) { $called = "called by " . basename($trace[1]["file"]) . " {$trace[1]["function"]}() line {$trace[1]["line"]}"; } } } if (preg_match("#delete.*?webfilter_members#i", $sql)) { $this->ToSyslog("FATAL!! CHATEAU-THIERRY {$sql} {$called}"); } if ($GLOBALS["DEBUG_SQL"]) { echo "this->BD_CONNECT\n"; } @mysql_close($this->mysql_connection); if (!$this->BD_CONNECT(false, $called)) { if ($GLOBALS["VERBOSE"]) { echo "Unable to BD_CONNECT class mysql/QUERY_SQL\n"; } if (function_exists("system_admin_events")) { $trace = @debug_backtrace(); if (isset($trace[1])) { $called = "called by " . basename($trace[1]["file"]) . " {$trace[1]["function"]}() line {$trace[1]["line"]}"; } system_admin_events("MySQL error DB:\"{$database}\" Error, unable to connect to MySQL server, request failed\n{$called}", __FUNCTION__, __FILE__, __LINE__, "mysql-error"); } $this->writeLogs("QUERY_SQL:" . __LINE__ . ": DB:\"{$database}\" Error, unable to connect to MySQL server, request failed", __CLASS__ . '/' . __FUNCTION__, __LINE__); $this->ok = false; $this->mysql_error = $this->BD_CONNECT_ERROR . " Error, unable to connect to MySQL server"; $this->ToSyslog($this->mysql_error); return false; } if (preg_match("#DROP TABLE\\s+(.+)\$#i", $sql, $re)) { $TableDropped = $re[1]; if (!isset($TableDropped_blacklist[$TableDropped])) { if (function_exists("system_admin_events")) { $trace = @debug_backtrace(); if (isset($trace[1])) { $called = "called by " . basename($trace[1]["file"]) . " {$trace[1]["function"]}() line {$trace[1]["line"]}"; } system_admin_events("MySQL table {$database}/{$TableDropped} was deleted {$called}", __FUNCTION__, __FILE__, __LINE__, "mysql-delete"); } } } if ($GLOBALS["DEBUG_SQL"]) { echo "mysql_select_db()\n"; } if ($GLOBALS['VERBOSE']) { $ok = mysql_select_db($database, $this->mysql_connection); } else { $ok = @mysql_select_db($database, $this->mysql_connection); } if (!$ok) { $errnum = @mysql_errno($this->mysql_connection); $des = @mysql_error($this->mysql_connection); if (!is_numeric($errnum)) { if ($GLOBALS["VERBOSE"]) { echo "{$LOGPRF} mysql_select_db/{$this->database}/" . __LINE__ . " [FAILED] error {$errnum} {$des} -> RESTART !!\n"; } @mysql_close($this->mysql_connection); $this->mysql_connection = false; $this->BD_CONNECT(false, $called); $ok = @mysql_select_db($this->database, $this->mysql_connection); if (!$ok) { if ($GLOBALS["VERBOSE"]) { echo "{$LOGPRF} mysql_select_db/{$this->database}/" . __LINE__ . " [FAILED] -> SECOND TIME !!\n"; } if (function_exists("mysql_admin_mysql")) { mysql_admin_mysql(0, "FATAL MySQL error Connection failed to MySQL database", $sql . "\n{$this->mysql_error}", __FILE__, __LINE__); } $this->ok = false; return false; } } } if (!$ok) { $errnum = @mysql_errno($this->mysql_connection); $des = @mysql_error($this->mysql_connection); if ($GLOBALS["VERBOSE"]) { echo "{$LOGPRF} mysql_select_db/{$this->database}/" . __LINE__ . " [FAILED] N.{$errnum} DESC:{$des} mysql/QUERY_SQL\n"; } if ($GLOBALS["VERBOSE"]) { echo "mysql -u {$this->mysql_admin} -p{$this->mysql_password} -h {$this->mysql_server} -P {$this->mysql_port} -A {$this->database}\n"; } $this->mysql_errornum = $errnum; $this->mysql_error = $des; $time = date('h:i:s'); $this->writeLogs("{$LOGPRF} Line:" . __LINE__ . ":mysql_select_db DB:\"{$database}\" Error Number ({$errnum}) ({$des}) config:{$this->mysql_server}:{$this->mysql_port}@{$this->mysql_admin} ({$called})", __CLASS__ . '/' . __FUNCTION__, __LINE__); $this->mysql_error = "{$LOGPRF} Line:" . __LINE__ . ": mysql_select_db:: Error {$errnum} ({$des}) config:{$this->mysql_server}:{$this->mysql_port}@{$this->mysql_admin} line:" . __LINE__; $this->ok = false; $this->ToSyslog($this->mysql_error); $this->ToSyslog($sql); @mysql_close($this->mysql_connection); $this->mysql_connection = false; if (function_exists("mysql_admin_mysql")) { mysql_admin_mysql(0, "FATAL MySQL error Error Number ({$errnum}) ({$des})", $sql . "\n{$this->mysql_error}", __FILE__, __LINE__); } return null; } $mysql_unbuffered_query_log = null; if (preg_match("#^(UPDATE|DELETE)#i", $sql)) { $mysql_unbuffered_query_log = "mysql_unbuffered_query"; if ($GLOBALS["DEBUG_SQL"]) { echo "mysql_unbuffered_query()\n"; } $results = @mysql_unbuffered_query($sql, $this->mysql_connection); } else { if ($unbuffered) { $mysql_unbuffered_query_log = "mysql_unbuffered_query"; if ($GLOBALS["DEBUG_SQL"]) { echo "mysql_unbuffered_query()\n"; } $results = @mysql_unbuffered_query($sql, $this->mysql_connection); } else { $mysql_unbuffered_query_log = "mysql_query"; if ($GLOBALS["DEBUG_SQL"]) { echo "mysql_query()\n"; } $results = @mysql_query($sql, $this->mysql_connection); $this->last_id = @mysql_insert_id($this->mysql_connection); } } if (!$results) { $errnum = @mysql_errno($this->mysql_connection); $des = @mysql_error($this->mysql_connection); if (preg_match('#Duplicate entry#', $des)) { $this->writeLogs("QUERY_SQL:" . __LINE__ . ": DB:\"{$database}\" Error {$errnum} {$des} line:" . __LINE__, __CLASS__ . '/' . __FUNCTION__, __FILE__, __LINE__); $this->writeLogs("QUERY_SQL:" . __LINE__ . ": DB:\"{$database}\" " . substr($sql, 0, 255) . "...line:" . __LINE__, __CLASS__ . '/' . __FUNCTION__, __FILE__, __LINE__); $this->writelogs($sql, __CLASS__ . '/' . __FUNCTION__, __FILE__); $this->ok = true; @mysql_close($this->mysql_connection); $this->mysql_connection = false; return true; } $this->mysql_errornum = $errnum; $this->mysql_error = "QUERY_SQL:" . __LINE__ . ": {$mysql_unbuffered_query_log}:: {$called} Error {$errnum} ({$des}) config:{$this->mysql_server}:{$this->mysql_port}@{$this->mysql_admin} line:" . __LINE__; if (isset($GLOBALS["HOTSPOT_DEBUG"])) { if ($GLOBALS["HOTSPOT_DEBUG"]) { if (function_exists("wifidog_logs")) { wifidog_logs("{$this->mysql_error}", __FUNCTION__, __LINE__); } } } $this->ToSyslog($this->mysql_error); $sql = str_replace("\n", " ", $sql); $sql = str_replace("\t", " ", $sql); $sql = str_replace(" ", " ", $sql); $this->ToSyslog($sql); if (preg_match("#Table\\s+'(.+?)'.*?is marked as crashed#", $des, $re)) { if (class_exists("sockets")) { $sock = new sockets(); $ARRAY["DB"] = $database; $ARRAY["TABLE"] = $re[1]; $data = urlencode(base64_encode(serialize($ARRAY))); $sock->getFrameWork("squid.php?mysql-crash={$data}"); } } if ($GLOBALS["VERBOSE"]) { echo "{$LOGPRF} {$mysql_unbuffered_query_log}/" . __LINE__ . " [FAILED] N.{$errnum} DESC:{$des} {$called}\n"; } if ($GLOBALS["VERBOSE"]) { echo "{$LOGPRF} {$mysql_unbuffered_query_log}" . __LINE__ . " [FAILED] {$sql}\n"; } if (function_exists("mysql_admin_mysql")) { mysql_admin_mysql(0, "FATAL MySQL error Error Number ({$errnum}) ({$des})", $sql . "\n{$this->mysql_error}", __FILE__, __LINE__); } @mysql_free_result($this->mysql_connection); @mysql_close($this->mysql_connection); $this->mysql_connection = false; $this->ok = false; return null; } if ($GLOBALS["DEBUG_SQL"]) { echo "SUCCESS\n"; } $this->ok = true; if ($this->last_id == 0) { $this->last_id = @mysql_insert_id($this->mysql_connection); } $result_return = $results; @mysql_free_result($this->mysql_connection); @mysql_close($this->mysql_connection); $this->mysql_connection = false; return $result_return; }
function wifidog_register_perform() { session_start(); $page = CurrentPageName(); $tpl = new templates(); $autocreate_confirmed = 0; if ($GLOBALS["HOTSPOT_DEBUG"]) { wifidog_logs("Starting wifidog_register_perform()"); } $wifidog_build_uri = wifidog_build_uri(); $uriext = $wifidog_build_uri[0]; $HiddenFields = $wifidog_build_uri[1]; $sock = new wifidog_settings($_SESSION["WIFIDOG_RULES"]); $ENABLED_SMTP = intval($sock->GET_INFO("ENABLED_SMTP")); $wifidog_templates = new wifidog_templates($_SESSION["WIFIDOG_RULES"]); $ENABLED_REDIRECT_LOGIN = intval($sock->GET_INFO("ENABLED_REDIRECT_LOGIN")); $email = trim(strtolower($_POST["email"])); $q = new mysql_squid_builder(); $password2 = trim($_POST["password2"]); $password = trim($_POST["password"]); if ($password2 != $password) { return wifidog_register("{password_mismatch}"); } if ($ENABLED_SMTP == 1) { if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { return wifidog_register("{error_email_invalid}"); } $tr = explode("@", $email); $domain = $tr[1]; if (!checkdnsrr($domain, 'MX')) { return wifidog_register("«{$domain}»<br>{error_domain_email_invalid}"); } } $ligne = mysql_fetch_array($q->QUERY_SQL("SELECT uid,autocreate FROM hotspot_members WHERE `uid`='{$email}'")); if ($ENABLED_REDIRECT_LOGIN == 1) { if (intval($ligne["autocreate"]) == 1) { if ($ligne["uid"] != null) { $q->QUERY_SQL("DELETE FROM hotspot_members WHERE `uid`='{$email}'"); $ligne["uid"] = null; } } } if ($ligne["uid"] != null) { if ($ENABLED_SMTP == 1) { $link = "<br><strong><a href=\"{$page}?wifidog-recover=yes&{$uriext}\">{lost_password}</a></strong>"; } return wifidog_register($tpl->_ENGINE_parse_body("{this_account_already_exists}<hr>{$link}")); } wifidog_logs("Ruleid: {$_SESSION["WIFIDOG_RULES"]}"); $MAC = $_REQUEST["mac"]; $REGISTER_MAX_TIME = intval($sock->GET_INFO("REGISTER_MAX_TIME")); $ArticaSplashHotSpotEndTime = intval($sock->GET_INFO("ArticaSplashHotSpotEndTime")); if ($REGISTER_MAX_TIME == 0) { $REGISTER_MAX_TIME = 5; } wifidog_logs("Ruleid: REGISTER_MAX_TIME:{$REGISTER_MAX_TIME}"); wifidog_logs("Ruleid: ArticaSplashHotSpotEndTime:{$ArticaSplashHotSpotEndTime}"); $password = md5($password); $creationtime = time(); $autocreate_maxttl = $ArticaSplashHotSpotEndTime; $sessionkey = md5($password . $creationtime . $email); $gateway_addr = $_REQUEST["gw_address"]; $gw_port = $_REQUEST["gw_port"]; $gw_id = $_REQUEST["gw_id"]; $ARP = $_REQUEST["mac"]; $url = $_REQUEST["url"]; $token = $_REQUEST["token"]; if ($ENABLED_SMTP == 0) { $autocreate_confirmed = 1; } if ($GLOBALS["HOTSPOT_DEBUG"]) { wifidog_logs("Create new member {$email},{$password},TTL:{$ArticaSplashHotSpotEndTime} in line:" . __LINE__); } $sql = "INSERT IGNORE INTO hotspot_members \n\t(uid,username,token,ruleid,ttl,sessiontime,password,enabled,creationtime,autocreate,autocreate_confirmed,autocreate_maxttl,sessionkey,MAC) VALUES\n\t('{$email}','{$email}','{$token}','{$_SESSION["WIFIDOG_RULES"]}','{$ArticaSplashHotSpotEndTime}','','{$password}',1,'{$creationtime}',1,'{$autocreate_confirmed}',{$autocreate_maxttl},'{$sessionkey}','{$MAC}')"; $q->QUERY_SQL($sql); wifidog_logs("{$email}: Create New member with a token {$token}"); if (!$q->ok) { if (strpos(" {$q->mysql_error}", "Unknown column") > 0) { if ($GLOBALS["HOTSPOT_DEBUG"]) { wifidog_logs("check_hotspot_tables in line:" . __LINE__); } $q->check_hotspot_tables(); if (!$q->ok) { if ($GLOBALS["HOTSPOT_DEBUG"]) { wifidog_logs("{$q->mysql_error} in line:" . __LINE__); } } $q->QUERY_SQL($sql); } } if (!$q->ok) { wifidog_logs("{$q->mysql_error}"); wifidog_logs("{$sql}"); return wifidog_register($q->mysql_error); } if ($ENABLED_SMTP == 0) { $array["SERVER_NAME"] = $_SERVER["SERVER_NAME"]; $array["redirecturi"] = $url; $array["LOGIN"] = $email; $array["redirecturi"] = $_REQUEST["url"]; $array["REMOTE_ADDR"] = $_REQUEST["ip"]; $array["token"] = $token; $array["HOST"] = $_REQUEST["ip"]; $array["ruleid"] = $_SESSION["WIFIDOG_RULES"]; UnLock($array); wifidog_logs("wifidog_auth/" . __FUNCTION__ . ":: SESSION(HOTSPOT_REDIRECT_URL) = {$url}"); $_SESSION["HOTSPOT_AUTO_REGISTER"] = true; $_SESSION["HOTSPOT_REDIRECT_URL"] = $url; $redirecturi = "http://{$gateway_addr}:{$gw_port}/wifidog/auth?token={$token}"; wifidog_logs("wifidog_auth/" . __FUNCTION__ . ":: Redirect Token: {$token} to {$redirecturi}"); header("Location: {$redirecturi}"); return; } $proto = "http"; $myHostname = $_SERVER["HTTP_HOST"]; $page = CurrentPageName(); if (isset($_SERVER["HTTPS"])) { $proto = "https"; } $URL_REDIRECT = "{$proto}://{$myHostname}/{$page}?wifidog-confirm={$sessionkey}"; $smtp_sender = $sock->GET_INFO("smtp_sender"); $smtp_senderTR = explode("@", $smtp_sender); $instance = $smtp_senderTR[1]; $random_hash = md5(date('r', time())); $boundary = "{$random_hash}/{$instance}"; $body[] = "Return-Path: <{$smtp_sender}>"; $body[] = "Date: " . date("D, d M Y H:i:s") . " +0100 (CET)"; $body[] = "From: {$smtp_sender}"; $body[] = "Subject: {$wifidog_templates->REGISTER_SUBJECT}"; $body[] = "To: {$email}"; $body[] = "Auto-Submitted: auto-replied"; $body[] = "MIME-Version: 1.0"; $body[] = "Content-Type: multipart/mixed;"; $body[] = "\tboundary=\"{$boundary}\""; $body[] = "Content-Transfer-Encoding: 8bit"; $body[] = "Message-Id: <{$random_hash}@{$instance}>"; $body[] = "--{$boundary}"; $body[] = "Content-Description: Notification"; $body[] = "Content-Type: text/plain; charset=us-ascii"; $body[] = ""; $body[] = $wifidog_templates->REGISTER_MESSAGE; $body[] = $URL_REDIRECT; $body[] = ""; $body[] = ""; $body[] = "--{$boundary}"; $finalbody = @implode("\r\n", $body); $webauth_msmtp = new webauth_msmtp($smtp_sender, $finalbody, $email); if (!$webauth_msmtp->Send()) { $smtp = new smtp(); if ($sock->GET_INFO("smtp_auth_user") != null) { $params["auth"] = true; $params["user"] = $sock->GET_INFO("smtp_auth_user"); $params["pass"] = $sock->GET_INFO("smtp_auth_passwd"); } $params["host"] = $sock->GET_INFO("smtp_server_name"); $params["port"] = $sock->GET_INFO("smtp_server_port"); if (!$smtp->connect($params)) { return wifidog_register("{error_while_sending_message} {error} {$smtp->error_number} {$smtp->error_text}"); } if (!$smtp->send(array("from" => $smtp_sender, "recipients" => $email, "body" => $finalbody, "headers" => null))) { $smtp->quit(); return wifidog_register("{error_while_sending_message} {error} {$smtp->error_number} {$smtp->error_text}"); } if ($GLOBALS["HOTSPOT_DEBUG"]) { wifidog_logs("From: {$smtp_sender} to {$email} {$wifidog_templates->REGISTER_SUBJECT} success"); } $smtp->quit(); } if ($GLOBALS["HOTSPOT_DEBUG"]) { wifidog_logs("wifidog_auth/" . __FUNCTION__ . ":: Token: {$token} -> UnLock(.."); } $array["LOGIN"] = $email; $array["ARP"] = $_REQUEST["mac"]; $array["token"] = $token; $array["redirecturi"] = $_REQUEST["url"]; $array["REMOTE_ADDR"] = $_REQUEST["ip"]; $array["REGISTER"] = true; if (!UnLock($array)) { if ($GLOBALS["HOTSPOT_DEBUG"]) { wifidog_logs("wifidog_auth/" . __FUNCTION__ . "::failed_to_create_session"); } return wifidog_register("{error} {failed_to_create_session}"); } wifidog_logs("wifidog_auth/" . __FUNCTION__ . ":: SESSION(HOTSPOT_REDIRECT_URL) = {$url}"); $_SESSION["HOTSPOT_AUTO_REGISTER"] = true; $_SESSION["HOTSPOT_REDIRECT_URL"] = $url; $redirecturi = "http://{$gateway_addr}:{$gw_port}/wifidog/auth?token={$token}"; wifidog_logs("wifidog_auth/" . __FUNCTION__ . ":: Redirect Token: {$token} to {$redirecturi}"); header("Location: {$redirecturi}"); }
function BuildFullPage($content, $error = null, $headerAdd = null) { $prefix = null; $tpl = new templates(); $sock = new sockets(); if ($error != null) { $content = "<p class=text-error>" . $tpl->_ENGINE_parse_body($error) . "</p>{$content}"; } $wifidog_templates = new wifidog_templates($_SESSION["WIFIDOG_RULES"], $headerAdd); if ($GLOBALS["HOTSPOT_DEBUG"]) { wifidog_logs("End BuildFullPage " . strlen($content) . " bytes"); } return $wifidog_templates->build($content); }
function trust_mac() { $mac = strtoupper($_GET["trust-mac"]); $eth = trim(@file_get_contents("/etc/artica-postfix/settings/Daemons/ArticaHotSpotInterface")); $unix = new unix(); $iptables = $unix->find_program("iptables"); $cmd = "{$iptables} -t mangle -I WiFiDog_{$eth}_Trusted -m mac --mac-source {$mac} -j MARK --set-xmark 0x2/0xffffffff"; wifidog_logs($cmd, __FUNCTION__, __LINE__); shell_exec($cmd); }