<?php
class FichierController extends Zend_Controller_Action
{
	public function init()
	{
		$this->_helper->layout()->disableLayout();
		$this->_helper->viewRenderer->setNoRender(true);
	}
	
	public function bodaccAction()
	{
		$auth = Zend_Auth::getInstance();
		if ( $auth->hasIdentity() ) {

		    $directory = '/mnt/bodacc';
			
			$q = $this->getRequest()->getParam('q');
			$file = base64_decode($q);
				
			$output_file = $directory.'/'.$file;
				
			if (file_exists($output_file) && filesize($output_file)>0) {
				$content_type = 'application/pdf';
				header('Content-type: '.$content_type.'');
				header('Content-Length: '.filesize($output_file));
				header('Content-MD5: '.base64_encode(md5_file($output_file)));
				header('Content-Disposition: inline; filename="'.basename($output_file).'"');
				header('Cache-Control: private, max-age=0, must-revalidate');
				header('Pragma: public');
				ini_set('zlib.output_compression','0');
				echo file_get_contents($output_file);
			
				//Log de la requete
				$tabInsert = array(
					'login'			=> $auth->getIdentity()->username,
					'idClient'		=> $auth->getIdentity()->idClient,
					'page'			=> 'histobodacc',
					'siren'			=> '',
					'nic'			=> '',
					'params'		=> $file,
					'test'			=> 0,
					'raisonSociale'	=> '',
					'cp'			=> '',
					'ville'			=> '',
					'ipClient'		=> $_SERVER['REMOTE_ADDR'],
				);
				require_once 'framework/common/mysql.php';
				$iDbCrm = new WDB('sdv1');
				$rep = $iDbCrm->insert('logs', array_merge($tabInsert,$tabRdvInsee), false, true);
			
			} else {
				echo "Erreur lors de l'affichage du fichier.";
			}
		} else {
		    echo "Authentification échoué.";
		}
	}

	public function logsAction()
	{
	    $file = $this->getRequest()->getParam('fichier');
	    $content_type = 'application/csv-tab-delimited-table';
	    $c = Zend_Registry::get('config');
	    $path = realpath($c->profil->path->files).'/';
	    //Envoi du fichier sur la sortie standard
	    if ( file_exists($path.$file) ) {
	        header('Content-Transfer-Encoding: none');
	        header('Content-type: ' . $content_type.'');
	        header('Content-Length: ' . filesize($path.$file));
	        header('Content-MD5: ' . base64_encode(md5_file($path.$file)));
	        header('Content-Disposition: filename="' . basename($path.$file) . '"');
	        header('Cache-Control: private, max-age=0, must-revalidate');
	        header('Pragma: public');
	        ini_set('zlib.output_compression', '0');
	        echo file_get_contents($path.$file);
	    }
	}
	
	public function kbisAction()
	{
	    $file = $this->getRequest()->getParam('fichier');
	    $content_type = 'application/csv-tab-delimited-table';
	    $c = Zend_Registry::get('config');
	    $path = realpath($c->profil->path->secure).'/kbis/';
	    //Envoi du fichier sur la sortie standard
	    if ( file_exists($path.$file) ) {
	        header('Content-Transfer-Encoding: none');
	        header('Content-type: ' . $content_type.'');
	        header('Content-Length: ' . filesize($path.$file));
	        header('Content-MD5: ' . base64_encode(md5_file($path.$file)));
	        header('Content-Disposition: filename="' . basename($path.$file) . '"');
	        header('Cache-Control: private, max-age=0, must-revalidate');
	        header('Pragma: public');
	        ini_set('zlib.output_compression', '0');
	        echo file_get_contents($path.$file);
	    }
	}
		
}