Browse Source

Put file paths in a central config file

master
Timothy Warren 8 years ago
parent
commit
004944fdd8
4 changed files with 128 additions and 39 deletions
  1. 1
    1
      README.md
  2. 36
    0
      config/config.php
  3. 15
    11
      css.php
  4. 76
    27
      js.php

+ 1
- 1
README.md View File

@@ -4,7 +4,7 @@ A simple set of minifying scripts for CSS and Javascript
4 4
 
5 5
 ## Basic Use: ##
6 6
 
7
-1. Figure out your file paths, and set them in css.php and js.php.
7
+1. Figure out your file paths, and set them in `config/config.php`.
8 8
 2. Add your css and javascript files to groups, in `config/css_groups.php` and `config/js_groups.php` respectively
9 9
 3. Point your CSS links in your HTML to `css.php/g/[group_name]`, and likewise your javascript to `js.php/g/[group_name]`
10 10
 4. Add a folder named "cache" to your js path

+ 36
- 0
config/config.php View File

@@ -0,0 +1,36 @@
1
+<?php
2
+
3
+/*
4
+|--------------------------------------------------------------------------
5
+| Document Root
6
+|--------------------------------------------------------------------------
7
+|
8
+| The folder where the index of the website exists. In most situations, 
9
+| this will not need to be changed. 
10
+|
11
+| If the website is in a folder off of the domain name, like:
12
+|	http://example.com/website/ 
13
+| you will need to add that folder to the document root.
14
+|
15
+*/
16
+$document_root = $_SERVER['DOCUMENT_ROOT'];
17
+
18
+/*
19
+|--------------------------------------------------------------------------
20
+| CSS Folder
21
+|--------------------------------------------------------------------------
22
+|
23
+| The folder where css files exist, in relation to the document root
24
+|
25
+*/
26
+$css_root = $document_root. '/css/';
27
+
28
+/*
29
+|--------------------------------------------------------------------------
30
+| JS Folder
31
+|--------------------------------------------------------------------------
32
+|
33
+| The folder where javascript files exist, in relation to the document root
34
+|
35
+*/
36
+$js_root = $document_root. '/js/';

+ 15
- 11
css.php View File

@@ -1,10 +1,13 @@
1 1
 <?php
2
+//Get config files
3
+require('./config/config.php');
4
+
5
+//Include the css groups
6
+$groups = require("./config/css_groups.php");
7
+
8
+//The name of this file
9
+$this_file = $css_root.'css.php';
2 10
 
3
-//Change as needed
4
-$base_path = $_SERVER['DOCUMENT_ROOT'];
5
-//This GZIPs the CSS for transmission to the user
6
-//making file size smaller and transfer rate quicker
7
-ob_start("ob_gzhandler");
8 11
 
9 12
 //Function for compressing the CSS as tightly as possible
10 13
 function compress($buffer) {
@@ -33,9 +36,6 @@ while($i < $pia_len)
33 36
 	$i = $j + 1;
34 37
 };
35 38
 
36
-//Include the css groups
37
-$groups = require("./config/css_groups.php");
38
-
39 39
 $css = '';
40 40
 $modified = array();
41 41
 
@@ -43,14 +43,14 @@ if(isset($groups[$_GET['g']]))
43 43
 {
44 44
 	foreach($groups[$_GET['g']] as $file)
45 45
 	{
46
-		$new_file = realpath($base_path.$file);
46
+		$new_file = realpath($css_root.$file);
47 47
 		$css .= file_get_contents($new_file);
48 48
 		$modified[] = filemtime($new_file);
49 49
 	}
50 50
 }
51 51
 
52 52
 //Add this page too
53
-$modified[] = filemtime($base_path."css.php");
53
+$modified[] = filemtime($this_file);
54 54
 
55 55
 //Get the latest modified date
56 56
 rsort($modified);
@@ -71,10 +71,14 @@ if($last_modified === $requested_time)
71 71
 	exit();
72 72
 }
73 73
 
74
+//This GZIPs the CSS for transmission to the user
75
+//making file size smaller and transfer rate quicker
76
+ob_start("ob_gzhandler");
77
+
74 78
 header("Content-Type: text/css; charset=utf8");
75 79
 header("Cache-control: public, max-age=691200, must-revalidate");
76 80
 header("Last-Modified: ".gmdate('D, d M Y H:i:s', $last_modified)." GMT");
77
-header("Expires: ".gmdate('D, d M Y H:i:s', (filemtime($base_path.'css.php') + 691200))." GMT");
81
+header("Expires: ".gmdate('D, d M Y H:i:s', (filemtime($this_file) + 691200))." GMT");
78 82
 
79 83
 echo $css;
80 84
 

+ 76
- 27
js.php View File

@@ -1,14 +1,41 @@
1 1
 <?php
2
-//Change as needed
3
-$base_path = $_SERVER['DOCUMENT_ROOT'];
4
-
2
+//Get config files
3
+require('./config/config.php');
5 4
 require('./config/jshrink.php');
6 5
 
7
-//This GZIPs the js for transmission to the user
8
-//making file size smaller and transfer rate quicker
9
-ob_start("ob_gzhandler");
6
+//Include the js groups
7
+$groups = require("./config/js_groups.php");
8
+
9
+//The name of this file
10
+$this_file = $js_root.'js.php';
11
+
12
+// --------------------------------------------------------------------------
13
+
14
+/**
15
+ * Get Files
16
+ * 
17
+ * Concatonates the javascript files for the current
18
+ * group as a string
19
+ * @return string
20
+ */
21
+function get_files()
22
+{
23
+	global $groups, $js_root;
24
+
25
+	$js = '';
26
+	
27
+	foreach($groups[$_GET['g']] as $file)
28
+	{
29
+		$new_file = realpath($js_root.$file);
30
+		$js .= file_get_contents($new_file);
31
+	}
32
+	
33
+	return $js;
34
+}
35
+
36
+// --------------------------------------------------------------------------
10 37
 
11
-//Creative rewriting
38
+//Creative rewriting of /g/groupname to ?g=groupname
12 39
 $pi = $_SERVER['PATH_INFO'];
13 40
 $pia = explode('/', $pi);
14 41
 
@@ -25,38 +52,39 @@ while($i < $pia_len)
25 52
 	$i = $j + 1;
26 53
 };
27 54
 
28
-//Include the js groups
29
-$groups = require("./config/js_groups.php");
30
-
31 55
 $js = '';
32 56
 $modified = array();
33 57
 
58
+// --------------------------------------------------------------------------
59
+
60
+//Aggregate the last modified times of the files
34 61
 if(isset($groups[$_GET['g']]))
35 62
 {
36
-	$cache_file = $base_path.'/cache/'.$_GET['g'];
63
+	$cache_file = $js_root.'/cache/'.$_GET['g'];
37 64
 	
38 65
 	foreach($groups[$_GET['g']] as $file)
39 66
 	{
40
-		$new_file = realpath($base_path.$file);
41
-		$js .= file_get_contents($new_file);
67
+		$new_file = realpath($js_root.$file);
42 68
 		$modified[] = filemtime($new_file);
43 69
 	}
70
+	
71
+	//Add this page too
72
+	$modified[] = filemtime($this_file);
73
+	
74
+	$cache_modified = 0;
75
+	
76
+	//Add the cache file
77
+	if(is_file($cache_file))
78
+	{
79
+		$cache_modified = filemtime($cache_file);
80
+	}
44 81
 }
45 82
 else //Nothing to display? Just exit
46 83
 {
47 84
 	die("You must specify a group that exists");
48 85
 }
49 86
 
50
-//Add this page too
51
-$modified[] = filemtime($base_path."js.php");
52
-
53
-$cache_modified = 0;
54
-
55
-//Add the cache file
56
-if(is_file($cache_file))
57
-{
58
-	$cache_modified = filemtime($cache_file);
59
-}
87
+// --------------------------------------------------------------------------
60 88
 
61 89
 //Get the latest modified date
62 90
 rsort($modified);
@@ -66,26 +94,47 @@ $requested_time=(isset($_SERVER['HTTP_IF_MODIFIED_SINCE']))
66 94
 	? strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) 
67 95
 	: time();
68 96
 
97
+// If the browser's cached version is up to date, 
98
+// don't resend the file
69 99
 if($last_modified === $requested_time)
70 100
 {
71 101
 	header("HTTP/1.1 304 Not Modified");
72 102
 	exit();
73 103
 }
74 104
 
75
-if(!isset($_GET['debug']) && ($cache_modified < $last_modified))
105
+// --------------------------------------------------------------------------
106
+
107
+//Determine what to do: rebuild cache, send files as is, or send cache.
108
+if($cache_modified < $last_modified)
109
+{
110
+	$js = trim(JShrink::minify(get_files(), array('flaggedComments' => false)));
111
+	$cs = file_put_contents($cache_file, $js);
112
+	
113
+	//Make sure cache file gets created/updated
114
+	if($cs === FALSE)
115
+	{
116
+		die("Cache file was not created. Make sure you have the correct folder permissions.");
117
+	}
118
+}
119
+else if(isset($_GET['debug']))
76 120
 {
77
-	$js = trim(JShrink::minify($js, array('flaggedComments' => false)));
78
-	file_put_contents($cache_file, $js);
121
+	$js = get_files();
79 122
 }
80 123
 else
81 124
 {
82 125
 	$js = file_get_contents($cache_file);
83 126
 }
84 127
 
128
+// --------------------------------------------------------------------------
129
+
130
+//This GZIPs the js for transmission to the user
131
+//making file size smaller and transfer rate quicker
132
+ob_start("ob_gzhandler");
133
+
85 134
 header("Content-Type: application/x-javascript; charset=utf8");
86 135
 header("Cache-control: public, max-age=691200, must-revalidate");
87 136
 header("Last-Modified: ".gmdate('D, d M Y H:i:s', $last_modified)." GMT");
88
-header("Expires: ".gmdate('D, d M Y H:i:s', (filemtime($base_path.'js.php') + 691200))." GMT");
137
+header("Expires: ".gmdate('D, d M Y H:i:s', (filemtime($this_file) + 691200))." GMT");
89 138
 
90 139
 echo $js;
91 140
 

Loading…
Cancel
Save