<?php
if (isset ( $_POST ['submit'] ) && $_POST ['check'] == 'setting' || $_POST ['action'] == 'multiple') {
	
	$setting = $_POST ['setting'];
	
	$c_name = $setting ['c_name'];
	
	$c_email = $setting ['c_email'];
	
	$website = $setting ['website'];
	
	$address = $setting ['address'];
	
	$city = $setting ['city'];
	
	$state = $setting ['state'];
	
	$zip = $setting ['zip'];
	
	$telephone = $setting ['telephone'];
	
	$telephone2 = $setting ['telephone2'];
	
	$fax = $setting ['fax'];
	
	$tax = $setting ['e_tax'];
	
	$taxvalue = $setting ['taxvalue'];
	
	$registration = $setting ['registration'];
	
	$taxnumber = $setting ['taxnumber'];
	
	$currency = $setting ['currency'];
	
	if ($setting ['fiscal'] != '') {
		$fiscal = explode ( '-', $setting ['fiscal'] );
		
		$fiscal = $fiscal [1] . '-' . $fiscal [0];
	}
	$all = $db->get_count ( 'setting' );
	
	$imagetype = $_FILES ['my_field'] ['type'];
	$lstid = '1';
	$fname = $_FILES ['my_field'] ['name'];
	
	if ($all == '0') {
		
		if ($fname [0] != '') {
			$cli = (isset ( $argc ) && $argc > 1);
			($cli);
			if ($cli) {
				if (isset ( $argv [1] ))
					$_GET ['file'] = $argv [1];
				print_r ( $_GET ['file'] );
				if (isset ( $argv [2] ))
					$_GET ['dir'] = $argv [2];
				if (isset ( $argv [3] ))
					$_GET ['pics'] = $argv [3];
			}
			
			// set variables
			$dir_dest = (isset ( $_GET ['dir'] ) ? $_GET ['dir'] : 'uploads/image');
			$dir_pics = (isset ( $_GET ['pics'] ) ? $_GET ['pics'] : $dir_dest);
			
			if (! $cli && ! (isset ( $_SERVER ['HTTP_X_FILE_NAME'] ) && isset ( $_SERVER ['CONTENT_LENGTH'] ))) 

			{
				
				$files = array ();
				
				foreach ( $_FILES ['my_field'] as $k => $l ) {
					foreach ( $l as $i => $v ) {
						
						if (! array_key_exists ( $i, $files ))
							$files [$i] = array ();
						
						$files [$i] [$k] = $v;
					}
				}
				
				// now we can loop through $files, and feed each element to the
				// class
				foreach ( $files as $file ) {
					// we instanciate the class for each element of $file
					$name = mt_rand ( 15, 215 ) . "___" . mb_strtolower ( preg_replace ( '/\s+/', '', $file ['name'] ) );
					$file ['name'] = str_replace ( array ('(', ')' ), '', $name );
					$savedname = $file ['name'];
					$handle = new upload ( $file );
					
					// then we check if the file has been uploaded properly
					// in its *temporary* location in the server (often, it is
					// /tmp)
					
					if ($handle->uploaded) {
						
						// now, we start the upload 'process'. That is, to copy
						// the uploaded file
						// from its temporary location to the wanted location
						// It could be something like
						// $handle->Process('/home/www/my_uploads/');
						// we now process the image a second time, with some
						// other settings
						
						$handle->image_resize = true;
						$handle->image_ratio_y = true;
						$handle->image_x = 200;
						$handle->Process ( $dir_dest );
						
						$query_insert = $db->insert ( 'setting', array ('c_name' => $c_name, 'c_email' => $c_email, 'website' => $website, 'address' => $address, 'city' => $city, 'state' => $state, 'zip' => $zip, 'telephone' => $telephone, 'telephone2' => $telephone2, 'fax' => $fax, 'c_logo' => $savedname, 'e_tax' => $tax, 'taxvalue' => $taxvalue, 'registration' => $registration, 'taxnumber' => $taxnumber, 'currency' => $currency, 'fiscal' => $fiscal ) );
						
						if ($query_insert) {
							?>

<div id="hide">
	<div id="gritter-notice-wrapper">
		<div id="gritter-item-1" class="gritter-item-wrapper gritter-success"
			style="">
			<div class="gritter-top"></div>
			<div class="gritter-item">

				<div class="gritter-without-image">
					<span class="gritter-title">Cheers..
						<div class="gritter-close" id="gritterremove"
							style="display: inline;"></div>
					</span>
					<p>Your Record Is Inserted...</p>
				</div>
				<div style="clear: both"></div>
			</div>
			<div class="gritter-bottom"></div>
		</div>
	</div>
</div>

<?php
						
						} else {
							?>

<div class="hide1">
	<div id="gritter-notice-wrapper">
		<div id="gritter-item-2" class="gritter-item-wrapper gritter-warning"
			style="">
			<div class="gritter-top"></div>
			<div class="gritter-item">
				<div class="gritter-close" id="gritterremove"
					style="display: inline;"></div>
				<h5>Oops! There Are Some Problem In Inserted...</h5>
				<div style="clear: both"></div>
			</div>
			<div class="gritter-bottom"></div>
		</div>
	</div>
</div>

<?php
						
						}
					}
				}
			} else {
				?>
<div class="hide1">
	<div id="gritter-notice-wrapper">
		<div id="gritter-item-2" class="gritter-item-wrapper gritter-warning"
			style="">
			<div class="gritter-top"></div>
			<div class="gritter-item">
				<div class="gritter-close" id="gritterremove"
					style="display: inline;"></div>
				<h5>Oops! Your File Is Too Large Or Your File Is Not Png Type...</h5>
				<div style="clear: both"></div>
			</div>
			<div class="gritter-bottom"></div>
		</div>
	</div>
</div>

<?php
			
			}
		} else {
			
			$query_insert1 = $db->insert ( 'setting', array ('c_name' => $c_name, 'c_email' => $c_email, 'website' => $website, 'address' => $address, 'city' => $city, 'state' => $state, 'zip' => $zip, 'telephone' => $telephone, 'telephone2' => $telephone2, 'fax' => $fax, 'e_tax' => $tax, 'taxvalue' => $taxvalue, 'registration' => $registration, 'taxnumber' => $taxnumber, 'currency' => $currency ) );
			
			if ($query_insert1) {
				?>
<div id="hide">
	<div id="gritter-notice-wrapper">
		<div id="gritter-item-1" class="gritter-item-wrapper gritter-success"
			style="">
			<div class="gritter-top"></div>
			<div class="gritter-item">

				<div class="gritter-without-image">
					<span class="gritter-title">Cheers..
						<div class="gritter-close" id="gritterremove"
							style="display: inline;"></div>
					</span>
					<p>Your Record Is Inserted...</p>
				</div>
				<div style="clear: both"></div>
			</div>
			<div class="gritter-bottom"></div>
		</div>
	</div>
</div>
<?php
			
			} else {
				?>

<div class="hide1">
	<div id="gritter-notice-wrapper">
		<div id="gritter-item-2" class="gritter-item-wrapper gritter-warning"
			style="">
			<div class="gritter-top"></div>
			<div class="gritter-item">
				<div class="gritter-close" id="gritterremove"
					style="display: inline;"></div>
				<h5>Oops! There Are Some Problem In Inserted...</h5>
				<div style="clear: both"></div>
			</div>
			<div class="gritter-bottom"></div>
		</div>
	</div>
</div>

<?php
			
			}
		
		}
	
	} 

	else {
		
		$query = $db->get_row ( 'setting' );
		
		if ($fname [0] != '') {
			if ($query ['c_logo'] != '') {
				$delete1 = $db->update ( 'setting', array ('c_logo' => '' ), array ('id' => $id ) );
				$file_name = SERVER_ROOT . '/uploads/image/' . $query ['c_logo'];
				if (file_exists ( $file_name )) {
					unlink ( $file_name );
				}
			}
			
			$cli = (isset ( $argc ) && $argc > 1);
			($cli);
			if ($cli) {
				if (isset ( $argv [1] ))
					$_GET ['file'] = $argv [1];
				print_r ( $_GET ['file'] );
				if (isset ( $argv [2] ))
					$_GET ['dir'] = $argv [2];
				if (isset ( $argv [3] ))
					$_GET ['pics'] = $argv [3];
			}
			
			// set variables
			$dir_dest = (isset ( $_GET ['dir'] ) ? $_GET ['dir'] : 'uploads/image');
			$dir_pics = (isset ( $_GET ['pics'] ) ? $_GET ['pics'] : $dir_dest);
			
			if (! $cli && ! (isset ( $_SERVER ['HTTP_X_FILE_NAME'] ) && isset ( $_SERVER ['CONTENT_LENGTH'] ))) 

			{
				
				$files = array ();
				
				foreach ( $_FILES ['my_field'] as $k => $l ) {
					foreach ( $l as $i => $v ) {
						
						if (! array_key_exists ( $i, $files ))
							$files [$i] = array ();
						
						$files [$i] [$k] = $v;
					}
				}
				
				// now we can loop through $files, and feed each element to the
				// class
				foreach ( $files as $file ) {
					// we instanciate the class for each element of $file
					$name1 = mt_rand ( 15, 215 ) . "___" . mb_strtolower ( preg_replace ( '/\s+/', '', $file ['name'] ) );
					$file ['name'] = str_replace ( array ('(', ')' ), '', $name1 );
					$savedname = $file ['name'];
					$handle = new upload ( $file );
					
					// then we check if the file has been uploaded properly
					// in its *temporary* location in the server (often, it is
					// /tmp)
					
					if ($handle->uploaded) {
						
						// now, we start the upload 'process'. That is, to copy
						// the uploaded file
						// from its temporary location to the wanted location
						// It could be something like
						// $handle->Process('/home/www/my_uploads/');
						// we now process the image a second time, with some
						// other settings
						
						$handle->image_resize = true;
						$handle->image_ratio_y = true;
						$handle->image_x = 200;
						
						$handle->Process ( $dir_dest );
						
						$query_update = $db->update ( 'setting', array ('c_name' => $c_name, 'c_email' => $c_email, 'website' => $website, 'address' => $address, 'city' => $city, 'state' => $state, 'zip' => $zip, 'telephone' => $telephone, 'telephone2' => $telephone2, 'fax' => $fax, 'c_logo' => $savedname, 'e_tax' => $tax, 'taxvalue' => $taxvalue, 'registration' => $registration, 'taxnumber' => $taxnumber, 'currency' => $currency, 'fiscal' => $fiscal ), array ('id' => $query ['id'] ) );
						
						if ($query_update) {
							?>

<div id="hide">
	<div id="gritter-notice-wrapper">
		<div id="gritter-item-1" class="gritter-item-wrapper gritter-success"
			style="">
			<div class="gritter-top"></div>
			<div class="gritter-item">

				<div class="gritter-without-image">
					<span class="gritter-title">Cheers..
						<div class="gritter-close" id="gritterremove"
							style="display: inline;"></div>
					</span>
					<p>Your Record Is Updated...</p>
				</div>
				<div style="clear: both"></div>
			</div>
			<div class="gritter-bottom"></div>
		</div>
	</div>
</div>

<?php
						
						} else {
							?>

<div class="hide1">
	<div id="gritter-notice-wrapper">
		<div id="gritter-item-2" class="gritter-item-wrapper gritter-warning"
			style="">
			<div class="gritter-top"></div>
			<div class="gritter-item">
				<div class="gritter-close" id="gritterremove"
					style="display: inline;"></div>
				<h5>Oops! There Are Some Problem In Updated...</h5>
				<div style="clear: both"></div>
			</div>
			<div class="gritter-bottom"></div>
		</div>
	</div>
</div>

<?php
						
						}
					}
				}
			} else {
				?>

<div class="hide1">
	<div id="gritter-notice-wrapper">
		<div id="gritter-item-2" class="gritter-item-wrapper gritter-warning"
			style="">
			<div class="gritter-top"></div>
			<div class="gritter-item">
				<div class="gritter-close" id="gritterremove"
					style="display: inline;"></div>
				<h5>Oops! Your File Is Too Large Or Your File Is Not Png Type...</h5>
				<div style="clear: both"></div>
			</div>
			<div class="gritter-bottom"></div>
		</div>
	</div>
</div>

<?php
			
			}
		
		} else {
			$query_insert1 = $db->update ( 'setting', array ('c_name' => $c_name, 'c_email' => $c_email, 'website' => $website, 'address' => $address, 'city' => $city, 'state' => $state, 'zip' => $zip, 'telephone' => $telephone, 'telephone2' => $telephone2, 'fax' => $fax, 'e_tax' => $tax, 'taxvalue' => $taxvalue, 'registration' => $registration, 'taxnumber' => $taxnumber, 'currency' => $currency, 'fiscal' => $fiscal ), array ('id' => $query ['id'] ) );
			
			if ($query_insert1) {
				?>

<div id="hide">
	<div id="gritter-notice-wrapper">
		<div id="gritter-item-1" class="gritter-item-wrapper gritter-success"
			style="">
			<div class="gritter-top"></div>
			<div class="gritter-item">

				<div class="gritter-without-image">
					<span class="gritter-title">Cheers..
						<div class="gritter-close" id="gritterremove"
							style="display: inline;"></div>
					</span>
					<p>Your Record Is Updated...</p>
				</div>
				<div style="clear: both"></div>
			</div>
			<div class="gritter-bottom"></div>
		</div>
	</div>
</div>

<?php
			
			} else {
				?>

<div class="hide1">
	<div id="gritter-notice-wrapper">
		<div id="gritter-item-2" class="gritter-item-wrapper gritter-warning"
			style="">
			<div class="gritter-top"></div>
			<div class="gritter-item">
				<div class="gritter-close" id="gritterremove"
					style="display: inline;"></div>
				<h5>Oops! There Are Some Problem In Updated...</h5>
				<div style="clear: both"></div>
			</div>
			<div class="gritter-bottom"></div>
		</div>
	</div>
</div>

<?php
			
			}
		}
	
	}
	
	// retrieve eventual CLI parameters

}

$getid = $link->hrefquery ();

$id = $getid [1];
if ($id == 1) {
	$delete = $db->update ( 'setting', array ('c_logo' => '' ), array ('id' => $id ) );
	if ($delete) {
		unlink ( SERVER_ROOT . '/uploads/image/' . LOGO );
		$session->redirect ( "setting", Admin );
	
	}
}
?>