<?php
session_start();
ini_set('display_errors', 1); 
ini_set('display_startup_errors', 1); 
error_reporting(1); 


header("Content-Type: application/json;charset=utf-8"); 
include('conf.php');
//include("injection.php");
//include("encrypt.php");


mysqli_query($conn,"SET NAMES 'utf8'");
mysqli_query($conn,'SET CHARACTER SET utf8');
mysqli_query($conn,"SET character_set_client=utf8");
mysqli_query($conn,"SET character_set_connection=utf8");
mysqli_query($conn,"SET character_set_database=utf8");
mysqli_query($conn,"SET character_set_results=utf8");
mysqli_query($conn,"SET character_set_server=utf8");
mysqli_query($conn,"set character_set_server='utf8'"); 
	$new_id=0;
	$user_id='1';
	$table='form_questions';
	$operation='edit_ques';
	$auth_check='user_update';
	$token = null;
	$status='';
	$affected_rows=0;
	$error_arr=[];
	$error_database_arr=[];
	$user_auth='';
	$data_array = array();
	$headers = apache_request_headers();
	
	
	
	//include "auth.php";
	//if($auth_errors==1){
	//   goto to_result;
	//}			
			


	$raw_data = file_get_contents('php://input');
	$post_data = json_decode($raw_data, true);
	
	//$post_data['id']=str_replace(' ','+',$post_data['id']);
	//$post_data['id']=decrypt_dynamic($post_data['id'],$headers['Authorization']);
	$modified=date('Y-m-d H:i:s');


	//استدعاء ملف تخزين اللوجز والحماية من الاسبام	
	include "spam_logs.php";
	if($check_spam['block']) {
		$status='not_done';
		$message=$check['reason'] ?? 'ممنوع مؤقتًا';
		goto to_result;
	}

	//استدعاء ملف التحقق من المدخلات وكشف التلاعب
	include 'purifier.php';
	if($escape_num==3){				
		$status='not_done';
		$message='النص مشبوه';
		goto to_result;
	}
	

	  
	$post_data["ques_text"] = $purifier->purify($post_data["ques_text"]);if($post_data["ques_text"]=='' ){array_push($error_arr,'خطأ فى عنوان السؤال');$status='not_done';$message='خطأ فى المدخلات';goto to_result;}
	$post_data["ques_type"] = $purifier->purify($post_data["ques_type"]);if( $post_data["ques_type"]=='' ){array_push($error_arr,'خطأ فى نوع السؤال');$status='not_done';$message='خطأ فى نوع السؤال';goto to_result;}
	if(filter_var($post_data["ques_required"], FILTER_VALIDATE_INT )=== false || $post_data["ques_required"]==''){array_push($error_arr,'خطأ فى حقل الالزام');$status='not_done';$message='خطأ فى المدخلات';goto to_result;}
	if(filter_var($post_data["ques_show_answer"], FILTER_VALIDATE_INT )=== false || $post_data["ques_show_answer"]==''){array_push($error_arr,'خطأ فى حقل اظهار الاجابات');$status='not_done';$message='خطأ فى المدخلات';goto to_result;}
	if(filter_var($post_data["ques_level"], FILTER_VALIDATE_INT )=== false || $post_data["ques_level"]==''){array_push($error_arr,'خطأ فى حقل المستوى');$status='not_done';$message='خطأ فى المدخلات';goto to_result;}
	if(filter_var($post_data["ques_shuffle"], FILTER_VALIDATE_INT )=== false || $post_data["ques_shuffle"]==''){array_push($error_arr,'خطأ فى عشوائية الاجابات');$status='not_done';$message='خطأ فى المدخلات';goto to_result;} 	  
	if(filter_var($post_data["ques_dgree"], FILTER_VALIDATE_FLOAT )=== false || $post_data["ques_dgree"]==''){array_push($error_arr,'خطأ فى حقل درجة السؤال');$status='not_done';$message='خطأ فى المدخلات';goto to_result;}
 	if(filter_var($post_data["ques_min"], FILTER_VALIDATE_INT )=== false || $post_data["ques_min"]==''){array_push($error_arr,'خطأ فى اقل قيمة');$status='not_done';$message='خطأ فى المدخلات';goto to_result;}
	if(filter_var($post_data["ques_max"], FILTER_VALIDATE_INT )=== false || $post_data["ques_max"]==''){array_push($error_arr,'خطأ فى اعلى قيمة');$status='not_done';$message='خطأ فى المدخلات';goto to_result;}
	if(filter_var($post_data["ques_length"], FILTER_VALIDATE_INT )=== false || $post_data["ques_length"]==''){array_push($error_arr,'خطأ فى طول الحقل');$status='not_done';$message='خطأ فى المدخلات';goto to_result;}
	if(filter_var($post_data["ques_width"], FILTER_VALIDATE_INT )=== false || $post_data["ques_width"]==''){array_push($error_arr,'خطأ فى عرض الحقل');$status='not_done';$message='خطأ فى المدخلات';goto to_result;}


	  
	//حقول غير الزامية
	  
	$post_data["ques_images"] = $purifier->purify($post_data["ques_images"]);
	if(filter_var($post_data["ques_videos"], FILTER_SANITIZE_URL )=== false && $post_data["ques_videos"]!='' ){array_push($error_arr,'خطأ فى الفيديوهات');$status='not_done';$message='خطأ فى الصور والملفات';goto to_result;}


      
	  
	  //حقول غير الزامية
	 
	 if(count($error_arr) == 0){
		  //echo 'validatation success';
		  
		  
		  $query = mysqli_prepare(
			$conn,
			"Update `$table` SET 
			`ques_text` = ?  , `ques_type` = ?  ,`ques_required` = ?  ,`ques_show_answer` = ?  ,`ques_level` = ?  ,`ques_shuffle` = ?  ,`ques_dgree` = ?  , `ques_min` = ?  , `ques_max` = ?  , `ques_length` = ?  , `ques_width` = ?  , `images` = ?  ,`videos` = ?  , `modified` = ? , modified_user=? 
			 where id = ?");
		
			mysqli_stmt_bind_param($query,'ssiiiidiiiisssii',
			$post_data['ques_text'],$post_data['ques_type'],$post_data['ques_required'],$post_data['ques_show_answer'],$post_data['ques_level'],$post_data['ques_shuffle'],$post_data['ques_dgree'],$post_data['ques_min'],$post_data['ques_max'],$post_data['ques_length'],$post_data['ques_width'],$post_data['ques_images'],$post_data['ques_videos'],$modified,$user_id,$post_data['current_id']
			);
			
			$run=mysqli_stmt_execute($query);
			 
			 if(mysqli_error($conn)!=''){
				 $error_database_arr['err']=mysqli_error($conn);
				 $error_database_arr['err_num']= mysqli_errno($conn);
				 $error_database_arr['error_stmt']= mysqli_stmt_error($query);
			 }

			$affected_rows=mysqli_affected_rows($conn);

			if(mysqli_stmt_affected_rows($query)){
				
				
				
				
				if($affected_rows>0){
					
					$status='done';
					$message='تم الارسال بنجاح';
					
					log_request($conn, $post_data['current_id'] , $user_id, $operation, $post_data, 'success');
					

					
				}else{
					

					if(count($error_database_arr) == 0){
					
						$status='not_done';
						$message='لم يتم ادخال البيانات . ربما تكون مكررة';
					}else{
											
						$status='not_done';
						$message='لم يتم ادخال البيانات . ربما هناك خطأ فى محتوى الحقل <br/>'.$error_database_arr['err'];	
						
					}
				}
			}else{
				
				$status='not_done';
				$message='خطأ فى كود الادخال';
			}
			  
		 }else{
			  $status='not_done';
			  $message='خطأ فى البيانات';
			  //print_r($error_arr);
		 }
	  
	  
	to_result:
	  
	$result = array(
		//'token' => $token,
		'status' => $status,
		'message' => $message,
		'affected_rows'=>$affected_rows,
		'errors' => $error_arr,
		'errors_database' => $error_database_arr,
		'structure' => $table,
		'reload' => 'true',
		'current_text'=> $post_data['ques_text'],
		'current_type'=> $post_data['ques_type'],
		'current_required'=> $post_data['ques_required'],
		'current_show_answer'=> $post_data['ques_show_answer'],
		'current_dgree'=> $post_data['ques_dgree'],
		'current_level'=> $post_data['ques_level'],
		'current_shuffle'=> $post_data['ques_shuffle'],
		'current_images'=> $post_data['ques_images'],
		'current_videos'=> $post_data['ques_videos']
		

	);


echo json_encode($result,JSON_UNESCAPED_UNICODE);

 

?>