Skip to content

Commit d13925a

Browse files
committed
Fixed bugs and code review
1 parent 9352722 commit d13925a

File tree

1 file changed

+24
-21
lines changed

1 file changed

+24
-21
lines changed

html2canvasproxy.php

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/*
3-
* html2canvas-php-proxy 1.1.0
3+
* html2canvas-php-proxy 1.1.1
44
*
55
* Copyright (c) 2018 Guilherme Nascimento (brcontainer@yahoo.com.br)
66
*
@@ -19,8 +19,9 @@
1919
define('H2CP_DATAURI', false); // Enable use of "data URI scheme"
2020
define('H2CP_PREFER_CURL', true); // Enable curl if avaliable or disable
2121
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
2425

2526
/*
2627
* Set false for disable SSL check
@@ -130,13 +131,13 @@ function checkContentType($content)
130131
$content = strtolower($content);
131132
$encode = null;
132133

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);
135136
$encode = empty($encode[1]) ? null : trim($encode[1]);
136137
}
137138

138139
$mime = trim(
139-
preg_replace('/[;]([\\s\\S]|)+$/', '',
140+
preg_replace('#[;](.*)?$#', '',
140141
str_replace('content-type:', '',
141142
str_replace('/x-', '/', $content)
142143
)
@@ -253,7 +254,7 @@ function relativeToAbsolute($url, $relative)
253254
if (preg_match('#^[a-z0-9]+[:]#i', $relative) !== 0) {
254255
$pu = parse_url($relative);
255256

256-
if (preg_match('#^(http|https)$#i', $pu['scheme']) === 0) {
257+
if (preg_match('#^https?$#i', $pu['scheme']) === 0) {
257258
return '';
258259
}
259260

@@ -284,7 +285,7 @@ function relativeToAbsolute($url, $relative)
284285
$pm = parse_url('http://1/' . $relative);
285286
$pm['path'] = isset($pm['path']) ? $pm['path'] : '';
286287

287-
$isPath = $pm['path'] !== '' && strpos(strrev($pm['path']), '/') === 0 ? true : false;
288+
$isPath = $pm['path'] !== '' && strpos(strrev($pm['path']), '/') === 0;
288289

289290
if (strpos($relative, '/') === 0) {
290291
$pu['path'] = '';
@@ -336,7 +337,7 @@ function relativeToAbsolute($url, $relative)
336337
*/
337338
function isHttpUrl($url)
338339
{
339-
return preg_match('#^http(|s)[:][/][/][a-z0-9]#i', $url) === 1;
340+
return preg_match('#^https?[:]//.#i', $url) === 1;
340341
}
341342

342343
/**
@@ -352,7 +353,7 @@ function isAllowedUrl($url, &$message) {
352353
if (in_array('*', $domains) === false) {
353354
$ok = false;
354355

355-
foreach (H2CP_ALLOWED_DOMAINS as $domain) {
356+
foreach ($domains as $domain) {
356357
if ($domain === $uri['host']) {
357358
$ok = true;
358359
break;
@@ -413,7 +414,7 @@ function createFolder()
413414
*/
414415
function createTmpFile($basename, $isEncode)
415416
{
416-
$folder = preg_replace('#[/]$#', '', H2CP_PATH) . '/';
417+
$folder = preg_replace('#/$#', '', H2CP_PATH) . '/';
417418

418419
if ($isEncode === false) {
419420
$basename = H2CP_SECPREFIX . strlen($basename) . '.' . sha1($basename);
@@ -426,7 +427,7 @@ function createTmpFile($basename, $isEncode)
426427
return createTmpFile($basename, true);
427428
}
428429

429-
$source = fopen($folder . $basename . $tmpMime, 'w');
430+
$source = fopen($folder . $basename . $tmpMime, 'wb');
430431

431432
if ($source !== false) {
432433
return array(
@@ -450,14 +451,14 @@ function curlDownloadSource($url, $toSource)
450451

451452
//Reformat url
452453
$currentUrl = (empty($uri['scheme']) ? 'http': $uri['scheme']) . '://';
453-
$currentUrl .= empty($uri['host']) ? '': $uri['host'];
454+
$currentUrl .= empty($uri['host']) ? '': $uri['host'];
454455

455456
if (isset($uri['port'])) {
456457
$currentUrl .= ':' . $uri['port'];
457458
}
458459

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']);
461462

462463
$ch = curl_init();
463464

@@ -618,7 +619,7 @@ function downloadSource($url, $toSource, $caller)
618619
}
619620

620621
if ($isHttp === false) {
621-
if (preg_match('#^HTTP[/]1[.]#i', $data) === 0) {
622+
if (preg_match('#^HTTP/1\.#i', $data) === 0) {
622623
fclose($fp);//Close connection
623624
$data = '';
624625
return array('error' => 'This request did not return a HTTP response valid');
@@ -633,7 +634,7 @@ function downloadSource($url, $toSource, $caller)
633634
$data = '';
634635
return array('error' => 'Request returned HTTP_304, this status code is incorrect because the html2canvas not send Etag');
635636
} else {
636-
$isRedirect = preg_match('#^(301|302|303|307|308)$#', $tmp) !== 0;
637+
$isRedirect = preg_match('#^3\\d{2}$#', $tmp) !== 0;
637638

638639
if ($isRedirect === false && $tmp !== '200') {
639640
fclose($fp);
@@ -669,11 +670,11 @@ function downloadSource($url, $toSource, $caller)
669670
}
670671

671672
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) {
673674
fclose($fp);
674675
$data = '';
675676
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) {
677678
$response = checkContentType($data);
678679

679680
if (isset($response['error'])) {
@@ -826,7 +827,7 @@ function downloadSource($url, $toSource, $caller)
826827
echo H2CP_JSONP, '(',
827828
JsonEncodeString(
828829
($http_port === 443 ? 'https://' : 'http://') .
829-
preg_replace('#:[0-9]+$#', '', $_SERVER['HTTP_HOST']) .
830+
preg_replace('#[:]\\d+$#', '', $_SERVER['HTTP_HOST']) .
830831
($http_port === 80 || $http_port === 443 ? '' : (
831832
':' . $_SERVER['SERVER_PORT']
832833
)) .
@@ -853,7 +854,9 @@ function downloadSource($url, $toSource, $caller)
853854

854855
removeOldFiles();
855856

856-
echo H2CP_JSONP, '(',
857+
$callback = H2CP_JSONP !== false ? H2CP_ALTERNATIVE;
858+
859+
echo $callback, '(',
857860
JsonEncodeString(
858861
'error: html2canvas-proxy-php: ' . $response['error']
859862
),

0 commit comments

Comments
 (0)