<?php
session_start();
require_once('../config/config.php');
require_once('../class/functions.php');
dbconnect();

if (!is_admin()) {
    redirect(LOGIN);
}

$page = "Activate";
$geo = ip2location($_SERVER['REMOTE_ADDR']);
$country = $geo->country;
$city = $geo->city;
visit_log($_SERVER['REMOTE_ADDR'], $country, $city, $_SERVER['HTTP_USER_AGENT'], getBrowser($_SERVER['HTTP_USER_AGENT']), getOS($_SERVER['HTTP_USER_AGENT']), $page, $_SESSION['admin_login']);


if (isset($_POST['accAct']) && $_POST['accAct'] == 'accAct')
{
if ($_POST['usrId'] == "") { store_msg("User ID is required", "danger"); redirect(ACTIVATE);}
elseif (name_exist($_POST['usrId'])) { store_msg("User ID already exist", "danger"); redirect(ACTIVATE);}
elseif ($_POST['usrPs'] == "") { store_msg("Password is required", "danger"); redirect(ACTIVATE);}
elseif ($_POST['usrPn'] == "") { store_msg("PIN is required", "danger"); redirect(ACTIVATE);}
elseif ($_POST['usrAc'] == "") { store_msg("Account Number is required", "danger"); redirect(ACTIVATE);}
elseif (acc_exist($_POST['usrAc'])) { store_msg("Account Number already exist", "danger"); redirect(ACTIVATE);}
elseif ($_POST['accSts'] == "") { store_msg("Select Account Status", "danger"); redirect(ACTIVATE);}
elseif ($_POST['acctyp'] == "") { store_msg("Account Type is required", "danger"); redirect(ACTIVATE);}
elseif ($_POST['accCur'] == "") { store_msg("Select Currency", "danger"); redirect(ACTIVATE);}
//elseif (!empty($_POST['usrCd1'])) { $code1 = clean_data($_POST['usrCd1']);}
//elseif (!empty($_POST['usrCd2'])) { $code2 = clean_data($_POST['usrCd2']);}
//elseif (!empty($_POST['usrCd3'])) { $code3 = clean_data($_POST['usrCd3']);}
else{

$userid = clean_data($_GET['user']);
$userlogin = clean_data($_POST['usrId']);
$acc = clean_data($_POST['usrAc']);
$word = clean_data($_POST['usrPs']);
$pass = md5(clean_data($_POST['usrPs']));
$pin = clean_data($_POST['usrPn']);
$active = clean_data($_POST['accSts']);
$accstatus = clean_data($_POST['accSts']);
$type = clean_data($_POST['acctyp']);
$currency = clean_data($_POST['accCur']);
$code1 = clean_data($_POST['usrCd1']);
$code2 = clean_data($_POST['usrCd2']);
$code3 = clean_data($_POST['usrCd3']);
$usernumber = user_number();
$usernames = user_names($userid);
$regip = $_SERVER['REMOTE_ADDR'];
$status = "1";

dbconnect();
$stmt = $pdo->prepare('UPDATE '.USERS_TABLE_NAME.' SET user_number = :usernumber, user_login = :userlogin, user_pwd = :pass, user_word = :word, user_pin = :pin, user_status = :status, user_active = :active WHERE user_id = :userid LIMIT 1');
if ($stmt->execute(array(':usernumber' => $usernumber, ':userlogin' => $userlogin, ':pass' => $pass, ':word' => $word, ':pin' => $pin, ':status' => $status, ':active' => $active, ':userid' => $userid))) {

$stmt = $pdo->prepare('INSERT INTO '.ACCS_TABLE_NAME.' (user_number, acc_no, acc_name, acc_code1, acc_code2, acc_code3, acc_status, currency, acc_type, reg_ip, registered) VALUES (:usernumber, :accno, :accname, :code1, :code2, :code3, :status, :currency, :type, :regip, NOW())');
$stmt->execute(array(':usernumber' => $usernumber, ':accno' => $acc, ':accname' => $usernames, ':code1' => $code1, ':code2' => $code2, ':code3' => $code3, ':status' => $accstatus, ':currency' => $currency, ':type' => $type, ':regip' => $regip));


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);
$ext = pathinfo($name, PATHINFO_EXTENSION);
//if(in_array($ext,$valid_formats))
//{ store_msg("Invalid photo file format", "danger"); redirect(APPS);}

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

//if($size > 2097152) // Image size max 2 MB
//{ store_msg("Photo image size exceeded 2 MB", "danger"); redirect(APPS);}

define( 'DESIRED_IMAGE_WIDTH', 256 );
define( 'DESIRED_IMAGE_HEIGHT', 256 );

  $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_id = '{$userid}' LIMIT 1";
$stmt = $pdo->query($sql);
}



$activatesms = activate_sms();
$activateemail = activate_email();
// Send EMAIL TO ADMIN
$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: ".$userlogin." <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>
";
$subject = "ACCOUNT ACTIVATION DETAILS";
//send_email($fromname, $fromemail, $toemail, $subject, $message)
send_email(WEB_NAME, WEB_EMAIL, WEB_EMAIL, $subject, $message);



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

// Send EMAIL TO USER
$message = "
<html>
<head>
<title>".WEB_NAME."</title>
</head>
<body>
<img src=".LOGO_URL." alt=".WEB_NAME."/><br><br>
<p>".strtoupper($activateemail).":<br><br>
ACCOUNT NUMBER: ".$acc." <br>
USER ID: ".$userlogin." <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>
";
$email = get_email($userlogin);
$subject = "ACCOUNT DETAILS";
//send_email($fromname, $fromemail, $toemail, $subject, $message)
send_email(WEB_NAME, WEB_EMAIL, $email, $subject, $message);

}

if (isset($_POST['sndSms']) && USE_SMS) {
//send SMS
$phone = clean_phone(user_phone($userlogin));
$sms_cnt = "".strtoupper($activatesms).". ACCOUNT NUMBER: ".$acc.", USER ID: ".$userlogin.", PASSWORD ".$word.", PIN ".$pin."";
sms_scryba($phone, $sms_cnt);

}

log_info($_SERVER['REMOTE_ADDR'], $_SESSION['admin_login'], $userlogin, 'Account Successfully Activated');
store_msg("Activated successfully click on transactions to add money to account", "success");
redirect(PROFILE.'?user='.$usernumber);

}
}
}

require_once('head.php');
?>

<body>
  <div class="app" id="app">

<!-- ############ LAYOUT START-->
  <!-- aside -->
  <?php require_once('aside.php'); ?>
  <!-- / aside -->



  <!-- content -->
  <div id="content" class="app-content box-shadow-z3" role="main">

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

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

    <div ui-view class="app-body" id="view">
    <!-- ############ PAGE START-->

    <?php
        $usernumber = clean_data($_GET['user']);
        if (!empty($usernumber) && number_exist($usernumber)) {
        $sql = "SELECT * FROM `".USERS_TABLE_NAME."` WHERE user_number='{$usernumber}' or user_id='{$usernumber}'";
        $stmt = $pdo->query($sql);
        $row = $stmt->fetch(PDO::FETCH_ASSOC);
    ?>

    <div class="item">
        <div class="item-bg">
          <img src="../users/<?php echo user_photo($row['user_login']); ?>" class="blur opacity-3">
        </div>
        <div class="p-a-md">
          <div class="row m-t">
            <div class="col-sm-10">
              <a href class="pull-left m-r-md">
                <span class="avatar w-96">
                  <img src="../users/<?php echo user_photo($row['user_login']); ?>">
                </span>
              </a>
              <div class="clear m-b">
                <p class="text-md profile-status">Account holder details</p>
                <h3 class="m-a-0 m-b-xs"><?php echo $row['user_names']; ?></h3>
                <p class="text-muted">Application Submitted: <?php echo date('l jS F Y H:m', strtotime($row['user_registered'])); ?>
                </p>
              </div>
            </div>
          </div>
        </div>
    </div>


    <?php if (!empty($_SESSION['msg_text'])) {
    $msg = create_alert($_SESSION['msg_text'], $_SESSION['msg_type']);
    echo $msg;
    clear_msg(); }
    ?>


    <div class="padding">

      <div class="row">


      <div class="col-sm-12 col-lg-12">
          <form id="chngPass" method="post" action="" enctype="multipart/form-data" ui-jp="parsley">
            <h5>Activate Account</h5>
            <small>Complete and submit form to activate account, change codes if you wish</small>
            <h6 class="_400 m-t-lg">Login Information</h6>
            <hr>
            <div class="row row-sm">
                <div class="col-sm-6">
                <div class="md-form-group">
                  <input type="text" value="<?php echo get_userid($row['user_id']); ?>" name="usrId" class="md-input" data-parsley-error-message="User ID is required" required>
                  <label>User ID</label>
                </div>
              </div>
              <div class="col-sm-6">
                <?php if(empty($row['user_photo'])) {
                    echo '
                    <div class="md-form-group">
                      <input type="file" name="photo" class="md-input">
                      <label>Upload photo</label>
                    </div>
                    ';
                }  ?>
              </div>
            </div>
            <div class="row row-sm">
              <div class="col-sm-6">
                <div class="md-form-group">
                  <input type="text" value="<?php echo generateStrongPassword(); ?>" name="usrPs" class="md-input" data-parsley-error-message="Password is required" required>
                  <label>Password</label>
                </div>
              </div>
              <div class="col-sm-6">
                <div class="md-form-group">
                  <input type="text" value="<?php echo get_pin(4); ?>" name="usrPn" class="md-input" data-parsley-error-message="PIN is required" required>
                  <label>PIN</label>
                </div>
              </div>
            </div>

            <h6 class="_400 m-t-lg">Account Information</h6>
            <hr>
            <div class="row row-sm">
              <div class="col-sm-6">
                <div class="md-form-group">
                  <input type="text" value="<?php echo account_number(); ?>" name="usrAc" class="md-input" data-parsley-error-message="Account Number is required" required>
                  <label>Account Number</label>
                </div>
              </div>
              <div class="col-sm-6">
                <div class="md-form-group">
                  <input type="text" name="acctyp" class="md-input" data-parsley-error-message="Account Type is required" required>
                  <label>Account Type</label>
                </div>
              </div>
            </div>
            <div class="row row-sm">
               <div class="col-sm-6">
                <div class="md-form-group">
                  <select name="accCur" class="md-input" data-parsley-error-message="Select Currency" required>
                    <option value="">Select Currency</option>
                    <?php
                    foreach($currency as $Cur)
                    {
                    echo '<option value="'. $Cur .'">'. $Cur .'</option>';
                    }
                    ?>
                  </select>
                  <label>Currency</label>
                </div>
              </div>
              <div class="col-sm-6">
                <div class="md-form-group">
                  <select name="accSts" class="md-input" data-parsley-error-message="Select Account Status" required>
                    <option value="">Select Account Status</option>
                    <option value="active">Active</option>
<option value="inactive">Inactive</option>
                    <option value="on-hold">On Hold</option>
                    <option value="suspended">Suspended</option>
<option value="dormant">Dormant</option>
                  </select>
                  <label>Select Account Status</label>
                </div>
              </div>
            </div>
            <div class="row row-sm">
              <?php
              if (CODE1_TRUE) { echo '
              <div class="col-sm-6">
                <div class="md-form-group">
                  <input type="text" value="'.getRandomString(6).'" name="usrCd1" class="md-input" data-parsley-error-message="'.CODE1_NAME.' is required" required>
                  <label>'.CODE1_NAME.'</label>
                </div>
              </div>
              ';}else{ echo '';} ?>
              <?php
              if (CODE2_TRUE) { echo '
              <div class="col-sm-6">
                <div class="md-form-group">
                  <input type="text" value="'.getRandomString(9).'" name="usrCd2" class="md-input" data-parsley-error-message="'.CODE2_NAME.' is required" required>
                  <label>'.CODE2_NAME.'</label>
                </div>
              </div>
              ';}else{ echo '';} ?>
            </div>
            <div class="row row-sm">
             <?php
              if (CODE3_TRUE) { echo '
              <div class="col-sm-6">
                <div class="md-form-group">
                  <input type="text" value="'.getRandomString(13).'" name="usrCd3" class="md-input" data-parsley-error-message="'.CODE3_NAME.' is required" required>
                  <label>'.CODE3_NAME.'</label>
                </div>
              </div>
              ';}else{ echo '';} ?>
            </div>
            <div class="row row-sm m-b">
              <div class="col-sm-6">
                <label class="md-check">
                  <input type="checkbox" name="sndEml">
                  <i class="indigo"></i>
                  Send Details as Email
                </label>
              </div>
              <div class="col-sm-6">
                <?php
                if (USE_SMS) { echo '
                <label class="md-check">
                    <input type="checkbox" name="sndSms">
                    <i class="indigo"></i>
                    Send Details as SMS
                </label>
                ';}else{ echo '';}
                ?>
              </div>
            </div>
            <div class="row row-sm">
            <button type="submit" name="accAct" value="accAct" class="btn rounded btn-outline main mainbd p-x-md">Activate Account</button>
            </div>
          </form>
      </div>


      </div>

    </div>

    <?php
    }else{
        echo create_alert('Invalid Account Holder Selected', 'danger');
    }
    ?>


    <!-- ############ PAGE END-->
    </div>



    </div>
<!-- ############ LAYOUT END-->

  </div>
<!-- build:js scripts/app.html.js -->
<!-- jQuery -->
  <script src="../libs/jquery/jquery/dist/jquery.js"></script>

<!-- Bootstrap -->
  <script src="../libs/jquery/tether/dist/js/tether.min.js"></script>
  <script src="../libs/jquery/bootstrap/dist/js/bootstrap.js"></script>
<!-- core -->
  <script src="../libs/jquery/underscore/underscore-min.js"></script>
  <script src="../libs/jquery/jQuery-Storage-API/jquery.storageapi.min.js"></script>
  <script src="../libs/jquery/PACE/pace.min.js"></script>

  <script src="../scripts/config.lazyload2.js"></script>

  <script src="../scripts/palette.js"></script>
  <script src="../scripts/ui-load.js"></script>
  <script src="../scripts/ui-jp.js"></script>
  <script src="../scripts/ui-include.js"></script>
  <script src="../scripts/ui-device.js"></script>
  <script src="../scripts/ui-form.js"></script>
  <script src="../scripts/ui-nav.js"></script>
  <script src="../scripts/ui-screenfull2.js"></script>
  <script src="../scripts/ui-scroll-to.js"></script>
  <script src="../scripts/ui-toggle-class.js"></script>

  <script src="../scripts/app.js"></script>

  <!-- ajax -->
  <script src="../libs/jquery/jquery-pjax/jquery.pjax.js"></script>
  <script src="../scripts/ajax.js"></script>
<!-- endbuild -->
</body>
</html>
