<?php

// allow direct access
define('_JEXEC', 1);

// folder structure: main-joomla-folder/myfolder
define('JPATH_BASE', realpath(dirname(__FILE__) . '/..'));

// bootstrap joomla
require_once JPATH_BASE . '/includes/defines.php';
require_once JPATH_BASE . '/includes/framework.php';


function mail_mb1($sbj){
// the message
$msg = $sbj;
// use wordwrap() if lines are longer than 70 characters
$msg = wordwrap($msg,70);
// send email
mail("Questo indirizzo email è protetto dagli spambots. È necessario abilitare JavaScript per vederlo.",$sbj,$msg);
}


function cancella_mb1(&$nomefile7){
//mail_mb1("cancella");
//mail_mb1($nomefile7);
//$nomefile7="/home/trava065/air/07121036.txt";
if (file_exists($nomefile7)> 0) {
unlink($nomefile7);
echo $nomefile7." cancellato";
}
}


function glob_mb2(){
//mail_mb1("glob_mb2");
global $nomefile;
$nomefile="/home/trava065/travair.it/cntr001/air4/";
for ($a1=01; $a1 < 13; $a1++) {
$nomefile6=$nomefile.sprintf('%02d', $a1);
for ($a=01; $a < 32; $a++) {
$nomefile5=$nomefile6.sprintf('%02d', $a);
for ($b=00; $b < 24; $b++) {
$nomefile4=$nomefile5.sprintf('%02d', $b);
for ($c=0; $c < 6; $c++) {
$nomefile2=$nomefile4.$c;
for ($d=0; $d < 10; $d++) {
$nomefile3=$nomefile2.$d;
$nomefile3=$nomefile3.".txt";

if (file_exists($nomefile3)> 0) {
//mail_mb1('trovato!');
echo $nomefile3." ";
echo "<br />\n";
$directory = "/home/trava065/travair.it/cntr001/air4/";
$filecount = count(glob($directory . "*"));
if($filecount>"1"){
glob_mb1($nomefile3);
//cancella_mb1($nomefile3);
exit();
}
}
}
}
}

}
}
}


function glob_mb1(&$nomefile){
//mail_mb1('glob_mb1_c');
//mail_mb1($nomefile);
global $data;
if (($handle = fopen($nomefile, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 2000, ",")) !== FALSE) {
$num = count($data);

if($num != 36){
mail_mb1("record irregolare, glob_mb1 (cron), record diverso da 34");
mail_mb1($nomefile);
$k=(36-$num);
mail_mb1($k);
for ($c=0; $c<$k; $c++) {
$z=$num+$c;
$data[$z]="0";
}
mail_mb1(implode(',', $data));
}

//mail_mb1(implode(',', $data));
echo "<p> $num fields in line $row: <br /></p>\n";
$row++;
//mail_mb1($row);
impo_mb2($data);

//for ($c=0; $c < $num; $c++) {
// echo $data[$c] . "<br />\n";
//}
}
fclose($handle);
}

}

function impo_mb2(&$data2){
mail_mb1("impo_mb2");
//exit();
//mail_mb1(implode(',', $data2));
//echo "fx3"."<br />\n";
$tmp= array('null');
$data2 = str_replace(":", "", $data2);
$data2 = str_replace("v", "", $data2);
mail_mb1(implode(',', $data2));
$values = array_merge($tmp, $data2);
mail_mb1(implode(',', $values));
echo implode(',', $values);
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$my_table = 'trava065_joomlaste.cntr3';
$columns = array('id', 'anno', 'mese', 'giorno', 'ora', 'Volt_batt','Carica_batt','Versione','temperatura','Pressione','Umidità', 'ID_controllore', 'C1_allarme','C1_anomalia','C2_allarme','C2_anomalia','C3_allarme','C3_anomalia','E1_allarme','E2_allarme','G1_allarme','G2_allarme','G3_allarme','G4_allarme','G5_allarme','G6_allarme','G7_allarme','G8_allarme','S_1','S_2','S_3','S_4','S_5','S_6','S_7','S_8','Note',);
$query
->insert($db->quoteName($my_table))
->columns($db->quoteName($columns))
->values(implode(',', $values));
$db->setQuery($query);
$db->execute();
mail_mb1("fine");
}

function leggi_mb2(){
if (($handle = fopen("/home/trava065/air/07211306.txt", "r")) !== FALSE) {
echo "a05 ";
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
echo "<p> $num fields in line $row: <br /></p>\n";
$row++;
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "<br />\n";
}
}
fclose($handle);
}
}

function leggi_mb(){
$myfile = fopen("/home/trava065/air/07211306.txt", "r") or die("Unable to open file!");
//echo fread($myfile,filesize("/home/trava065/air/07211306.txt"));

while(!feof($myfile)) {
echo fgets($myfile) . "<br>";
echo "****" . "<br>";
}

fclose($myfile);


//echo readfile("/home/trava065/air/07211306.txt");
}

function impo_mb(){
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$my_table = 'cntr3';

$columns = array('id', 'anno', 'mese', 'giorno');

//$values = array('null', $db->quote(2023), $db->quote(01), $db->quote(17));

$query
->insert($db->quoteName($my_table))
->columns($db->quoteName($columns))
->values(implode(',', $values));


$db->setQuery($query);
$db->execute();
}

//aaaa
//mail_mb1("ini");
glob_mb2();


?>