-
Notifications
You must be signed in to change notification settings - Fork 0
/
reharvest.php
104 lines (96 loc) · 3.77 KB
/
reharvest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
function getFile($url, $zip=false,$tmpziphandle=null){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, trim($url));
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
if($zip){
curl_setopt($ch, CURLOPT_BINARYTRANSFER,true);
curl_setopt($ch, CURLOPT_FILE, $tmpziphandle);
}
$data = curl_exec($ch);
echo $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return ($httpcode>=200 && $httpcode<300) ? $data : false;
}
$abcdrootdir='ABCD';
$abcddir='ABCD';
$archivejson=json_decode(file_get_contents($abcdrootdir.'/archives.txt'));
$tempzip='ABCD/tmp.zip';
if(isset($_GET['archive'])){
$foundinJSON=false;
#
if(file_exists($abcdrootdir.'/'.$_GET['archive'])){
foreach($archivejson as $aid=>$existingarchives){
if($existingarchives->archive_folder==$_GET['archive']){
$archivezip=$existingarchives->archive_url;
$extraxtzipdir=basename($archivezip,'.zip');
$extractdir=preg_replace('![^A-Za-z]+!','_', $extraxtzipdir);
echo $extractdir.'<br><hr>';
if($extractdir==$_GET['archive']){
echo '<br>Opening ZIP Archive: '.$archivezip.' to be extracted to: '.$extractdir.'<br>'."\r\n";;
$ziphandle = fopen($tempzip, "w"); if($ziphandle===false) {echo 'Insufficient rights to create temp ZIP..';exit;}
$zipfile=getFile($archivezip,true,$ziphandle);
$zip = new ZipArchive;
$zipres = $zip->open($tempzip);
if($zipres===true){
echo'reharvesting..<br>';
if(!file_exists($abcddir.'/'.$extractdir)){
mkdir($abcddir.'/'.$extractdir);
}elseif(!is_dir($abcddir.'/'.$extractdir)){
if(filesize($abcddir.'/'.$extractdir)==0){
unlink($abcddir.'/'.$extractdir);
mkdir($abcddir.'/'.$extractdir);
}
}
echo 'Extracting ZIP Archive to '.$abcddir.'/'.$extractdir.'<br>'."\r\n";
//delete old files
echo 'deleting old files..'.'<br>'."\r\n";
$deletefiles = glob($abcddir.'/'.$extractdir.'/*'); // get all file names
print_r($deletefiles);
foreach($deletefiles as $delfile){ // iterate files
echo ($delfile);
if(is_file($delfile))
unlink($delfile); // delete file
}
//extract and rename
echo 'extract and rename..'.'<br>'."\r\n";
for($f=0;$fst=$zip->statIndex($f);$f++){
#print_r($zip->statIndex($f));
$filename = $zip->getNameIndex($f);
$fileinfo = pathinfo($filename);
echo $newname=str_pad($f+1, 5 ,'0', STR_PAD_LEFT);
//echo $zip->renameIndex($f,'newresponse.'.$newname.'.xml');
copy("zip://".$tempzip."#".$filename, $abcddir.'/'.$extractdir.'/response.'.$newname.'.xml');
}
touch($abcddir.'/'.$extractdir, $zip->statIndex(0)['mtime']);
$archivejson[$aid]->harvest_status='success';
$archivejson[$aid]->harvest_time=date ('c');
$zip->close();
}
}
foreach($existingarchives->xml_archives as $archive){
if($archive->latest==1){
echo $archivezip= $archive->xml_archive;
echo'<br>';
#
#$extractdir=preg_replace('![^A-Za-z]+!','_', $extraxtzipdir);
#if()
#
#$zip = new ZipArchive;
}
}
$foundinJSON=true;
echo 'Marked '.$_GET['archive'].' for reharvesting';
}
}
if(!$foundinJSON){
echo 'No entry in JSON File..';
}
echo'<br> Saved modified XML files..';
print_r($archivejson);
file_put_contents($abcdrootdir.'/archives.txt',json_encode($archivejson));
}else echo 'Archive does not exist';
}else echo 'Please enter a ABCD Archive Folder';
?>