1
1
<?php
2
2
/*
3
- * html2canvas-php-proxy 0.2.0
3
+ * html2canvas-php-proxy 0.2.1
4
4
*
5
- * Copyright (c) 2016 Guilherme Nascimento (brcontainer@yahoo.com.br)
5
+ * Copyright (c) 2017 Guilherme Nascimento (brcontainer@yahoo.com.br)
6
6
*
7
7
* Released under the MIT license
8
8
*/
9
9
10
10
//Turn off errors because the script already own uses "error_get_last"
11
- ini_set ('display_errors ' , 'On ' );
11
+ ini_set ('display_errors ' , 'Off ' );
12
12
13
13
//setup
14
14
define ('PATH ' , 'images ' ); //relative folder where the images are saved
15
15
define ('PATH_PERMISSION ' , 0666 ); //use 644 or 666 for remove execution for prevent sploits
16
16
define ('CCACHE ' , 60 * 5 * 1000 ); //Limit access-control and cache, define 0/false/null/-1 to not use "http header cache"
17
- define ('TIMEOUT ' , 2 ); //Timeout from load Socket
18
- define ('MAX_LOOP ' , 10 ); //Configure loop limit for redirect (location header)
19
- define ('CROSS_DOMAIN ' , true ); //Enable use of "data URI scheme"
20
- define ('SSL_VERIFY_PEER ' , false ); //Enable or disable SSL checking
17
+ define ('TIMEOUT ' , 30 ); //Timeout from load Socket
18
+ define ('MAX_LOOP ' , 10 ); //Configure loop limit for redirects (location header)
19
+ define ('CROSS_DOMAIN ' , false ); //Enable use of "data URI scheme"
21
20
define ('PREFER_CURL ' , true ); //Enable curl if avaliable or disable
22
21
22
+ /*
23
+ * Set false for disable SSL check
24
+ * Set true for enable SSL check, require config `curl.cainfo=/path/to/cacert.pem` in php.ini
25
+ * Set path (string) if need config CAINFO manualy like this define('SSL_VERIFY_PEER', '/path/to/cacert.pem');
26
+ */
27
+
28
+ define ('SSL_VERIFY_PEER ' , false );
29
+
23
30
//constants
24
31
define ('EOL ' , chr (10 ));
25
32
define ('WOL ' , chr (13 ));
37
44
38
45
$ http_port = 0 ;
39
46
40
- //set mime-type
41
- header ('Content-Type: application/javascript ' );
42
-
43
- $ param_callback = false ;//if not using callback
44
47
$ tmp = null ;//tmp var usage
45
48
$ response = array ();
46
49
@@ -136,7 +139,7 @@ function removeOldFiles()
136
139
*/
137
140
function getError ()
138
141
{
139
- if (false === function_exists ('error_get_last ' )) {
142
+ if (function_exists ('error_get_last ' ) === false ) {
140
143
return error_get_last ();
141
144
}
142
145
@@ -212,7 +215,7 @@ function JsonEncodeString($s, $onlyEncode=false)
212
215
} else {
213
216
if (isset ($ vetor [$ c ])) {
214
217
$ tmp = $ vetor [$ c ];
215
- } else if (($ c > 31 ) === false ) {
218
+ } elseif (($ c > 31 ) === false ) {
216
219
$ d = '000 ' . dechex ($ c );
217
220
$ tmp = '\\u ' . substr ($ d , strlen ($ d ) - 4 );
218
221
}
@@ -413,12 +416,27 @@ function curlDownloadSource($url, $toSource)
413
416
414
417
$ ch = curl_init ();
415
418
419
+ if (SSL_VERIFY_PEER === true ) {
420
+ curl_setopt ($ ch , CURLOPT_SSL_VERIFYPEER , true );
421
+ } elseif (is_string (SSL_VERIFY_PEER )) {
422
+ if (is_file (SSL_VERIFY_PEER )) {
423
+ curl_close ($ ch );
424
+ return array ('error ' => 'Not found certificate: ' . SSL_VERIFY_PEER );
425
+ }
426
+
427
+ curl_setopt ($ ch , CURLOPT_SSL_VERIFYPEER , true );
428
+ curl_setopt ($ ch , CURLOPT_CAINFO , SSL_VERIFY_PEER );
429
+ } else {
430
+ curl_setopt ($ ch , CURLOPT_SSL_VERIFYPEER , false );
431
+ }
432
+
433
+ curl_setopt ($ ch , CURLOPT_TIMEOUT , TIMEOUT );
416
434
curl_setopt ($ ch , CURLOPT_URL , $ currentUrl );
417
435
curl_setopt ($ ch , CURLOPT_HEADER , false );
418
436
curl_setopt ($ ch , CURLOPT_FOLLOWLOCATION , true );
437
+ curl_setopt ($ ch , CURLOPT_MAXREDIRS , MAX_LOOP );
419
438
curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , true );
420
439
curl_setopt ($ ch , CURLOPT_BINARYTRANSFER , true );
421
- curl_setopt ($ ch , CURLOPT_SSL_VERIFYPEER , SSL_VERIFY_PEER );
422
440
423
441
if (isset ($ uri ['user ' ])) {
424
442
curl_setopt ($ ch , CURLOPT_HTTPAUTH , CURLAUTH_ANY );
@@ -608,11 +626,11 @@ function downloadSource($url, $toSource, $caller)
608
626
}
609
627
610
628
return downloadSource ($ data , $ toSource , $ caller );
611
- } else if (preg_match ('#^content[-]length[:]( 0|0)$#i ' , $ data ) !== 0 ) {
629
+ } elseif (preg_match ('#^content[-]length[:]( 0|0)$#i ' , $ data ) !== 0 ) {
612
630
fclose ($ fp );
613
631
$ data = '' ;
614
632
return array ('error ' => 'source is blank (Content-length: 0) ' );
615
- } else if (preg_match ('#^content[-]type[:]#i ' , $ data ) !== 0 ) {
633
+ } elseif (preg_match ('#^content[-]type[:]#i ' , $ data ) !== 0 ) {
616
634
$ response = checkContentType ($ data );
617
635
618
636
if (isset ($ response ['error ' ])) {
@@ -622,15 +640,15 @@ function downloadSource($url, $toSource, $caller)
622
640
623
641
$ encode = $ response ['encode ' ];
624
642
$ mime = $ response ['mime ' ];
625
- } else if ($ isBody === false && trim ($ data ) === '' ) {
643
+ } elseif ($ isBody === false && trim ($ data ) === '' ) {
626
644
$ isBody = true ;
627
645
continue ;
628
646
}
629
- } else if ($ isRedirect === true ) {
647
+ } elseif ($ isRedirect === true ) {
630
648
fclose ($ fp );
631
649
$ data = '' ;
632
650
return array ('error ' => 'The response should be a redirect " ' . $ url . '", but did not inform which header "Localtion:" ' );
633
- } else if ($ mime === null ) {
651
+ } elseif ($ mime === null ) {
634
652
fclose ($ fp );
635
653
$ data = '' ;
636
654
return array ('error ' => 'Not set the mimetype from " ' . $ url . '" ' );
@@ -646,7 +664,7 @@ function downloadSource($url, $toSource, $caller)
646
664
647
665
if ($ isBody === false ) {
648
666
return array ('error ' => 'Content body is empty ' );
649
- } else if ($ mime === null ) {
667
+ } elseif ($ mime === null ) {
650
668
return array ('error ' => 'Not set the mimetype from " ' . $ url . '" ' );
651
669
}
652
670
@@ -657,23 +675,21 @@ function downloadSource($url, $toSource, $caller)
657
675
}
658
676
}
659
677
660
- if (false === empty ($ _GET ['callback ' ])) {
661
- $ param_callback = $ _GET ['callback ' ];
662
- }
678
+ define ('JSONP_CALLBACK ' , empty ($ _GET ['callback ' ]) ? false : $ _GET ['callback ' ]);
663
679
664
680
if (empty ($ _SERVER ['HTTP_HOST ' ])) {
665
681
$ response = array ('error ' => 'The client did not send the Host header ' );
666
- } else if (isset ($ _SERVER ['SERVER_PORT ' ]) === false ) {
682
+ } elseif (isset ($ _SERVER ['SERVER_PORT ' ]) === false ) {
667
683
$ response = array ('error ' => 'The Server-proxy did not send the PORT (configure PHP) ' );
668
- } else if (MAX_EXEC < 10 ) {
684
+ } elseif (MAX_EXEC < 10 ) {
669
685
$ response = array ('error ' => 'Execution time is less 15 seconds, configure this with ini_set/set_time_limit or "php.ini" (if safe_mode is enabled), recommended time is 30 seconds or more ' );
670
- } else if (MAX_EXEC <= TIMEOUT ) {
686
+ } elseif (MAX_EXEC <= TIMEOUT ) {
671
687
$ response = array ('error ' => 'The execution time is not configured enough to TIMEOUT in SOCKET, configure this with ini_set/set_time_limit or "php.ini" (if safe_mode is enabled), recommended that the "max_execution_time =;" be a minimum of 5 seconds longer or reduce the TIMEOUT in "define( \'TIMEOUT \', ' . TIMEOUT . ');" ' );
672
- } else if (empty ($ _GET ['url ' ])) {
688
+ } elseif (empty ($ _GET ['url ' ])) {
673
689
$ response = array ('error ' => 'No such parameter "url" ' );
674
- } else if (isHttpUrl ($ _GET ['url ' ]) === false ) {
690
+ } elseif (isHttpUrl ($ _GET ['url ' ]) === false ) {
675
691
$ response = array ('error ' => 'Only http scheme and https scheme are allowed ' );
676
- } else if (createFolder () === false ) {
692
+ } elseif (createFolder () === false ) {
677
693
$ err = getError ();
678
694
$ response = array ('error ' => 'Can not create directory ' . (
679
695
$ err !== null && empty ($ err ['message ' ]) ? '' : (': ' . $ err ['message ' ])
@@ -699,12 +715,15 @@ function downloadSource($url, $toSource, $caller)
699
715
}
700
716
}
701
717
718
+ //set mime-type
719
+ header ('Content-Type: application/javascript ' );
720
+
702
721
if (is_array ($ response ) && false === empty ($ response ['mime ' ])) {
703
722
clearstatcache ();
704
723
705
724
if (false === file_exists ($ tmp ['location ' ])) {
706
725
$ response = array ('error ' => 'Request was downloaded, but file can not be found, try again ' );
707
- } else if (filesize ($ tmp ['location ' ]) < 1 ) {
726
+ } elseif (filesize ($ tmp ['location ' ]) < 1 ) {
708
727
$ response = array ('error ' => 'Request was downloaded, but there was some problem and now the file is empty, try again ' );
709
728
} else {
710
729
$ extension = str_replace (array ('image/ ' , 'text/ ' , 'application/ ' ), '' , $ response ['mime ' ]);
@@ -725,41 +744,43 @@ function downloadSource($url, $toSource, $caller)
725
744
726
745
removeOldFiles ();
727
746
728
- if (false === $ param_callback ) {
729
- //set mime-type
730
- header ('Content-Type: ' . $ response ['mime ' ]);
731
- echo file_get_contents ($ locationFile );
732
- } else if (CROSS_DOMAIN === true ) {
733
- $ mime = $ response ['mime ' ];
734
- $ charset = JsonEncodeString ($ mime , true );
747
+ $ mime = $ response ['mime ' ];
735
748
736
- if ($ response ['encode ' ] !== null ) {
737
- $ mime .= ';charset= ' . JsonEncodeString ($ response ['encode ' ], true );
738
- }
749
+ if ($ response ['encode ' ] !== null ) {
750
+ $ mime .= ';charset= ' . JsonEncodeString ($ response ['encode ' ], true );
751
+ }
739
752
753
+ if (JSONP_CALLBACK === false ) {
754
+ header ('Content-Type: ' . $ mime );
755
+ echo file_get_contents ($ locationFile );
756
+ } elseif (CROSS_DOMAIN === true ) {
740
757
$ tmp = $ response = null ;
741
758
759
+ header ('Content-Type: application/javascript ' );
760
+
742
761
if (strpos ($ mime , 'image/svg ' ) !== 0 && strpos ($ mime , 'image/ ' ) === 0 ) {
743
- echo $ param_callback , '("data: ' , $ mime , ';base64, ' ,
762
+ echo JSONP_CALLBACK , '("data: ' , $ mime , ';base64, ' ,
744
763
base64_encode (
745
764
file_get_contents ($ locationFile )
746
765
),
747
766
'"); ' ;
748
767
} else {
749
- echo $ param_callback , '("data: ' , $ mime , ', ' ,
768
+ echo JSONP_CALLBACK , '("data: ' , $ mime , ', ' ,
750
769
asciiToInline (file_get_contents ($ locationFile )),
751
770
'"); ' ;
752
771
}
753
772
} else {
754
773
$ tmp = $ response = null ;
755
774
775
+ header ('Content-Type: application/javascript ' );
776
+
756
777
$ dir_name = dirname ($ _SERVER ['SCRIPT_NAME ' ]);
757
778
758
779
if ($ dir_name === '\/ ' || $ dir_name === '\\' ) {
759
780
$ dir_name = '' ;
760
781
}
761
782
762
- echo $ param_callback , '( ' ,
783
+ echo JSONP_CALLBACK , '( ' ,
763
784
JsonEncodeString (
764
785
($ http_port === 443 ? 'https:// ' : 'http:// ' ) .
765
786
preg_replace ('#:[0-9]+$# ' , '' , $ _SERVER ['HTTP_HOST ' ]) .
@@ -786,9 +807,11 @@ function downloadSource($url, $toSource, $caller)
786
807
//errors
787
808
setHeaders (true );//no-cache
788
809
810
+ header ('Content-Type: application/javascript ' );
811
+
789
812
removeOldFiles ();
790
813
791
- echo $ param_callback , '( ' ,
814
+ echo JSONP_CALLBACK , '( ' ,
792
815
JsonEncodeString (
793
816
'error: html2canvas-proxy-php: ' . $ response ['error ' ]
794
817
),
0 commit comments