Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $app = include 'app.php';
- $url = parse_url($_POST['url']);
- $error = '';
- if ($url['scheme'] != 'http') {
- $error = 'Ссылка должна начинаться с "http://"';
- }
- else if (!$url['path'] || $url['path'] == '/') {
- $error = 'Не указан путь до файла';
- }
- else {
- $fh = fsockopen($url['host'], $url['port'] ? $url['port'] : 80, $errno, $error, 1);
- if ($fh) {
- fwrite($fh, "GET " . $url['path'] . ($url['query'] ? '?' . $url['query'] : '') . " HTTP/1.1\r\nHost: {$url['host']}\r\nConnection: close\r\n\r\n");
- $status = explode(' ', fgets($fh), 3);
- if ($status[1] != 200) {
- $error = $status[1] . ' ' . $status[2];
- }
- else {
- $headers = array();
- while (1) {
- $header = rtrim( fgets($fh) );
- // \r\n\r\n заголовки кончились
- if ($header == '') {
- break;
- }
- list($key, $val) = explode(': ', $header);
- $headers[ strtolower($key) ] = $val;
- }
- if ( !preg_match('#^image/(' . implode('|', $app['allowedImageTypes']) . ')$#', $headers['content-type']) ) {
- $error = $app['someErrors']['invalidFileType'];
- }
- else if ( $headers['content-length'] < $app['minImageSize'] ) {
- $error = $app['someErrors']['imageTooSmall'];
- }
- else if ( $headers['content-length'] > $app['maxImageSize'] ) {
- $error = $app['someErrors']['imageTooBig'];;
- }
- else {
- $data = '';
- while ( !feof($fh) ) {
- $data .= fread($fh, 8192);
- }
- echo( 'data:' . $headers['content-type'] . ';base64,' . base64_encode($data) );
- }
- }
- fclose($fh);
- }
- }
- if ($error) {
- echo("error:$error");
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment