Your IP : 216.73.217.79


Current Path : /var/www/cesa.co.za/crons/
Upload File :
Current File : /var/www/cesa.co.za/crons/import.php.bak

<?php
include("../inc/auth.inc");

/*
$arrTables = array('Contacts', 'tblMemberFirms', 'OfficeContacts', 'MemberContacts', 'tblMemberFirmOffices', 
'qryEvaluationValid', 'vwNLCMembersContacts', 'EventSchedules', 'IndustryEvents', 'IndustrySchedules', 
'NLCClientBody', 'NLCOrdering', 'NLCBranchesFeedback', 'NLCReports', 'PriceList', 'SAACEBranches', 
'SAACEOfficeBearers', 'SchoolEvents', 'SchoolFacilitators', 'SchoolProviders', 'SchoolPresenters', 
'tblCommittees', 'tblCommitteeMembers', 'TrainingEvaluators', 'TrainingMaterial', 'SchoolAttendees', 
'CourseVenues', 'SchoolBankAccs');
*/

$arrTables = array('Contacts', 'tblMemberFirms', 'OfficeContacts', 'MemberContacts', 'tblMemberFirmOffices', 
'qryEvaluationValid', 'vwNLCMembersContacts', 'EventSchedules', 'IndustryEvents', 'IndustrySchedules', 
'NLCClientBody', 'NLCOrdering', 'NLCBranchesFeedback', 'NLCReports', 'PriceList', 
'SchoolEvents', 'SchoolFacilitators', 'SchoolProviders', 'SchoolPresenters', 
'TrainingEvaluators', 'TrainingMaterial', 'SchoolAttendees', 
'CourseVenues', 'SchoolBankAccs');

// $arrTables = array('Contacts', 'tblMemberFirms', 'OfficeContacts', 'MemberContacts', 'tblMemberFirmOffices');

$db = mysql_connect($loghost,$logid,$logpwd);
mysql_select_db($dbname,$db);

$sErrors = "";

// Is the synchronisation enabled?
$bSynchEnabled = false;
$dtSynchLastDate = date("Y-m-d H:i");
$sql = "SELECT SynchEnable, SynchLastDate FROM SynchConfig";
$rec = mysql_query($sql);
if ($row = mysql_fetch_assoc($rec)) {
	if ($row["SynchEnable"] == 1) $bSynchEnabled = true;
	$dtSynchLastDate = date("Y-m-d H:i", strtotime($row["SynchLastDate"]));
}
mysql_free_result($rec);

if ($bSynchEnabled) {

	// delete copy of website log (created for download by SAACEPDC)
	if (file_exists("/usr/www/users/saacea/data_from_saace/weblog.sql")) {
		rename("/usr/www/users/saacea/data_from_saace/weblog.sql", "/usr/www/users/saacea/data_from_saace/weblog.".date("YmdHi").".sql");
	}
	
	// execute sql log from SAACEPDC
	if (file_exists("/usr/www/users/saacea/data_from_saace/log.sql")) {
		$fp = fopen("/usr/www/users/saacea/data_from_saace/log.sql", "r");
		while ($sline = fgets($fp)) {
			$result = mysql_query($sline);
			if (!$result) $sErrors .= "Cannot execute from log file: ".$sline.", Error: ".mysql_error()."\r\n";
		}
		fclose($fp);
	
		// delete SAACEPDC log (uploaded by SAACEPDC)
		rename("/usr/www/users/saacea/data_from_saace/log.sql", "/usr/www/users/saacea/data_from_saace/log.".date("YmdHi").".sql");
	}
	
	for ($i=0; $i < count($arrTables); $i++) {
	
		$csvfile = "/usr/www/users/saacea/data_from_saace/".$arrTables[$i].".csv";
		$gzfile = $csvfile.".gz";
	
		if (file_exists($gzfile)) {
			
			// is this file new (since the last synch)?
			if (date("Y-m-d H:i", filemtime($gzfile)) <= $dtSynchLastDate) {
				$sErrors .= $gzfile." too old."."\r\n";
			} else {
	
				// decompress file
				$fp = gzopen($gzfile, "r");
				$contents = gzread($fp, 4*1024*1024);
				gzclose($fp);
				
				if (empty($contents)) {
						$sErrors .= $gzfile." is empty."."\r\n";
				} else {

					$arrlines = preg_split("/((\r(?!\n))|((?<!\r)\n)|(\r\n))/", $contents);
					unset($contents);

					$fp2 = fopen($csvfile, "w");

					for ($line=0; $line < count($arrlines); $line++) {
						$contents = $arrlines[$line];
						// transform dates
						$patterns = array ('#(\d{1,2})/(\d{1,2})/(19|20)(\d{2})#');
						$replace = array ('\3\4-\1-\2');
						$contents = preg_replace($patterns, $replace, $contents);	
						
						// transform booleans
						$contents = str_replace("|\"True\"|", "|1|", $contents);
						$contents = str_replace("|\"False\"|", "|0|", $contents);
						$contents = str_replace("|\"True\"|", "|1|", $contents);
						$contents = str_replace("|\"False\"|", "|0|", $contents);
						$contents = str_replace("|\"True\"|", "|1|", $contents);
						$contents = str_replace("|\"False\"|", "|0|", $contents);
					
						fwrite($fp2, $contents);
						
						fwrite($fp2, '\r\n');

						unset($contents);

					}
					fclose($fp2);
					
					echo $arrTables[$i].".csv.gz unzipped<BR>";
					
					// clear database table
					$sql = "DELETE FROM ".$arrTables[$i];
					mysql_query($sql) or $sErrors .= $arrTables[$i]." delete error: ".mysql_error()."\r\n";
					
					// upload to database table
					$sql = "LOAD DATA LOCAL INFILE '".$csvfile."'
					REPLACE
					INTO TABLE ".$arrTables[$i]."
					FIELDS
					TERMINATED BY '|'
					OPTIONALLY ENCLOSED BY '\"'
					LINES
					TERMINATED BY '\$#'
					";
					
					mysql_query($sql) or $sErrors .= $arrTables[$i]." import error: ".mysql_error()."\r\n";
					echo $arrTables[$i]." imported<br>";
				}
			}
		} else {
			$sErrors .= $gzfile." does not exist"."\r\n";
		}
		
	}
	
	if (!empty($sErrors)) mail("julia@xe.co.za", "Errors during CESA import", $sErrors);
	
	mysql_query("UPDATE SynchConfig SET SynchLastDate='".date("Y-m-d H:i")."'");
} else {
	mail("julia@xe.co.za","CESA DB Synch Disabled","");
}
?>