thomascube
2011-04-12 3d8b54edf74792e3996d861a6a30c41d82976261
commit | author | age
97bd2c 1 <?php
T 2
3 /*
4  +-----------------------------------------------------------------------+
677e1f 5  | program/steps/utils/modcss.inc                                        |
97bd2c 6  |                                                                       |
e019f2 7  | This file is part of the Roundcube Webmail client                     |
3e0e91 8  | Copyright (C) 2007-2011, The Roundcube Dev Team                       |
97bd2c 9  | Licensed under the GNU GPL                                            |
T 10  |                                                                       |
11  | PURPOSE:                                                              |
12  |   Modify CSS source from a URL                                        |
13  |                                                                       |
14  +-----------------------------------------------------------------------+
15  | Author: Thomas Bruederli <roundcube@gmail.com>                        |
16  +-----------------------------------------------------------------------+
17
155bbb 18  $Id$
97bd2c 19
T 20 */
21
12bc8b 22 $source = '';
97bd2c 23
3e0e91 24 $url = preg_replace('![^a-z0-9.-]!i', '', $_GET['_u']);
T 25 if ($url === null || !($realurl = $_SESSION['modcssurls'][$url])) {
12bc8b 26     header('HTTP/1.1 403 Forbidden');
3e0e91 27     echo "Unauthorized request";
12bc8b 28     exit;
1608f4 29 }
97bd2c 30
3e0e91 31 $a_uri = parse_url($realurl);
12bc8b 32 $port  = $a_uri['port'] ? $a_uri['port'] : 80;
T 33 $host  = $a_uri['host'];
34 $path  = $a_uri['path'] . ($a_uri['query'] ? '?'.$a_uri['query'] : '');
35
db5221 36 // don't allow any other connections than http(s)
T 37 if (strtolower(substr($a_uri['scheme'], 0, 4)) != 'http') {
38     header('HTTP/1.1 403 Forbidden');
39     echo "Invalid URL";
40     exit;
41 }
42
43 // try to open socket connection
44 if (!($fp = fsockopen($host, $port, $errno, $error, 15))) {
12bc8b 45     header('HTTP/1.1 500 Internal Server Error');
T 46     echo $error;
47     exit;
48 }
49
db5221 50 // set timeout for socket
T 51 stream_set_timeout($fp, 30);
52
53 // send request
12bc8b 54 $out  = "GET $path HTTP/1.0\r\n";
T 55 $out .= "Host: $host\r\n";
56 $out .= "Connection: Close\r\n\r\n";
57 fwrite($fp, $out);
58
db5221 59 // read response
12bc8b 60 $header = true;
db5221 61 $headers = array();
12bc8b 62 while (!feof($fp)) {
T 63     $line = trim(fgets($fp, 4048));
64
db5221 65     if ($header) {
T 66         if (preg_match('/^HTTP\/1\..\s+(\d+)/', $line, $regs)
67             && intval($regs[1]) != 200) {
68             break;
69         }
70         else if (empty($line)) {
71             $header = false;
72         }
73         else {
74             list($key, $value) = explode(': ', $line);
75             $headers[strtolower($key)] = $value;
76         }
77     }
78     else {
12bc8b 79         $source .= "$line\n";
T 80     }
81 }
f9160e 82 fclose($fp);
12bc8b 83
db5221 84 // check content-type header and mod styles
T 85 $mimetype = strtolower($headers['content-type']);
86 if (!empty($source) && in_array($mimetype, array('text/css','text/plain'))) {
12bc8b 87     header('Content-Type: text/css');
3e0e91 88     echo rcmail_mod_css_styles($source, preg_replace('/[^a-z0-9]/i', '', $_GET['_c']));
12bc8b 89     exit;
T 90 }
db5221 91 else
T 92     $error = "Invalid response returned by server";
12bc8b 93
T 94 header('HTTP/1.0 404 Not Found');
95 echo $error;
96 exit;
677e1f 97
b25dfd 98