<?php
/***************************************************************************
 *                                                                          *
 * Copyright (c) 2005 ISPG Technologies (I) Pvt Ltd. All rights reserved.   *
 *                                                                          *
 * This  is  commercial  software,  only  users  who have purchased a valid *
 * license  and  accept  to the terms of the  License Agreement can install *
 * and use this program.                                                    *
 *                                                                          *
 ****************************************************************************
 * PLEASE READ THE FULL TEXT  OF THE SOFTWARE  LICENSE   AGREEMENT  IN  THE *
 * "copyright.txt" FILE PROVIDED WITH THIS DISTRIBUTION PACKAGE.            *
 ****************************************************************************/ //
//$Id: CacheControllerController.php, v 1.0 2011/06/14 20:52:11
//
class CacheController extends Controller {
    public function indexAction() {
        $ObjStat        = new AnalyticsModel();
        $ObjCommonModel = new CommonModel();
        if (__ANALYTICS_MODULE) {
            $rowsToClear = $ObjStat->searchRowsCountToDel();
        }
        if (isset($_POST['cleanLog'])) {
            $this->cleanLog($rowsToClear);
        } else if (isset($_POST['cleanSession']) && __ANALYTICS_MODULE) {
            $this->cleanSession();
        } else if (isset($_POST['cleanTemp'])) {
            $this->cleanTemp();
        } else if (isset($_POST['cleanCompile'])) {
            $this->cleanCompile();
        }
        $this->ObjView->assign("analyticsEnabled", __ANALYTICS_MODULE);
        $this->ObjView->assign("rowsToClean", $rowsToClear['rowscount']);
        $this->ObjView->assign("rowsToCleanDate", $rowsToClear['txt']);
        $this->ObjView->display('CleanLogs.tpl');
    }
    //FUNCTION TO CLEAN URL TABLE THAT ARE ALREADY PROCESSED ON CRONE EXECUTION
    public function cleanLog($rowsToClear) {
        $ObjStat = new AnalyticsModel();
        if ($rowsToClear > 0) {
            $deleteUnwantedRows = $ObjStat->deleteUnwantedSearchRows();
            $this->setMessage("language.msgRowsDelated", "message_success");
        } else {
            $this->setMessage("language.msgNothingToDelete", "error");
        }
        $this->setRedirect("cache/index/");
    }
    //FUNCTION TO CLEAR SESSION FOLDER
    public function cleanSession() {
        $folderPath = __CORE_PUBLIC_PATH . "/uploads/ses/"; //ses FOLDER PATH
        $today      = date("d-m-Y");
        $saveDate   = date("d-m-Y", strtotime($today . "-1 Day"));
        //CHECK WHETHER THE FOLDER EXISTS OR NOT
        if (is_dir($folderPath)) {
            //OPEN THE FOLDER SPECIFIED IN THE PATH
            if ($handle = opendir($folderPath)) {
                $count = 0; //COUNT NUMBER OF FILES IN THE FOLDER  					
                while (false !== ($entry = readdir($handle))) {
                    if ($entry != "." && $entry != "..") {
                        $imagePathPublic  = $folderPath . $entry;
                        $filelastmodified = filemtime($imagePathPublic); //FILE CREATED DATE
                        $latModDateForm   = date("d-m-Y", $filelastmodified); //FILE CREATED DATE IN DATE FORMAT
                        if ((strtotime($latModDateForm) < strtotime($saveDate))) {
                            if ($entry != ".gitignore") {
                                $count = $count + 1;
                                unlink($imagePathPublic); //DELETE FILES	
                            }
                        }
                    }
                }
                closedir($handle); //CLOSE THE OPENED FOLDER
            }
        } else {
            $this->setMessage("language.msgFolderNotFound", "error");
        }
        if ($count > 0) {
            $msgCntent = $count . "language.msgTempDeleted";
            $this->setMessage($msgCntent, "message_success");
        } else {
            $this->setMessage("language.msgTempNotDeleted", "error");
        }
        $this->setRedirect();
    }
    //FUNCTION TO CLEAR TEMP FOLDER 
    //Cleaned only one day old files
    public function cleanTemp($folderPath = "") {
        if (!$folderPath)
            $folderPath = __CORE_PUBLIC_PATH . "/uploads/temp/";
        //temp FOLDER PATH
        $today    = date("d-m-Y");
        $saveDate = date("d-m-Y", strtotime($today . "-1 Day"));
        //CHECK WHETHER THE FOLDER EXISTS OR NOT
        if (is_dir($folderPath)) {
            //OPEN THE FOLDER SPECIFIED IN THE PATH
            if ($handle = opendir($folderPath)) {
                $count = 0; //COUNT NUMBER OF FILES IN THE FOLDER  					
                while (false !== ($entry = readdir($handle))) {
                    if ($entry != "." && $entry != "..") {
                        $imagePathPublic  = $folderPath . $entry;
                        /* 					echo $imagePathPublic;
                        echo "<br>"; */
                        $filelastmodified = filemtime($imagePathPublic); //FILE CREATED DATE
                        $latModDateForm   = date("d-m-Y", $filelastmodified); //FILE CREATED DATE IN DATE FORMAT
                        if ((strtotime($latModDateForm) < strtotime($saveDate))) {
                            if ($entry != ".gitignore" && !is_dir($entry)) {
                                $count = $count + 1;
                                unlink($imagePathPublic); //DELETE FILES	
                            } else if ($entry != ".gitignore" && is_dir($entry)) {
                                $folderPath = __CORE_PUBLIC_PATH . "/uploads/temp/thumb/";
                                $this->cleanTemp($folderPath);
                            }
                        }
                    }
                }
                closedir($handle); //CLOSE THE OPENED FOLDER
            }
        } else {
            $this->setMessage("language.msgFolderNotFound", "error");
        }
        if (isset($count) && $count > 0) {
            $msgCntent = $count . "language.msgTempDeleted";
            $this->setMessage($msgCntent, "message_success");
        } else {
            $this->setMessage("language.msgTempNotDeleted", "error");
        }
        $this->setRedirect();
    }
    public function cleanCompile() {
        $exceptCases   = array(
            '.',
            '..',
            '.gitignore',
            'common'
        );
        $allViewPath   = __CORE_PUBLIC_PATH . "/default/" . __DIR_FRONTEND . "/";
        $themes        = scandir($allViewPath);
        $tempFileCount = 0;
        //Each theme directory except common
        foreach ($themes as $theme) {
            if (!in_array($theme, $exceptCases)) {
                $cacheMainDirectories = $allViewPath . $theme . "/compiled/cache/";
                if (is_dir($cacheMainDirectories)) {
                    //Each cache directory
                    $cacheSubDirecties = scandir($cacheMainDirectories);
                    foreach ($cacheSubDirecties as $cacheDir) {
                        if (!in_array($cacheDir, $exceptCases)) {
                            if (is_dir($cacheMainDirectories . $cacheDir)) {
                                //Each Language Directory
                                $cacheLanguageDirectories = scandir($cacheMainDirectories . $cacheDir);
                                foreach ($cacheLanguageDirectories as $cacheLanguageDirectory) {
                                    if (!in_array($cacheLanguageDirectory, $exceptCases)) {
                                        if (file_exists($cacheMainDirectories . $cacheDir . "/" . $cacheLanguageDirectory)) {
                                            unlink($cacheMainDirectories . $cacheDir . "/" . $cacheLanguageDirectory);
                                            $tempFileCount++;
                                        }
                                    }
                                }
                            } else {
                                if (file_exists($cacheMainDirectories . $cacheDir)) {
                                    unlink($cacheMainDirectories . $cacheDir);
                                    $tempFileCount++;
                                }
                            }
                        }
                    }
                } else {
                    $msgCntent = $cacheMainDirectories . " " . $this->getLanguageValue("msgFolderNotFound");
                    $this->setMessage($msgCntent, "error");
                }
            }
        }
        if ($tempFileCount > 0) {
            $msgCntent = $tempFileCount . " " . $this->getLanguageValue("msgTempDeleted");
            $this->setMessage($msgCntent, "message_success");
        } else {
            $this->setMessage("language.msgTempNotDeleted", "error");
        }
        $this->setRedirect();
    }
}