]> git.donarmstrong.com Git - roundcube.git/blob - program/steps/utils/modcss.inc
Imported Upstream version 0.5.1
[roundcube.git] / program / steps / utils / modcss.inc
1 <?php
2
3 /*
4  +-----------------------------------------------------------------------+
5  | program/steps/utils/modcss.inc                                        |
6  |                                                                       |
7  | This file is part of the Roundcube Webmail client                     |
8  | Copyright (C) 2007-2011, Roundcube Dev. - Switzerland                 |
9  | Licensed under the GNU GPL                                            |
10  |                                                                       |
11  | PURPOSE:                                                              |
12  |   Modify CSS source from a URL                                        |
13  |                                                                       |
14  +-----------------------------------------------------------------------+
15  | Author: Thomas Bruederli <roundcube@gmail.com>                        |
16  +-----------------------------------------------------------------------+
17
18  $Id: modcss.inc 4509 2011-02-09 10:51:50Z thomasb $
19
20 */
21
22 $source = '';
23
24 $url = preg_replace('![^a-z0-9.-]!i', '', $_GET['_u']);
25 if ($url === null || !($realurl = $_SESSION['modcssurls'][$url])) {
26     header('HTTP/1.1 403 Forbidden');
27     echo "Unauthorized request";
28     exit;
29 }
30
31 $a_uri = parse_url($realurl);
32 $port  = $a_uri['port'] ? $a_uri['port'] : 80;
33 $host  = $a_uri['host'];
34 $path  = $a_uri['path'] . ($a_uri['query'] ? '?'.$a_uri['query'] : '');
35
36 // don't allow any other connections than http(s)
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))) {
45     header('HTTP/1.1 500 Internal Server Error');
46     echo $error;
47     exit;
48 }
49
50 // set timeout for socket
51 stream_set_timeout($fp, 30);
52
53 // send request
54 $out  = "GET $path HTTP/1.0\r\n";
55 $out .= "Host: $host\r\n";
56 $out .= "Connection: Close\r\n\r\n";
57 fwrite($fp, $out);
58
59 // read response
60 $header = true;
61 $headers = array();
62 while (!feof($fp)) {
63     $line = trim(fgets($fp, 4048));
64
65     if ($header) {
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 {
79         $source .= "$line\n";
80     }
81 }
82 fclose($fp);
83
84 // check content-type header and mod styles
85 $mimetype = strtolower($headers['content-type']);
86 if (!empty($source) && in_array($mimetype, array('text/css','text/plain'))) {
87     header('Content-Type: text/css');
88     echo rcmail_mod_css_styles($source, preg_replace('/[^a-z0-9]/i', '', $_GET['_c']));
89     exit;
90 }
91 else
92     $error = "Invalid response returned by server";
93
94 header('HTTP/1.0 404 Not Found');
95 echo $error;
96 exit;
97
98