<?php



class dbackup {



    function dbackup($configObj) {

        $this->sortvar = '<img alt="sorting" src="' . GLOBAL_ADMIN_IMAGES_PATH . 'arrow-up.png" style="vertical-align:middle;">';

        $this->configObj = $configObj;

    }



    function intitialize() {

        $this->searchby = (isset($_REQUEST['searchby'])) ? $_REQUEST['searchby'] : $this->searchby;

        $this->searchtxt = (isset($_REQUEST['searchtxt'])) ? urldecode($_REQUEST['searchtxt']) : '';

        $this->searchtxtjs = (isset($_REQUEST['searchtxt'])) ? urlencode($_REQUEST['searchtxt']) : '';

        $this->orderby = (isset($_REQUEST['orderby'])) ? $_REQUEST['orderby'] : $this->orderby;

        $this->ordertype = (isset($_REQUEST['ordertype'])) ? $_REQUEST['ordertype'] : $this->ordertype;

        $this->filterby = (isset($_REQUEST['filterby'])) ? $_REQUEST['filterby'] : $this->filterby;



        if (isset($_REQUEST['sorting'])) {

            if ($this->ordertype == "asc") {   // This is for sort image

                $this->ordertype = "desc";

                $this->sortvar = "<img alt=\"sorting\" src=\"" . GLOBAL_ADMIN_IMAGES_PATH . "arrow-down.png\" style=\"vertical-align:middle;\">";

            } else if ($this->ordertype == "desc") {   // This is for sort image

                $this->ordertype = "asc";



                $this->sortvar = "<img alt=\"sorting\" src=\"" . GLOBAL_ADMIN_IMAGES_PATH . "arrow-up.png\" style=\"vertical-align:middle;\">";

            }

        } else {

            $this->sortvar = "<img alt=\"sorting\" src=\"" . GLOBAL_ADMIN_IMAGES_PATH . "arrow-up.png\" style=\"vertical-align:middle;\">";

        }



        $this->pagesize = (isset($_REQUEST['pagesize'])) ? $_REQUEST['pagesize'] : $this->pagesize;

        $this->pagenumber = (isset($_REQUEST['pagenumber'])) ? $_REQUEST['pagenumber'] : $this->pagenumber;

        $this->start = ($this->pagenumber - 1 ) * $this->pagesize;

    }



    // **********************

    // Generate URL

    // **********************



    function Generateurl() {



        $this->urlwithpara = $this->pagename . '&' . 'pagesize=' . $this->pagesize . '&numofrows=' . $this->numofrows . '&orderby=' . $this->orderby . '&ordertype=' . $this->ordertype . '&searchby=' . $this->searchby . '&searchtxt=' . $this->searchtxtjs . '&pagenumber=' . $this->pagenumber . '&filterby=' . $this->filterby;

        $this->urlwithpoutsearch = $this->pagename . '&' . 'pagesize=' . $this->pagesize . '&orderby=' . $this->orderby . '&ordertype=' . $this->ordertype . '&filterby=' . $this->filterby;

        $this->urlwithoutsort = $this->pagename . '&' . 'pagesize=' . $this->pagesize . '&numofrows=' . $this->numofrows . '&searchby=' . $this->searchby . '&searchtxt=' . $this->searchtxtjs . '&pagenumber=' . $this->pagenumber . '&ordertype=' . $this->ordertype . '&filterby=' . $this->filterby;

        $this->urlwithoutpaging = $this->pagename . '&orderby=' . $this->orderby . '&ordertype=' . $this->ordertype . '&searchby=' . $this->searchby . '&searchtxt=' . $this->searchtxtjs . '&filterby=' . $this->filterby;

        $this->urlwithoutfilter = $this->pagename . '&' . 'pagesize=' . $this->pagesize . '&orderby=' . $this->orderby . '&ordertype=' . $this->ordertype . '&searchby=' . $this->searchby . '&searchtxt=' . $this->searchtxtjs;

    }



    function org_generateParam($view = 'category', $actionurl = 'index.php?module=category&action=add', $searchArray = array("contacts" => "Contactus"), $table = 'DB_PREFIX.contacts', $position = 'top') {

        $filterPos = ($position == 'top') ? 'FT' : 'FB';

        if ($this->configObj->glSessionObj->value('chr_user') == 'A') {

            $filterstar = array('filterarray' => array('chr_read-Y' => 'Read Only',

                    'chr_read-N' => 'Unread Only',

                    'chr_star-Y' => 'Star Only',

                    'chr_star-N' => 'Unstar Only'

                ),

                'filterurl' => $this->urlwithoutfilter,

                'filterposition' => $filterPos,

                'filterby' => $this->filterby

            );

        } else {



            $filterstar = array();

        }

        return array('pageurl' => $this->pagename,

            'heading' => $view,

            'listImage' => 'add-new-user-icon.png',

            'tablename' => $table,

            'position' => $position,

            'actionImage' => 'add-new-button-blue.gif',

            'actionImageHover' => 'add-new-button-blue-hover.gif',

            'actionUrl' => $actionurl

        );

    }



    function DateCmp($a, $b) {

        return ($a[1] < $b[1]) ? -1 : 0;

    }



    //start load page



    /*     * ********************* */



    function load_dbackup() {



        $dir = "html/dbbackup/db_backups";

        $path = $this->configObj->currentModulePath . 'html/dbbackup/db_backups/'; // change the path here related to this page

        $handle = opendir($path);

        $i = 0;

        while (($file = readdir($handle)) !== false) {

            $zipfile = substr(strrchr($file, '.'), 1);

            if ($zipfile == 'bz2') {

                $j[$i] = $file;

                $i++;

            }

        }

        if ($i != 0) {

            @asort($j);

        }

        $t = 0;

        if ($i != 0) {

            foreach ($j as $key => $val) {

                $keysort[$t] = $val;

                $t++;

            }

        }

        $r = count($keysort);

        $g = 0;

        for ($a = $r - 1; $a >= 0; $a--) {

            $e[$g] = $keysort[$a];

            $g++;

        }

        closedir($handle);



        $this->pagename = 'index.php?module=dbackup'; // Attribute of Page Name

        $this->configObj->glTplObj->set('fileziparray', $e);

        $this->Generateurl();

        $view = 'Database Backup List';

        $actionurl = 'modules/dbackup/html/dbbackup/backup_dbs.php';

        $searchArray = array("var_title" => "Category Title");

        $table = DB_PREFIX . 'category';

        $parmas = $this->org_generateParam($view, $actionurl, $searchArray, $table);



        $this->configObj->glTplObj->set('headerpanel', $this->configObj->glLibraryObj->generateHeaderPanel($parmas, 'no'));

        $this->configObj->glTplObj->set('pagingTop', $this->configObj->glLibraryObj->generatePagingPannel_zipbackupdata($parmas));



        // Set the sorting image

        $tmpsetsortvar = 'setsortimg' . $this->orderby;

        $$finalsortvar = $tmpsetsortvar;

        $this->configObj->glTplObj->set($$finalsortvar, $this->sortvar);



        // End of Set the sorting image

        if ($_REQUEST['msg']) {

            if ($_REQUEST['msg'] == 'add') {

                $messagebox = $this->configObj->glLibraryObj->message("The record is successfully saved");

                $this->configObj->glTplObj->set('messagebox', $messagebox);

            } else if ($_REQUEST['msg'] == 'edit') {

                $messagebox = $this->configObj->glLibraryObj->message("The record is successfully edited!");

                $this->configObj->glTplObj->set('messagebox', $messagebox);

            }

        }



        if ($this->configObj->isAjaxSet) {

            echo $this->configObj->glTplObj->process('dbackup.tpl.php');

            exit;

        } else {

            $this->configObj->adminContentPanel = $this->configObj->glTplObj->process('dbackup.tpl.php');

        }

    }

    

    function importFiles($filezip)

    {

        $dir = "html/dbbackup/db_backups";

        $path = $this->configObj->currentModulePath . 'html/dbbackup/db_backups/'; // change the path here related to this page

        

            $fileSql=  str_replace(".bz2", "", $filezip); 



            $bz = bzopen($path.$filezip,"r");



            $contents ="";



            while(!feof($bz))

            {

                $contents.=fgets($bz);

            }

            bzclose($bz);

            $file_pointer = fopen ($path.$fileSql,"w+");

            fwrite($file_pointer,$contents);

            fclose($file_pointer);

            

            $mysqlDatabaseName ='eclipze.ky';

            $mysqlUserName ='sa';

            $mysqlPassword ='netclues@123';

            $mysqlHostName ='localhost';

            $mysqlImportFilename =$path.$fileSql;



            //DONT EDIT BELOW THIS LINE

            //Export the database and output the status to the page

            $command='mysql -h' .$mysqlHostName .' -u' .$mysqlUserName .' -p' .$mysqlPassword .' ' .$mysqlDatabaseName .' < ' .$mysqlImportFilename;

            exec($command,$output=array(),$worked);

//            switch($worked){

//                case 0:

//                    echo 'Import file <b>' .$mysqlImportFilename .'</b> successfully imported to database <b>' .$mysqlDatabaseName .'</b>';

//                    break;

//                case 1:

//                    echo 'There was an error during import. Please make sure the import file is saved in the same folder as this script and check your values:<br/><br/><table><tr><td>MySQL Database Name:</td><td><b>' .$mysqlDatabaseName .'</b></td></tr><tr><td>MySQL User Name:</td><td><b>' .$mysqlUserName .'</b></td></tr><tr><td>MySQL Password:</td><td><b>NOTSHOWN</b></td></tr><tr><td>MySQL Host Name:</td><td><b>' .$mysqlHostName .'</b></td></tr><tr><td>MySQL Import Filename:</td><td><b>' .$mysqlImportFilename .'</b></td></tr></table>';

//                    break;

//            }



            // End of code to import the database

            

             if(file_exists($path . $fileSql)){  unlink($path . $fileSql);}

            

             echo $worked; exit;







            // End of empty the database

     }



     



    function delfile($filezip) {

        $dir = "html/dbbackup/db_backups";

        $path = $this->configObj->currentModulePath . 'html/dbbackup/db_backups/'; // change the path here related to this page

        $handle = opendir($path);

        $fileSql=  str_replace(".bz2", "", $filezip); 

        

        while (($file = readdir($handle)) !== false) {

            $zipfile = substr(strrchr($file, '.'), 1);

            if ($zipfile == 'bz2') {

                if ($filezip == $file) {

                    unlink($path . $filezip);

                    if(file_exists($path . $fileSql)){

                    unlink($path . $fileSql);}

                }

            }

        }



        closedir($handle);

        exit;

    }

}

?>