<?php

/* ====================
  [BEGIN_COT_EXT]
  Hooks=module
  [END_COT_EXT]
  ==================== */

/**
 * EasyAdmin for Giglance
 *
 * @package Eadmin main panel
 * @version 2.6.9
 * @author CoTEMPLATE
 * @copyright Copyright (c) CoTEMPLATE.com
 */
(defined('COT_ADMIN')) or die('Wrong URL.');

list($usr['auth_read'], $usr['auth_write'], $usr['isadmin']) = cot_auth('admin', 'any');
cot_block($usr['isadmin']);

if (empty($out['head'])) $out['head'] = '';

cot::$out['head'] .= $R['code_noindex'];

$classFile = cot_incfile('eadmin', 'module', 'main');

$em = cot_import('em', 'G', 'TXT');
$r = cot_import('r', 'G', 'TXT');

cot::$out['adminpath'][] = array(cot_url('admin'), $L['Adminpanel']);

if (file_exists($classFile)) {
    require_once $classFile;

    $MainClass = new MainMod();

    $currentAction = $em . 'Action';
    $ajaxAction = $r . 'Ajax';

    cot::$out['subtitle'] = cot::$L['Administration'];
    cot::$out['title'] = $L['EA_panel'];

    $etp = new XTemplate(cot_tplfile('eadmin', 'module'));
    if (!$em && !$r) {
        if (!cot::$cfg['debug_mode'] && file_exists('install.php') && is_writable('datas/config.php')) {
            cot_clear_messages();
            cot_error('home_installable_error');
        }

        if (cot::$cfg['check_updates'] && cot::$cache) {
            $update_info = cot::$cache->db->get('update_info');
            if (!$update_info) {
                if (ini_get('allow_url_fopen')) {
                    $update_info = @file_get_contents('http://www.verotela.com/update-check');
                    if ($update_info) {
                        $update_info = json_decode($update_info, TRUE);
                        cot::$cache->db->store('update_info', $update_info, COT_DEFAULT_REALM, 86400);
                    }
                } elseif (function_exists('curl_init')) {
                    $curl = curl_init();
                    curl_setopt($curl, CURLOPT_URL, 'http://www.verotela.com/update-check');
                    curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
                    $update_info = curl_exec($curl);
                    if ($update_info) {
                        $update_info = json_decode($update_info, TRUE);
                        cot::$cache->db->store('update_info', $update_info, COT_DEFAULT_REALM, 86400);
                    }
                    curl_close($curl);
                }
            }
            if ($update_info['update_ver'] > cot::$cfg['version']) {
                $etp->assign(array(
                    'ADMIN_HOME_UPDATE_REVISION' => sprintf(cot::$L['home_update_revision'], cot::$cfg['version'], htmlspecialchars($update_info['update_ver'])),
                    'ADMIN_HOME_UPDATE_MESSAGE' => cot_parse($update_info['update_message']),
                ));
                $etp->parse('MAIN.EHOME.UPDATE');
            }
        }

        $sql = cot::$db->query("SHOW TABLES");
        foreach ($sql->fetchAll(PDO::FETCH_NUM) as $row) {
            $table_name = $row[0];
            $status = cot::$db->query("SHOW TABLE STATUS LIKE '$table_name'");
            $status1 = $status->fetch();
            $status->closeCursor();
            $tables[] = $status1;
        }

        $total_length = 0;
        $total_rows = 0;
        $total_index_length = 0;
        $total_data_length = 0;
        foreach ($tables as $dat) {
            $table_length = $dat['Index_length'] + $dat['Data_length'];
            $total_length += $table_length;
            $total_rows += $dat['Rows'];
            $total_index_length += $dat['Index_length'];
            $total_data_length += $dat['Data_length'];
        }

        $totalplugins = cot::$db->query("SELECT DISTINCT(pl_code) FROM $db_plugins WHERE 1 GROUP BY pl_code")->rowCount();
        $totalhooks = cot::$db->query("SELECT COUNT(*) FROM $db_plugins")->fetchColumn();

        $etp->assign(array(
            'ADMIN_HOME_DB_TOTAL_ROWS' => $total_rows,
            'ADMIN_HOME_DB_INDEXSIZE' => number_format(($total_index_length / 1024), 1, '.', ' '),
            'ADMIN_HOME_DB_DATASSIZE' => number_format(($total_data_length / 1024), 1, '.', ' '),
            'ADMIN_HOME_DB_TOTALSIZE' => number_format(($total_length / 1024), 1, '.', ' '),
            'ADMIN_HOME_TOTALPLUGINS' => $totalplugins,
            'ADMIN_HOME_TOTALHOOKS' => $totalhooks,
            'ADMIN_HOME_VERSION' => cot::$cfg['version'],
            'ADMIN_HOME_DB_VERSION' => htmlspecialchars(cot::$db->query("SELECT upd_value FROM $db_updates WHERE upd_param = 'revision'")->fetchColumn()),
            'ADMIN_TITLE' => cot::$L['Main'],
        ));

        /* === Hook === */
        foreach (cot_getextplugins('admin.home.mainpanel', 'R') as $pl) {
            $line = '';
            include $pl;
            if (!empty($line)) {
                $etp->assign('ADMIN_HOME_MAINPANEL', $line);
                $etp->parse('MAIN.EHOME.MAINPANEL');
            }
        }
        /* ===== */

        /* === Hook === */
        foreach (cot_getextplugins('admin.home.sidepanel', 'R') as $pl) {
            $line = '';
            include $pl;
            if (!empty($line)) {
                $etp->assign('ADMIN_HOME_SIDEPANEL', $line);
                $etp->parse('MAIN.EHOME.SIDEPANEL');
            }
        }
        /* ===== */

        /* === Hook === */
        foreach (cot_getextplugins('admin.home', 'R') as $pl) {
            include $pl;
        }
        /* ===== */

        cot_display_messages($etp);
    }
    if (empty($em) && empty($r)) {
        $etp->parse('MAIN.EHOME');
    }
    if (!$r && method_exists($MainClass, $currentAction)) {
        $mcont = $MainClass->$currentAction();
    }

    $etp->assign(array(
        'ADMIN_TITLE' => $out['title'],
        'ADMIN_BREADCRUMBS' => cot_breadcrumbs($out['adminpath'], false),
        'ADMIN_SUBTITLE' => $out['small_title'],
        'ADMIN_MAIN' => $mcont,
        'ADMIN_HELP' => $adminhelp
    ));
    if ($r && method_exists($MainClass, $ajaxAction)) {
        $outContent = $MainClass->$ajaxAction();
    } else {
        $etp->parse('MAIN');
        $outContent = $etp->text('MAIN');
    }

    if (!$r) {
        require_once $cfg['system_dir'] . '/header.php';
    }
    if (isset($outContent))
        echo $outContent;
    if (!$r) {
        require_once $cfg['system_dir'] . '/footer.php';
    }
} else {
    cot_die_message(404);
    exit;
}