source: trunk/library/csstidy/css_optimiser.php @ 7655

Revision 7655, 13.9 KB checked in by douglasz, 11 years ago (diff)

Ticket #3236 - Melhorias de performance no codigo do Expresso.

  • Property svn:executable set to *
Line 
1<?php
2header('Content-Type:text/html; charset=utf-8');
3require('class.csstidy.php');
4require('lang.inc.php');
5
6
7if (isset($_REQUEST['css_text']) && get_magic_quotes_gpc()) {
8        $_REQUEST['css_text'] = stripslashes($_REQUEST['css_text']);
9}
10
11function rmdirr($dirname,$oc=0)
12{
13        // Sanity check
14        if (!file_exists($dirname)) {
15          return false;
16        }
17        // Simple delete for a file
18        if (is_file($dirname) && (time()-fileatime($dirname))>3600) {
19           return unlink($dirname);
20        }
21        // Loop through the folder
22        if(is_dir($dirname))
23        {
24        $dir = dir($dirname);
25        while (false !== $entry = $dir->read()) {
26           // Skip pointers
27           if ($entry == '.' || $entry == '..') {
28                   continue;
29           }
30           // Recurse
31           rmdirr("$dirname/$entry",$oc);
32        }
33        $dir->close();
34        }
35        // Clean up
36        if ($oc==1)
37        {
38                return rmdir($dirname);
39        }
40}
41
42function options($options, $selected = null, $labelIsValue = false)
43{
44    $html = '';
45
46    settype($selected, 'array');
47    settype($options, 'array');
48
49    foreach ($options as $value=>$label)
50    {
51        if (is_array($label)) {
52            $value = $label[0];
53            $label = $label[1];
54        }
55        $label = htmlspecialchars($label, ENT_QUOTES, "utf-8");
56        $value = $labelIsValue ? $label
57                               : htmlspecialchars($value, ENT_QUOTES, "utf-8");
58
59        $html .= '<option value="'.$value.'"';
60        if (in_array($value, $selected)) {
61            $html .= ' selected="selected"';
62        }
63        $html .= '>'.$label.'</option>';
64    }
65    if (!$html) {
66        $html .= '<option value="0">---</option>';
67    }
68
69    return $html;
70}
71
72$css = new csstidy();
73if(isset($_REQUEST['custom']) && !empty($_REQUEST['custom']))
74{
75    setcookie ('custom_template', $_REQUEST['custom'], time()+360000);
76}
77rmdirr('temp');
78
79if(isset($_REQUEST['case_properties'])) $css->set_cfg('case_properties',$_REQUEST['case_properties']);
80if(isset($_REQUEST['lowercase'])) $css->set_cfg('lowercase_s',true);
81if(!isset($_REQUEST['compress_c']) && isset($_REQUEST['post'])) $css->set_cfg('compress_colors',false);
82if(!isset($_REQUEST['compress_fw']) && isset($_REQUEST['post'])) $css->set_cfg('compress_font-weight',false);
83if(isset($_REQUEST['merge_selectors'])) $css->set_cfg('merge_selectors', $_REQUEST['merge_selectors']);
84if(isset($_REQUEST['optimise_shorthands'])) $css->set_cfg('optimise_shorthands',$_REQUEST['optimise_shorthands']);
85if(!isset($_REQUEST['rbs']) && isset($_REQUEST['post'])) $css->set_cfg('remove_bslash',false);
86if(isset($_REQUEST['preserve_css'])) $css->set_cfg('preserve_css',true);
87if(isset($_REQUEST['sort_sel'])) $css->set_cfg('sort_selectors',true);
88if(isset($_REQUEST['sort_de'])) $css->set_cfg('sort_properties',true);
89if(isset($_REQUEST['remove_last_sem'])) $css->set_cfg('remove_last_;',true);
90if(isset($_REQUEST['discard'])) $css->set_cfg('discard_invalid_properties',true);
91if(isset($_REQUEST['css_level'])) $css->set_cfg('css_level',$_REQUEST['css_level']);
92if(isset($_REQUEST['timestamp'])) $css->set_cfg('timestamp',true);
93
94?>
95<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
96    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
97<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
98  <head>
99    <title>
100      <?php echo $lang[$l][0]; echo $css->version; ?>)
101    </title>
102    <meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8" />
103    <link rel="stylesheet" href="cssparse.css" type="text/css" />
104    <script type="text/javascript">
105    function enable_disable_preserve()
106    {
107        var inputs =   new Array('sort_sel', 'sort_de', 'optimise_shorthands', 'merge_selectors', 'none');
108        var inputs_v = new Array( true,       true,      true,                  true,              false);
109        for(var i = 0; i < inputs.length; i++)
110        {
111            if(document.getElementById('preserve_css').checked)  {
112                document.getElementById(inputs[i]).disabled = inputs_v[i];
113            } else {
114                document.getElementById(inputs[i]).disabled = !inputs_v[i];
115            }
116        }
117    }
118    function ClipBoard()
119    {
120        window.clipboardData.setData('Text',document.getElementById("copytext").innerText);
121    }
122    </script>
123  </head>
124  <body onload="enable_disable_preserve()">
125    <div><h1 style="display:inline">
126      <?php echo $lang[$l][1]; ?>
127    </h1>
128    <?php echo $lang[$l][2]; ?> <a
129      href="http://csstidy.sourceforge.net/">csstidy</a> <?php echo $css->version; ?>)
130    </div><p>
131    <?php echo $lang[$l][39]; ?>: <a hreflang="en" href="?lang=en">English</a> <a hreflang="de" href="?lang=de">Deutsch</a> <a hreflang="fr" href="?lang=fr">French</a> <a hreflang="zh" href="?lang=zh">Chinese</a></p>
132    <p><?php echo $lang[$l][4]; ?>
133      <?php echo $lang[$l][6]; ?>
134    </p>
135
136    <form method="post" action="">
137      <div>
138        <fieldset id="field_input">
139          <legend><?php echo $lang[$l][8]; ?></legend> <label for="css_text"
140          class="block"><?php echo $lang[$l][9]; ?></label><textarea id="css_text" name="css_text" rows="20" cols="35"><?php if(isset($_REQUEST['css_text'])) echo htmlspecialchars($_REQUEST['css_text']); ?></textarea>
141            <label for="url"><?php echo $lang[$l][10]; ?></label> <input type="text"
142          name="url" id="url" <?php if(isset($_REQUEST['url']) &&
143          !empty($_REQUEST['url'])) echo 'value="'.$_REQUEST['url'].'"'; ?>
144          size="35" /><br />
145          <input type="submit" value="<?php echo $lang[$l][35]; ?>" id="submit" />
146        </fieldset>
147        <div id="rightcol">
148          <fieldset id="code_layout">
149            <legend><?php echo $lang[$l][11]; ?></legend> <label for="template"
150            class="block"><?php echo $lang[$l][12]; ?></label> <select
151            id="template" name="template" style="margin-bottom:1em;">
152              <?php
153                $num = ($_REQUEST['template']) ? intval($_REQUEST['template']) : 1;
154                echo options(array(3 => $lang[$l][13], 2 => $lang[$l][14], 1 => $lang[$l][15], 0 => $lang[$l][16], 4 => $lang[$l][17]), $num);
155              ?>
156            </select><br />
157            <label for="custom" class="block">
158            <?php echo $lang[$l][18]; ?> </label> <textarea id="custom"
159            name="custom" cols="33" rows="4"><?php
160               if(isset($_REQUEST['custom']) && !empty($_REQUEST['custom'])) echo
161              htmlspecialchars($_REQUEST['custom']);
162               elseif(isset($_COOKIE['custom_template']) &&
163              !empty($_COOKIE['custom_template'])) echo
164              htmlspecialchars($_COOKIE['custom_template']);
165               ?></textarea>
166          </fieldset>
167          <fieldset id="options">
168         <legend><?php echo $lang[$l][19]; ?></legend>
169
170            <input onchange="enable_disable_preserve()" type="checkbox" name="preserve_css" id="preserve_css"
171                   <?php if($css->get_cfg('preserve_css')) echo 'checked="checked"'; ?> />
172            <label for="preserve_css" title="<?php echo $lang[$l][52]; ?>" class="help"><?php echo $lang[$l][51]; ?></label><br />
173
174
175            <input type="checkbox" name="sort_sel" id="sort_sel"
176                   <?php if($css->get_cfg('sort_selectors')) echo 'checked="checked"'; ?> />
177            <label for="sort_sel" title="<?php echo $lang[$l][41]; ?>" class="help"><?php echo $lang[$l][20]; ?></label><br />
178
179
180            <input type="checkbox" name="sort_de" id="sort_de"
181                   <?php if($css->get_cfg('sort_properties')) echo 'checked="checked"'; ?> />
182            <label for="sort_de"><?php echo $lang[$l][21]; ?></label><br />
183
184
185            <label for="merge_selectors"><?php echo $lang[$l][22]; ?></label>
186            <select style="width:15em;" name="merge_selectors" id="merge_selectors">
187              <?php echo options(array('0' => $lang[$l][47], '1' => $lang[$l][48], '2' => $lang[$l][49]), $css->get_cfg('merge_selectors')); ?>
188            </select><br />
189
190            <label for="optimise_shorthands"><?php echo $lang[$l][23]; ?></label>
191            <select name="optimise_shorthands" id="optimise_shorthands">
192            <?php echo options(array($lang[$l][54], $lang[$l][55], $lang[$l][56]), $css->get_cfg('optimise_shorthands')); ?>
193            </select><br />
194
195
196            <input type="checkbox" name="compress_c" id="compress_c"
197                   <?php if($css->get_cfg('compress_colors')) echo 'checked="checked"';?> />
198            <label for="compress_c"><?php echo $lang[$l][24]; ?></label><br />
199
200
201            <input type="checkbox" name="compress_fw" id="compress_fw"
202                   <?php if($css->get_cfg('compress_font-weight')) echo 'checked="checked"';?> />
203            <label for="compress_fw"><?php echo $lang[$l][45]; ?></label><br />
204
205
206            <input type="checkbox" name="lowercase" id="lowercase" value="lowercase"
207                   <?php if($css->get_cfg('lowercase_s')) echo 'checked="checked"'; ?> />
208            <label title="<?php echo $lang[$l][30]; ?>" class="help" for="lowercase"><?php echo $lang[$l][25]; ?></label><br />
209
210
211            <?php echo $lang[$l][26]; ?><br />
212            <input type="radio" name="case_properties" id="none" value="0"
213                   <?php if($css->get_cfg('case_properties') == 0) echo 'checked="checked"'; ?> />
214            <label for="none"><?php echo $lang[$l][53]; ?></label>
215            <input type="radio" name="case_properties" id="lower_yes" value="1"
216                   <?php if($css->get_cfg('case_properties') == 1) echo 'checked="checked"'; ?> />
217            <label for="lower_yes"><?php echo $lang[$l][27]; ?></label>
218            <input type="radio" name="case_properties" id="upper_yes" value="2"
219                   <?php if($css->get_cfg('case_properties') == 2) echo 'checked="checked"'; ?> />
220            <label for="upper_yes"><?php echo $lang[$l][29]; ?></label><br />
221
222            <input type="checkbox" name="rbs" id="rbs"
223                   <?php if($css->get_cfg('remove_bslash')) echo 'checked="checked"'; ?> />
224            <label for="rbs"><?php echo $lang[$l][31]; ?></label><br />
225
226
227            <input type="checkbox" id="remove_last_sem" name="remove_last_sem"
228                   <?php if($css->get_cfg('remove_last_;')) echo 'checked="checked"'; ?> />
229                        <label for="remove_last_sem"><?php echo $lang[$l][42]; ?></label><br />
230
231
232            <input type="checkbox" id="discard" name="discard"
233                   <?php if($css->get_cfg('discard_invalid_properties')) echo 'checked="checked"'; ?> />
234            <label for="discard"><?php echo $lang[$l][43]; ?></label>
235            <select name="css_level"><?php echo options(array('CSS2.1','CSS2.0','CSS1.0'),$css->get_cfg('css_level'), true); ?></select><br />
236
237
238            <input type="checkbox" id="timestamp" name="timestamp"
239                   <?php if($css->get_cfg('timestamp')) echo 'checked="checked"'; ?> />
240                        <label for="timestamp"><?php echo $lang[$l][57]; ?></label><br />
241
242
243            <input type="checkbox" name="file_output" id="file_output" value="file_output"
244                   <?php if(isset($_REQUEST['file_output'])) echo 'checked="checked"'; ?> />
245            <label class="help" title="<?php echo $lang[$l][34]; ?>" for="file_output">
246                                <strong><?php echo $lang[$l][33]; ?></strong>
247                        </label><br />
248
249          </fieldset>
250        <input type="hidden" name="post" />
251        </div>
252      </div>
253    </form>
254    <?php
255
256    $file_ok = false;
257    $result = false;
258
259    $url = (isset($_REQUEST['url']) && !empty($_REQUEST['url'])) ? $_REQUEST['url'] : false;
260
261        if(isset($_REQUEST['template']))
262        {
263                switch($_REQUEST['template'])
264                {
265                        case 4:
266                        if(isset($_REQUEST['custom']) && !empty($_REQUEST['custom']))
267                        {
268                                $css->load_template($_REQUEST['custom'],false);
269                        }
270                        break;
271
272                        case 3:
273                        $css->load_template('highest_compression');
274                        break;
275
276                        case 2:
277                        $css->load_template('high_compression');
278                        break;
279
280                        case 0:
281                        $css->load_template('low_compression');
282                        break;
283                }
284        }
285
286    if($url)
287    {
288        if(substr($_REQUEST['url'],0,7) != 'http://')
289                {
290                        $_REQUEST['url'] = 'http://'.$_REQUEST['url'];
291                }
292        $result = $css->parse_from_url($_REQUEST['url'],0);
293    }
294    elseif(isset($_REQUEST['css_text']) && strlen($_REQUEST['css_text'])>5)
295    {
296        $result = $css->parse($_REQUEST['css_text']);
297    }
298
299    if($result)
300    {
301        $ratio = $css->print->get_ratio();
302        $diff = $css->print->get_diff();
303        if(isset($_REQUEST['file_output']))
304        {
305            $filename = md5(mt_rand().time().mt_rand());
306            $handle = fopen('temp/'.$filename.'.css','w');
307            if($handle) {
308                if(fwrite($handle,$css->print->plain()))
309                {
310                    $file_ok = true;
311                }
312            }
313            fclose($handle);
314        }
315        if($ratio>0) $ratio = '<span style="color:green;">'.$ratio.'%</span>
316    ('.$diff.' Bytes)'; else $ratio = '<span
317    style="color:red;">'.$ratio.'%</span> ('.$diff.' Bytes)';
318        if(count($css->log) > 0): ?>
319        <fieldset id="messages"><legend>Messages</legend>
320                        <div><dl><?php
321                        foreach($css->log as $line => $array)
322                        {
323                                echo '<dt>'.$line.'</dt>';
324                                for($i = 0; $i < count($array); ++$i)
325                                {
326                                        echo '<dd class="'.$array[$i]['t'].'">'.$array[$i]['m'].'</dd>';
327                                }
328                        }
329                        ?></dl></div>
330        </fieldset>
331        <?php endif;
332        echo '<fieldset><legend>'.$lang[$l][37].': '.$css->print->size('input').'KB, '.$lang[$l][38].':'.$css->print->size('output').'KB, '.$lang[$l][36].': '.$ratio;
333        if($file_ok)
334        {
335            echo ' - <a href="temp/'.$filename.'.css">Download</a>';
336        }
337        echo ' - <a href="javascript:ClipBoard()">Copy to clipboard</a>';
338        echo '</legend>';
339        echo '<pre><code id="copytext">';
340        echo $css->print->formatted();
341        echo '</code></pre>';
342        echo '</fieldset><p><a href="javascript:scrollTo(0,0)">&#8593; Back to top</a></p>';
343     }
344     elseif(isset($_REQUEST['css_text']) || isset($_REQUEST['url'])) {
345        echo '<p class="important">'.$lang[$l][28].'</p>';
346     }
347     ?>
348    <p style="text-align:center;font-size:0.8em;clear:both;">
349      For bugs and suggestions feel free to <a
350      href="http://csstidy.sourceforge.net/contact.php">contact me</a>.
351    </p>
352  </body>
353</html>
Note: See TracBrowser for help on using the repository browser.