<?php

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

class Download extends MX_Controller {

    public function __construct() {
        parent::__construct();
        $user = $this->session->userdata('userInfo');
        $this->vservices->setApiUrl($this->config->item('api_url'));
        $this->vservices->setConnection($this->curl);
        $this->vservices->setUserId($user['us_id']);
    }

    public function getFile() {
        $user = $this->session->userdata('userInfo');
        $data = $this->input->post('data');
        $treeArray = json_decode($data, TRUE);

        if ((count($treeArray) == 1) && ($treeArray[0]['type'] == 'file')) {
            $file = $treeArray[0];
            $local_file = "downloads/" . $file['name'];
            $download_file = $file['name'];
            $filecontent = file_get_contents($file['fileurl']);
            file_put_contents($local_file, $filecontent);
        } else {
            $download_file=$user['username']."_".date("d-m-Y");
            $folder = "downloads/" . $download_file;
            mkdir($folder, 0777);
            foreach ($treeArray as $tree)
            {
                if($tree['type']=='file')
                {
                    $path=$folder."/".$tree['name'];
                    $filecontent = file_get_contents($file['fileurl']);
                    file_put_contents($path, $filecontent);
                }
                if($tree['type']=='directory')
                {
                    mkdir($folder."/".$tree['name']);
                    $childDirs = $this->aasort($tree['childDirs'],"id");
                    foreach ($childDirs as $index=>$dir)
                    {
                        if($dir['parentID']==$tree['id'])
                        {
                           
                        $path=$folder."/".$tree['name']."/".$dir['name'];
                        mkdir($path,0777);
                        $childDirs[$index]['path']=$path;
                        }
                        else
                        {
                            foreach ($childDirs as $index2=>$dir2)
                            {
                                if($dir2['id']==$dir['parentID'])
                                {
                                   $path=$childDirs[$index2]['path']."/".$dir['name'];
                                   mkdir($path,0777);
                                   $childDirs[$index]['path']=$path;
                                }
                            }
                        
                        }
                    }
                    $childFiles = $tree['childFiles'];
                    foreach ($childFiles as $index=>$file)
                    {
                        if ($file['parentID']==$tree['id'])
                        {
                            $path=$folder."/".$tree['name']."/".$file['name'];
                            
                            $filecontent = file_get_contents($file['fileurl']);
                            file_put_contents($path, $filecontent);
                        }
                        else
                        {
                            foreach ($childDirs as $index2=>$dir2)
                            {
                                if($dir2['id']==$file['parentID'])
                                {
                                   $path=$childDirs[$index2]['path']."/".$file['name'];
                                   $filecontent = file_get_contents($file['fileurl']);
                                   file_put_contents($path, $filecontent);
                                }
                            }
                        }
                    }
                }
            }
            $local_file = $folder.".zip";
            $download_file = $download_file.".zip";
            exec("zip -r ".$folder.".zip"." $folder/*");
            
        }

        header('Cache-control: private');
        header('Content-Type: application/zip');
        header('Content-Length: ' . filesize($local_file));
        header('Content-Disposition: filename=' . $download_file);
        $download_rate = 512;

        // flush content
        flush();

        // open file stream
        $file = fopen($local_file, "r");

        while (!feof($file)) {

            // send the current file part to the browser
            print fread($file, round($download_rate * 1024));

            // flush the content to the browser
            flush();

            // sleep one second
            sleep(1);
        }

        // close file stream
        fclose($file);

        exit;
    }

    public function aasort($array, $key) {
        $sorter = array();
        $ret = array();
        reset($array);
        foreach ($array as $ii => $va) {
            $sorter[$ii] = $va[$key];
        }
        asort($sorter);
        foreach ($sorter as $ii => $va) {
            $ret[$ii] = $array[$ii];
        }
        $array = $ret;
        return $array;
    }

}

/* End of file privatecontent.php */
/* Location: ./application/modules/ajax/controllers/privatecontent.php */