<?php
class FichierController extends Zend_Controller_Action
{

	public function init()
	{
		$this->_helper->layout()->disableLayout();
		$this->_helper->viewRenderer->setNoRender(true);
	}

	public function indexAction(){}

	/**
	 * Gestion du chargement des logos
	 */
	public function logoAction()
	{
		$file = $this->getRequest()->getParam('fichier');
		$explode = explode('.', $file);
        switch ($explode[1]) {
        	case 'png' : $content_type = 'image/png';  break;
        	case 'gif' : $content_type = 'image/gif';  break;
        	case 'jpeg':
        	case 'jpg' : $content_type = 'image/jpeg'; break;
        }
        $c = Zend_Registry::get('config');
        $path = realpath($c->profil->path->data).'/logos/';
        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);
		} else {
			echo 'Impossible de charger le logo.';
		}
	}

	/**
	 * Gestion du chargement des images du cache
	 */
	public function imgcacheAction()
	{
		$content_type = 'image/png';
		$c = Zend_Registry::get('config');
		$path = $c->profil->path->pages.'/imgcache/';
		$file = $this->getRequest()->getParam('fichier');
    	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);
		} else {
			echo 'Impossible de charger le fichier.';
		}
	}

	/**
	 * Gestion du chargement des fichiers des marques
	 */
	public function marqueAction()
	{
		$content_type = 'application/pdf';
	    $c = Zend_Registry::get('config');
		$path = realpath($c->profil->path->data).'/marques/';
		$file = $this->getRequest()->getParam('fichier');
		if(file_exists($path.$file) && filesize($path.$file)>0) {
			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);
		} else {
			echo "Erreur lors de l'affichage du fichier.";
		}
	}

	/**
	 * Gestion des fichiers Actes et Bilans
	 */
	public function pdfAction()
	{
	    $file = $this->getRequest()->getParam('fichier');
	    $content_type = 'application/pdf';
	    $c = Zend_Registry::get('config');
	    //bilan
	    if (preg_match('/^bilan-[0-9]{9}-(consolides|sociaux)-([0-9]{8})/', $file, $matches)) {
	        $path = realpath($c->profil->path->data).
	        	'/greffes/bilans/'.$matches[1].'/'.substr($matches[2],0,4).'/';
	    }
	    //acte
	    else if (preg_match('/^acte-[0-9]{9}-(.*)-([0-9]{8})-.*-.*-.*-.*-.*-.*$/', $file, $matches)) {
	        $type = $matches[1];
	        $date = $matches[2];
	        $annee = substr($date,0,4);
	        $mois = substr($date,4,2);
	        $path = realpath($c->profil->path->data).'/greffes/actes/'.$annee.'/'.$mois.'/';
	    }

	    Zend_Registry::get('firebug')->info($path);

		if(file_exists($path.$file) && filesize($path.$file)>0) {
			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);
		} else {
			echo "Erreur lors de l'affichage du fichier.";
		}
	}

	/**
	* Gestion des fichiers Actes et Bilans
	*/
	public function pdfassociationAction()
	{
	    $content_type = 'application/pdf';
	    $c = Zend_Registry::get('config');
	    $path = realpath($c->profil->path->data).'/association/';
		$file = $this->getRequest()->getParam('fichier');
		$type = $this->getRequest()->getParam('type');
		$file = $type.'/'.$file;
		if(file_exists($path.$file) && filesize($path.$file)>0) {
		    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);
		} else {
			echo "Erreur lors de l'affichage du fichier.";
		}
	}

	/**
	 * Gestion des liasses au format excel
	 */
	public function liasseAction()
	{
	    $content_type = 'application/vnd.ms-excel';
	    $c = Zend_Registry::get('config');
	    $path = realpath($c->profil->path->files).'/';
	    $file = $this->getRequest()->getParam('fichier');
		//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);
		} else {
			echo 'Impossible de charger le fichier.';
		}
	}

	/**
	 * Gestion des log de consommation
	 */
	public function consommationAction()
	{
		$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);
		} else {
			echo 'Impossible de charger le fichier.';
		}
	}

	/**
	 * Export du portefeuille au format CSV
	 */
	public function portefeuilleAction()
	{
		$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);
		} else {
			echo 'Impossible de charger le fichier.';
		}
	}

	/**
	 * Export de la liste des surveillances au format CSV
	 */
	public function surveillanceAction()
	{
		$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);
		} else {
			echo 'Impossible de charger le fichier.';
		}
	}

	/**
	 * Gestion des fichiers bilan saisie par les clients
	 */
	public function bilanclientAction()
	{
		$c = Zend_Registry::get('config');
		$path = realpath($c->profil->path->data).'/bilanclient/';
		$file = $this->getRequest()->getParam('fichier');
		$explode = explode('.', $file);
		switch ($explode[1]) {
		    case 'pdf' : $content_type = 'application/pdf';  break;
		    case 'tiff' : $content_type = 'image/tiff'; break;
		}
		if ( file_exists($path.$file) && filesize($path.$file)>0 ) {
		    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);
		} else {
			echo "Erreur lors de l'affichage du fichier.";
		}
	}

	/**
	 * Gestion des kbis
	 */
	public function kbisAction()
	{
	    $content_type = 'application/pdf';
	    $file = $this->getRequest()->getParam('fichier');
		$c = Zend_Registry::get('config');
	    $path = realpath($c->profil->path->files).'/';
		if ( file_exists($path.$file) && filesize($path.$file)>0 ) {
		    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);
		} else {
			echo "Erreur lors de l'affichage du fichier.";
		}
	}

	/**
	 * Gestion des rapports personnalisés
	 */
	public function customrapportAction()
	{
	    $content_type = 'application/pdf';
	    $c = Zend_Registry::get('config');
		$path = realpath($c->profil->path->pages).'/';
		$file = $this->getRequest()->getParam('fichier');
		$output_file = $directory.'/'.$file;
		if ( file_exists($path.$file) && filesize($path.$file)>0 ) {
		    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);
		} else {
			echo "Impossible de charger le fichier.";
		}
	}

	/**
	 * Gestion bodacc au format PDF
	 */
	public function bodaccAction()
	{
	    $content_type = 'application/pdf';
	    $c = Zend_Registry::get('config');
		$path = realpath($c->profil->path->data).'/bodacc/';
		$file = $this->getRequest()->getParam('fichier');
		//Construire le chemin du dossier
		preg_match('/BODACC_(A|B|C)_([0-9]{4})_(.*)\.pdf/', $file, $matches);
		$path.= $matches[1].'/'.$matches[2].'/';
		if ( file_exists($path.$file) && filesize($path.$file)>0 ) {
		    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);
		} else {
			echo "Impossible de charger le fichier.";
		}
	}

	/**
	 * Gestion des fichiers PDF des nouveautés
	 */
	public function newAction()
	{
	    $content_type = 'application/pdf';
		$c = Zend_Registry::get('config');
		$path = realpath($c->profil->path->data).'/nouveautes/';
		$file = $this->getRequest()->getParam('fichier');
		if ( file_exists($path.$file) && filesize($path.$file)>0 ) {
		    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);
		} else {
			echo "Impossible de charger le fichier.";
		}
	}

	/**
	 * Bodacc history file
	 */
	public function histopdfAction()
	{
	    $content_type = 'application/pdf';
		$file = $this->getRequest()->getParam('fichier');
		$c = Zend_Registry::get('config');
	    $path = realpath($c->profil->path->files).'/';
		if ( file_exists($path.$file) && filesize($path.$file)>0 ) {
		    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);
		} else {
			echo "Impossible de charger le fichier.";
		}
	}

	/**
	 * Internal files for groupes
	 */
	public function groupesAction()
	{
	    $content_type = 'application/pdf';
		$file = $this->getRequest()->getParam('fichier');
		$c = Zend_Registry::get('config');
		$path = realpath($c->profil->path->data).'/groupes/';
		if ( file_exists($path.$file) && filesize($path.$file)>0 ) {
		    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);
		} else {
		    echo "Impossible de charger le fichier.";
		}
	}


	/**
	 * Bilan : Association
	 */
	public function greffeAction()
	{
	    $content_type = 'application/pdf';
	    $file = $this->getRequest()->getParam('fichier');
	    $c = Zend_Registry::get('config');
		$path = $c->profil->path->files.'/';
		Zend_Registry::get('firebug')->info($path.$file);
	    if ( file_exists($path.$file) && filesize($path.$file)>0 ) {
	        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);
	    } else {
	        echo "Impossible de charger le fichier.";
	    }
	}

	/**
	 * Façade d'immeuble
	 */
	public function streetviewAction()
	{
		$file = $this->getRequest()->getParam('fichier');
		$explode = explode('.', $file);
		switch ($explode[1]) {
			case 'png' : $content_type = 'image/png';  break;
			case 'gif' : $content_type = 'image/gif';  break;
			case 'jpeg':
			case 'jpg' : $content_type = 'image/jpeg'; break;
		}
		$c = Zend_Registry::get('config');
		$path = realpath($c->profil->path->data).'/google/streetview/';
		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);
		} else {
			echo file_get_contents(APPLICATION_PATH.'/../public/themes/default/images/1x1.png');
		}
	}

}