1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: pedro |
5
|
|
|
* Date: 24/11/16 |
6
|
|
|
* Time: 17:19 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Classes\Update\Content; |
10
|
|
|
|
11
|
|
|
use Classes\Update\ProgressBar; |
12
|
|
|
use Classes\Update\ProtocolFileContent; |
13
|
|
|
|
14
|
|
|
abstract class AbstractContent |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
private static $opts = array ( |
18
|
|
|
'http' => array ( |
19
|
|
|
'method' => 'GET' , |
20
|
|
|
'header' => array ( |
21
|
|
|
'User-Agent: PHP' |
22
|
|
|
) |
23
|
|
|
) |
24
|
|
|
); |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @type \Classes\Update\ProtocolFileContent |
28
|
|
|
*/ |
29
|
|
|
protected $objProtocol; |
30
|
|
|
/** |
31
|
|
|
* @type Content |
32
|
|
|
*/ |
33
|
|
|
private static $objContent; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @type array |
37
|
|
|
*/ |
38
|
|
|
private $content = array (); |
39
|
|
|
|
40
|
|
|
protected function __construct () |
41
|
|
|
{ |
42
|
|
|
$this->objProtocol = ProtocolFileContent::getInstance (); |
43
|
|
|
$this->init (); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
protected function init (){ } |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return \Classes\Update\Content |
50
|
|
|
*/ |
51
|
|
|
public static function getInstance () |
52
|
|
|
{ |
53
|
|
|
if ( empty( self::$objContent ) ) |
54
|
|
|
{ |
55
|
|
|
self::$objContent = new static(); |
|
|
|
|
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return self::$objContent; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param $url |
63
|
|
|
* |
64
|
|
|
* @return string |
65
|
|
|
*/ |
66
|
|
|
public function getContent ( $url , $progress = false ) |
67
|
|
|
{ |
68
|
|
|
if ( ! isset( $this->content[ $url ] ) ) |
69
|
|
|
{ |
70
|
|
|
$this->content[ $url ] = ''; |
71
|
|
|
switch ( $this->objProtocol->getProtocol () ) |
72
|
|
|
{ |
73
|
|
|
case 'curl': |
74
|
|
|
$this->content[ $url ] = $this->getCurlContent ( $url , $progress ); |
75
|
|
|
break; |
76
|
|
|
case 'file_content': |
77
|
|
|
$this->content[ $url ] = $this->getFileContent ( $url , $progress ); |
78
|
|
|
break; |
79
|
|
|
case 'steam_content': |
80
|
|
|
$this->content[ $url ] = $this->getStreamContent ( $url , $progress ); |
81
|
|
|
break; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $this->content[ $url ]; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param $url |
90
|
|
|
* |
91
|
|
|
* @return string |
92
|
|
|
*/ |
93
|
|
|
protected function getCurlContent ( $url , $progress = false ) |
94
|
|
|
{ |
95
|
|
|
$conn = curl_init ( $url ); |
96
|
|
|
curl_setopt ( $conn , CURLOPT_RETURNTRANSFER , true ); |
97
|
|
|
curl_setopt ( $conn , CURLOPT_BINARYTRANSFER , true ); |
98
|
|
|
curl_setopt ( $conn , CURLOPT_USERAGENT , self::$opts[ 'http' ][ 'method' ] ); |
99
|
|
|
if ( $progress ) |
100
|
|
|
{ |
101
|
|
|
curl_setopt ( $conn , CURLOPT_NOPROGRESS , false ); |
102
|
|
|
curl_setopt ( $conn , CURLOPT_PROGRESSFUNCTION , array ( |
103
|
|
|
$this , 'progressCallback' |
104
|
|
|
) ); |
105
|
|
|
} |
106
|
|
|
$url_get_contents_data = ( curl_exec ( $conn ) ); |
107
|
|
|
curl_close ( $conn ); |
108
|
|
|
|
109
|
|
|
return $url_get_contents_data; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param $url |
114
|
|
|
* |
115
|
|
|
* @return string |
116
|
|
|
*/ |
117
|
|
View Code Duplication |
protected function getFileContent ( $url , $progress = false ) |
|
|
|
|
118
|
|
|
{ |
119
|
|
|
$context = stream_context_create ( self::$opts ); |
120
|
|
|
if ( $progress ) |
121
|
|
|
{ |
122
|
|
|
stream_context_set_params ( $context , array ( |
123
|
|
|
"notification" => array ( |
124
|
|
|
$this , 'stream_notification_callback' |
125
|
|
|
) |
126
|
|
|
) ); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return file_get_contents ( $url , false , $context ); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param $url |
134
|
|
|
* |
135
|
|
|
* @return string |
136
|
|
|
*/ |
137
|
|
View Code Duplication |
protected function getStreamContent ( $url , $progress = false ) |
|
|
|
|
138
|
|
|
{ |
139
|
|
|
$context = stream_context_create ( self::$opts ); |
140
|
|
|
if ( $progress ) |
141
|
|
|
{ |
142
|
|
|
stream_context_set_params ( $context , array ( |
143
|
|
|
"notification" => array ( |
144
|
|
|
$this , 'stream_notification_callback' |
145
|
|
|
) |
146
|
|
|
) ); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
$handle = fopen ( $url , "r" , null , $context ); |
150
|
|
|
|
151
|
|
|
return stream_get_contents ( $handle ); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public function putContent ( $url , $content ) |
155
|
|
|
{ |
156
|
|
|
switch ( $this->objProtocol->getProtocol () ) |
157
|
|
|
{ |
158
|
|
|
case 'curl': |
159
|
|
|
$this->putFopen ( $url , $content ); |
160
|
|
|
break; |
161
|
|
|
case 'steam_content': |
162
|
|
|
case 'file_content': |
163
|
|
|
$this->putFileContent ( $url , $content ); |
164
|
|
|
break; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
public function putFileContent ( $url , $content ) |
170
|
|
|
{ |
171
|
|
|
// check if all is OK |
172
|
|
|
if ( file_put_contents ( $url , $content ) ) |
173
|
|
|
{ |
174
|
|
|
ProgressBar::getInstance ()->finish (); |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
public function putFopen ( $url , $content ) |
179
|
|
|
{ |
180
|
|
|
$fp = fopen ( $url , "a" ); |
181
|
|
|
fwrite ( $fp , $content ); |
182
|
|
|
fclose ( $fp ); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Stream downloading |
187
|
|
|
* |
188
|
|
|
* @param $notification_code |
189
|
|
|
* @param $severity |
190
|
|
|
* @param $message |
191
|
|
|
* @param $message_code |
192
|
|
|
* @param $bytes_transferred |
193
|
|
|
* @param $bytes_max |
194
|
|
|
*/ |
195
|
|
|
public function stream_notification_callback ( $notificationCode , $severity , $message , $messageCode , $bytesTransferred , $bytesMax ) |
196
|
|
|
{ |
197
|
|
|
$objProgress = ProgressBar::getInstance (); |
198
|
|
|
switch ( $notificationCode ) |
199
|
|
|
{ |
200
|
|
|
case STREAM_NOTIFY_RESOLVE: |
201
|
|
|
case STREAM_NOTIFY_AUTH_REQUIRED: |
202
|
|
|
case STREAM_NOTIFY_FAILURE: |
203
|
|
|
case STREAM_NOTIFY_AUTH_RESULT: |
204
|
|
|
var_dump ( $notificationCode , $severity , $message , $messageCode , $bytesTransferred , $bytesMax ); |
|
|
|
|
205
|
|
|
/* Ignore */ |
206
|
|
|
break; |
207
|
|
|
case STREAM_NOTIFY_CONNECT: |
208
|
|
|
echo "\033[1;32mConnected...\033[0m\n"; |
209
|
|
|
break; |
210
|
|
|
case STREAM_NOTIFY_REDIRECTED: |
211
|
|
|
$objProgress->clear (); |
212
|
|
|
break; |
213
|
|
|
case STREAM_NOTIFY_FILE_SIZE_IS: |
214
|
|
|
$objProgress->clear ()->setMaxByte ( $bytesMax ); |
215
|
|
|
break; |
216
|
|
|
case STREAM_NOTIFY_PROGRESS: |
217
|
|
|
$objProgress->setProgress ( $bytesTransferred ) |
218
|
|
|
->render (); |
219
|
|
|
break; |
220
|
|
|
case STREAM_NOTIFY_COMPLETED: |
221
|
|
|
$objProgress->finish (); |
222
|
|
|
break; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
function progressCallback ( $download_size , $downloaded_size , $upload_size , $uploaded_size ) |
|
|
|
|
228
|
|
|
{ |
229
|
|
|
ProgressBar::getInstance () |
230
|
|
|
->clear () |
231
|
|
|
->setMaxByte ( $download_size ) |
232
|
|
|
->setProgress ( $downloaded_size ) |
233
|
|
|
->render () |
234
|
|
|
->finish (); |
235
|
|
|
|
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
} |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..