<?php

// check di integrità di tutto il database di webapp di tutte le applicazioni.
// c/o riva stefano 2019

// versione 1 con lista applicazioni interna alla funzione di integrità.

function verifyOrInstallWEBAPP($ip, $user, $pass, $name, $prefix, $sharedfolder, $usernameLogin, $passwordLogin)
{

  include("check_session.php");
  include("../library/php/functions.php");
  include_once("dumps.php");

  set_time_limit(10000);

  $db = mysqli_connect($ip, $user, $pass);

  if (!mysqli_select_db($db, $name)) {

    mysqli_query($db, "CREATE DATABASE `" . $name . "` CHARACTER SET utf8 COLLATE utf8_general_ci");

    if (!mysqli_select_db($db, $name))
      return "error_schema_selection";
  }

  mysqli_set_charset($db, "utf8");


  // VARIABILI CHE VERRANNO PRESE DA PARAMETRO
  $channel = 'CH1';
  $company = 'COMPANY';
  $folder = "webapp_default";

  /* Fay 01-2021 */
  if (!empty($sharedfolder)) $sharedfolder = str_replace("\\", "/", $sharedfolder);

  $arrayApplications = array(
    "0" => array(0 => "Registri per il Garante", 1 => "garante"),
    "1" => array(0 => "Facebook Messages", 1 => "fbmessage"),
    "2" => array(0 => "Message Center", 1 => "messagescenter"),
    "3" => array(0 => "Languages", 1 => "languages"),
    "4" => array(0 => "Interventi", 1 => "interventi"),
    "5" => array(0 => "SMS Manager", 1 => "smsmanager"),
    "6" => array(0 => "WebControl", 1 => "webcontrol"),
    "7" => array(0 => "Twitter messages", 1 => "twittermessages"),
    "8" => array(0 => "Content Manager", 1 => "contentmanager"),
    "9" => array(0 => "Assistenza", 1 => "assistenza"),
    "10" => array(0 => "Analysis", 1 => "analysis"),
    "11" => array(0 => "Copyright Management", 1 => "rendicontazione"),
    "12" => array(0 => "Report", 1 => "Report"),
    "13" => array(0 => "Philo Advertising", 1 => "Philo"),
    "14" => array(0 => "Task Manager", 1 => "taskmanager"),
    "15" => array(0 => "Log", 1 => "logs"),
    "16" => array(0 => "Media Manager", 1 => "mediamanager"),
  );



  // CREAZIONE TABELLE WEBAPP PRINCIPALI

  // check tb_account
  $table = "tb_account";
  if ($check = mysqli_query($db, "SELECT * FROM " . $table . " LIMIT 1")) {

    checkDBField($db, "idgroup", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "name", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "lastname", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "company", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "address1", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "address2", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "idcountry", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "phone1", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "email", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "folder", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "imglogo", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "prefix", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "multiuser", $table, " int(11) DEFAULT NULL ");
  } else {

    mysqli_query($db, "CREATE TABLE  `tb_account` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `idgroup` int(11) DEFAULT NULL,
      `name` varchar(255) DEFAULT NULL,
      `lastname` varchar(255) DEFAULT NULL,
      `company` varchar(255) DEFAULT NULL,
      `address1` varchar(255) DEFAULT NULL,
      `address2` varchar(255) DEFAULT NULL,
      `idcountry` int(11) DEFAULT NULL,
      `phone1` varchar(255) DEFAULT NULL,
      `email` varchar(255) DEFAULT NULL,
      `folder` varchar(255) DEFAULT NULL,
      `imglogo` varchar(255) DEFAULT NULL,
      `prefix` varchar(255) DEFAULT NULL,
      `multiuser` int(11) DEFAULT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;");

    // dump
    $folder = str_replace(" ", "", $company);
    $folder = str_replace("'", "`", $folder);
    $folder = str_replace("<", "&lt;", $folder);
    $folder = str_replace(">", "&gt;", $folder);


    // cartelle salvataggio pdf e log 
    //if(!is_dir("../../public/".$folder)) mkdir("../../public/".$folder);
    //if(!is_dir("../../public/".$folder."/log")) mkdir("../../public/".$folder."/log");

    // creo account admin default admin@admin
    mysqli_query($db, "INSERT INTO `tb_account` (`id`, `idgroup`,`company`, `idcountry`, `folder`, `prefix`, `multiuser`) VALUES
  	 (1, 1, '" . $company . "', 106, '" . $folder . "', '" . $prefix . "', 0);");
  }


  // check tb_update
  $table = "tb_update";
  if ($check = mysqli_query($db, "SELECT * FROM " . $table . " LIMIT 1")) {

    checkDBField($db, "date", $table, " timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ");
  } else {

    mysqli_query($db, "CREATE TABLE `tb_update` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;");
  }




  // check tb_controltype
  $table = "tb_controltype";
  if ($check = mysqli_query($db, "SELECT * FROM " . $table . " LIMIT 1")) {

    checkDBField($db, "controltype", $table, " varchar(255) DEFAULT NULL ");
  } else {

    mysqli_query($db, "CREATE TABLE `tb_controltype` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `controltype` varchar(255) DEFAULT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;");


    // dump
    mysqli_query($db, "INSERT INTO `tb_controltype` (`id`,`controltype`) VALUES
  	 (1,'Orario'),
  	 (2,'Orario forzato'),
  	 (3,'Descrizione - Titolo'),
  	 (4,'Autore'),
  	 (5,'File');");
  }


  // check tb_searchtype
  $table = "tb_searchtype";
  if ($check = mysqli_query($db, "SELECT * FROM " . $table . " LIMIT 1")) {

    checkDBField($db, "searchtype", $table, " varchar(255) DEFAULT NULL ");
  } else {

    mysqli_query($db, "CREATE TABLE `tb_searchtype` (
        `id` int(11) NOT NULL AUTO_INCREMENT,
        `searchtype` varchar(255) DEFAULT NULL,
        PRIMARY KEY (`id`)
      ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;");


    // dump

    mysqli_query($db, "INSERT INTO `tb_searchtype` (`id`,`searchtype`) VALUES
  	(1,'Inizia con'),
  	(2,'Contiene'),
  	(3,'Finisce con'),
  	(4,'Esatta');");
  }


  // check tb_height
  $table = "tb_height";
  if ($check = mysqli_query($db, "SELECT * FROM " . $table . " LIMIT 1")) {

    checkDBField($db, "name", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "tb_height", $table, " int(11) DEFAULT NULL ");
  } else {

    mysqli_query($db, "CREATE TABLE  `tb_height` (
        `id` int(11) NOT NULL AUTO_INCREMENT,
        `name` varchar(255) DEFAULT NULL,
        `tb_height` int(11) DEFAULT NULL,
        PRIMARY KEY (`id`)
      ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;");


    // dump
    mysqli_query($db, "INSERT INTO `tb_height` (`id`,`name`,`tb_height`) VALUES
  	 (1,'100%',0),
  	 (2,'400px',400),
  	 (3,'600px',600),
  	 (4,'800px',800),
  	 (5,'1000px',1000),
  	 (6,'1200px',1200);");
  }


  // check tb_historylog
  $table = "tb_historylog";
  if ($check = mysqli_query($db, "SELECT * FROM " . $table . " LIMIT 1")) {

    checkDBField($db, "usr", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "pwd", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "ip", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "date", $table, " timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ");
  } else {

    mysqli_query($db, "CREATE TABLE `tb_historylog` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `usr` varchar(255) DEFAULT NULL,
      `pwd` varchar(255) DEFAULT NULL,
      `ip` varchar(255) DEFAULT NULL,
      `date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;");
  }


  // check tb_type
  $table = "tb_type";
  if ($check = mysqli_query($db, "SELECT * FROM " . $table . " LIMIT 1")) {

    checkDBField($db, "cod", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "type", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "spotper", $table, " double(5,2) DEFAULT NULL ");
    checkDBField($db, "newsper", $table, " double(5,2) DEFAULT NULL ");
  } else {

    mysqli_query($db, "CREATE TABLE `tb_type` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `cod` int(11) DEFAULT NULL,
      `type` varchar(255) DEFAULT NULL,
      `spotper` double(5,2) DEFAULT NULL,
      `newsper` double(5,2) DEFAULT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;");

    // dump
    mysqli_query($db, "INSERT INTO `tb_type` (`id`,`cod`,`type`,`spotper`,`newsper`) VALUES
  	 (1,0,'Clip',0.00,0.00),
  	 (2,1,'Video',0.00,0.00),
  	 (3,2,'Audio',0.00,0.00),
  	 (4,3,'Graphic',0.00,0.00),
  	 (5,4,'Comment',0.00,0.00),
  	 (6,5,'Automation',0.00,0.00),
  	 (7,6,'Tabellare',100.00,0.00),
  	 (8,7,'Televendita',100.00,0.00),
  	 (9,8,'Promo',0.00,0.00),
  	 (10,9,'Jingle',0.00,100.00);");
  }




  // check tb_origin
  $table = "tb_origin";
  if ($check = mysqli_query($db, "SELECT * FROM " . $table . " LIMIT 1")) {

    checkDBField($db, "origin", $table, " varchar(255) NOT NULL ");
    checkDBField($db, "color", $table, " varchar(255) NOT NULL ");
  } else {

    mysqli_query($db, "CREATE TABLE `tb_origin` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `origin` varchar(255) NOT NULL,
      `color` varchar(255) DEFAULT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;");


    // dump
    mysqli_query($db, "INSERT INTO `tb_origin` (`id`,`origin`) VALUES
  	 (1,'AUTOPRODOTTO'), (2,'ETEROPRODOTTO');");
  }


  // check tb_userlevel
  $table = "tb_userlevel";
  if ($check = mysqli_query($db, "SELECT * FROM " . $table . " LIMIT 1")) {

    checkDBField($db, "userlevel", $table, " varchar(255) NOT NULL ");
  } else {

    mysqli_query($db, "CREATE TABLE `tb_userlevel` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `userlevel` varchar(255) DEFAULT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;");

    mysqli_query($db, "INSERT INTO `tb_userlevel` (`id`,`userlevel`) VALUES
  	 (1,'Amministratore'),
  	 (2,'Gestore'),
  	 (3,'Utente'),
  	 (4,'Staff');");
  }



  // check tb_users
  $table = "tb_users";
  if ($check = mysqli_query($db, "SELECT * FROM " . $table . " LIMIT 1")) {

    checkDBField($db, "idaccount", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "idu", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "usr", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "pwd", $table, " varchar(255) DEFAULT NULL ");
  } else {

    mysqli_query($db, "CREATE TABLE `tb_users` (
        `id` int(11) NOT NULL AUTO_INCREMENT,
        `idaccount` int(11) DEFAULT NULL,
        `idu` int(11) DEFAULT NULL,
        `usr` varchar(255) DEFAULT NULL,
        `pwd` varchar(255) DEFAULT NULL,
        PRIMARY KEY (`id`)
      ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;");


    // dump
    mysqli_query($db, "INSERT INTO `tb_users` (`id`,`idaccount`,`idu`,`usr`,`pwd`) VALUES
    	 (1,0,999,'bitonlive','ffb0da7514745214815f6a25c241c7a8b8f92af4'),
       (2,1,1,'" . $usernameLogin . "','" . sha1($passwordLogin) . "');");
  }

  // check tb_webapp
  $table = "tb_webapp";
  mysqli_query($db, "DELETE FROM " . $table);
  mysqli_query($db, "CREATE TABLE `tb_webapp` (
        `id` int(11) NOT NULL AUTO_INCREMENT,
        `name` varchar(255) DEFAULT NULL,
        `namefolder` varchar(255) DEFAULT NULL,
        `active` int(11) DEFAULT NULL,
        PRIMARY KEY (`id`)
      ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;");

  // dump
  // APPLICAZIONI
  $stringWebapps = "INSERT INTO `tb_webapp` (`id`, `name`,`namefolder`,`active`) VALUES ";

  /* 
      Fay 02-2021
      Inserimento solo app (Report, Analisys, Rendicontazione, Philo e Task Manager)
      */
  for ($i = 0; $i < count($arrayApplications); $i++) {
    if (($i >= 10 && $i <= 16) || $i == 8 || $i == 6 || $i == 2) {
      $id = $i + 1;
      $stringWebapps .= " (" . $id . " , '" . $arrayApplications[$i][0] . "' , '" . $arrayApplications[$i][1] . "' , '1' ),";
    }
  }

  mysqli_query($db, substr($stringWebapps, 0, (strlen($stringWebapps) - 1)));


  // check tb_webappaccount
  $table = "tb_webappaccount";
  if ($check = mysqli_query($db, "SELECT * FROM " . $table . " LIMIT 1")) {

    checkDBField($db, "idwebapp", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "idaccount", $table, " int(11) DEFAULT NULL ");
  } else {

    mysqli_query($db, "CREATE TABLE `tb_webappaccount` (
        `id` int(11) NOT NULL AUTO_INCREMENT,
        `idwebapp` int(11) DEFAULT NULL,
        `idaccount` int(11) DEFAULT NULL,
        PRIMARY KEY (`id`)
      ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;");
  }


  // Fay 02-2021 -> non abilito l'account alle applicazione inesistenti
  // dump all applications.
  /* $stringWebapps = "INSERT INTO `tb_webappaccount` (`idwebapp`,`idaccount`) VALUES ";

      for ($i = 0; $i <= count($arrayApplications); $i++) {
        $id = $i + 1;
        $q = "SELECT COUNT(*) AS tot FROM tb_webappaccount WHERE idaccount=1 AND idwebapp=" . $id;
        //echo '1) '.$qp.'<br>';
        $vp = mysqli_query($db, $q);
        $rp = mysqli_fetch_array($vp, MYSQLI_ASSOC);
        
        if($rp['tot'] == 0) $stringWebapps .= " ( '" . $id . "' , '1' ),";
      }

      mysqli_query($db, substr($stringWebapps, 0, (strlen($stringWebapps) - 1)));*/


  // check tb_webappuser
  $table = "tb_webappuser";
  if ($check = mysqli_query($db, "SELECT * FROM " . $table . " LIMIT 1")) {

    checkDBField($db, "idwebapp", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "idaccount", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "iduser", $table, " int(11) DEFAULT NULL ");
  } else {

    mysqli_query($db, "CREATE TABLE `tb_webappuser` (
        `id` int(11) NOT NULL AUTO_INCREMENT,
        `idwebapp` int(11) DEFAULT NULL,
        `idaccount` int(11) DEFAULT NULL,
        `iduser` int(11) DEFAULT NULL,
        PRIMARY KEY (`id`)
      ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;");
  }

  // check tb_width
  $table = "tb_width";
  if ($check = mysqli_query($db, "SELECT * FROM " . $table . " LIMIT 1")) {

    checkDBField($db, "name", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "tb_width", $table, " varchar(255) DEFAULT NULL ");
  } else {

    mysqli_query($db, "CREATE TABLE `tb_width` (
        `id` int(11) NOT NULL AUTO_INCREMENT,
        `name` varchar(255) DEFAULT NULL,
        `tb_width` int(11) DEFAULT NULL,
        PRIMARY KEY (`id`)
      ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;");

    // dump

    mysqli_query($db, "INSERT INTO `tb_width` (`id`,`name`,`tb_width`) VALUES
    	 (1,'100%',0),
    	 (2,'400px',400),
    	 (3,'600px',600),
    	 (4,'800px',800),
    	 (5,'1000px',1000),
    	 (6,'1200px',1200);");
  }


  // check tb_anag_group
  $table = "tb_anag_group";
  if ($check = mysqli_query($db, "SELECT * FROM " . $table . " LIMIT 1")) {

    checkDBField($db, "name", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "description", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "email", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "phone", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "page", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "imglogo", $table, " varchar(255) DEFAULT NULL ");
  } else {

    mysqli_query($db, "CREATE TABLE  `tb_anag_group` (
        `id` int(11) NOT NULL AUTO_INCREMENT,
        `name` varchar(255) DEFAULT NULL,
        `description` varchar(255) DEFAULT NULL,
        `email` varchar(255) DEFAULT NULL,
        `phone` varchar(255) DEFAULT NULL,
        `page` varchar(255) DEFAULT NULL,
        `imglogo` varchar(255) DEFAULT NULL,
        PRIMARY KEY (`id`)
      ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;");

    // dump

    mysqli_query($db, "INSERT INTO `tb_anag_group` (`id`, `name`, `description`, `email`, `phone`, `page`, `imglogo`) VALUES (1, '" . $company . "', '', '', '', '', '')");
  }


  // check tb_user_group
  $table = "tb_user_group";
  if ($check = mysqli_query($db, "SELECT * FROM " . $table . " LIMIT 1")) {

    checkDBField($db, "iduser", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "idgroup", $table, " int(11) DEFAULT NULL ");
  } else {

    mysqli_query($db, "CREATE TABLE `tb_user_group` (
        `id` int(11) NOT NULL AUTO_INCREMENT,
        `iduser` int(11) DEFAULT NULL,
        `idgroup` int(11) DEFAULT NULL,
        PRIMARY KEY (`id`)
      ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;");
  }


  // check tb_type_channel
  $table = "tb_type_channel";
  if ($check = mysqli_query($db, "SELECT * FROM " . $table . " LIMIT 1")) {

    checkDBField($db, "code", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "name", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "description", $table, " varchar(255) DEFAULT NULL ");
  } else {

    mysqli_query($db, "CREATE TABLE `tb_type_channel` (
        `id` int(11) NOT NULL AUTO_INCREMENT,
        `code` int(11) DEFAULT NULL,
        `name` varchar(255) DEFAULT NULL,
        `description` varchar(255) DEFAULT NULL,
        PRIMARY KEY (`id`)
      ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;");

    // dump
    mysqli_query($db, "INSERT INTO `tb_type_channel` (`code`, `name`, `description`) VALUES
    		(0, 'Radio', 'Radiofonica'), (1, 'TV', 'Televisiva'), (2, 'TV Music', 'Televisiva'), (3, 'Radio L.', 'Radiofonica Locale');");
  }


  // check tb_default_date
  $table = "tb_default_date";
  if ($check = mysqli_query($db, "SELECT * FROM " . $table . " LIMIT 1")) {

    checkDBField($db, "description", $table, " varchar(255) DEFAULT NULL ");
  } else {

    mysqli_query($db, "CREATE TABLE  `tb_default_date` (
        `id` int(11) NOT NULL AUTO_INCREMENT,
        `description` varchar(255) DEFAULT NULL,
        PRIMARY KEY (`id`)
      ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;");

    // dump
    mysqli_query($db, "INSERT INTO `tb_default_date` (`id`, `description`) VALUES
    			(1, 'Mese precedente'), (2, 'Giorno precedente');");
  }


  // CREO LE TABELLE PER IL PREFISSO RICHIESTO.


  $ch_type = 0;
  $ch_name = $channel;
  // Fay 01-2021
  if (!empty($sharedfolder)) {
    $file = $sharedfolder . "/CONFIG/Channel_Configuration.xml";
    if (file_exists($file)) {
      echo "file exists";
      $xml  = simplexml_load_file($file);


      foreach ($xml->children() as $current) {
        $elem = $current->getname();
        if ($elem->getname() == "Row1") {
          foreach ($xml->$elem as $item) {
            if ($item->getname() == "Type") {
              $ch_type = $item;
              //break;
            }
            if ($item->getname() == "Name") {
              $ch_name = $item;
              //break;
            }
          }
        }
      }


      foreach ($xml->children() as $current) {
        $elem = $current->getname();
        if ($xml->$elem->getname() == "Row1") {
          foreach ($xml->$elem as $item) {
            if ($item->getname() == "Type") {
              $ch_type = $item;
              //break;
            }
            if ($item->getname() == "Name") {
              $ch_name = $item;
              //break;
            }
          }
        }
      }
    }
  }



  $table = $prefix . "_tb_channel";
  if ($check = mysqli_query($db, "SELECT * FROM " . $table . " LIMIT 1")) {

    checkDBField($db, "shortname", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "name", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "description", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "pathexport", $table, " varchar(2558) DEFAULT NULL ");
    checkDBField($db, "pathexport", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "pathconfig", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "pathfacebook", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "pathexportmsg", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "idmachine", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "page", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "listen", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "imglogo", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "type", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "mezzodif", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "emittente", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "ragsoc", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "structu", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "pathadv", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "nrarea", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "importnazionale", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "pathmdb", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "piva", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "licenza", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "hide", $table, " tinyint(2) DEFAULT  0");
    checkDBField($db, "importregistry", $table, " tinyint(2) DEFAULT  0");
    checkDBField($db, "exportadv", $table, " tinyint(2) DEFAULT  0");
    checkDBField($db, "exportregistry", $table, " tinyint(2) DEFAULT  0");
    checkDBField($db, "ignoreactive", $table, " tinyint(2) DEFAULT  0");
  } else {

    mysqli_query($db, "CREATE TABLE  `" . $table . "` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `shortname` varchar(255) DEFAULT NULL,
      `name` varchar(255) DEFAULT NULL,
      `description` varchar(255) DEFAULT NULL,
      `pathexport` varchar(2558) DEFAULT NULL,
      `pathconfig` varchar(255) DEFAULT NULL,
      `pathfacebook` varchar(255) DEFAULT NULL,
      `pathexportmsg` varchar(255) DEFAULT NULL,
      `idmachine` int(11) DEFAULT NULL,
      `page` varchar(255) DEFAULT NULL,
      `listen` varchar(255) DEFAULT NULL,
      `imglogo` varchar(255) DEFAULT NULL,
      `type` int(11) DEFAULT NULL,
      `mezzodif` varchar(255) DEFAULT NULL,
      `emittente` varchar(255) DEFAULT NULL,
      `ragsoc` varchar(255) DEFAULT NULL,
      `structu` int(11) DEFAULT NULL,
      `pathadv` varchar(255) DEFAULT NULL,
      `nrarea` int(11) DEFAULT NULL,
      `importnazionale` int(11) DEFAULT NULL,
      `pathmdb` varchar(255) DEFAULT NULL,
      `piva` varchar(255) DEFAULT NULL,
      `licenza` varchar(255) DEFAULT NULL,
      `hide` tinyint(2) DEFAULT  0,
      `importregistry` tinyint(2) DEFAULT  0,
      `exportadv` tinyint(2) DEFAULT  0,
      `exportregistry` tinyint(2) DEFAULT  0,
      `ignoreactive` tinyint(2) DEFAULT  0,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;");

    mysqli_query($db, "INSERT INTO `" . $table . "` (`id`, `shortname`, `name`, `description`, `pathexport`, `pathconfig`, `pathfacebook`, `pathexportmsg`, `type`, `structu`) VALUES
  	(1, '" . $channel . "', '" . $ch_name . "', '" . $ch_name . "', 'export', \"" . $sharedfolder . "\", 'fb', 'mm', " . $ch_type . ", 1);");
  }


  $table = $prefix . "_channel_machine";
  if ($check = mysqli_query($db, "SELECT * FROM " . $table . " LIMIT 1")) {

    checkDBField($db, "idchannel", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "idmachine", $table, " varchar(255) DEFAULT NULL ");
  } else {

    mysqli_query($db, "CREATE TABLE  `" . $table . "` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `idchannel` int(11) DEFAULT NULL,
      `idmachine` int(11) DEFAULT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;") or die('Query failed: ' . mysqli_error($db));
  }



  $table = $prefix . "_user_channel";
  if ($check = mysqli_query($db, "SELECT * FROM " . $table . " LIMIT 1")) {

    checkDBField($db, "iduser", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "idchannel", $table, " int(11) DEFAULT NULL ");
  } else {

    mysqli_query($db, "CREATE TABLE  `" . $table . "` (
        `id` int(11) NOT NULL AUTO_INCREMENT,
        `iduser` int(11) DEFAULT NULL,
        `idchannel` int(11) DEFAULT NULL,
        PRIMARY KEY (`id`)
      ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;");
  }


  $table = $prefix . "_tb_users";
  if ($check = mysqli_query($db, "SELECT * FROM " . $table . " LIMIT 1")) {

    checkDBField($db, "name", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "lastname", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "userlevel", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "tb_width", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "tb_height", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "idchannel", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "genmsg", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "usr", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "pwd", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "language", $table, " varchar(20) DEFAULT NULL ");
    checkDBField($db,  "able_edit", $table, " int(11) NOT NULL DEFAULT '0' ");
  } else {
    mysqli_query($db, "CREATE TABLE  `" . $table . "` (
        `id` int(11) NOT NULL AUTO_INCREMENT,
        `name` varchar(255) DEFAULT NULL,
        `lastname` varchar(255) DEFAULT NULL,
        `userlevel` int(11) DEFAULT NULL,
        `tb_width` int(11) DEFAULT NULL,
        `tb_height` int(11) DEFAULT NULL,
        `idchannel` int(11) DEFAULT NULL,
        `genmsg` int(11) DEFAULT NULL,
        `usr` varchar(255) DEFAULT NULL,
        `pwd` varchar(255) DEFAULT NULL,
        `language` varchar(20) DEFAULT NULL,
        `able_edit` int(10) NOT NULL DEFAULT '0',
        PRIMARY KEY (`id`)
      ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;");


    mysqli_query($db, "INSERT INTO `" . $prefix . "_tb_users` (`id`, `name`, `lastname`, `userlevel`, `tb_width`, `tb_height`, `idchannel`, `genmsg`, `usr`, `pwd`,  `language`) VALUES
      (1, '.', '" . $usernameLogin . "', 1, 800, 600, 1, 0, '" . $usernameLogin . "', '" . sha1($passwordLogin) . "', 'english');");

    mysqli_query($db, "INSERT INTO `" . $prefix . "_user_channel` (`id`, `iduser`, `idchannel`) VALUES (1, 1, 1);");
  }

  // check _default_user
  $table = $prefix . "_default_user";
  if ($check = mysqli_query($db, "SELECT * FROM " . $table . " LIMIT 1")) {

    checkDBField($db, "iduser", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "field1", $table, " varchar(255) DEFAULT NULL COMMENT 'CH Message Manager' ");
    checkDBField($db, "field2", $table, " varchar(255) DEFAULT NULL COMMENT 'LIST Message Manager' ");
    checkDBField($db, "field3", $table, " varchar(255) DEFAULT NULL COMMENT 'CH FbMessage' ");
    checkDBField($db, "field4", $table, " varchar(255) DEFAULT NULL COMMENT 'LIST FbMessage' ");
    checkDBField($db, "field5", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "field6", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "field7", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "field8", $table, " varchar(255) DEFAULT NULL ");
  } else {

    mysqli_query($db, "CREATE TABLE  `" . $table . "` (
          `id` int(11) NOT NULL AUTO_INCREMENT,
          `iduser` int(11) NOT NULL,
          `field1` varchar(255) DEFAULT NULL COMMENT 'CH Message Manager',
          `field2` varchar(255) DEFAULT NULL COMMENT 'LIST Message Manager',
          `field3` varchar(255) DEFAULT NULL COMMENT 'CH FbMessage',
          `field4` varchar(255) DEFAULT NULL COMMENT 'LIST FbMessage',
          `field5` varchar(255) DEFAULT NULL,
          `field6` varchar(255) DEFAULT NULL,
          `field7` varchar(255) DEFAULT NULL,
          `field8` varchar(255) DEFAULT NULL,
          PRIMARY KEY (`id`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8;");
  }



  // check _default_print
  $table = $prefix . "_default_print";
  if ($check = mysqli_query($db, "SELECT * FROM " . $table . " LIMIT 1")) {

    checkDBField($db, "id_user", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "id_channel", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "orientation", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "margintop", $table, " int(11) NOT NULL ");
    checkDBField($db, "margintop_int", $table, " int(11) NOT NULL ");
    checkDBField($db, "intLinea", $table, " double DEFAULT NULL ");
    checkDBField($db, "intLinea_int", $table, " double DEFAULT NULL ");
    checkDBField($db, "breakpage", $table, " int(11) NOT NULL ");
    checkDBField($db, "containerlines", $table, " int(11) NOT NULL ");
    checkDBField($db, "editor", $table, " int(11) NOT NULL ");
    checkDBField($db, "totals", $table, " int(11) NOT NULL ");
    checkDBField($db, "block", $table, " int(11) NOT NULL ");
    checkDBField($db, "advsingle", $table, " int(11) NOT NULL ");
    checkDBField($db, "fontsize", $table, " int(11) NOT NULL ");
    checkDBField($db, "fontsize_int", $table, " int(11) NOT NULL ");
    checkDBField($db, "editornote", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "editorlength", $table, " int(11) NOT NULL ");
    checkDBField($db, "editorstart", $table, " int(11) NOT NULL ");
    checkDBField($db, "editorend", $table, " int(11) NOT NULL ");
    checkDBField($db, "defaultdate", $table, " int(11) NOT NULL ");
    checkDBField($db, "asktemplate", $table, " int(11) NOT NULL ");
    checkDBField($db, "groupdescription", $table, " int(11) NOT NULL ");
    checkDBField($db, "markfiller", $table, " int(11) NOT NULL ");
    checkDBField($db, "advexternal", $table, " int(11) NOT NULL ");
    checkDBField($db, "adv_origin", $table, " int(11) NOT NULL ");
    checkDBField($db, "date", $table, " timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ");
    checkDBField($db, "printNation", $table, " int(11) NOT NULL ");
    checkDBField($db, "printYear", $table, " int(11) NOT NULL ");
    checkDBField($db, "printDurata", $table, " int(11) NOT NULL ");
    checkDBField($db, "printCentroInt", $table, " int(11) NOT NULL ");
    checkDBField($db, "printDetTipologia", $table, " int(11) NOT NULL ");
    checkDBField($db, "width_data", $table, " int(11) NOT NULL ");
    checkDBField($db, "width_titolo", $table, " int(11) NOT NULL ");
    checkDBField($db, "width_inizio", $table, " int(11) NOT NULL ");
    checkDBField($db, "width_fine", $table, " int(11) NOT NULL ");
    checkDBField($db, "width_durata", $table, " int(11) NOT NULL ");
    checkDBField($db, "width_tipologia", $table, " int(11) NOT NULL ");
    checkDBField($db, "width_dettagli", $table, " int(11) NOT NULL ");
    checkDBField($db, "width_note", $table, " int(11) NOT NULL ");
    checkDBField($db, "width_nazionalita", $table, " int(11) NOT NULL ");
    checkDBField($db, "width_anno", $table, " int(11) NOT NULL ");
    checkDBField($db, "width_produzione", $table, " int(11) NOT NULL ");
    checkDBField($db, "pubper", $table, " int(11) DEFAULT '0' ");
    checkDBField($db, "timeinfo", $table, " int(11) DEFAULT '0' ");
    checkDBField($db, "timeinfo24", $table, " int(11) DEFAULT '0' ");
    checkDBField($db, "timeinfoloc", $table, " int(11) DEFAULT '0' ");
    checkDBField($db, "timeadv", $table, " int(11) DEFAULT '0' ");
    checkDBField($db, "explodeShow", $table, " int(11) DEFAULT '0' ");
    checkDBField($db,  "usecode", $table, " int(11) DEFAULT '0' ");
    checkDBField($db, "timeweek", $table, " int(11) DEFAULT '0' ");
    checkDBField($db, "timelocweek", $table, " int(11) DEFAULT '0' ");
    checkDBField($db, "registrystart", $table, " int(11) DEFAULT '0' ");
    checkDBField($db, "registryend", $table, " int(11) DEFAULT '86400000'");
  } else {

    mysqli_query($db, "CREATE TABLE  `" . $table . "` (
        `id` int(11) NOT NULL AUTO_INCREMENT,
        `id_user` int(11) NOT NULL,
        `id_channel` int(11) NOT NULL,
        `orientation` int(11) NOT NULL,
        `margintop` int(11) NOT NULL,
        `margintop_int` int(11) NOT NULL,
        `intLinea` double DEFAULT NULL,
        `intLinea_int` double DEFAULT NULL,
        `breakpage` int(11) NOT NULL,
        `containerlines` int(11) NOT NULL,
        `editor` int(11) NOT NULL,
        `totals` int(11) NOT NULL,
        `block` int(11) NOT NULL,
        `advsingle` int(11) NOT NULL,
        `fontsize` int(11) NOT NULL,
        `fontsize_int` int(11) NOT NULL,
        `editornote` varchar(255) DEFAULT NULL,
        `editorlength` int(11) NOT NULL,
        `editorstart` int(11) NOT NULL,
        `editorend` int(11) NOT NULL,
        `defaultdate` int(11) NOT NULL,
        `asktemplate` int(11) NOT NULL,
        `groupdescription` int(11) NOT NULL,
        `markfiller` int(11) NOT NULL,
        `advexternal` int(11) NOT NULL,
        `adv_origin` int(11) NOT NULL,
        `date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
        `printNation` int(11) NOT NULL,
        `printYear` int(11) NOT NULL,
        `printDurata` int(11) NOT NULL,
        `printCentroInt` int(11) NOT NULL,
        `printDetTipologia` int(11) NOT NULL,
        `width_data` int(11) NOT NULL,
        `width_titolo` int(11) NOT NULL,
        `width_inizio` int(11) NOT NULL,
        `width_fine` int(11) NOT NULL,
        `width_durata` int(11) NOT NULL,
        `width_tipologia` int(11) NOT NULL,
        `width_dettagli` int(11) NOT NULL,
        `width_note` int(11) NOT NULL,
        `width_nazionalita` int(11) NOT NULL,
        `width_anno` int(11) NOT NULL,
        `width_produzione` int(11) NOT NULL,
        `pubper` int(11) DEFAULT '0',
        `timeinfo` int(11) DEFAULT '0',
        `timeinfo24` int(11) DEFAULT '0',
        `timeinfoloc` int(11) DEFAULT '0',
        `timeadv` int(11) DEFAULT '0',
        `timeweek` int(11) DEFAULT '0',
        `timelocweek` int(11) DEFAULT '0',
        `explodeShow` int(11) DEFAULT '0',
        `usecode` int(11) DEFAULT '0',
        `registrystart` int(11) DEFAULT '0',
        `registryend` int(11) DEFAULT '86400000',
        PRIMARY KEY (`id`)
      ) ENGINE=InnoDB DEFAULT CHARSET=utf8;");
  }


  // check _default_regcreate
  $table = $prefix . "_default_regcreate";
  if ($check = mysqli_query($db, "SELECT * FROM " . $table . " LIMIT 1")) {

    checkDBField($db, "id_user", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "id_channel", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "monday", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "tuesday", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "wednesday", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "thursday", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "friday", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "saturday", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "sunday", $table, " int(11) DEFAULT NULL ");
  } else {

    mysqli_query($db, "CREATE TABLE `" . $table . "` (
            `id` int(11) NOT NULL AUTO_INCREMENT,
            `id_user` int(11) NOT NULL,
            `id_channel` int(11) NOT NULL,
            `monday` int(11) NOT NULL,
            `tuesday` int(11) NOT NULL,
            `wednesday` int(11) NOT NULL,
            `thursday` int(11) NOT NULL,
            `friday` int(11) NOT NULL,
            `saturday` int(11) NOT NULL,
            `sunday` int(11) NOT NULL,
            PRIMARY KEY (`id`)
          ) ENGINE=InnoDB DEFAULT CHARSET=utf8;");
  }



  // check _default_regcreate
  $table = $prefix . "_default_regcreate";
  if ($check = mysqli_query($db, "SELECT * FROM " . $table . " LIMIT 1")) {

    checkDBField($db, "id_user", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "id_channel", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "monday", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "tuesday", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "wednesday", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "thursday", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "friday", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "saturday", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "sunday", $table, " int(11) DEFAULT NULL ");
  } else {

    mysqli_query($db, "CREATE TABLE `" . $table . "` (
            `id` int(11) NOT NULL AUTO_INCREMENT,
            `id_user` int(11) NOT NULL,
            `id_channel` int(11) NOT NULL,
            `monday` int(11) NOT NULL,
            `tuesday` int(11) NOT NULL,
            `wednesday` int(11) NOT NULL,
            `thursday` int(11) NOT NULL,
            `friday` int(11) NOT NULL,
            `saturday` int(11) NOT NULL,
            `sunday` int(11) NOT NULL,
            PRIMARY KEY (`id`)
          ) ENGINE=InnoDB DEFAULT CHARSET=utf8;");
  }



  // check _origin
  $table = $prefix . "_origin";
  if ($check = mysqli_query($db, "SELECT * FROM " . $table . " LIMIT 1")) {

    checkDBField($db, "Description", $table, " varchar(100) DEFAULT '' ");
  } else {

    mysqli_query($db, "CREATE TABLE `" . $table . "` (
            `ID` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
            `Description` varchar(100) DEFAULT '',
            PRIMARY KEY (`ID`)
          ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;");


    // dump
    mysqli_query($db, "INSERT INTO `" . $prefix . "_origin` (`ID`,`Description`) VALUES
        		 (1,'AUTOPRODOTTO'),
        		 (2,'ETEROPRODOTTO');");
  }



  // check _programdetails
  $table = $prefix . "_programdetails";
  if ($check = mysqli_query($db, "SELECT * FROM " . $table . " LIMIT 1")) {

    checkDBField($db, "Details", $table, " varchar(100) DEFAULT '' ");
    checkDBField($db, "Typology", $table, " varchar(100) DEFAULT '' ");
    checkDBField($db, "CodeDetails", $table, " varchar(100) DEFAULT '' ");
    checkDBField($db, "CodeTypology", $table, " varchar(10) DEFAULT '' ");
    checkDBField($db, "Note", $table, " text ");
    checkDBField($db,  "ColorView", $table, " varchar(45) DEFAULT NULL ");
    checkDBField($db,  "ColorGraph", $table, " varchar(45) DEFAULT NULL ");
  } else {

    mysqli_query($db, "CREATE TABLE `" . $table . "` (
            `ID` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
            `Details` varchar(100) DEFAULT '',
            `Typology` varchar(100) DEFAULT '',
            `CodeDetails` varchar(10) DEFAULT '',
            `CodeTypology` varchar(10) DEFAULT '',
            `Note` text,
            `ColorView` varchar(45) DEFAULT NULL,
            `ColorGraph` varchar(45) DEFAULT NULL,
            PRIMARY KEY (`ID`)
          ) ENGINE=InnoDB DEFAULT CHARSET=utf8;");


    // dump
    mysqli_query($db, "INSERT INTO `" . $prefix . "_programdetails` (`ID`,`Details`,`Typology`,`CodeDetails`,`CodeTypology`,`Note`) VALUES
        		 (1,'Radiogiornale','Notiziari','1a','1','Trasmissione a carattere informativo con programmazione quotidiana all\'interno di fasce orarie prestabilite'),
        		 (2,'Radiogiornale sportivo','Notiziari','1b','1','Trasmissione di informazione sportiva con programmazione quotidiana all\'interno di fasce orarie prestabilite'),
        		 (3,'Servizi teletext','Notiziari','1c','1',''),
        		 (4,'Telequiz','Giochi','2a','2','Trasmissioni di quiz in diretta o registrati, in studio e con concorrenti, caratterizzati dal succedersi di domande e risposte con vincite di premi non simbolici'),
        		 (5,'Giochi televisivi','Giochi','2b','2','Trasmissioni di giochi in studio con concorrenti o telespettaori che vi partecipano, con vincite di premi non simbolici o denaro'),
        		 (6,'','Talk Show','','3','Programmi con ospiti in studio (ed eventualmente anche pubblico) che dibattono argomenti vari con un intrattenitore che media tra i vari interventi per animare la conversazione'),
        		 (7,'','Manifestazioni sportive','','4','Manifestazioni (in diretta o in differita) a carattere sportivo (sport riconosciuti dal CONI)'),
        		 (8,'Pubblicit�','Pubblicit�','5a','5',''),
        		 (9,'Telepromozioni','Pubblicit�','5b','5',''),
        		 (10,'Sponsorizzazioni','Pubblicit�','5c','5',''),
        		 (11,'','Televendite','','6',''),
        		 (12,'Film cinematografici','Film','7a','7','Produzioni filmiche destinate principalmente al circuito cinematografico e prodotte su pellicola'),
        		 (13,'Film TV','Film','7b','7','Produzioni filmiche su supporto magnetico, di durata massima di 200 minuti, eccezionalmente composte di due episodi'),
        		 (14,'Miniserie - sceneggiato','Fiction','8a','8','Fiction di produzione italiana che contenga un numero minimo di 5 puntate. Le puntate di circa 60 minuti hanno il finale aperto che si chiude con l\'ultima puntata'),
        		 (15,'Telefilm','Fiction','8b','8','Serie costituita da episodi che non superano mai i 60 minuti che propongono storie autonome (con finale chiuso). La continuit� narrativa � assicurata dalla presenza di personaggi fissi, da una ambientazione che raramente varia e da caratteri strutturali comuni'),
        		 (16,'Situation comedies','Fiction','8c','8','Serie costituita da episodi 30 minuti con finale solitamente chiuso. Girate solitamente in interni, mettono in scena vicende soprattutto familiari con un impronta comico-grottesca'),
        		 (17,'Soap operas - telenovelas','Fiction','8d','8','Serial in puntate da 20 a 35 minuti con finale aperto'),
        		 (18,'Comiche d\'epoca','Fiction','8e','8','Genere usato per i film comici d\'epoca'),
        		 (19,'Storia - geografia','Documentari','9a','9','Trasmissioni il cui scopo � documentare con filmati ed immagini la realt� storico-geografica'),
        		 (20,'Scienza','Documentari','9b','9','Trasmissioni il cui scopo � documentare con filmati ed immagini la realt� animale, vegetale, etc.)'),
        		 (21,'Informazione parlamentare','Programmi informativi / approfondimento','10a','10','Telegiornale informativo con collocazione periodica (quotidiana o settimanale) su temi che attengono quasi esclusivamente alla politica o il parlamento'),
        		 (22,'Dichiarazioni parlamentari','Programmi informativi / approfondimento','10b','10','Riprese in diretta di dibattiti in Parlamento, dichiarazioni del Pres. Del Consiglio, della repubblica, etc'),
        		 (23,'Inchieste','Programmi informativi / approfondimento','10c','10','Programma giornalistico di approfondimento (spesso anche con filmati) solitamente su singole tematiche.'),
        		 (24,'Rubriche di approfondimento delle testate giornalistiche','Programmi informativi / approfondimento','10d','10','Programmi di approfondimento su tematiche di attualit�. Supplementi informativi alle edizioni dei TG a cura delle testate giornalistiche'),
        		 (25,'Costume e societ�','Programmi informativi / approfondimento','10e','10','Trasmissioni che documentano usi, costumi, tradizioni, viaggi, curiosit�, della societ� moderna. Programmi che trattano del profilo e della vita di personaggi celebri scomparsi'),
        		 (26,'Rubriche religiose','Programmi informativi / approfondimento','10f','10','Programmi a carattere religioso, di qualunque \"credo\", registrati in studio'),
        		 (27,'Dibattiti','Programmi informativi / approfondimento','10g','10','Programmi che prevedono un dibattito in studio o fuori studio per l\'approfondimento di temi solitamente di attualit� sociale o politica. Possono essere legati alla trasmissione di un film che li precede o li segue'),
        		 (28,'Rubriche di approfondimento sportivo','Programmi informativi / approfondimento','10h','10','Trasmissioni di approfondimento sportivo a programmazione periodica. Possono essere anche monografie di personaggi o episodi sportivi o fungere da contenitore di manifestazioni sportive'),
        		 (29,'Teledidattica','Programmi informativi / approfondimento','10i','10','Programmi puramente didattico-informativi. Programmi generalmente caratterizzati dal logo \"DSE\", \"Video Sapere\" e \"RAI Educational\"'),
        		 (30,'Approfondimento culturale','Programmi informativi / approfondimento','10j','10','Programmi, anche con eventuali dibattiti, a carattere culturale su temi di storia, geografia, scienza, ambiente, letteratura, arte, etc.'),
        		 (31,'Concerti','Programmi culturali con parti autonome','11a','11','Programma il cui contenuto coincide con la messa in onda di concerti di musica leggera o sinfonici'),
        		 (32,'Balletti','Programmi culturali con parti autonome','11b','11','Rappresentazione di uno spettacolo di danza classica'),
        		 (33,'Lirica','Programmi culturali con parti autonome','11c','11','Trasmissione il cui contenuto prevede l\'esecuzione di \"Opere liriche\"'),
        		 (34,'Prosa','Programmi culturali con parti autonome','11d','11','Rappresentazione di spettacoli di prosa teatrale o televisiva'),
        		 (35,'','Cartoni animati per bambini','','12','Programma di animazione della durata massima di 60 min. destinato ad un pubblico infantile'),
        		 (36,'Programmi musicali','Intrattenimento','13a','13','Programmi girati in studio che si occupano del panorama della musica leggera: clip musicali, classifiche, retrospettive. Possono fungere da contenitore di concerti'),
        		 (37,'Reality show','Intrattenimento','13b','13','Programmi basati sulla trasmissione di riprese effettuate dal vivo ed in diretta, aventi come target esclusivo la riproduzione televisiva di scene di vita reale o comunque di attivit� non preordinate svolte da parte di una o pi� persone all\'interno di uno studio televisivo o un ambiente predefinito'),
        		 (38,'Programmi di montaggio','Intrattenimento','13c','13','Programmi basati sull\'accostamento di immagini registrate, montate secondo una specifica linea interpretativa'),
        		 (39,'Variet�','Intrattenimento','13d','13','Trasmissioni di intrattenimento leggero. Le componenti che caratterizzano questo prodotto sono: un\'impostazione di derivazione teatrale, una scenografia ad effetto, la presenza di balletti, di canzoni e di sketch nonch� di uno o pi� conduttori'),
        		 (40,'Astrologia - cartomanzia','Intrattenimento','13e','13','Programmi girati in studio e caratterizzati dalla presenza di un astrologo o cartomante, in genere in contatto telefonico con i telespettatori'),
        		 (41,'Programma contenitore radiofonico','Intrattenimento','13f','13',''),
        		 (42,'Cartoni animati per adulti','Intrattenimento','13g','13','Programma di animazione della durata massima di 60 min. destinato ad un pubblico adulto'),
        		 (43,'Trasmissioni per bambini','Intrattenimento','13h','13','Trasmissioni destinate ad un pubblico infantile, condotte in studio o in esterno con o senza la partecipazione di bambini. Possono contenere giochi o quiz e spesso cartoni animati'),
        		 (44,'Anteprima','Attualit�','14a','14','Programmi che hanno lo scopo di dare informazione o promuovere l\'imminente programmazione cinematografica'),
        		 (45,'Promo','Attualit�','14b','14','Auto-promozione di eventi che saranno trasmessi sulla stessa rete o su altre reti dello stesso gruppo'),
        		 (46,'Rotocalchi','Attualit�','14c','14','Trasmissioni \"informative\" a carattere di cronaca rosa e di curiosit� varie'),
        		 (47,'Meteo','Attualit�','14d','14','Programma di previsioni metereologiche'),
        		 (48,'Lotterie','Attualit�','14e','14','Estrazioni del Lotto'),
        		 (49,'Rubriche di servizio','Attualit�','14f','14','Trasmissioni non condotte in studio che offrono informazioni su: modalit� per il voto; viabilit� e bollettini sul traffico; numeri telefonici utili'),
        		 (50,'Trasmissioni di servizio','Attualit�','14g','14','Programmi condotti in studio con lo scopo di offrire un servizio socio-informativo'),
        		 (51,'Inaugurazioni','Attualit�','14h','14','Trasmissioni, generalmente in diretta, che documentano inaugurazioni'),
        		 (52,'Premiazioni','Attualit�','14i','14','Trasmissioni, generalmente in diretta, che documentano premi letterari e premiazioni'),
        		 (53,'Manifestazioni di piazza','Attualit�','14j','14','Trasmissioni, generalmente in diretta, che documentano manifestazioni di piazza'),
        		 (54,'Santa Messa','Eventi religiosi','15a','15','Trasmissioni, generalmente domenicali ed in diretta, che seguono la Santa Messa'),
        		 (55,'Eventi religiosi','Eventi religiosi','15b','15','Trasmissioni, generalmente in diretta, che documentano manifestazioni religiose'),
        		 (56,'Annunci','Programmi accessori','16a','16','Programmi aventi carattere accessorio rispetto al palinsesto'),
        		 (57,'Sigle','Programmi accessori','16b','16','Programmi aventi carattere accessorio rispetto al palinsesto'),
        		 (58,'Intervalli','Programmi accessori','16c','16','Programmi aventi carattere accessorio rispetto al palinsesto'),
        		 (59,'Segnale orario','Programmi accessori','16d','16','Programmi aventi carattere accessorio rispetto al palinsesto'),
        		 (60,'','Messaggi politici autogestiti gratuiti','','17','Messaggi politici autogestiti a titolo gratuito ai sensi dell\'art. 3 della legge 22 Febbraio 2000 n. 28'),
        		 (61,'','Messaggi politici autogestiti a pagamento','','18','Messaggi politici autogestiti a pagamento ai sensi dell\'art. 3 della legge 22 Febbraio 2000 n. 28'),
        		 (62,'','Comunicazione politica','','19','Programmi di comunicazione politica ai sensi dell\'art. 2 della legge 22 Febbraio 2000 n. 28'),
        		 (63,'','Immagini fisse o ripetitive','','20','');");
  }


  // check list_comuni
  $table = "list_comuni";
  if ($check = mysqli_query($db, "SELECT * FROM " . $table . " LIMIT 1")) {

    checkDBField($db, "code_istat", $table, " mediumint(6) unsigned NOT NULL DEFAULT '0' ");
    checkDBField($db, "code_istat_province", $table, " smallint(3) unsigned NOT NULL DEFAULT '0' ");
    checkDBField($db, "code_istat_city", $table, " smallint(3) unsigned NOT NULL DEFAULT '0' ");
    checkDBField($db, "code_land", $table, " char(4) NOT NULL DEFAULT '' ");
    checkDBField($db, "code_postal", $table, " mediumint(5) unsigned NOT NULL DEFAULT '0' ");
    checkDBField($db, "comune", $table, " varchar(255) NOT NULL DEFAULT '' ");
    checkDBField($db, "province_code", $table, "enum('AG','AL','AN','AO','AP','AQ','AR','AT','AV','BA','BG','BI','BL','BN','BO','BR','BS','BT','BZ','CA','CB','CE','CH','CI','CL','CN','CO','CR','CS','CT','CZ','EE','EN','FC','FE','FG','FI','FM','FR','GE','GO','GR','IM','IS','KR','LC','LE','LI','LO','LT','LU','MB','MC','ME','MI','MN','MO','MS','MT','NA','NO','NU','OG','OR','OT','PA','PC','PD','PE','PG','PI','PN','PO','PR','PT','PU','PV','PZ','RA','RC','RE','RG','RI','RM','RN','RO','SA','SI','SO','SP','SR','SS','SV','TA','TE','TN','TO','TP','TR','TS','TV','UD','Unknown','VA','VB','VC','VE','VI','VR','VS','VT','VV') NOT NULL DEFAULT 'Unknown' ");
    checkDBField($db, "province_name", $table, " varchar(255) NOT NULL DEFAULT '' ");
    checkDBField($db, "region", $table, " enum('Abruzzo','Africa','America Centrale','America Meridionale','America Settentrionale','Asia','Basilicata','Calabria','Campania','Emilia-Romagna','Europa','Friuli-Venezia Giulia','Lazio','Liguria','Lombardia','Marche','Molise','Oceania','Piemonte','Puglia','Sardegna','Sicilia','Terre Polari Antartiche','Terre Polari Artiche','Toscana','Trentino-Alto Adige','Umbria','Unknown','Valle d''Aosta/Vall?e d''Aoste','Veneto') NOT NULL DEFAULT 'Unknown' ");
    checkDBField($db, "telephone_prefix", $table, " varchar(10) NOT NULL DEFAULT '' ");
    checkDBField($db, "website", $table, " varchar(255) NOT NULL DEFAULT '' ");
    checkDBField($db, "deleted", $table, " enum('Yes','No') NOT NULL DEFAULT 'No' ");
    checkDBField($db, "moved", $table, " enum('Yes','No') NOT NULL DEFAULT 'No' ");
    checkDBField($db, "moved_code_istat", $table, " mediumint(8) unsigned NOT NULL DEFAULT '0' ");
    checkDBField($db, "notes", $table, " text NOT NULL ");
  } else {

    mysqli_query($db, "CREATE TABLE `list_comuni` (
          `id` int(11) NOT NULL AUTO_INCREMENT,
          `code_istat` mediumint(6) unsigned NOT NULL DEFAULT '0',
          `code_istat_province` smallint(3) unsigned NOT NULL DEFAULT '0',
          `code_istat_city` smallint(3) unsigned NOT NULL DEFAULT '0',
          `code_land` char(4) NOT NULL DEFAULT '',
          `code_postal` mediumint(5) unsigned NOT NULL DEFAULT '0',
          `comune` varchar(255) NOT NULL DEFAULT '',
          `province_code` enum('AG','AL','AN','AO','AP','AQ','AR','AT','AV','BA','BG','BI','BL','BN','BO','BR','BS','BT','BZ','CA','CB','CE','CH','CI','CL','CN','CO','CR','CS','CT','CZ','EE','EN','FC','FE','FG','FI','FM','FR','GE','GO','GR','IM','IS','KR','LC','LE','LI','LO','LT','LU','MB','MC','ME','MI','MN','MO','MS','MT','NA','NO','NU','OG','OR','OT','PA','PC','PD','PE','PG','PI','PN','PO','PR','PT','PU','PV','PZ','RA','RC','RE','RG','RI','RM','RN','RO','SA','SI','SO','SP','SR','SS','SV','TA','TE','TN','TO','TP','TR','TS','TV','UD','Unknown','VA','VB','VC','VE','VI','VR','VS','VT','VV') NOT NULL DEFAULT 'Unknown',
          `province_name` varchar(255) NOT NULL DEFAULT '',
          `region` enum('Abruzzo','Africa','America Centrale','America Meridionale','America Settentrionale','Asia','Basilicata','Calabria','Campania','Emilia-Romagna','Europa','Friuli-Venezia Giulia','Lazio','Liguria','Lombardia','Marche','Molise','Oceania','Piemonte','Puglia','Sardegna','Sicilia','Terre Polari Antartiche','Terre Polari Artiche','Toscana','Trentino-Alto Adige','Umbria','Unknown','Valle d''Aosta/Vall?e d''Aoste','Veneto') NOT NULL DEFAULT 'Unknown',
          `telephone_prefix` varchar(10) NOT NULL DEFAULT '',
          `website` varchar(255) NOT NULL DEFAULT '',
          `deleted` enum('Yes','No') NOT NULL DEFAULT 'No',
          `moved` enum('Yes','No') NOT NULL DEFAULT 'No',
          `moved_code_istat` mediumint(8) unsigned NOT NULL DEFAULT '0',
          `notes` text NOT NULL,
          PRIMARY KEY (`id`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8;");

    // dump COMUNI
    mysqli_query($db, getLargeDumps('comuni', $db)); // richiamo funzione da large dumps di dumps.php per la query di insert dei comuni

  }



  // check list_country
  $table = "list_country";
  if ($check = mysqli_query($db, "SELECT * FROM " . $table . " LIMIT 1")) {

    checkDBField($db, "abb", $table, " varchar(255) CHARACTER SET ucs2 COLLATE ucs2_unicode_ci NOT NULL ");
    checkDBField($db, "country", $table, " varchar(255) CHARACTER SET ucs2 COLLATE ucs2_unicode_ci DEFAULT NULL ");
  } else {

    mysqli_query($db, "CREATE TABLE `list_country` (
          `id` int(11) NOT NULL AUTO_INCREMENT,
          `abb` varchar(255) CHARACTER SET ucs2 COLLATE ucs2_unicode_ci NOT NULL,
          `country` varchar(255) CHARACTER SET ucs2 COLLATE ucs2_unicode_ci DEFAULT NULL,
          PRIMARY KEY (`id`)
        ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;");

    // dump COMUNI
    mysqli_query($db, getLargeDumps('nazioni', $db)); // richiamo funzione da large dumps di dumps.php per la query di insert dei comuni

  }


  // check tb_typology
  $table = "tb_typology";
  if ($check = mysqli_query($db, "SELECT * FROM " . $table . " LIMIT 1")) {

    checkDBField($db, "typology", $table, " varchar(100) CHARACTER SET ucs2 COLLATE ucs2_unicode_ci DEFAULT NULL ");
    checkDBField($db, "description", $table, " varchar(100) CHARACTER SET ucs2 COLLATE ucs2_unicode_ci DEFAULT NULL ");
    checkDBField($db, "CodeDetails", $table, " varchar(10) CHARACTER SET ucs2 COLLATE ucs2_unicode_ci DEFAULT '' ");
    checkDBField($db, "CodeTypology", $table, " varchar(10) CHARACTER SET ucs2 COLLATE ucs2_unicode_ci DEFAULT '' ");
    checkDBField($db, "Note", $table, " text CHARACTER SET ucs2 COLLATE ucs2_unicode_ci ");
  } else {

    mysqli_query($db, "CREATE TABLE `tb_typology` (
          `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
          `typology` varchar(100) CHARACTER SET ucs2 COLLATE ucs2_unicode_ci DEFAULT NULL,
          `description` varchar(100) CHARACTER SET ucs2 COLLATE ucs2_unicode_ci DEFAULT NULL,
          `CodeDetails` varchar(10) CHARACTER SET ucs2 COLLATE ucs2_unicode_ci DEFAULT '',
          `CodeTypology` varchar(10) CHARACTER SET ucs2 COLLATE ucs2_unicode_ci DEFAULT '',
          `Note` text CHARACTER SET ucs2 COLLATE ucs2_unicode_ci,
          PRIMARY KEY (`id`) USING BTREE
        ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;");

    // dump COMUNI
    mysqli_query($db, getLargeDumps('tipologia', $db)); // richiamo funzione da large dumps di dumps.php per la query di insert dei comuni

  }



  // FINE CREAZIONE TABELLE PER WEBAPP / ACCOUNT / USERS



  /////////////////////////////////////////////
  ///////     INSTALLAZIONE GARANTE     ///////
  /////////////////////////////////////////////

  // ************ CONFIG UTENTI E AMMINISTRATORI PER ABILITAZIONE A GARANTE

  // check _tb_container
  $table = $prefix . "_tb_container";
  if ($check = mysqli_query($db, "SELECT * FROM " . $table . " LIMIT 1")) {

    checkDBField($db, "description", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "id_origin", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "id_typology", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "code_typology", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "newsper", $table, " double(5,2) DEFAULT NULL ");
    checkDBField($db, "pubper", $table, " double(5,2) DEFAULT NULL ");
    checkDBField($db, "newsreg", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "cdefault", $table, " int(11) NOT NULL ");
    checkDBField($db, "priority", $table, " int(11) NOT NULL ");
    checkDBField($db, "codetype", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "duration", $table, " int(11) DEFAULT '0' ");
  } else {

    mysqli_query($db, "CREATE TABLE `" . $table . "` (
            `id` int(11) NOT NULL AUTO_INCREMENT,
            `description` varchar(255) DEFAULT NULL,
            `id_origin` int(11) DEFAULT NULL,
            `id_typology` int(11) DEFAULT NULL,
            `code_typology` varchar(255) DEFAULT NULL,
            `newsper` double(5,2) DEFAULT NULL,
            `pubper` double(5,2) DEFAULT NULL,
            `newsreg` int(11) DEFAULT NULL,
            `cdefault` int(11) NOT NULL,
            `priority` int(11) NOT NULL,
            `codetype` varchar(255) DEFAULT NULL,
            `duration` int(10)  DEFAULT '0',
            PRIMARY KEY (`id`)
          ) ENGINE=InnoDB DEFAULT CHARSET=utf8;");
  }



  // check _tb_containerstart
  $table = $prefix . "_tb_containerstart";
  if ($check = mysqli_query($db, "SELECT * FROM " . $table . " LIMIT 1")) {

    checkDBField($db, "description", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "id_container", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "id_controltype", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "time", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "description", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "startstring", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "startnext", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "id_searchtype", $table, " int(11) NOT NULL ");
  } else {

    mysqli_query($db, "CREATE TABLE `" . $table . "` (
            `id` int(11) NOT NULL AUTO_INCREMENT,
            `id_container` int(11) DEFAULT NULL,
            `id_controltype` int(11) DEFAULT NULL,
            `time` int(11) DEFAULT NULL,
            `description` varchar(255) DEFAULT NULL,
            `startstring` int(11) DEFAULT NULL,
            `startnext` int(11) DEFAULT NULL,
            `id_searchtype` int(11) DEFAULT NULL,
            PRIMARY KEY (`id`)
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8;");
  }



  // check _tb_containerend
  $table = $prefix . "_tb_containerend";
  if ($check = mysqli_query($db, "SELECT * FROM " . $table . " LIMIT 1")) {

    checkDBField($db, "description", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "id_container", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "id_controltype", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "time", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "description", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "startstring", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "endprev", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "id_searchtype", $table, " int(11) NOT NULL ");
  } else {

    mysqli_query($db, "CREATE TABLE `" . $table . "` (
                `id` int(11) NOT NULL AUTO_INCREMENT,
                `id_container` int(11) DEFAULT NULL,
                `id_controltype` int(11) DEFAULT NULL,
                `time` int(11) DEFAULT NULL,
                `description` varchar(255) DEFAULT NULL,
                `startstring` int(11) DEFAULT NULL,
                `endprev` int(11) DEFAULT NULL,
                `id_searchtype` int(11) DEFAULT '0',
                PRIMARY KEY (`id`)
              ) ENGINE=InnoDB DEFAULT CHARSET=utf8;");
  }



  // check _tb_profile
  $table = $prefix . "_tb_profile";
  if ($check = mysqli_query($db, "SELECT * FROM " . $table . " LIMIT 1")) {

    checkDBField($db, "name", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "active", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "date", $table, " timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ");
    /*checkDBField($db , "time" , $table , " int(11) DEFAULT NULL ");*/
    checkDBField($db, "id_channel", $table, " int(11) DEFAULT '0' ");
  } else {

    mysqli_query($db, "CREATE TABLE `" . $table . "` (
                `id` int(11) NOT NULL AUTO_INCREMENT,
                `name` varchar(255) DEFAULT NULL,
                `active` int(11) DEFAULT NULL,
                `date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
                `id_channel` int(11) DEFAULT '0',
                PRIMARY KEY (`id`)
              ) ENGINE=InnoDB DEFAULT CHARSET=utf8;");
  }


  // check _tb_profilecontainer
  $table = $prefix . "_tb_profilecontainer";
  if ($check = mysqli_query($db, "SELECT * FROM " . $table . " LIMIT 1")) {

    checkDBField($db, "id_profile", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "id_container", $table, " int(11) DEFAULT NULL ");
  } else {

    mysqli_query($db, "CREATE TABLE `" . $table . "` (
                  `id` int(11) NOT NULL AUTO_INCREMENT,
                  `id_profile` int(11) DEFAULT NULL,
                  `id_container` int(11) DEFAULT NULL,
                  PRIMARY KEY (`id`)
                ) ENGINE=InnoDB DEFAULT CHARSET=utf8;");
  }



  // check _tmp_crea
  $table = $prefix . "_tmp_crea";
  if ($check = mysqli_query($db, "SELECT * FROM " . $table . " LIMIT 1")) {

    checkDBField($db, "id_user", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "date", $table, "  date DEFAULT NULL ");
    checkDBField($db, "id_container", $table, "  int(11) NOT NULL ");
    checkDBField($db, "description", $table, "  varchar(255) DEFAULT NULL ");
    checkDBField($db, "id_from", $table, "  int(11) NOT NULL ");
    checkDBField($db, "id_to", $table, "  int(11) NOT NULL ");
    checkDBField($db, "starttime", $table, "  int(11) NOT NULL ");
    checkDBField($db, "endtime", $table, "  int(11) NOT NULL ");
    checkDBField($db, "length", $table, "  int(11) DEFAULT NULL ");
    checkDBField($db, "id_origin", $table, "  int(11) NOT NULL ");
    checkDBField($db, "id_typology", $table, "  int(11) NOT NULL ");
    checkDBField($db, "code_typology", $table, "  varchar(255) DEFAULT NULL ");
    checkDBField($db, "newsper", $table, " double(5,2) DEFAULT NULL ");
    checkDBField($db, "pubper", $table, " double(5,2) DEFAULT NULL ");
    checkDBField($db, "newsreg", $table, " double(5,2) DEFAULT NULL ");
  } else {

    mysqli_query($db, "CREATE TABLE `" . $table . "` (
                      `id` int(11) NOT NULL AUTO_INCREMENT,
                      `id_user` int(11) NOT NULL,
                      `date` date DEFAULT NULL,
                      `id_container` int(11) NOT NULL,
                      `description` varchar(255) DEFAULT NULL,
                      `id_from` int(11) NOT NULL,
                      `id_to` int(11) NOT NULL,
                      `starttime` int(11) NOT NULL,
                      `endtime` int(11) NOT NULL,
                      `length` int(11) DEFAULT NULL,
                      `id_origin` int(11) NOT NULL,
                      `id_typology` int(11) NOT NULL,
                      `code_typology` varchar(255) DEFAULT NULL,
                      `newsper` double(5,2) DEFAULT NULL,
                      `pubper` double(5,2) DEFAULT NULL,
                      `newsreg` double(5,2) DEFAULT NULL,
                      PRIMARY KEY (`id`)
                    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;");
  }


  // check _tmp_display
  $table = $prefix . "_tmp_display";
  if ($check = mysqli_query($db, "SELECT * FROM " . $table . " LIMIT 1")) {

    checkDBField($db, "date", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "description", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "starttime", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "endtime", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "duration", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "position", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "id_origin", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "desc_origin", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "id_typology", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "code_typology", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "newsper", $table, " double(5,2) DEFAULT NULL ");
    checkDBField($db, "container", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "national", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "yearprod", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "newsreg", $table, " double(5,2) DEFAULT NULL ");
    checkDBField($db, "newsreg", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "pubper", $table, " double(5,2) DEFAULT NULL ");
    checkDBField($db, "note", $table, " longtext ");
    checkDBField($db, "idele", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "id_user", $table, " int(11) DEFAULT NULL ");
  } else {

    mysqli_query($db, "CREATE TABLE `" . $table . "` (
                        `id` int(11) NOT NULL AUTO_INCREMENT,
                        `date` date DEFAULT NULL,
                        `description` varchar(255) DEFAULT NULL,
                        `starttime` int(11) DEFAULT NULL,
                        `endtime` int(11) DEFAULT NULL,
                        `duration` int(11) DEFAULT NULL,
                        `position` int(11) DEFAULT NULL,
                        `id_origin` int(11) DEFAULT NULL,
                        `desc_origin` varchar(255) DEFAULT NULL,
                        `id_typology` int(11) DEFAULT NULL,
                        `code_typology` varchar(255) DEFAULT NULL,
                        `newsper` double(5,2) DEFAULT NULL,
                        `container` varchar(255) DEFAULT NULL,
                        `national` int(11) DEFAULT NULL,
                        `yearprod` int(11) DEFAULT NULL,
                        `newsreg` int(11) DEFAULT NULL,
                        `pubper` double(5,2) DEFAULT NULL,
                        `note` longtext,
                        `idele` int(11) DEFAULT NULL,
                        `id_user` int(11) DEFAULT NULL,
                        PRIMARY KEY (`id`)
                      ) ENGINE=InnoDB DEFAULT CHARSET=utf8;");
  }





  // check _tmp_hystoric
  $table = $prefix . "_tmp_hystoric";
  if ($check = mysqli_query($db, "SELECT * FROM " . $table . " LIMIT 1")) {

    checkDBField($db, "idele", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "tbname", $table, " VARCHAR(255) DEFAULT NULL ");
    checkDBField($db, "date", $table, " date DEFAULT NULL ");
    checkDBField($db, "title", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "artist", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "type", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "length", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "onairplayedday", $table, " date DEFAULT NULL ");
    checkDBField($db, "onairplayedhours", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "aux", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "description", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "id_typology", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "id_origin", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "desc_origin", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "national", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "yearprod", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "newsreg", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "pubper", $table, " double(5,2) DEFAULT NULL ");
    checkDBField($db, "trademark", $table, " varchar(255) DEFAULT NULL");
    checkDBField($db, "codeisrc", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "note", $table, " longtext ");
    checkDBField($db, "id_user", $table, " int(11) NOT NULL ");
  } else {

    mysqli_query($db, "CREATE TABLE `" . $table . "` (
                        `id` int(11) NOT NULL AUTO_INCREMENT,
                        `idele` int(11) DEFAULT NULL,
                        `tbname` varchar(255) DEFAULT NULL,
                        `date` date DEFAULT NULL,
                        `title` varchar(255) DEFAULT NULL,
                        `artist` varchar(255) DEFAULT NULL,
                        `type` int(11) DEFAULT NULL,
                        `length` int(11) DEFAULT NULL,
                        `onairplayedday` date DEFAULT NULL,
                        `onairplayedhours` int(11) DEFAULT NULL,
                        `aux` varchar(255) DEFAULT NULL,
                        `description` varchar(255) DEFAULT NULL,
                        `id_typology` int(11) DEFAULT NULL,
                        `code_typology` varchar(255) DEFAULT NULL,
                        `id_origin` int(11) DEFAULT NULL,
                        `desc_origin` varchar(255) DEFAULT NULL,
                        `national` int(11) DEFAULT NULL,
                        `yearprod` int(11) DEFAULT NULL,
                        `newsreg` int(11) DEFAULT NULL,
                        `pubper` double(5,2) DEFAULT NULL,
                        `trademark` varchar(255) DEFAULT NULL,
                        `codeisrc` varchar(255) DEFAULT NULL,
                        `note` longtext,
                        `id_user` int(11) NOT NULL,
                        PRIMARY KEY (`id`)
                      ) ENGINE=InnoDB DEFAULT CHARSET=utf8;");
  }



  // check _tb_files
  $table = $prefix . "_tb_files";
  if ($check = mysqli_query($db, "SELECT * FROM " . $table . " LIMIT 1")) {

    checkDBField($db, "dateday", $table, " date DEFAULT NULL ");
    checkDBField($db, "datefile", $table, " date DEFAULT NULL ");
    checkDBField($db, "timefile", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "sizefile", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "typeimport", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "id_channel", $table, "int(11) DEFAULT NULL ");
  } else {

    mysqli_query($db, "CREATE TABLE `" . $table . "` (
                            `id` int(11) NOT NULL AUTO_INCREMENT,
                            `dateday` date DEFAULT NULL,
                            `datefile` date DEFAULT NULL,
                            `timefile` varchar(255) DEFAULT NULL,
                            `sizefile` int(11) DEFAULT NULL,
                            `typeimport` varchar(255) DEFAULT NULL,
                            `id_channel` int(11) DEFAULT NULL,
                            PRIMARY KEY (`id`)
                          ) ENGINE=InnoDB DEFAULT CHARSET=utf8;");
  }


  // check _tb_files_typology
  $table = $prefix . "_tb_files_typology";
  if ($check = mysqli_query($db, "SELECT * FROM " . $table . " LIMIT 1")) {

    checkDBField($db, "dateday", $table, " date DEFAULT NULL ");
    checkDBField($db, "datefile", $table, " date DEFAULT NULL ");
    checkDBField($db, "timefile", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "sizefile", $table, " int(11) DEFAULT NULL ");
  } else {

    mysqli_query($db, "CREATE TABLE `" . $table . "` (
                            `id` int(11) NOT NULL AUTO_INCREMENT,
                            `dateday` date DEFAULT NULL,
                            `datefile` date DEFAULT NULL,
                            `timefile` varchar(255) DEFAULT NULL,
                            `sizefile` int(11) DEFAULT NULL,
                            PRIMARY KEY (`id`)
                            ) ENGINE=InnoDB DEFAULT CHARSET=utf8;");
  }

  // check _nationality
  $table = $prefix . "_nationality";
  if ($check = mysqli_query($db, "SELECT * FROM " . $table . " LIMIT 1")) {

    checkDBField($db, "Description", $table, " varchar(100) DEFAULT '' ");
  } else {

    mysqli_query($db, "CREATE TABLE `" . $table . "` (
                              `ID` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
                              `Description` varchar(100) DEFAULT '',
                              PRIMARY KEY (`ID`)
                            ) ENGINE=InnoDB DEFAULT CHARSET=utf8;");
  }




  ///////////////////// fine garante



  /////////////////////////////////////////////////////
  ///////     INSTALLAZIONE CONTENT MANAGER     ///////
  /////////////////////////////////////////////////////


  // abilito l'account
  $sql = "SELECT id, folder FROM tb_account WHERE prefix='" . $prefix . "'";
  $values = mysqli_query($db, $sql);
  if ($row = mysqli_fetch_array($values, MYSQLI_ASSOC)) {
    $idaccount = $row['id'];

    $stringWebapps = "INSERT INTO `tb_webappaccount` (`idwebapp`,`idaccount`) VALUES ";

    for ($i = 0; $i <= count($arrayApplications); $i++) {
      $id = $i + 1;
      $q = "SELECT COUNT(*) AS tot FROM tb_webappaccount WHERE idaccount=1 AND idwebapp=" . $id;
      //echo '1) '.$qp.'<br>';
      $vp = mysqli_query($db, $q);
      $rp = mysqli_fetch_array($vp, MYSQLI_ASSOC);

      if ($rp['tot'] == 0) $stringWebapps .= " ( '" . $id . "' , " . $idaccount . " ),";
    }

    mysqli_query($db, substr($stringWebapps, 0, (strlen($stringWebapps) - 1)));

    // abilito gli amministratori
    // Fey 09/2022 abilito le app agli amministrazione solo se è la prima installazione
    if (!file_exists('instld.wap')) {
      $sqla = "SELECT id FROM " . $prefix . "_tb_users WHERE userlevel=1";
      $valuesa = mysqli_query($db, $sqla);
      while ($rowa = mysqli_fetch_array($valuesa, MYSQLI_ASSOC)) {
        for ($i = 0; $i < count($arrayApplications); $i++) {
          $idwebapp = $i + 1;
          if ($idwebapp == 11 || $idwebapp == 12 || $idwebapp == 13) { // Report, Analysis, Rendicontazione
            $query = "INSERT INTO `tb_webappuser` (`idwebapp`, `idaccount`, `iduser`) VALUES (" . $idwebapp  . " , " . $idaccount . ", " . $rowa['id'] . ")";
            mysqli_query($db, $query);
          }
        }
      }
    }
  }


  // VERIFICO DI NUOVO LA CARTELLA SE è STATA CREATA O MENO.
  //if(!is_dir("../../public/".$folder)) mkdir("../../public/".$folder);


  // check _items
  $table = $prefix . "_items";
  if ($check = mysqli_query($db, "SELECT * FROM " . $table . " LIMIT 1")) {

    checkDBField($db, "name", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "pathtxt", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "namefile", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "maxlength", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "rows", $table, " int(11) DEFAULT NULL ");
  } else {

    mysqli_query($db, "CREATE TABLE `" . $table . "` (
              `id` int(11) NOT NULL AUTO_INCREMENT,
        		  `name` varchar(255) DEFAULT NULL,
        		  `pathtxt` varchar(255) DEFAULT NULL,
        		  `namefile` varchar(255) DEFAULT NULL,
        		  `maxlength` int(11) DEFAULT NULL,
        		  `rows` int(11) DEFAULT NULL,
        		  PRIMARY KEY (`id`)
        		) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;");
  }

  // check _rss
  $table = $prefix . "_rss";
  if ($check = mysqli_query($db, "SELECT * FROM " . $table . " LIMIT 1")) {

    checkDBField($db, "name", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "pathtxt", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "namefile", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "img", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "imgw", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "imgh", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "maxlength", $table, " int(11) DEFAULT NULL ");
  } else {

    mysqli_query($db, "CREATE TABLE `" . $table . "` (
           `id` int(11) NOT NULL AUTO_INCREMENT,
           `name` varchar(255) DEFAULT NULL,
           `pathtxt` varchar(255) DEFAULT NULL,
           `namefile` varchar(255) DEFAULT NULL,
           `img` varchar(255) DEFAULT NULL,
           `imgw` int(11) DEFAULT NULL,
           `imgh` int(11) DEFAULT NULL,
           `maxlength` int(11) DEFAULT NULL,
           PRIMARY KEY (`id`)
         ) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;");
  }

  // check _content
  $table = $prefix . "_content";
  if ($check = mysqli_query($db, "SELECT * FROM " . $table . " LIMIT 1")) {

    checkDBField($db, "idItems", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "txt", $table, " longtext ");
    checkDBField($db, "active", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "dateins", $table, " datetime DEFAULT NULL ");
  } else {

    mysqli_query($db, "CREATE TABLE `" . $table . "` (
            `id` int(11) NOT NULL AUTO_INCREMENT,
      		  `idItems` int(11) DEFAULT NULL,
      		  `txt` longtext,
      		  `active` int(11) DEFAULT NULL,
      		  `dateins` datetime DEFAULT NULL,
      		  PRIMARY KEY (`id`)
      		) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;");
  }

  // check _contentrss
  $table = $prefix . "_contentrss";
  if ($check = mysqli_query($db, "SELECT * FROM " . $table . " LIMIT 1")) {

    checkDBField($db, "idRss", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "title", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "txt", $table, " longtext ");
    checkDBField($db, "txt2", $table, " longtext ");
    checkDBField($db, "txt3", $table, " longtext ");
    checkDBField($db, "txt4", $table, " longtext ");
    checkDBField($db, "txt5", $table, " longtext ");
    checkDBField($db, "image", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "active", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "dateins", $table, " datetime DEFAULT NULL ");
  } else {

    mysqli_query($db, "CREATE TABLE `" . $table . "` (
            `id` int(11) NOT NULL AUTO_INCREMENT,
      		  `idRss` int(11) DEFAULT NULL,
      		  `title` varchar(255) DEFAULT NULL,
      		  `txt`  longtext,
      		  `txt2` longtext,
      		  `txt3` longtext,
      		  `txt4` longtext,
      		  `txt5` longtext,
      		  `image` varchar(255) DEFAULT NULL,
      		  `active` int(11) DEFAULT NULL,
      		  `dateins` datetime DEFAULT NULL,
      		  PRIMARY KEY (`id`)
      		) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;");
  }


  ///////////////////// fine content manager



  /////////////////////////////////////////////////////
  ///////     INSTALLAZIONE MESSAGE CENTER      ///////
  /////////////////////////////////////////////////////


  /*if(!is_dir("../../public/".$folder."/sms")) mkdir("../../public/".$folder."/sms");
			if(!is_dir("../../public/".$folder."/wa")) mkdir("../../public/".$folder."/wa");
			if(!is_dir("../../public/".$folder."/fb")) mkdir("../../public/".$folder."/fb");
			if(!is_dir("../../public/".$folder."/tw")) mkdir("../../public/".$folder."/tw");

			if(!is_dir("../../public/".$folder."/sms/1")) mkdir("../../public/".$folder."/sms/1");
			if(!is_dir("../../public/".$folder."/wa/1")) mkdir("../../public/".$folder."/wa/1");
			if(!is_dir("../../public/".$folder."/fb/1")) mkdir("../../public/".$folder."/fb/1");
			if(!is_dir("../../public/".$folder."/tw/1")) mkdir("../../public/".$folder."/tw/1");
*/

  // check _sms_setup
  $table = $prefix . "_sms_setup";
  if ($check = mysqli_query($db, "SELECT * FROM " . $table . " LIMIT 1")) {

    checkDBField($db, "pathsms", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "pathuploadtv", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "pathuploadweb", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "fontsize", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "fontweigh", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "rowheight", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "maxlength_tv", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "rows_tv", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "dateupdate", $table, " datetime DEFAULT NULL ");
    checkDBField($db, "repeat_tv", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "nrargoument", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "days", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "h0", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "h1", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "h2", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "h3", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "h4", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "h5", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "h6", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "h7", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "doubleclick", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "delsms", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "idch", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "groupmsg", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "alreadysend_tv", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "reloadtime", $table, " int(11) DEFAULT NULL ");
  } else {

    mysqli_query($db, "CREATE TABLE `" . $table . "` (
          `id` int(11) NOT NULL AUTO_INCREMENT,
          `pathsms` varchar(255) DEFAULT NULL,
          `pathuploadtv` varchar(255) DEFAULT NULL,
          `pathuploadweb` varchar(255) DEFAULT NULL,
          `fontsize` int(11) DEFAULT NULL,
          `fontweigh` varchar(255) DEFAULT NULL,
          `rowheight` int(11) DEFAULT NULL,
          `maxlength_tv` int(11) DEFAULT NULL,
          `rows_tv` int(11) DEFAULT NULL,
          `dateupdate` datetime DEFAULT NULL,
          `repeat_tv` int(11) DEFAULT NULL,
          `nrargoument` int(11) DEFAULT NULL,
          `days` varchar(255) DEFAULT NULL,
          `h0` varchar(255) DEFAULT NULL,
          `h1` varchar(255) DEFAULT NULL,
          `h2` varchar(255) DEFAULT NULL,
          `h3` varchar(255) DEFAULT NULL,
          `h4` varchar(255) DEFAULT NULL,
          `h5` varchar(255) DEFAULT NULL,
          `h6` varchar(255) DEFAULT NULL,
          `h7` varchar(255) DEFAULT NULL,
          `doubleclick` int(11) DEFAULT NULL,
          `delsms` int(11) DEFAULT NULL,
          `idch` int(11) DEFAULT NULL,
          `groupmsg` int(11) DEFAULT NULL,
          `alreadysend_tv` int(11) DEFAULT NULL,
          `reloadtime` int(11) NOT NULL,
          PRIMARY KEY (`id`)
        ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;");

    // dump
    mysqli_query($db, "INSERT INTO `" . $table . "` (`id`,`fontsize`,`fontweigh`,`rowheight`,`maxlength_tv`,`rows_tv`,`dateupdate`,`repeat_tv`, doubleclick, idch, groupmsg, alreadysend_tv, h1, h2, h3, h4, h5, h6, h7) VALUES (1,13,'normal',20,180,3,'2012-01-01 00:00:00',2,0, 1, 0, 0, '111111111111111111111111', '111111111111111111111111', '111111111111111111111111', '111111111111111111111111', '111111111111111111111111', '111111111111111111111111', '111111111111111111111111');");
  }






  /////////////////////////////////////////////////////
  ///////     INSTALLAZIONE MESSAGE CENTER      ///////
  /////////////////////////////////////////////////////

/*
  if (!is_dir("../public/" . $folder . "/sms")) mkdir("../public/" . $folder . "/sms");
  if (!is_dir("../public/" . $folder . "/wa")) mkdir("../public/" . $folder . "/wa");
  if (!is_dir("../public/" . $folder . "/fb")) mkdir("../public/" . $folder . "/fb");
  if (!is_dir("../public/" . $folder . "/tw")) mkdir("../public/" . $folder . "/tw");

  if (!is_dir("../public/" . $folder . "/sms/1")) mkdir("../public/" . $folder . "/sms/1");
  if (!is_dir("../public/" . $folder . "/wa/1")) mkdir("../public/" . $folder . "/wa/1");
  if (!is_dir("../public/" . $folder . "/fb/1")) mkdir("../public/" . $folder . "/fb/1");
  if (!is_dir("../public/" . $folder . "/tw/1")) mkdir("../public/" . $folder . "/tw/1");
*/


  // check _sms_setup
  $table = $prefix . "_sms_setup";
  if ($check = mysqli_query($db, "SELECT * FROM " . $table . " LIMIT 1")) {

    checkDBField($db, "pathsms", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "pathuploadtv", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "pathuploadweb", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "fontsize", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "fontweigh", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "rowheight", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "maxlength_tv", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "rows_tv", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "dateupdate", $table, " datetime DEFAULT NULL ");
    checkDBField($db, "repeat_tv", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "nrargoument", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "days", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "h0", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "h1", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "h2", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "h3", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "h4", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "h5", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "h6", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "h7", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "doubleclick", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "delsms", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "idch", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "groupmsg", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "alreadysend_tv", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "reloadtime", $table, " int(11) DEFAULT NULL ");
  } else {

    mysqli_query($db, "CREATE TABLE `" . $table . "` (
          `id` int(11) NOT NULL AUTO_INCREMENT,
          `pathsms` varchar(255) DEFAULT NULL,
          `pathuploadtv` varchar(255) DEFAULT NULL,
          `pathuploadweb` varchar(255) DEFAULT NULL,
          `fontsize` int(11) DEFAULT NULL,
          `fontweigh` varchar(255) DEFAULT NULL,
          `rowheight` int(11) DEFAULT NULL,
          `maxlength_tv` int(11) DEFAULT NULL,
          `rows_tv` int(11) DEFAULT NULL,
          `dateupdate` datetime DEFAULT NULL,
          `repeat_tv` int(11) DEFAULT NULL,
          `nrargoument` int(11) DEFAULT NULL,
          `days` varchar(255) DEFAULT NULL,
          `h0` varchar(255) DEFAULT NULL,
          `h1` varchar(255) DEFAULT NULL,
          `h2` varchar(255) DEFAULT NULL,
          `h3` varchar(255) DEFAULT NULL,
          `h4` varchar(255) DEFAULT NULL,
          `h5` varchar(255) DEFAULT NULL,
          `h6` varchar(255) DEFAULT NULL,
          `h7` varchar(255) DEFAULT NULL,
          `doubleclick` int(11) DEFAULT NULL,
          `delsms` int(11) DEFAULT NULL,
          `idch` int(11) DEFAULT NULL,
          `groupmsg` int(11) DEFAULT NULL,
          `alreadysend_tv` int(11) DEFAULT NULL,
          `reloadtime` int(11) NOT NULL,
          PRIMARY KEY (`id`)
        ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;");

    // dump
    mysqli_query($db, "INSERT INTO `" . $table . "` (`id`,`fontsize`,`fontweigh`,`rowheight`,`maxlength_tv`,`rows_tv`,`dateupdate`,`repeat_tv`, doubleclick, idch, groupmsg, alreadysend_tv, h1, h2, h3, h4, h5, h6, h7) VALUES (1,13,'normal',20,180,3,'2012-01-01 00:00:00',2,0, 1, 0, 0, '111111111111111111111111', '111111111111111111111111', '111111111111111111111111', '111111111111111111111111', '111111111111111111111111', '111111111111111111111111', '111111111111111111111111');");
  }



  // check _social
  $table = $prefix . "_social";
  if ($check = mysqli_query($db, "SELECT * FROM " . $table . " LIMIT 1")) {

    checkDBField($db, "caption", $table, " text CHARACTER SET utf8 NOT NULL ");
    checkDBField($db, "idapp", $table, " bigint(20) unsigned DEFAULT NULL ");
    checkDBField($db, "key_remote_jid", $table, " varchar(45) CHARACTER SET utf8 DEFAULT NULL ");
    checkDBField($db, "key_from_me", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "key_id", $table, " varchar(45) CHARACTER SET utf8 DEFAULT NULL ");
    checkDBField($db, "profile_image", $table, " varchar(255) CHARACTER SET utf8 DEFAULT NULL ");
    checkDBField($db, "status", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "name", $table, " varchar(255) CHARACTER SET utf8 DEFAULT NULL ");
    checkDBField($db, "thumb_ts", $table, " varchar(255) CHARACTER SET utf8 DEFAULT NULL ");
    checkDBField($db, "media_url", $table, " varchar(255) CHARACTER SET utf8 DEFAULT NULL ");
    checkDBField($db, "media_type", $table, " varchar(255) CHARACTER SET utf8 DEFAULT NULL ");
    checkDBField($db, "media_wa_type", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "media_name", $table, " varchar(255) CHARACTER SET utf8 DEFAULT NULL ");
    checkDBField($db, "media_hash", $table, " varchar(255) CHARACTER SET utf8 DEFAULT NULL ");
    checkDBField($db, "media_duration", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "origin", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "txtmsg", $table, " longtext CHARACTER SET utf8 ");
    checkDBField($db, "datemsg", $table, " varchar(10) CHARACTER SET utf8 DEFAULT NULL ");
    checkDBField($db, "timemsg", $table, " varchar(8) CHARACTER SET utf8 DEFAULT NULL ");
    checkDBField($db, "ok", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "tv", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "tv_send", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "web", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "web_send", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "deleted", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "dateins", $table, " datetime DEFAULT NULL ");
    checkDBField($db, "time_originale", $table, " bigint(20) DEFAULT NULL ");
    checkDBField($db, "txtmsg_originale", $table, " longtext CHARACTER SET utf8 ");
    checkDBField($db, "app", $table, " varchar(3) CHARACTER SET utf8 DEFAULT NULL ");
    checkDBField($db, "thumb_image", $table, " blob ");
    checkDBField($db, "idch", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "stato", $table, " varchar(255) CHARACTER SET utf8 DEFAULT NULL ");
    checkDBField($db, "active", $table, " int(11) NOT NULL ");
  } else {

    mysqli_query($db, "CREATE TABLE `" . $table . "` (
            `ID` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
            `caption` text CHARACTER SET utf8 NOT NULL,
            `idapp` bigint(20) unsigned DEFAULT NULL,
            `key_remote_jid` varchar(45) CHARACTER SET utf8 DEFAULT NULL,
            `key_from_me` int(11) DEFAULT NULL,
            `key_id` varchar(45) CHARACTER SET utf8 DEFAULT NULL,
            `profile_image` varchar(255) CHARACTER SET utf8 DEFAULT NULL,
            `status` int(11) DEFAULT NULL,
            `name` varchar(255) CHARACTER SET utf8 DEFAULT NULL,
            `thumb_ts` varchar(255) CHARACTER SET utf8 DEFAULT NULL,
            `media_url` varchar(255) CHARACTER SET utf8 DEFAULT NULL,
            `media_type` varchar(255) CHARACTER SET utf8 DEFAULT NULL,
            `media_wa_type` int(11) DEFAULT NULL,
            `media_name` varchar(255) CHARACTER SET utf8 DEFAULT NULL,
            `media_hash` varchar(255) CHARACTER SET utf8 DEFAULT NULL,
            `media_duration` int(11) DEFAULT NULL,
            `origin` int(11) DEFAULT NULL,
            `txtmsg` longtext CHARACTER SET utf8,
            `datemsg` varchar(10) CHARACTER SET utf8 DEFAULT NULL,
            `timemsg` varchar(8) CHARACTER SET utf8 DEFAULT NULL,
            `ok` int(11) DEFAULT NULL,
            `tv` int(11) DEFAULT NULL,
            `tv_send` int(11) DEFAULT NULL,
            `web` int(11) DEFAULT NULL,
            `web_send` int(11) DEFAULT NULL,
            `deleted` int(11) DEFAULT NULL,
            `dateins` datetime DEFAULT NULL,
            `time_originale` bigint(20) DEFAULT NULL,
            `txtmsg_originale` longtext CHARACTER SET utf8,
            `app` varchar(3) CHARACTER SET utf8 DEFAULT NULL,
            `thumb_image` blob,
            `idch` int(11) DEFAULT NULL,
            `stato` varchar(255) CHARACTER SET utf8 DEFAULT NULL,
            `active` int(11) NOT NULL,
            PRIMARY KEY (`ID`)
          ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_swedish_ci;");
  }



  // check _sms_argoument
  $table = $prefix . "_sms_argoument";
  if ($check = mysqli_query($db, "SELECT * FROM " . $table . " LIMIT 1")) {

    checkDBField($db, "text", $table, " longtext ");
    checkDBField($db, "dateins", $table, " datetime DEFAULT NULL ");
  } else {

    mysqli_query($db, "CREATE TABLE `" . $table . "` (
                  `id` int(11) NOT NULL AUTO_INCREMENT,
                  `text` longtext,
                  `dateins` datetime DEFAULT NULL,
                  PRIMARY KEY (`id`)
                ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;");
  }



  // check _sms_sms
  $table = $prefix . "_sms_sms";
  if ($check = mysqli_query($db, "SELECT * FROM " . $table . " LIMIT 1")) {

    checkDBField($db, "nrsms", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "datesms", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "timesms", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "txtsms", $table, " longtext ");
    checkDBField($db, "ok", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "tv", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "tv_send", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "web", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "web_send", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "deleted", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "dateins", $table, " datetime DEFAULT NULL ");
    checkDBField($db, "txtsms_originale", $table, " longtext ");
  } else {

    mysqli_query($db, "CREATE TABLE `" . $table . "` (
                  `id` int(11) NOT NULL AUTO_INCREMENT,
                  `nrsms` varchar(255) DEFAULT NULL,
                  `datesms` varchar(255) DEFAULT NULL,
                  `timesms` varchar(255) DEFAULT NULL,
                  `txtsms` longtext,
                  `ok` int(11) DEFAULT NULL,
                  `tv` int(11) DEFAULT NULL,
                  `tv_send` int(11) DEFAULT NULL,
                  `web` int(11) DEFAULT NULL,
                  `web_send` int(11) DEFAULT NULL,
                  `deleted` int(11) DEFAULT NULL,
                  `dateins` datetime DEFAULT NULL,
                  `txtsms_originale` longtext,
                  PRIMARY KEY (`id`)
                ) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;");
  }



  // check _blacklist
  $table = $prefix . "_blacklist";
  if ($check = mysqli_query($db, "SELECT * FROM " . $table . " LIMIT 1")) {

    checkDBField($db, "number", $table, " varchar(50) DEFAULT NULL ");
    checkDBField($db, "blocked", $table, " tinyint(4) NOT NULL DEFAULT '0' ");
  } else {

    mysqli_query($db, "CREATE TABLE `" . $table . "` (
                  `id` int(11) NOT NULL AUTO_INCREMENT,
                  `number` varchar(50) DEFAULT NULL,
                  `blocked` tinyint(4) NOT NULL DEFAULT '0',
                  PRIMARY KEY (`id`)
                ) ENGINE=InnoDB DEFAULT CHARSET=utf8;");
  }




  ///////////////////// fine message center


  // check _sms_argoument
  $table = $prefix . "_sms_argoument";
  if ($check = mysqli_query($db, "SELECT * FROM " . $table . " LIMIT 1")) {

    checkDBField($db, "text", $table, " longtext ");
    checkDBField($db, "dateins", $table, " datetime DEFAULT NULL ");
  } else {
    mysqli_query($db, "CREATE TABLE `" . $table . "` (
                  `id` int(11) NOT NULL AUTO_INCREMENT,
                  `text` longtext,
                  `dateins` datetime DEFAULT NULL,
                  PRIMARY KEY (`id`)
                ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;");
  }



  // check _sms_sms
  $table = $prefix . "_sms_sms";
  if ($check = mysqli_query($db, "SELECT * FROM " . $table . " LIMIT 1")) {

    checkDBField($db, "nrsms", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "datesms", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "timesms", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "txtsms", $table, " longtext ");
    checkDBField($db, "ok", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "tv", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "tv_send", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "web", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "web_send", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "deleted", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "dateins", $table, " datetime DEFAULT NULL ");
    checkDBField($db, "txtsms_originale", $table, " longtext ");
  } else {

    mysqli_query($db, "CREATE TABLE `" . $table . "` (
                  `id` int(11) NOT NULL AUTO_INCREMENT,
                  `nrsms` varchar(255) DEFAULT NULL,
                  `datesms` varchar(255) DEFAULT NULL,
                  `timesms` varchar(255) DEFAULT NULL,
                  `txtsms` longtext,
                  `ok` int(11) DEFAULT NULL,
                  `tv` int(11) DEFAULT NULL,
                  `tv_send` int(11) DEFAULT NULL,
                  `web` int(11) DEFAULT NULL,
                  `web_send` int(11) DEFAULT NULL,
                  `deleted` int(11) DEFAULT NULL,
                  `dateins` datetime DEFAULT NULL,
                  `txtsms_originale` longtext,
                  PRIMARY KEY (`id`)
                ) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;");
  }



  // check _blacklist
  $table = $prefix . "_blacklist";
  if ($check = mysqli_query($db, "SELECT * FROM " . $table . " LIMIT 1")) {
    checkDBField($db, "number", $table, " varchar(50) DEFAULT NULL ");
    checkDBField($db, "blocked", $table, " tinyint(4) NOT NULL DEFAULT '0' ");
  } else {

    mysqli_query($db, "CREATE TABLE `" . $table . "` (
                  `id` int(11) NOT NULL AUTO_INCREMENT,
                  `number` varchar(50) DEFAULT NULL,
                  `blocked` tinyint(4) NOT NULL DEFAULT '0',
                  PRIMARY KEY (`id`)
                ) ENGINE=InnoDB DEFAULT CHARSET=utf8;");
  }




  //tb_software_custom (x stampe personalizzate)
  $table = "tb_software_custom";
  if ($check = mysqli_query($db, "SELECT * FROM " . $table . " LIMIT 1")) {
    checkDBField($db, "Value", $table, " varchar(45) DEFAULT '' ");
    checkDBField($db, "Location", $table, " varchar(45) DEFAULT '' ");
    checkDBField($db, "Description", $table, " varchar(45) DEFAULT '' ");
    checkDBField($db, "Display", $table, " int(11) DEFAULT 1 ");
  } else {

    mysqli_query($db, "CREATE TABLE `tb_software_custom` (
                  `ID` int(10) unsigned NOT NULL AUTO_INCREMENT,
                  `Value` varchar(45) NOT NULL DEFAULT ' ',
                  `Location` varchar(45) NOT NULL DEFAULT ' ',
                  `Description` varchar(45) NOT NULL DEFAULT ' ',
                  `Display` int(11) NOT NULL DEFAULT '1',
                  PRIMARY KEY (`ID`)
                ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;");

    $query = "INSERT INTO `tb_software_custom` (`id`,`Value`,`Location`,`Description`,`Display`) VALUES
                        (1,3,'print/print_registro.php','Registro',1),
                        (2,5,'print/print_registro.php','Completo',1),
                        (3,7,'print/print_registro.php','Export Excel',1),
                        (4,8,'print/print_registro.php','Export TXT',1),
                        (5,9,'print/print/print_registro.php','Export SDF',1),
                        (6,10,'print/print_registro.php','Percentuali',1),
                        (7,2,'print/print_registro.php','Pagine numerate',1),
                        (8,1,'print/print_registro.php','Intestazione',1),
                        (9,4,'print/print_registro.php','Pagine e intestazione',1);";

    mysqli_query($db, $query) or die('Query failed: ');
  }


  // tb_parameters
  $table = "tb_parameters";
  if ($check = mysqli_query($db, "SELECT * FROM " . $table . " LIMIT 1")) {

    checkDBField($db, "name", $table, " varchar(50) NOT NULL DEFAULT ' ' ");
    checkDBField($db, "value", $table, " vBIGINT(20) ");
    checkDBField($db, "valuestring1", $table, " TEXT ");
    checkDBField($db, "valuestring2", $table, " TEXT ");
  } else {
    mysqli_query($db, "CREATE TABLE `" . $table . "` (
                          `ID` bigint(20) NOT NULL AUTO_INCREMENT,
                          `name` varchar(40) NOT NULL,
                          `value` bigint(20) DEFAULT NULL,
                          `valuestring1` text,
                          `valuestring2` text,
                          PRIMARY KEY (`ID`)
                        ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8");
  }

  mysqli_query($db, "TRUNCATE TABLE" . $table);
  mysqli_query($db, "INSERT INTO `tb_parameters` (`id`, `name`,`value`) VALUES (1, 'starttime', 0);");
  mysqli_query($db, "INSERT INTO `tb_parameters` (`id`, `name`,`value`) VALUES (2, 'endtime', 86399000);");
  mysqli_query($db, "INSERT INTO `tb_parameters` (`id`, `name`,`value`) VALUES (3, 'timewithoutzero', 0);");


  // wa_tb_control
  $table =  $prefix . "_tb_control";
  if ($check = mysqli_query($db, "SELECT * FROM " . $table . " LIMIT 1")) {

    checkDBField($db, "description", $table, " varchar(50) NOT NULL ");
    checkDBField($db, "color", $table, " varchar(50) NOT NULL ");
  } else {
    mysqli_query($db, "CREATE TABLE `" . $table . "` (
                          `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
                          `description` varchar(45) NOT NULL,
                          `color` varchar(45) NOT NULL,
                          PRIMARY KEY (`id`) USING BTREE
                        ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8");

    // dump
    mysqli_query($db, "INSERT INTO {$table} (`id`, `description`,`color`) VALUES (1, 'INFO 24H', '#ff80ff');");
    mysqli_query($db, "INSERT INTO {$table} (`id`, `description`,`color`) VALUES (2, 'INFO LOCALE', '#99ff99');");
    mysqli_query($db, "INSERT INTO {$table} (`id`, `description`,`color`) VALUES (3, 'INFO', '#00ccff');");
    mysqli_query($db, "INSERT INTO {$table} (`id`, `description`,`color`) VALUES (4, 'PUBBLICITA', '#ff6600');");
    mysqli_query($db, "INSERT INTO {$table} (`id`, `description`,`color`) VALUES (5, 'CODIFICATO', '#ffff66');");
    mysqli_query($db, "INSERT INTO {$table} (`id`, `description`,`color`) VALUES (6, 'NON CODIFICATO', '#66ffff');");
    mysqli_query($db, "INSERT INTO {$table} (`id`, `description`,`color`) VALUES (6, 'NO DATA', '#ff6666');");
  }


  //wa_tb_controlhy
  $table =  $prefix . "_tb_controlhy";
  if ($check = mysqli_query($db, "SELECT * FROM " . $table . " LIMIT 1")) {

    checkDBField($db, "id_channel", $table, " int(11) unsigned NOT NULL ");
    checkDBField($db, "date", $table, " date NOT NULL ");
    checkDBField($db, "id_control", $table, " int(11) unsigned NOT NULL");
    checkDBField($db, "value", $table, " varchar(45) DEFAULT NULL");
    checkDBField($db, "expectedValue", $table, " varchar(45) DEFAULT NULL");
    checkDBField($db, "creation_date", $table, " date DEFAULT '0000-00-00'");
    checkDBField($db, "validation_date", $table, " date DEFAULT '0000-00-00'");
    checkDBField($db, "print_date", $table, " date DEFAULT '0000-00-00'");
    checkDBField($db, "error", $table, " int(10) unsigned NOT NULL DEFAULT '0'");
  } else {
    mysqli_query($db, "CREATE TABLE `" . $table . "` (
                          `id` int(10)  NOT NULL AUTO_INCREMENT,
                          `id_channel` int(11) unsigned NOT NULL,
                          `date` date NOT NULL,
                          `id_control` int(11) unsigned NOT NULL,
                          `value` varchar(45) DEFAULT NULL,
                          `expectedValue` varchar(45) DEFAULT NULL,
                          `creation_date` date DEFAULT '0000-00-00',
                          `validation_date` date DEFAULT '0000-00-00',
                          `print_date` date DEFAULT '0000-00-00',
                          `error` int(10)  NOT NULL DEFAULT '0',
                          PRIMARY KEY (`id`)
                        ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8");
  }

  // wa_tb_cron
  $table =  $prefix . "_tb_cron";
  if ($check = mysqli_query($db, "SELECT * FROM " . $table . " LIMIT 1")) {

    checkDBField($db, "startedAt", $table, " datetime NOT NULL ");
    checkDBField($db, "Status", $table, " varchar(45) NOT NULL DEFAULT ' ' ");
    checkDBField($db, "PID", $table, " varchar(45) NOT NULL DEFAULT '0'");
    checkDBField($db, "Timestamp", $table, " datetime DEFAULT NULL");
    checkDBField($db, "toKill", $table, " int(11) NOT NULL DEFAULT '0'");
  } else {
    mysqli_query($db, "CREATE TABLE `" . $table . "` (
                          `ID` int(11)  NOT NULL AUTO_INCREMENT,
                          `startedAt` datetime NOT NULL,
                          `Status` varchar(45) NOT NULL DEFAULT ' ',
                          `PID` varchar(45) NOT NULL DEFAULT '0',
                          `Timestamp` datetime DEFAULT NULL,
                          `toKill` int(11)  NOT NULL DEFAULT '0',
                          PRIMARY KEY (`ID`)
                        ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8");
  }

  /* tb_logs contiene i percorsi per i records, fingerprints e logs (attualmente usata solo x bocra) */
  $table = "tb_logconfig";
  if ($check = mysqli_query($db, "SELECT * FROM " . $table . " LIMIT 1")) {

    checkDBField($db, "type", $table, " varchar(45) NOT NULL DEFAULT ' ' ");
    checkDBField($db, "tv", $table, " varchar(100) NOT NULL DEFAULT ' ' ");
    checkDBField($db, "radio", $table, " varchar(100) NOT NULL DEFAULT ' ' ");
  } else {
    mysqli_query($db, "CREATE TABLE `" . $table . "` (
                                    `id` int(11)  NOT NULL AUTO_INCREMENT,
                                    `type` varchar(45) DEFAULT ' ',
                                    `tv` varchar(100) DEFAULT NULL,
                                    `radio` varchar(100) DEFAULT NULL,
                                    PRIMARY KEY (`id`)
                                  ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;");
  }

  /* tb_records -> contiene la lista dei record caricati */
  $table = "{$prefix}_tb_records";
  if ($check = mysqli_query($db, "SELECT * FROM " . $table . " LIMIT 1")) {

    checkDBField($db, "id_channel", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "namefile", $table, " varchar(45) DEFAULT NULL ");
    checkDBField($db, "dateday", $table, " date DEFAULT NULL ");
    checkDBField($db, "datelastedit", $table, " datetime DEFAULT NULL ");
    checkDBField($db, "sizefile", $table, " varchar(255) DEFAULT NULL");
    checkDBField($db, "duration", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "type", $table, " varchar(45) DEFAULT NULL");
  } else {
    mysqli_query($db, "CREATE TABLE  {$table} (
                                      `id` int(11) NOT NULL AUTO_INCREMENT,
                                      `id_channel` int(11) DEFAULT NULL ,
                                      `namefile` varchar(255) DEFAULT NULL,
                                      `dateday` date DEFAULT NULL,
                                      `datelastedit` datetime DEFAULT NULL,
                                      `sizefile` varchar(255) DEFAULT NULL,
                                      `duration` varchar(255) DEFAULT NULL,
                                      `type` varchar(45) DEFAULT NULL,
                                      PRIMARY KEY (`id`)
                                    ) ENGINE=InnoDB AUTO_INCREMENT=233244 DEFAULT CHARSET=utf8;");
  }

  /* tb_fingerprints */
  $table = "{$prefix}_tb_fingerprints";
  if ($check = mysqli_query($db, "SELECT * FROM " . $table . " LIMIT 1")) {

    checkDBField($db, "id_channel", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "namefile", $table, " varchar(45) DEFAULT NULL ");
    checkDBField($db, "dateday", $table, " date DEFAULT NULL ");
    checkDBField($db, "datelastedit", $table, " datetime DEFAULT NULL ");
    checkDBField($db, "sizefile", $table, " varchar(255) DEFAULT NULL");
  } else {
    mysqli_query($db, "CREATE TABLE  {$table} (
                                      `id` int(11) NOT NULL AUTO_INCREMENT,
                                      `id_channel` int(11) DEFAULT NULL ,
                                      `namefile` varchar(255) DEFAULT NULL,
                                      `dateday` date DEFAULT NULL,
                                      `datelastedit` datetime DEFAULT NULL,
                                      `sizefile` varchar(255) DEFAULT NULL,
                                      PRIMARY KEY (`id`)
                                    ) ENGINE=InnoDB AUTO_INCREMENT=233244 DEFAULT CHARSET=utf8;");
  }

  /* tb_logs */
  $table = $prefix . "_tb_logs";
  if ($check = mysqli_query($db, "SELECT * FROM " . $table . " LIMIT 1")) {

    checkDBField($db, "id_channel", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "dateday", $table, " date DEFAULT NULL ");
    checkDBField($db, "datefile", $table, " date DEFAULT NULL ");
    checkDBField($db, "timefile", $table, " varchar(255) DEFAULT NULL");
    checkDBField($db, "sizefile", $table, " varchar(255) DEFAULT NULL");
    checkDBField($db, "typeimport", $table, " varchar(255) DEFAULT NULL");
  } else {
    mysqli_query($db, "CREATE TABLE  {$table} (
                                      `id` int(11) NOT NULL AUTO_INCREMENT,
                                      `id_channel` int(11) DEFAULT NULL,
                                      `dateday` date DEFAULT NULL,
                                      `datefile` date DEFAULT NULL,
                                      `timefile` varchar(255) DEFAULT NULL,
                                      `sizefile` varchar(255) DEFAULT NULL,
                                      `typeimport` varchar(255) DEFAULT NULL,
                                      PRIMARY KEY (`id`)
                                     ) ENGINE=InnoDB AUTO_INCREMENT=17673 DEFAULT CHARSET=utf8;");
  }


  // Task Manager
  $table = "tm_instances";
  if ($check = mysqli_query($db, "SELECT * FROM " . $table . " LIMIT 1")) {

    checkDBField($db, "idmachine", $table, " int(11) NOT NULL");
    checkDBField($db, "groupname", $table, " varchar(255) NOT NULL ");
    checkDBField($db, "note", $table, " TEXT DEFAULT NULL");
    checkDBField($db, "enabled", $table, " int(11) unsigned DEFAULT '0'");
    checkDBField($db, "address", $table, " varchar(255) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT ' '");
    checkDBField($db, "port", $table, " int(11) unsigned DEFAULT '0'");
    checkDBField($db, "intervalms", $table, " int(11) unsigned DEFAULT '0'");
    checkDBField($db, "label", $table, " varchar(255) DEFAULT ' '");
  } else {
    $query = "CREATE TABLE  {$table} (
                                `id` int(11) NOT NULL AUTO_INCREMENT,
                                `idmachine` int(11) NOT NULL,
                                `groupname` varchar(255) NOT NULL,
                                `note` TEXT DEFAULT NULL,
                                `enabled` int(11)  DEFAULT '0',
                                `address` varchar(255)  DEFAULT ' ',
                                `port` int(11)  DEFAULT '0',
                                `intervalms` int(11)  DEFAULT '0',
                                `label` varchar(255) DEFAULT ' ',
                                 PRIMARY KEY (`id`)
                              ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;";

    mysqli_query($db, $query) or die('Query failed: ' . $query);
  }

  $table = "tm_instance_machine";
  if ($check = mysqli_query($db, "SELECT * FROM " . $table . " LIMIT 1")) {
    checkDBField($db, "idinstance", $table, " int(11) NOT NULL");
    checkDBField($db, "idmachine", $table, " int(11) NOT NULL");
  } else {
    $query = "CREATE TABLE  {$table} (
                                `id` int(11) NOT NULL AUTO_INCREMENT,
                                `idinstance` int(11) NOT NULL,
                                `idmachine` int(11) NOT NULL,
                                 PRIMARY KEY (`id`)
                              ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;";

    mysqli_query($db, $query) or die('Query failed: ' . $query);
  }

  $table = "tm_tasks";
  if ($check = mysqli_query($db, "SELECT * FROM " . $table . " LIMIT 1")) {

    checkDBField($db, "idinstance", $table, " int(11) NOT NULL ");
    checkDBField($db, "mode", $table, " int(11)  NOT NULL ");
    checkDBField($db, "enabled", $table, " int(11)  DEFAULT NULL");
    checkDBField($db, "description", $table, " TEXT DEFAULT NULL ");
    checkDBField($db, "time", $table, " varchar(45) DEFAULT NULL");
    checkDBField($db, "timeto", $table, " varchar(45) DEFAULT NULL");
    checkDBField($db, "days", $table, " varchar(45) DEFAULT NULL");
    checkDBField($db, "lastExecution", $table, " varchar(45) DEFAULT ' '");
    checkDBField($db, "maxRetry", $table, " int(11) DEFAULT NULL");
    checkDBField($db, "timeoutRetry", $table, " int(11) DEFAULT NULL");
    checkDBField($db, "repeatScheduling", $table, " int(11) DEFAULT NULL");
    checkDBField($db, "uniqueid", $table, " varchar(255) DEFAULT NULL");
  } else {

    $query = "CREATE TABLE  {$table} (
                            `id` int(11)  NOT NULL AUTO_INCREMENT,
                            `idinstance` int(11) NOT NULL,
                            `mode` int(11) NOT NULL,
                            `enabled` int(11) DEFAULT NULL,
                            `description` TEXT DEFAULT NULL,
                            `time` varchar(45) DEFAULT NULL,
                            `timeto` varchar(45) DEFAULT NULL,
                            `days` varchar(45) DEFAULT NULL,
                            `lastExecution` varchar(45) DEFAULT ' ',
                            `maxRetry` int(11) DEFAULT NULL,
                            `timeoutRetry` int(11) DEFAULT NULL,
                            `repeatScheduling` int(11) DEFAULT NULL,
                            `uniqueid` varchar(255) DEFAULT NULL,
                            PRIMARY KEY (`id`),
                            KEY `idinstance` (`idinstance`),
                            CONSTRAINT `idinstance` FOREIGN KEY (`idinstance`) REFERENCES `tm_instances` (`id`) ON DELETE CASCADE
                          ) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=utf8;";

    mysqli_query($db, $query) or die('Query failed: ' . $query);
  }

  $table = "tm_jobs";
  if ($check = mysqli_query($db, "SELECT * FROM {$table} LIMIT 1")) {

    checkDBField($db, "idtask", $table, " int(11) NOT NULL ");
    checkDBField($db, "enabled", $table, " int(11)  NOT NULL");
    checkDBField($db, "fileSource", $table, " varchar(255) NOT NULL ");
    checkDBField($db, "fileSource_user", $table, " varchar(255) NOT NULL");
    checkDBField($db, "fileSource_pass", $table, " varchar(255) NOT NULL");
    checkDBField($db, "fileDest", $table, " varchar(255) NOT NULL");
    checkDBField($db, "deleteFile", $table, " int(11)  NOT NULL");
    checkDBField($db, "passive", $table, " int(11) NOT NULL");
    checkDBField($db, "fromFolder", $table, " varchar(100) NOT NULL");
  } else {
    $query = "CREATE TABLE  `tm_jobs` (
                            `id` int(11)  NOT NULL AUTO_INCREMENT,
                            `idtask` int(11)  NOT NULL,
                            `enabled` int(11)  NOT NULL,
                            `fileSource` varchar(255) NOT NULL,
                            `fileSource_user` varchar(255) NOT NULL,
                            `fileSource_pass` varchar(255) NOT NULL,
                            `fileDest` varchar(255) NOT NULL,
                            `deleteFile` int(11)  NOT NULL,
                            `passive` int(11)  NOT NULL,
                            `fromFolder` varchar(100) NOT NULL,
                            PRIMARY KEY (`id`),
                            KEY `idtask` (`idtask`),
                            CONSTRAINT `idtask` FOREIGN KEY (`idtask`) REFERENCES `tm_tasks` (`id`) ON DELETE CASCADE
                          ) ENGINE=InnoDB AUTO_INCREMENT=311 DEFAULT CHARSET=utf8;";

    mysqli_query($db, $query) or die('Query failed: ' . $query);
  }


  // Fay 01-2021 anag_machine ed anag_area sono state rimosse, ma riaggiunte xhè servono x il task manager 

  $table = $prefix . "_anag_machine";
  if ($check = mysqli_query($db, "SELECT * FROM " . $table . " LIMIT 1")) {

    checkDBField($db, "idarea", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "name", $table, "  varchar(255) DEFAULT NULL ");
    checkDBField($db, "description", $table, " varchar(255) DEFAULT NULL ");
  } else {

    mysqli_query($db, "CREATE TABLE `" . $table . "` (
                                    `id` int(111) NOT NULL AUTO_INCREMENT,
                                    `idarea` int(11) DEFAULT NULL,
                                    `name` varchar(255) DEFAULT NULL,
                                    `description` varchar(255) DEFAULT NULL,
                                    PRIMARY KEY (`id`)
                                  ) ENGINE=MyISAM AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;");
  }


  $table = $prefix . "_anag_area";
  if ($check = mysqli_query($db, "SELECT * FROM " . $table . " LIMIT 1")) {
    checkDBField($db, "idcustomer", $table, " int(11) DEFAULT NULL ");
    checkDBField($db, "name", $table, " varchar(255) DEFAULT NULL ");
    checkDBField($db, "description", $table, " varchar(255) DEFAULT NULL ");
  } else {

    mysqli_query($db, "CREATE TABLE  {$table} (
                                    `id` int(11) NOT NULL AUTO_INCREMENT,
                                    `idcustomer` int(11) DEFAULT NULL,
                                    `name` varchar(255) DEFAULT NULL,
                                    `description` varchar(255) DEFAULT NULL,
                                    PRIMARY KEY (`id`)
                                  ) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;");

    // creazione area di default 
    mysqli_query($db, "INSERT INTO " . $table . " (`id`, `idcustomer`,`name`, `description`) VALUES (1, 1, 'AREA1', '');");
  }


  // wa_report_projects

  if ($check = mysqli_query($db, "SELECT * FROM wa_report_projects LIMIT 1")) {

    checkDBField($db, "id_user", "wa_report_projects", " int(10) unsigned NOT NULL DEFAULT '0' ");
    checkDBField($db, "name", "wa_report_projects", " varchar(45) NOT NULL DEFAULT '' ");
    checkDBField($db, "payload", "wa_report_projects", " longtext NOT NULL ");
    checkDBField($db, "type", "wa_report_projects", " varchar(45) NOT NULL DEFAULT '' ");
    checkDBField($db, "is_default", "wa_report_projects", " tinyint(3) unsigned NOT NULL DEFAULT '0' ");
  } else {

    mysqli_query($db, "CREATE TABLE `wa_report_projects` (
          `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
          `id_user` int(10) unsigned NOT NULL DEFAULT '0',
          `name` varchar(45) NOT NULL DEFAULT '',
          `payload` longtext NOT NULL,
          `type` varchar(45) NOT NULL DEFAULT '',
          `is_default` tinyint(3) unsigned NOT NULL DEFAULT '0',
          PRIMARY KEY (`id`)
        ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;");
  }


  //
  // TABELLA X UPLOADMANAGER
  //
  //
  // TABELLA CONFIG
  //
  $table = $prefix . "_config";
  if ($check = mysqli_query($db, "SELECT * FROM " . $table . " LIMIT 1")) {
    checkDBField($db, "email_1", $table, "text");
    checkDBField($db, "email_2", $table, "text");
    checkDBField($db, "enabled", $table, "tinyint(3) unsigned DEFAULT NULL");
  } else {
    mysqli_query($db, "CREATE TABLE IF NOT EXISTS `$table` (
      `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
      `email_1` text,
      `email_2` text,
      `enabled` tinyint(3) unsigned DEFAULT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;");
    mysqli_query($db, "INSERT INTO `$table` (`id`, `email_1`, `email_2`, `enabled`) VALUES
      (1, 'info@bitonlive.com', 'info@bitonlive.com', 0);");
  }

  //
  // TABELLA EDITIONS
  //
  $table = $prefix . "_editions";
  if ($check = mysqli_query($db, "SELECT * FROM " . $table . " LIMIT 1")) {
    checkDBField($db, "idtype", $table, "int(11) DEFAULT NULL");
    checkDBField($db, "timeedition", $table, "int(11) DEFAULT NULL");
    checkDBField($db, "maxtimeup", $table, "int(11) DEFAULT NULL");
    checkDBField($db, "days", $table, "varchar(7) DEFAULT NULL");
    checkDBField($db, "path", $table, "varchar(500) DEFAULT NULL");
    checkDBField($db, "name", $table, "varchar(100) DEFAULT NULL");
  } else {
    mysqli_query($db, "CREATE TABLE IF NOT EXISTS `$table` (
      `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
      `idtype` int(11) DEFAULT NULL,
      `timeedition` int(11) DEFAULT NULL,
      `maxtimeup` int(11) DEFAULT NULL,
      `days` varchar(7) DEFAULT NULL,
      `path` varchar(500) DEFAULT NULL,
      `name` varchar(100) DEFAULT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;");
  }


  //
  // TABELLA EDITIONSTYPE
  //
  $table = $prefix . "_editionstype";
  if ($check = mysqli_query($db, "SELECT * FROM " . $table . " LIMIT 1")) {
      checkDBField($db, "description", $table, "varchar(45) DEFAULT NULL");
  } else {
      mysqli_query($db, "CREATE TABLE IF NOT EXISTS `$table` (
        `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
        `description` varchar(45) DEFAULT NULL,
        PRIMARY KEY (`id`)
      ) ENGINE=InnoDB DEFAULT CHARSET=utf8;");
  }

  //
  // TABELLA UPLOADS
  //
  $table = $prefix . "_uploads";
  if ($check = mysqli_query($db, "SELECT * FROM " . $table . " LIMIT 1")) {
      checkDBField($db, "dateup", $table, "timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP");
      checkDBField($db, "idedition", $table, "int(11) DEFAULT NULL");
      checkDBField($db, "dateedition", $table, "varchar(45) DEFAULT NULL");
      checkDBField($db, "iduser", $table, "int(11) DEFAULT NULL");
      checkDBField($db, "filename", $table, "varchar(245) DEFAULT NULL");
  } else {
      mysqli_query($db, "CREATE TABLE IF NOT EXISTS `$table` (
        `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
        `dateup` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
        `idedition` int(11) DEFAULT NULL,
        `dateedition` varchar(45) DEFAULT NULL,
        `iduser` int(11) DEFAULT NULL,
        `filename` varchar(245) DEFAULT NULL,
        PRIMARY KEY (`id`)
      ) ENGINE=InnoDB DEFAULT CHARSET=utf8;");
  }

  //
  // TABELLA EDITIONSUSER
  //
  $table = $prefix . "_editionsuser";
  if ($check = mysqli_query($db, "SELECT * FROM " . $table . " LIMIT 1")) {
      checkDBField($db, "iduser", $table, "int(11) NOT NULL");
      checkDBField($db, "idedition", $table, "int(11) NOT NULL");
  } else {
      mysqli_query($db, "CREATE TABLE IF NOT EXISTS `$table` (
        `id` int(11) NOT NULL AUTO_INCREMENT,
        `iduser` int(11) NOT NULL,
        `idedition` int(11) NOT NULL,
        PRIMARY KEY (`id`)
      ) ENGINE=MyISAM DEFAULT CHARSET=utf8;");
  }




  // SCRITTURA FILE E INSTALLAZIONE COMPLETATA
  $CONNECTIONFILE = "[CONNECTION]" . PHP_EOL . "SERVER=" . $ip . PHP_EOL . "USER=" . $user . PHP_EOL . "PWD=" . $pass . PHP_EOL . "DBNAME=" . $name . PHP_EOL;

  if (file_put_contents("../include/.htsite", $CONNECTIONFILE)) {

    file_put_contents('instld.wap', "INSTALLAZIONE ESEGUITA CON SUCCESSO IN DATA " . date('d-m-Y H:i:s'));
    return "installation_complete";
  }

  return "error";
}


function checkDBField($dblocal, $field, $table, $type)
{

  if (!mysqli_query($dblocal, "SELECT `" . $field . "` FROM " . $table . " LIMIT 1")) {
    @mysqli_query($dblocal, "ALTER TABLE " . $table . " ADD COLUMN `" . $field . "` " . $type);
    logThis("database", "ALTER TABLE " . $table . " ADD COLUMN` " . $field . "` " . $type);
  } else {
    @mysqli_query($dblocal, "ALTER TABLE " . $table . " CHANGE COLUMN `" . $field . "` `" . $field . "` " . $type);
    logThis("database", "ALTER TABLE " . $table . " CHANGE COLUMN `" . $field . "` `" . $field . "` " . $type);
  }
}
