/**
 * 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;
}
Beispiel #2
0
/**
 * Callback to for proxy page
 */
function ac_templates_proxy()
{
    drupal_add_http_header('Content-Type', 'application/javascript');
    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 {
                $extension = str_replace(array('image/', 'text/', 'application/'), '', $response['mime']);
                $extension = str_replace(array('windows-bmp', 'ms-bmp'), 'bmp', $extension);
                $extension = str_replace(array('svg+xml', 'svg-xml'), 'svg', $extension);
                $extension = str_replace('xhtml+xml', 'xhtml', $extension);
                $extension = str_replace('jpeg', 'jpg', $extension);
                $locationFile = preg_replace('#[.][0-9_]+$#', '.' . $extension, $tmp['location']);
                if (file_exists($locationFile)) {
                    unlink($locationFile);
                }
                if (rename($tmp['location'], $locationFile)) {
                    //set cache
                    setHeaders(FALSE);
                    remove_old_files();
                    if (CROSS_DOMAIN === 1) {
                        $mime = JsonEncodeString($response['mime'], TRUE);
                        $mime = $response['mime'];
                        if ($response['encode'] !== NULL) {
                            $mime .= ';charset=' . JsonEncodeString($response['encode'], TRUE);
                        }
                        $tmp = $response = NULL;
                        if (strpos($mime, 'image/svg') !== 0 && strpos($mime, 'image/') === 0) {
                            echo $param_callback, '("data:', $mime, ';base64,', base64_encode(file_get_contents($locationFile)), '");';
                        } else {
                            echo $param_callback, '("data:', $mime, ',', asciiToInline(file_get_contents($locationFile)), '");';
                        }
                    } else {
                        $tmp = $response = NULL;
                        $dir_name = dirname($_SERVER['SCRIPT_NAME']);
                        if ($dir_name === '\\/' || $dir_name === '\\') {
                            $dir_name = '';
                        }
                        if (strpos($locationFile, 'public://') === FALSE) {
                            $parse_file_location = explode('/', $locationFile);
                            $locationFile = sprintf('%s/%s', PATH, end($parse_file_location));
                        }
                        echo $param_callback, '(', JsonEncodeString(file_create_url($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, '(', JsonEncodeString('error: html2canvas-proxy-php: ' . $response['error']), ');';
}