<?php
defined('BASEPATH') or exit('No direct script access allowed');

class Api_password_activity_lupa extends CI_Controller
{

	public function __construct()
	{
		parent::__construct();
		$this->load->helper(['jwt', 'authorization']);
	}


	public function index()
	{
		if ($_SERVER['REQUEST_METHOD'] == 'POST') {
			date_default_timezone_set("Asia/Jakarta");
			$mst_user_email =  $this->mst_user_email();
			$mst_user_password_normal =  "Pass" . strtoupper(substr(sha1(uniqid()), 0, 5));
			$mst_user_password_sha1 =  sha1($mst_user_password_normal);
			$mst_user_token =  "TOKEN-" . strtoupper(substr(sha1(uniqid()), 0, 10)) . "-" . rand(0, 999) . "-" . date('d-m-Y--H:m:s');

			//CEK EMAIL
			$email = $this->db->query("SELECT * FROM tbl_mst_user WHERE mst_user_email='$mst_user_email'");
			if ($email->num_rows() > 0) {
				foreach ($email->result() as $row) {
					$mst_user_status = $row->mst_user_status;
				}

				if ($mst_user_status == "0") {
					//KIRIM EMAIL
					$config = array(
						'protocol' => 'smtp',
						'smtp_host' => 'ssl://mail.Arifah Pulsa.com',
						'smtp_port' => 465,
						'smtp_user' => 'noreply@Arifah Pulsa.com',
						'smtp_pass' => 'Byfm1979*',
						'mailtype'  => 'html',
						'charset'   => 'iso-8859-1',
						'priority' => '1',
						'crlf' => '\r\n',
						'validate' => true,
						'wordwrap' => true,
						'starttls'  => true,
						'newline' => '\r\n',
					);

					$this->load->library('email', $config);

					$this->email->from('noreply@Arifah Pulsa.com', 'arifahpulsa');
					$this->email->to($mst_user_email);
					$this->email->subject('Arifah Pulsa Password');
					$this->email->message($this->email_isi($mst_user_password_normal));
					if ($this->email->send()) {
						//UPDATE TOKEN & PASSWORD
						$this->db->query("UPDATE tbl_mst_user set mst_user_token='$mst_user_token',mst_user_password='$mst_user_password_sha1' WHERE mst_user_email='$mst_user_email'");

						$sess["status"] = "001";
						$sess["pesan"] = $this->config->item('error_lupa_password_berhasil');
						$keytoken = strtoupper(substr(sha1(uniqid()), 0, 16));;
						$sess['data'] = array(
							'token' =>  encrypt($keytoken, $this->config->item('keytoken')),
							'value' =>  encrypt(json_encode(array()), $keytoken)
						);
						echo json_encode($sess);
					} else {
						//EMAIL TIDAK TERKIRIM
						$sess["status"] = "002";
						$sess["pesan"] = $this->config->item('error_lupa_password_gagal');
						$keytoken = strtoupper(substr(sha1(uniqid()), 0, 16));;
						$sess['data'] = array(
							'token' =>  encrypt($keytoken, $this->config->item('keytoken')),
							'value' =>  encrypt(json_encode(array()), $keytoken)
						);
						echo json_encode($sess);
					}
				}

				//AKUN PENDING
				if ($mst_user_status == "1") {
					$sess["status"] = "005";
					$sess["pesan"] = $this->config->item('error_lupa_password_pending');
					$keytoken = strtoupper(substr(sha1(uniqid()), 0, 16));;
					$sess['data'] = array(
						'token' =>  encrypt($keytoken, $this->config->item('keytoken')),
						'value' =>  encrypt(json_encode(array()), $keytoken)
					);
					echo json_encode($sess);
				}

				//AKUN DIBLOKIR
				if ($mst_user_status == "2") {
					$sess["status"] = "006";
					$sess["pesan"] = $this->config->item('error_lupa_password_blokir');
					$keytoken = strtoupper(substr(sha1(uniqid()), 0, 16));;
					$sess['data'] = array(
						'token' =>  encrypt($keytoken, $this->config->item('keytoken')),
						'value' =>  encrypt(json_encode(array()), $keytoken)
					);
					echo json_encode($sess);
				}
			} else {
				//BELUM TERDAFTAR
				$sess["status"] = "007";
				$sess["pesan"] = $this->config->item('error_lupa_password_belum_terdaftar');
				$keytoken = strtoupper(substr(sha1(uniqid()), 0, 16));;
				$sess['data'] = array(
					'token' =>  encrypt($keytoken, $this->config->item('keytoken')),
					'value' =>  encrypt(json_encode(array()), $keytoken)
				);
				echo json_encode($sess);
			}
		}
	}

	private function email_isi($mst_user_password)
	{
		$isi = '<!DOCTYPE html><html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office"><head>
		<meta http-equiv="X-UA-Compatible" content="IE=edge" />
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1" />
		<style type="text/css">
		  #outlook a {
			padding: 0;
		  }
	  
		  body {
			margin: 0;
			padding: 0;
			-webkit-text-size-adjust: 100%;
			-ms-text-size-adjust: 100%;
		  }
	  
		  table,
		  td {
			border-collapse: collapse;
			mso-table-lspace: 0pt;
			mso-table-rspace: 0pt;
		  }
	  
		  img {
			border: 0;
			height: auto;
			line-height: 100%;
			outline: none;
			text-decoration: none;
			-ms-interpolation-mode: bicubic;
		  }
	  
		  p {
			display: block;
			margin: 13px 0;
		  }
		</style>
		<link href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,700" rel="stylesheet" type="text/css" />
		<style type="text/css">
		  @import url(https://fonts.googleapis.com/css?family=Roboto:100,300,400,700);
		</style>
		<style type="text/css">
		  @media only screen and (min-width:480px) {
			.mj-column-per-100 {
			  width: 100% !important;
			  max-width: 100%;
			}
		  }
		</style>
		<style type="text/css">
		  @media only screen and (max-width:480px) {
			table.mj-full-width-mobile {
			  width: 100% !important;
			}
	  
			td.mj-full-width-mobile {
			  width: auto !important;
			}
		  }
		</style>
		<style type="text/css">
		  a,
		  span,
		  td,
		  th {
			-webkit-font-smoothing: antialiased !important;
			-moz-osx-font-smoothing: grayscale !important;
		  }
		</style>
	  <input type="hidden" id="_w_simile" data-inspect-config="3" /><script type="text/javascript" src="chrome-extension://dbjbempljhcmhlfpfacalomonjpalpko/scripts/inspector.js"></script></head>
	  
	  <body style="background-color:#f3f3f5;">
		<div style="display:none;font-size:1px;color:#ffffff;line-height:1px;max-height:0px;max-width:0px;opacity:0;overflow:hidden;"></div>
		<div style="background-color:#f3f3f5;">
		  <div style="margin:0px auto;max-width:600px;">
			<table align="center" border="0" cellpadding="0" cellspacing="0" role="presentation" style="width:100%;">
			  <tbody>
				<tr>
				  <td style="direction:ltr;font-size:0px;padding:20px 0;text-align:center;">
					<div class="mj-column-per-100 mj-outlook-group-fix" style="font-size:0px;text-align:left;direction:ltr;display:inline-block;vertical-align:top;width:100%;">
					  <table border="0" cellpadding="0" cellspacing="0" role="presentation" style="vertical-align:top;" width="100%">
						<tbody><tr>
						  <td style="font-size:0px;word-break:break-word;">
							<div style="height:20px;"></div>
						  </td>
						</tr>
					  </tbody></table>
					</div>
				
				  </td>
				</tr>
			  </tbody>
			</table>
		  </div>
		  <div style="background:#54595f;background-color:#54595f;margin:0px auto;border-radius:4px 4px 0 0;max-width:600px;">
			<table align="center" border="0" cellpadding="0" cellspacing="0" role="presentation" style="background:#54595f;background-color:#54595f;width:100%;border-radius:4px 4px 0 0;">
			  <tbody>
				<tr>
				  <td style="direction:ltr;font-size:0px;padding:20px 0;text-align:center;">
				
					<div style="margin:0px auto;max-width:600px;">
					  <table align="center" border="0" cellpadding="0" cellspacing="0" role="presentation" style="width:100%;">
						<tbody>
						  <tr>
							<td style="direction:ltr;font-size:0px;padding:0px;text-align:center;">
							  <div class="mj-column-per-100 mj-outlook-group-fix" style="font-size:0px;text-align:left;direction:ltr;display:inline-block;vertical-align:top;width:100%;">
								<table border="0" cellpadding="0" cellspacing="0" role="presentation" style="vertical-align:top;" width="100%">
								  <tbody><tr>
									<td align="center" style="font-size:0px;padding:10px 25px;word-break:break-word;">
									  <table border="0" cellpadding="0" cellspacing="0" role="presentation" style="border-collapse:collapse;border-spacing:0px;">
										<tbody>
										  <tr>
											<td style="width:150px;">
											  <img height="auto" src="https://Arifah Pulsa.com/assets/img/icon_warna.png" style="border:0;display:block;outline:none;text-decoration:none;height:auto;width:100%;font-size:13px;" width="150" />
											</td>
										  </tr>
										</tbody>
									  </table>
									</td>
								  </tr>
								  <tr>
									<td style="font-size:0px;word-break:break-word;">
									  <div style="height:20px;"></div>
									</td>
								  </tr>
								  <tr>
									<td align="center" style="font-size:0px;padding:10px 25px;word-break:break-word;">
									  <div style="font-family:Roboto, Helvetica, Arial, sans-serif;font-size:24px;font-weight:400;line-height:30px;text-align:center;color:#ffffff;">
										<h1 style="margin: 0; font-size: 24px; line-height: normal; font-weight: 400;">Password Baru</h1>
									  </div>
									</td>
								  </tr>
								  <tr>
									<td align="center" style="font-size:0px;padding:10px 25px;word-break:break-word;">
									  <div style="font-family:Roboto, Helvetica, Arial, sans-serif;font-size:14px;font-weight:400;line-height:20px;text-align:center;color:#ffffff;">
										<p style="margin: 0;">Hi pengguna Arifah Pulsa, berikut adalah password baru kamu untuk login di aplikasi Arifah Pulsa. Mohon dijaga kerahasiaan password kamu,
											pihak Arifah Pulsa tidak bertanggung jawab atas kelalaian sendiri terhadap akun kamu. Jangan beritahukan password tersebut kepada siapapun
											termasuk kepada pihak Arifah Pulsa sendiri </p>
									  </div>
									</td>
								  </tr>
								  <tr>
									<td align="center" vertical-align="middle" style="font-size:0px;padding:10px 25px;word-break:break-word;">
									  <table border="0" cellpadding="0" cellspacing="0" role="presentation" style="border-collapse:separate;line-height:100%;">
										<tbody><tr>
										  <td align="center" bgcolor="#2e58ff" role="presentation" style="border:none;border-radius:3px;cursor:auto;mso-padding-alt:10px 25px;background:#2e58ff;" valign="middle">
											<a style="display: inline-block; background: #2e58ff; color: white; font-family: Roboto, Helvetica, Arial, sans-serif; font-size: 14px; font-weight: normal; line-height: 32px; margin: 0; text-decoration: none; text-transform: none; padding: 10px 25px; mso-padding-alt: 0px; border-radius: 3px;" target="_blank">
											  <strong>' . $mst_user_password . '</strong>
											</a>
										  </td>
										</tr>
									  </tbody></table>
									</td>
								  </tr>
								</tbody></table>
							  </div>
							</td>
						  </tr>
						</tbody>
					  </table>
					</div>
					<div style="margin:0px auto;max-width:600px;">
					  <table align="center" border="0" cellpadding="0" cellspacing="0" role="presentation" style="width:100%;">
						<tbody>
						  <tr>
							<td style="direction:ltr;font-size:0px;padding:20px 0;text-align:center;">
		
							  <div class="mj-column-per-100 mj-outlook-group-fix" style="font-size:0px;text-align:left;direction:ltr;display:inline-block;vertical-align:top;width:100%;">
								<table border="0" cellpadding="0" cellspacing="0" role="presentation" style="vertical-align:top;" width="100%">
								  <tbody><tr>
									<td align="center" style="font-size:0px;padding:10px 25px;word-break:break-word;">
									  <div style="font-family:Roboto, Helvetica, Arial, sans-serif;font-size:14px;font-weight:400;line-height:20px;text-align:center;color:#ffffff;">
										<p style="margin: 0;">Terimakasih<br><strong>Arifah Pulsa</strong></p>
									  </div>
									</td>
								  </tr>
								</tbody></table>
							  </div>
			
							</td>
						  </tr>
						</tbody>
					  </table>
					</div>
			   
				  </td>
				</tr>
			  </tbody>
			</table>
		  </div>
	
		  <div style="background:#ffffff;background-color:#ffffff;margin:0px auto;border-radius:0 0 4px 4px;max-width:600px;">
			<table align="center" border="0" cellpadding="0" cellspacing="0" role="presentation" style="background:#ffffff;background-color:#ffffff;width:100%;border-radius:0 0 4px 4px;">
			  <tbody>
				<tr>
					<div class="mj-column-per-100 mj-outlook-group-fix" style="font-size:0px;text-align:left;direction:ltr;display:inline-block;vertical-align:top;width:100%;">
					  <table border="0" cellpadding="0" cellspacing="0" role="presentation" style="vertical-align:top;" width="100%">
						<tbody><tr>
						  <td align="center" style="font-size:0px;padding:10px 25px;word-break:break-word;">
							<div style="font-family:Roboto, Helvetica, Arial, sans-serif;font-size:14px;font-weight:400;line-height:20px;text-align:center;color:#93999f;">
								Arifah Pulsa<br>
								<a class="footer-link" href="#" style="color: #2e58ff; text-decoration: none;">info@Arifah Pulsa.com</a> <br /> 
								<a class="footer-link" href="#" style="color: #2e58ff; text-decoration: none;">www.Arifah Pulsa.com</a></div>
						  </td>
						</tr>
						<tr>
						  <td align="center" style="font-size:0px;padding:10px 25px;word-break:break-word;">
		
						   
						
							<table align="center" border="0" cellpadding="0" cellspacing="0" role="presentation" style="float:none;display:inline-table;">
							  <tbody><tr>
								<td style="padding:4px;">
								  <table border="0" cellpadding="0" cellspacing="0" role="presentation" style="border-radius:3px;width:24px;">
									<tbody><tr>
									  <td style="font-size:0;height:24px;vertical-align:middle;width:24px;">
										<a href="https://www.facebook.com/Arifah Pulsa.2023" target="_blank" style="color: #2e58ff; text-decoration: none;">
										  <img alt="facebook-logo" height="24" src="https://codedmails.com/images/social/color/facebook-logo-transparent.png" style="border-radius:3px;display:block;" width="24" />
										</a>
									  </td>
									</tr>
								  </tbody></table>
								</td>
							  </tr>
							</tbody></table>
						  </td>
						</tr>
					  </tbody></table>
					</div>
				  </td>
				</tr>
			  </tbody>
			</table>
		  </div>
		  <div style="margin:0px auto;max-width:600px;">
			<table align="center" border="0" cellpadding="0" cellspacing="0" role="presentation" style="width:100%;">
			  <tbody>
				<tr>
				  <td style="direction:ltr;font-size:0px;padding:20px 0;text-align:center;">
							<div style="height:1px;">   </div> 
						  </td>
						</tr>
					  </tbody></table>
					</div>
					
				  </td>
				</tr>
			  </tbody>
			</table>
		  </div>
		</div>
	  
	  
	  </body></html>';

		return $isi;
	}

	private function mst_user_email()
	{
		$input = decrypt($this->input->post("mst_user_email", true), $this->config->item('key'));
		$token = AUTHORIZATION::generateToken($input);
		try {
			$data = AUTHORIZATION::validateToken($token);
			if ($data === false) {
				$sess["status"] = "004";
				$sess["pesan"] = $this->config->item('error_parameter');
				$keytoken = strtoupper(substr(sha1(uniqid()), 0, 16));;
				$sess['data'] = array(
					'token' =>  encrypt($keytoken, $this->config->item('keytoken')),
					'value' =>  encrypt(json_encode(array()), $keytoken)
				);
				echo json_encode($sess);
				exit();
			} else {
				return $data;
			}
		} catch (Exception $e) {
			$sess["status"] = "004";
			$sess["pesan"] = $this->config->item('error_parameter');
			$keytoken = strtoupper(substr(sha1(uniqid()), 0, 16));;
			$sess['data'] = array(
				'token' =>  encrypt($keytoken, $this->config->item('keytoken')),
				'value' =>  encrypt(json_encode(array()), $keytoken)
			);
			echo json_encode($sess);
			exit();
		}
	}

	private function mst_user_password()
	{
		$input = decrypt($this->input->post("mst_user_password", true), $this->config->item('key'));
		$token = AUTHORIZATION::generateToken($input);
		try {
			$data = AUTHORIZATION::validateToken($token);
			if ($data === false) {
				$sess["status"] = "004";
				$sess["pesan"] = $this->config->item('error_parameter');
				$keytoken = strtoupper(substr(sha1(uniqid()), 0, 16));;
				$sess['data'] = array(
					'token' =>  encrypt($keytoken, $this->config->item('keytoken')),
					'value' =>  encrypt(json_encode(array()), $keytoken)
				);
				echo json_encode($sess);
				exit();
			} else {
				return $data;
			}
		} catch (Exception $e) {
			$sess["status"] = "004";
			$sess["pesan"] = $this->config->item('error_parameter');
			$keytoken = strtoupper(substr(sha1(uniqid()), 0, 16));;
			$sess['data'] = array(
				'token' =>  encrypt($keytoken, $this->config->item('keytoken')),
				'value' =>  encrypt(json_encode(array()), $keytoken)
			);
			echo json_encode($sess);
			exit();
		}
	}
}
