forked from JacerOmri/curl-axel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCurlAxel.php
611 lines (512 loc) · 15 KB
/
CurlAxel.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
<?php
/*
**************************************************************************************
* CurlAxel Class by JokerHacker
* This class is used to make parallel connections to download files.
* This is the first release, for feedback send mail to [email protected]
* This class is made to help PHP community. Pleas keep it away from commercial use.
**************************************************************************************
*/
class CurlAxel {
/*
* url to download, set by setUrl method
* string
*/
private $url = "";
/*
* custom filename, set by setFilename method (or automatically)
* string
*/
private $filename = "";
/*
* user curl option array to be passed to curl
* array
*/
private $optarray = array();
/*
* maintain curl multithread connections
* curl_multi handle
*/
private $megaconnection;
/*
* store partfile names
* array
*/
private $partnames = array();
/*
* temporary directory used by CurlAxel to store partfiles and logs
* string
*/
private $tempdir = "/temp/";
/*
* final download directory
* string
*/
private $downdir = "/downloads/";
/*
* number of splits (connections)
* integer
*/
private $partcount = 5;
/*
*
* options activated on user request
* inialized as false(bool)
* once used, type will be changed depending on each fonction
*
*/
/* bool */
private $log = false;
/* bool */
private $progress = false;
/* string */
private $cookies = false;
/* string */
private $cookiesfile = false;
/* string */
private $proxy = false;
/* string */
private $proxyauth = false;
/* string */
private $auth = false;
/* integer */
private $maxspeed = false;
/* integer */
private $minspeed = false;
/* integer */
private $minspeedtime = false;
/*---------------------------------------
*
* end of user activated options
*
*--------------------------------------- */
/*
* as default, buffer size is set to 64Mb
* integer
*/
private $buffersize = 67108864;
/*
* current CurlAxel version
*/
public $version = "1.0 beta 20/12/11";
/*
* good old construct medhod ;)
*/
function __construct() {
}
/*
* check and create folders if needed
*/
private function init() {
/* create temp dir */
$this->tempdir = getcwd() . $this->tempdir;
if(!is_dir($this->tempdir)) mkdir($this->tempdir);
/* create download dir */
$this->downdir = getcwd() . $this->downdir;
if(!is_dir($this->downdir)) mkdir($this->downdir);
}
/*
* activate/desactivate log
*/
public function activeLog($is) {
$this->log = (bool)$is;
}
/*
* set number of splits (connections)
*/
public function setParts($num) {
$this->partcount = (int)$num;
}
/*
* set temporary directory
*/
public function setTempDir($dir) {
$this->tempdir = $dir;
}
/*
* set download directory
*/
public function setDownloadDir($dir) {
$this->downdir = $dir;
}
/*
* set cookies as a string (if is set, cookiesfile is ignored)
*/
public function setCookies($cookies) {
$this->cookies = (string)$cookies;
}
/*
* set path of the cookie file
*/
public function setCookiesFile($cookiesfile) {
$this->cookiesfile = (string)$cookiesfile;
}
/*
* set proxy ip/port/auth
*/
public function setProxy($server, $port, $username = '', $password = '') {
$this->proxy = $server . ':' . $port;
if(($username != '') or ($password != '')) {
$this->proxyauth = $username . ':' . $password;
}
}
/*
* set connection auth
*/
public function setAuth($username, $password) {
$this->auth = $username . ':' . $password;
}
/*
* set maximum speed limit
*/
public function setMaxSpeed($speed) {
$this->maxspeed = (integer)$speed;
}
/*
* set minimum speed limit
*/
public function setMinSpeed($speed, $time) {
$this->minspeed = (integer)$speed;
$this->minspeedtime = (integer)$time;
}
/*
* user personalized curl options
*/
public function setCurlOpts($opts) {
$this->optarray = $opts;
}
/*
* activate/desactivate progressbars
*/
public function setProgressCallback($is) {
$this->progress = (bool)$is;
}
/*
* set file merge memory buffersize
*/
public function setBufferSize($buffersie){
$this->buffersize = (int)$buffersie;
}
/*
* returns filesize from url
* (integer) filesize on success
* (bool) false on fail
*/
public function getFileSize($url) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
$data = curl_exec($ch);
$filesize = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
curl_close($ch);
if ($filesize)
return $filesize;
else return false;
}
/*
* check the server behaviour against multithread connections
* (bool) true if curl_multi is applicable
* (bool) false on fail
*/
public function isMT($url) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_NOBODY, false); // Range in HEAD request is ignored in many servers
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_RANGE, 100-200);
curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
if($info['download_content_length'] != 100) return false;
else return true;
}
/*
* set url to download
* (bool) true if url is accepted
* (bool) false on fail
*/
public function setUrl($url) {
/* regex pattern for http(s)/ftp links only */
$pattern = "/(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:\/~\+#]*[\w\-\@?^=%&\/~\+#])?/i";
if (preg_match($pattern, $url)) {
$this->url = $url;
return true;
}
else return false;
}
/*
* set a filename
* (bool) true on success
* (bool) false if failed
*/
public function setFilename($filename) {
if (is_string($filename)) {
/* remove forbidden characters from filename */
$forbidden = '<>:"/\\|?*\'@#+~{}[]^';
str_replace(str_split($forbidden), '', $filename);
$this->filename = $filename;
return true;
}
return false;
}
/*
* extract needed file infos
*/
private function parseFile($issplit = true) {
$filename = basename(urldecode($this->url));
if ($this->filename == '') {
$this->filename = $filename;
}
$size = $this->getFileSize($this->url);
$this->size = $size;
/* make splits */
if($issplit) {
$this->splits = range(0, $size, ceil($size/$this->partcount));
}
}
/*
* download file using multithreaded connections
*/
public function fast_download() {
/* init CurlAxel folders */
$this->init();
$this->parseFile();
/* init log if activated */
if($this->log) $log = fopen($this->tempdir . 'log.txt', 'a+');
/* loop for creating curl handles */
for ($i = 0; $i <= sizeof($this->splits)-1; $i++) {
/* init curl*/
$ch[$i] = curl_init();
/* user option array */
curl_setopt_array($ch[$i], $this->optarray);
/* CurlAxel config */
curl_setopt($ch[$i], CURLOPT_URL, $this->url);
curl_setopt($ch[$i], CURLOPT_RETURNTRANSFER, false);
curl_setopt($ch[$i], CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch[$i], CURLOPT_FOLLOWLOCATION, true);
/* init log for current curl handle */
if($this->log) {
curl_setopt($ch[$i], CURLOPT_VERBOSE, true);
curl_setopt($ch[$i], CURLOPT_STDERR, $log); }
/* set the progress CallBack function
* TODO: add other progress outputs (xml, db and text)
*/
if ($this->progress) {
curl_setopt($ch[$i], CURLOPT_NOPROGRESS, false);
/* init a jquery progress bar */
echo '<span id="pb'. $i .'"></span>
<script>(function() {$("#pb'. $i .'").progressBar();})</script>';
/* create a temporary progress function for current curl
* handle and register it
*/
$progress = create_function('$download_size, $downloaded, $upload_size, $uploaded','static $sprog = 0;
@$prog = ceil($downloaded*100/$download_size);
if(!isset($time)) static $time = 0;
if (($prog > $sprog) and ((time() >= $time+1) or ($time == 0) or ($downloaded == $download_size))){
$sprog = $prog;
echo \'<script>$("#pb'. $i .'").progressBar(\'. $sprog. \');</script>\';
$time = time();
}');
curl_setopt($ch[$i], CURLOPT_PROGRESSFUNCTION, $progress);
curl_setopt($ch[$i], CURLOPT_BUFFERSIZE, 10*1024*1024);
}
/* set the cookies */
/* look for cookies string first */
if ($this->cookies) {
curl_setopt($ch[$i], CURLOPT_COOKIE, $this->cookies);
}
/* then check for cookies file */
elseif ($this->cookiesfile) {
curl_setopt($ch[$i], CURLOPT_COOKIEFILE, $this->cookiesfile);
}
/* set the proxy */
if ($this->proxy) {
curl_setopt($ch[$i], CURLOPT_HTTPPROXYTUNNEL, TRUE);
curl_setopt($ch[$i], CURLOPT_PROXY, $this->proxy);
/* if proxy auth is set */
if($this->proxyauth)
curl_setopt($ch[$i], CURLOPT_PROXYUSERPWD, $this->proxyauth);
}
/* set maximum speed limit */
if($this->maxspeed) {
curl_setopt($ch[$i], CURLOPT_MAX_RECV_SPEED_LARGE, $this->maxspeed);
}
/* set minimum speed limit */
if($this->minspeed) {
curl_setopt($ch[$i], CURLOPT_LOW_SPEED_LIMIT, $this->minspeed);
curl_setopt($ch[$i], CURLOPT_LOW_SPEED_TIME, $this->minspeedtme);
}
/* binary transfer to ensure losslessness */
curl_setopt($ch[$i], CURLOPT_BINARYTRANSFER, true);
/* not to use old cached connections */
curl_setopt($ch[$i], CURLOPT_FRESH_CONNECT, true);
/* connection timeout limit
* TODO: make it optional
*/
curl_setopt($ch[$i], CURLOPT_CONNECTTIMEOUT, 10);
/* register current part filename created by this handle */
$this->partnames[$i] = $this->filename . $i;
/* lock the part file */
$bh[$i] = fopen($this->tempdir . $this->partnames[$i], 'w+');
/* register the locked part file as current curl handle output */
curl_setopt($ch[$i], CURLOPT_FILE, $bh[$i]);
/* set part range */
$x = ($i == 0 ? 0 : $this->splits[$i]+1);
if($i == sizeof($this->splits)-1) {
$range = $x.'-';
} else {
$y = $this->splits[$i+1];
$range = $x.'-'.$y;
}
curl_setopt($ch[$i], CURLOPT_RANGE, $range);
/* register the current curl handle to be executed */
curl_multi_add_handle($this->megaconnection, $ch[$i]);
}
/*
* execute connections!
*/
$active = null;
do {
$mrc = curl_multi_exec($this->megaconnection, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
while ($active && $mrc == CURLM_OK) {
if (curl_multi_select($this->megaconnection) != -1) {
do {
$mrc = curl_multi_exec($this->megaconnection, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
}
}
/* create final file */
$finalpath = $this->downdir . $this->filename;
$final = fopen($finalpath, "w+");
/*
* merge splits into final file
*/
for ($i = 0; $i <= sizeof($this->splits)-1; $i++) {
$partpath = $this->tempdir . $this->partnames[$i];
/* !important : reset file handle index */
fseek($bh[$i], 0, SEEK_SET);
/* load parts and write to final file */
while (!feof($bh[$i])) {
$contents = fread($bh[$i], $this->buffersize);
fwrite($final, $contents);
}
/* close current file handle */
fclose($bh[$i]);
/* remove part file */
unlink($partpath);
}
fclose($final);
}
/*
* download file using a single connection
*/
public function slow_download() {
/* init CurlAxel folders */
$this->init();
$this->parseFile(false);
/* init log if activated */
if($this->log) $log = fopen($this->tempdir . 'log.txt', 'a+');
/* init curl*/
$ch = curl_init();
/* user option array */
curl_setopt_array($ch, $this->optarray);
/* CurlAxel config */
curl_setopt($ch, CURLOPT_URL, $this->url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
/* init log for current curl handle */
if($this->log) {
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_STDERR, $log);
}
/* set the progress CallBack function
* TODO: add other progress outputs (xml, db and text)
*/
if ($this->progress) {
curl_setopt($ch, CURLOPT_NOPROGRESS, false);
/* init a jquery progress bar */
echo '<span id="pb1"></span>
<script>(function() {$("#pb1").progressBar();})</script>';
/* create a temporary progress function for current curl
* handle and register it
*/
$progress = create_function('$download_size, $downloaded, $upload_size, $uploaded','static $sprog = 0;
@$prog = ceil($downloaded*100/$download_size);
if(!isset($time)) static $time = 0;
if (($prog > $sprog) and ((time() >= $time+1) or ($time == 0) or ($downloaded == $download_size))){
$sprog = $prog;
echo \'<script>$("#pb1").progressBar(\'. $sprog. \');</script>\';
$time = time();
}');
curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, $progress);
curl_setopt($ch, CURLOPT_BUFFERSIZE, 10*1024*1024);
}
/* set the cookies */
/* look for cookies string first */
if ($this->cookies) {
curl_setopt($ch, CURLOPT_COOKIE, $this->cookies);
}
/* then check for cookies file */
elseif ($this->cookiesfile) {
curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookiesfile);
}
/* set the proxy */
if ($this->proxy) {
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, TRUE);
curl_setopt($ch, CURLOPT_PROXY, $this->proxy);
if($this->proxyauth)
curl_setopt($ch, CURLOPT_PROXYUSERPWD, $this->proxyauth);
}
/* set maximum speed limit */
if($this->maxspeed) {
curl_setopt($ch, CURLOPT_MAX_RECV_SPEED_LARGE, $this->maxspeed);
}
/* set minimum speed limit */
if($this->minspeed) {
curl_setopt($ch, CURLOPT_LOW_SPEED_LIMIT, $this->minspeed);
curl_setopt($ch, CURLOPT_LOW_SPEED_TIME, $this->minspeedtme);
}
/* binary transfer to ensure losslessness */
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
/* not to use old cached connections */
curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
/* connection timeout limit
* TODO: make it optional
*/
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
/* lock output file */
$bh = fopen($this->downdir . $this->filename , 'w+');
/* register file handle to curl */
curl_setopt($ch, CURLOPT_FILE, $bh);
/* execute! */
curl_exec($ch);
curl_close($ch);
}
/*
* determines which type of download to use
*/
public function download() {
/* check if curl multi is applicable and determine filesize */
$isMT = $this->isMT($this->url);
$size = $this->getFileSize($this->url);
if($isMT and $size > 5*1024*1024 /* 5Mb */) {
/* initiate curl_multi handle */
$this->megaconnection = curl_multi_init();
/* start download using curl_multi handle */
$this->fast_download();
} else {
/* start download using single curl handle */
$this->slow_download();
}
}
}