| 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/simbada_2019/application/modules/update_aplikasi/models/ |
Upload File : |
<?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();
date_default_timezone_set('Asia/Jakarta');
$this->tanggal = date('Ymd_His');
$this->dst_aplikasi = LOCAL_APP_DIR . '_' . $this->tanggal; // nama directory aplikasi hasil backup
$this->db_aplikasi = $this->db->database; // database yang digunakan.
$this->dst_db = dirname(LOCAL_APP_DIR) . '/' . basename($this->db->database, '.FDB') . '_' . $this->tanggal . '.FBK'; // 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
** Format nama direktori hasil backup : namaaplikasi_tanggalhariini
**/
function directory_copy($srcdir, $dstdir) /** 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 ( !copy($srcdir.'/'.$object_value,$dstdir.'/'.$object_value) ) { //This is a File not a directory
return FALSE;
}
}
else
$this->directory_copy($srcdir.'/'.$object_key,$dstdir.'/'.$object_key);//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;
}
return $this->directory_copy(LOCAL_APP_DIR, $this->dst_aplikasi);
}
/** (III)
* Proses ketiga adalah proses bakcup database yang disimpan di $this->dst_db
**/
function backup_db() /** backup db **/
{
$resource_service_handle = ibase_service_attach ($this->db->hostname, $this->db->username, $this->db->password);
ibase_backup($resource_service_handle, $this->db_aplikasi, $this->dst_db);
return ibase_service_detach ($resource_service_handle);
}
/** (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($current_version)
{
if ( !is_dir(LOCAL_UPDATE_DIR) ) mkdir(LOCAL_UPDATE_DIR, DIR_WRITE_MODE, true);
$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';
$src_file_zip = UPDATE_SERVER. '/' . $file_zip;
$dst_file_zip = LOCAL_UPDATE_DIR . '/' . $file_zip;
$src_file_json = UPDATE_SERVER. '/' . $file_json;
$dst_file_json = LOCAL_UPDATE_DIR . '/' . $file_json;
$src_file_md5 = UPDATE_SERVER. '/' . $file_md5;
$dst_file_md5 = LOCAL_UPDATE_DIR . '/' . $file_md5;
// unduh file zip
if ($this->unduh($dst_file_zip, $src_file_zip) == FALSE) return 'failed'; // unduh file zip
if (!$this->change_file_mode($dst_file_zip)) return 'can\'t change'; // rubah mode file zip
// unduh file json
if ($this->unduh($dst_file_json, $src_file_json) == FALSE) return 'failed'; // unduh file json
if (!$this->change_file_mode($dst_file_json)) return 'can\'t change'; // rubah mode file json
// unduh file md5
if ($this->unduh($dst_file_md5, $src_file_md5) == FALSE) return 'failed'; // unduh file md5
if (!$this->change_file_mode($dst_file_md5)) return 'can\'t change'; // rubah mode file md5
if (!$this->check_md5_hash($dst_file_zip, $dst_file_md5)) return 'corrupt'; // cek md5 hash
$download_version = $this->read_file_version($dst_file_json);
$current_version = $download_version;
$status = $download_version;
}
return 'ok';
}
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;
}
}