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