<?php
//This code is copyright (c) ViciSoft SL, all rights reserved.
//The contents of this file are protected under law as the intellectual property 
//of ViciSoft SL. Any use, reproduction, disclosure or copying of any kind 
//without the express and written permission of ViciSoft SL is forbidden.
//Author: Vince Reid, vincereid@gmail.com
if(@$storesessionvalue=="") $storesessionvalue="virtualstore".time();
if(@$_SESSION['loggedon']!=$storesessionvalue||@$GLOBALS['disallowlogin']||!@$GLOBALS['incfunctionsdefined']) exit;
$addsuccess=TRUE;
$success=TRUE;
$successlines=0;
$faillines=0;
$failinarchive=0;
$pidnotfoundlines=0;
$pidnotfoundpids='';
$stoppedonerror=FALSE;
$showaccount=TRUE;
$dorefresh=FALSE;
$isstockupdate=FALSE;
$isimagesupdate=FALSE;
$ismailinglistupdate=FALSE;
$iscouponupdate=FALSE;
$hasworkingname=FALSE;
$hasstartdate=FALSE;
$hasenddate=FALSE;
$alreadygotadmin=getadminsettings();
$show_errors=(getpost('show_errors')=='ON');
$stop_errors=(getpost('stop_errors')=='ON');
$isupdate=(getpost('theaction')=='update');
$progressevery=500;
if(@$admindateformat=='') $admindateformat=0;
@set_time_limit(180);
// ini_set('upload_max_filesize','3M');
function ect_errorno(){
	print(@$GLOBALS['ectdatabase']?$GLOBALS['ectdatabase']->errno:mysql_errno());
}
function ect_affected_rows(){
	return(@$GLOBALS['ectdatabase']?$GLOBALS['ectdatabase']->affected_rows:mysql_affected_rows());
}
function toplevel_database_error($clist){
	global $success, $errmsg;
	$errmsg=ect_error();
	$open_basedir=trim(@ini_get('open_basedir'));
	if($open_basedir!='') print "open_basedir: YES<br>";
	$errmsg.='<br>Column list is: "'  . $clist . '"<br>';
	$success=FALSE;
}
function csv_database_error(){
	global $show_errors, $stop_errors, $stoppedonerror, $successlines, $faillines, $line_num, $pidnotfoundlines;
	if($show_errors)
		print '<br>Line ' . $line_num . ', ' . ect_error() . '<br>';
	$theerrno=ect_errorno();
	if($stop_errors)
		$stoppedonerror=TRUE;
	if($theerrno==1062)
		$pidnotfoundlines++;
	else
		$faillines++;
	$successlines--;
}
function checkmanufacturer($pmanufacturer){
	global $manufacturerarray;
	if(@$manufacturerarray[$pmanufacturer]!='')
		return($pmanufacturer);
	return(0);
}
function updatemanufacturer($pid,$pmanufacturer){
	global $manufacturerarray,$allmanufacturers;
	if($allmanufacturers!='')
		ect_query('DELETE FROM multisearchcriteria WHERE mSCscID IN ('.$allmanufacturers.") AND mSCpID='".escape_string($pid)."'") or ect_error();
	if($pmanufacturer!=0){
		if(@$manufacturerarray[$pmanufacturer]!=''){
			ect_query('INSERT INTO multisearchcriteria (mSCscID,mSCpID) VALUES ('.$pmanufacturer.",'".escape_string($pid)."')") or ect_error();
		}
	}
}
function execute_sql(){
	global $csvarray,$valuesarray,$columnarray,$columncount,$isupdate,$isstockupdate,$keycolumn,$column_list,$successlines,$faillines,$failinarchive,$pidnotfoundlines,$isimagesupdate,$ismailinglistupdate,$iscouponupdate,$hasmanufacturer,$pidnotfoundpids,$hasworkingname,$hasstartdate,$hasenddate;
	if($isimagesupdate){
		$sSQL="SELECT * FROM products WHERE pID='" . escape_string($valuesarray[0]) . "'";
		$result=ect_query($sSQL) or ect_error();
		if(ect_num_rows($result)>0){
			if($isupdate){
				if($valuesarray[1]=='')
					$sSQL="DELETE FROM productimages WHERE imageproduct='" . escape_string($valuesarray[0]) . "' AND imagetype=" . $valuesarray[2] . " AND imagenumber=" . $valuesarray[3];
				else
					$sSQL="UPDATE productimages SET imagesrc='" . escape_string($valuesarray[1]) . "' WHERE imageproduct='" . escape_string($valuesarray[0]) . "' AND imagetype=" . $valuesarray[2] . " AND imagenumber=" . $valuesarray[3];
				ect_query($sSQL) or ect_error();
			}else{
				$sSQL="DELETE FROM productimages WHERE imageproduct='" . escape_string($valuesarray[0]) . "' AND imagetype=" . $valuesarray[2] . " AND imagenumber>=" . $valuesarray[3];
				ect_query($sSQL) or ect_error();
				$sSQL='INSERT INTO productimages (imageproduct,imagesrc,imagetype,imagenumber) VALUES (';
				$sSQL.="'" . escape_string($valuesarray[0]) . "','" . escape_string($valuesarray[1]) . "'," . $valuesarray[2] . "," . $valuesarray[3] . ")";
				ect_query($sSQL) or ect_error();
			}
		}else{
			$pidnotfoundlines++;
			$pidnotfoundpids.=$valuesarray[0].'<br />';
			$successlines--;
		}
		ect_free_result($result);
	}elseif($isstockupdate){
		if(trim($valuesarray[4])!='')
			$sSQL="UPDATE options SET optStock=" . $valuesarray[3] . " WHERE optID=" . $valuesarray[4];
		else
			$sSQL="UPDATE products SET pInStock=" . $valuesarray[3] . " WHERE pID='" . trim($valuesarray[0]) . "'";
		ect_query($sSQL) or ect_error();
		if(ect_affected_rows()==0){
			$pidnotfoundlines++;
			$successlines--;
		}
	}elseif($ismailinglistupdate){
		$sSQL='UPDATE mailinglist SET ';
		$gotemail=FALSE;
		$emailaddress=$fullname='';
		for($i=0; $i < $columncount; $i++){
			if($columnarray[$i]=='email'){
				$gotemail=TRUE;
				$emailaddress=trim($valuesarray[$i]);
				if($emailaddress=='') $gotemail=FALSE;
			}elseif($columnarray[$i]=='full name'){
				$fullname=trim($valuesarray[$i]);
			}
		}
		if($gotemail){
			$sSQL="SELECT email FROM mailinglist WHERE email='" . escape_string($emailaddress) . "'";
			$result=ect_query($sSQL) or ect_error();
			$emailexists=(ect_num_rows($result)>0);
			ect_free_result($result);
			if($emailexists){
				$sSQL='UPDATE mailinglist SET isconfirmed=1';
				if($fullname!='') $sSQL.=",mlName='" . escape_string($fullname) . "'";
				$sSQL.=" WHERE email='" . escape_string($emailaddress) . "'";
			}else
				$sSQL="INSERT INTO mailinglist (email,mlName,isconfirmed,mlConfirmDate) VALUES ('" . escape_string($emailaddress) . "','" . escape_string($fullname) . "',1,'" . date('Y-m-d') . "')";
			ect_query($sSQL) or ect_error();
		}
	}elseif($isupdate){
		$sSQL="SELECT * FROM products WHERE pID='" . escape_string($valuesarray[0]) . "'";
		$result=ect_query($sSQL) or ect_error();
		$pidexists=ect_num_rows($result)>0;
		ect_free_result($result);
		
		if($pidexists){
			$sSQL='UPDATE products SET ';
			$pid='';
			$pimage='';
			$plargeimage='';
			$pgiantimage='';
			$addcomma='';
			$hasdimensions=FALSE;
			$dimspattern='PLENxPWIDxPHEI';
			$pmanufacturer=0;
			for($i=0; $i < $columncount; $i++){
				if($i != $keycolumn){
					if($columnarray[$i]=='plength' || $columnarray[$i]=='pwidth' || $columnarray[$i]=='pheight' || $columnarray[$i]=='pimage' || $columnarray[$i]=='plargeimage' || $columnarray[$i]=='pgiantimage'){
						if($columnarray[$i]=='plength' || $columnarray[$i]=='pwidth' || $columnarray[$i]=='pheight'){
							if($columnarray[$i]=='plength') $dimspattern=str_replace('PLEN',$valuesarray[$i],$dimspattern);
							if($columnarray[$i]=='pwidth') $dimspattern=str_replace('PWID',$valuesarray[$i],$dimspattern);
							if($columnarray[$i]=='pheight') $dimspattern=str_replace('PHEI',$valuesarray[$i],$dimspattern);
							$hasdimensions=TRUE;
						}
						if($columnarray[$i]=='pimage') $pimage=$valuesarray[$i];
						if($columnarray[$i]=='plargeimage') $plargeimage=$valuesarray[$i];
						if($columnarray[$i]=='pgiantimage') $pgiantimage=$valuesarray[$i];
					}else{
						if($columnarray[$i]=='pmanufacturer'){
							$valuesarray[$i]=checkmanufacturer($valuesarray[$i]);
							$pmanufacturer=$valuesarray[$i];
						}
						$sSQL.=$addcomma . $columnarray[$i] . "='" . escape_string($valuesarray[$i]) . "'";
						$addcomma=',';
					}
				}
			}
			if($hasdimensions){
				$dimspattern=str_replace('PLEN',0,$dimspattern);
				$dimspattern=str_replace('PWID',0,$dimspattern);
				$dimspattern=str_replace('PHEI',0,$dimspattern);
				$sSQL.=$addcomma . 'pDims' . "='" . $dimspattern . "'";
			}
			$sSQL.=" WHERE pID='" . escape_string($valuesarray[$keycolumn]) . "'";
			//== print $sSQL . "<br />";
			ect_query($sSQL) or ect_error();
			if($pimage!='') ect_query("UPDATE productimages SET imageSrc='" . escape_string($pimage) . "' WHERE imageProduct='" . escape_string($valuesarray[$keycolumn]) . "' AND imageNumber=0 AND imageType=0") or csv_database_error();
			if($plargeimage!='') ect_query("UPDATE productimages SET imageSrc='" . escape_string($plargeimage) . "' WHERE imageProduct='" . escape_string($valuesarray[$keycolumn]) . "' AND imageNumber=0 AND imageType=1") or csv_database_error();
			if($pgiantimage!='') ect_query("UPDATE productimages SET imageSrc='" . escape_string($pgiantimage) . "' WHERE imageProduct='" . escape_string($valuesarray[$keycolumn]) . "' AND imageNumber=0 AND imageType=2") or csv_database_error();
			if($hasmanufacturer) updatemanufacturer($valuesarray[$keycolumn],$pmanufacturer);
		}else{
			$pidnotfoundlines++;
			$pidnotfoundpids.=$valuesarray[$keycolumn].'<br />';
			$successlines--;
		}
	}else{
		$hasdimensions=FALSE;
		$dimspattern='PLENxPWIDxPHEI';
		$pid='';
		$pimage='';
		$plargeimage='';
		$pgiantimage='';
		$addcomma='';
		$pcolumns='(';
		$pvalues=') VALUES (';
		$pmanufacturer=0;
		$cpnname='';
		for($i=0; $i < $columncount; $i++){
			if($columnarray[$i]=='plength' || $columnarray[$i]=='pwidth' || $columnarray[$i]=='pheight' || $columnarray[$i]=='pimage' || $columnarray[$i]=='plargeimage' || $columnarray[$i]=='pgiantimage'){
				if($columnarray[$i]=='plength' || $columnarray[$i]=='pwidth' || $columnarray[$i]=='pheight'){
					if($columnarray[$i]=='plength') $dimspattern=str_replace('PLEN',$valuesarray[$i],$dimspattern);
					if($columnarray[$i]=='pwidth') $dimspattern=str_replace('PWID',$valuesarray[$i],$dimspattern);
					if($columnarray[$i]=='pheight') $dimspattern=str_replace('PHEI',$valuesarray[$i],$dimspattern);
					$hasdimensions=TRUE;
				}
				if($columnarray[$i]=='pimage') $pimage=$valuesarray[$i];
				if($columnarray[$i]=='plargeimage') $plargeimage=$valuesarray[$i];
				if($columnarray[$i]=='pgiantimage') $pgiantimage=$valuesarray[$i];
			}else{
				if($columnarray[$i]=='cpnname') $cpnname=$valuesarray[$i];
				if($columnarray[$i]=='pid') $pid=$valuesarray[$i];
				if($columnarray[$i]=='pmanufacturer'){
					$valuesarray[$i]=checkmanufacturer($valuesarray[$i]);
					$pmanufacturer=$valuesarray[$i];
				}
				$pcolumns.=$addcomma . $columnarray[$i];
				$pvalues.=$addcomma . "'" . escape_string($valuesarray[$i]) . "'";
				$addcomma=',';
			}
		}
		if($hasdimensions){
			$dimspattern=str_replace('PLEN',0,$dimspattern);
			$dimspattern=str_replace('PWID',0,$dimspattern);
			$dimspattern=str_replace('PHEI',0,$dimspattern);
			$pcolumns.=$addcomma . 'pDims';
			$pvalues.=$addcomma . "'" . escape_string($dimspattern) . "'";
		}
		$pidexistsinarchive=FALSE;
		if($iscouponupdate)
			$sSQL='INSERT INTO coupons ' . $pcolumns . ($hasworkingname?'':',cpnworkingname') . ($hasstartdate?'':',cpnstartdate') . ($hasenddate?'':',cpnenddate') . $pvalues . ($hasworkingname?'':",'" . escape_string($cpnname) . "'") . ($hasstartdate?'':",'2000-01-01'") . ($hasenddate?'':",'3000-01-01'") . ')';
		else{
			$sSQL="SELECT * FROM products_archive WHERE pID='" . escape_string($pid) . "'";
			$result=ect_query($sSQL) or ect_error();
			$pidexistsinarchive=ect_num_rows($result)>0;
			ect_free_result($result);
			$sSQL='INSERT INTO products ' . $pcolumns . $pvalues . ')';
		}
		if(!$pidexistsinarchive){
			ect_query($sSQL) or csv_database_error();
			if($pimage!='') ect_query("INSERT INTO productimages (imageProduct,imageSrc,imageNumber,imageType) VALUES ('" . escape_string($pid) . "','" . escape_string($pimage) . "',0,0)") or csv_database_error();
			if($plargeimage!='') ect_query("INSERT INTO productimages (imageProduct,imageSrc,imageNumber,imageType) VALUES ('" . escape_string($pid) . "','" . escape_string($plargeimage) . "',0,1)") or csv_database_error();
			if($pgiantimage!='') ect_query("INSERT INTO productimages (imageProduct,imageSrc,imageNumber,imageType) VALUES ('" . escape_string($pid) . "','" . escape_string($pgiantimage) . "',0,2)") or csv_database_error();
			if($hasmanufacturer) updatemanufacturer($pid,$pmanufacturer);
		}else{
			$failinarchive++;
			$faillines++;
			$successlines--;
		}
	}
}
function getsection($swn){
	$sSQL="SELECT sectionID FROM sections WHERE rootSection=1 AND sectionWorkingName='".escape_string($swn)."'";
	$result2=ect_query($sSQL) or ect_error();
	if($rs2=ect_fetch_assoc($result2)) $secid=$rs2['sectionID']; else $secid=0;
	ect_free_result($result2);
	return($secid);
}
function getmanufacturer($swn){
	$sSQL="SELECT scID FROM searchcriteria WHERE scGroup=0 AND scName='".escape_string($swn)."'";
	$result2=ect_query($sSQL) or ect_error();
	if($rs2=ect_fetch_assoc($result2)) $secid=$rs2['scID']; else $secid=0;
	ect_free_result($result2);
	return($secid);
}
if(getpost('posted')=="1"){
	// print '<meta http-equiv="refresh" content="2; url=admincsv.php">';
	error_reporting (E_ALL);
	@ini_set("display_errors",'On');
	$time_start=microtime_ect_float();
	$csvfile=$_FILES['csvfile'];
	$manufacturerarray=array();
	$allmanufacturers='';
	$hasmanufacturer=FALSE;
	//foreach(@$csvfile as $objItem => $objValue){
	//	print $objItem . ":" . $objValue . "<br>";
	//}
	if($csvfile['error']=='1'||@$GLOBALS['ectdemostore']){
		$max_size=ini_get('upload_max_filesize');
		$success=FALSE;
		$errmsg='CSV File was not uploaded successfully. This could be that it is larger than the maximum upload size or that the temporary upload directory is not writeable.';
		$errmsg.=' '.'The maximum upload filesize is ' . $max_size . '.';
	}elseif($csvfile['error']=='2'){
		$success=FALSE;
		$errmsg='The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form';
	}elseif($csvfile['error']=='3'){
		$success=FALSE;
		$errmsg='The uploaded file was only partially uploaded';
	}elseif($csvfile['error']=='4'){
		$success=FALSE;
		$errmsg='No file was uploaded';
	}elseif($csvfile['error']=='5'){
		$success=FALSE;
		$errmsg='Unknown upload error';
	}elseif($csvfile['error']=='6'){
		$success=FALSE;
		$errmsg='Missing a temporary folder';
	}elseif($csvfile['error']=='7'){
		$success=FALSE;
		$errmsg='Failed to write file to disk.';
	}elseif($csvfile['error']=='8'){
		$success=FALSE;
		$errmsg='A PHP extension stopped the file upload.';
	}else{
		if($csvfile['error']!=UPLOAD_ERR_OK) print "Upload error number: " . $csvfile['error'] . "<br />";
		@ini_set('auto_detect_line_endings', true);
		$csvarray=file($csvfile['tmp_name']);
		if($csvarray===FALSE) print "Could not read CSV File<br>";
		$column_list=strtolower(str_replace('"','',trim($csvarray[0])));
		// pid,pname,pprice,pinstock,optid,optiongroup,options
		if($column_list=="imageproduct,imagesrc,imagetype,imagenumber")
			$isimagesupdate=TRUE;
		elseif($column_list=='pid,pname,pprice,pinstock,optid,optiongroup,option')
			$isstockupdate=TRUE;
		elseif(substr($column_list,0,15)=='email,full name')
			$ismailinglistupdate=TRUE;
		elseif(strpos(','.$column_list.',','cpnnumber')!==FALSE&&strpos(','.$column_list.',','cpnname')!==FALSE&&strpos(','.$column_list.',','cpndiscount')!==FALSE){
			$iscouponupdate=TRUE;
			$hasworkingname=strpos(','.$column_list.',','cpnworkingname')!==FALSE;
			$hasstartdate=strpos(','.$column_list.',','cpnstartdate')!==FALSE;
			$hasenddate=strpos(','.$column_list.',','cpnenddate')!==FALSE;
		}else{
			$column_list_copy=str_replace(' ','',$column_list);
			$column_list_copy=str_replace('plength','',$column_list_copy);
			$column_list_copy=str_replace('pwidth','',$column_list_copy);
			$column_list_copy=str_replace('pheight','',$column_list_copy);
			$column_list_copy=str_replace('pimage','',$column_list_copy);
			$column_list_copy=str_replace('plargeimage','',$column_list_copy);
			$column_list_copy=str_replace('pgiantimage','',$column_list_copy);
			while($column_list_copy != str_replace(',,',',',$column_list_copy)){
				$column_list_copy=str_replace(',,',',',$column_list_copy);
			}
			$column_list_copy=trim($column_list_copy,',');
			ect_query('SELECT ' . $column_list_copy . " FROM products WHERE pID='abcwxyz'") or toplevel_database_error($column_list_copy);
		}
		if($success){
			$columnarray=explode(',', $column_list);
			$valuesarray=$columnarray;
			$columncount=count($columnarray);
			$iscontinue=FALSE;
			$columnnum=0;
			$keycolumn='';
			for($i=0; $i < $columncount; $i++){
				$columnarray[$i]=trim($columnarray[$i]);
				if($columnarray[$i]=='pid') $keycolumn=$i;
				if($columnarray[$i]=='pmanufacturer') $hasmanufacturer=TRUE;
			}
			if($keycolumn==='' && $isimagesupdate==FALSE && $ismailinglistupdate==FALSE && !$iscouponupdate){
				$success=FALSE;
				$errmsg="There was no pID column specified.";
			}
		}
	}
	if($success){
		if($isupdate)
			print '&nbsp;Updating row: ';
		else
			print '&nbsp;Adding row: ';
		if($hasmanufacturer){
			$sSQL="SELECT scID,scGroup,scName FROM searchcriteria WHERE scGroup=0";
			$result=ect_query($sSQL) or ect_error();
			while($rs=ect_fetch_assoc($result)){
				$manufacturerarray[$rs['scID']]=$rs['scName'];
				$allmanufacturers.=$rs['scID'].' ';
			}
			$allmanufacturers=str_replace(' ',',',trim($allmanufacturers));
			ect_free_result($result);
		}
		foreach ($csvarray as $line_num => $line) {
			if($line_num != 0){
				$line.="\r";
				if(! $iscontinue) $gotquotes=FALSE;
				$iscontinue=FALSE;
				$linelen=strlen($line);
				$index=0;
				while($index < $linelen){
					if(! $gotquotes){
						if($line[$index]=='"'){
							$needquote=TRUE;
							$index++;
						}else
							$needquote=FALSE;
						$thiscol='';
						$gotquotes=TRUE;
					}
					$thechar=$line[$index];
					if(! $needquote){
						if($thechar != ',' && $thechar != "\r" && $thechar != "\n"){
							$thiscol.=$thechar;
						}else{
							if($thechar=="\r" || $thechar=="\n") $index=$linelen;
							if(strtolower($thiscol)=='null' || strtolower($thiscol)==''){
								if($columnarray[$columnnum]=='ptax' || $columnarray[$columnnum]=='pshipping' || $columnarray[$columnnum]=='pshipping2' || $columnarray[$columnnum]=='pweight' || $columnarray[$columnnum]=='pprice' || $columnarray[$columnnum]=='pwholesaleprice' || $columnarray[$columnnum]=='plistprice' || $columnarray[$columnnum]=='pexemptions' || $columnarray[$columnnum]=='pinstock' || $columnarray[$columnnum]=='porder' || $columnarray[$columnnum]=='pmanufacturer' || $columnarray[$columnnum]=='ptotrating' || $columnarray[$columnnum]=='pnumratings')
									$thiscol='0';
								else
									$thiscol='';
							}
							if($columnarray[$columnnum]=='psection'){
								if(!is_numeric($thiscol)) $thiscol=getsection($thiscol);
								$valuesarray[$columnnum++]=$thiscol;
							}elseif($columnarray[$columnnum]=='pmanufacturer'){
								if(!is_numeric($thiscol)) $thiscol=getmanufacturer($thiscol);
								$valuesarray[$columnnum++]=$thiscol;
							}elseif($columnarray[$columnnum]=='pdateadded' && strpos($thiscol, '-')===FALSE)
								$valuesarray[$columnnum++]=date('Y-m-d', parsedate($thiscol));
							else
								$valuesarray[$columnnum++]=$thiscol;
							// print "Adding col:" . $columnarray[$columnnum-1] . ": " . $valuesarray[$columnnum-1] . "<br>";
							$gotquotes=FALSE;
						}
					}elseif($thechar=='"'){
						if($index+1 < $linelen){
							if($line[$index+1]=='"'){
								$thiscol.='"';
								$index++;
							}else
								$needquote=FALSE;
						}else
							$needquote=FALSE;
					}else{
						$pos=strpos($line, '"', $index);
						if($pos===FALSE){
							$thiscol.=substr($line, $index, $linelen - $index);
							$index=$linelen-1;
						}else{
							$pos--;
							$thiscol.=substr($line, $index, $pos - ($index-1));
							$index+=$pos - $index;
						}
						if($index+1==$linelen) $iscontinue=TRUE; // continues over line
					}
					$index++;
				}
				if(! $iscontinue){
					$successlines++;
					$columnnum=0;
					execute_sql();
					if(($line_num % $progressevery)==0){
						print $line_num . ", ";
						flush();
					}
				}
			}
			if($stoppedonerror) break;
		}
		print $line_num . "</p>";
	}
	$time_end=microtime_ect_float();
?>
      <table border="0" cellspacing="0" cellpadding="0" width="100%" align="center">
        <tr>
          <td width="100%">
			<table width="100%" border="0" cellspacing="0" cellpadding="3">
			  <tr> 
                <td width="100%" colspan="2" align="center"><?php
					if(! $success) print '<p>ERROR: ' . $errmsg . '</p>';
					if($isupdate || $isimagesupdate){
						print '<p>Rows successfully updated ' . $successlines . '</p>';
						if($faillines > 0) print '<p>Error rows ' . $faillines . '</p>';
						if($pidnotfoundlines > 0) print '<p>Rows unchanged or where pID not found ' . $pidnotfoundlines . '</p>';
					}else{
						print '<p>Rows successfully added ' . $successlines . '</p>';
						if($faillines > 0) print '<p>Error rows ' . $faillines . '</p>';
						if($pidnotfoundlines > 0) print '<p>Rows with duplicate product id (pID) ' . $pidnotfoundlines . '</p>';
						if($failinarchive > 0) print '<p>Rows found in product archive ' . $failinarchive . '</p>';
						print '<p>&nbsp;</p>';
					}
					print "<p>This page took: " . round($time_end - $time_start,4) . " seconds</p>";
					if($successlines + $faillines > 0) print "<p>That is " . round(($time_end - $time_start) / ($successlines + $faillines), 4) . " seconds per row</p>";
					if($pidnotfoundpids!=''){
						print '<div style="display:inline-block;width:250px;text-align:left;margin:15px;padding:15px;border:1px solid black">';
						print '<div style="margin-bottom:10px">Rows where pID not found ' . $pidnotfoundlines . '</div>';
						print $pidnotfoundpids . '</div>';
					}
                ?></td>
			  </tr>
			  <tr> 
                <td width="100%" colspan="2" align="center"><br /><strong><?php if($stoppedonerror) print '<span style="color:#FF0000">' . $yyOpFai . '</span>'; else print $yyUpdSuc?></strong><br /><br /><br /><br />
                        Please <a href="admin.php"><strong><?php print $yyClkHer?></strong></a> for the admin home page<?php if($stoppedonerror) print ' or <a href="javascript:history.go(-1)"><strong><?php print $yyClkHer?></strong></a> to go back and try again'?>.<br />
                        <br /><br />&nbsp;
                </td>
			  </tr>
			</table></td>
        </tr>
      </table>
<?php
}else{
?>
		  <form name="mainform" method="post" action="admincsv.php" enctype="multipart/form-data">
		  <input type="hidden" name="posted" value="1">
			<table width="100%" border="0" cellspacing="0" cellpadding="2">
			  <tr>
				<td width="100%" align="center" colspan="2"><strong><?php print$yyCSVUpl?></strong><br />&nbsp;</td>
			  </tr>
			  <tr>
				<td align="right"><strong><?php print$yyCSVFlN?>:</strong></td>
				<td><input type="file" name="csvfile" /></td>
			  </tr>
			  <tr>
				<td align="right"><strong><?php print$yyAct?>:</strong></td>
				<td><select name="theaction" size="1">
					<option value="add"><?php print$yyAddDB?></option>
					<option value="update"><?php print$yyUpdDB?></option>
					</select></td>
			  </tr>
			  <tr>
				<td align="right"><strong><?php print$yyShwErr?>:</strong></td>
				<td><input type="checkbox" name="show_errors" value="ON" checked /></td>
			  </tr>
			  <tr>
				<td align="right"><strong><?php print$yyStpErr?>:</strong></td>
				<td><input type="checkbox" name="stop_errors" value="ON" checked /></td>
			  </tr>
			  <tr>
				<td width="100%" align="center" colspan="2">&nbsp;<br /><input type="submit" value="<?php print $yySubmit?>" /><br />&nbsp;</td>
			  </tr>
			  <tr> 
				<td width="100%" align="center" colspan="2"><br />
					  <a href="admin.php"><strong><?php print $yyAdmHom?></strong></a><br />&nbsp;</td>
			  </tr>
			</table>
		  </form>
<?php
}
?>