alecpl
2010-04-21 93c01888547210f54bd593d0774f9f63ec38f7a7
commit | author | age
97bd2c 1 <?php
T 2
3 /*
4  +-----------------------------------------------------------------------+
155bbb 5  | bin/modcss.php                                                        |
97bd2c 6  |                                                                       |
T 7  | This file is part of the RoundCube Webmail client                     |
cbbef3 8  | Copyright (C) 2007-2009, RoundCube Dev. - Switzerland                 |
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
b685e9 22 define('INSTALL_PATH', realpath(dirname(__FILE__) . '/..') . '/');
1608f4 23 require INSTALL_PATH . 'program/include/iniset.php';
T 24
25 $RCMAIL = rcmail::get_instance();
97bd2c 26
12bc8b 27 $source = '';
T 28 $error  = 'Requires a valid user session and source url';
97bd2c 29
12bc8b 30 if (empty($RCMAIL->user->ID)) {
T 31     header('HTTP/1.1 403 Forbidden');
32     echo $error;
33     exit;
97bd2c 34 }
T 35
db5221 36 $url = preg_replace('![^a-z0-9:./\-_?$&=%]!i', '', $_GET['u']);
12bc8b 37 if ($url === null) {
T 38     header('HTTP/1.1 403 Forbidden');
39     echo $error;
40     exit;
1608f4 41 }
97bd2c 42
12bc8b 43 $a_uri = parse_url($url);
T 44 $port  = $a_uri['port'] ? $a_uri['port'] : 80;
45 $host  = $a_uri['host'];
46 $path  = $a_uri['path'] . ($a_uri['query'] ? '?'.$a_uri['query'] : '');
47
db5221 48 // don't allow any other connections than http(s)
T 49 if (strtolower(substr($a_uri['scheme'], 0, 4)) != 'http') {
50     header('HTTP/1.1 403 Forbidden');
51     echo "Invalid URL";
52     exit;
53 }
54
55 // try to open socket connection
56 if (!($fp = fsockopen($host, $port, $errno, $error, 15))) {
12bc8b 57     header('HTTP/1.1 500 Internal Server Error');
T 58     echo $error;
59     exit;
60 }
61
db5221 62 // set timeout for socket
T 63 stream_set_timeout($fp, 30);
64
65 // send request
12bc8b 66 $out  = "GET $path HTTP/1.0\r\n";
T 67 $out .= "Host: $host\r\n";
68 $out .= "Connection: Close\r\n\r\n";
69 fwrite($fp, $out);
70
db5221 71 // read response
12bc8b 72 $header = true;
db5221 73 $headers = array();
12bc8b 74 while (!feof($fp)) {
T 75     $line = trim(fgets($fp, 4048));
76
db5221 77     if ($header) {
T 78         if (preg_match('/^HTTP\/1\..\s+(\d+)/', $line, $regs)
79             && intval($regs[1]) != 200) {
80             break;
81         }
82         else if (empty($line)) {
83             $header = false;
84         }
85         else {
86             list($key, $value) = explode(': ', $line);
87             $headers[strtolower($key)] = $value;
88         }
89     }
90     else {
12bc8b 91         $source .= "$line\n";
T 92     }
93 }
f9160e 94 fclose($fp);
12bc8b 95
db5221 96 // check content-type header and mod styles
T 97 $mimetype = strtolower($headers['content-type']);
98 if (!empty($source) && in_array($mimetype, array('text/css','text/plain'))) {
12bc8b 99     header('Content-Type: text/css');
db5221 100     echo rcmail_mod_css_styles($source, preg_replace('/[^a-z0-9]/i', '', $_GET['c']));
12bc8b 101     exit;
T 102 }
db5221 103 else
T 104     $error = "Invalid response returned by server";
12bc8b 105
T 106 header('HTTP/1.0 404 Not Found');
107 echo $error;
108 exit;