2011-01-28 07:37:00 +00:00
|
|
|
<?php
|
|
|
|
// MyDMS. Document Management System
|
|
|
|
// Copyright (C) 2010 Matteo Lucarelli
|
2016-08-09 05:34:30 +00:00
|
|
|
// Copyright (C) 2010-2016 Uwe Steinmann
|
2011-01-28 07:37:00 +00:00
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation; either version 2 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
|
|
|
|
include("../inc/inc.Settings.php");
|
2014-12-08 13:47:32 +00:00
|
|
|
include("../inc/inc.LogInit.php");
|
|
|
|
include("../inc/inc.Language.php");
|
|
|
|
include("../inc/inc.Init.php");
|
|
|
|
include("../inc/inc.Extension.php");
|
2010-10-29 13:19:51 +00:00
|
|
|
include("../inc/inc.DBInit.php");
|
2011-01-28 07:37:00 +00:00
|
|
|
include("../inc/inc.ClassUI.php");
|
2010-10-29 13:19:51 +00:00
|
|
|
include("../inc/inc.Authentication.php");
|
|
|
|
|
|
|
|
if (!$user->isAdmin()) {
|
2011-01-28 07:37:00 +00:00
|
|
|
UI::exitError(getMLText("admin_tools"),getMLText("access_denied"));
|
2010-10-29 13:19:51 +00:00
|
|
|
}
|
|
|
|
|
2011-01-28 07:37:00 +00:00
|
|
|
/**
|
|
|
|
* Adds file header to the tar file, it is used before adding file content.
|
|
|
|
* code by calmarius at nospam dot atw dot hu
|
|
|
|
*
|
|
|
|
* @param resource $f file resource (provided by eg. fopen)
|
|
|
|
* $param string $phisfn path to file
|
|
|
|
* $param string $archfn path to file in archive, directory names must
|
|
|
|
* be followed by '/'
|
|
|
|
*/
|
|
|
|
function TarAddHeader($f,$phisfn,$archfn) { /* {{{ */
|
2011-04-11 06:29:47 +00:00
|
|
|
$info=@stat($phisfn);
|
|
|
|
if($info === false)
|
|
|
|
return false;
|
2010-10-29 13:19:51 +00:00
|
|
|
$ouid=sprintf("%6s ", decoct($info[4]));
|
|
|
|
$ogid=sprintf("%6s ", decoct($info[5]));
|
|
|
|
$omode=sprintf("%6s ", decoct(fileperms($phisfn)));
|
|
|
|
$omtime=sprintf("%11s", decoct(filemtime($phisfn)));
|
2011-01-28 07:37:00 +00:00
|
|
|
if (@is_dir($phisfn)) {
|
2010-10-29 13:19:51 +00:00
|
|
|
$type="5";
|
|
|
|
$osize=sprintf("%11s ", decoct(0));
|
2011-01-28 07:37:00 +00:00
|
|
|
} else {
|
2010-10-29 13:19:51 +00:00
|
|
|
$type='';
|
|
|
|
$osize=sprintf("%11s ", decoct(filesize($phisfn)));
|
|
|
|
clearstatcache();
|
|
|
|
}
|
|
|
|
$dmajor = '';
|
|
|
|
$dminor = '';
|
|
|
|
$gname = '';
|
|
|
|
$linkname = '';
|
|
|
|
$magic = '';
|
|
|
|
$prefix = '';
|
|
|
|
$uname = '';
|
|
|
|
$version = '';
|
|
|
|
$chunkbeforeCS=pack("a100a8a8a8a12A12",$archfn, $omode, $ouid, $ogid, $osize, $omtime);
|
|
|
|
$chunkafterCS=pack("a1a100a6a2a32a32a8a8a155a12", $type, $linkname, $magic, $version, $uname, $gname, $dmajor, $dminor ,$prefix,'');
|
|
|
|
|
|
|
|
$checksum = 0;
|
|
|
|
for ($i=0; $i<148; $i++) $checksum+=ord(substr($chunkbeforeCS,$i,1));
|
|
|
|
for ($i=148; $i<156; $i++) $checksum+=ord(' ');
|
|
|
|
for ($i=156, $j=0; $i<512; $i++, $j++) $checksum+=ord(substr($chunkafterCS,$j,1));
|
|
|
|
|
|
|
|
fwrite($f,$chunkbeforeCS,148);
|
|
|
|
$checksum=sprintf("%6s ",decoct($checksum));
|
|
|
|
$bdchecksum=pack("a8", $checksum);
|
|
|
|
fwrite($f,$bdchecksum,8);
|
|
|
|
fwrite($f,$chunkafterCS,356);
|
|
|
|
return true;
|
2011-01-28 07:37:00 +00:00
|
|
|
} /* }}} */
|
2010-10-29 13:19:51 +00:00
|
|
|
|
|
|
|
// Writes file content to the tar file must be called after a TarAddHeader
|
|
|
|
// f:file resource provided by fopen
|
|
|
|
// phisfn: path to file
|
|
|
|
// code by calmarius at nospam dot atw dot hu
|
2011-01-28 07:37:00 +00:00
|
|
|
function TarWriteContents($f,$phisfn) { /* {{{ */
|
2011-04-11 06:29:47 +00:00
|
|
|
if(!file_exists($phisfn))
|
|
|
|
return;
|
|
|
|
if (@is_dir($phisfn)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$size=filesize($phisfn);
|
|
|
|
$padding=$size % 512 ? 512-$size%512 : 0;
|
|
|
|
$f2=fopen($phisfn,"rb");
|
|
|
|
while (!feof($f2)) fwrite($f,fread($f2,1024*1024));
|
|
|
|
$pstr=sprintf("a%d",$padding);
|
|
|
|
fwrite($f,pack($pstr,''));
|
2011-01-28 07:37:00 +00:00
|
|
|
} /* }}} */
|
2010-10-29 13:19:51 +00:00
|
|
|
|
|
|
|
// Adds 1024 byte footer at the end of the tar file
|
|
|
|
// f: file resource
|
|
|
|
// code by calmarius at nospam dot atw dot hu
|
2011-01-28 07:37:00 +00:00
|
|
|
function TarAddFooter($f) { /* {{{ */
|
2010-10-29 13:19:51 +00:00
|
|
|
fwrite($f,pack('a1024',''));
|
2011-01-28 07:37:00 +00:00
|
|
|
} /* }}} */
|
2010-10-29 13:19:51 +00:00
|
|
|
|
2011-01-28 07:37:00 +00:00
|
|
|
// thanks to Doudoux
|
|
|
|
function getFolderPathPlainAST($folder) { /* {{{ */
|
2010-10-29 13:19:51 +00:00
|
|
|
$path="";
|
|
|
|
$folderPath = $folder->getPath();
|
|
|
|
for ($i = 0; $i < count($folderPath); $i++) {
|
|
|
|
$path .= $folderPath[$i]->getName();
|
|
|
|
if ($i+1 < count($folderPath)) $path .= "/";
|
|
|
|
}
|
|
|
|
return $path;
|
2011-01-28 07:37:00 +00:00
|
|
|
} /* }}} */
|
2010-10-29 13:19:51 +00:00
|
|
|
|
2015-01-08 13:53:53 +00:00
|
|
|
function createFolderTar($folder,$ark, $human_readable, $dms) { /* {{{ */
|
2010-10-29 13:19:51 +00:00
|
|
|
$documents=$folder->getDocuments();
|
|
|
|
foreach ($documents as $document){
|
2011-01-28 07:37:00 +00:00
|
|
|
|
|
|
|
if (file_exists($dms->contentDir.$document->getDir())){
|
2010-10-29 13:19:51 +00:00
|
|
|
|
|
|
|
if ($human_readable){
|
2011-01-28 07:37:00 +00:00
|
|
|
// create an archive containing the files with original names and DMS path
|
|
|
|
// thanks to Doudoux
|
2010-10-29 13:19:51 +00:00
|
|
|
$latestContent = $document->getLatestContent();
|
2011-01-28 07:37:00 +00:00
|
|
|
if (is_object($latestContent)) {
|
2010-10-29 13:19:51 +00:00
|
|
|
TarAddHeader(
|
|
|
|
$ark,
|
2011-04-11 06:29:47 +00:00
|
|
|
$dms->contentDir.$latestContent->getPath(),
|
2011-12-05 14:32:26 +00:00
|
|
|
getFolderPathPlainAST($folder)."/".$document->getID()."_".$latestContent->getOriginalFileName());
|
2011-01-28 07:37:00 +00:00
|
|
|
|
2011-04-11 06:29:47 +00:00
|
|
|
TarWriteContents($ark, $dms->contentDir.$latestContent->getPath());
|
2010-10-29 13:19:51 +00:00
|
|
|
}
|
2011-01-28 07:37:00 +00:00
|
|
|
} else {
|
2010-10-29 13:19:51 +00:00
|
|
|
|
|
|
|
// create a server backup archive
|
2011-01-28 07:37:00 +00:00
|
|
|
$handle = opendir($dms->contentDir.$document->getDir());
|
|
|
|
while ($entry = readdir($handle) ) {
|
|
|
|
if (!is_dir($dms->contentDir.$document->getDir().$entry)){
|
|
|
|
|
|
|
|
TarAddHeader($ark,$dms->contentDir.$document->getDir().$entry,$document->getDir().$entry);
|
|
|
|
TarWriteContents($ark,$dms->contentDir.$document->getDir().$entry);
|
|
|
|
}
|
|
|
|
}
|
2010-10-29 13:19:51 +00:00
|
|
|
closedir($handle);
|
|
|
|
}
|
2011-01-28 07:37:00 +00:00
|
|
|
}
|
2010-10-29 13:19:51 +00:00
|
|
|
}
|
|
|
|
|
2011-01-28 07:37:00 +00:00
|
|
|
$subFolders=$folder->getSubfolders();
|
|
|
|
foreach ($subFolders as $folder)
|
2015-01-08 13:53:53 +00:00
|
|
|
if (!createFolderTar($folder,$ark,$human_readable,$dms))
|
2011-01-28 07:37:00 +00:00
|
|
|
return false;
|
|
|
|
|
2010-10-29 13:19:51 +00:00
|
|
|
return true;
|
2011-01-28 07:37:00 +00:00
|
|
|
} /* }}} */
|
2010-10-29 13:19:51 +00:00
|
|
|
|
2014-07-08 06:30:59 +00:00
|
|
|
if (!isset($_GET["targetid"]) || !is_numeric($_GET["targetid"]) || intval($_GET["targetid"])<1) {
|
2011-01-28 07:37:00 +00:00
|
|
|
UI::exitError(getMLText("admin_tools"),getMLText("invalid_folder_id"));
|
|
|
|
}
|
2014-07-08 06:30:59 +00:00
|
|
|
$folderid = $_GET["targetid"];
|
2010-11-26 08:34:47 +00:00
|
|
|
$folder = $dms->getFolder($folderid);
|
2011-01-28 07:37:00 +00:00
|
|
|
|
|
|
|
if (!is_object($folder)) {
|
|
|
|
UI::exitError(getMLText("admin_tools"),getMLText("invalid_folder_id"));
|
2010-10-29 13:19:51 +00:00
|
|
|
}
|
|
|
|
|
2019-07-30 04:39:25 +00:00
|
|
|
if (!$settings->_backupDir) {
|
|
|
|
UI::exitError(getMLText("admin_tools"),getMLText("no_backup_dir"));
|
|
|
|
}
|
|
|
|
|
2010-10-29 13:19:51 +00:00
|
|
|
$human_readable = (isset($_GET["human_readable"]) && $_GET["human_readable"]==1 ? true : false);
|
|
|
|
|
2019-07-30 04:39:25 +00:00
|
|
|
if ($human_readable)$ark_name = addDirSep($settings->_backupDir).time()."_".$folderid."_HR.tar";
|
|
|
|
else $ark_name = addDirSep($settings->_backupDir).time()."_".$folderid.".tar";
|
2011-01-28 07:37:00 +00:00
|
|
|
|
2010-10-29 13:19:51 +00:00
|
|
|
$ark = fopen($ark_name,"w");
|
2011-01-28 07:37:00 +00:00
|
|
|
|
2015-01-08 13:53:53 +00:00
|
|
|
if (!createFolderTar($folder,$ark, $human_readable, $dms)) {
|
2010-10-29 13:19:51 +00:00
|
|
|
fclose($ark);
|
2011-01-28 07:37:00 +00:00
|
|
|
unlink($ark_name);
|
|
|
|
UI::exitError(getMLText("admin_tools"),getMLText("error_occured"));
|
2010-10-29 13:19:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TarAddFooter($ark);
|
|
|
|
fclose($ark);
|
|
|
|
|
2013-02-14 11:10:53 +00:00
|
|
|
if (SeedDMS_Core_File::gzcompressfile($ark_name,9)) unlink($ark_name);
|
2010-10-29 13:19:51 +00:00
|
|
|
else UI::exitError(getMLText("admin_tools"),getMLText("error_occured"));
|
|
|
|
|
|
|
|
add_log_line();
|
|
|
|
|
2011-01-28 07:37:00 +00:00
|
|
|
header("Location:../out/out.BackupTools.php");
|
|
|
|
|
|
|
|
?>
|