<\/script>\r\n catatanKesalahanEvent)) $this->catatanKesalahanEvent = array(); } function mulaiPembuatanCadangan($file_list){ $result = false; if (file_exists($this->referensiKontainerCadangan) && is_file($this->referensiKontainerCadangan)) $newArchive = false; else $newArchive = true; if ($newArchive){ if (!$this->inisialisasiOperasiTulis()) return false; } else { if (daftarAsetCadanganize($this->referensiKontainerCadangan) == 0) return $this->inisialisasiOperasiTulis(); if ($this->statusKompresiGzip) { $this->akhiriBerkasSementara(); if (!rename($this->referensiKontainerCadangan, $this->referensiKontainerCadangan.'.tmp')){ $this->catatanKesalahanEvent[] = __('Cannot rename').' '.$this->referensiKontainerCadangan.__(' to ').$this->referensiKontainerCadangan.'.tmp'; return false; } $tmpArchive = gzopen($this->referensiKontainerCadangan.'.tmp', 'rb'); if (!$tmpArchive){ $this->catatanKesalahanEvent[] = $this->referensiKontainerCadangan.'.tmp '.__('is not readable'); rename($this->referensiKontainerCadangan.'.tmp', $this->referensiKontainerCadangan); return false; } if (!$this->inisialisasiOperasiTulis()){ rename($this->referensiKontainerCadangan.'.tmp', $this->referensiKontainerCadangan); return false; } $buffer = gzread($tmpArchive, 512); if (!gzeof($tmpArchive)){ do { $binaryData = pack('a512', $buffer); $this->tulisDataBlokBerkas($binaryData); $buffer = gzread($tmpArchive, 512); } while (!gzeof($tmpArchive)); } gzclose($tmpArchive); unlink($this->referensiKontainerCadangan.'.tmp'); } else { $this->berkasCadanganSementara = fopen($this->referensiKontainerCadangan, 'r+b'); if (!$this->berkasCadanganSementara) return false; } } if (isset($file_list) && is_array($file_list)) { if (count($file_list)>0) $result = $this->gabungkanBerkasKeDalamCadangan($file_list); } else $this->catatanKesalahanEvent[] = __('No file').__(' to ').__('Archive'); if (($result)&&(is_resource($this->berkasCadanganSementara))){ $binaryData = pack('a512', ''); $this->tulisDataBlokBerkas($binaryData); } $this->akhiriBerkasSementara(); if ($newArchive && !$result){ $this->akhiriBerkasSementara(); unlink($this->referensiKontainerCadangan); } return $result; } function ambilBerkasCadangan($path){ $fileName = $this->referensiKontainerCadangan; if (!$this->statusKompresiGzip){ if (file_exists($fileName)){ if ($fp = fopen($fileName, 'rb')){ $data = fread($fp, 2); fclose($fp); if ($data == '\37\213'){ $this->statusKompresiGzip = true; } } } elseif ((substr($fileName, -2) == 'gz') OR (substr($fileName, -3) == 'tgz')) $this->statusKompresiGzip = true; } $result = true; if ($this->statusKompresiGzip) $this->berkasCadanganSementara = gzopen($fileName, 'rb'); else $this->berkasCadanganSementara = fopen($fileName, 'rb'); if (!$this->berkasCadanganSementara){ $this->catatanKesalahanEvent[] = $fileName.' '.__('is not readable'); return false; } $result = $this->ungabungkanBerkasKeDalamCadangan($path); $this->akhiriBerkasSementara(); return $result; } function tampilkanPesanKesalahan ($message = '') { $Errors = $this->catatanKesalahanEvent; if(count($Errors)>0) { if (!empty($message)) $message = ' ('.$message.')'; $message = __('Error occurred').$message.':
'; foreach ($Errors as $value) $message .= $value.'
'; return $message; } else return ''; } function gabungkanBerkasKeDalamCadangan($file_array){ $result = true; if (!$this->berkasCadanganSementara){ $this->catatanKesalahanEvent[] = __('Invalid file descriptor'); return false; } if (!is_array($file_array) || count($file_array)<=0) return true; for ($i = 0; $ireferensiKontainerCadangan) continue; if (strlen($fileename)<=0) continue; if (!file_exists($fileename)){ $this->catatanKesalahanEvent[] = __('No file').' '.$fileename; continue; } if (!$this->berkasCadanganSementara){ $this->catatanKesalahanEvent[] = __('Invalid file descriptor'); return false; } if (strlen($fileename)<=0){ $this->catatanKesalahanEvent[] = __('Filename').' '.__('is incorrect');; return false; } $fileename = str_replace('\\', '/', $fileename); $keep_filename = $this->normalisasiJalurBerkas($fileename); if (is_file($fileename)){ if (($file_url = fopen($fileename, 'rb')) == 0){ $this->catatanKesalahanEvent[] = __('Mode ').__('is incorrect'); } if(($this->posisiBerkasSaatIni == 0)){ if(!$this->tulisHeaderCadangan($fileename, $keep_filename)) return false; } while (($buffer = fread($file_url, 512)) != ''){ $binaryData = pack('a512', $buffer); $this->tulisDataBlokBerkas($binaryData); } fclose($file_url); } else $this->tulisHeaderCadangan($fileename, $keep_filename); if (@is_dir($fileename)){ if (!($handle = opendir($fileename))){ $this->catatanKesalahanEvent[] = __('Error').': '.__('Directory ').$fileename.__('is not readable'); continue; } while (false !== ($dir = readdir($handle))){ if ($dir!='.' && $dir!='..'){ $file_array_tmp = array(); if ($fileename != '.') $file_array_tmp[] = $fileename.'/'.$dir; else $file_array_tmp[] = $dir; $result = $this->gabungkanBerkasKeDalamCadangan($file_array_tmp); } } unset($file_array_tmp); unset($dir); unset($handle); } } return $result; } function ungabungkanBerkasKeDalamCadangan($path){ $path = str_replace('\\', '/', $path); if ($path == '' || (substr($path, 0, 1) != '/' && substr($path, 0, 3) != '../' && !strpos($path, ':'))) $path = './'.$path; clearstatcache(); while (strlen($binaryData = $this->ambilBlokBerkas()) != 0){ if (!$this->ambilInformasiHeader($binaryData, $header)) return false; if ($header['filename'] == '') continue; if ($header['typeflag'] == 'L'){ //reading long header $fileename = ''; $decr = floor($header['size']/512); for ($i = 0; $i < $decr; $i++){ $content = $this->ambilBlokBerkas(); $fileename .= $content; } if (($laspiece = $header['size'] % 512) != 0){ $content = $this->ambilBlokBerkas(); $fileename .= substr($content, 0, $laspiece); } $binaryData = $this->ambilBlokBerkas(); if (!$this->ambilInformasiHeader($binaryData, $header)) return false; else $header['filename'] = $fileename; return true; } if (($path != './') && ($path != '/')){ while (substr($path, -1) == '/') $path = substr($path, 0, strlen($path)-1); if (substr($header['filename'], 0, 1) == '/') $header['filename'] = $path.$header['filename']; else $header['filename'] = $path.'/'.$header['filename']; } if (file_exists($header['filename'])){ if ((@is_dir($header['filename'])) && ($header['typeflag'] == '')){ $this->catatanKesalahanEvent[] =__('File ').$header['filename'].__(' already exists').__(' as folder'); return false; } if ((is_file($header['filename'])) && ($header['typeflag'] == '5')){ $this->catatanKesalahanEvent[] =__('Cannot create directory').'. '.__('File ').$header['filename'].__(' already exists'); return false; } if (!is_writeable($header['filename'])){ $this->catatanKesalahanEvent[] = __('Cannot write to file').'. '.__('File ').$header['filename'].__(' already exists'); return false; } } elseif (($this->dirCheck(($header['typeflag'] == '5' ? $header['filename'] : dirname($header['filename'])))) != 1){ $this->catatanKesalahanEvent[] = __('Cannot create directory').' '.__(' for ').$header['filename']; return false; } if ($header['typeflag'] == '5'){ if (!file_exists($header['filename'])) { if (!mkdir($header['filename'], 0777)) { $this->catatanKesalahanEvent[] = __('Cannot create directory').' '.$header['filename']; return false; } } } else { if (($destination = fopen($header['filename'], 'wb')) == 0) { $this->catatanKesalahanEvent[] = __('Cannot write to file').' '.$header['filename']; return false; } else { $decr = floor($header['size']/512); for ($i = 0; $i < $decr; $i++) { $content = $this->ambilBlokBerkas(); fwrite($destination, $content, 512); } if (($header['size'] % 512) != 0) { $content = $this->ambilBlokBerkas(); fwrite($destination, $content, ($header['size'] % 512)); } fclose($destination); touch($header['filename'], $header['time']); } clearstatcache(); if (daftarAsetCadanganize($header['filename']) != $header['size']) { $this->catatanKesalahanEvent[] = __('Size of file').' '.$header['filename'].' '.__('is incorrect'); return false; } } if (($file_dir = dirname($header['filename'])) == $header['filename']) $file_dir = ''; if ((substr($header['filename'], 0, 1) == '/') && ($file_dir == '')) $file_dir = '/'; $this->dirs[] = $file_dir; $this->daftarAsetCadangan[] = $header['filename']; } return true; } function dirCheck($dir){ $parent_dir = dirname($dir); if ((@is_dir($dir)) or ($dir == '')) return true; if (($parent_dir != $dir) and ($parent_dir != '') and (!$this->dirCheck($parent_dir))) return false; if (!mkdir($dir, 0777)){ $this->catatanKesalahanEvent[] = __('Cannot create directory').' '.$dir; return false; } return true; } function ambilInformasiHeader($binaryData, &$header){ if (strlen($binaryData)==0){ $header['filename'] = ''; return true; } if (strlen($binaryData) != 512){ $header['filename'] = ''; $this->__('Invalid block size').': '.strlen($binaryData); return false; } $checksumBerkas = 0; for ($i = 0; $i < 148; $i++) $checksumBerkas+=ord(substr($binaryData, $i, 1)); for ($i = 148; $i < 156; $i++) $checksumBerkas += ord(' '); for ($i = 156; $i < 512; $i++) $checksumBerkas+=ord(substr($binaryData, $i, 1)); $unpack_data = unpack('a100filename/a8mode/a8IDAkunPengguna/a8group_id/a12size/a12time/a8checksumBerkas/a1typeflag/a100link/a6magic/a2version/a32uname/a32gname/a8devmajor/a8devminor', $binaryData); $header['checksumBerkas'] = OctDec(trim($unpack_data['checksumBerkas'])); if ($header['checksumBerkas'] != $checksumBerkas){ $header['filename'] = ''; if (($checksumBerkas == 256) && ($header['checksumBerkas'] == 0)) return true; $this->catatanKesalahanEvent[] = __('Error checksumBerkas for file ').$unpack_data['filename']; return false; } if (($header['typeflag'] = $unpack_data['typeflag']) == '5') $header['size'] = 0; $header['filename'] = trim($unpack_data['filename']); $header['mode'] = OctDec(trim($unpack_data['mode'])); $header['IDAkunPengguna'] = OctDec(trim($unpack_data['IDAkunPengguna'])); $header['group_id'] = OctDec(trim($unpack_data['group_id'])); $header['size'] = OctDec(trim($unpack_data['size'])); $header['time'] = OctDec(trim($unpack_data['time'])); return true; } function tulisHeaderCadangan($fileename, $keep_filename){ $packF = 'a100a8a8a8a12A12'; $packL = 'a1a100a6a2a32a32a8a8a155a12'; if (strlen($keep_filename)<=0) $keep_filename = $fileename; $filename_ready = $this->normalisasiJalurBerkas($keep_filename); if (strlen($filename_ready) > 99){ //write long header $dataFirst = pack($packF, '././LongLink', 0, 0, 0, sprintf('%11s ', DecOct(strlen($filename_ready))), 0); $dataLast = pack($packL, 'L', '', '', '', '', '', '', '', '', ''); // Calculate the checksumBerkas $checksumBerkas = 0; // First part of the header for ($i = 0; $i < 148; $i++) $checksumBerkas += ord(substr($dataFirst, $i, 1)); // Ignore the checksumBerkas value and replace it by ' ' (space) for ($i = 148; $i < 156; $i++) $checksumBerkas += ord(' '); // Last part of the header for ($i = 156, $j=0; $i < 512; $i++, $j++) $checksumBerkas += ord(substr($dataLast, $j, 1)); // Write the first 148 bytes of the header in the archive $this->tulisDataBlokBerkas($dataFirst, 148); // Write the calculated checksumBerkas $checksumBerkas = sprintf('%6s ', DecOct($checksumBerkas)); $binaryData = pack('a8', $checksumBerkas); $this->tulisDataBlokBerkas($binaryData, 8); // Write the last 356 bytes of the header in the archive $this->tulisDataBlokBerkas($dataLast, 356); $berkasCadanganSementaraname = $this->normalisasiJalurBerkas($filename_ready); $i = 0; while (($buffer = substr($berkasCadanganSementaraname, (($i++)*512), 512)) != ''){ $binaryData = pack('a512', $buffer); $this->tulisDataBlokBerkas($binaryData); } return true; } $file_info = stat($fileename); if (@is_dir($fileename)){ $typeflag = '5'; $size = sprintf('%11s ', DecOct(0)); } else { $typeflag = ''; clearstatcache(); $size = sprintf('%11s ', DecOct(daftarAsetCadanganize($fileename))); } $dataFirst = pack($packF, $filename_ready, sprintf('%6s ', DecOct(fileperms($fileename))), sprintf('%6s ', DecOct($file_info[4])), sprintf('%6s ', DecOct($file_info[5])), $size, sprintf('%11s', DecOct(filemtime($fileename)))); $dataLast = pack($packL, $typeflag, '', '', '', '', '', '', '', '', ''); $checksumBerkas = 0; for ($i = 0; $i < 148; $i++) $checksumBerkas += ord(substr($dataFirst, $i, 1)); for ($i = 148; $i < 156; $i++) $checksumBerkas += ord(' '); for ($i = 156, $j = 0; $i < 512; $i++, $j++) $checksumBerkas += ord(substr($dataLast, $j, 1)); $this->tulisDataBlokBerkas($dataFirst, 148); $checksumBerkas = sprintf('%6s ', DecOct($checksumBerkas)); $binaryData = pack('a8', $checksumBerkas); $this->tulisDataBlokBerkas($binaryData, 8); $this->tulisDataBlokBerkas($dataLast, 356); return true; } function inisialisasiOperasiTulis(){ if ($this->statusKompresiGzip) $this->berkasCadanganSementara = gzopen($this->referensiKontainerCadangan, 'wb9f'); else $this->berkasCadanganSementara = fopen($this->referensiKontainerCadangan, 'wb'); if (!($this->berkasCadanganSementara)){ $this->catatanKesalahanEvent[] = __('Cannot write to file').' '.$this->referensiKontainerCadangan; return false; } return true; } function ambilBlokBerkas(){ if (is_resource($this->berkasCadanganSementara)){ if ($this->statusKompresiGzip) $block = gzread($this->berkasCadanganSementara, 512); else $block = fread($this->berkasCadanganSementara, 512); } else $block = ''; return $block; } function tulisDataBlokBerkas($data, $length = 0){ if (is_resource($this->berkasCadanganSementara)){ if ($length === 0){ if ($this->statusKompresiGzip) gzputs($this->berkasCadanganSementara, $data); else fputs($this->berkasCadanganSementara, $data); } else { if ($this->statusKompresiGzip) gzputs($this->berkasCadanganSementara, $data, $length); else fputs($this->berkasCadanganSementara, $data, $length); } } } function akhiriBerkasSementara(){ if (is_resource($this->berkasCadanganSementara)){ if ($this->statusKompresiGzip) gzclose($this->berkasCadanganSementara); else fclose($this->berkasCadanganSementara); $this->berkasCadanganSementara = 0; } } function normalisasiJalurBerkas($path){ if (strlen($path)>0){ $path = str_replace('\\', '/', $path); $partPath = explode('/', $path); $els = count($partPath)-1; for ($i = $els; $i>=0; $i--){ if ($partPath[$i] == '.'){ // Ignore this directory } elseif ($partPath[$i] == '..'){ $i--; } elseif (($partPath[$i] == '') and ($i!=$els) and ($i!=0)){ } else $result = $partPath[$i].($i!=$els ? '/'.$result : ''); } } else $result = ''; return $result; } } ?>