<?php 
include("includes/FCKeditor/fckeditor.php");
$file_name		= "index?page=alter";
$form_action	= "insert";
$query_string = "";
$page_name      = "Add Alter";


################################################## START INSERT ##################################################
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'insert'){	
	utility::addslashesOnAllInput(array("query_string"));
      
	$alter = stripcslashes($_REQUEST['alter']);
	$alter = str_ireplace(["\n", "\r"], [' ', ' '], $alter);
	
	if (preg_match('/(?:DELETE|DROP|TRUNCATE)\s+\S+/i', $alter)) {
		$_SESSION['info_message'] = "Error: DELETE, DROP, and TRUNCATE queries are not allowed. Please remove these queries and try again.";
	}else if (preg_match('/SELECT(?:.|\s)+FROM\s+`?([^\s`]+)`?/i', $alter, $matches)) {
		$tableName = isset($matches[1]) ? $matches[1] : '';
		header("Location: index?page=tables_result&table_name=" . $tableName . "&query=" . urlencode($alter));
		exit();
	}else{
		$result = mysqli_multi_query($conn,$alter);
		$pattern = '/(?:ALTER|CREATE)\s+TABLE\s+(?:IF\s+EXISTS\s+)?`([^`]+)`/i';
		preg_match($pattern, $alter, $matches);
		$tableName = isset($matches[1]) ? $matches[1] : '';
		if($tableName!=''){
			$view_table='<b>View Table : <a href="index?page=tables&table_name='.$tableName.'" target="_blank">'.$tableName.'</a></b>';
		}else{
			$view_table='';
		}
		if (!$result) {
			$_SESSION['info_message'] = "Error : " .mysqli_error($conn)." ".$view_table;
		} else {
			$_SESSION['info_message'] = "Alter Executed sucessfully. ".$view_table;
		}
	}
	header("Location: $file_name");
    exit();
}
################################################# END INSERT #################################################
?>
<style>
em{color:red;font-size: 20px;position: absolute;}
</style>
<div class="content-header">
<div class="container-fluid">
	<div class="row mb-2">
		<div class="col-sm-12">
			<h1 class="m-0" style="display: inline-block;">Alter</h1>
			<div style="float: right; margin: -5px 4px 1px 18px; font-size: 17px;">
				<a href="index?page=database">
					<i class="fa fa-list"></i> <span>Database</span>
				</a>
			</div>
		</div>
	</div>
	<!-- /.row -->
</div>
<!-- /.container-fluid -->
</div>

<section class="content">
	<div class="row">
		<div class="col-12">
			<!----------------------- Message ------------------------------->
			<? if(isset($_SESSION['info_message'])){ ?>
			  <div class="alert alert-info alert-dismissable">
				<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
				<? echo $_SESSION['info_message']; unset($_SESSION['info_message']);?>
			  </div>
			  <? } ?>
			<!----------------------- End Message ------------------------------->
			<!-- general form elements -->
              <div class="card">
                <form method="post" name="form1" id="form1" action="index?page=alter&action=<? echo $form_action; ?>" onsubmit="return validateForm(this)" enctype="multipart/form-data">
                  <div class="card-body">
					<div class="row">
						<div class="col-lg-12 col-sm-12">
							<div class="form-group">
							  <label for="Alter" class="control-label">Alter</label>
							  <div class="">
								<textarea class="form-control" id="alter" name="alter" rows="15" placeholder="Alter"></textarea>
							  </div>
							</div>
						</div>
					</div>
                  </div><!-- /.box-body -->
                  <div class="card-footer">
					<input type="hidden" id="id" name="id" value="<?php echo $id;?>">
					<input type="hidden" id="from" name="from" value="<?php echo $from;?>">
					<input type="hidden" id="query_string" name="query_string" value="<?php echo $query_string;?>">
                    <button type="submit" id="submit" name="submit" value="<?php echo $button_value;?>" class="btn btn-primary">Submit
						<i class="fas fa-sync-alt fa-spin" style="display:none;" id="page_load_div"></i>
					</button>
                  </div>
                </form>
              </div><!-- /.box -->
		</div>
	</div>
</section>
<script type="text/javascript">
//CKEDITOR.replace('alter');  
$(function(){
	
});
</script>