<?php
ob_start(); 
	$absolute_path = __FILE__;
	$path_to_file = explode( 'wp-content', $absolute_path );
	$path_to_wp = $path_to_file[0];
	
	require_once( $path_to_wp.'/wp-load.php' );
	require_once( $path_to_wp.'/wp-includes/functions.php');
	require_once( $path_to_wp.'/wp-admin/includes/file.php');
	/*
	echo ' Import Files loaded<br />'; 
	echo ' Import Sliders:<br />'; 
	*/

 $temporal_purchased_code = get_option( 'slaido-key' );

//Get Server URL

  function get_slaido_url_zip($slaido_id, $temporal_purchased_code) {

	$ch = curl_init("http://www.bypixels.com/slaido_plugin/validation/slaido_links.php?purchase_code_input=$temporal_purchased_code&slaido_id=$slaido_id");
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                "Content-Type: application/json",
              "Authorization: Bearer Yt4ifs2hrrKcE1lkA43FO5uGsqmrlOUv")
            );

           $result   = curl_exec($ch);


if ($result != "Error") {

	$selected_slider = $result;

	install_slaido($selected_slider,$slaido_id);


}else {
	echo "Your Purchase Code Seems Invalid or there is a problem";
	
}




    
  }



$slaido_to_import =  $_GET['slaido_import'];

switch ($slaido_to_import) {
		case '2':
		

	install_slaido('http://bypixels.com/slaido_plugin/zip/slaido_89.zip',89);
		

	//get_slaido_url_zip( 24, $temporal_purchased_code);
			
		

		


		break;

		case '1':
			echo "Importando Slaido";
			$selected_slider = 'http://bypixels.com/slaido_plugin/zip/slaido_89.zip';

			$install = install_slaido($selected_slider,89);
			echo $install;
		break;

		case '21':
			echo "Importando Slaido";
			$selected_slider = 'http://bypixels.com/slaido_plugin/zip/slaido_93.zip';

			$install = install_slaido($selected_slider,93);
			echo $install;

		break;

		case '35':
			echo "Importando Slaido";
			$selected_slider = 'http://bypixels.com/slaido_plugin/zip/slaido_35.zip';

		break;

		case '44':
			echo "Importando Slaido";
			$selected_slider = 'http://bypixels.com/slaido_plugin/zip/slaido_44.zip';

		break;

		case '51':
			echo "Importando Slaido";
			$selected_slider = 'http://bypixels.com/slaido_plugin/zip/slaido_51.zip';

		break;

		case '53':
			echo "Importando Slaido";
			$selected_slider = 'http://bypixels.com/slaido_plugin/zip/slaido_53.zip';

		break;

		case '54':
			echo "Importando Slaido";
			$selected_slider = 'http://bypixels.com/slaido_plugin/zip/slaido_54.zip';

		break;

		case '65':
			echo "Importando Slaido";
			$selected_slider = 'http://bypixels.com/slaido_plugin/zip/slaido_65.zip';

		break;

		case '68':
			echo "Importando Slaido";
			$selected_slider = 'http://bypixels.com/slaido_plugin/zip/slaido_68.zip';

		break;

		case '72':
			echo "Importando Slaido";
			$selected_slider = 'http://bypixels.com/slaido_plugin/zip/slaido_72.zip';

		break;

		case '75':
			echo "Importando Slaido";
			$selected_slider = 'http://bypixels.com/slaido_plugin/zip/slaido_75.zip';

		break;

		case '77':
			echo "Importando Slaido";
			$selected_slider = 'http://bypixels.com/slaido_plugin/zip/slaido_77.zip';

		break;

		case '83':
			echo "Importando Slaido";
			$selected_slider = 'http://bypixels.com/slaido_plugin/zip/slaido_83.zip';

		break;

		case '86':
			echo "Importando Slaido";
			$selected_slider = 'http://bypixels.com/slaido_plugin/zip/slaido_86.zip';

		break;

		case '89':
			echo "Importando Slaido";
			$selected_slider = 'http://bypixels.com/slaido_plugin/zip/slaido_89.zip';

		break;

		case '90':
			echo "Importando Slaido";
			$selected_slider = 'http://bypixels.com/slaido_plugin/zip/slaido_90.zip';

		break;

		case '93':
			echo "Importando Slaido";
			$selected_slider = 'http://bypixels.com/slaido_plugin/zip/slaido_93.zip';

		break;
		case '96':
			echo "Importando Slaido";
			$selected_slider = 'http://bypixels.com/slaido_plugin/zip/slaido_96.zip';

		break;
		case '97':
			echo "Importando Slaido";
			$selected_slider = 'http://bypixels.com/slaido_plugin/zip/slaido_97.zip';

		break;
		


	
	
}



function install_slaido($selected_slider, $slaido_to_import ) {



$name_of_zip = $slaido_to_import.'token102';
$upload_dir = wp_upload_dir(); // Set upload folder
$template_path = '/slaido/templates';
$file = $upload_dir['basedir'].'/slaido/templates/'.$name_of_zip.'.zip';

echo $selected_slider; echo "<br>"; 
echo $slaido_to_import; echo "<br>";
echo $name_of_zip; echo "<br>";

echo $template_path; echo "<br>";
echo $file; echo "<br>";




		if(wp_mkdir_p( $upload_dir['basedir'].$template_path ) ) { //check here to not flood the server
			
			$request = wp_remote_post($selected_slider);

							if(!is_wp_error($request)) {

														if($response = $request['body']) {
																	if($response !== 'invalid'){
//echo "creando directorio";
																				$file = $upload_dir['basedir']. $template_path . '/'.$name_of_zip.'.zip';
																				@mkdir(dirname($file));
																				
																				$ret = @file_put_contents( $file, $response );
																				if($ret !== false){
																					//return $file so it can be processed. We have now downloaded it into a zip file
																					$return = $file;
																					echo "creando archivo";
																				}else{//else, print that file could not be written
																					$return = array('error' => __('Can\'t write the file into the uploads folder of WordPress, please change permissions and try again!', 'revslider'));
																				}

																	}else {
																		

																		$return = "Purchase Code";

																	}
														}


				         }else {
				         	$return = array('error' => __('Can\'t write into the uploads folder of WordPress, please change permissions and try again!', 'revslider'));
				         }
         //return $return;
				         echo "Slaido...";


		}



 
         $slider = new RevSlider();
		$slider->importSliderFromPost(true, true, $file); 


					
 	echo "Complete!";

  echo ob_get_clean();
  	$selected_slider ="";
   // wp_redirect( admin_url().'admin.php?page=revslider' );




}





?>