A fatal MySQL error occured\n\n".mysql_error()."\nQuery:\n------------\n".$query."\n-----------\nURL: http://".$_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"]."\nDate: ".date($LANG["datetime_format"])); } die("A fatal MySQL error occured:

\n\n".mysql_error($CONN)."

\n\n ".$query); } } return($r); } function nmail($CONN) { GLOBAL $COUNTER; $CONFIG=mysql_fetch_array(cnstats_sql_query("SELECT language FROM cns_config",$CONN)); include "lang/lang_".$CONFIG["language"].".php"; $MAIL=mysql_fetch_array(cnstats_sql_query("SELECT mail_day, mail_email, mail_subject, mail_content FROM cns_config",$CONN)); if (!empty($MAIL["mail_email"])) { $need=0;$per=1; if ($MAIL["mail_day"]==0) {$need=1;$pper=$LANG["yesterday"];} else { $wd=date("w",time()+$COUNTER["timeoffset"]); if($wd==0) $wd=7; if($MAIL["mail_day"]==$wd){$need=1;$per=7;$pper=$LANG["last7dayes"];}} $t1=date("Y-m-d H:i:s",mktime(0,0,0,date("m"),date("d")-1,date("Y"))+$COUNTER["timeoffset"]); $t2=date("Y-m-d H:i:s",mktime(23,59,59,date("m"),date("d")-1,date("Y"))+$COUNTER["timeoffset"]); $style="style=\"font-family: verdana;font-size: 11px;color:#333333\""; $table1=""; $table2="
"; $bg[1]="#f2fcf4";$bg[2]="#e7f9ea"; if ($need==1) { $mail=""; $mail.="\n\n"; if (($MAIL["mail_content"]&1)!=0) { $mail.=$table1."\n
\n".$table2."\n
"; $mail.=$LANG["attendanceper"]." $pper"; $mail.="CNStats 2.5
".$COUNTER["servername"].""; $mail.=date("Y-m-d H:i:s",time()+$COUNTER["timeoffset"]); $mail.="
\n\n
\n"; $mail.=$table1."\n".$table2."\n"; $mail.=$LANG["date"]; $mail.=""; $mail.=$LANG["sessionss"]; $mail.=""; $mail.=$LANG["hostss"]; $mail.=""; $mail.=$LANG["hitss"]; $mail.="\n"; $r=cnstats_sql_query("select LEFT(date,10) as dt,hits,hosts,users from cns_counter_total ORDER BY date desc LIMIT $per",$CONN); while (($a=mysql_fetch_array($r))) { $mail.="".$a["dt"]."".$a["users"]."".$a["hosts"]."".$a["hits"]."\n"; } $mail.="\n\n
\n"; } if (($MAIL["mail_content"]&2)!=0) { $mail.=$table1."\n".$table2."\n"; $mail.=$LANG["yesterdayreferers"]; $mail.="\n"; $nreferer="IF(LOCATE('?',referer),LEFT(referer,LOCATE('?',referer)-1),referer)"; $sql="select ".$nreferer.",count(*) from cns_log WHERE date>'".$t1."' AND date<'".$t2."' AND type1=1 group by 1 order by 2 desc"; $r=cnstats_sql_query($sql,$CONN); $count=mysql_num_rows($r); $k=1; for ($i=0;$i<=10;$i++) { $data=urldecode(mysql_result($r,$i,0)); $cnt=mysql_result($r,$i,1); if (!empty($data)) { $mail.=""; $mail.=$cnt; $mail.=""; $mail.=$data; $mail.="\n"; if ($k==1) $k=2; else $k=1; } } $mail.="
"; } if (($MAIL["mail_content"]&4)!=0) { $mail.=$table1."\n".$table2."\n"; $mail.=$LANG["Sspages"]; $mail.="\n"; $npage="IF(LOCATE('%3F',page),LEFT(page,LOCATE('%3F',page)-1),page)"; $sql="select IF(STRCMP(LEFT(page,13),'http%3A%2F%2F')=0,IF(LOCATE('%2F',page,13),SUBSTRING(".$npage.",LOCATE('%2F',".$npage.",13)),'/'),".$npage."),count(*) from cns_log WHERE date>'".$t1."' AND date<'".$t2."' group by 1 order by 2 desc"; $r=cnstats_sql_query($sql,$CONN); $count=mysql_num_rows($r); $k=1; for ($i=0;$i<=10;$i++) { $data=urldecode(mysql_result($r,$i,0)); $cnt=mysql_result($r,$i,1); if (!empty($data)) { $mail.=""; $mail.=$cnt; $mail.=""; $mail.=$data; $mail.="\n"; if ($k==1) $k=2; else $k=1; } } $mail.="\n"; } $mail.=""; $MAIL["mail_subject"]=str_replace("%Y",date("Y",time()+$COUNTER["timeoffset"]),$MAIL["mail_subject"]); $MAIL["mail_subject"]=str_replace("%d",date("d",time()+$COUNTER["timeoffset"]),$MAIL["mail_subject"]); $MAIL["mail_subject"]=str_replace("%m",date("m",time()+$COUNTER["timeoffset"]),$MAIL["mail_subject"]); mail($MAIL["mail_email"],$MAIL["mail_subject"],$mail,"From: \"CNStats\" <".$MAIL["mail_email"].">\nContent-type: text/html; charset=windows-1251","-f".$MAIL["mail_email"]); } } } function midnight_calc() { GLOBAL $COUNTER; $sdays=intval($COUNTER["savelog"]);if ($sdays<1 || $sdays>30) $sdays=30; cnstats_sql_query("DELETE FROM cns_log WHERE date<'".date("Y-m-d H:i:s",mktime(0,0,0,date("m") ,date("d")-$sdays,date("Y"))+$COUNTER["timeoffset"])."'",$COUNTER["CONN"]); cnstats_sql_query("OPTIMIZE TABLE cns_log",$COUNTER["CONN"]); $r=cnstats_sql_query("SHOW TABLE STATUS",$COUNTER["CONN"]); $size=0; while ($a=mysql_fetch_array($r,MYSQL_ASSOC)) { while (list ($key, $val) = each ($a)) { if ($key=="Data_length" && (substr($tname,0,4)=="cns_")) $size+=$val; if ($key=="Index_length" && (substr($tname,0,4)=="cns_")) $size+=$val; if ($key=="Name") $tname=$val; } } cnstats_sql_query("INSERT INTO cns_size SET date=NOW(), size='".$size."';",$COUNTER["CONN"]); nmail($COUNTER["CONN"]); mysql_close($COUNTER["CONN"]); } function stats_hit($sqlhost,$sqluser,$sqlpassword,$db_name) { GLOBAL $_SERVER,$HTTP_COOKIE_VARS,$STATS_CONF,$COUNTER,$_GET,$CNSTATS_DR; $noclose=false; if (!is_array($COUNTER["excludeip"])) { $tmp1=$COUNTER["excludeip"]; $tmp2=$COUNTER["excludemask"]; $COUNTER["excludemask"]=$COUNTER["excludeip"]=Array(); $COUNTER["excludeip"][]=$tmp1; $COUNTER["excludemask"][]=$tmp2; } $exclude=false; while (list ($key, $val) = each ($COUNTER["excludeip"])) { $eip=ip2long($val); $emask=ip2long($COUNTER["excludemask"][$key]); if ((ip2long($_SERVER["REMOTE_ADDR"])&$emask)==($eip&$emask)) {$exclude=true;break;} } if ($exclude) { $CONN=mysql_connect($STATS_CONF["sqlhost"],$STATS_CONF["sqluser"],$STATS_CONF["sqlpassword"],TRUE); mysql_select_db($STATS_CONF["dbname"],$CONN); $r=mysql_query("SELECT t_hits,hits,hosts FROM cns_counter") or die(mysql_error()); $STATS_CONF=mysql_fetch_array($r,MYSQL_ASSOC); mysql_close($CONN); return; } // Connecting to DB if (version_compare(phpversion(), "4.2.0", ">=")) $CONN=@mysql_connect($STATS_CONF["sqlhost"],$STATS_CONF["sqluser"],$STATS_CONF["sqlpassword"],TRUE); else $CONN=@mysql_connect($STATS_CONF["sqlhost"],$STATS_CONF["sqluser"],$STATS_CONF["sqlpassword"]); if (!$CONN) return; if (!@mysql_select_db($STATS_CONF["dbname"],$CONN)) return; $r=cnstats_sql_query("UPDATE cns_counter SET last='".date("d",time()+$COUNTER["timeoffset"]-$COUNTER["mnoffset"])."';",$CONN); if (mysql_affected_rows()==1) { ignore_user_abort(1); @set_time_limit(0); $date=date("Y-m-d H:i:s",mktime(0,0,0,date("m") ,date("d")-1,date("Y"))+$COUNTER["timeoffset"]); $r=cnstats_sql_query("SELECT hits,hosts,users FROM cns_counter",$CONN); cnstats_sql_query("UPDATE cns_counter SET hits=0, hosts=0, users=0;",$CONN); for ($i=0;$icountry_code; $city=mysql_escape_string($gir->city."|".intval($gir->latitude*10)."|".intval($gir->longitude*10)); if ($country=="" && $city==""){ $gi = geoip_open($CNSTATS_DR."cnstats/geoip/GeoIPCity.dat",GEOIP_STANDARD); $nip=long2ip($proxy); $gir=geoip_record_by_addr($gi,$nip); $country=$gir->country_code; $city=mysql_escape_string($gir->city."|".intval($gir->latitude*10)."|".intval($gir->longitude*10)); } }elseif (is_file($CNSTATS_DR."cnstats/geoip/GeoIP.dat")) { include($CNSTATS_DR."cnstats/geoip/geoip.inc"); $gi = geoip_open($CNSTATS_DR."cnstats/geoip/GeoIP.dat",GEOIP_STANDARD); $country=geoip_country_code_by_addr($gi,$nip); if ($country==""){ $gi = geoip_open($CNSTATS_DR."cnstats/geoip/GeoIP.dat",GEOIP_STANDARD); $nip=long2ip($proxy); $country=geoip_country_code_by_addr($gi,$nip); } geoip_close($gi); }else{ $country=""; $city=""; } if ($country!="") $country=(ord($country[0])<<8)+ord($country[1]); else $country=0; } cnstats_sql_query("INSERT DELAYED INTO cns_log (date,ip,type,proxy,page,agent,referer,language,type1,uid,res,depth,cookie,country,city) VALUES ('".$date."','".$ip."',".$flag.",'".$proxy."','".mysql_escape_string($page)."','".$agent."','".mysql_escape_string($referer)."','".$language."','".$type1."','".$uid."','".$res."','".$depth."','".$c."','".$country."','".$city."')",$CONN); $r=cnstats_sql_query("SELECT hits,hosts,t_hits,t_hosts,users,t_users FROM cns_counter",$CONN); if (mysql_num_rows($r)!=1) { cnstats_sql_query("INSERT INTO cns_counter SET hits='1', hosts='1', t_hits='1', t_hosts='1', users='1', t_users='1'",$CONN); $hits=1;$hosts=1;$t_hits=1;$t_hosts=1; }else{ $hits=mysql_result($r,0,0)+1; $t_hits=mysql_result($r,0,2)+1; $hosts=mysql_result($r,0,1); $t_hosts=mysql_result($r,0,3); $users=mysql_result($r,0,4); $t_users=mysql_result($r,0,5); if ($flag==1) { $hosts++;$t_hosts++; } if ($type1==1) { $users++;$t_users++; } cnstats_sql_query("UPDATE cns_counter SET hits='$hits', hosts='$hosts', t_hits='$t_hits', t_hosts='$t_hosts', users='$users', t_users='$t_users';",$CONN); } $STATS_CONF["hits"]=$hits; $STATS_CONF["hosts"]=$hosts; $STATS_CONF["t_hits"]=$t_hits; if (!$noclose) mysql_close($CONN); } stats_hit($STATS_CONF["sqlhost"], $STATS_CONF["sqluser"], $STATS_CONF["sqlpassword"], $STATS_CONF["dbname"]); ?> ** pass - Password for authentication Default: ** timeout - The timeout in seconds for the call Default: 5 ** to fsockopen() ***************************************/ function smtp($params = array()){ if(!defined('CRLF')) define('CRLF', "\r\n", TRUE); $this->authenticated = FALSE; $this->timeout = 5; $this->status = SMTP_STATUS_NOT_CONNECTED; $this->host = 'localhost'; $this->port = 25; $this->helo = 'localhost'; $this->auth = FALSE; $this->user = ''; $this->pass = ''; $this->errors = array(); foreach($params as $key => $value){ $this->$key = $value; } } /*************************************** ** Connect function. This will, when called ** statically, create a new smtp object, ** call the connect function (ie this function) ** and return it. When not called statically, ** it will connect to the server and send ** the HELO command. ***************************************/ function &connect($params = array()){ if(!isset($this->status)){ $obj = new smtp($params); if($obj->connect()){ $obj->status = SMTP_STATUS_CONNECTED; } return $obj; }else{ $this->connection = fsockopen($this->host, $this->port, $errno, $errstr, $this->timeout); if(function_exists('socket_set_timeout')){ @socket_set_timeout($this->connection, 5, 0); } $greeting = $this->get_data(); if(is_resource($this->connection)){ return $this->auth ? $this->ehlo() : $this->helo(); }else{ $this->errors[] = 'Failed to connect to server: '.$errstr; return FALSE; } } } /*************************************** ** Function which handles sending the mail. ** Arguments: ** $params - Optional assoc array of parameters. ** Can contain: ** recipients - Indexed array of recipients ** from - The from address. (used in MAIL FROM:), ** this will be the return path ** headers - Indexed array of headers, one header per array entry ** body - The body of the email ** It can also contain any of the parameters from the connect() ** function ***************************************/ function send($params = array()){ foreach($params as $key => $value){ $this->set($key, $value); } if($this->is_connected()){ // Do we auth or not? Note the distinction between the auth variable and auth() function if($this->auth AND !$this->authenticated){ if(!$this->auth()) return FALSE; } $this->mail($this->from); if(is_array($this->recipients)) foreach($this->recipients as $value) $this->rcpt($value); else $this->rcpt($this->recipients); if(!$this->data()) return FALSE; // Transparency $headers = str_replace(CRLF.'.', CRLF.'..', trim(implode(CRLF, $this->headers))); $body = str_replace(CRLF.'.', CRLF.'..', $this->body); $body = $body[0] == '.' ? '.'.$body : $body; $this->send_data($headers); $this->send_data(''); $this->send_data($body); $this->send_data('.'); $result = (substr(trim($this->get_data()), 0, 3) === '250'); //$this->rset(); return $result; }else{ $this->errors[] = 'Not connected!'; return FALSE; } } /*************************************** ** Function to implement HELO cmd ***************************************/ function helo(){ if(is_resource($this->connection) AND $this->send_data('HELO '.$this->helo) AND substr(trim($error = $this->get_data()), 0, 3) === '250' ){ return TRUE; }else{ $this->errors[] = 'HELO command failed, output: ' . trim(substr(trim($error),3)); return FALSE; } } /*************************************** ** Function to implement EHLO cmd ***************************************/ function ehlo(){ if(is_resource($this->connection) AND $this->send_data('EHLO '.$this->helo) AND substr(trim($error = $this->get_data()), 0, 3) === '250' ){ return TRUE; }else{ $this->errors[] = 'EHLO command failed, output: ' . trim(substr(trim($error),3)); return FALSE; } } /*************************************** ** Function to implement RSET cmd ***************************************/ function rset(){ if(is_resource($this->connection) AND $this->send_data('RSET') AND substr(trim($error = $this->get_data()), 0, 3) === '250' ){ return TRUE; }else{ $this->errors[] = 'RSET command failed, output: ' . trim(substr(trim($error),3)); return FALSE; } } /*************************************** ** Function to implement QUIT cmd ***************************************/ function quit(){ if(is_resource($this->connection) AND $this->send_data('QUIT') AND substr(trim($error = $this->get_data()), 0, 3) === '221' ){ fclose($this->connection); $this->status = SMTP_STATUS_NOT_CONNECTED; return TRUE; }else{ $this->errors[] = 'QUIT command failed, output: ' . trim(substr(trim($error),3)); return FALSE; } } /*************************************** ** Function to implement AUTH cmd ***************************************/ function auth(){ if(is_resource($this->connection) AND $this->send_data('AUTH LOGIN') AND substr(trim($error = $this->get_data()), 0, 3) === '334' AND $this->send_data(base64_encode($this->user)) // Send username AND substr(trim($error = $this->get_data()),0,3) === '334' AND $this->send_data(base64_encode($this->pass)) // Send password AND substr(trim($error = $this->get_data()),0,3) === '235' ){ $this->authenticated = TRUE; return TRUE; }else{ $this->errors[] = 'AUTH command failed: ' . trim(substr(trim($error),3)); return FALSE; } } /*************************************** ** Function that handles the MAIL FROM: cmd ***************************************/ function mail($from){ if($this->is_connected() AND $this->send_data('MAIL FROM:<'.$from.'>') AND substr(trim($this->get_data()), 0, 2) === '250' ){ return TRUE; }else return FALSE; } /*************************************** ** Function that handles the RCPT TO: cmd ***************************************/ function rcpt($to){ if($this->is_connected() AND $this->send_data('RCPT TO:<'.$to.'>') AND substr(trim($error = $this->get_data()), 0, 2) === '25' ){ return TRUE; }else{ $this->errors[] = trim(substr(trim($error), 3)); return FALSE; } } /*************************************** ** Function that sends the DATA cmd ***************************************/ function data(){ if($this->is_connected() AND $this->send_data('DATA') AND substr(trim($error = $this->get_data()), 0, 3) === '354' ){ return TRUE; }else{ $this->errors[] = trim(substr(trim($error), 3)); return FALSE; } } /*************************************** ** Function to determine if this object ** is connected to the server or not. ***************************************/ function is_connected(){ return (is_resource($this->connection) AND ($this->status === SMTP_STATUS_CONNECTED)); } /*************************************** ** Function to send a bit of data ***************************************/ function send_data($data){ if(is_resource($this->connection)){ return fwrite($this->connection, $data.CRLF, strlen($data)+2); }else return FALSE; } /*************************************** ** Function to get data. ***************************************/ function &get_data(){ $return = ''; $line = ''; $loops = 0; if(is_resource($this->connection)){ while((strpos($return, CRLF) === FALSE OR substr($line,3,1) !== ' ') AND $loops < 100){ $line = fgets($this->connection, 512); $return .= $line; $loops++; } return $return; }else return FALSE; } /*************************************** ** Sets a variable ***************************************/ function set($var, $value){ $this->$var = $value; return TRUE; } } // End of class ?>