-
Notifications
You must be signed in to change notification settings - Fork 1
/
restylegc.php
executable file
·202 lines (187 loc) · 8.34 KB
/
restylegc.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<?php
/***********************************************i********************************
* FILE: restylegc.php
*
* DESCRIPTION:
* This script is an intermediary between an iframe and Google Calendar that
* allows you to override the default style.
*
* USAGE:
* <iframe src="restylegc.php?src=user%40domain.tld"></iframe>
*
* where [email protected] is a valid Google Calendar account.
*
* VALID QUERY STRING PARAMETERS:
* title: any valid url encoded string
* if not present, takes title from first src
* showTitle: 0 or 1 (default)
* showNav: 0 or 1 (default)
* showDate: 0 or 1 (default)
* showTabs: 0 or 1 (default)
* showCalendars: 0 or 1 (default)
* mode: WEEK, MONTH (default), AGENDA
* height: a positive integer (should be same height as iframe)
* wkst: 1 (Sun; default), 2 (Mon), or 7 (Sat)
* hl: en, zh_TW, zh_CN, da, nl, en_GB, fi, fr, de, it, ja, ko,
* no, pl, pt_BR, ru, es, sv, tr
* if not present, takes language from first src
* bgcolor: url encoded hex color value, #FFFFFF (default)
* src: url encoded Google Calendar account (required)
* color: url encoded hex color value
* must immediately follow src
*
* The query string can contain multiple src/color pairs. It's recommended
* to have these pairs of query string parameters at the end of the query
* string.
*
* HISTORY:
* 03 December 2008 - Original release
* Uses technique from MyGoogleCal2 for all browsers,
* rather than giving IE special treatment.
* 16 December 2008 - Modified restylegc-js.php so that the regex does a
* general match rather than specifically look for the
* variable 'Ac'.
* Mar--Apr 2009 - Added jQuery for modifying the style after page load
* 23 June 2009 - Replaced jQuery with Dojo since jQuery, Prototype, and
* MooTools are not compatible
* 03 July 2009 - Fixed bug to remove width style from bubble
* 05 July 2009 - Rebranded to RESTYLEgc
* 16 August 2009 - Updated regex in restylegc-js.php
* 19 December 2009 - Removed MyGoogleCal references
* Updated Dojo version
* Archived additional .js and .css files
*
*
* ACKNOWLEDGMENTS:
* Michael McCall (http://www.castlemccall.com/) for pointing out "htmlembed"
* Mike (http://mikahn.com/) for the link to the online CSS formatter
* TechTriad.com (http://techtriad.com/) for requesting and funding the
* Javascript code to edit CSS properties and for selflessly letting the
* code be published for everyone's use and benefit.
*
*
* MIT LICENSE:
* Copyright (c) 2009 Brian Gibson (http://www.restylegc.com/)
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
******************************************************************************/
/* URL for overriding stylesheet
* The best way to create this stylesheet is to
* 1) Load "http://www.google.com/calendar/embed?src=user%40domain.tld" in a
* browser,
* 2) View the source (e.g., View->Page Source in Firefox),
* 3) Copy the relative URL of the stylesheet (i.e., the href value of the
* <link> tag),
* 4) Load the stylesheet in the browser by pasting the stylesheet URL into
* the address bar so that it reads similar to:
* "http://www.google.com/calendar/d003e2eff7c42eebf779ecbd527f1fe0embedcompiled.css"
* 5) Save the stylesheet (e.g., File->Save Page As in Firefox)
* Edit this new file to change the style of the calendar.
*
* As an alternative method, take the URL you copied in Step 3, and paste it
* in the URL field at http://mabblog.com/cssoptimizer/uncompress.html.
* That site will automatically format the CSS so that it's easier to edit.
*/
$stylesheet = 'restylegc.css';
/*
* Set domain to your google apps domain to use a google apps hosted calendar
* ex: $domain = 'mitesdesign.com';
*/
$domain = false;
/*******************************************************************************
* DO NOT EDIT BELOW UNLESS YOU KNOW WHAT YOU'RE DOING
******************************************************************************/
// Check if a cached version already exists
// Caching is simple and will not support multiple calendars
// yet... if I run into the need I'll add it
// anyone else is welcome to fork this and make it more intelligent
$cachefile = 'cache/'.basename($_SERVER['SCRIPT_NAME']);
$cachetime = 120 * 60; // 2 hours
if (file_exists($cachefile) && (time() - $cachetime < filemtime($cachefile))) {
include($cachefile);
echo "<!-- Cached ".date('jS F Y H:i', filemtime($cachefile))." -->";
exit;
}
// URL for the calendar
$url = "";
if(count($_GET) > 0) {
if ($domain) {
$url = "http://www.google.com/calendar/hosted/$domain/embed?" . $_SERVER['QUERY_STRING'];
} else {
$url = "http://www.google.com/calendar/embed?" . $_SERVER['QUERY_STRING'];
}
} else {
exit('No Calendar variables');
}
// Request the calendar
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// Added based on suggestions from troubleshooting "Moved Temporarily" page
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // follow redirects
curl_setopt($ch, CURLOPT_MAXREDIRS, 10); // maximum number of redirects
$buffer = curl_exec($ch);
// Added based on suggestions from troubleshooting "Moved Temporarily" page
$n = 0; //failsafe so this thing doesn't loop to infinity
while ((strpos($buffer, 'Moved Temporarily') !== false) && ($n < 10)) {
$n++;
//re-execute the cURL request
$buffer = curl_exec($ch);
}
curl_close($ch);
// Point stylesheet and javascript to custom versions
$pattern = '/(<link.*>)/';
$replacement = '<link rel="stylesheet" type="text/css" href="' . $stylesheet . '" />';
$buffer = preg_replace($pattern, $replacement, $buffer);
$pattern = '/src="(.*js)"/';
$replacement = 'src="restylegc-js.php?$1"';
$buffer = preg_replace($pattern, $replacement, $buffer);
// Add a hook to the window onload function
$pattern = '/}\);}<\/script>/';
$replacement = '}); restylegc();}</script>';
$buffer = preg_replace($pattern, $replacement, $buffer);
// Use DHTML to modify the DOM after the calendar loads
$pattern = '/(<\/head>)/';
$replacement = <<<RGC
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
function restylegc() {
// remove inline style from body so background-color can be set using the stylesheet
$('body').removeAttr('style');
// iterate over each bubble and remove the width property from the style attribute
// so that the width can be set using the stylesheet
$('.bubble').each(function() {
$(this).css('width', '');
});
}
</script>
</head>
RGC;
$buffer = preg_replace($pattern, $replacement, $buffer);
$fp = fopen($cachefile, 'w'); // open the cache file for writing
fwrite($fp, $buffer); // save the contents of output buffer to the file
fclose($fp); // close the file
// display the calendar
echo $buffer;
?>