|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SugarAPI\SDK\Response; |
|
4
|
|
|
|
|
5
|
|
|
use SugarAPI\SDK\Response\Abstracts\AbstractResponse; |
|
6
|
|
|
|
|
7
|
|
|
class File extends AbstractResponse { |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* @var |
|
11
|
|
|
*/ |
|
12
|
|
|
protected $fileName; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* File Path for response |
|
16
|
|
|
* @var string |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $destinationPath; |
|
19
|
|
|
|
|
20
|
|
|
public function __construct($curlResponse, $curlRequest,$destination = null) { |
|
21
|
|
|
parent::__construct($curlResponse, $curlRequest); |
|
22
|
|
|
$this->extractFileName(); |
|
23
|
|
|
if (!empty($destination)) { |
|
24
|
|
|
$this->setupDestiantion($destination); |
|
25
|
|
|
$this->writeFile(); |
|
26
|
|
|
} |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
protected function setupDestiantion($destination = null){ |
|
30
|
|
|
if (empty($destination)){ |
|
31
|
|
|
$destination = sys_get_temp_dir().'/SugarAPI'; |
|
32
|
|
|
if (!file_exists($destination)){ |
|
33
|
|
|
mkdir($destination,0777); |
|
34
|
|
|
} |
|
35
|
|
|
} |
|
36
|
|
|
$this->destinationPath = $destination; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
protected function extractFileName(){ |
|
40
|
|
|
foreach (explode("\r\n",$this->headers) as $header) |
|
41
|
|
|
{ |
|
42
|
|
|
if (strpos($header,'filename')!==FALSE){ |
|
43
|
|
|
$this->fileName = substr($header,(strpos($header,"\"")+1),-1); |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function getFileName(){ |
|
49
|
|
|
return $this->fileName; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
protected function writeFile(){ |
|
53
|
|
|
$fileHandle = fopen($this->file(),'w+'); |
|
54
|
|
|
fwrite($fileHandle,$this->body); |
|
55
|
|
|
fclose($fileHandle); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function file(){ |
|
59
|
|
|
return rtrim($this->destinationPath,DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.$this->fileName; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
} |