<?php


error_reporting(E_ALL & ~E_NOTICE);

define('DS', DIRECTORY_SEPARATOR);
define('APP_PATH_ROOT', realpath( dirname(__FILE__) . DS . '..' ));
define('APP_PATH_WWW', dirname(__FILE__));
define('APP_PATH_CONFIG', APP_PATH_ROOT . DS . 'config');

//session_save_path("../session") ;
session_start();

date_default_timezone_set('Europe/Warsaw');// PHP 5 >= 5.1.0 required by date functions

if (file_exists(APP_PATH_ROOT . DS . 'config' . DS . '.config_'.$_SERVER['SERVER_NAME'].'.php')) {
	require APP_PATH_ROOT . DS . 'config' . DS . '.config_'.$_SERVER['SERVER_NAME'].'.php';
}

require_once APP_PATH_ROOT . DS . 'se-lib' . DS . 'Lib.php';
Lib::loadClass('V');
Lib::loadClass('User');
Lib::loadClass('Config');
Lib::loadClass('App');
Lib::loadClass('DB');
Lib::loadClass('S');

if (($function_init = V::get('function_init', '', $_GET))) {
	if (function_exists($function_init)) {
		$function_init();
	} else {
		header('HTTP/1.1 400: Bad Request');
		header('Warning: wrong ID L.' . __LINE__);
	}
} else {
	header('HTTP/1.1 400: Bad Request');
	header('Warning: wrong ID L.' . __LINE__);
}



function fun_SHOW_EXTERNAL_IMAGE() {
	$remote_table = V::get('tbl', '', $_REQUEST);
	$remote_id = V::get('id', '', $_REQUEST, 'int');
	$number = V::get('number', '', $_REQUEST, 'int');
	$image_resize = V::get('resize', '', $_REQUEST);

	Lib::loadClass('DB_Image');

	// check remote id
	if ($remote_id <= 0) {
		header('HTTP/1.1 400: Bad Request');
		header('Warning: wrong ID L.' . __LINE__);
		exit;
	}

	// check remote table
	$remote_tables = DB_Image::conf_get('remote_tables');
	if (!in_array($remote_table, $remote_tables)) {
		header('HTTP/1.1 400: Bad Request');
		header('Warning: table not allowed L.' . __LINE__);
		exit;
	}

	$db = DB::getDB();
	$sql = "select `ID`, `TYPE`, `SIZE`, `IMAGE`, `WIDTH`, `HEIGHT`, UNIX_TIMESTAMP(`A_CREATE_DATE`) as A_CREATE_DATE
		from `".DB_Image::conf_get_table_name()."`
		where
			`REMOTE_ID`='".$remote_id."'
			and `REMOTE_TABLE`='".$remote_table."'
		order by `ID` asc
		limit 1 offset ".$number."
	";

	$res = $db->query($sql);
	if (!$db->num_rows($res)) {
		header('HTTP/1.1 400: Bad Request');
		header('Warning: images not found in db L.' . __LINE__);
		exit;
	}

	if ($image_row = $db->fetch( $res )) {
		$etag = md5($image_row->ID . $image_row->A_CREATE_DATE);
		if (@strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $image_row->A_CREATE_DATE
			|| trim($_SERVER['HTTP_IF_NONE_MATCH']) == $etag) {
			header("HTTP/1.1 304 Not Modified");
			exit;
		}
		$expire_date = mktime(0, 0, 0, date('m') + 3, date('d'), date('Y'));

		if ($image_resize) {
			list($resize_w, $resize_h) = explode('x', $image_resize, 2);
			$resize_w = intval($resize_w); $resize_h = intval($resize_h);
			if ($resize_w > 0 && $resize_h > 0) {
				$im = imagecreatefromstring( $image_row->IMAGE );
				$image_resized = DB_Image::resize_image_from_data($im, $resize_w, $resize_h);

				header("Content-type: ".$image_row->TYPE);
				//header("Cache-control: public");
				header("Cache-control: public");
				header("Pragma: public");// default 'no-cache'
				header("Etag: ".$etag);
				header("Expires: " . gmdate("D, d M Y H:i:s", $expire_date) . " GMT");
				header("Last-Modified: " . gmdate("D, d M Y H:i:s", $image_row->A_CREATE_DATE) . " GMT");
				if ($image_row->TYPE == 'image/png') {
					imagepng($image_resized);
				} else if ($image_row->TYPE == 'image/jpeg' || $image_row->TYPE == 'image/jpg') {
					imagejpeg($image_resized);
				} else if ($image_row->TYPE == 'image/gif') {
					imagegif($image_resized);
				}
			} else {
				echo'ERROR wrong param resize (WxH)';
			}
			exit;
		}

		//echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;">';print_r($image_row);echo'</pre>';
		//header("Content-type: ".$image_row->TYPE);
		header("Content-type: ".$image_row->TYPE);
		header("Cache-control: public");
		header("Pragma: public");// default 'no-cache'
		header("Etag: ".$etag);
		header("Content-length: ".$image_row->SIZE);
		header("Expires: " . gmdate("D, d M Y H:i:s", $expire_date) . " GMT");
		header("Last-Modified: " . gmdate("D, d M Y H:i:s", $image_row->A_CREATE_DATE) . " GMT");
		//echo stripslashes($image_row->IMAGE);
		print $image_row->IMAGE;
		//file_put_contents('/home/pl/projekty/kyoritsu.pl/src/1.jpg', $image_row->IMAGE);
	} else {
		header('HTTP/1.1 400: Bad Request');
		header('Warning: image not found in db L.' . __LINE__);
	}
	exit;
}
