<?php

namespace Neag\MainBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Session;
//use Neag\MainBundle\Controller;

class FileadminController extends ControllerAbstract
{
    
    /**
     * @Route("/fileadmin/download")
     */
    public function downloadAction()
    {
        if (isset($_GET['fileadmin']['file']) && isset($_GET['fileadmin']['dir']) && !empty($_GET['fileadmin']['session_key'])) {
            $globals = $this->get('globals');
            $session = $globals->getSession(); 
            if ($session->has($_GET['fileadmin']['session_key'])) {
                $session_vars = $session->get($_GET['fileadmin']['session_key']);                
                if (empty($session_vars)) {
                    throw $this->createNotFoundException('Ошибка данных сессии');
                }                
                $dir = trim($session_vars['CDIR'], '/').'/';
                //throw $this->createNotFoundException('dir: '.$_GET['fileadmin']['dir']);
                //throw $this->createNotFoundException(print_r($session_vars, true));
                if (empty($dir)) {
                    $filepath = $session_vars['base_path'].$_GET['fileadmin']['file'];
                } else {
                    $filepath = $session_vars['base_path'].$dir.$_GET['fileadmin']['file'];
                }
                if (@file_exists($filepath)) {
                    if (is_readable($filepath)) {
                        $info = pathinfo($filepath);
                        //throw $this->createNotFoundException('chp 1');
                        //throw $this->createNotFoundException(print_r($info, true));
                        $buffer = file_get_contents($filepath);
                        $response = new Response(); 
                        $response->headers->set('Pragma', 'public');
                        $response->headers->set('Expires', '0');
                        $response->headers->set('Cache-Control', 'must-revalidate, post-check=0, pre-check=0');
                        $response->headers->set('Content-Type', 'application/force-download');
                        $response->headers->set('Content-Disposition', 'attachment; filename="'.$_GET['fileadmin']['file'].'"');
                        $response->headers->set('Content-Transfer-Encoding', 'binary');
                        $response->headers->set('Content-Length', strlen($buffer));
                        $response->setContent($buffer);
                        return $response;
                    } else {
                        throw $this->createNotFoundException('Не удаётся прочесть содержимое файла');
                    }
                } else {
                    throw $this->createNotFoundException('Запрошенный файл не найден: '.$filepath);
                }
            } else {
                //throw $this->createNotFoundException('empty arg');
            }
        }
        //throw $this->createNotFoundException(print_r($_GET, true));
        throw $this->createNotFoundException('Ошибка данных');
    }       

}

?>
