<?php
/*
if(!is_bool(strpos($_SERVER['argv'][0], '&'))) $end = strpos($_SERVER['argv'][0], '&');
else $end = strpos($_SERVER['argv'][0], '|');

header("Location: http://" . substr($_SERVER['argv'][0], 0, $end));
return;
*/
error_reporting(E_ALL);
ini_set("display_errors","on");
require_once dirname($_SERVER['DOCUMENT_ROOT'])."/inc/_conf.inc";
// error_reporting(E_ALL);
// ini_set("display_errors","On");
require_once OUTCODEPATH . "/common/_klasy.SimpleImage.inc";
//print_r($_SERVER['REDIRECT_URL']);
//print_r($_SERVER['argv'][0]);


if( !isset($_SERVER['argv'][0]) ) die('?');
else 
{
	$_argv=preg_split('/[&|]/',$_SERVER['argv'][0]);
	$_argv[0] = str_replace('%2F', "/", urlencode($_argv[0]));
	$img['src'] = 'http://' . $_argv[0];
	if(isset($_argv[1])) $img['scale']=$_argv[1];
	else $img['scale']=100;
}



if (1==1) :

	// error_reporting(E_ALL);
	// ini_set("display_errors","On");	
	
	// http://www.canal-plus.pl/thumbnails/www.alekinoplus.pl/wideo/zmediastore/alekino/2017/kino-mowi/KinoMowiExtraKomisarzMaigret_a.jpg&123&w
	$file_base = substr(strstr( $img['src'],'.pl/' ),3);
	
	$file = false;
	
	if (substr($file_base,0,strlen('/wideo/')) === '/wideo/') {
		$file = '/WIDEO/' . substr($file_base,strlen('/wideo/'));			
	} elseif (substr($file_base,0,strlen('/ms_galeria/')) === '/ms_galeria/') {
		$file = '/canal-plus.pl/www' . $file_base;			
	} else {
		if (file_exists('/canalplus.pl/www' . $file_base)) {
			$file = '/canalplus.pl/www' . $file_base;	
		}
	}

if (isset($_SERVER['HTTP_CF_CONNECTING_IP']) && ($_SERVER['HTTP_CF_CONNECTING_IP'] === '77.252.244.152' || $_SERVER['HTTP_CF_CONNECTING_IP'] === '95.40.51.236') && (1==2)){
	print_r('<pre style="background-color:#FFFFFF;color:#000000;padding:3px;">');
		print(__FILE__ . ',' . __LINE__) . "\n";
		print(__CLASS__ . ',' . __FUNCTION__) . "\n";
		echo '$file_base|' . var_export($file_base,true) . "|\r\n";
		echo '$file|' . var_export($file,true) . "|\r\n";
		echo '$file_exists|' . var_export(file_exists($file),1) . '|' . "\n";
	print_r('</pre>');
	die();
}

	if (empty($file)) {
		die('Brak param file');	
	}

	$img['info'] = @getimagesize( $file );
	
	if( !isset($img['info'][0]) ) {
		
		if (isset($_SERVER['HTTP_CF_CONNECTING_IP']) && ($_SERVER['HTTP_CF_CONNECTING_IP'] === '77.252.244.152') && (1==2)){
			print_r('<pre style="background-color:#FFFFFF;color:#000000;padding:3px;">');
				print(__FILE__ . ',' . __LINE__) . "\n";
				print(__CLASS__ . ',' . __FUNCTION__) . "\n";
				echo '$img[src]|' . var_export($img['src'],1) . '|' . "\n";
				echo '$file_base|' . var_export($file_base,1) . '|' . "\n";
				echo '$file|' . var_export($file,1) . '|' . "\n";
			print_r('</pre>');
			die();
		}		
		
		die('Brak');	
	}
	
	$img['src'] = $file;
	
	$image = new SimpleImage();
	$image->load( $img['src'] );
	
	if(isset($_argv[2]) && $_argv[2]=='w') $image->resizeToWidth( $img['scale'] );
	else $image->resizeToHeight( $img['scale'] );
	$image->gama();
	
	//header('HTTP/1.1 304 Not Modified');
	header('Content-Type: image/jpeg');
	header("Expires: Fri, 01 Jan 2012 05:00:00 GMT");
	header("Cache-Control: max-age=8640000");
	header('Last-Modified:' . gmdate( "D, d M Y H:i:s", mktime(date('H'),date('i'),date('s'),date('m'),date('d')-5,date('Y')) ). ' GMT');
	
	$image->unsharpmask(120, 0.8, 3);
	$image->output();
	
	die();	
	
	print_r('<pre style="background-color:#FFFFFF;color:#000000;padding:3px;">');
		print(__FILE__ . ',' . __LINE__) . "\n";
		print(__CLASS__ . ',' . __FUNCTION__) . "\n";
		echo '$img|' . var_export($img,1) . '|' . "\n";
		echo '$file|' . var_export($file,1) . '|' . "\n";
		echo '$file|' . var_export(is_file($file),1) . '|' . "\n";
	print_r('</pre>');
	die();
	
endif;

die('EXIT');

if( !isset($img['info'][0]) ) 
{
	$file = substr(strstr( $img['src'],'.pl/' ),3);
	
	if(is_file('/storage/cyfraplus/virtual/sportpro.canalplus.pl/local/www'.$file)) 
	{
		$file = '/storage/cyfraplus/virtual/sportpro.canalplus.pl/local/www'.$file;
	}
	else if(is_file('/storage/cyfraplus/virtual/canalplus.pl/local/www'.$file)) 
	{
		$file = '/storage/cyfraplus/virtual/canalplus.pl/local/www'.$file;
	}
	else if(is_file('/storage/cyfraplus/virtual/canalpluscyfrowy.pl/local/www'.$file))
	{
		$file = '/storage/cyfraplus/virtual/canalpluscyfrowy.pl/local/www'.$file;
	}
	else die();
	$img['info'] = @getimagesize( $file );
	if( isset($img['info'][0]) ) $img['src']=$file;
	else die();
}

$image = new SimpleImage();
$image->load( $img['src'] );

if(isset($_argv[2]) && $_argv[2]=='w') $image->resizeToWidth( $img['scale'] );
else $image->resizeToHeight( $img['scale'] );
$image->gama();

//header('HTTP/1.1 304 Not Modified');
header('Content-Type: image/jpeg');
header("Expires: Fri, 01 Jan 2012 05:00:00 GMT");
header("Cache-Control: max-age=8640000");
header('Last-Modified:' . gmdate( "D, d M Y H:i:s", mktime(date('H'),date('i'),date('s'),date('m'),date('d')-5,date('Y')) ). ' GMT');

$image->unsharpmask(120, 0.8, 3);
$image->output();

?>