<?php
function saisie_logo( $siren )
{
	$message = '';
	if ( isset($_FILES['logoFile']) &&
		$_FILES['logoFile']['error']!=UPLOAD_ERR_NO_FILE ) {

		/** Un fichier a été uploadé **/
		$logoFile = $_FILES['logoFile'];
	    $tmp_file = $logoFile['tmp_name'];

	    if ( $logoFile['error']!=UPLOAD_ERR_OK ) {
	    	$message = 'Erreur lors de la copie du fichier';
	    }

	    if ( !is_uploaded_file($tmp_file) ){
	    	$message = "Le fichier est introuvable";
	    } else {
		    // on vérifie maintenant l'extension
	    	$type_file = $logoFile['type'];
	    	$ext = '';
	    	if    ( strstr($type_file, 'jpg'))	$ext='jpg';
			elseif( strstr($type_file, 'jpeg'))	$ext='jpeg';
			elseif( strstr($type_file, 'bmp'))	$ext='bmp';
			elseif( strstr($type_file, 'gif'))	$ext='gif';
			elseif( strstr($type_file, 'png'))	$ext='png';
			if ($ext=='') {
				$message = "Le fichier n'est pas une image";
			} else {
	    		// on copie le fichier dans le dossier de destination
	    		$name_file = $siren.'.'.$ext;
				if( !move_uploaded_file($tmp_file, PATH_LOGOS . $name_file) ) {
					$message = "Impossible de copier le fichier dans ".PATH_LOGOS;
				} else {
					$message = "Le fichier a bien été uploadé";
				}
	    	}
	    }
	//Suppression ou URL fichier image
	} elseif ( isset($_REQUEST['logoUrl']['del']) ||
			( isset($_REQUEST['logoUrl']['url']) &&
				$_REQUEST['logoUrl']['url']!='' ) ) {

		//Suppression du fichier
		if ( isset($_REQUEST['logoUrl']['del']) && $_REQUEST['logoUrl']['del'] )
		{
			$extensions = array('jpeg', 'jpg', 'png', 'gif', 'bmp');
			foreach ( $extensions as $ext ) {
				if ( file_exists(PATH_LOGOS.$siren.'.'.$ext) ){
					unlink(PATH_LOGOS.$siren.'.'.$ext);
				}
			}
		} else {
			saisie_getlogo($siren);
		}
	}
	return $message;
}

function saisie_saveimg( $siren )
{
	$message = '';
	if ( isset($_FILES['logoFile']) &&
		$_FILES['logoFile']['error']!=UPLOAD_ERR_NO_FILE ) {
		$logoFile = $_FILES['logoFile'];
	    $tmp_file = $logoFile['tmp_name'];
	    if ( $logoFile['error']!=UPLOAD_ERR_OK ) {
	    	$output = '';
	    } elseif ( !is_uploaded_file($tmp_file) ){
	    	$output = '';
	    } else {
		    // On vérifie maintenant l'extension
	    	$extAuthorized = array('jpeg', 'jpg', 'png', 'gif', 'bmp');
		    $type_file = $logoFile['type'];
		    $ext = '';
	    	if ( !in_array($type_file, $extAuthorized ) )
		    {
		    	$ext = $type_file;
		    }
		    if ( !empty($ext) ){
		    	// on copie le fichier dans le dossier de destination
	    		$name_file = 'tmp_'.$siren.'.'.$ext;
				if( !move_uploaded_file($tmp_file, PATH_LOGOS . $name_file) ) {
					$output = '';
				} else {

					$output = $name_file;
				}
		    } else {
		    	$output = '';
		    }
	    }
	}
	return $output;
}

function saisie_findlogo( $siren, $isin = '' )
{
	$img = '';
	$extensions = array('jpeg', 'jpg', 'png', 'gif', 'bmp');
	//Recherche image sur base siren
	foreach ( $extensions as $ext ) {
		if ( file_exists(PATH_LOGOS.$siren.'.'.$ext) ) {
			$img = $siren.'.'.$ext;
			break;
		}
	}
	//Recherche image sur base isin
	if ( $img == '' && $isin != '' ) {
		foreach ( $extensions as $ext ) {
			if ( file_exists(PATH_LOGOS.$isin.'.'.$ext) ) {
				$img = $isin.'.'.$ext;
				break;
			}
		}
	}
	FB::log($img, 'img');
	return $img;
}


function saisie_loadlogo( $siren, $isin = '' )
{
	$urlImg = '';
	$locImg = PATH_LOGOS . $siren;
	$extensions = array('jpeg', 'jpg', 'png', 'gif', 'bmp');

	//Recherche image sur base siren
	foreach ( $extensions as $ext ) {
		if ( file_exists($locImg.'.'.$ext) ) {
			$urlImg = '/logos/'.$siren.'.'.$ext;
			break;
		}
	}

	//Recherche image sur base isin
	if ( $urlImg == '' && $isin != '' ) {
		$locImg = PATH_LOGOS . $isin;
		foreach ( $extensions as $ext ) {
			if ( file_exists($locImg.'.'.$ext) ) {
				$urlImg = '/logos/'.$isin.'.'.$ext;
				break;
			}
		}
	}

	$output = '';
	//Redimensionnement
	if ( $urlImg != '' ) {
		$tabTmp = getimagesize($locImg.'.'.$ext);
		$w = $tabTmp[0];
		$h = $tabTmp[1];
		if ( $w>350 ) {
			$strSize = redimage($locImg.'.'.$ext,350,150);
		} else {
			$strSize = '';
		}

		$output = '<img src="'.$urlImg.'" '.$strSize.'/>';
	}
	return $output;
}

function saisie_getlogo( $siren )
{
	require_once 'common/curl.php';
	$logoUrl = $_REQUEST['logoUrl']['url'];
	$tabTmp  = parse_url($logoUrl);
	$hostUrl = $tabTmp['host'];
	$pathUrl = $tabTmp['path'];
	$tmp = explode('.', basename($pathUrl));
	$ext = strtolower(end($tmp));
	$page = getUrl($logoUrl, '', '', '', false, $hostUrl, '', 7);
	$body = $page['body'];
	$extAuthorized = array('jpeg', 'jpg', 'png', 'gif', 'bmp');
	if ( !in_array($ext, $extAuthorized ) )
	{
		$tmp = explode('/', $page['header']['Content-Type']);
		$ext = trim ( str_replace('?', '',strtolower(end($tmp)) ) );
	}
	$name_file = $siren.'.'.$ext;
	$fp = @fopen(PATH_LOGOS.$name_file, 'w');
	@fwrite($fp, $body);
	@fclose($fp);
}