Skip to content

Commit 96c5c6b

Browse files
authored
Revert upgrade
1 parent c2545d3 commit 96c5c6b

File tree

1 file changed

+67
-44
lines changed

1 file changed

+67
-44
lines changed

html2canvasproxy.php

Lines changed: 67 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,32 @@
11
<?php
22
/*
3-
* html2canvas-php-proxy 0.2.0
3+
* html2canvas-php-proxy 0.2.1
44
*
5-
* Copyright (c) 2016 Guilherme Nascimento (brcontainer@yahoo.com.br)
5+
* Copyright (c) 2017 Guilherme Nascimento (brcontainer@yahoo.com.br)
66
*
77
* Released under the MIT license
88
*/
99

1010
//Turn off errors because the script already own uses "error_get_last"
11-
ini_set('display_errors', 'On');
11+
ini_set('display_errors', 'Off');
1212

1313
//setup
1414
define('PATH', 'images'); //relative folder where the images are saved
1515
define('PATH_PERMISSION', 0666); //use 644 or 666 for remove execution for prevent sploits
1616
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"
2120
define('PREFER_CURL', true); //Enable curl if avaliable or disable
2221

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+
2330
//constants
2431
define('EOL', chr(10));
2532
define('WOL', chr(13));
@@ -37,10 +44,6 @@
3744

3845
$http_port = 0;
3946

40-
//set mime-type
41-
header('Content-Type: application/javascript');
42-
43-
$param_callback = false;//if not using callback
4447
$tmp = null;//tmp var usage
4548
$response = array();
4649

@@ -136,7 +139,7 @@ function removeOldFiles()
136139
*/
137140
function getError()
138141
{
139-
if (false === function_exists('error_get_last')) {
142+
if (function_exists('error_get_last') === false) {
140143
return error_get_last();
141144
}
142145

@@ -212,7 +215,7 @@ function JsonEncodeString($s, $onlyEncode=false)
212215
} else {
213216
if (isset($vetor[$c])) {
214217
$tmp = $vetor[$c];
215-
} else if (($c > 31) === false) {
218+
} elseif (($c > 31) === false) {
216219
$d = '000' . dechex($c);
217220
$tmp = '\\u' . substr($d, strlen($d) - 4);
218221
}
@@ -413,12 +416,27 @@ function curlDownloadSource($url, $toSource)
413416

414417
$ch = curl_init();
415418

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);
416434
curl_setopt($ch, CURLOPT_URL, $currentUrl);
417435
curl_setopt($ch, CURLOPT_HEADER, false);
418436
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
437+
curl_setopt($ch, CURLOPT_MAXREDIRS, MAX_LOOP);
419438
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
420439
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
421-
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, SSL_VERIFY_PEER);
422440

423441
if (isset($uri['user'])) {
424442
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
@@ -608,11 +626,11 @@ function downloadSource($url, $toSource, $caller)
608626
}
609627

610628
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) {
612630
fclose($fp);
613631
$data = '';
614632
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) {
616634
$response = checkContentType($data);
617635

618636
if (isset($response['error'])) {
@@ -622,15 +640,15 @@ function downloadSource($url, $toSource, $caller)
622640

623641
$encode = $response['encode'];
624642
$mime = $response['mime'];
625-
} else if ($isBody === false && trim($data) === '') {
643+
} elseif ($isBody === false && trim($data) === '') {
626644
$isBody = true;
627645
continue;
628646
}
629-
} else if ($isRedirect === true) {
647+
} elseif ($isRedirect === true) {
630648
fclose($fp);
631649
$data = '';
632650
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) {
634652
fclose($fp);
635653
$data = '';
636654
return array('error' => 'Not set the mimetype from "' . $url . '"');
@@ -646,7 +664,7 @@ function downloadSource($url, $toSource, $caller)
646664

647665
if ($isBody === false) {
648666
return array('error' => 'Content body is empty');
649-
} else if ($mime === null) {
667+
} elseif ($mime === null) {
650668
return array('error' => 'Not set the mimetype from "' . $url . '"');
651669
}
652670

@@ -657,23 +675,21 @@ function downloadSource($url, $toSource, $caller)
657675
}
658676
}
659677

660-
if (false === empty($_GET['callback'])) {
661-
$param_callback = $_GET['callback'];
662-
}
678+
define('JSONP_CALLBACK', empty($_GET['callback']) ? false : $_GET['callback']);
663679

664680
if (empty($_SERVER['HTTP_HOST'])) {
665681
$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) {
667683
$response = array('error' => 'The Server-proxy did not send the PORT (configure PHP)');
668-
} else if (MAX_EXEC < 10) {
684+
} elseif (MAX_EXEC < 10) {
669685
$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) {
671687
$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'])) {
673689
$response = array('error' => 'No such parameter "url"');
674-
} else if (isHttpUrl($_GET['url']) === false) {
690+
} elseif (isHttpUrl($_GET['url']) === false) {
675691
$response = array('error' => 'Only http scheme and https scheme are allowed');
676-
} else if (createFolder() === false) {
692+
} elseif (createFolder() === false) {
677693
$err = getError();
678694
$response = array('error' => 'Can not create directory'. (
679695
$err !== null && empty($err['message']) ? '' : (': ' . $err['message'])
@@ -699,12 +715,15 @@ function downloadSource($url, $toSource, $caller)
699715
}
700716
}
701717

718+
//set mime-type
719+
header('Content-Type: application/javascript');
720+
702721
if (is_array($response) && false === empty($response['mime'])) {
703722
clearstatcache();
704723

705724
if (false === file_exists($tmp['location'])) {
706725
$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) {
708727
$response = array('error' => 'Request was downloaded, but there was some problem and now the file is empty, try again');
709728
} else {
710729
$extension = str_replace(array('image/', 'text/', 'application/'), '', $response['mime']);
@@ -725,41 +744,43 @@ function downloadSource($url, $toSource, $caller)
725744

726745
removeOldFiles();
727746

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'];
735748

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+
}
739752

753+
if (JSONP_CALLBACK === false) {
754+
header('Content-Type: ' . $mime);
755+
echo file_get_contents($locationFile);
756+
} elseif (CROSS_DOMAIN === true) {
740757
$tmp = $response = null;
741758

759+
header('Content-Type: application/javascript');
760+
742761
if (strpos($mime, 'image/svg') !== 0 && strpos($mime, 'image/') === 0) {
743-
echo $param_callback, '("data:', $mime, ';base64,',
762+
echo JSONP_CALLBACK, '("data:', $mime, ';base64,',
744763
base64_encode(
745764
file_get_contents($locationFile)
746765
),
747766
'");';
748767
} else {
749-
echo $param_callback, '("data:', $mime, ',',
768+
echo JSONP_CALLBACK, '("data:', $mime, ',',
750769
asciiToInline(file_get_contents($locationFile)),
751770
'");';
752771
}
753772
} else {
754773
$tmp = $response = null;
755774

775+
header('Content-Type: application/javascript');
776+
756777
$dir_name = dirname($_SERVER['SCRIPT_NAME']);
757778

758779
if ($dir_name === '\/' || $dir_name === '\\') {
759780
$dir_name = '';
760781
}
761782

762-
echo $param_callback, '(',
783+
echo JSONP_CALLBACK, '(',
763784
JsonEncodeString(
764785
($http_port === 443 ? 'https://' : 'http://') .
765786
preg_replace('#:[0-9]+$#', '', $_SERVER['HTTP_HOST']) .
@@ -786,9 +807,11 @@ function downloadSource($url, $toSource, $caller)
786807
//errors
787808
setHeaders(true);//no-cache
788809

810+
header('Content-Type: application/javascript');
811+
789812
removeOldFiles();
790813

791-
echo $param_callback, '(',
814+
echo JSONP_CALLBACK, '(',
792815
JsonEncodeString(
793816
'error: html2canvas-proxy-php: ' . $response['error']
794817
),

0 commit comments

Comments
 (0)