<?php
require_once('head.php');
?>

  <body>
  <!-- container section start -->
  <section id="container" class="">
      
	<?php require_once('header.php'); ?>	

	<?php require_once('sidebar.php'); ?>
      
      <!--main content start-->
      <section id="main-content">
          <section class="wrapper">

			  <div class="row">
                  <div class="col-lg-12">
                      <!--breadcrumbs start -->
                      <ul class="breadcrumb">
                          <li><a href="dashboard.php"><i class="icon_house_alt"></i> Home</a></li>
						  <li><a href="accounts.php"><i class="icon-signal"></i> Accounts</a></li>
                          <li class="active"> Activated</li>
						  <li class="pull-right"> <i class="icon_calendar"></i>
							 <?php echo date('l jS F Y'); ?></li>
                      </ul>
                      <!--breadcrumbs end -->
                  </div>
              </div>
			  
			  
			  <div class="row">
					<div class="col-lg-12">
					<section class="panel panel-body">
					<h3><?php echo WEB_NAME ?></h3>
					<h4>ACCOUNT ACTIVATION</h4>
					<p>Find  Account Activation Details below.</p>
					</section>
					</div>
              </div>
			  

			<?php


			if(isset($_POST['submit']))
			{

			if ($_POST['acc'] == ""){ redirect('activate.php?error=' . urlencode('Enter Account Number'));}
			elseif ($_POST['user'] == ""){ redirect('activate.php?error=' . urlencode('Enter User ID'));}
			elseif ($_POST['pass'] == ""){ redirect('activate.php?error=' . urlencode('Enter Password'));}
			elseif ($_POST['pin'] == ""){ redirect('activate.php?error=' . urlencode('Enter PIN'));}
            elseif ($_POST['code1'] == ""){ redirect('activate.php?error=' . urlencode('Enter '.CODE1_NAME.''));}
            elseif ($_POST['code2'] == ""){ redirect('activate.php?error=' . urlencode('Enter '.CODE2_NAME.''));}
            elseif ($_POST['code3'] == ""){ redirect('activate.php?error=' . urlencode('Enter '.CODE3_NAME.''));}
			elseif ($_POST['status'] == ""){ redirect('activate.php?error=' . urlencode('Enter Account Status'));}
			elseif ($_POST['type'] == ""){ redirect('activate.php?error=' . urlencode('Enter Account Type'));}
			elseif ($_POST['currency'] == ""){ redirect('activate.php?error=' . urlencode('Enter Account Currency'));}

			else{
			$user_id = $_POST['user_id'];
			//$acc = $_POST['acc'];
			$acc = preg_replace('/\s/', '', $_POST['acc']);
			$user = $_POST['user'];
			$word = $_POST['pass'];
			$pass = md5($_POST['pass']);
			$pin = $_POST['pin'];
            $code1 = $_POST['code1'];
            $code2 = $_POST['code2'];
            $code3 = $_POST['code3'];
			$active = $_POST['status'];
			$type = $_POST['type'];
			$currency = $_POST['currency'];
			$status = "1";

			dbconnect();
			$stmt = $pdo->prepare('UPDATE '.USERS_TABLE_NAME.' SET user_acc = :acc, user_name = :user, user_pwd = :pass, user_word = :word, user_pin = :pin, user_code1 = :code1, user_code2 = :code2, user_code3 = :code3, user_status = :status, user_active = :active, currency = :currency, acc_type = :type WHERE user_id = :userid LIMIT 1');

			$stmt->execute(array(':acc' => $acc, ':user' => $user, ':pass' => $pass, ':word' => $word, ':pin' => $pin, ':code1' => $code1, ':code2' => $code2, ':code3' => $code3, ':status' => $status, ':active' => $active, ':currency' => $currency, ':type' => $type, ':userid' => $user_id));

			
			// Send EMAIL TO ADMIN
			$email = get_email($acc);
			$message = "
			<html>
			<head>
			<title>".WEB_NAME."</title>
			</head>
			<body>
			<img src=".LOGO_URL." alt=".WEB_NAME."/><br><br>
			<p>ACCOUNT ACTIVATION DETAILS:<br><br>	
			ACCOUNT NUMBER: ".$acc." <br>
			USER ID: ".$user." <br>
			PASSWORD: ".$word." <br>
			PIN: ".$pin." <br>
            ".CODE1_NAME.": ".$code1." <br>
            ".CODE2_NAME.": ".$code2." <br>
            ".CODE3_NAME.": ".$code3." <br><br><br>
			</p><br><br><br>
			===========
			<p>".WEB_NAME."</p>
			</body>
			</html>
			";
			
			// Send the email
			$headers = "MIME-Version: 1.0" . "\r\n";
			$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
			$headers .= "From: ".$user." <".$email.">" . "\r\n";
			$subject = "ACCOUNT SUCCESSFULLY ACTIVATED";
			mail(WEB_EMAIL,$subject,$message,$headers);
			
			
			
			if (!empty($_FILES['photo']['tmp_name'])) {

			// upload photo

			$digits = rand(pow(10, 12-1), pow(10, 12)-1);
			$photoid = time().$digits;
			$valid_formats = array("jpg", "png", "gif", "bmp","jpeg");
			$name = $_FILES['photo']['name'];
			$size = $_FILES['photo']['size'];
			if(strlen($name))
			{
			list($txt, $ext) = explode(".", $name);
			if(in_array($ext,$valid_formats))
			{ $msg = "<span class='alert alert-danger'>Invalid photo File Format.</span>";}

			$filename = stripslashes($_FILES['photo']['name']);
			$extension = strtolower($ext);

			if($size<(1024*1024)) // Image size max 1 MB
			{ $msg = "<span class='alert alert-danger'>Photo Image Size Exceeded 1 MB.</span>";}

			define( 'DESIRED_IMAGE_WIDTH', 200 );
			define( 'DESIRED_IMAGE_HEIGHT', 220 );

			  $source_path = $_FILES[ 'photo' ][ 'tmp_name' ];

			  list( $source_width, $source_height, $source_type ) = getimagesize( $source_path );

			  switch ( $source_type )
			  {
				case IMAGETYPE_GIF:
				  $source_gdim = imagecreatefromgif( $source_path );
				  break;

				case IMAGETYPE_JPEG:
				  $source_gdim = imagecreatefromjpeg( $source_path );
				  break;

				case IMAGETYPE_PNG:
				  $source_gdim = imagecreatefrompng( $source_path );
				  break;
			  }

			  $source_aspect_ratio = $source_width / $source_height;
			  $desired_aspect_ratio = DESIRED_IMAGE_WIDTH / DESIRED_IMAGE_HEIGHT;

			  if ( $source_aspect_ratio > $desired_aspect_ratio )
			  {
				// Triggered when source image is wider
				$temp_height = DESIRED_IMAGE_HEIGHT;
				$temp_width = ( int ) ( DESIRED_IMAGE_HEIGHT * $source_aspect_ratio );
			  }
			  else
			  {
				// Triggered otherwise (i.e. source image is similar or taller)
				$temp_width = DESIRED_IMAGE_WIDTH;
				$temp_height = ( int ) ( DESIRED_IMAGE_WIDTH / $source_aspect_ratio );
			  }

			  // Resize the image into a temporary GD image

			  $temp_gdim = imagecreatetruecolor( $temp_width, $temp_height );
			  imagecopyresampled($temp_gdim, $source_gdim, 0, 0, 0, 0, $temp_width, $temp_height, $source_width, $source_height);

			  // Copy cropped region from temporary image into the desired GD image

			  $x0 = ( $temp_width - DESIRED_IMAGE_WIDTH ) / 2;
			  $y0 = ( $temp_height - DESIRED_IMAGE_HEIGHT ) / 2;

			  $desired_gdim = imagecreatetruecolor( DESIRED_IMAGE_WIDTH, DESIRED_IMAGE_HEIGHT );
			  imagecopy($desired_gdim, $temp_gdim, 0, 0, $x0, $y0, DESIRED_IMAGE_WIDTH, DESIRED_IMAGE_HEIGHT);
			  
			$filename = "../users/". $photoid .".".$ext;
			$cphoto = "$photoid".".$ext";

			imagejpeg($desired_gdim,$filename,100);

			imagedestroy($source_gdim);
			imagedestroy($desired_gdim);

			}
			$sql = "UPDATE `".USERS_TABLE_NAME."` SET user_photo = '{$cphoto}' WHERE user_name = '{$user}' LIMIT 1";
			$stmt = $pdo->query($sql);
			}
			
			
			if (isset($_POST['email'])) {
			
			// Send EMAIL TO USER
			$email = get_email($acc);
			$message = "
			<html>
			<head>
			<title>".WEB_NAME."</title>
			</head>
			<body>
			<img src=".LOGO_URL." alt=".WEB_NAME."/><br><br>
			<p>YOUR ACCOUNT HAS BEEN SUCCESSFULLY ACTIVATED:<br><br>	
			ACCOUNT NUMBER: ".$acc." <br>
			USER ID: ".$user." <br>
			PASSWORD: ".$word." (*Temporary)<br>
			PIN: ".$pin." (*Temporary)<br><br><br>
			*An update will be requested on first login.
			</p><br><br><br>
			===========
			<p>".WEB_NAME."</p>
			</body>
			</html>
			";
			
			// Send the email
			$headers = "MIME-Version: 1.0" . "\r\n";
			$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
			$headers .= "From: ".WEB_NAME." <".WEB_EMAIL.">" . "\r\n";
			$subject = "ACCOUNT SUCCESSFULLY ACTIVATED";
			mail($email,$subject,$message,$headers);
				
			}
			
			if (isset($_POST['sms']) && USE_SMS) {
			//$phone = ltrim(user_phone($_SESSION['user_login']), '+');
			$phone = clean_phone(user_phone($user));
			$sms_cnt = "YOUR ACCOUNT HAS BEEN ACTIVATED. ACCOUNT NUMBER: ".$acc.", USER ID: ".$user.", PASSWORD ".$word.", PIN ".$pin."";
			
			// Send SMS
			$url="http://www.easy2text.com/index.php";

			$postfields = array(
				'app' => SMS_APP,
				'ta' => SMS_TA,
				'u' => SMS_USER,
				'p' => SMS_WORD,
				'to' => "$phone",
				'from' => SMS_SENDER,
				'msg' => "$sms_cnt"
			);

			if (!$curld = curl_init()) {
				exit;
			}

			curl_setopt($curld, CURLOPT_POST, true);
			curl_setopt($curld, CURLOPT_POSTFIELDS, $postfields);
			curl_setopt($curld, CURLOPT_URL,$url);
			curl_setopt($curld, CURLOPT_RETURNTRANSFER, true);

			$output = curl_exec($curld);
			curl_close ($curld);
		
			}

			log_info($_SERVER['REMOTE_ADDR'], $_SESSION['admin_login'], $_POST['user'], 'Account Successfully Activated');
			echo "<div class='alert fade in alert-success'><button type='button' class='close close-sm' data-dismiss='alert' aria-hidden='true'><i class='icon-remove'></i></button>Account Successfully Activated.</div>";							
			}
			}		
			?>
			
			<?php
			$sql = "SELECT * FROM `".USERS_TABLE_NAME."` WHERE user_name = '{$user}'";
			$stmt = $pdo->query($sql);
			$row = $stmt->fetch(PDO::FETCH_ASSOC);
			?>
			  
			  <!-- Recent Transactions Starts -->
			  <div class="row">
                  <div class="col-lg-12">					  
					  <section class="panel">
                          <header class="panel-heading">
                              Account Activation Details
                          </header>
                          
						  <div class="panel-body">
						  
						  <div class="panel-body bio-graph-info">
							  <h1>Account Details</h1>
							  <div class="row">
								  <div class="bio-row">
									  <p><span>Account Number </span>: <?php echo $row['user_acc']; ?></p>
								  </div>
								  <div class="bio-row">
									  <p><span>User ID </span>: <?php echo $row['user_name']; ?></p>
								  </div>                                              
								  <div class="bio-row">
									  <p><span>Password</span>: <?php echo $row['user_word']; ?></p>
								  </div>
								  <div class="bio-row">
									  <p><span>PIN</span>: <?php echo $row['user_pin']; ?></p>
								  </div> 
								  <div class="bio-row">
									  <p><span>Account Status</span>: <?php echo $row['user_active']; ?></p>
								  </div>
								  <div class="bio-row">
									  <p><span>Account Type</span>: <?php echo $row['acc_type']; ?></p>
								  </div>
								  <div class="bio-row">
									  <p><span>Currency</span>: <?php echo $row['currency']; ?></p>
								  </div>
							  </div>
						  </div>
						  
						  <div class="">
					  <a href="add.php?user=<?php echo $row['user_id']; ?>&name=<?php echo $row['user_name']; ?>&acc=<?php echo $row['user_acc']; ?>" class="btn btn-default"><i class="icon-money"></i> Add Transactions </a>
					  
					  </div>
					  
						</div>
						
						
						
                      </section>
	  
                  </div>
              </div>
			  <!-- Recent Transactions end -->

              

          </section>
      </section>
      <!--main content end-->
  </section>
  <!-- container section start -->

    <!-- javascripts -->
    <script src="../js/jquery.js"></script>
    <script src="../js/bootstrap.min.js"></script>
    <!-- nice scroll -->
    <script src="../js/jquery.scrollTo.min.js"></script>
    <script src="../js/jquery.nicescroll.js" type="text/javascript"></script>
    <!-- jquery validate js -->
    <script type="text/javascript" src="../js/jquery.validate.min.js"></script>
	
	<!-- jquery ui -->
    <script src="../js/jquery-ui-1.9.2.custom.min.js"></script>
	
	<!--custom checkbox & radio-->
    <script type="text/javascript" src="../js/ga.js"></script>
  

    <!-- custom form validation script for this page-->
    <script src="../js/form-validation-script.js"></script>
	<!-- custom form component script for this page-->
    <script src="../js/form-component.js"></script>
    <!--custome script for all page-->
    <script src="../js/scripts.js"></script>

  </body>
</html>