<?php
class Hooks extends CI_Controller
{
		var $working_path;
    public function __construct()
    {
				parent::__construct();
				$this->working_path = getcwd();
    }

    public function index()
    {
			echo 'Hooks';
    }

		public function gitPullInstant()
		{
			$url = base_url()."hooks/gitPull";
	
			$curl = curl_init();
			$post['key'] = '065693a0-d52f-9256-47f9-01f415aa23f8'; // our data todo in received
			curl_setopt($curl, CURLOPT_URL, $url);
			// curl_setopt ($curl, CURLOPT_POST, TRUE);
			// curl_setopt ($curl, CURLOPT_POSTFIELDS, $post); 
		
			curl_setopt($curl, CURLOPT_USERAGENT, 'api');
		
			curl_setopt($curl, CURLOPT_TIMEOUT, 1); 
			curl_setopt($curl, CURLOPT_HEADER, 0);
			curl_setopt($curl, CURLOPT_RETURNTRANSFER, false);
			curl_setopt($curl, CURLOPT_FORBID_REUSE, true);
			curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 1);
			curl_setopt($curl, CURLOPT_DNS_CACHE_TIMEOUT, 10); 
		
			curl_setopt($curl, CURLOPT_FRESH_CONNECT, true);
		
			$res = curl_exec($curl);   
		
			curl_close($curl);  
	
		}

    public function gitPush()
    {
        $res = array();
        $commit_msg = 'server commit '.date('Ymd H:i:s');
        $command = 'cd '.$this->working_path.' && git add . && git commit -m "'.$commit_msg.'" && git push origin master 2>&1';
        $output = shell_exec($command);
        $exploded_output = explode(PHP_EOL, $output);
        foreach($exploded_output as $dx)
        {
            $res[] = $dx;
        }

				header('Content-Type: application/json');
        echo json_encode($res);
    }

    public function gitPull()
    {
				$res = array();
				$command = 'cd '.$this->working_path.' && git pull origin master 2>&1';
				
        $output = shell_exec($command);
        $exploded_output = explode(PHP_EOL, $output);
        foreach($exploded_output as $dx)
        {
            $res[] = $dx;
        }

				header('Content-Type: application/json');
        echo json_encode($res);
    }

    public function gitLog()
    {
			$log_size = $this->input->post('log_size');
			$log_output = '';
			if($log_size)
			$log_output = '-'.$log_size;
        $res = array();
        $output = shell_exec('cd '.$this->working_path.' && git log '.$log_output.' 2>&1');
        $exploded_output = explode(PHP_EOL, $output);
        foreach($exploded_output as $dx)
        {
            $res[] = $dx;
        }

				header('Content-Type: application/json');
        echo json_encode($res);
		}
		
    public function gitStash()
    {
        $res = array();
        $output = shell_exec('cd '.$this->working_path.' && git stash 2>&1');
        $exploded_output = explode(PHP_EOL, $output);
        foreach($exploded_output as $dx)
        {
            $res[] = $dx;
        }

				header('Content-Type: application/json');
        echo json_encode($res);
    }
		
    public function deleteCache()
    {
				$this->load->database();
        $res_db = $this->db->cache_delete_all();
				$res_view = $this->output->delete_cache();
				$res['db'] = $res_db;
				$res['view'] = $res_view;
				header('Content-Type: application/json');
        echo json_encode($res);
    }
}
