<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Update_dl_ekstract_tri extends CI_Controller {
	
	function index(){
		
		if($this->session->userdata('is_logged_in')==FALSE){
			redirect('login');
		}

		if($this->session->userdata('idgroup')>1){
			show_404();
		}
		$data['page_title'] = 'UPDATE APLIKASI SIPP';
		$data['main_body'] = 'update/extract_v_tri';
		$data['app_ver'] = $this->session->userdata('app_version');
		$this->load->vars($data);
		$this->load->view('header');
		$this->load->view('body/body_new');
	}

	

	function download(){
		$url = "http://103.16.79.19/Archive.zip";
		$zipFile = ('resources/download/update_sipp.zip');
		$this->donloat_file($url, $zipFile);
		/*
		$this->load->helper('download');
		if ($this->input->post('url')!=''){
			$url=$this->input->post('url');
		}else{
			echo 'Error :- isi path file update dari server ma';
			return;
		};
		if ($this->input->post('extract_to')!=''){
			$extractPath=$this->input->post('extract_to');
		}else{
			echo 'Error :- isi extract path';
			return;
		};
		$url = "http://103.16.79.19/Archive.zip";
		//$url = "http://google.com";
		$zipFile = ('resources/download/update_sipp.zip');// Local Zip File Path
		$this->donloat_file($url, $zipFile);
		$this->ekstrak2($zipFile, $extractPath);*/
	}

	function donloat_file($url, $zipFile){
		set_time_limit(0);
		$this->nativesession->set('update_download_progress', 0);
		$zipResource = fopen($zipFile, "w");
		// Get The Zip File From Server
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_FAILONERROR, true);
		curl_setopt($ch, CURLOPT_HEADER, 0);
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
		curl_setopt($ch, CURLOPT_AUTOREFERER, true);
		curl_setopt($ch, CURLOPT_BINARYTRANSFER,true);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt( $ch, CURLOPT_NOPROGRESS, false );
		curl_setopt( $ch, CURLOPT_PROGRESSFUNCTION, 'progressCallback' );
		curl_setopt($ch, CURLOPT_FILE, $zipResource);
		curl_setopt($ch, CURLOPT_TIMEOUT, 300);
		$page = curl_exec($ch);
		if(!$page) {
				echo "Error :- ".curl_error($ch);
				//echo 'Error :- file tidak ditemukan';
				return;
			
		};
		curl_close($ch);
		fclose($zipResource);
	}

	function downloadtri(){
		file_put_contents( 'progress.txt', '' );
		$url = "http://103.16.79.19/Archive.zip";
		$zipFile = ('resources/download/update_sipp.zip');
		$this->donloat_file($url, $zipFile);
		return;
		$targetFile = fopen('resources/download/update_sipp.zip', 'w' );

		$url ='http://103.16.79.19/Archive.zip';
		$ch = curl_init();

		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_FAILONERROR, true);
		curl_setopt($ch, CURLOPT_HEADER, 0);
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
		curl_setopt($ch, CURLOPT_AUTOREFERER, true);
		curl_setopt($ch, CURLOPT_BINARYTRANSFER,true);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_NOPROGRESS, false );
		curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, array('Update_dl_ekstract_tri', 'progressCallback' ));
		curl_setopt($ch, CURLOPT_FILE, $targetFile);
		curl_setopt($ch, CURLOPT_TIMEOUT, 300);
		curl_exec( $ch );
		fclose( $ch );
	}

	function progressCallbackTri( $download_size, $downloaded_size, $upload_size, $uploaded_size ){
		static $previousProgress = 0;
		if ( $download_size == 0 )
			$progress = 0;
		else
			$progress = round( $downloaded_size * 100 / $download_size );
		if ( $progress > $previousProgress){
			$previousProgress = $progress;
			$fp = fopen( 'progress.txt', 'a' );
			fputs( $fp, "$progress\n" );
			fclose( $fp );
		}
	}

	function progressCallback( $download_size, $downloaded_size, $upload_size, $uploaded_size ){
	    static $previousProgres;
	    if(empty($previousProgres)){
	    	$previousProgres = 0;
	    }
	    #$this->nativesession->set('update_download_progress', 0);
	    if ( $download_size == 0 )
	        $progress = 0;
	    else
	        $progress = round( $downloaded_size * 100 / $download_size );
	    
	    if ( $progress > $previousProgress)
	    {
	        $previousProgress = $progress;
	        $fp = fopen( 'resources/download/progress.txt', 'a' );
	        fputs( $fp, "$progress\n" );
	        fclose( $fp );

	        $this->nativesession->set('update_download_progress', $progress);
	        session_write_close();
	    }
	}

	function ekstrak($zipFile, $extractPath=''){
		/* Open the Zip file */
		$zip = new ZipArchive;
		//$extractPath = ('resources/tmp/');
		if($zip->open($zipFile) != "true"){
		 	echo "Error :- Unable to open the Zip File";
			$zip->close(); 
			return;
		} 
		/* Extract Zip File */
		$zip->extractTo($extractPath);
		$zip->close();
		echo "Success:- Proses Ekstrak Berhasil";
	}

	function ekstrak2($zipFile,$extractPath=''){
		//Open The File And Do Stuff
		$zipHandle = zip_open($zipFile);
		$extractPath= getcwd().'/test_ektrak/';
		echo '<ul>';
		while ($aF = zip_read($zipHandle) ) {
			$thisFileName = zip_entry_name($aF);
			$thisFileDir = dirname($thisFileName);
			//Continue if its not a file
			echo '<li>Working on '.$thisFileName.'</li>';
			if ( substr($thisFileName,-1,1) == '/') continue;
							
			//Make the directory if we need to...
			if ( !is_dir ( $extractPath.$thisFileDir ) ){
				echo '<li>Creating Directory '.$extractPath.$thisFileDir.'</li>';
				@mkdir($extractPath.$thisFileDir,0750,true);
				echo '<li>Created Directory '.$thisFileDir.'</li>';
			}
			
			//Overwrite the file
			if ( !is_dir($extractPath.$thisFileName) ) {
				echo '<li>'.$thisFileName.'...........';
				$contents = zip_entry_read($aF, zip_entry_filesize($aF));
				$contents = str_replace("\r\n", "\n", $contents);
				$updateThis = '';
								
				//If we need to run commands, then do it.
				if ( $thisFileName == 'upgrade.php' ){
					$upgradeExec = fopen ('upgrade.php','w');
					fwrite($upgradeExec, $contents);
					fclose($upgradeExec);
					include ('upgrade.php');
					unlink('upgrade.php');
					echo' EXECUTED</li>';
				}
				else
				{
					$updateThis = fopen($extractPath.$thisFileName, 'w');
					fwrite($updateThis, $contents);
					fclose($updateThis);
					unset($contents);
					echo' UPDATED</li>';
				}
			}
			
		}
		echo '</ul>';
		$updated = true;
	}

}