OSDN Git Service

Spriteを組み合わせるFormSpriteを作成。
[yukkurioverwint/YukkuriOverwinter.git] / php / create_script.php
1 <?php
2    // This script has been placed in the public domain.
3    // Use it and modify it however you wish.
4
5    // Disable zlib compression, if present, for duration of this script.  
6    // So we don't double gzip 
7    ini_set("zlib.output_compression", "Off");
8
9    //Set the content type header
10    header("Content-Type: text/javascript; charset=UTF-8"); 
11
12    // Set the cache control header
13    // http 1.1 browsers MUST revalidate -- always
14    header("Cache-Control: must-revalidate");     
15 function getallheaders() {
16     global $HTTP_SERVER_VARS;
17     if (!empty($HTTP_SERVER_VARS) && is_array($HTTP_SERVER_VARS)) {
18         reset( $HTTP_SERVER_VARS );
19         while( $each_HTTP_SERVER_VARS = each($HTTP_SERVER_VARS) ) {
20             $name = $each_HTTP_SERVER_VARS['key'];
21             $value = $each_HTTP_SERVER_VARS['value'];
22             if(substr($name, 0, 5) == 'HTTP_')
23                 $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
24         }
25     }
26     return $headers;
27 }
28
29    
30    // Here we're going to extract the filename list.
31    // We just split the original URL with "/" as the pivot
32    $expl = explode("/",$HTTP_SERVER_VARS["REQUEST_URI"]);
33    // Do a little trimming and url decoding to change %20 into spaces.
34    $fileList = trim(urldecode($expl[count($expl)-1]));
35    $fileList = $_GET['filelist'];
36    // And explode the remainder out with "," as the pivot to get our list.
37    $orgFileNames = explode(",",$fileList);
38    // $fileNames now is an array of the requested file names.
39
40    // Go through each of the files and get its last modified time so we
41    // can send a last-modified header so caching works properly
42    $newestFile = 0;
43    $ii=0;
44    $longFilename = ''; // This is generated for the Hash
45    $fileNames = Array();
46    for ($i=0; ($i < count($orgFileNames)); $i++) {
47       $orgFileNames[$i] = trim($orgFileNames[$i]);  // Get rid of whitespace
48       if (preg_match('/\.js$/i',$orgFileNames[$i])) { // Allow only files ending in .js in the list.
49          $fileNames[$ii++]= 'lib/' . $orgFileNames[$i];         // Valid file name, so go ahead and use it.
50          $longFilename .= 'lib/' . $orgFileNames[$i];          // Build our LONG file name for the hash.
51          $lastMod = @filemtime('lib/' . $orgFileNames[$i]);    // Get file last modified time
52          if ($lastMod > $newestFile) {                // Is this the newest file?
53             $newestFile = $lastMod;                   // Yup, so mark it.
54          }
55       } 
56    }
57 /////////////////////////////////////////////////////////////////////////////
58 // Begin *BROWSER* Cache Control
59
60    // Here we check to see if the browser is doing a cache check
61    // First we'll do an etag check which is to see if we've already stored
62    // the hash of the filename . '-' . $newestFile.  If we find it
63    // nothing has changed so let the browser know and then die.  If we
64    // don't find it (or it's a mismatch) something has changed so force
65    // the browser to ignore the cache.
66    $fileHash = md5($longFilename);       // This generates a key from the collective file names
67    $hash = $fileHash . '-'.$newestFile;  // This appends the newest file date to the key.
68    $headers = getallheaders();           // Get all the headers the browser sent us.
69    if (ereg($hash, $headers['If-None-Match']))  {   // Look for a hash match
70       // Our hash+filetime was matched with the browser etag value so nothing
71       // has changed.  Just send the last modified date and a 304 (nothing changed) 
72       // header and exit.
73       header('Last-Modified: '.gmdate('D, d M Y H:i:s', $newestFile).' GMT', true, 304);
74       die();
75    }
76
77    // We're still alive so save the hash+latest modified time in the e-tag.
78    header("ETag: \"{$hash}\"");
79
80    // For an additional layer of protection we'll see if the browser
81    // sent us a last-modified date and compare that with $newestFile
82    // If there's no change we'll send a cache control header and die.
83
84    if (isset($headers['If-Modified-Since'])) {
85       if ($newestFile <= strtotime($headers['If-Modified-Since'])) {
86          // No change so send a 304 header and terminate
87           header('Last-Modified: '.gmdate('D, d M Y H:i:s', $newestFile).' GMT', true, 304);
88           die();
89        }
90    }
91
92    // Set the last modified date as the date of the NEWEST file in the list.
93    header('Last-Modified: '.gmdate('D, d M Y H:i:s', $newestFile).' GMT');
94
95 // End *BROWSER* Cache Control
96 /////////////////////////////////////////////////////////////////////////////
97
98
99 /////////////////////////////////////////////////////////////////////////////
100 // Begin File System Cache Control
101
102    // Attempt to open a cache file for this set.  (This is the server file-system
103    // cache, not the browser cache.  From here on out we're done with the browser
104    // cache. 
105    $fp = @fopen("cache/$fileHash.txt","r");
106    if ($fp) {
107       // A cache file exists but if contents have changed delete the file pointer
108       // so we re-process the files like there was no cache
109       if ($newestFile>@filemtime("cache/$fileHash.txt")) { fclose($fp); $fp=false;}
110    }
111    if (!$fp) {
112       // No file pointer exists so we create the cache files for this set.
113       // for each filename in $fileNames, put the contents into $buffer
114       // with two blank lines between each file.
115       $buffer='';
116       for ($i=0; ($i < count($fileNames)); $i++) {
117          $buffer .= @file_get_contents($fileNames[$i]) . "\n\n";
118       }
119       // We've created our concatenated file so first we'll save it as
120       // plain text for non gzip enabled browsers.
121       $fp = @fopen("cache/$fileHash.txt","w");
122       @fwrite($fp,$buffer);
123       @fclose($fp);
124       // Now we'll compress the file (maximum compression) and save
125       // the compressed version.
126       $fp = @fopen("cache/$fileHash.gz","w");
127       $buffer = gzencode($buffer, 9, FORCE_GZIP);
128       @fwrite($fp,$buffer);
129       @fclose($fp);
130    }
131
132 // End File System Cache Control
133 /////////////////////////////////////////////////////////////////////////////
134
135
136 /////////////////////////////////////////////////////////////////////////////
137 // Begin Output 
138
139    if (strstr($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) {
140       // Browser can handle gzip data so send it the gzip version.
141       header ("Content-Encoding: gzip");
142       header ("Content-Length: " . filesize("cache/$fileHash.gz"));
143       readfile("cache/$fileHash.gz");
144    } else {
145       // Browser can't handle gzip so send it plain text version.
146       header ("Content-Length: " . filesize("cache/$fileHash.txt"));
147       readfile("cache/$fileHash.txt");
148    }
149
150 // End Output -- End Program
151 /////////////////////////////////////////////////////////////////////////////
152 ?>