]> git.donarmstrong.com Git - roundcube.git/blob - bin/modcss.php
Imported Upstream version 0.2.1
[roundcube.git] / bin / modcss.php
1 <?php
2
3 /*
4  +-----------------------------------------------------------------------+
5  | bin/modcss.php                                                        |
6  |                                                                       |
7  | This file is part of the RoundCube Webmail client                     |
8  | Copyright (C) 2007-2009, 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.php 2249 2009-01-22 11:23:00Z thomasb $
19
20 */
21
22 define('INSTALL_PATH', realpath(dirname(__FILE__) . '/..') . '/');
23 require INSTALL_PATH . 'program/include/iniset.php';
24
25 $RCMAIL = rcmail::get_instance();
26
27 $source = '';
28 $error  = 'Requires a valid user session and source url';
29
30 if (empty($RCMAIL->user->ID)) {
31     header('HTTP/1.1 403 Forbidden');
32     echo $error;
33     exit;
34 }
35
36 $url = preg_replace('/[^a-z0-9.-_\?\$&=%]/i', '', $_GET['u']);
37 if ($url === null) {
38     header('HTTP/1.1 403 Forbidden');
39     echo $error;
40     exit;
41 }
42
43 $a_uri = parse_url($url);
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
48 if (!($fp = fsockopen($host, $port, $errno, $errstr, 30))) {
49     header('HTTP/1.1 500 Internal Server Error');
50     echo $error;
51     exit;
52 }
53
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 $header = true;
60 while (!feof($fp)) {
61     $line = trim(fgets($fp, 4048));
62
63     if ($header
64         && preg_match('/^HTTP\/1\..\s+(\d+)/', $line, $regs)
65         && intval($regs[1]) != 200) {
66         break;
67     } else if (empty($line) && $header) {
68         $header = false;
69     } else if (!$header) {
70         $source .= "$line\n";
71     }
72 }
73 fclose($fp);
74
75 if (!empty($source)) {
76     header('Content-Type: text/css');
77     echo rcmail_mod_css_styles(
78         $source,
79         preg_replace('/[^a-z0-9]/i', '', $_GET['c']),
80         $url
81     );
82     exit;
83 }
84
85 header('HTTP/1.0 404 Not Found');
86 echo $error;
87 exit;