-
Notifications
You must be signed in to change notification settings - Fork 16
/
cdn-sync-tool.php
219 lines (180 loc) · 6.75 KB
/
cdn-sync-tool.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<?php
/*
Plugin Name: CDN Sync Tool
Plugin URI: http://catn.com/
Description: Syncs static files to a CDN
Author: Fubra Limited
Author URI: http://www.catn.com
Version: 1.12
*/
/*
* Copyright (C) 2010 Fubra Limited
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.Gert
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
require_once 'etc/constants.php';
require_once CST_DIR.'/lib/Cst/Debug.php';
require_once CST_DIR.'/lib/Cst/Sync.php';
require_once CST_DIR.'/lib/Cst/Plugin.php';
function cst_install(){
if(is_multisite()) {
exit('Unfortunately this plugin isn\'t currently compatible with multisite. We apologize');
}
global $wpdb;
$oldVersion = get_option("cst_version");
update_option("cst_version", CST_VERSION);
if ( $oldVersion !== false ){
// If not current update!
// Otherwise exit.
if ( $oldVersion != CST_VERSION ){
cst_upgrade($oldVersion);
}
Cst_Debug::addLog("CST upgraded successfully");
return;
}
$wpdb->query("CREATE TABLE IF NOT EXISTS ".CST_TABLE_FILES." (
`id` int(11) NOT NULL AUTO_INCREMENT,
`filename` varchar(255) NOT NULL,
`smushed` varchar(11) NOT NULL,
`transferred` varchar(11) NOT NULL,
`timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`hash` varchar(32) DEFAULT NULL,
`file_location` varchar(255) DEFAULT NULL,
`media` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE = MYISAM ;");
update_option("cst_theme",true);
wp_schedule_event(time(), 'hourly', 'cst_cron_hourly');
Cst_Debug::addLog("CST installed successfully");
}
function cst_upgrade($oldVersion){
global $wpdb;
if ( $oldVersion <= "1.2" ){
wp_schedule_event(time(), 'hourly', 'cst_cron_hourly');
}
if ( $oldVersion <= "0.8" ){
$wpdb->query("ALTER TABLE `".CST_TABLE_FILES."` ADD `hash` VARCHAR( 32 ) NULL");
$wpdb->query("CREATE TABLE IF NOT EXISTS `".CST_TABLE_JSCSS."` (
`id` int(11) NOT NULL,
`filename` varchar(255) NOT NULL,
`template` varchar(255) NOT NULL,
`timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`tAWS_SECRET_KEype` varchar(3) NOT NULL
) ENGINE = MYISAM ;
");
}
if ( $oldVersion <= "0.9" ){
$wpdb->query("ALTER TABLE `".CST_TABLE_JSCSS."` ADD `type` VARCHAR( 255 ) NOT NULL");
}
if ( $oldVersion <= "1.1" ){
$wpdb->query("ALTER TABLE `".CST_TABLE_FILES."` ADD `media` INT(1) NULL");
$wpdb->query("ALTER TABLE `".CST_TABLE_FILES."` ADD `file_location` varchar(255) DEFAULT NULL,");
}
if ( version_compare($oldVersion, '1.12') < 0 ){
$wpdb->query("DROP TABLE IF EXISTS `".CST_TABLE_JSCSS."`");
}
return true;
}
function cst_cron_hourly(){
global $wpdb;
Cst_Debug::addLog('Hourly cron has run');
$fileHashes = $wpdb->get_results( "SELECT * FROM `".CST_TABLE_FILES."` GROUP BY filename",ARRAY_A );
foreach( $fileHashes as $file ){
if ( $file['hash'] != hash_file('md5',$file['file_location']) ){
Cst_Sync::process($file['filename'],$file['media']);
}
}
}
// Stolen from http://www.php.net/manual/en/function.mime-content-type.php#87856
if(!function_exists('mime_content_type')) {
function mime_content_type($filename) {
$mime_types = array(
'txt' => 'text/plain',
'htm' => 'text/html',
'html' => 'text/html',
'php' => 'text/html',
'css' => 'text/css',
'js' => 'application/javascript',
'json' => 'application/json',
'xml' => 'application/xml',
'swf' => 'application/x-shockwave-flash',
'flv' => 'video/x-flv',
// images
'png' => 'image/png',
'jpe' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'jpg' => 'image/jpeg',
'gif' => 'image/gif',
'bmp' => 'image/bmp',
'ico' => 'image/vnd.microsoft.icon',
'tiff' => 'image/tiff',
'tif' => 'image/tiff',
'svg' => 'image/svg+xml',
'svgz' => 'image/svg+xml',
// archives
'zip' => 'application/zip',
'rar' => 'application/x-rar-compressed',
'exe' => 'application/x-msdownload',
'msi' => 'application/x-msdownload',
'cab' => 'application/vnd.ms-cab-compressed',
// audio/video
'mp3' => 'audio/mpeg',
'qt' => 'video/quicktime',
'mov' => 'video/quicktime',
// adobe
'pdf' => 'application/pdf',
'psd' => 'image/vnd.adobe.photoshop',
'ai' => 'application/postscript',
'eps' => 'application/postscript',
'ps' => 'application/postscript',
// ms office
'doc' => 'application/msword',
'rtf' => 'application/rtf',
'xls' => 'application/vnd.ms-excel',
'ppt' => 'application/vnd.ms-powerpoint',
// open office
'odt' => 'application/vnd.oasis.opendocument.text',
'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
);
$ext = strtolower(array_pop(explode('.',$filename)));
if (array_key_exists($ext, $mime_types)) {
return $mime_types[$ext];
}
elseif (function_exists('finfo_open')) {
$finfo = finfo_open(FILEINFO_MIME);
$mimetype = finfo_file($finfo, $filename);
finfo_close($finfo);
return $mimetype;
}
else {
return 'application/octet-stream';
}
}
}
register_activation_hook( __FILE__, "cst_install" );
$objCstPlugin = new Cst_Plugin();
function cst_handle_upload($file) {
// images will already sync, so only upload non-image files
$rejected_upload_types = array( 'image/jpeg', 'image/png', 'image/gif' );
if( ! in_array( $file['type'], $rejected_upload_types ) ) {
// convert the full upload path to a relative path readable by the sync tool
$uploadDir = wp_upload_dir();
$basedir = $uploadDir['basedir'] . '/';
$relative_file_path = substr( $file['file'], strlen( $basedir ) );
Cst_Sync::process($relative_file_path, true);
}
return $file;
}
add_filter('wp_handle_upload', 'cst_handle_upload');