<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
global $configverssion_id; global $configapp_version; global $userID; global $server; global $LANG; $resellerID = getReseller();
//=== List page stuffs =============
$page = 1; $setLimit = 30;
if(isset($_GET["page"])) $page = (int)$_GET["page"];
$pageLimit = ($page * $setLimit) - $setLimit; 
$pageurl=$_SERVER['REQUEST_URI']."?";
if(count($_GET)) { $pageurl = $_SERVER['REQUEST_URI'].'&'; }
$trans = array("s=d_o" => "", "s=d_n" => "", "s=r_h" => "", "s=r_l" => "", "s=s_r" => "", "s=s_u" => "");
$pageurl = strtr($pageurl,$trans);
$pageurl = str_replace('&&','&',$pageurl);
//====End ==============
define('TITLE','Backups');
if(userRole()>1) {
	header('location: index.php');	
	exit();
}
$today = date('Y-m-d'); $time = strtotime($today.'-1 months'); $lastMonth = date("Y-m", $time); $time = strtotime($today.'-1 years'); $lastYear = date("Y", $time);
?>
<?php
if(isset($_GET['delete'])) { 
	$delete = mysqli_real_escape_string($server,$_GET['delete']); 
	$query="SELECT * FROM backups WHERE id = '$delete'"; 
	$result = mysqli_query($server,$query) or die(mysqli_error($server));  
	$row = mysqli_fetch_assoc($result); 
	$file = 'backups/'.$row['file'];	
	mysqli_query($server,"DELETE FROM `backups` WHERE `id` = '$delete'") or die(mysqli_error($server));
	unlink($file);
	logEvent($userID,'Deleted system backup with ID '.$delete);
	$_SESSION['message'] = $LANG['The selected backup(s) has been deleted from your system'];
	header('location: abackup?err=0');
}
	$added = '';	
	if(isset($_GET['sc'])) {
		$keywords = mysqli_real_escape_string($server,$_GET['sc']);
		$ser = explode(' ', $keywords);
		$clauses = array();
		foreach($ser as $look){
			$term = trim(preg_replace('/[^a-z0-9]/i', '', $look));   
			if (!empty($term)){
				 $clauses[] = " date LIKE '%$term%'";
			} else {
				 $clauses[] = " id LIKE '%%' ";
			}
		}
		$added .= ' AND ('.implode(' OR ', $clauses).') ';		
	}
	if(isset($_GET['s'])) {
		switch($_GET['s']) {
			case 'd_o': $added .= " order by date ASC ";
			break;
			case 'd_n': $added .= " order by date DESC ";
			break;
		}
	} else {
		$added .= " order by date DESC ";	
	}

    $query="SELECT * FROM backups WHERE 1 $added limit $pageLimit,$setLimit";
	$result = mysqli_query($server,$query) or die(mysqli_error($server));
	$x = mysqli_num_rows($result);
 ?>
<div class="row no-m-t no-m-b">
 	<div class="col s12 m12 l12">
        <div class="card card-transparent">
            <div class="card-content">
            	<div class="col s12 m4 l4">
                </div>
                <div class="col s12 m8 l8">
                  <div class="col s12 m12 l12">
                    <div class="row no-m-t no-m-b">
                     <form action="abackup" method="get">
                     <div class="input-field col s12">
                        <i class="material-icons prefix">search</i>
                        <input type="date" name="sc" value="<?=@$_GET['sc']?>" placeholder="<?=$LANG['Search...']?>">
                     </div>
                     </form>  
                    </div>
                  </div> 
                </div>
            </div>
        </div>
    </div>
 </div>
 
	<div class="row no-m-t no-m-b">
       <div class="col s12 m12 l12">
         <div class="card card-white">
            <div class="card-content">
            <div class="card-options">
            	<ul>
                	<li><a class='dropdown-button btn btn-flat' href='#' data-activates='filter' data-beloworigin='true' data-constrainwidth='false'><i class="fa fa-sort"></i> <?=$LANG['Sort']?></a></li>
                </ul>    
                <ul id='filter' class='dropdown-content'>
                    <li><a href="<?=$pageurl?>s=d_o" ><?=$LANG['Oldest First']?></a></li>
                    <li><a href="<?=$pageurl?>s=d_n" ><?=$LANG['Newest First']?></a></li>
                </ul>
            </div>
            <span class="card-title"></span>
       		<table class="highlight responsive-table">
                <thead>
                	<tr>
                        <th data-field="to"><?=$LANG['Date']?></th>
                        <th data-field="from"><?=$LANG['Description']?></th>
                        <th data-field="to"><?=$LANG['Size']?></th>
                        <th data-field="link"></th>
                    </tr>
                 </thead>
                 <tbody>
<?php $xf=0; while ($rows = mysqli_fetch_assoc($result)) { $xf+=1; ?>
						<tr class="">
                             <td><?=date('M j Y h:iA',strtotime($rows['date']))?></td>
                             <td><?=$LANG[$rows['type']]?></td>
                             <td><?=format_size(filesize('backups/'.$rows['file']))?></td>
                             <td><a href="<?='backups/'.$rows['file']?>" class="hoverable blue-text"><i class="fa fa-download"></i> <?=$LANG['Download Backup']?></a> &nbsp; &nbsp;<a onclick="return confirm('<?=$LANG['delete_confirm']?>');" href="abackup?delete=<?=$rows['id']?>" class="hoverable red-text"><i class="fa fa-trash"></i> <?=$LANG['Delete']?></a></td>
                        </tr>
<?php } ?>
<?php if($xf<1) echo '<tr class="orange lighten-5"><td colspan=10><h5 class="center-align">'.$LANG['No records found!'].'</h5></td></tr>' ?>	
                 </tbody>
            </table>  
          </div>
        </div>          
     </div>
  </div>
 <div class="row">
 	<div class="col s12 m12 l12" style="margin-top: 5px;">
        <?php displayPagination($setLimit,$page,$query);  ?>
    </div>
 </div>                       