<?

class logging {

	function logging($username) {
		$this->username = $username;
	}
	
	function act($code, $action, $info) {
		global $db, $auth, $settings;
		//send notification
		
		if ($code == 0) {
			$notifications = $db->GetAll("SELECT * FROM ".$settings->config['notifications_tablename']." WHERE action like '%".$action."%' AND ((object_type='user' AND object_id='".$auth->currentUserInfo['id']."') OR (object_type='group' AND object_id='".$auth->currentUserInfo['gid']."'))");
			foreach($notifications as $notification) {
				$mail = new htmlMimeMail();
				$message = "\n";
				$message .= "Username: \"".$auth->currentUserInfo['username']."\"\n";
				$message .= "Full name: \"".$auth->currentUserInfo['firstname']." ".$auth->currentUserInfo['lastname']."\"\n";
				$message .= "Company: \"".$auth->currentUserInfo['company']."\"\n";
				$message .= "Action: \"".$action."\"\n";
				$message .= "Date: ".date("n/d/Y H:i")."\n";
				$message .= "Details: ".$info."";
				$mail->setText($message);
				$mail->setFrom(($sys['license']['unbranded']?$sys['license']['unbranded']:"afian_notification"));
				$mail->setSubject(($sys['license']['unbranded']?$sys['license']['unbranded']:"Afian")." notification");
				$result = $mail->send(array($notification['email_address']));
			}
		}
		
		$rs = $db->query("INSERT INTO `".$settings->config['activity_logs_tablename']."` SET uid='".$auth->currentUserInfo['id']."', code='".$code."', date='".time()."', action='".$action."', details='".addslashes($info)."'");
		if ($db->Affected_Rows() > 0) {
			return true;
		} else {
			return false;
		}
	}

	function get($uid="", $year, $month, $day) {
		global $db,$settings;
		$dateStart = mktime(0, 0, 0, $month, $day, $year);
		$dateEnd = mktime(23, 59, 59, $month, $day, $year);
			if ($uid) {
				$rs = $db->query("SELECT * FROM ".$settings->config['activity_logs_tablename']." WHERE uid='".$uid."' AND  date > ".$dateStart." AND date < ".$dateEnd."");
			} else {
				$rs = $db->query("SELECT ".$settings->config['activity_logs_tablename'].".*,".$settings->config['interauth_tablename'].".username FROM ".$settings->config['activity_logs_tablename'].",".$settings->config['interauth_tablename']." WHERE ".$settings->config['activity_logs_tablename'].".uid=".$settings->config['interauth_tablename'].".id AND ".$settings->config['activity_logs_tablename'].".date > ".$dateStart." AND ".$settings->config['activity_logs_tablename'].".date < ".$dateEnd."");
			}
		return $rs->GetArray();
	}
	
	function clearAll($uid) {
		global $db;
		$db->query("DELETE FROM afian_activity_logs WHERE uid='".$uid."'");
		return $db->Affected_Rows();
	}


	function lastUpload($path) {
		global $config, $db, $auth, $settings;
		$data = array ("uid"=>$auth->currentUserInfo['id'], "path"=>$path, "date"=>time());
		$sql = "SELECT *,min(id) AS minid FROM ".$settings->config['last_uploads_tablename']." GROUP BY id"; 
		$rs = $db->query($sql);
		if ($rs->RecordCount() >= $config->settings['lastuploads']) {
			$sql = "DELETE FROM ".$settings->config['last_uploads_tablename']." WHERE id='".$rs->fields['minid']."'"; 
			$rs = $db->query($sql);
		}

			$sql = "SELECT * FROM ".$settings->config['last_uploads_tablename']." WHERE id='' LIMIT 1"; 
			$rs = $db->query($sql);
			$insertSQL = $db->GetInsertSQL($rs, $data);
			$rs = $db->query($insertSQL);
			//$db->debug=0;
		if ($db->Affected_Rows() > 0) {
			return true;
		} else {
			return false;
		}
	}

	function getCSVLogs($uid, $dateStart, $dateEnd) {
		global $config, $sys, $db, $sys, $fm, $settings;
		ignore_user_abort(true);
		set_time_limit(600);
		$rs = $db->query("SELECT ".$settings->config['interauth_tablename'].".username,".$settings->config['activity_logs_tablename'].".date,".$settings->config['activity_logs_tablename'].".action,".$settings->config['activity_logs_tablename'].".details FROM ".$settings->config['activity_logs_tablename'].",".$settings->config['interauth_tablename']." WHERE ".$settings->config['activity_logs_tablename'].".date >= ".$dateStart." AND ".$settings->config['activity_logs_tablename'].".date <= ".$dateEnd." AND ".$settings->config['interauth_tablename'].".id=".$settings->config['activity_logs_tablename'].".uid");
		require_once($sys['root']."/system/class/adodb/rsfilter.inc.php");
		require_once(gluePath($sys['root'], "system/class/adodb/toexport.inc.php"));
		$rs = RSFilter($rs,'readableDate');
		$data = rs2csv($rs,$addtitles=true);
		$date = getdate($config->settings["logsactionlastdate"]);
		return $data;
	}
	
	function clearLogs($uid, $dateStart, $dateEnd) {
		global $config, $sys, $db, $sys, $fm, $settings;
		$rs = $db->query("DELETE FROM ".$settings->config['activity_logs_tablename']." WHERE uid='".$uid."' ".$settings->config['activity_logs_tablename'].".date >= ".$dateStart." AND ".$settings->config['activity_logs_tablename'].".date <= ".$dateEnd."");
		return $rs;
	}
}

	function readableDate(&$arr) {
		foreach($arr as $k => $v) {
			if ($k == "date") {
				$arr[$k] = date("d-M-Y G:i",$v);
			}
		}
	}
?>