root / trunk / contrib / compress_yui.php

Revision 1250, 3.1 kB (checked in by gogo, 2 years ago)

ticket:1515 Security patch to prevent rogue access to the compressors in contrib.

Line 
1<?
2// This is script that uses the YUI compressor (http://www.julienlecomte.net/blog/2007/08/11/)
3// It yields gradually better results than the dojo comressor, but it produces unreadable code
4//die("Run this script to batch-compress the current Xinha snapshot. To run the script, open the file and comment out the die() command");
5
6$repository_url = 'http://svn.xinha.org/trunk';
7$version ='';
8$date = date('r');
9
10$xinha_root = realpath(dirname(__FILE__).'/..');
11
12error_reporting(E_ALL);
13ini_set('show_errors',1);
14
15$return = array();
16function scan($dir, $durl = '',$min_size="3000")
17{
18    static $seen = array();
19    global $return;
20    $files = array();
21
22    $dir = realpath($dir);
23    if(isset($seen[$dir]))
24    {
25        return $files;
26    }
27    $seen[$dir] = TRUE;
28    $dh = @opendir($dir);
29
30
31    while($dh && ($file = readdir($dh)))
32    {
33        if($file !== '.' && $file !== '..')
34        {
35            $path = realpath($dir . '/' . $file);
36            $url  = $durl . '/' . $file;
37
38            if(preg_match("/\.svn|lang/",$path)) continue;
39           
40            if(is_dir($path))
41            {
42                scan($path);
43            }
44            elseif(is_file($path))
45            {
46                if(!preg_match("/\.(js|css)$/",$path) || filesize($path) < $min_size) continue;
47                $return[] =  $path;
48            }
49
50        }
51    }
52    @closedir($dh);
53
54    return $files;
55}
56
57scan($xinha_root,0);
58
59print "Processing ".count($return)." files<br />";
60
61$prefix = "/* This compressed file is part of Xinha. For uncompressed sources, forum, and bug reports, go to xinha.org */";
62if ($version) $prefix .= "\n/* This file is part of version $version released $date */";
63$core_prefix = '
64  /*--------------------------------------------------------------------------
65    --  Xinha (is not htmlArea) - http://xinha.org
66    --
67    --  Use of Xinha is granted by the terms of the htmlArea License (based on
68    --  BSD license)  please read license.txt in this package for details.
69    --
70    --  Copyright (c) 2005-'.date('Y').' Xinha Developer Team and contributors
71    -- 
72    --  Xinha was originally based on work by Mihai Bazon which is:
73    --      Copyright (c) 2003-2004 dynarch.com.
74    --      Copyright (c) 2002-2003 interactivetools.com, inc.
75    --      This copyright notice MUST stay intact for use.
76    -------------------------------------------------------------------------*/
77';
78
79foreach ($return as $file)
80{
81    set_time_limit ( 60 );
82    print "Processing $file\n";
83    flush();
84   
85    $file_url = $repository_url.str_replace($xinha_root,'',$file);
86
87    copy($file,$file."_uncompr.js");
88   
89    $file_prefix = $prefix."\n/* The URL of the most recent uncompressed version of this file is $file_url */";
90    $ext = preg_replace('/.*?(\.js|\.css)$/','$1',$file);
91   
92    file_put_contents($file."_uncompr${ext}", preg_replace('/(\/\/[^\n]*)?(?![*])\\\[\n]/','',file_get_contents($file)));
93
94    passthru("echo \"".(preg_match('/XinhaCore.js$/',$file) ? $file_prefix.$core_prefix : $prefix)."\" > $file && java -jar {$xinha_root}/contrib/yuicompressor-2.4.2.jar  --charset UTF-8 ${file}_uncompr${ext} >> $file 2>&1");
95    if (preg_match('/\d+:\d+:syntax error/',file_get_contents($file)) || preg_match('/sh: java: command not found/', file_get_contents($file)))
96    {
97        unlink($file);
98        rename($file."_uncompr${ext}",$file);
99    }
100    else
101    {
102        unlink($file."_uncompr${ext}");
103    }
104
105}
106print "Operation complete."
107?>
Note: See TracBrowser for help on using the browser.