<?php
//ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);
?>
<html>
<head><title>INTRASTATS</title></head>
<body>
<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="0" />
<?php
/**
 * COMPATIBILITY NOTE: This file has been checked for PHP 5.6 and PHP 8.x compatibility
 * - Updated array access to use quoted string keys
 * - Replaced mysql_* function calls with mysqli equivalents via sqlquery()
 * - Added link to PHP compatibility checker
 *
 * Last checked: 2025-04-02
 */
include("../../operainfo/getq.php");

?>


<script type="text/javascript">
function updatetable(useday , usemonth , useyear) {
	document.getElementById('updatetables').innerHTML="Updating Tables";
 var link="amendmentgrid.php<?php print "$x";?>&week="+useday+"&month="+usemonth+"&year="+useyear;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
 xmlhttp=new XMLHttpRequest();
}
else
 {// code for IE6, IE5
 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
 }
xmlhttp.open("GET",link,true);
xmlhttp.send();
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById('updatetables').innerHTML=xmlhttp.responseText;
    }
  }
}
</script>


<?php

// LIST OUT ZIP FILES
function list_zipfiles() {
	$mydirectory = "/intranet/databases/intrastats_checkers/";
	// directory we want to scan
	$dircontents = scandir($mydirectory);

	// list the contents
	echo '
';
	foreach ($dircontents as $file) {
		$extension = pathinfo($file, PATHINFO_EXTENSION);
		if ($extension == 'zip') {
			echo "$file \n";
		}
	}
	echo '
';
}



$s="SELECT id, location, phpfile, datemodified, datecreated, filecode, notes FROM phplisting_auditlog";
$result=sqlquery($s);
$num=mysqli_num_rows($result);
while ($row=mysqli_fetch_array($result))
	{
	$filelocation="".$row['location']."".$row['phpfile']."";
	$filemd5=md5($filelocation);
	$lastmod[$filemd5]=$row['datemodified'];
	$lastcre[$filemd5]=$row['datecreated'];
	$originalfilecode=$row['filecode'];
	$filecodelist[$originalfilecode]=$originalfilecode;

	if ($row['datemodified']<>1485955957 and $row['datemodified']<>1575638715 and $row['datemodified']<>1383743515 and $row['datemodified']<>1575638825 and $row['notes']<>'Vaimed Sync') {
	$yearcode=date("Y",$row['datemodified']);
	$monthyearcode=date("Yn",$row['datemodified']);
	$daymonthyearcode=date("dnY",$row['datemodified']);
	$monthcode=date("n",$row['datemodified']);
	$monthlist[$monthcode]= isset($monthlist[$monthcode]) ? $monthlist[$monthcode]+1 : 1;
	$yeartotal[$yearcode]= isset($yeartotal[$yearcode]) ? $yeartotal[$yearcode]+1 : 1;
	$monthyeartotal[$monthyearcode]= isset($monthyeartotal[$monthyearcode]) ? $monthyeartotal[$monthyearcode]+1 : 1;
	$daymonthyeartotal[$daymonthyearcode]= isset($daymonthyeartotal[$daymonthyearcode]) ? $daymonthyeartotal[$daymonthyearcode]+1 : 1;
	}
	}


// Lets do some numbers
$monthsmessage = "";
$monthsmessage .= "<table class=CSSTable_n2><tr><Td>Year</td>";
$month=1;
while ($month < 13) {
	$monthsmessage .= "<td>$month</td>";
	$month++;
}
$monthsmessage .= "<td>Total</td></tr>";

foreach ($yeartotal as $year => $yeartot) {
	$monthsmessage .= "<tr><Td>$year</td>";
	$month=1;
	$linetotal=0;
	while ($month < 13) {
		$monthyearcode=$year.$month;
		$monthyeartotal[$monthyearcode]= isset($monthyeartotal[$monthyearcode]) ? $monthyeartotal[$monthyearcode] : 0;
		$monthsmessage .= "<Td><a href=\"javascript:void(0)\" onclick=\"updatetable('',".$month.",'".$year."')\">".$monthyeartotal[$monthyearcode]."</a></td>";
		$linetotal=$linetotal+$monthyeartotal[$monthyearcode];
		$month++;
	}
	$monthsmessage .= "<td><a href=\"javascript:void(0)\" onclick=\"updatetable('','','".$year."')\">$linetotal</a></td></tr>";
}
$monthsmessage .= "</table>";



$monthsmessage .= "</table>";

print "$monthsmessage";

// Dev mode banner: show when amendment log folder or source files are missing
$devBanner = "";
if (!is_dir("/var/www/vmserver10/intranet/amendmentlog")) {
    $devBanner .= "<div style='background:#fff3cd;border:1px solid #ffc107;color:#856404;padding:10px;margin-bottom:10px;border-radius:5px;'>";
    $devBanner .= "<strong>Dev Mode:</strong> Amendment log folder does not exist. File copying will be skipped.";
    $devBanner .= "</div>";
}
print $devBanner;

print "<div id='updatetables'></div>";

$s="SELECT id, location, phpfile, datemodified, datecreated FROM phplisting";
$result=sqlquery($s);

$num=mysqli_num_rows($result);
while ($row=mysqli_fetch_array($result))
	{
	$filelocation="".$row['location']."".$row['phpfile']."";
	$filemd5=md5($filelocation);

	// limiters
	//


	if ((isset($lastcre[$filemd5]) && $lastcre[$filemd5]=='' or isset($lastmod[$filemd5]) && $lastmod[$filemd5] != $row['datemodified']) )
		{
		//print "MD5 THIS : `<br>$filemd5 $filelocation<br>`";
		//print "$filemd5 ".(isset($lastcre[$filemd5]) ? $lastcre[$filemd5] : '')."=='' or ".(isset($lastmod[$filemd5]) ? $lastmod[$filemd5] : '')." != ".$row['datemodified']." <br>";

		$lastmod[$filemd5]=$row['datemodified'];
		$lastcre[$filemd5]=$row['datecreated'];
		// need to now copy the file into the Daily Amendment log

		$yearmod=date("Y",$row['datemodified']);
		$monthmod=date("M",$row['datemodified']);
		$daymod=date("d",$row['datemodified']);

		// try to make year folder /var/www/vmserver10/intranet/
		$folder="/var/www/vmserver10/intranet/amendmentlog/$yearmod";
		try {
		//print "$folder <br>";
if (!is_dir($folder)) {
    mkdir($folder, 0777, true);
}

		$folder .= "/".$monthmod;
		//print "$folder <br>";
if (!is_dir($folder)) {
    mkdir($folder, 0777, true);
}

		$folder .= "/".$daymod;
		//print "$folder <br>";
if (!is_dir($folder)) {
    mkdir($folder, 0777, true);
}

			}
		catch (Exception $e) {

			}
			$src=$row['location']."/".$row['phpfile'];
			
			// Skip if source file does not exist (dev machine case)
			if (!file_exists($src)) {
			    // print "Missing source file: $src<br>";
			    continue;
			}
			
			$filecode=md5_file($src);

if (!isset($filecodelist[$filecode])) {
    // Check if file exists before trying to copy it
    if (file_exists($src)) {
        $folder .= "/".$filecode;
        $dst = $folder;
        print "Source: $src <br>Destination: $dst $filecode<br>";
        copy($src, $dst);

        $i = "INSERT INTO phplisting_auditlog (location ,phpfile , datemodified , datecreated , filecode  )
              VALUES ('".$row['location']."','".$row['phpfile']."','".$row['datemodified']."','".$row['datecreated']."','".$filecode."')";
        sqlquery($i);

        $reload = 1;
    } else {
        // Optionally log or print a message if the source file is missing
        print "Missing source file: $src<br>";
    }
}
		// copy the file into folder


		}
	}







?>
Examine Tables / Files:<br>
Search Table name Path or File<br>
<form method=post action='searchtables.php<?php echo $x; ?>'>
<input type=text name=search>
<input type=submit>
</form><br>

<script>
function settext() {
	document.getElementById('isoimpact').value='This update does not impact on underlying ISO systems and is an Incremental Improvement';
}
</script>

<div style="margin: 20px 0; padding: 10px; background-color: #f0f8ff; border: 1px solid #add8e6; border-radius: 5px;">
<h3>PHP Compatibility Tools</h3>
<a href="php_compatibility_index.php<?php print "$x";?>" style="display: inline-block; padding: 5px 10px; background-color: #4CAF50; color: white; text-decoration: none; border-radius: 3px; margin-right: 10px;">PHP 5.6 to PHP 8.x Compatibility Checker</a>
<p>Check and track PHP compatibility across your codebase</p>
</div>

<a href="refresh_tables.php<?php print "$x";?>">Update to bring in todays changes</a><br>
<?php

$s="SELECT id, location, phpfile, datemodified, notes FROM phplisting_auditlog where updatedby='0'
and
notes !='Review Only' and
notes !='reviewed' order by location,datemodified";
$result=sqlquery($s);
$num=mysqli_num_rows($result);
if ($num >0) {
print "<br><br><h1>Required Updates</br></h1><form method=post action='signoff2mass.php<?php echo $x; ?>'>";
print "<table><tr><Td>Notes</td><td><a onclick=\"settext()\">Impact of Change to ISO systems</a></td><td>Is change significant for Notifications</td></tr>
<tr><td>
<textarea name='notes' rows=6 cols=64></textarea></td>
<td><textarea name='impact' id='isoimpact' rows=6 cols=64></textarea><br></td>
<td valign=top><select name='significant'><option value='No'>No</option><option value='Yes'>Yes</option></select><br>
Has it Been Reviewed by AI for PHP8?<br>
<select name='ai_php8'><option value='1'>Yes</option><option value='0' selected>No</option></select></td>
</tr></table>";


print "Files needing Notes:<br>";
print " <table class=CSSTable_n2><tr><td>Tag</td><td>ID</td><td>Reference Location</td><td>File Updated</td><td> Epoch Updated Date</td><td>Individual Sign Off</td></tr>";

while ($row=mysqli_fetch_array($result))
		{

		    $prndate=date(" h:i:s A d M Y",$row['datemodified']);
		print "<tr><td><input type=checkbox name='$row[id]'></td><td>
		<a href=\"/intranet/databases/intrastats_checkers/viewfile.php$x&fileid=$row[id]\" target=\"_view\">
		<img src=\"/intranet/images/mglass.png\" width=16>
		".$row['id']."</a>
		</td><td>".$row['location']."</td><Td>".$row['phpfile']."</td><td>".$row['datemodified']." $prndate</td><td>";
		?>
		<a href="/intranet/databases/intrastats_checkers/signoff.php<?php print "$x&A=$A";?>&fileid=<?php print "".$row['id']."";?>">
		<?php
		print "Requires sign off</a></td></tr>";

		}
print "</table>";
print "<input type=submit value='Submit Changes'></form>";
}
?>
<h3>Amendment Log</h3>
<a href="historypdf.php<?php print "$x";?>">Complete PDF</a><br>
<a href="historyyear.php<?php print "$x";?>">Tabled: last 12 months</a><br>
<?php
print "<table class=CSSTable_n2><tr><td>Edit</td><td>Date Modified</td><td>Location</td><td>File</td><td>Archive Code</td><td>Notes</td><td>Verification</td></tr>";
$s="SELECT id, location, phpfile, datemodified, filecode, notes, verification FROM phplisting_auditlog where
notes !='Added to Intrastats Amendment Log' and
notes !='Review Only' and
notes !='reviewed' and notes =''
order by
datemodified desc";
$result=sqlquery($s);

$num=mysqli_num_rows($result);
while ($row=mysqli_fetch_array($result))
	{

	    $moddate=date (" h:i:s A d M Y",$row['datemodified']);
	$link=date("Y",$row['datemodified'])."/".date("M",$row['datemodified'])."/".date("d",$row['datemodified'])."/";
	print "<tr><td>";
				?>
		<a href="/intranet/databases/intrastats_checkers/signoff.php<?php print "$x";?>&fileid=<?php print "".$row['id']."";?>">
		<?php
	print "Edit</a><td>$moddate</td><td>".$row['location']."</td><Td>".$row['phpfile']."</td><td><a href=\"download.php$x&link=".$link."".$row['filecode']."\">".$row['filecode']."</a></td><td>".$row['notes']."</td><td>".$row['verification']."</td><td>";

	print "</tr>";

	}



if (isset($reload) && $reload==1)
	{
	?>
	<script>
	window.location='/intranet/databases/intrastats_checkers/index.php<?php print "$x&A=$A";?>'
	</script>
	<?php
	}
	$lastfile = "";
	$mydirectory = "/var/www/vmserver10/intranet/databases/intrastats_checkers/";
	// directory we want to scan
	$dircontents = scandir($mydirectory);

	// list the contents
	echo '
';
	foreach ($dircontents as $file) {
		$extension = pathinfo($file, PATHINFO_EXTENSION);

		if ($extension == 'zip') {
			$lastfile = "Last Export $file <Br>";
		}
	}
	echo '
';
print "$lastfile";
?>
<a href="findinfile.php">find in File</a>

<script>
updatetable('','' , '');
</script>

hi
</body>
</html>
