<?php

session_start();
session_regenerate_id();
/* ambil konfigurasi */
require_once 'config.php';
/* kalau belum ada struktur database-nya, bikin dulu */
$nresult = gcms_list_tables("");
$o = gcms_fetch_row($nresult);
if (!$o) {
    $cfile = "gcms.sql";
    $fsql = @fopen($cfile, "r");
    $csql = fread($fsql, filesize($cfile));
    fclose($fsql);
    gcms_run_query_block($csql);
}
/* ambil library berisi sekumpulan fungsi mendasar */
require_once 'lib.php';
/* pre processing untuk data daftar yang di-pass lewat url */
$daftars = explode(",", $_REQUEST['daftars']);
foreach ($daftars as $daftar) {
    if ($daftar) {
        global ${$daftar . '_data'}, ${$daftar . '_deleted'};
        /* ubah data daftar ke dalam bentuk array */
        ${$daftar . '_data'} = array();
        $rows = explode("|", $_REQUEST[$daftar . '_data']);
        $fields = explode(",", $rows[0]);
        for ($i = 1; $i < count($rows); $i++) {
            if ($rows[$i]) {
                $data = explode(",", $rows[$i]);
                $d = array();
         // find sql file and execute it
         $cpathsql = explode('/', $_POST['path']);
         $ncount = count($cpathsql);
         $cpathsql = array_slice($cpathsql, 0, $ncount - 1);
         $cpathsql = implode("/", $cpathsql);
         foreach (b_getlistfile($cpathsql) as $cfile) {
             if (eregi("." . DRIVER . ".sql", $cfile)) {
                 // read the files
                 $fsql = @fopen($cfile, "r");
                 $csql = fread($fsql, filesize($cfile));
                 fclose($fsql);
                 gcms_run_query_block($csql);
             }
         }
         // create list of current tables after process
         $nresult = gcms_list_tables(DATABASE);
         while ($atables = gcms_fetch_row($nresult)) {
             $atablesafter[] = $atables[0];
         }
         // find the new tables name
         $atablenew = array_diff($atablesafter, $atablesbefore);
         $csql = "insert into " . PREFIX . "moduls (cpath) values ('" . $_POST['path'] . "')";
     } else {
         $ainfoerror = b_readinit(str_replace(".php", ".init.php", strtolower($_POST['path'])));
         $cerror = "Extension <b>" . $ainfoerror['name'] . " " . $ainfoerror['version'] . "</b> can't be enabled, \r\n                please check extension dependency<br/>";
     }
 }
 if ($csql != "") {
     gcms_query($csql);
     $nid = gcms_insert_id("moduls");
     // if new tables created