<?php

if ( !defined( 'SMARTY_DIR' ) ) {
	include_once( 'init.php' );
}

include ( 'sessioninc.php' );

include (INC_DIR."internal/snaps_functions.php");

$userid = $_SESSION['UserId'];

$row =& $osDB->getRow( 'SELECT id, picture, tnpicture FROM ! WHERE userid = ? AND picno = ?', array( USER_SNAP_TABLE, $userid, $_POST['txtpicno'] ) );

$userinfo =& $osDB->getRow('select * from ! where id = ?', array( USER_TABLE, $userid) );

$err = 0;

if ($config['snaps_require_approval'] == 'Y') {

	$act = 'N';

} else {

	$act = 'Y';

}

$curr_imgfile = $curr_tnimgfile = '';

if (substr_count($row['picture'], 'file:' )>0 ) {
	$curr_imgfile = ltrim(rtrim(str_replace('file:','',$row['picture'] ) ) );
}
if (substr_count($row['tnpicture'],'file:' )>0 ) {
	$curr_tnimgfile = ltrim(rtrim(str_replace('file:','',$row['tnpicture'] ) ) );
}

$allwdsize = $config['upload_snap_maxsize'];

$album_id = $_REQUEST['album_id'];

$type = $_REQUEST['type'];

$default_pic = ($_REQUEST['default_pic']=='Y')?'Y':'N';

$time = time();

if( is_uploaded_file( $_FILES['txtimage']['tmp_name'] ) ) {

	$img_file = $_FILES['txtimage']['tmp_name'];

	$ext = split( '/', $_FILES['txtimage']['type'] );

	$picext = strtolower($ext[1]);

	if( $picext == 'pjpeg' || $picext == 'jpeg'){

		$picext = 'jpg';
	}

	if( $picext == 'x-png' ) {
		$picext= 'png';
	}
	//echo "$picext<br>";

	$ext_ok = '0';

	foreach (explode(',',$config['upload_snap_ext']) as $ex) {


		if ( $ex == $picext ) $ext_ok++;

	}

	/* bmp is removed as valid source time being */
	if ( $ext_ok <= '0' or $picext == 'bmp') {

		header( 'location: uploadsnaps.php?msg=' .WRONG_TYPE .'&type='.$type.'&album_id='.$album_id  );
		exit;

	}

	clearstatcache();

	$fstats= stat($img_file);

	$picsize = $fstats[7];

	$handle = fopen ($img_file, 'rb');

	/* Get current picture size and allowed size. If pic size is more than the allowed size, flag error.. */


	if ($picsize > $allwdsize) {

		header( 'location: uploadsnaps.php?msg='.BIG_PIC_SIZE.'&type='.$type.'&album_id='.$album_id  );
		exit;

	}

	$orgimg = fread($handle, $picsize);

	fclose ($handle);

	if ($_POST['txtpicno'] == '' or !isset($_POST['txtpicno']) ) {
		$_POST['txtpicno'] = $osDB->getOne('select max(picno)+1 from ! where userid = ?',array(USER_SNAP_TABLE,$userid) )+1;
	}

	if ( $picext != 'jpg' ) {
	/* convert the picture to jpg. This is to enable picture editing  */

		//$jpgfile = createThumb($orgimg, 'N');
		$img_tmp=createImg($picext,$img_file);
		$jpgfile = createJpeg($img_tmp, 'N');
		$newimg = file_get_contents($jpgfile);
		unlink($jpgfile, $img_file);

	} else {

		$newimg = $orgimg;
	}

	if ($_REQUEST['generate_tnpic'] == 'Y' || !$row ) {

		$img_tmp=createImg($picext,$img_file);

		$tnimg_file = createJpeg($img_tmp, "Y");

		$tnimg = file_get_contents($tnimg_file);

		unlink($tnimg_file);

		$tnext = 'jpg';
		if ($config['images_in_db'] == 'N') {

			$tnimgfile = writeImageToFile($tnimg, $userid, '2'.$_POST['txtpicno'],$curr_tnimgfile);

			$tnimg = 'file:'.$tnimgfile;

		} else {

			$tnimg = base64_encode($tnimg);
		}
	}

	$picext = 'jpg';

	if ($config['images_in_db'] == 'N') {

		$imgfile = writeImageToFile($newimg, $userid, '1'.$_POST['txtpicno'],$curr_imgfile);

		$newimg = 'file:'.$imgfile;
		sleep(5);
	} else {
		$newimg = base64_encode($newimg);
	}

	if ( $row ) {

		if ($_REQUEST['generate_tnpic'] == 'Y') {
			$osDB->query('update ! set picture = ?, ins_time = ?, active=?, picext=?, tnpicture = ?, tnext = ?, album_id = ? , default_pic = ? where userid = ? and picno = ? and id = ?', array( USER_SNAP_TABLE, $newimg, $time, $act,	$picext, $tnimg, $tnext, $album_id, $default_pic, $userid, $_POST['txtpicno'], $row['id'] ) );
		} else {
			$osDB->query('update ! set picture = ?, ins_time = ?, active=?, picext=?,  album_id = ? , default_pic = ? where userid = ? and picno = ? and id = ?', array( USER_SNAP_TABLE, $newimg, $time, $act,	$picext, $album_id, $default_pic, $userid, $_POST['txtpicno'], $row['id'] ) );
		}
	} else {

		$osDB->query( 'insert into ! (  userid, picno, picture, ins_time, active, picext, tnpicture, tnext, album_id, default_pic ) values (  ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )', array( USER_SNAP_TABLE, $userid, $_POST['txtpicno'], $newimg, $time, $act, $picext, $tnimg, $tnext, $album_id, $default_pic ) );

	}

	updateLoadedPicturesCnt($userid);

	unset($newimg, $tnimg);

	if ($config['newpic_admin_info'] == 'Y') {
		sendAdminEmail();
	}


	Header("Cache-Control: must-revalidate");

	$ExpStr = "Expires: " . gmdate("D, d M Y H:i:s", time() -30) . " GMT";
	Header($ExpStr);

	header( 'location: uploadsnaps.php?msg='.PICTURE_LOADED.'&type='.$type.'&album_id='.$album_id );
	exit;

} elseif ( is_uploaded_file( $_FILES['tnimage']['tmp_name'] ) ) {

	$tnimg_file = $_FILES['tnimage']['tmp_name'];

	$ext = split( '/', $_FILES['tnimage']['type'] );

	$tnext = strtolower($ext[1]);

	$tnsize = $config['upload_snap_tnsize'];

	if( $tnext == 'pjpeg' || $tnext == 'jpeg'){

		$tnext = 'jpg';

	}

	if( $tnext == 'x-png' ) {
		$tnext= 'png';
	}

	$ext_ok = 0;

	foreach (explode(',',$config['upload_snap_ext']) as $ex) {

		if ( $ex == $tnext ) $ext_ok++;

	}

	if ( $ext_ok <= 0 ) {

		header( 'location: uploadsnaps.php?msg=' .WRONG_TYPE .'&type='.$type.'&album_id='.$album_id  );
		exit;

	}

	clearstatcache();

	$fstats= stat($tnimg_file);

	$picsize = $fstats[7];

	if ($picsize > $allwdsize) {

		header( 'location: uploadsnaps.php?msg='.BIG_PIC_SIZE.'&type='.$type.'&album_id='.$album_id  );
		exit;

	}

	list($tnwidth, $tnheight, $tntype, $tnattr) = getimagesize($tnimg_file);


	if ($tnwidth > $tnsize or $tnheight > $tnsize) {

			header( 'location: uploadsnaps.php?msg='.BIGTHUMBNAIL.'&type='.$type.'&album_id='.$album_id  );
			exit;
	}

	$handle = fopen ($tnimg_file, 'rb');

	/* Get current picture size and allowed size. If pic size is more than the allowed size, flag error.. */

	$tnimg = fread($handle, filesize ($tnimg_file));

	fclose ($handle);

	if ( $tnext != 'jpg' ) {

	/* convert the picture to jpg. This is to enable picture editing  */

		//$jpgfile = createThumb($tnimg, 'N');
		$img_tmp=createImg($tnext,$tnimg_file);
		$jpgfile = createJpeg($img_tmp, 'N');

		$newtnimg = file_get_contents($jpgfile);

		unlink($jpgfile, $tnimg_file);

		$tnext = 'jpg';

	} else {

		$newtnimg = $tnimg;
	}


	$tnimg = base64_encode( $newtnimg );
	unset($newtnimg);

	if ($config['images_in_db'] == 'N') {

		$tnimgfile = writeImageToFile($tnimg, $userid, $_POST['txtpicno'], $curr_tnimgfile);

		$tnimg = 'file:'.tnimgfile;
	}

	if ($row) {

		$osDB->query( 'update ! set tnpicture = ?, ins_time = ?, active=?, tnext=?, album_id = ? where id = ?', array( USER_SNAP_TABLE, $tnimg, $time, $act, 	$tnext, $album_id, $row['id'] ) );

	} else {

		$osDB->query( 'insert into ! (  userid, picno, tnpicture, ins_time, active, tnext, album_id ) values (  ?, ?, ?, ?, ?, ?, ? )', array( USER_SNAP_TABLE,  $userid, $_POST['txtpicno'], $tnimg, $time, $act, $tnext, $album_id ) );

	}

	updateLoadedPicturesCnt($userid);

	unset($tnimg);

	if ($config['newpic_admin_info'] == 'Y') {
		sendAdminEmail();
	}

	Header("Cache-Control: must-revalidate");

	$ExpStr = "Expires: " . gmdate("D, d M Y H:i:s", time() -30) . " GMT";
	Header($ExpStr);

	header( 'location: uploadsnaps.php?msg='.PICTURE_LOADED .'&type='.$type.'&album_id='.$album_id );
	exit;

} else {
	if ($type=='profilepics' ) {
		if ($default_pic == 'Y') {
			/* Make all other profile pics as non default */
			$osDB->query('update ! set default_pic = ? where userid=? and picno <> ? and ( album_id is null or album_id = ?)', array(USER_SNAP_TABLE, 'N', $userid, $_POST['txtpicno'], '0') );
		}
		$osDB->query('update ! set default_pic = ? where userid=? and picno = ? and ( album_id is null or album_id = ?)', array(USER_SNAP_TABLE, $default_pic, $userid, $_POST['txtpicno'], '0') );

		header( 'location: uploadsnaps.php?msg='.PROFILE_PIC_CHNGD .'&type='.$type.'&album_id='.$album_id );
		exit;

	}
}

header( 'location: uploadsnaps.php?msg='.FAILED_UPLOAD .'&type='.$type.'&album_id='.$album_id );
exit;

?>