t3ll0

Aplikasi.php

Jun 20th, 2016
97
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php if(!defined('BASEPATH')) exit('No direct script access allowed');
  2.  
  3. class Aplikasi extends CI_Controller {
  4.     public function __construct() {
  5.         parent::__construct();
  6.  
  7.         if(!$this->session->has_userdata("username")) {
  8.             $this->session->set_flashdata("info", "Silahkan login untuk melanjutkan pekerjaan anda.");
  9.             redirect("sys_auth");
  10.         }
  11.  
  12.         $this->load->library(["form_validation", "encrypt"]);
  13.     }
  14.  
  15.     public function index() {
  16.         $app = $this->app_model->get_by();
  17.  
  18.          // var_dump($this->encrypt->gembok("wordpress5", "wp5"));die();
  19.  
  20.         foreach($app as &$a) {
  21.             switch($a["status"]) {
  22.                 case 0:
  23.                     $a["status"] = '<div class="#ff1744 red accent-3">Nonaktif</div>';
  24.                 break;
  25.  
  26.                 case 1:
  27.                     $a["status"] = '<div class="teal lighten-2">Aktif</div>';
  28.                 break;
  29.  
  30.                 default:
  31.                     $a["status"] = '<div class="#bdbdbd grey lighten-1">N/A</div>';
  32.                 break;
  33.             }
  34.         }
  35.  
  36.         $this->twig->display("_backend/app.c", [
  37.             "title" => "List App",
  38.             "app" => $app,
  39.             "success" => $this->session->flashdata("success"),
  40.             "danger" => $this->session->flashdata("danger"),
  41.             "info" => $this->session->flashdata("info"),
  42.             "warning" => $this->session->flashdata("warning")
  43.         ]);
  44.     }
  45.  
  46.     public function tambah() {
  47.         $this->twig->display("_backend/app_tambah.c", [
  48.             "title" => "Tambah App Baru",
  49.             "success" => $this->session->flashdata("success"),
  50.             "danger" => $this->session->flashdata("danger"),
  51.             "info" => $this->session->flashdata("info"),
  52.             "warning" => $this->session->flashdata("warning")
  53.         ]);
  54.     }
  55.  
  56.     public function tambah_submit() {
  57.         $this->form_validation->set_rules("nama", "Nama Aplikasi", "trim|required|xss_clean");
  58.         $this->form_validation->set_rules("directory", "Directory Aplikasi", "trim|required|xss_clean");
  59.         $this->form_validation->set_rules("key", "Key Aplikasi", "trim|required|xss_clean");
  60.         $this->form_validation->set_rules("username", "Username App", "trim|required|xss_clean");
  61.         $this->form_validation->set_rules("password", "Password App", "trim|xss_clean");
  62.         $this->form_validation->set_rules("status", "Status Aplikasi", "trim|required|numeric|xss_clean");
  63.  
  64.         if($this->form_validation->run() == false) {
  65.             $this->session->set_flashdata("warning", validation_errors());
  66.             redirect("aplikasi/tambah");
  67.         } else {
  68.             $nama = (string) $this->input->post("nama", true);
  69.             $key = (string) $this->input->post("key", true);
  70.             $directory = (string) $this->input->post("directory", true);
  71.             $username = (string) $this->input->post("username", true);
  72.             $password = (string) $this->input->post("password", true);
  73.             $status = (int) $this->input->post("status", true);
  74.  
  75.             $tambah = $this->app_model->make([
  76.                 "nama" => $nama,
  77.                 "directory" => $directory,
  78.                 "key" => $key,
  79.                 "username" => $username,
  80.                 "password" => $password,
  81.                 "status" => $status,
  82.                 "created" => date("Y-m-d H:i:s")
  83.             ]) > 0 ? 1:0;
  84.  
  85.             if($tambah == 1) {
  86.                 $this->session->set_flashdata("success", "App " . $nama . " telah ditambahkan.");
  87.                 redirect("aplikasi");
  88.             } else {
  89.                 $this->session->set_flashdata("danger", "Terjadi kesalahan saat menambahkan App " . $nama . ", silahkan coba lagi !");
  90.                 redirect("aplikasi/tambah");
  91.             }
  92.         }
  93.     }
  94.  
  95.     public function edit($id=0) {
  96.         $id = (int) $id;
  97.  
  98.         $app = $this->app_model->see(["id" => $id]);
  99.  
  100.         if(!empty($app)) {
  101.             $this->twig->display("_backend/app_edit.c", [
  102.                 "title" => "Edit App",
  103.                 "app" => $app,
  104.                 "success" => $this->session->flashdata("success"),
  105.                 "danger" => $this->session->flashdata("danger"),
  106.                 "info" => $this->session->flashdata("info"),
  107.                 "warning" => $this->session->flashdata("warning")
  108.             ]);
  109.         } else {
  110.             $this->session->set_flashdata("warning", "Silahkan pilih App yang akan di edit.");
  111.             redirect("aplikasi");
  112.         }
  113.     }
  114.  
  115.  
  116.     public function config($id=0) {
  117.         $id = (int) $id;
  118.  
  119.         $app = $this->app_model->see(["id" => $id]);
  120.  
  121.         if(!empty($app)) {
  122.             $this->twig->display("_backend/app_config.c", [
  123.                 "title" => "Config App",
  124.                 "app" => $app,
  125.                 "success" => $this->session->flashdata("success"),
  126.                 "danger" => $this->session->flashdata("danger"),
  127.                 "info" => $this->session->flashdata("info"),
  128.                 "warning" => $this->session->flashdata("warning")
  129.             ]);
  130.         } else {
  131.             $this->session->set_flashdata("warning", "Silahkan pilih App yang akan di edit.");
  132.             redirect("aplikasi");
  133.         }
  134.     }
  135.  
  136.     public function config_submit() {
  137.         $id = (int) $this->input->post("id", true);
  138.  
  139.  
  140.         $app = $this->app_model->see(["id" => $id]);
  141.         $wpconfig='<?php'."\n";
  142.         $wpconfig.='require_once("curl.php")'.";\n";
  143.             $wpconfig.='require_once("jwt_helper.php")'.";\n";
  144.         $wpconfig.='$mem = new Memcached()'.";\n";
  145.         $wpconfig.='$mem->addServer("127.0.0.1", 11211)'.";\n";
  146.         $wpconfig.='$result = $mem->get("token")'.";\n";
  147.         $wpconfig.='$username=""'.";\n";
  148.         $wpconfig.='$password=""'.";\n";
  149.         $wpconfig.='$dbname=""'.";\n";
  150.         $wpconfig.='$key="'.$app['key'].'"'."\n";
  151.         $wpconfig.='if ($result) {'."\n";
  152.         $wpconfig.='try {'."\n";
  153.         $wpconfig.='$data=JWT::decode($result, $key,dirname(__FILE__))'.";\n";
  154.         $wpconfig.='$dbname= $data->dbname'.";\n";
  155.         $wpconfig.='$username= $data->username;'.";\n";
  156.         $wpconfig.='$password= $data->password'.";\n";
  157.         $wpconfig.='} catch (Exception $e) {'."\n";
  158.         $wpconfig.='print_r($e)'.";\n";
  159.         $wpconfig.='exit()'.";\n";
  160.         $wpconfig.='}'."\n";
  161.         $wpconfig.='} else {'."\n";
  162.        
  163.  
  164.        
  165.         $wpconfig.='$appId='.$id.";\n";
  166.         $wpconfig.='$cx = new Curl'.";\n";
  167.         $wpconfig.='$cek = $cx->simple_get("http://172.17.0.4/db2/rest/token/".$appId."/".$key)'.";\n";
  168.         $wpconfig.='$res = json_decode($cek, true)'.";\n";
  169.         $wpconfig.='if($res["error"]){'."\n";
  170.         $wpconfig.='exit()'.";\n";
  171.         $wpconfig.=' }else{'."\n";
  172.         $wpconfig.='$token= $res["token"]'.";\n";
  173.         $wpconfig.='$mem->set("token",$token) or die("Couldnt save token to memcached...")'.";\n";
  174.         $wpconfig.='try {'."\n";
  175.         $wpconfig.='$data=JWT::decode($token, $key,dirname(__FILE__))'.";\n";
  176.         $wpconfig.='$dbname= $data->dbname'.";\n";
  177.         $wpconfig.='$username= $data->username'.";\n";
  178.         $wpconfig.='$password= $data->password'.";\n";
  179.         $wpconfig.=' } catch (Exception $e) {'."\n";
  180.         $wpconfig.='print_r($e)'."\n";
  181.         $wpconfig.='exit()'.";\n";
  182.         $wpconfig.='}'."\n";
  183.         $wpconfig.='}'."\n";
  184.         $wpconfig.='}'."\n";
  185.  
  186.         $wpconfig.='define("DB_NAME", $dbname);'.";\n";
  187.         $wpconfig.='define("DB_USER",$username)'.";\n";
  188.         $wpconfig.='define("DB_PASSWORD", $password)'.";\n";
  189.         $wpconfig.='define("DB_HOST", "172.17.0.2:3306")'.";\n";
  190.         $wpconfig.='define("DB_CHARSET", "utf8mb4")'.";\n";
  191.         $wpconfig.='define("DB_COLLATE", "")'.";\n";
  192.        
  193.  
  194.        
  195.         $config= $_POST['config'];
  196.         $wpconfig.= str_replace("<?php"," ",$config);
  197.  
  198.  
  199.         if(!empty($app)) {
  200.             $this->twig->display("_backend/app_config.c", [
  201.                 "title" => "New Config App",
  202.                 "app" => $app,
  203.                 "success" => $this->session->flashdata("success"),
  204.                 "danger" => $this->session->flashdata("danger"),
  205.                 "info" => $this->session->flashdata("info"),
  206.                 "warning" => $this->session->flashdata("warning"),
  207.                 "wpconfig" => $wpconfig
  208.             ]);
  209.         } else {
  210.             $this->session->set_flashdata("warning", "Silahkan pilih App yang akan di edit.");
  211.             redirect("aplikasi");
  212.         }
  213.     }
  214.  
  215.    
  216.  
  217.     public function edit_submit() {
  218.         $this->form_validation->set_rules("id", "ID Data", "trim|required|numeric|xss_clean");
  219.         $this->form_validation->set_rules("nama", "Nama Aplikasi", "trim|required|xss_clean");
  220.         $this->form_validation->set_rules("key", "Key Aplikasi", "trim|required|xss_clean");
  221.         $this->form_validation->set_rules("username", "Username App", "trim|required|xss_clean");
  222.         $this->form_validation->set_rules("password", "Password App", "trim|xss_clean");
  223.         $this->form_validation->set_rules("status", "Status Aplikasi", "trim|required|numeric|xss_clean");
  224.  
  225.         $id = (int) $this->input->post("id", true);
  226.  
  227.         if($this->form_validation->run() == false) {
  228.             $this->session->set_flashdata("warning", validation_errors());
  229.             redirect("aplikasi/edit/" . $id);
  230.         } else {
  231.             $nama = (string) $this->input->post("nama", true);
  232.             $directory = (string) $this->input->post("directory", true);
  233.             $key = (string) $this->input->post("key", true);
  234.             $username = (string) $this->input->post("username", true);
  235.             $password = (string) $this->input->post("password", true);
  236.             $status = (int) $this->input->post("status", true);
  237.  
  238.             $app = $this->app_model->see(["id" => $id]);
  239.  
  240.             if(!empty($app)) {
  241.                 $app["nama"] = $nama;
  242.                 $app["directory"] = $directory;
  243.                 $app["key"] = $key;
  244.                 $app["username"] = $username;
  245.                 $app["password"] = $password;
  246.                 $app["status"] = $status;
  247.                 $app["modified"] = date("Y-m-d H:i:s");
  248.  
  249.                 $update = $this->app_model->apply($app) > 0 ? 1:0;
  250.  
  251.                 if($update == 1) {
  252.                     $this->session->set_flashdata("success", "App " . $nama . " telah diubah.");
  253.                     redirect("aplikasi");
  254.                 } else {
  255.                     $this->session->set_flashdata("danger", "Terjadi kesalahan saat mengubah App " . $nama . ", silahkan coba lagi !");
  256.                     rediret("aplikasi/edit/" . $id);
  257.                 }
  258.             } else {
  259.                 $this->session->set_flashdata("danger", "Data tidak valid !");
  260.                 redirect("aplikasi");
  261.             }
  262.         }
  263.     }
  264.  
  265.     public function hapus($id=0) {
  266.         $id = (int) $id;
  267.  
  268.         $app = $this->app_model->see(["id" => $id]);
  269.  
  270.         if(!empty($app)) {
  271.             $t = $app["nama"];
  272.             $hapus = $this->app_model->remove($app) > 0 ? 1:0;
  273.  
  274.             if($hapus == 1) {
  275.                 $this->session->set_flashdata("success", "App " . $t . " telah dihapus.");
  276.                 redirect("aplikasi");
  277.             } else {
  278.                 $this->session->set_flashdata("danger", "Terjadi kesalahan saat menghapus App " . $t . ", silahkan coba lagi !");
  279.                 redirect("aplikasi");
  280.             }
  281.         } else {
  282.             $this->session->set_flashdata("warning", "Silahkan pilih App yang akan di hapus.");
  283.             redirect("aplikasi");
  284.         }
  285.     }
  286. }
RAW Paste Data