Source for file MySQLRandomElements.class.php
Documentation is available at MySQLRandomElements.class.php
* MySQLRandomElements.class.php
* File with the class used to generate random elements and save then in MySQL (users, URL's and IP's)
* @author José Manuel Ciges Regueiro <jmanuel@ciges.net>, Web page {@link http://www.ciges.net}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPLv3
* @package InternetAccessLog
require_once("RandomElements.class.php");
* This class is used to generate random elements (users, IP's and URL's) and save them into MySQL
* With this elements created we can simulate non FTP and FTP log entries (in our demo the acces by FTP are stored in a separate collection)
* Default names for random data collections
* Default prefixes for monthly reports
* Constants for default connection values
* Connection to the database
private $db_databasename;
* Arrays to load random data in memory
* Number of element of each created collection in MySQL (for cache purposes)
private $rnd_users_number;
private $rnd_domains_number;
private $rnd_uris_number;
* Gets the connection to MySQL
public function getDB() {
* Sends a query to the database and returns the results. If no rows are got null is returned
if ($results = $this->getDB()->query($query)) {
if ($results->num_rows > 0) {
die ("Error sending the query '". $query. "' to MySQL: ". $this->db_conn->error. "\n");
* Sends a query to the database and returns the first row as an associative array. If no rows are got null is returned
public function getRow($query) {
return $results->fetch_assoc();
* Sends a query to the database and returns the first field of the first row. If no rows are got null is returned
public function getOne($query) {
$row = $results->fetch_row();
* This function says if a table exists in MySQL
* @param string tablename
private function tableExists($tablename) {
$query = "show table status where Name=\"". $tablename. "\"";
if ($result = $this->db_conn->query($query)) {
return $result->num_rows > 0;
die ("Error sending the query '". $query. "' to MySQL: ". $this->db_conn->error. "\n");
* This function deletes a table if it exists in MySQL. If the table does not exists returns false (and does nothing)
* @param string tablename
if ($this->tableExists($tablename)) {
if ($this->db_conn->query("drop table if exists ". $tablename)) {
die ("Error sending the query '". $query. "' to MySQL: ". $this->db_conn->error. "\n");
* This function deletes the table used for saving NonFTP logs. If the table does not exists returns false (and does nothing)
* @param string tablename
* Sends a query to the database and stops the script if it is no succesfull
private function sendQuery($query) {
$this->db_conn->query($query) ||
die ("Error sending the query '". $query. "' to MySQL: ". $this->db_conn->error. "\n");
* This function returns the number of records for the table passed as parameter. If the table does not exists returns 0.
* @param string tablename
private function recordNumber($tablename) {
$query = "show table status where Name=\"". $tablename. "\"";
if ($result = $this->db_conn->query($query)) {
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
return (int) $row["Rows"];
die ("Error sending the query '". $query. "' to MySQL: ". $this->db_conn->error. "\n");
* This function queries the database to return the users number (records in Random_UsersList)
$this->rnd_users_number = $this->recordNumber(self::RNDUSERSC_NAME);
return $this->rnd_users_number;
* This function returns the username searching by the id. If the user does not exist null is returner
* @return string $username
if ($userid > count($this->$rnd_users_number)) {
return $this->$rnd_users[$userid];
* This function queries the database to return the domains number (records in Random_DomainsList)
$this->rnd_domains_number = $this->recordNumber(self::RNDDOMAINSC_NAME);
return $this->rnd_domains_number;
* This function returns the domain searching by the id. If the domain does not exist null is returner
if ($id > count($this->$rnd_domains_number)) {
return $this->$rnd_domains[$id];
* This function returns the user data from the raports for a year and month specified. If there is no data returns null
* @param string $username
* @param integer $month Number from 1 to 12
$col = self::USERS_REPORT_PREFIX. $year. sprintf("%02d", $month);
$query = "select * from ". $col. " where user=\"". $username. "\"";
if ($result = $this->db_conn->query($query)) {
if ($result->num_rows > 0) {
$row = $result->fetch_array();
die ("Error sending the query '". $query. "' to MySQL");
* This function returns the domain data from the reports for a year and month specified. If there is no data returns null
* @param string $domainname
* @param integer $month Number from 1 to 12
$col = self::DOMAINS_REPORT_PREFIX. $year. sprintf("%02d", $month);
$query = "select * from ". $col. " where domain=\"". $username. "\"";
if ($result = $this->db_conn->query($query)) {
if ($result->num_rows > 0) {
$row = $result->fetch_array();
die ("Error sending the query '". $query. "' to MySQL");
* Helper function to create Users table in database
* @param string $tablename
* @param boolean $useindex Sets if a unique index for user name must be created
private function createUsersTable($tablename = self::DATA_RNDUSERSC_NAME, $use_index = true) {
if (!$this->tableExists($tablename)) {
$query = "CREATE TABLE ". $tablename. " (
id INT NOT NULL PRIMARY KEY,
unique index user_index (user)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;";
$query = "CREATE TABLE ". $tablename. " (
id INT NOT NULL PRIMARY KEY,
) ENGINE=MyISAM DEFAULT CHARSET=utf8;";
$this->db_conn->query($query) ||
die ("Error sending the query '". $query. "' to MySQL: ". $this->db_conn->error. "\n");
* This function add a user to the table passed as second argument. If not collection done then the user will be added to Random_UsersList.
* This function is coded for load tests, not for real use. The id is autonumeric
* Returns true if the user has been succesfull added, false if not
* @param string $username
* @param string $tablename
public function addFakeUser($username, $tablename = self::DATA_RNDUSERSC_NAME) {
// Table creation if it does not exists
if (!$this->tableExists($tablename)) {
$query = "CREATE TABLE ". $tablename. " (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
) ENGINE=MyISAM DEFAULT CHARSET=utf8;";
$this->db_conn->query($query) ||
die ("Error sending the query '". $query. "' to MySQL: ". $this->db_conn->error. "\n");
$query = "insert into ". $tablename. " (user) values (\"". $username. "\")";
if($this->db_conn->query($query)) {
* This function verifies if the user exists in the collection passed as second argument. If not collection done then the user will be added to Random_UsersList.
* @param string $username
* @param string $tablename
public function existUser($username, $tablename = self::RNDUSERSC_NAME) {
if ($this->tableExists($tablename)) {
$query = "select id from ". $tablename. " where user=\"". $username. "\"";
$results = $this->db_conn->query($query) ||
die ("Error sending the query '". $query. "' to MySQL: ". $this->mysrnd_con->error. "\n");
return $results->num_rows > 0;
* Constructor. For creating an instance we need to pass all the parameters for the MongoDB database where the data will be stored (user, password, host & database name).
* <li>The default user and password will be mysqldb
* <li>The default host will be localhost
* <li>The default database name will be InternetAccessLog
* @param string $password
* @param string $database
function __construct($user = self::DEFAULT_USER, $password = self::DEFAULT_PASSWORD, $host = self::DEFAULT_HOST, $database = self::DEFAULT_DB) {
// Open a connection to MySQL
$this->db_conn = new mysqli($host, $user, $password, $database);
$this->db_databasename = $database;
die("Connection MySQL impossible: (". $e->getCode(). ") ". $e->getMessage(). "\n");
// Stores the number of elements of each stored random elements collection
$this->rnd_users_number = $this->recordNumber(self::DATA_RNDUSERSC_NAME);
$this->rnd_ips_number = $this->recordNumber(self::DATA_RNDIPSC_NAME);
$this->rnd_domains_number = $this->recordNumber(self::DATA_RNDDOMAINSC_NAME);
$this->rnd_uris_number = $this->recordNumber(self::DATA_RNDURISC_NAME);
//$this->loadDataInRAM();
* Destructor. Close the open connection to MySQL database
* Save random users in MySQL.
* The parameters are the number of users two create and to booleans: if we want an unique index to be created for the user name (default is TRUE) and if we want that the user name is unique (default TRUE).
* If the user name is going to be unique the existence of the name is verified with a query before inserting a new one.
* The id will be autonumeric (1, 2, 3 ....)
* @param boolean $use_index
* @param boolean $dont_repeat
function createUsers($number, $use_index = TRUE, $dont_repeat = TRUE) {
$id = $this->rnd_users_number + 1; // Autonumeric
// Table creation if it does not exists
$this->createUsersTable(self::DATA_RNDUSERSC_NAME, $use_index);
// We verify if the user is in the collection only if it is needed
$query = "select id from ". self::DATA_RNDUSERSC_NAME. " where user=\"". $user. "\"";
if ($result = $this->db_conn->query($query)) {
die ("Error sending the query '". $query. "' to MySQL");
$query = "insert into ". self::DATA_RNDUSERSC_NAME. " (id, user) values (". $id. ", \"". $user. "\")";
$this->db_conn->query($query) ||
die ("Error sending the query '". $query. "' to MySQL: ". $this->mysrnd_con->error. "\n");
// Update users numbers property
$this->rnd_users_number = $this->recordNumber(self::DATA_RNDUSERSC_NAME);
* Returns true if the "Random_UsersList" table has records
return $this->recordNumber(self::RNDUSERSC_NAME) > 0;
* Helper function to create IPs table in database
* @param string $tablename
* @param boolean $useindex Sets if a unique index for IP name must be created
private function createIPsTable($tablename = self::DATA_RNDIPSC_NAME, $use_index = true) {
// Table creation if it does not exists
if (!$this->tableExists(self::DATA_RNDIPSC_NAME)) {
$query = "CREATE TABLE ". self::DATA_RNDIPSC_NAME. " (
id INT NOT NULL PRIMARY KEY,
unique index ip_index (ip)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;";
$query = "CREATE TABLE ". self::DATA_RNDIPSC_NAME. " (
id INT NOT NULL PRIMARY KEY,
) ENGINE=MyISAM DEFAULT CHARSET=utf8;";
$this->db_conn->query($query) ||
die ("Error sending the query '". $query. "' to MySQL: ". $this->db_conn->error. "\n");
* Save random IPs in MySQL.
* The parameters are the number of IPs to create and two booleans: if we want an unique index to be created for the IP (default is TRUE) and if we want that the IP is unique (default TRUE)
* If the IP is going to be unique the existence of itis verified with a query before inserting a new one.
* The id will be autonumeric (1, 2, 3 ....)
* @param boolean $use_index
* @param boolean $dont_repeat
function createIPs($number, $use_index = TRUE, $dont_repeat = TRUE) {
$id = $this->rnd_ips_number + 1; // Autonumeric
// Table creation if it does not exists
$this->createIPsTable(self::DATA_RNDIPSC_NAME, $use_index);
// We verify if IP is in the collection only if it is needed
$query = "select id from ". self::DATA_RNDIPSC_NAME. " where ip=\"". $ip. "\"";
if ($result = $this->db_conn->query($query)) {
die ("Error sending the query '". $query. "' to MySQL");
$query = "insert into ". self::DATA_RNDIPSC_NAME. " (id, ip) values (". $id. ", \"". $ip. "\")";
$this->db_conn->query($query) ||
die ("Error sending the query '". $query. "' to MySQL: ". $this->mysrnd_con->error. "\n");
// Update IPs number property
$this->rnd_ips_number = $this->recordNumber(self::DATA_RNDIPSC_NAME);
* Returns true if the "Random_IPsList" table has records
return $this->recordNumber(self::RNDIPSC_NAME) > 0;
* Helper function to create Domains table in database
* @param string $tablename
* @param boolean $useindex Sets if a unique index for IP name must be created
private function createDomainsTable($tablename = self::DATA_RNDDOMAINSC_NAME, $use_index = true) {
// Table creation if it does not exists
if (!$this->tableExists(self::DATA_RNDDOMAINSC_NAME)) {
$query = "CREATE TABLE ". self::DATA_RNDDOMAINSC_NAME. " (
id INT NOT NULL PRIMARY KEY,
unique index domain_index (domain)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;";
$query = "CREATE TABLE ". self::DATA_RNDDOMAINSC_NAME. " (
id INT NOT NULL PRIMARY KEY,
) ENGINE=MyISAM DEFAULT CHARSET=utf8;";
$this->db_conn->query($query) ||
die ("Error sending the query '". $query. "' to MySQL: ". $this->db_conn->error. "\n");
* Save random domains in MySQL.
* The parameters are the number of domains to create and two booleans: if we want an unique index to be created for the domain (default is TRUE) and if we want that the domain is unique (default TRUE)
* If the domain is going to be unique the existence of itis verified with a query before inserting a new one.
* The id will be autonumeric (1, 2, 3 ....)
* @param boolean $use_index
* @param boolean $dont_repeat
function createDomains($number, $use_index = TRUE, $dont_repeat = TRUE) {
$id = $this->rnd_domains_number + 1; // Autonumeric
// Table creation if it does not exists
$this->createIPsTable(self::DATA_RNDDOMAINSC_NAME, $use_index);
// We verify if the domain is in the collection only if it is needed
$query = "select id from ". self::DATA_RNDDOMAINSC_NAME. " where domain=\"". $domain. "\"";
if ($result = $this->db_conn->query($query)) {
die ("Error sending the query '". $query. "' to MySQL");
$query = "insert into ". self::DATA_RNDDOMAINSC_NAME. " (id, domain) values (". $id. ", \"". $domain. "\")";
$this->db_conn->query($query) ||
die ("Error sending the query '". $query. "' to MySQL: ". $this->mysrnd_con->error. "\n");
// Update Domains number property
$this->rnd_domains_number = $this->recordNumber(self::DATA_RNDDOMAINSC_NAME);
* Returns true if the "Random_DomainsList" table has records
return $this->recordNumber(self::RNDDOMAINSC_NAME) > 0;
* Helper function to create URIs table in database
* @param string $tablename
private function createURIsTable($tablename = self::DATA_RNDURISC_NAME) {
// Table creation if it does not exists
if (!$this->tableExists(self::DATA_RNDURISC_NAME)) {
$query = "CREATE TABLE ". self::DATA_RNDURISC_NAME. " (
id INT NOT NULL PRIMARY KEY,
) ENGINE=MyISAM DEFAULT CHARSET=utf8;";
$this->db_conn->query($query) ||
die ("Error sending the query '". $query. "' to MySQL: ". $this->db_conn->error. "\n");
* Save random URIs in MySQL.
* The parameter is the number of URIs to create.
* The id will be autonumeric (1, 2, 3 ....)
$id = $this->rnd_uris_number + 1; // Autonumeric
// Table creation if it does not exists
$this->createIPsTable(self::DATA_RNDURISC_NAME);
$query = "insert into ". self::DATA_RNDURISC_NAME. " (id, uri) values (". $id. ", \"". $uri. "\")";
$this->db_conn->query($query) ||
die ("Error sending the query '". $query. "' to MySQL: ". $this->mysrnd_con->error. "\n");
// Update Domains number property
$this->rnd_uris_number = $this->recordNumber(self::DATA_RNDURISC_NAME);
* Create in memory tables and loads user, ips and domains data from persitent ones
if ($this->tableExists(self::DATA_RNDUSERSC_NAME)) {
$this->rnd_users = array();
if ($results = $this->getResults("select * from ". self::DATA_RNDUSERSC_NAME)) {
while ($user = $results->fetch_assoc()) {
$this->rnd_users[$user['id']] = $user['user'];
if ($this->tableExists(self::DATA_RNDIPSC_NAME)) {
$this->rnd_ips = array();
if ($results = $this->getResults("select * from ". self::DATA_RNDIPSC_NAME)) {
while ($ip = $results->fetch_assoc()) {
$this->rnd_ips[$ip['id']] = $ip['ip'];
if ($this->tableExists(self::DATA_RNDDOMAINSC_NAME)) {
$this->rnd_domains = array();
if ($results = $this->getResults("select * from ". self::DATA_RNDDOMAINSC_NAME)) {
while ($domain = $results->fetch_assoc()) {
$this->rnd_domains[$domain['id']] = $domain['domain'];
if ($this->tableExists(self::DATA_RNDURISC_NAME)) {
$this->rnd_uris = array();
if ($results = $this->getResults("select * from ". self::DATA_RNDURISC_NAME)) {
while ($uri = $results->fetch_assoc()) {
$this->rnd_uris[$uri['id']] = $uri['uri'];
* Returns a random IP from the generated collection
$position = mt_rand(1, $this->rnd_ips_number);
return $this->rnd_ips[$position];
* Returns a random user from the generated collection
$position = mt_rand(1, $this->rnd_users_number);
return $this->rnd_users[$position];
* Export user table in CSV format saving it in path passed as parameter (Users.csv under CSV directory by default)
* @param string $filename
($fh = fopen($filename, "a")) || die("Not possible to open ". $filename. " file");
fwrite($fh, "id,user\n") || die("Not possible to write in ". $filename. " file");
for ($i = 1; $i <= $this->rnd_users_number; $i++ ) {
fputcsv($fh, array($i,$this->rnd_users[$i])) || die("Not possible to write in ". $filename. " file");
* Import users data in a CSV file to a MySQL table (if the table already exist it will be deleted)
* The MySQL user must have the global privilege FILE!
* @param string $filename
if ($filename == "CSV/Users.csv") {
$filename = getcwd(). "/". $filename;
$this->createUsersTable();
$this->sendQuery("load data infile \"". $filename. "\" into table ". self::DATA_RNDUSERSC_NAME. " fields terminated by ',' ignore 1 lines");
* Export ip's table in CSV format saving it in path passed as parameter (IPs.csv under CSV directory by default)
* @param string $filename
($fh = fopen($filename, "a")) || die("Not possible to open ". $filename. " file");
for ($i = 1; $i <= $this->rnd_ips_number; $i++ ) {
fputcsv($fh, array($i,$this->rnd_ips[$i])) || die("Not possible to write in ". $filename. " file");
* Import IPs data in a CSV file to a MySQL table (if the table already exist it will be deleted)
* The MySQL user must have the global privilege FILE!
* @param string $filename
if ($filename == "CSV/IPs.csv") {
$filename = getcwd(). "/". $filename;
$this->sendQuery("load data infile \"". $filename. "\" into table ". self::DATA_RNDIPSC_NAME. " fields terminated by ',' ignore 1 lines");
* Export domains table in CSV format saving it in path passed as parameter (Domains.csv under CSV directory by default)
* @param string $filename
($fh = fopen($filename, "a")) || die("Not possible to open ". $filename. " file");
for ($i = 1; $i <= $this->rnd_domains_number; $i++ ) {
fputcsv($fh, array($i,$this->rnd_domains[$i])) || die("Not possible to write in ". $filename. " file");
* Import Domains data in a CSV file to a MySQL table (if the table already exist it will be deleted)
* The MySQL user must have the global privilege FILE!
* @param string $filename
if ($filename == "CSV/Domains.csv") {
$filename = getcwd(). "/". $filename;
$this->dropTable(self::DATA_RNDDOMAINSC_NAME);
$this->createDomainsTable();
$this->sendQuery("load data infile \"". $filename. "\" into table ". self::DATA_RNDDOMAINSC_NAME. " fields terminated by ',' ignore 1 lines");
* Export URIs table in CSV format saving it in path passed as parameter (URIs.csv under CSV directory by default)
* @param string $filename
($fh = fopen($filename, "a")) || die("Not possible to open ". $filename. " file");
for ($i = 1; $i <= $this->rnd_uris_number; $i++ ) {
fputcsv($fh, array($i,$this->rnd_uris[$i])) || die("Not possible to write in ". $filename. " file");
* Import URIs data in a CSV file to a MySQL table (if the table already exist it will be deleted)
* The MySQL user must have the global privilege FILE!
* @param string $filename
if ($filename == "CSV/URIs.csv") {
$filename = getcwd(). "/". $filename;
$this->createURIsTable();
$this->sendQuery("load data infile \"". $filename. "\" into table ". self::DATA_RNDURISC_NAME. " fields terminated by ',' ignore 1 lines");
* Writes the first line (title) of a CSV file for non ftp log entry (NonFTP_Access_log.csv under CSV directory by default). If it exist it will be truncated. Returns the file handle
($fh = fopen($filename, "w")) || die("Not possible to open ". $filename. " file");
fwrite($fh, "clientip,user,datetime,method,protocol,domain,uri,return_code,size\n");
* Adds the non ftp log entry passed as parameter to a CSV file (NonFTP_Access_log.csv under CSV directory by default).
* A third optional parameter is the file handle (to not open and close the file in a for loop which calls this function)
* @param array $log_entry log entry as returned by {@link getRandomNonFTPLogEntry}
* @param string $filename
// A file handle is not given
$fh = fopen($filename, "a");
$log_entry['datetime'] = strftime("%Y-%m-%d %H:%M:%S", $log_entry['datetime']);
* Returns a random HTTP method from the generated collection
* Returns a random FTP method from the generated collection
* Returns a random domain
$position = mt_rand(1, $this->rnd_domains_number);
return $this->rnd_domains[$position];
$position = mt_rand(1, $this->rnd_uris_number);
return $this->rnd_uris[$position];
* Returns a random protocol
* Returns a random return code
* Return a random log entry for non FTP access (http and tunnel)
* It has two optional arguments, initial and final timestamps, if we want to get a random time in log entry created
* @param integer $initial_timestamp
* @param integer $final_timestamp
$ts = mt_rand($initial_timestamp, $final_timestamp);
die("Incorrect arguments number in getRrandomSORLogEntry function: ". implode(" ", $arguments). "\n");
'size' => $this->searchSize() // Size is recorded in the database as string
* Update Users monthly report
* This function is private and is meant to be used each time an access log is processed to have real time statistics (only by month)
* @param string $user user name
* @param timestamp $timestamp date & time of access
* @param integer $volume size of data transferred
private function saveUserReport($user, $timestamp, $volume) {
$table_name = self::USERS_REPORT_PREFIX. strftime("%Y%m", $timestamp);
// Table creation if it does not exists
if (!$this->tableExists($table_name)) {
$query = "CREATE TABLE ". $table_name. " (
nb INTEGER UNSIGNED NOT NULL,
volume INTEGER UNSIGNED NOT NULL,
unique index user_index (user)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;";
$this->db_conn->query($query) ||
die ("Error sending the query '". $query. "' to MySQL: ". $this->db_conn->error. "\n");
// Insertion of a new user entry
$query_insert = "insert into ". $table_name. " (user, nb, volume) values (\"". $user. "\", 1, ". $volume. ")";
$this->db_conn->query($query_insert) ||
die ("Error sending the query '". $query_insert. "' to MySQL: ". $this->db_conn->error. "\n");
$query = "select * from ". $table_name. " where user=\"". $user. "\"";
if ($result = $this->db_conn->query($query)) {
if ($result->num_rows > 0) {
// There is a user entry for this month
$row = $result->fetch_assoc();
$new_nb = $row['nb'] + 1;
$new_volume = $row['volume'] + $volume;
$query_update = "update ". $table_name. " set nb=". $new_nb. ", volume=". $new_volume. " where user=\"". $user. "\"";
$this->db_conn->query($query_update) ||
die ("Error sending the query '". $query_update. "' to MySQL: ". $this->db_conn->error. "\n");
// Insertion of a new user entry
$query_insert = "insert into ". $table_name. " (user, nb, volume) values (\"". $user. "\", 1, ". $volume. ")";
$this->db_conn->query($query_insert) ||
die ("Error sending the query '". $query_insert. "' to MySQL: ". $this->db_conn->error. "\n");
die ("Error sending the query '". $query. "' to MySQL");
* Update Domains monthly report
* This function is private and is meant to be used each time an access log is processed to have real time statistics (only by month)
* @param string $domain domain name
* @param timestamp $timestamp date & time of access
* @param integer $volume size of data transferred
private function saveDomainReport($domain, $timestamp, $volume) {
$table_name = self::DOMAINS_REPORT_PREFIX. strftime("%Y%m", $timestamp);
// Table creation if it does not exists
if (!$this->tableExists($table_name)) {
$query = "CREATE TABLE ". $table_name. " (
domain CHAR(255) NOT NULL,
nb INTEGER UNSIGNED NOT NULL,
volume INTEGER UNSIGNED NOT NULL,
unique index domain_index (domain)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;";
$this->db_conn->query($query) ||
die ("Error sending the query '". $query. "' to MySQL: ". $this->db_conn->error. "\n");
// Insertion of a new domain entry
$query_insert = "insert into ". $table_name. " (domain, nb, volume) values (\"". $domain. "\", 1, ". $volume. ")";
$this->db_conn->query($query_insert) ||
die ("Error sending the query '". $query_insert. "' to MySQL: ". $this->db_conn->error. "\n");
$query = "select * from ". $table_name. " where domain=\"". $domain. "\"";
if ($result = $this->db_conn->query($query)) {
if ($result->num_rows > 0) {
// There is a domain entry for this month
$row = $result->fetch_assoc();
$new_nb = $row['nb'] + 1;
$new_volume = $row['volume'] + $volume;
$query_update = "update ". $table_name. " set nb=". $new_nb. ", volume=". $new_volume. " where domain=\"". $domain. "\"";
$this->db_conn->query($query_update) ||
die ("Error sending the query '". $query_update. "' to MySQL: ". $this->db_conn->error. "\n");
// Insertion of a new domain entry
$query_insert = "insert into ". $table_name. " (domain, nb, volume) values (\"". $domain. "\", 1, ". $volume. ")";
$this->db_conn->query($query_insert) ||
die ("Error sending the query '". $query_insert. "' to MySQL: ". $this->db_conn->error. "\n");
die ("Error sending the query '". $query. "' to MySQL");
* Helper function to create Domains table in database
* @param string $tablename
* @param boolean $useindex Sets if a unique index for IP name must be created
* Receives a log entry and saves the data and, optionally, monthly and daily precalculated values in database.
* By default the reports are created. If the second argument is FALSE they will not be generated.
* A id field autonumeric will be created.
* @param array $log_entry log entry as returned by {@link getRandomNonFTPLogEntry}
* @param boolean $create_reports
// Table creation if it does not exists
if (!$this->tableExists(self::NONFTPLOG_NAME)) {
$query = "CREATE TABLE ". self::NONFTPLOG_NAME. " (
`id` int(11) NOT NULL AUTO_INCREMENT,
`clientip` varchar(15) NOT NULL,
`datetime` datetime NOT NULL,
`method` varchar(10) NOT NULL,
`protocol` varchar(10) NOT NULL,
`domain` varchar(255) NOT NULL,
`uri` varchar(100) NOT NULL,
`return_code` smallint(5) unsigned NOT NULL,
`size` int(10) unsigned NOT NULL,
INDEX `domain` (`domain`),
INDEX `datetime` (`datetime`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8";
$this->db_conn->query($query) ||
die ("Error sending the query '". $query. "' to MySQL: ". $this->db_conn->error. "\n");
$query = "insert into ". self::NONFTPLOG_NAME. " (clientip, user, datetime, method, protocol, domain, uri, return_code, size) values (".
"\"". $log_entry['clientip']. "\", \"". $log_entry['user']. "\", \"". date("Y:m:d H:i:s", $log_entry['datetime']). "\", \"". $log_entry['method']. "\", ".
"\"". $log_entry['protocol']. "\", \"". $log_entry['domain']. "\", \"". $log_entry['uri']. "\", ". $log_entry['return_code']. ", ". $log_entry['size']. ")";
$this->db_conn->query($query) ||
die ("Error sending the query '". $query. "' to MySQL: ". $this->mysrnd_con->error. "\n");
# Monthly reports data update
$timestamp = $log_entry["datetime"];
$this->saveUserReport($log_entry["user"], $timestamp, $log_entry['size']);
$this->saveDomainReport($log_entry["domain"], $timestamp, $log_entry['size']);
* Gets a line with a log entry in CSV format and saves the data and, optionally, monthly and daily precalculated values in database.
* By default the reports are created. If the second argument is FALSE they will not be generated.
* A id field autonumeric will be created.
* @param string $line Line in CSV format (clientip, user, datetime, method, protocol, domain, uri, return_code, size)
* @param boolean $create_reports
(list ($clientip, $user, $datetime, $method, $protocol, $domain, $uri, $return_code, $size) = explode(",", $line)) || die("Not possible to read from ". $filename. " file");
'return_code' => $return_code,
* Return a random log entry for FTP access. It is very similar to HTTP and tunnel access but with less fields (there is no protocol and return code)
* It has two optional arguments, initial and final timestamps, if we want to get a random time in log entry created
* @param integer $initial_timestamp
* @param integer $final_timestamp
$ts = mt_rand($initial_timestamp, $final_timestamp);
die("Incorrect arguments number in getRrandomSORLogEntry function: ". implode(" ", $arguments). "\n");
'size' => $this->searchSize() // Size is recorded in the database as string
* Receives a FTP log entry and saves the data and, optionally, monthly and daily precalculated values in database.
* By default the reports are created. If the second argument is FALSE they will not be generated
* A id field autonumeric will be created.
* @param array $log_entry log entry as returned by {@link getRandomNonFTPLogEntry}
* @param boolean $create_reports
// Table creation if it does not exists
if (!$this->tableExists(self::FTPLOG_NAME)) {
$query = "CREATE TABLE ". self::FTPLOG_NAME. " (
id int not null auto_increment,
clientip VARCHAR(15) NOT NULL,
datetime DATETIME NOT NULL,
method VARCHAR(10) NOT NULL,
domain VARCHAR(255) NOT NULL,
uri VARCHAR(100) NOT NULL,
size INTEGER UNSIGNED NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;";
$this->db_conn->query($query) ||
die ("Error sending the query '". $query. "' to MySQL: ". $this->db_conn->error. "\n");
$query = "insert into ". self::FTPLOG_NAME. " (clientip, user, datetime, method, domain, uri, size) values (".
"\"". $log_entry['clientip']. "\", \"". $log_entry['user']. "\", \"". date("Y-m-d H:i:s", $log_entry['datetime']). "\", \"". $log_entry['method']. "\", ".
"\"". $log_entry['domain']. "\", \"". $log_entry['uri']. "\", ". $log_entry['size']. ")";
$this->db_conn->query($query) ||
die ("Error sending the query '". $query. "' to MySQL: ". $this->mysrnd_con->error. "\n");
# Monthly reports data update
$timestamp = $log_entry["datetime"];
$this->saveUserReport($log_entry["user"], $timestamp, $log_entry['size']);
$this->saveDomainReport($log_entry["domain"], $timestamp, $log_entry['size']);
|