db->password; $temp_file = tempnam(sys_get_temp_dir(), '-log-db') . ".sql"; if (file_exists($temp_file)) { unlink($temp_file); } $cmd = "/usr/bin/mysqldump --triggers --routines --no-data -u root -p$pwd cpone_log > $temp_file"; exec($cmd); if (file_exists($temp_file)) { return $temp_file; } return false; } /* create table xdl_numbering ( xdlNumberingID int not null auto_increment primary key, xdlNumberingS_NumberingID int, xdlNumberingType varchar(12), xdlNumberingPrefix varchar(50), xdlNumberingIsActive varchar(1) default 'Y', xdlNumberingCreated datetime default current_timestamp(), xdlNumberingLastUpdated datetime default current_timestamp() on update current_timestamp(), key(xdlNumberingIsActive), key(xdl_numberingType), key(xdlNumberingPrefix) ); create table xdl_log( xdlLogID int not null auto_increment primary key, xdlLogMgmMcuID int, xdlLogIsActive varchar(1) default 'Y', xdlLogStatus varchar(1) default 'N' comment 'N:New, P:Process, D:Done', xdlLogCreated datetime current_timestamp(), xdlLogLastUpdated datetime current_timestamp() on update current_timestamp(), key(xdlLogIsActive), key(xdlLogStatus) ); create table xdl_table( xdlTableID int not null auto_increment primary key, xdlTableName varchar(200), xdlTableStruOnly varchar(1) default 'N', xdlTableIsActive varchar(1) default 'Y', xdlTableCreated datetime default current_timestamp(), xdlTableLastUpdated datetime default current_timestamp() on update current_timestamp() ); */ function table() { $pwd = $this->db->password; $sql = "select * from xdl_table where xdlTableIsActive ='Y'"; $qry = $this->db->query($sql); if (!$qry) { return [false, $this->db->error()["message"]]; } $temp_file = tempnam(sys_get_temp_dir(), "cpone-stru") . ".sql"; if (file_exists($temp_file)) { unlink($temp_file); } $rows = $qry->result_array(); if (count($rows) == 0) { unlink($temp_file); return [false, "Download Tables not defined yet"]; } foreach ($rows as $r) { $tbl = $r["xdlTableName"]; $isStruOnly = $r["xdlTableStruOnly"] == "Y"; if ($isStruOnly) { $cmd = "/usr/bin/mysqldump --triggers --no-data -u root -p$pwd cpone $tbl >> $temp_file"; } else { $cmd = "/usr/bin/mysqldump --triggers -u root -p$pwd cpone $tbl >> $temp_file"; } exec($cmd); } return [$temp_file, ""]; } function routines() { $pwd = $this->db->password; $temp_file = tempnam(sys_get_temp_dir(), '-routines') . ".sql"; if (file_exists($temp_file)) { unlink($temp_file); } $cmd = "/usr/bin/mysqldump --triggers --routines --no-data -u root -p$pwd --no-create-db --no-create-info cpone > $temp_file"; exec($cmd); if (file_exists($temp_file)) { return $temp_file; } return false; } function compress($mcuID, $log_file, $table_file, $routines_file) { $dt = date("Ymd"); $temp_file = tempnam(sys_get_temp_dir(), "xdl_{$mcuID}_{$dt}") . ".zip"; if (file_exists($temp_file)) { unlink($temp_file); } $cmd = "/usr/bin/zip $temp_file $log_file $table_file $routines_file"; exec($cmd); if (file_exists($temp_file)) { return $temp_file; } return false; } function check_and_update($mcuID) { $sql = "select * from xdl_log where xdlLogMgmMcuID = ? and xdlLogIsActive = 'Y'"; $qry = $this->db->query($sql, [$mcuID]); if (!$qry) { echo json_encode(["status" => "ERR", "message" => $this->db->error()["message"]]); exit; } $rows = $qry->result_array(); if (count($rows) > 0) { $dt = $rows[0]["xdlLogCreated"]; if ($rows[0]["xdlLogStatus"] == "P") { echo json_encode(["status" => "ERR", "message" => "Proses Download Mcu sedang berlangsung @ $dt"]); exit; } if ($rows[0]["xdlLogStatus"] == "D") { echo json_encode(["status" => "ERR", "message" => "Proses Download Mcu sudah selesai @ $dt"]); exit; } } $sql = "select group_concat( concat(S_NumberingType,':',S_NumberingPrefix)) as duplicate from s_numbering join xdl_numbering on S_NumberingIsActive = 'Y' and S_NumberingType = xdlNumberingType and S_NumberingPrefix = xdlNumberingPrefix"; $qry = $this->db->query($sql, [$mcuID]); if (!$qry) { echo json_encode(["status" => "ERR", "message" => $this->db->error()["message"]]); exit; } $rows = $qry->result_array(); $msg = ""; if (count($rows) > O) { $msg = $rows[0]["duplicate"]; } if ($msg != "") { echo json_encode(["status" => "ERR", "message" => "Setting Numbering Offline ada yang duplicate:\n$msg"]); exit; } $sql = "insert into xdl_log(xdlLogMgmMcuID,xdlLogStatus) values(?,'P')"; $qry = $this->db->query($sql, [$mcuID]); if (!$qry) { echo json_encode(["status" => "ERR", "message" => $this->db->error()["message"]]); exit; } return $this->db->insert_id(); } function update_log($logID, $status) { $sql = "update xdl_log set xdlLogStatus = ? where xdlLogID = ?"; $qry = $this->db->query($sql, [$status, $logID]); if (!$qry) { echo json_encode(["status" => "ERR", "message" => $this->db->error()["message"]]); exit; } } function file($mcuID) { header("Content-type: application/zip"); $tgt_file = "/tmp-offline/offline-{$mcuID}-data.zip"; $tgt_name = "offline-{$mcuID}-data.zip"; header("Content-Disposition: inline; filename=\"$tgt_name\""); echo file_get_contents($tgt_file); } function go($mcuID) { header("Content-type: application/json"); $logID = $this->check_and_update($mcuID); $log_file = $this->log(); if ($log_file === false) { $this->update_log($logID, "E"); echo json_encode(["status" => "ERR", "message" => "Error export cpone_log structure"]); exit; } list($table_file, $msg) = $this->table(); if ($table_file === false) { unlink($log_file); $this->update_log($logID, "E"); echo json_encode(["status" => "ERR", "message" => "Error export cpone :\n$msg"]); exit; } $routines_file = $this->routines(); if ($routines_file === false) { unlink($log_file); unlink($table_file); $this->update_log($logID, "E"); echo json_encode(["status" => "ERR", "message" => "Error export cpone routines and triggers"]); exit; } $zip_file = $this->compress($mcuID, $log_file, $table_file, $routines_file); unlink($log_file); unlink($table_file); unlink($routines_file); $this->update_log($logID, "D"); $tgt_file = "/tmp-offline/offline-{$mcuID}-data.zip"; $status = rename($zip_file, $tgt_file); if ($status == false) { echo json_encode([ "status" => "ERR", "message" => "Permission when moving file to $tgt_file" ]); unlink($zip_file); exit; } echo json_encode([ "status" => "OK", "file_name" => "offline-{$mcuID}-data.zip", "url" => "/one-api/tools/xdownload/file/$mcuID" ]); unlink($zip_file); } }