<?php

if (!defined('BASEPATH')) {
    exit('No direct script access allowed');
}

 
class Update extends Hosting_Billing 
{
    private $update_url;

    public function __construct()
    {
        parent::__construct();
        $this->load->helper(array('url', 'file', 'curl'));
        $this->update_url = 'http://updates.hostingbilling.net';
    }



    public function index()
    {
        $sql = "SELECT value FROM hd_config WHERE config_key = 'purchase_code'"; 
        $query = $this->db->query($sql);
        $result = $query->result();
        $data['key'] = $result[0]->value;  
        $this->load->view('update', $data);
    }



    public function verify()
    {     
        $p = array();
        $env_data = $this->_valid_script();

        $p = json_decode($env_data, true);
        if (json_decode($env_data) == null) {
            $this->session->set_flashdata('message', 'Your purchase code is Invalid. Please try again');
            redirect('update/?step=2');
        } else {
            if ($p['buyer'] != $this->input->post('set_envato_user')) {
                $this->session->set_flashdata('message', 'Your envato username does not match the buyer username');
                redirect('update/?step=2');
            }
            
            $this->db->where('config_key', 'purchase_code')->update('config', array('value' => $this->input->post('set_envato_license')));

            $this->_autoload(); 
            $this->_change_routing();
           
            if (!$this->_initialize_db(config_item('version'))) {
                $this->session->set_flashdata('message', 'Database import failed. Check if the file exists: resource/tmp/hostingbilling_'. $version .'.sql');
                redirect('update/?step=3');
            }
        }       
        redirect('update/done');  
    }
 

    public function done()
    {
        $this->load->view('installed');
    }

 
    public function _valid_script()
    {
        $purchase_code = $this->input->post('set_envato_license');
        $update_url = $this->input->post('update_url');
        return curl_get_contents($this->update_url.'/verify/index.php?code='.$purchase_code);
    }   

 

    public function _initialize_db($version = null)
    {  
        $templine = '';
        // Read in entire file
        $lines = file('./resource/tmp/incremental.sql');
        foreach ($lines as $line) {
            if (substr($line, 0, 2) == '--' || $line == '') {
                continue;
            }
            $templine .= $line;
            if (substr(trim($line), -1, 1) == ';') {
                $this->db->query($templine) or print 'Error performing query \'<strong>'.$templine.'\': '.mysql_error().'<br /><br />';
                $templine = '';
            }
        
        }
         
        return true;
    }


    public function _autoload()
    {        
        $libdata = read_file('./application/config/autoload.php');
        $libdata = str_replace(
            '$autoload[\'libraries\'] = array(\'session\',\'database\',\'tank_auth\',\'applib\');',
            '$autoload[\'libraries\'] = array(\'session\',\'database\',\'tank_auth\',\'applib\',\'modules\');',
            $libdata);
        write_file('./application/config/autoload.php', $libdata); 
    }


    public function _change_routing()
    { 
        $rdata = read_file('./application/config/routes.php');
        $rdata = str_replace('update', 'home', $rdata);
        write_file('./application/config/routes.php', $rdata);
    }


    
    private function curl_get_contents($url)
    {
        $ch = curl_init();
        $options = array(
            CURLOPT_URL => $url,
            CURLOPT_HEADER => 0,
            CURLOPT_RETURNTRANSFER => true
        );
        if (!ini_get('safe_mode') && !ini_get('open_basedir')) {
            $options[CURLOPT_FOLLOWLOCATION] = true;
        }
        curl_setopt_array($ch, $options);
  
        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
    }
  

}
 

/* End of file update.php */
