<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
	require_once 'common/curl.php';
	$logoUrl = $_REQUEST['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');

	//Vérification fichier est une image
	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);

		$output = '<div style="text-align:center;">';
		$output.= '<img src="logos/'.$name_file.'&uid='.time().'" />';
		$output.= '<br/>';
		$output.= '<a id="logo_crop" href="/?page=saisieajax&q=logo/crop/'.
			$name_file.'">Editer l\'image</a>';
		$output.= '</div>';

	} else {
		$output = 'Saisir l\'URL d\'une image.';
	}
	echo $output;
	exit;
}
?>
<form id="urlForm" name="urlForm" action="/?page=saisieajax&q=logo/url" method="post">
<input type="hidden" name="siren" value="<?=$siren?>" />
<label>URL : </label><input type="text" name="url" />
<input type="submit" name="submit" value="Valider"/>
</form>
<div id="result"></div>
<script type="text/javascript">
$('#urlForm').ajaxForm({
	dataType: 'html',
	target: '#result',
	beforeSubmit: function(a,f,o) {
        $('#result').html('Récupération en cours...');
	},
    success: function(data) {
		$('#result').html(data);
		return false;
	}
});
</script>