-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathurlupload.php
162 lines (135 loc) · 3.67 KB
/
urlupload.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
/**
* Handles file upload from server
*
* @param String $fileurl
* @param String $url
* @param String $unzipdir
*/
class UrlUpload {
private $filename; //file name of the zip file
private $url; //The url where the file is hosted
private $unzipdir; //This is the directory where we will unzip our file
private $localname; // serial number portion of the zip file back from the uspto
function __construct($fileurl, $dir, $number)
{
if (!is_string($dir))
{
$this->unzipdir = getcwd() . DIRECTORY_SEPARATOR;
}
else
{
$this->unzipdir = $dir . DIRECTORY_SEPARATOR;
}
$this->filename = $this->unzipdir . "number_{$number}.zip";
$this->url = $fileurl;
}
public function getLocalname()
{
return $this->localname;
}
/**
* Grabs zip file from server
*
* @return Bool TRUE
* @throws Exception
*/
public function uploadFromUrl()
{
// validate the url
if(!filter_var($this->url, FILTER_VALIDATE_URL))
{
throw new Exception("The provided url is invalid");
}
$length=5120;
if(!($handle = fopen($this->url,'rb')))
{
throw new Exception("Url was not able to be opened");
}
// we need to remember the attachment filename. requests by rn comes with
// sn_status_st96.xml inside the zip.
// @TODO: find a more elegant way to do this!
$headers = implode('!', $http_response_header);
if(preg_match("/filename=(\d+)\.zip/", $headers, $matches))
$this->localname = $matches[1];
if(!($write = fopen($this->filename,'w')))
{
throw new Exception("Could not open zip file");
}
while(!feof($handle))
{
$buffer = fread($handle,$length);
fwrite($write,$buffer);
}
fclose ($handle);
fclose ($write);
$this->_unzip();
return true;
}
/**
* Unzip compressed file retireved from server
*
* @param Bool $newdir
* @param Bool $delete
* @param Bool $filename
* @return Bool TRUE
*/
private function _unzip($newdir=false,$delete=true,$filename=false)
{
/*
* Lets check if the user has provided a filename.
* This is usefull if they just want to unzip an existing file
*/
if (!$filename)
{
$filename = $this->filename;
}
// Set directory where the file will be unzipped
if(!$newdir)
//if the user has not provided a directory name use the one created
{
$newdir = $this->unzipdir;
}
// Check to see if the zip file exists
if (!file_exists($filename))
{
throw new Exception('The zip file does not exist');
}
// Lets check if the default zip class exists
if(class_exists('ZipArchive'))
{
$zip = new ZipArchive;
if($zip->open($filename) !== TRUE)
{
throw new Exception('Unable to open the zip file');
}
if(!($zip->extractTo($newdir)))
{
throw new Exception("Unable to extract zip file");
}
$zip->close();
}
else
{
// The zip class is missing. try unix shell command
@shell_exec('unzip -d $newdir '. $this->filename);
}
//If delete has been set to true then we delete the existing zip file
if ($delete)
{
unlink($filename);
$files = glob($this->unzipdir . "*.{css,html}", GLOB_BRACE);
foreach ($files as $file)
{
unlink($file);
}
// additional search for extra images
$tm5thumbnail = glob($this->unzipdir . "{markThumbnailImage,tm5Image}.*", GLOB_BRACE);
foreach ($tm5thumbnail as $extraImages)
{
unlink($extraImages);
}
}
return true;
}
}