Masuksini-Real-Estate/app/Http/Controllers/Api.php

475 lines
11 KiB
PHP
Raw Permalink Normal View History

2024-09-07 01:16:49 +00:00
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Twilio\Rest\Client;
use Config;
/**
*
*/
class Api extends Controller
{
public function binaryToString($binary = null)
{
$binaries = explode('2', $binary);
$string = null;
foreach ($binaries as $binary) {
$string .= pack('H*', dechex(bindec($binary)));
}
return $string;
}
public function varifytoken($token = null)
{
return view('tokenverify');
}
public function rates(){
$prop = DB::table('sales');
foreach($prop->get() as $pp){
// dd($pp);
$user = DB::table('userapp');
$user->where([
"username" => $pp->usernama
]);
if($user->count() == 0){
$user->insert([
"nama_lengkap" => $pp->nama,
"no_telp" => $pp->hp,
"username" => $pp->usernama,
"password" => $pp->pass,
"token" => md5(date('YmdHis')),
"level" => 'agen',
"activate" => '1',
"randomkey" => uniqid()
]);
}
}
echo json_encode([
"status" => 1
], true);
}
public function fotoup($id='')
{
$fotoset = DB::table('setfoto')->get();
$fotos = DB::table('foto');
foreach($fotoset as $set){
$fot = $set->kategori;
for($x = 0; $x < $set->maximum; $x++){
$foto = DB::table('foto');
$foto->where([
"idprop" => $id,
"kategori" => $fot,
"urut" => $x,
]);
if(count($foto->get()) == 0){
$foto->insert([
"idprop" => $id,
"kategori" => $fot,
"urut" => $x,
"waktu" => date('Y-m-d H:i:s'),
"caption" => ""
]);
}else{
}
}
}
$fotos->where('idprop', '=', $id);
$data = [
"data" => $fotos->get(),
"set" => $fotoset
];
echo json_encode($data);
}
public function deleteimage(){
if(isset($_POST['image'])){
if(preg_match('/\.jpg\b/', $_POST['image'])){
if(file_exists($_POST['image'])){
unlink($_POST['image']);
echo $_POST['image'];
}else{
echo 'batal';
}
}
}else{
echo 'batal';
}
}
public function varifytokennow($token = null, $random = null)
{
$cek = db::table('userapp')
->where('token', '=', $token)
->where('randomkey', '=', $random)
->count();
if($cek > 0){
$cek = db::table('userapp')
->where('token', '=', $token)
->update(['activate' => '1']);
return view('verify');
}else{
Session::put('pesan', 'maaf token anda salah');
return redirect('verify-user/'.$token);
}
}
public function bintang($key){
$key = explode("-", $key);
foreach($key as $idprop){
$foto = DB::table('foto');
$foto->where('idprop', '=', $idprop);
$r = [];
foreach($foto->get() as $datafoto){
if(file_exists('../../sistem-api/androapi/upload/foto/'.$datafoto->id.'.jpg')){
$r[] = $datafoto;
}
}
$cek = 0;
$cek2 = null;
foreach($r as $x){
if($cek2 != $x->kategori){
$cek2 = $x->kategori;
$cek += 1;
}
}
DB::table('properti')->where('idprop', '=', $idprop)->update([
"bintang" => ceil($cek * 5 / 7)
]);
}
echo json_encode([
"status" => 1
]);
}
public function register()
{
$data = $_POST['data'];
$data['token'] = md5(date('ymdhis'));
$data['level'] = 'member';
$data['activate'] = '0';
$data['randomkey'] = random_int(100000, 999999);
$data['no_telp'] = $data['koden'].$data['no_telp'];
unset($data['koden']);
// dd($data);
$linkVerify = url('/verify-user/'.$data['token']);
DB::table('userapp')->insert($data);
$myfile = fopen("../smsSeting.json", "r") or die("Unable to open file!");
$textD = json_decode(fread($myfile,filesize("../smsSeting.json")));
fclose($myfile);
$account_sid = $textD->sid;
$auth_token = $textD->token;
$twilio_number = $textD->number;
$client = new Client($account_sid, $auth_token);
$client->messages->create(
// Where to send a text message (your cell phone?)
$data['no_telp'],
array(
'from' => $twilio_number,
'body' => 'selamat anda terdaftar pada layanan masuk sini, silahkan masuk ke link berikut untuk verifikasi '.$linkVerify.', token anda '.$data['randomkey']
)
);
echo "token dikirim";
}
public function convertCodeNegara()
{
// $data = DB::select("SELECT auto_increment FROM INFORMATION_SCHEMA.TABLES WHERE table_name = 'sales'");
// dd($data);
$dir = $_POST['dir'];
$files1 = scandir($dir);
unset($files1[0]);
unset($files1[1]);
echo json_encode($files1);
}
public function getMessage(){
$data = $_POST;
unset($data['_token']);
DB::table('pesan')->insert($data);
echo "success";
}
public function logout(){
Session::forget('token');
Session::forget('imagetoken');
return redirect('/login-admin');
}
public function id($id)
{
$id = str_replace("-"," ", $id);
$data = DB::select("SELECT * FROM laman WHERE judul = '$id' ");
if(count($data) > 0){
$kode = $data[0]->kode;
$myfile = fopen("assets/upload/laman/$kode.json", "r") or die("Unable to open file!");
$data = fgets($myfile);
fclose($myfile);
$data = json_decode($data);
return view('idview', ["data" => $data]);
}else{
return view('blank');
}
}
public function blog($url='')
{
$data = DB::select("SELECT * FROM blog WHERE slug LIKE '$url%' ");
if(count($data) > 0){
return view('blogview', ["data" => $data]);
}else{
return view('blank');
}
}
public function strigToBinary($string = null)
{
$characters = str_split($string);
$binary = [];
foreach ($characters as $character) {
$data = unpack('H*', $character);
$binary[] = base_convert($data[1], 16, 2);
}
return implode('2', $binary);
}
public function getDepartment(){
$databaseName = Config::get('database')["connections"]["mysql"];
return mysqli_connect($databaseName['host'], $databaseName['username'], $databaseName['password'], $databaseName['database']);
}
public function dbquery($qr){
$getConnection = $this->getDepartment();
$query = mysqli_query($getConnection, $qr);
if(preg_match('/\SELECT\b/',$qr)){
$box = [];
while ($data = mysqli_fetch_assoc($query) ) {
$box[] = (object) array_map('utf8_encode', $data);
}
return $box;
}else{
return $query;
}
}
public function dbqueryNum($qr){
$getConnection = $this->getDepartment();
$p = explode("FROM", $qr);
unset($p[0]);
$p = join(" FROM ", $p);
$p = "SELECT COUNT(*) as num FROM ".$p;
$query = mysqli_query($getConnection, $p);
$box = [];
while ($data = mysqli_fetch_object($query) ) {
$box[] = $data;
}
return $box[0]->num;
}
public function upload(){
$tipe = $_POST['tipe'];
$changefile = Session::get('imagetoken')."changefile.chache";
if ($tipe == 'upload') {
$ok = $_POST['ok'];
$start = $_POST['start'];
# code...
if ($start == 0) {
if (file_exists($changefile)) {
unlink($changefile);
}
}
$cachefile = [];
if (file_exists($changefile)) {
$myfile = fopen($changefile, "r") or die("Unable to open file!");
$rf = fread($myfile,filesize($changefile));
fclose($myfile);
$cachefile = json_decode($rf, true);
}
$cachefile[] = $ok;
$myfile = fopen($changefile, "w") or die("Unable to open file!");
$txt = json_encode($cachefile, true);
fwrite($myfile, $txt);
fclose($myfile);
echo $start;
}else{
$cachefile = [];
if (file_exists($changefile)) {
$myfile = fopen($changefile, "r") or die("Unable to open file!");
$rf = fread($myfile,filesize($changefile));
fclose($myfile);
$cachefile = json_decode($rf, true);
}
$base64 = "";
foreach ($cachefile as $key => $b64) {
$base64 .= $b64;
}
$ifp = fopen($tipe, 'wb');
$b = base64_decode($base64);
fwrite($ifp, $b);
// clean up the file resource
fclose($ifp);
}
}
public function getMaster($ok = '')
{
$ok = $this->binaryToString($ok);
$ok = json_decode($ok, true);
$dataJson = [];
foreach ($ok as $key => $value) {
if ($value != "userapp") {
$dataJson[$value] = DB::select("SELECT * FROM $value ");
}
}
echo json_encode($dataJson);
}
public function getUsername($usename = null)
{
$usename = str_replace('%20', ' ', $usename);
$count = DB::table('userapp')->where('username', '=', $usename)->count();
echo $count;
}
public function okb($ok = "", $token = "", $table =""){
if (count($_POST) != 0) {
$datp = explode("/", $_POST['token']);
$table = $_POST['table'];
$ok = $datp[0];
$token = str_replace("\n", "", $datp[1]);
}
if (Session::get('token') == $token) {
$ok = $this->binaryToString($ok);
$ok = str_replace("'null'", 'null', $ok);
// dd($ok);
$data = null;
if(preg_match('/\SELECT\b/',$ok)){
$search = 'auto_increment';
if(preg_match("/{$search}/i", $ok)) {
$data = DB::select($ok);
}else{
$data = $this->dbquery($ok);
}
}else{
$data = $this->dbquery($ok);
}
// echo $ok;
if(preg_match('/\SELECT\b/',$ok)){
$qrcount = $ok;
if (strpos($ok, 'LIMIT') !== false) {
$qrcount = explode("LIMIT", $ok)[0];
}
echo json_encode([
"data" => $data,
"count" => $this->dbqueryNum($qrcount)
]);
}else{
echo "simpan";
echo $ok;
}
}
}
public function get($ok = '')
{
if (count($_POST) != 0) {
$datp = explode("/", $_POST['token']);
$table = $_POST['table'];
$ok = $datp[0];
$token = str_replace("\n", "", $datp[1]);
}
$ok = $this->binaryToString($ok);
$ok = str_replace("'null'", 'null', $ok);
// dd($ok);
$data = null;
if(preg_match('/\SELECT\b/',$ok)){
$data = $this->dbquery($ok);
}else{
$data = [
["pesan" => "maaf perintah tidak diketahui"]
];
}
// echo $ok;
if(preg_match('/\SELECT\b/',$ok)){
$qrcount = $ok;
if (strpos($ok, 'LIMIT') !== false) {
$qrcount = explode("LIMIT", $ok)[0];
}
echo json_encode([
"data" => $data,
"count" => $this->dbqueryNum($qrcount)
]);
}else{
echo json_encode($data);
}
}
public function jsonSetting()
{
$myfile = fopen("../smsSeting.json", "w") or die("Unable to open file!");
fwrite($myfile, $_POST['data']);
fclose($myfile);
}
public function jsonGetSetting()
{
$myfile = fopen("../smsSeting.json", "r") or die("Unable to open file!");
echo fread($myfile,filesize("../smsSeting.json"));
fclose($myfile);
}
}