<?
/*********************************************************************
 *
 *  Wattmon
 *    
 *  File : cacheclear.cgi
 * 
 *  Description: Remove .cgc files from the various folders
 *
 *********************************************************************
 * Company:         Cynergy Software
 *
 * Software License Agreement
 *
 * Copyright (c) 2014 Cynergy Software.  All rights reserved.
 *
 * Cynergy licenses to you the right to use, modify, copy, and 
 * distribute: 
 * (i)  the Software when used on a Wattmon device
 * (ii) the Software on any platform or device for personal use
 *
 * For commercial use please contact us via http://www.wattmon.com
 *
 * THE SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS" WITHOUT 
 * WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT 
 * LIMITATION, ANY WARRANTY OF MERCHANTABILITY, FITNESS FOR A 
 * PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL 
 * CYNERGY BE LIABLE FOR ANY INCIDENTAL, SPECIAL, INDIRECT OR 
 * CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF 
 * PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY OR SERVICES, ANY CLAIMS 
 * BY THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY DEFENSE 
 * THEREOF), ANY CLAIMS FOR INDEMNITY OR CONTRIBUTION, OR OTHER 
 * SIMILAR COSTS, WHETHER ASSERTED ON THE BASIS OF CONTRACT, TORT 
 * (INCLUDING NEGLIGENCE), BREACH OF WARRANTY, OR OTHERWISE.
 *
 * Author               Date        Comment
 *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 * Akash Heimlich       23/07/14    v1.0
 ********************************************************************/
if (!$_SESSION['logged_in']) {
    print("{'status':0,'error':'Error: You must be logged in to clear the cache!'}");
    die();
}
if ($_SESSION['admin']==0) {
 //   header("Location: /index.cgi"); 
    die();     
}

if ($_SERVER['HW_PLATFORM']>=20)
    $folders=array("2:/","/cache");
else $folders=array("/","/app","/scripts","/package","/shell");
$nf=0;
for ($f=0;$f<sizeof($folders);$f++) {
    if (!chdir($folders[$f])) {
        $arr=findfirst("*.*",255);
        
        while ($arr) {
        //    print($arr['filename']."\r\n");
            if (strpos(strtolower($arr['filename']),".cgc")>0) {
                $fn=($folders[$f]=='/'?'/':$folders[$f].'/').$arr['filename'];
                //print("Found CGC File: ".$fn."\r\n");
                unlink($fn);
                $nf++;
            }
            $arr=findnext();
        }
    }
}
$_GLOBALS['sysmsg']="Cache cleared and ".$nf." files were removed.";
//header("Location: /index.cgi");
//die();
print("{\"status\":1,\"msg\":\"".$_GLOBALS['sysmsg']."\"}");
?>




