<?php
	function logAction($login, $page, $siret, $ip, $host, $resolution, $nbcolors, $user_agent, $referer, $date_login, $date_last_action, $request) {
		if (!file_exists(PATH_LOGS.'actions.log')) {
			$fp=fopen(PATH_LOGS.'actions.log', 'a');
			fwrite($fp, 'Date et Heure;Login;Page;Siret;Ip;Host;Resolution;Nbcolors;User Agent;Referer;Date login;Date last action;Request'."\n");
		} else {
			$fp=fopen(PATH_LOGS.'actions.log', 'a');
		}
		fwrite($fp, date('Y/m/d H:i:s').";$login;$page;$siret;$ip;$host;$resolution;$nbcolors;$user_agent;$referer;$date_login;$date_last_action;$request\n");
		fclose($fp);
	}

	function strWsToHtml($str) {
		return $str;
	}

	function afficheErreur($message, $refresh=5) {
		include('./pages/erreur.php');
	}

	function html2pdf($buffer) {
		/*$body=getAvisSituation($siret, 'pdf');
		header("Pragma: public");
		header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
		header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
		header("Cache-Control: must-revalidate");
		header("Content-type: application/pdf");
		header("Content-Length: ".strlen($body));
		header('Content-disposition: inline; filename="avis-'.$siret.'.pdf"');// attachement or inline
		header("Accept-Ranges: ".strlen($body));
		echo $body;*/
		global $fichierPdf;
		$fichier='printable_'.$_REQUEST['page'].'_'.$_REQUEST['siret'].$_SERVER['REMOTE_ADDR'].'_'.date('YmdHis');
		$fichierHtm = '/tmp/'.$fichier.'.htm';
		$fichierPdf = PATH_DATA.'/pdf/'.$fichier.'.pdf';

		$buffer=str_replace('<link rel="stylesheet" type="text/css" href="/',
							'<link rel="stylesheet" type="text/css" href="'.EXTRANET_URL,$buffer);
		$buffer=str_replace(' src="./',
							' src="'.EXTRANET_URL,$buffer);
		$buffer=str_replace(' src="/',
							' src="'.EXTRANET_URL,$buffer);

		$fp=fopen($fichierHtm,'w');
		fwrite($fp, $buffer);
		fclose($fp);

		exec('/usr/bin/htmldoc -t pdf14 --size A4 --no-links --webpage --compression=1 --no-toc --pagemode document --left 1.5cm --right 1.5cm -f '.$fichierPdf.' '.$fichierHtm);

		header('Content-type: application/pdf');
		header('Content-Disposition: inline; filename=doc.pdf');
		header('Last-Modified: '.gmdate('D, d M Y H:i:s') . ' GMT');
		header('Cache-Control: no-store, no-cache, must-revalidate');
		header('Cache-Control: pre-check=0, post-check=0, max-age=0');
		header('Pragma: anytextexeptno-cache', true);
		header('Cache-control: private');
		header('Expires: 0');
		/*
		header('Pragma: public');
		header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
		header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
		header('Cache-Control: must-revalidate');
		header("Content-Type: application/pdf");
		header("Content-Length: ".filesize($fichierPdf));
		header('Content-Type: application/force-download');
		header('Content-Disposition: attachment; filename="'.$_REQUEST['page'].'-'.$_REQUEST['siret'].'-'.date('Ymd').'.pdf"');
		//header("Accept-Ranges: ".filesize($fichierPdf));
		header('Accept-Ranges: bytes');
		/*
		header('HTTP/1.1 200 OK');
		header('Status: 200 OK');

		header('Accept-Ranges: bytes');
		header('Content-Transfer-Encoding: Binary');
		header("Content-Lenght: ".strval( $pdf->TailleFichierTemporaire( ) ));
		*/
		//header('Location: http://extranet"'.$NomFichier.'"');
	}

function redimage($img_src,$dst_w,$dst_h) {
   // Lit les dimensions de l'image
   $size = GetImageSize($img_src);
   $src_w = $size[0]; $src_h = $size[1];
   // Teste les dimensions tenant dans la zone
   $test_h = round(($dst_w / $src_w) * $src_h);
   $test_w = round(($dst_h / $src_h) * $src_w);
   // Si Height final non précisé (0)
   if(!$dst_h) $dst_h = $test_h;
   // Sinon si Width final non précisé (0)
   elseif(!$dst_w) $dst_w = $test_w;
   // Sinon teste quel redimensionnement tient dans la zone
   elseif($test_h>$dst_h) $dst_w = $test_w;
   else $dst_h = $test_h;

   // Affiche les dimensions optimales
   return "width=".$dst_w." height=".$dst_h;
}

function etabSession($siret, $idEntreprise = 0){
	global $client;
	if ($siret!=$_SESSION['tabInfo']['entrep']['siret'] ||
		($siret*1==0 && $idEntreprise!=$_SESSION['tabInfo']['entrep']['id']) ){
		$O2 = $client->getIdentite($siret, $idEntreprise, false);
		$etab = $O2['result'];
		$tabEntrep = array();
		$tabEntrep['raisonSociale'] = $etab['Nom'];
		$tabEntrep['id'] = $etab['id'];
		$tabEntrep['siren'] = $etab['Siren'];
		$tabEntrep['siret'] = $etab['Siret'];
		$tabEntrep['tva'] = $etab['TvaNumero'];
		$tabEntrep['isin'] = $etab['Isin'];
		$tabEntrep['AutreId'] = $etab['AutreId'];
		$tabEntrep['active'] = array_key_exists('Statut',$etab)? $etab['Statut'] : '';
		$tabEntrep['Source'] = $etab['Source'];
		$tabEntrep['SourceId'] = $etab['SourceId'];
		$tabEntrep['codeCommune'] = $etab['Dept'].$etab['codeCommune'];
		$tabEntrep['nafEn'] = $etab['NafEnt'];
		$tabEntrep['nafEt'] = $etab['NafEtab'];
		$tabEntrep['fj'] = $etab['FJ'];
		$_SESSION['tabInfo']['entrep'] = $tabEntrep;
		$raisonSociale = $etab['Nom'];
	} else {
		$raisonSociale = $_SESSION['tabInfo']['entrep']['raisonSociale'];
	}
	return $raisonSociale;
}

?>