|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Jaxon\Response; |
|
4
|
|
|
|
|
5
|
|
|
use Jaxon\Contracts\Response as ResponseContract; |
|
6
|
|
|
|
|
7
|
|
|
class UploadResponse implements ResponseContract |
|
8
|
|
|
{ |
|
9
|
|
|
use \Jaxon\Features\Config; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* The response type |
|
13
|
|
|
* |
|
14
|
|
|
* @var string |
|
15
|
|
|
*/ |
|
16
|
|
|
private $sContentType = 'text/html'; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* The path to the uploaded file |
|
20
|
|
|
* |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
private $sUploadedFile = ''; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* The error message |
|
27
|
|
|
* |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
private $sErrorMessage = ''; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Get the content type, which is always set to 'text/json' |
|
34
|
|
|
* |
|
35
|
|
|
* @return string |
|
36
|
|
|
*/ |
|
37
|
|
|
public function getContentType() |
|
38
|
|
|
{ |
|
39
|
|
|
return $this->sContentType; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Set the path to the uploaded file |
|
44
|
|
|
*/ |
|
45
|
|
|
public function setUploadedFile($sUploadedFile) |
|
46
|
|
|
{ |
|
47
|
|
|
$this->sUploadedFile = $sUploadedFile; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Set the error message |
|
52
|
|
|
*/ |
|
53
|
|
|
public function setErrorMessage($sErrorMessage) |
|
54
|
|
|
{ |
|
55
|
|
|
$this->sErrorMessage = $sErrorMessage; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Get the configured character encoding |
|
60
|
|
|
* |
|
61
|
|
|
* @return string |
|
62
|
|
|
*/ |
|
63
|
|
|
public function getCharacterEncoding() |
|
64
|
|
|
{ |
|
65
|
|
|
return $this->getOption('core.encoding'); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Add a command to display a debug message to the user |
|
70
|
|
|
* |
|
71
|
|
|
* @param string $sMessage The message to be displayed |
|
72
|
|
|
* |
|
73
|
|
|
* @return \Jaxon\Contracts\Response |
|
74
|
|
|
*/ |
|
75
|
|
|
public function debug($sMessage) |
|
76
|
|
|
{ |
|
77
|
|
|
// Todo: send this message to the console log. |
|
78
|
|
|
return $this; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Used internally to generate the response headers |
|
83
|
|
|
* |
|
84
|
|
|
* @return void |
|
85
|
|
|
*/ |
|
86
|
|
|
public function sendHeaders() |
|
87
|
|
|
{ |
|
88
|
|
|
$sCharacterSet = ''; |
|
89
|
|
|
$sCharacterEncoding = trim($this->getOption('core.encoding')); |
|
90
|
|
View Code Duplication |
if(($sCharacterEncoding) && strlen($sCharacterEncoding) > 0) |
|
|
|
|
|
|
91
|
|
|
{ |
|
92
|
|
|
$sCharacterSet = '; charset="' . trim($sCharacterEncoding) . '"'; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
header('content-type: ' . $this->sContentType . ' ' . $sCharacterSet); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Return the output, generated from the commands added to the response, that will be sent to the browser |
|
100
|
|
|
* |
|
101
|
|
|
* @return string |
|
102
|
|
|
*/ |
|
103
|
|
|
public function getOutput() |
|
104
|
|
|
{ |
|
105
|
|
|
$aResponse = ($this->sUploadedFile) ? |
|
106
|
|
|
['code' => 'success', 'upl' => $this->sUploadedFile] : |
|
107
|
|
|
['code' => 'error', 'msg' => $this->sErrorMessage]; |
|
108
|
|
|
return '<script>var res = ' . json_encode($aResponse) . '; </script>'; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Print the output, generated from the commands added to the response, that will be sent to the browser |
|
113
|
|
|
* |
|
114
|
|
|
* @return void |
|
115
|
|
|
*/ |
|
116
|
|
|
public function printOutput() |
|
117
|
|
|
{ |
|
118
|
|
|
print $this->getOutput(); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Merge the response commands from the specified <Response> object with |
|
123
|
|
|
* the response commands in this <Response> object |
|
124
|
|
|
* |
|
125
|
|
|
* @param ResponseContract $mCommands The <Response> object |
|
126
|
|
|
* @param boolean $bBefore Add the new commands to the beginning of the list |
|
127
|
|
|
* |
|
128
|
|
|
* @return void |
|
129
|
|
|
*/ |
|
130
|
|
|
public function appendResponse(ResponseContract $mCommands, $bBefore = false) |
|
131
|
|
|
{ |
|
132
|
|
|
// Nothing to do |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.