Skip to content
This repository
branch: master
Guilherme Nascimento
file 604 lines (517 sloc) 20.451 kb
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
<?php
/*
html2canvas-proxy-php 0.1.8
Copyright (c) 2014 Guilherme Nascimento (brcontainer@yahoo.com.br)

Released under the MIT license
*/

error_reporting(0);//Turn off errors because the script already own uses "error_get_last"

//constants
define('EOL', chr(10));
define('WOL', chr(13));
define('GMDATECACHE', gmdate('D, d M Y H:i:s'));

//setup
define('JSLOG', 'console.log'); //Configure alternative function log, eg. console.log, alert, custom_function
define('PATH', 'images');//relative folder where the images are saved
define('CCACHE', 60 * 5 * 1000);//Limit access-control and cache, define 0/false/null/-1 to not use "http header cache"
define('TIMEOUT', 30);//Timeout from load Socket
define('MAX_LOOP', 10);//Configure loop limit for redirect (location header)

/*
If execution has reached the time limit prevents page goes blank (off errors)
or generate an error in PHP, which does not work with the DEBUG (from html2canvas.js)
*/
$maxExec = (int) ini_get('max_execution_time');
define('MAX_EXEC', $maxExec < 1 ? 0 : ($maxExec - 5));//reduces 5 seconds to ensure the execution of the DEBUG

if(isset($_SERVER['REQUEST_TIME']) && strlen($_SERVER['REQUEST_TIME']) > 0) {
    $initExec = (int) $_SERVER['REQUEST_TIME'];
} else {
    $initExec = time();
}

define('INIT_EXEC', $initExec);
define('SECPREFIX', 'h2c_');

$http_port = 0;

//set mime-type
header('Content-Type: application/javascript');

$param_callback = JSLOG;//force use alternative log error
$tmp = null;//tmp var usage
$response = array();

/**
* Detect SSL stream transport
* @return boolean|string If returns string has an problem, returns true if ok
*/
function supportSSL() {
    if(defined('SOCKET_SSL_STREAM')) {
        return true;
    }

    if(function_exists('stream_get_transports')) {
        /* PHP 5 */
        $ok = in_array('ssl', stream_get_transports());
        if($ok) {
            defined('SOCKET_SSL_STREAM', '1');
            return true;
        }
    } else {
        /* PHP 4 */
        ob_start();
        phpinfo(1);

        $info = strtolower(ob_get_clean());

        if(preg_match('/socket\stransports/', $info) !== 0) {
            if(preg_match('/(ssl[,]|ssl [,]|[,] ssl|[,]ssl)/', $info) !== 0) {
                defined('SOCKET_SSL_STREAM', '1');
                return true;
            } else {
                return 'No SSL stream support detected';
            }
        }
    }

    return 'Don\'t detected streams (finder error), no SSL stream support';
}

/**
* set headers in document
* @return void return always void
*/
function remove_old_files() {
    $p = PATH . '/';

    if(
        (MAX_EXEC === 0 || (time() - INIT_EXEC) < MAX_EXEC) && //prevents this function locks the process that was completed
        (file_exists($p) || is_dir($p))
    ) {
        $h = opendir($p);
        if(false !== $h) {
            while(false !== ($f = readdir($h))) {
                if(
                    is_file($p . $f) && is_dir($p . $f) === false &&
                    strpos($f, SECPREFIX) !== false &&
                    (INIT_EXEC - filectime($p . $f)) > (CCACHE * 2)
                ) {
                    unlink($p . $f);
                }
            }
        }
    }
}

/**
* this function does not exist by default in php4.3, get detailed error in php5
* @return array if has errors
*/
function get_error() {
    if(function_exists('error_get_last') === false) {
        return error_get_last();
    }
    return null;
}

/**
* enconde string in "json" (only strings), json_encode (native in php) don't support for php4
* @param string $s to encode
* @return string always return string
*/
function json_encode_string($s) {
    $vetor = array();
    $vetor[0] = '\\0';
    $vetor[8] = '\\b';
    $vetor[9] = '\\t';
    $vetor[10] = '\\n';
    $vetor[12] = '\\f';
    $vetor[13] = '\\r';
    $vetor[34] = '\\"';
    $vetor[47] = '\\/';
    $vetor[92] = '\\\\';

    $tmp = '';
    $enc = '';
    $j = strlen($s);

    for($i = 0; $i < $j; ++$i) {
        $tmp = substr($s, $i, 1);
        $c = ord($tmp);
        if($c > 126) {
            $d = '000' . dechex($c);
            $tmp = '\\u' . substr($d, strlen($d) - 4);
        } else {
            if(isset($vetor[$c])) {
                $tmp = $vetor[$c];
            } else if(($c > 31) === false) {
                $d = '000' . dechex($c);
                $tmp = '\\u' . substr($d, strlen($d) - 4);
            }
        }

        $enc .= $tmp;
    }

    return '"' . $enc . '"';
}

/**
* set headers in document
* @param boolean $nocache If false set cache (if CCACHE > 0), If true set no-cache in document
* @return void return always void
*/
function setHeaders($nocache) {
    if($nocache === false && is_int(CCACHE) && CCACHE > 0) {
        //save to browser cache
        header('Last-Modified: ' . GMDATECACHE . ' GMT');
        header('Cache-Control: max-age=' . (CCACHE - 1));
        header('Pragma: max-age=' . (CCACHE - 1));
        header('Expires: ' . gmdate('D, d M Y H:i:s', INIT_EXEC + CCACHE - 1));
        header('Access-Control-Max-Age:' . CCACHE);
    } else {
        //no-cache
        header('Pragma: no-cache');
        header('Cache-Control: no-cache');
        header('Expires: '. GMDATECACHE .' GMT');
    }

    //set access-control
    header('Access-Control-Allow-Origin: *');
    header('Access-Control-Request-Method: *');
    header('Access-Control-Allow-Methods: OPTIONS, GET');
    header('Access-Control-Allow-Headers: *');
}

/**
* Converte relative-url to absolute-url
* @param string $u set base url
* @param string $m set relative url
* @return string return always string, if have an error, return blank string (scheme invalid)
*/
function relative2absolute($u, $m) {
    if(strpos($m, '//') === 0) {//http link //site.com/test
        return 'http:' . $m;
    }

    if(preg_match('#^[a-zA-Z0-9]+[:]#', $m) !== 0) {
        $pu = parse_url($m);

        if(preg_match('/^(http|https)$/i', $pu['scheme']) === 0) {
            return '';
        }

        $m = '';
        if(isset($pu['path'])) {
            $m .= $pu['path'];
        }

        if(isset($pu['query'])) {
            $m .= '?' . $pu['query'];
        }

        if(isset($pu['fragment'])) {
            $m .= '#' . $pu['fragment'];
        }

        return relative2absolute($pu['scheme'] . '://' . $pu['host'], $m);
    }

    if(preg_match('/^[?#]/', $m) !== 0) {
        return $u . $m;
    }

    $pu = parse_url($u);
    $pu['path'] = isset($pu['path']) ? preg_replace('#/[^/]*$#', '', $pu['path']) : '';

    $pm = parse_url('http://1/' . $m);
    $pm['path'] = isset($pm['path']) ? $pm['path'] : '';

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

    if(strpos($m, '/') === 0) {
        $pu['path'] = '';
    }

    $b = $pu['path'] . '/' . $pm['path'];
    $b = str_replace('\\', '/', $b);//Confuso ???

    $ab = explode('/', $b);
    $j = count($ab);

    $ab = array_filter($ab, 'strlen');
    $nw = array();

    for($i = 0; $i < $j; ++$i) {
        if(isset($ab[$i]) === false || $ab[$i] === '.') {
            continue;
        }
        if($ab[$i] === '..') {
            array_pop($nw);
        } else {
            $nw[] = $ab[$i];
        }
    }

    $m = $pu['scheme'] . '://' . $pu['host'] . '/' . implode('/', $nw) . ($isPath === true ? '/' : '');

    if(isset($pm['query'])) {
        $m .= '?' . $pm['query'];
    }

    if(isset($pm['fragment'])) {
        $m .= '#' . $pm['fragment'];
    }

    $nw = null;
    $ab = null;
    $pm = null;
    $pu = null;

    return $m;
}

/**
* validate url
* @param string $u set base url
* @return boolean return always boolean
*/
function isHttpUrl($u) {
    return preg_match('#^http(|s)[:][/][/][a-z0-9]#i', $u) !== 0;
}

/**
* create folder for images download
* @return boolean return always boolean
*/
function createFolder() {
    if(file_exists(PATH) === false || is_dir(PATH) === false) {
        return mkdir(PATH, 0755);
    }
    return true;
}

/**
* create temp file which will receive the download
* @param string $basename set url
* @param boolean $isEncode If true uses the "first" temporary name
* @return boolean|array If you can not create file return false, If create file return array
*/
function createTmpFile($basename, $isEncode) {
    $folder = preg_replace('#[/]$#', '', PATH) . '/';
    if($isEncode === false) {
        $basename = SECPREFIX . sha1($basename);
    }

    //$basename .= $basename;
    $tmpMime = '.' . mt_rand(0, 1000) . '_';
    if($isEncode === true) {
        $tmpMime .= isset($_SERVER['REQUEST_TIME']) && strlen($_SERVER['REQUEST_TIME']) > 0 ? $_SERVER['REQUEST_TIME'] : (string) time();
    } else {
        $tmpMime .= (string) INIT_EXEC;
    }

    if(file_exists($folder . $basename . $tmpMime)) {
        return createTmpFile($basename, true);
    }

    $source = fopen($folder . $basename . $tmpMime, 'w');
    if($source !== false) {
        return array(
            'location' => $folder . $basename . $tmpMime,
            'source' => $source
        );
    }
    return false;
}

/**
* download http request recursive (If found HTTP 3xx)
* @param string $url to download
* @param resource $toSource to download
* @return array retuns array
*/
function downloadSource($url, $toSource, $caller) {
    $errno = 0;
    $errstr = '';

    ++$caller;

    if($caller > MAX_LOOP) {
        return array('error' => 'Limit of ' . MAX_LOOP . ' redirects was exceeded, maybe there is a problem: ' . $url);
    }

    $uri = parse_url($url);
    $secure = strcasecmp($uri['scheme'], 'https') === 0;

    if($secure) {
        $response = supportSSL();
        if($response !== true) {
            return array('error' => $response);
        }
    }

    $port = isset($uri['port']) && strlen($uri['port']) > 0 ? (int) $uri['port'] : ($secure === true ? 443 : 80);
    $host = ($secure ? 'ssl://' : '') . $uri['host'];

    $fp = fsockopen($host, $port, $errno, $errstr, TIMEOUT);
    if($fp === false) {
        return array('error' => 'SOCKET: ' . $errstr . '(' . ((string) $errno) . ')');
    } else {
        fwrite(
            $fp, 'GET ' . (
                isset($uri['path']) && strlen($uri['path']) > 0 ? $uri['path'] : '/'
            ) . (
                isset($uri['query']) && strlen($uri['query']) > 0 ? ('?' . $uri['query']) : ''
            ) . ' HTTP/1.0' . WOL . EOL
        );

        if(isset($_SERVER['HTTP_ACCEPT']) && strlen($_SERVER['HTTP_ACCEPT']) > 0) {
            fwrite($fp, 'Accept: ' . $_SERVER['HTTP_ACCEPT'] . WOL . EOL);
        }
        if(isset($_SERVER['HTTP_USER_AGENT']) && strlen($_SERVER['HTTP_USER_AGENT']) > 0) {
            fwrite($fp, 'User-Agent: ' . $_SERVER['HTTP_USER_AGENT'] . WOL . EOL);
        }

        if(isset($_SERVER['HTTP_REFERER']) && strlen($_SERVER['HTTP_REFERER']) > 0) {
            fwrite($fp, 'Referer: ' . $_SERVER['HTTP_REFERER'] . WOL . EOL);
        }

        fwrite($fp, 'Host: ' . $uri['host'] . WOL . EOL);
        fwrite($fp, 'Connection: close' . WOL . EOL . WOL . EOL);

        $isRedirect = true;
        $isBody = false;
        $isHttp = false;
        $mime = null;
        $data = '';

        while(false === feof($fp)) {
            if(MAX_EXEC !== 0 && (time() - INIT_EXEC) >= MAX_EXEC) {
                return array('error' => 'Maximum execution time of ' . ((string) (MAX_EXEC + 5)) . ' seconds exceeded, configure this with ini_set/set_time_limit or "php.ini" (if safe_mode is enabled)');
            }

            $data = fgets($fp);

            if($data === false) { continue; }
            if($isHttp === false) {
                if(preg_match('#^HTTP[/]1[.]#i', $data) === 0) {
                    fclose($fp);//Close connection
                    $data = '';
                    return array('error' => 'This request did not return a HTTP response valid');
                }

                $tmp = preg_replace('#(HTTP/1[.]\\d |[^0-9])#i', '',
                    preg_replace('#^(HTTP/1[.]\\d \\d{3}) [\\w\\W]+$#i', '$1', $data)
                );

                if($tmp === '304') {
                    fclose($fp);//Close connection
                    $data = '';
                    return array('error' => 'Request returned HTTP_304, this status code is incorrect because the html2canvas not send Etag');
                } else {
                    $isRedirect = preg_match('#^(301|302|303|307|308)$#', $tmp) !== 0;
                    if($isRedirect === false && $tmp !== '200') {
                        fclose($fp);
                        $data = '';
                        return array('error' => 'Request returned HTTP_' . $tmp);
                    }
                    $isHttp = true;
                    continue;
                }
            }
            if($isBody === false) {
                if(preg_match('#^location[:]#i', $data) !== 0) {//200 force 302
                    fclose($fp);//Close connection
                    
                    $data = trim(preg_replace('#^location[:]#i', '', $data));
                    if($data === '') {
                        return array('error' => '"Location:" header is blank');
                    }

                    $nextUri = $data;
                    $data = relative2absolute($url, $data);

                    if($data === '') {
                        return array('error' => 'Invalid scheme in url (' . $nextUri . ')');
                    }
                    
                    if(isHttpUrl($data) === false) {
                        return array('error' => '"Location:" header redirected for a non-http url (' . $data . ')');
                    }
                    return downloadSource($data, $toSource, $caller);
                } else if(preg_match('#^content[-]length[:]( 0|0)$#i', $data) !== 0) {
                    fclose($fp);
                    $data = '';
                    return array('error' => 'source is blank (Content-length: 0)');
                } else if(preg_match('#^content[-]type[:]#i', $data) !== 0) {
                    $mime = trim(
                        preg_replace('/[;]([\\s\\S]|)+$/', '',
                            str_replace('content-type:', '',
                                str_replace('/x-', '/', strtolower($data))
                            )
                        )
                    );

                    if(in_array($mime, array(
                        'image/bmp', 'image/windows-bmp', 'image/ms-bmp',
                        'image/jpeg', 'image/jpg', 'image/png', 'image/gif',
                        'text/html', 'application/xhtml', 'application/xhtml+xml'
                    )) === false) {
                        fclose($fp);
                        $data = '';
                        return array('error' => $mime . ' mimetype is invalid');
                    }
                } else if($isBody === false && trim($data) === '') {
                    $isBody = true;
                    continue;
                }
            } else if($isRedirect === true) {
                fclose($fp);
                $data = '';
                return array('error' => 'The response should be a redirect "' . $url . '", but did not inform which header "Localtion:"');
            } else if($mime === null) {
                fclose($fp);
                $data = '';
                return array('error' => 'Not set the mimetype from "' . $url . '"');
            } else {
                fwrite($toSource, $data);
                continue;
            }
        }

        fclose($fp);
        $data = '';
        if($isBody === false) {
            return array('error' => 'Content body is empty');
        } else if($mime === null) {
            return array('error' => 'Not set the mimetype from "' . $url . '"');
        }
        return array(
            'mime' => $mime
        );
    }
}

if(isset($_GET['callback']) && strlen($_GET['callback']) > 0) {
    $param_callback = $_GET['callback'];
}

if(isset($_SERVER['HTTP_HOST']) === false || strlen($_SERVER['HTTP_HOST']) === 0) {
    $response = array('error' => 'The client did not send the Host header');
} else if(isset($_SERVER['SERVER_PORT']) === false) {
    $response = array('error' => 'The Server-proxy did not send the PORT (configure PHP)');
} else if(MAX_EXEC < 10) {
    $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');
} else if(MAX_EXEC <= TIMEOUT) {
    $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 . ');"');
} else if(isset($_GET['url']) === false || strlen($_GET['url']) === 0) {
    $response = array('error' => 'No such parameter "url"');
} else if(isHttpUrl($_GET['url']) === false) {
    $response = array('error' => 'Only http scheme and https scheme are allowed');
} else if(preg_match('#[^A-Za-z0-9_[.]\\[\\]]#', $param_callback) !== 0) {
    $response = array('error' => 'Parameter "callback" contains invalid characters');
    $param_callback = JSLOG;
} else if(createFolder() === false) {
    $err = get_error();
    $response = array('error' => 'Can not create directory'. (
        $err !== null && isset($err['message']) && strlen($err['message']) > 0 ? (': ' . $err['message']) : ''
    ));
    $err = null;
} else {
    $http_port = (int) $_SERVER['SERVER_PORT'];

    $tmp = createTmpFile($_GET['url'], false);
    if($tmp === false) {
        $err = get_error();
        $response = array('error' => 'Can not create file'. (
            $err !== null && isset($err['message']) && strlen($err['message']) > 0 ? (': ' . $err['message']) : ''
        ));
        $err = null;
    } else {
        $response = downloadSource($_GET['url'], $tmp['source'], 0);
        fclose($tmp['source']);
    }
}

if(is_array($response) && isset($response['mime']) && strlen($response['mime']) > 0) {
    clearstatcache();
    if(false === file_exists($tmp['location'])) {
        $response = array('error' => 'Request was downloaded, but file can not be found, try again');
    } else if(filesize($tmp['location']) < 1) {
        $response = array('error' => 'Request was downloaded, but there was some problem and now the file is empty, try again');
    } else {
        $response['mime'] = str_replace(array('windows-bmp', 'ms-bmp'), 'bmp', //mimetype bitmap to bmp extension
            str_replace('jpeg', 'jpg', //jpeg to jpg extesion
                str_replace('xhtml+xml', 'xhtml',//fix mime to xhtml
                    str_replace(array('image/', 'text/', 'application/'), '',
                        $response['mime']
                    )
                )
            )
        );

        $locationFile = preg_replace('#[.][0-9_]+$#', '.' . $response['mime'], $tmp['location']);
        if(file_exists($locationFile)) {
            unlink($locationFile);
        }

        if(rename($tmp['location'], $locationFile)) {
            //success
            $tmp = $response = null;

            //set cache
            setHeaders(false);

            remove_old_files();

            echo $param_callback, '(',
                json_encode_string(
                    ($http_port === 443 ? 'https://' : 'http://') .
                    preg_replace('#:[0-9]+$#', '', $_SERVER['HTTP_HOST']) .
                    ($http_port === 80 || $http_port === 443 ? '' : (
                        ':' . $_SERVER['SERVER_PORT']
                    )) .
                    dirname($_SERVER['SCRIPT_NAME']). '/' .
                    $locationFile
                ),
            ');';
            exit;
        } else {
            $response = array('error' => 'Failed to rename the temporary file');
        }
    }
}

if(is_array($tmp) && isset($tmp['location']) && file_exists($tmp['location'])) {
    //remove temporary file if an error occurred
    unlink($tmp['location']);
}

//errors
setHeaders(true);//no-cache

remove_old_files();

echo $param_callback, '(',
    json_encode_string(
        'error: html2canvas-proxy-php: ' . $response['error']
    ),
');';
Something went wrong with that request. Please try again.