1
1
<?php
2
2
/*
3
- * html2canvas-php-proxy 1.1.0
3
+ * html2canvas-php-proxy 1.1.1
4
4
*
5
5
* Copyright (c) 2018 Guilherme Nascimento (brcontainer@yahoo.com.br)
6
6
*
19
19
define ('H2CP_DATAURI ' , false ); // Enable use of "data URI scheme"
20
20
define ('H2CP_PREFER_CURL ' , true ); // Enable curl if avaliable or disable
21
21
define ('H2CP_SECPREFIX ' , 'h2cp_ ' ); // Prefix temp filename
22
- define ('H2CP_ALLOWED_DOMAINS ' , '* ' ); // * allow all domains, *.site.com for sub-domains, or fixed domains use array( 'site.com', 'www.site.com' )
23
- define ('H2CP_ALLOWED_PORTS ' , '80,443 ' ); // Allowed ports
22
+ define ('H2CP_ALLOWED_DOMAINS ' , '* ' ); // * allow all domains, *.site.com for sub-domains, or fixed domains use `define('H2CP_ALLOWED_DOMAINS', 'site.com,www.site.com' )
23
+ define ('H2CP_ALLOWED_PORTS ' , '80,443 ' ); // Allowed ports
24
+ define ('H2CP_ALTERNATIVE ' , 'console.log ' ); // callback alternative
24
25
25
26
/*
26
27
* Set false for disable SSL check
@@ -130,13 +131,13 @@ function checkContentType($content)
130
131
$ content = strtolower ($ content );
131
132
$ encode = null ;
132
133
133
- if (preg_match ('#[;](\s|)+ charset[=]# ' , $ content ) === 1 ) {
134
- $ encode = preg_split ('#[;](\s|)+ charset[=]# ' , $ content );
134
+ if (preg_match ('#[;]( \\ s+)? charset[=]# ' , $ content ) === 1 ) {
135
+ $ encode = preg_split ('#[;]( \\ s+)? charset[=]# ' , $ content );
135
136
$ encode = empty ($ encode [1 ]) ? null : trim ($ encode [1 ]);
136
137
}
137
138
138
139
$ mime = trim (
139
- preg_replace ('/ [;]([ \\ s \\ S]|)+$/ ' , '' ,
140
+ preg_replace ('# [;](.*)?$# ' , '' ,
140
141
str_replace ('content-type: ' , '' ,
141
142
str_replace ('/x- ' , '/ ' , $ content )
142
143
)
@@ -253,7 +254,7 @@ function relativeToAbsolute($url, $relative)
253
254
if (preg_match ('#^[a-z0-9]+[:]#i ' , $ relative ) !== 0 ) {
254
255
$ pu = parse_url ($ relative );
255
256
256
- if (preg_match ('#^(http| https) $#i ' , $ pu ['scheme ' ]) === 0 ) {
257
+ if (preg_match ('#^https? $#i ' , $ pu ['scheme ' ]) === 0 ) {
257
258
return '' ;
258
259
}
259
260
@@ -284,7 +285,7 @@ function relativeToAbsolute($url, $relative)
284
285
$ pm = parse_url ('http://1/ ' . $ relative );
285
286
$ pm ['path ' ] = isset ($ pm ['path ' ]) ? $ pm ['path ' ] : '' ;
286
287
287
- $ isPath = $ pm ['path ' ] !== '' && strpos (strrev ($ pm ['path ' ]), '/ ' ) === 0 ? true : false ;
288
+ $ isPath = $ pm ['path ' ] !== '' && strpos (strrev ($ pm ['path ' ]), '/ ' ) === 0 ;
288
289
289
290
if (strpos ($ relative , '/ ' ) === 0 ) {
290
291
$ pu ['path ' ] = '' ;
@@ -336,7 +337,7 @@ function relativeToAbsolute($url, $relative)
336
337
*/
337
338
function isHttpUrl ($ url )
338
339
{
339
- return preg_match ('#^http(|s) [:][/][/][a-z0-9] #i ' , $ url ) === 1 ;
340
+ return preg_match ('#^https? [:]//. #i ' , $ url ) === 1 ;
340
341
}
341
342
342
343
/**
@@ -352,7 +353,7 @@ function isAllowedUrl($url, &$message) {
352
353
if (in_array ('* ' , $ domains ) === false ) {
353
354
$ ok = false ;
354
355
355
- foreach (H2CP_ALLOWED_DOMAINS as $ domain ) {
356
+ foreach ($ domains as $ domain ) {
356
357
if ($ domain === $ uri ['host ' ]) {
357
358
$ ok = true ;
358
359
break ;
@@ -413,7 +414,7 @@ function createFolder()
413
414
*/
414
415
function createTmpFile ($ basename , $ isEncode )
415
416
{
416
- $ folder = preg_replace ('#[/] $# ' , '' , H2CP_PATH ) . '/ ' ;
417
+ $ folder = preg_replace ('#/ $# ' , '' , H2CP_PATH ) . '/ ' ;
417
418
418
419
if ($ isEncode === false ) {
419
420
$ basename = H2CP_SECPREFIX . strlen ($ basename ) . '. ' . sha1 ($ basename );
@@ -426,7 +427,7 @@ function createTmpFile($basename, $isEncode)
426
427
return createTmpFile ($ basename , true );
427
428
}
428
429
429
- $ source = fopen ($ folder . $ basename . $ tmpMime , 'w ' );
430
+ $ source = fopen ($ folder . $ basename . $ tmpMime , 'wb ' );
430
431
431
432
if ($ source !== false ) {
432
433
return array (
@@ -450,14 +451,14 @@ function curlDownloadSource($url, $toSource)
450
451
451
452
//Reformat url
452
453
$ currentUrl = (empty ($ uri ['scheme ' ]) ? 'http ' : $ uri ['scheme ' ]) . ':// ' ;
453
- $ currentUrl .= empty ($ uri ['host ' ]) ? '' : $ uri ['host ' ];
454
+ $ currentUrl .= empty ($ uri ['host ' ]) ? '' : $ uri ['host ' ];
454
455
455
456
if (isset ($ uri ['port ' ])) {
456
457
$ currentUrl .= ': ' . $ uri ['port ' ];
457
458
}
458
459
459
- $ currentUrl .= empty ($ uri ['path ' ]) ? '/ ' : $ uri ['path ' ];
460
- $ currentUrl .= empty ($ uri ['query ' ]) ? '' : ('? ' . $ uri ['query ' ]);
460
+ $ currentUrl .= empty ($ uri ['path ' ]) ? '/ ' : $ uri ['path ' ];
461
+ $ currentUrl .= empty ($ uri ['query ' ]) ? '' : ('? ' . $ uri ['query ' ]);
461
462
462
463
$ ch = curl_init ();
463
464
@@ -618,7 +619,7 @@ function downloadSource($url, $toSource, $caller)
618
619
}
619
620
620
621
if ($ isHttp === false ) {
621
- if (preg_match ('#^HTTP[/]1[.] #i ' , $ data ) === 0 ) {
622
+ if (preg_match ('#^HTTP/1\. #i ' , $ data ) === 0 ) {
622
623
fclose ($ fp );//Close connection
623
624
$ data = '' ;
624
625
return array ('error ' => 'This request did not return a HTTP response valid ' );
@@ -633,7 +634,7 @@ function downloadSource($url, $toSource, $caller)
633
634
$ data = '' ;
634
635
return array ('error ' => 'Request returned HTTP_304, this status code is incorrect because the html2canvas not send Etag ' );
635
636
} else {
636
- $ isRedirect = preg_match ('#^(301|302|303|307|308) $# ' , $ tmp ) !== 0 ;
637
+ $ isRedirect = preg_match ('#^3 \\ d{2} $# ' , $ tmp ) !== 0 ;
637
638
638
639
if ($ isRedirect === false && $ tmp !== '200 ' ) {
639
640
fclose ($ fp );
@@ -669,11 +670,11 @@ function downloadSource($url, $toSource, $caller)
669
670
}
670
671
671
672
return downloadSource ($ data , $ toSource , $ caller );
672
- } elseif (preg_match ('#^content[-] length[:]( 0|0) $#i ' , $ data ) !== 0 ) {
673
+ } elseif (preg_match ('#^content- length[:]( \\ s)?0 $#i ' , $ data ) !== 0 ) {
673
674
fclose ($ fp );
674
675
$ data = '' ;
675
676
return array ('error ' => 'source is blank (Content-length: 0) ' );
676
- } elseif (preg_match ('#^content[-] type[:]#i ' , $ data ) !== 0 ) {
677
+ } elseif (preg_match ('#^content- type[:]#i ' , $ data ) !== 0 ) {
677
678
$ response = checkContentType ($ data );
678
679
679
680
if (isset ($ response ['error ' ])) {
@@ -826,7 +827,7 @@ function downloadSource($url, $toSource, $caller)
826
827
echo H2CP_JSONP , '( ' ,
827
828
JsonEncodeString (
828
829
($ http_port === 443 ? 'https:// ' : 'http:// ' ) .
829
- preg_replace ('#:[0-9] +$# ' , '' , $ _SERVER ['HTTP_HOST ' ]) .
830
+ preg_replace ('#[:] \\ d +$# ' , '' , $ _SERVER ['HTTP_HOST ' ]) .
830
831
($ http_port === 80 || $ http_port === 443 ? '' : (
831
832
': ' . $ _SERVER ['SERVER_PORT ' ]
832
833
)) .
@@ -853,7 +854,9 @@ function downloadSource($url, $toSource, $caller)
853
854
854
855
removeOldFiles ();
855
856
856
- echo H2CP_JSONP , '( ' ,
857
+ $ callback = H2CP_JSONP !== false ? H2CP_ALTERNATIVE ;
858
+
859
+ echo $ callback , '( ' ,
857
860
JsonEncodeString (
858
861
'error: html2canvas-proxy-php: ' . $ response ['error ' ]
859
862
),
0 commit comments