forked from jquery/themeroller.jquerymobile.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_zip.php
83 lines (68 loc) · 5.41 KB
/
generate_zip.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
<?php
require_once('version.php');
date_default_timezone_set('America/Los_Angeles');
$theme_name = $_POST["theme_name"];
$uncompressed = $_POST["file"];
//minifying CSS file
$comment_pos = strpos($uncompressed, "\n/* Swatches");
$comment = substr($uncompressed, 0, $comment_pos);
$css = substr($uncompressed, $comment_pos, strlen($uncompressed) - $comment_pos);
$compressed = preg_replace_callback('/(\/\*.*?\*\/|\n|\t)/sim',
create_function(
'$matches',
'return "";'
), $css);
// remove all around in ",", ":" and "{"
$compressed = preg_replace_callback('/\s?(,|{|:){1}\s?/sim',
create_function(
'$matches',
'return $matches[1];'
), $compressed);
$compressed = $comment . $compressed;
$zip = new ZipArchive();
$dir = scandir('generated_zips');
$today = date('His', strtotime('now'));
//Loop to detect if any other zips are being created at this very second,
//Also deletes zip that are older than 15 seconds
$last_file_num = 0;
foreach($dir as $file) {
if($file != '.' && $file != '..' && $file != '.DS_Store') {
$file_name = explode('.', $file);
if(isset($file_name[0])) {
$date = explode('-', $file_name[0]);
$file_num = $date[4];
$time = $date[3];
if(is_numeric($file_num) && $file_num > $last_file_num && $date == $today) {
$last_file_num = $file_num;
}
if(strtotime('now - 15 seconds') >= strtotime($time) || strtotime('now + 15 seconds') <= strtotime($time)) {
unlink('generated_zips/' . $file);
}
}
}
}
$filename = "./generated_zips/jquery-mobile-theme-" . $today . "-" . $last_file_num . ".zip";
if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) {
exit("cannot open <$filename>\n");
}
/*
//gzipped version of jquery.mobile.min.css decided unneccessary for now
$gz = gzopen($filename . ".gz", "w");
gzwrite($gz, $compressed);
gzclose($gz);
$zip->addFile($filename . ".gz", "themes/jquery.mobile.min.css.gz");
*/
//add files to zip and echo it back to page
$zip->addFromString("themes/images/icons-18-white.png", file_get_contents("http://code.jquery.com/mobile/latest/images/icons-18-white.png"));
$zip->addFromString("themes/images/icons-18-black.png", file_get_contents("http://code.jquery.com/mobile/latest/images/icons-18-black.png"));
$zip->addFromString("themes/images/icons-36-white.png", file_get_contents("http://code.jquery.com/mobile/latest/images/icons-36-white.png"));
$zip->addFromString("themes/images/icons-36-black.png", file_get_contents("http://code.jquery.com/mobile/latest/images/icons-36-black.png"));
$zip->addFromString("themes/images/ajax-loader.png", file_get_contents("http://code.jquery.com/mobile/latest/images/ajax-loader.png"));
$zip->addFromString("themes/" . $theme_name . ".css", $uncompressed);
$zip->addFromString("themes/" . $theme_name . ".min.css", $compressed);
//$zip->addFromString("js/jquery.mobile.min.js", htmlspecialchars(file_get_contents("http://code.jquery.com/mobile/latest/jquery.mobile.min.js")));
//$zip->addFromString("js/jquery.min.js", htmlspecialchars(file_get_contents("http://code.jquery.com/jquery.min.js")));
$zip->addFromString("index.html", "<!DOCTYPE html>\n<html>\n<head>\n <meta charset=\"utf-8\">\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n <title>jQuery Mobile: Theme Download</title>\n <link rel=\"stylesheet\" href=\"themes/" . $theme_name . ".min.css\" />\n <link rel=\"stylesheet\" href=\"http://code.jquery.com/mobile/" . $JQM_VERSION . "/jquery.mobile.structure-" . $JQM_VERSION . ".min.css\" /> \n<script src=\"http://code.jquery.com/jquery-" . $JQUERY_VERSION . ".min.js\"></script> \n <script src=\"http://code.jquery.com/mobile/" . $JQM_VERSION . "/jquery.mobile-" . $JQM_VERSION . ".min.js\"></script>\n</head> \n<body> \n \n \n <div class=\"theme-preview\">\n <div data-role=\"header\" data-position=\"inline\">\n <h1>It Worked!</h1>\n </div>\n \n <div class=\"ui-body ui-body-a\">\n <p>Your theme was successfully downloaded. You can use this page as a reference for how to link it up!</p>\n <pre>\n<strong><link rel="stylesheet" href="themes/" . $theme_name . ".min.css"></strong>\n<link rel="stylesheet" href="http://code.jquery.com/mobile/" . $JQM_VERSION . "/jquery.mobile.structure-" . $JQM_VERSION . ".min.css" /> \n<script src="http://code.jquery.com/jquery-" . $JQUERY_VERSION . ".min.js"></script>\n<script src="http://code.jquery.com/mobile/" . $JQM_VERSION . "/jquery.mobile-" . $JQM_VERSION . ".min.js"></script>\n </pre>\n <p>This is content color swatch \"A\" and a preview of a <a href=\"#\" class=\"ui-link\">link</a>.</p>\n <label for=\"slider1\">Input slider:</label>\n <input type=\"range\" name=\"slider1\" id=\"slider1\" value=\"50\" min=\"0\" max=\"100\" data-theme=\"a\" />\n <fieldset data-role=\"controlgroup\" data-type=\"horizontal\" data-role=\"fieldcontain\">\n <legend>Cache settings:</legend>\n <input type=\"radio\" name=\"radio-choice-a1\" id=\"radio-choice-a1\" value=\"on\" checked=\"checked\" />\n <label for=\"radio-choice-a1\">On</label>\n <input type=\"radio\" name=\"radio-choice-a1\" id=\"radio-choice-b1\" value=\"off\" />\n <label for=\"radio-choice-b1\">Off</label>\n </fieldset>\n </div>\n\n </div>\n\n\n</body>\n</html>");
$zip->close();
echo ($filename);
?>