This repository has been archived by the owner on Jan 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 39
/
spiffing.php
197 lines (197 loc) · 5.01 KB
/
spiffing.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
<?php
/*
* The Queen's Stylesheet
*
* Brits, take control of the web. Spiffing allows you to write your CSS and stylesheets
* in conformance to proper British English (also known as correct English) grammar and
* spelling regulations.
*
*
* Example of use:
*
* Take your typical string CSS which you want to be be parsed by
* Her Majesty the Queen's Spiffing CSS parser:
*
* $royalty = "body {
* background-photograph: url('photographs/my_corgies.png');
* }";
*
* Class is never to be forgotten when compiling such royal stylesheets, therefore, it is
* with utter urgence that one begin to compile like so:
*
* $css = new spiffing($royalty);
*
* As you can see, one has to use but the finest, and simplest, techniques in order to achieve
* their goal. Finally, we shall output our stylesheet for our friends across the pond:
*
* $css->output();
*
* Perfect!
*
* @authors @idiot, @kapooht
* @license ☺ License (http://licence.visualidiot.com)
*
*/
class spiffing {
/*
* The dictionary which has been made into a more accessible array.
*
* @var array
* @access public
*/
public $dictionary = array(
// Queen's English // Primitive English from our stateside friends from across the pond.
'colour' => 'color',
'grey' => 'gray',
'!please' => '!important',
'transparency' => 'opacity',
'centre' => 'center',
'plump' => 'bold',
'photograph' => 'image',
'capitalise' => 'capitalize'
);
/*
* The 'fail gracefully' variable allows the user to load the 'NOT FOUND' header if, well, nothing is found.
* If, however, this is set to TRUE, then nothing will be shown at all.
*
* @var boolean
* @access public
*/
public $fail_gracefully = FALSE;
/*
* Did the operation fail? We shall see.
* This should be set to FALSE by default.
*
* @var boolean
* @access private
*/
private $we_failed = FALSE;
/*
* The URL hook. Check out the .htaccess for further customisation.
*
* @var string
* @access private
*/
private $hook = 'file=';
/*
* The file variable
*
* @var string
* @access public
*/
public $file;
/*
* Perfectly British CSS right here
*
* @var string
* @access public
*/
public $css;
/*
* Constructor
*
* @param string
* @return void
*/
function __construct($raw = '') {
if ( !empty( $raw )) {
$this->css = $raw;
} else if ( isset( $_SERVER ) ) {
// Santise the string.
$this->file = dirname( __FILE__ )
. '/' . str_replace( $this->hook, '',
filter_input( INPUT_GET, 'file', FILTER_SANITIZE_STRING ) );
if( !file_exists( $this->file ) or !is_readable( $file ) ) {
$this->not_found();
}
$this->css = file_get_contents( $this->file );
} else {
// No spiffing CSS was found.
$this->not_found();
}
$this->process();
}
/*
* Magic really.
*
* @param void
* @access public
* @return string
*/
public function process() {
// The finished CSS.
$processed = '';
// The array which will hold all found CSS attributes to be repalced.
$replacements = array();
// The magic pattern which finds ONLY attributes.
$pattern = '/(?:(?:\s|\t)*|\;)([\w-]*):/i';
// One should begin by searching the CSS for exlusive Britishness.
preg_match_all( $pattern, $this->css, $matches );
foreach( $matches[1] as $index => $value ) {
// Let's run through the Queen's dictionary for every found term.
foreach( $this->dictionary as $british => $primitive ) {
// Did we find some Britishness?
if( strpos( $value, $british ) !== FALSE ) {
// We don't want overlapse - now we do not have an as big foreach to get through.
$replacements[$value] = str_ireplace($british, $primitive, $value);
}
}
}
// Now that we have the attributes to replace, let us begin...
foreach( $replacements as $search => $replace ) {
$processed = str_ireplace( $search, $replace, $this->css );
}
// Set the CSS header.
$this->set_header('Content-Type: text/css');
echo $processed;
exit;
}
/*
* Set a custom header
*
* @param string
* @access private
* @return void
*/
private function set_header($header) {
// Safely set the header
if( !headers_sent() ) {
header( $header );
}
}
/*
* Create a 404 function, since we do that a lot.
*
* @param void
* @access private
* @return void
*/
private function not_found() {
// Since we got called, we failed.
$this->we_failed = TRUE;
// Let's check if we should do something about that.
if ( $this->fail_gracefully == FALSE ) {
// Set the header
$this->set_header( 'HTTP/1.0 404 Not Found' );
// And stop execution. We don't need no content.
exit;
}
}
/*
* A public access of the failure variable.
*
* @param void
* @access public
* @return boolean
*/
public function did_we_fail() {
return $this->we_failed;
}
/*
* Deconstructor
*/
function __deconstruct() {
$this->not_found();
}
}
?>