-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.php
353 lines (312 loc) · 15.8 KB
/
index.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
<?php
/*
Plugin Name: Cache Plugin
Plugin URI: http://www.osclass.org/
Description: Cache system for OSClass, make your website load faster!
Version: 2.0.5
Author: OSClass
Author URI: http://www.osclass.org/
Short Name: cacheplugin
Plugin update URI: plugin-cache
*/
function cacheplugin_subdomain() {
$cachepageURL = $_SERVER['HTTPS'] == 'on' ? 'https://' : 'http://';
$cachepageURL .= $_SERVER['SERVER_PORT'] != '80' ? $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"] : $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
$cacheparsedUrl = parse_url($cachepageURL);
$cachehost = explode('.', $cacheparsedUrl['host']);
return $cachehost[0];
}
if(!function_exists('osc_item_is_enabled')) {
function osc_item_is_enabled() {
return (osc_item_field("b_enabled")==1);
}
}
function cacheplugin_recursiveRemove($dir) {
$structure = glob(rtrim($dir, "/").'/*');
if (is_array($structure)) {
foreach($structure as $file) {
if (is_dir($file)) cacheplugin_recursiveRemove($file);
elseif (is_file($file)) unlink($file);
}
}
rmdir($dir);
}
function cacheplugin_install() {
@mkdir(osc_content_path().'uploads/cache_files/', 0777, true);
osc_set_preference('upload_path', osc_content_path().'uploads/cache_files/', 'cacheplugin', 'STRING');
osc_set_preference('main_time', '1', 'cacheplugin', 'INTEGER');
osc_set_preference('item_time', '24', 'cacheplugin', 'INTEGER');
osc_set_preference('static_time', '24', 'cacheplugin', 'INTEGER');
osc_set_preference('main_cache', 'active', 'cacheplugin', 'INTEGER');
osc_set_preference('item_cache', 'active', 'cacheplugin', 'INTEGER');
osc_set_preference('static_cache', 'active', 'cacheplugin', 'INTEGER');
osc_set_preference('posted_item_clean_cache', 'active', 'cacheplugin', 'INTEGER');
osc_set_preference('item_storage_folder', 'Y-m-d', 'cacheplugin', 'STRING');
}
function cacheplugin_uninstall() {
osc_delete_preference('upload_path', 'cacheplugin');
osc_delete_preference('item_time', 'cacheplugin');
osc_delete_preference('main_time', 'cacheplugin');
osc_delete_preference('static_time', 'cacheplugin');
osc_delete_preference('main_cache', 'cacheplugin');
osc_delete_preference('item_cache', 'cacheplugin');
osc_delete_preference('static_cache', 'cacheplugin');
osc_delete_preference('posted_item_clean_cache', 'cacheplugin');
osc_delete_preference('item_storage_folder', 'cacheplugin');
$dir = osc_content_path().'uploads/cache_files/'; // IMPORTANT: with '/' at the end
cacheplugin_recursiveRemove($dir);
}
if(!function_exists('cacheplugin_cache_start')) {
function cacheplugin_cache_start() {
if( osc_is_home_page() || osc_is_ad_page() || osc_is_static_page() || osc_get_osclass_location() == 'contact') {
if(!osc_is_web_user_logged_in()) {
if ((osc_is_ad_page())&&(osc_get_preference('item_cache', 'cacheplugin')== 'active')&&(!osc_item_is_spam())&&(osc_item_is_active())&&(osc_item_is_enabled())&&(!osc_show_flash_message())) {
$ItemStorageFolder = osc_get_preference('item_storage_folder', 'cacheplugin') ;
$PubbDate = osc_item_pub_date();
$DatePubb = date_create($PubbDate);
$cachePubbDate = date_format($DatePubb, $ItemStorageFolder);
$cachetitle = osc_item_id();
$cachefile = osc_get_preference('upload_path', 'cacheplugin')."item/".$cachePubbDate."/".$cachetitle.".html";
$cachetime = osc_get_preference('item_time', 'cacheplugin')*3600;
if (file_exists($cachefile) && (time() - $cachetime
< filemtime($cachefile)))
{
include($cachefile);
echo "<!-- Cached ".date('jS F Y H:i', filemtime($cachefile))." -->";
exit;
}
ob_start(); // start the output buffer
}
// cache static pages
elseif ((osc_is_static_page() || osc_get_osclass_location() == 'contact')&&(osc_get_preference('static_cache', 'cacheplugin')== 'active')&&(!osc_show_flash_message())) {
$cachetime = osc_get_preference('static_time', 'cacheplugin')*3600;
if(osc_get_osclass_location() == 'contact') {
$cachefile = osc_get_preference('upload_path', 'cacheplugin')."static/contact.html";
} else {
$page = Page::newInstance()->findByPrimaryKey(Params::getParam('id'));
$cachetitle = $page['pk_i_id'];
$cachefile = osc_get_preference('upload_path', 'cacheplugin')."static/".$cachetitle.".html";
}
if (file_exists($cachefile) && (time() - $cachetime < filemtime($cachefile)))
{
include($cachefile);
echo "<!-- Cached ".date('jS F Y H:i', filemtime($cachefile))." -->";
exit;
}
ob_start(); // start the output buffer
}
// cache home page
elseif ((osc_is_home_page())&&(osc_get_preference('main_cache', 'cacheplugin')== 'active')&&(!osc_show_flash_message())) {
$cachefile = osc_get_preference('upload_path', 'cacheplugin')."main/".cacheplugin_subdomain()."_cache.html";
$cachetime = osc_get_preference('main_time', 'cacheplugin')*3600;
if (file_exists($cachefile) && (time() - $cachetime
< filemtime($cachefile)))
{
include($cachefile);
echo "<!-- Cached ".date('jS F Y H:i', filemtime($cachefile))." -->";
exit;
}
ob_start(); // start the output buffer
}
}
}
}
}
if(!function_exists('cacheplugin_cache_end')) {
function cacheplugin_cache_end() {
if( osc_is_home_page() || osc_is_ad_page() || osc_is_static_page() || osc_get_osclass_location() == 'contact' ) {
if(!osc_is_web_user_logged_in()) {
if ((osc_is_ad_page())&&(osc_get_preference('item_cache', 'cacheplugin')== 'active')&&(!osc_item_is_spam())&&(osc_item_is_active())&&(osc_item_is_enabled())&&(!osc_show_flash_message())) {
$ItemStorageFolder = osc_get_preference('item_storage_folder', 'cacheplugin') ;
$PubbDate = osc_item_pub_date();
$DatePubb = date_create($PubbDate);
$cachePubbDate = date_format($DatePubb, $ItemStorageFolder);
$cachetitle = osc_item_id();
$cachefile = osc_get_preference('upload_path', 'cacheplugin')."item/".$cachePubbDate."/".$cachetitle.".html";
$cachetime = osc_get_preference('item_time', 'cacheplugin')*3600;
@mkdir(osc_get_preference('upload_path', 'cacheplugin')."item/".$cachePubbDate."/", 0777, true);
// open the cache file for writing
$fp = fopen($cachefile, 'w');
// save the contents of output buffer to the file
fwrite($fp, ob_get_contents());
// close the file
fclose($fp);
// Send the output to the browser
ob_end_flush();
}
// cache static pages
elseif ((osc_is_static_page() || osc_get_osclass_location() == 'contact')&&(osc_get_preference('static_cache', 'cacheplugin')== 'active')&&(!osc_show_flash_message())) {
$cachetime = osc_get_preference('static_time', 'cacheplugin')*3600;
if(osc_get_osclass_location() == 'contact') {
$cachefile = osc_get_preference('upload_path', 'cacheplugin')."static/contact.html";
} else {
$page = Page::newInstance()->findByPrimaryKey(Params::getParam('id'));
$cachetitle = $page['pk_i_id'];
$cachefile = osc_get_preference('upload_path', 'cacheplugin')."static/".$cachetitle.".html";
}
@mkdir(osc_get_preference('upload_path', 'cacheplugin')."static/", 0777, true);
// open the cache file for writing
$fp = fopen($cachefile, 'w');
// save the contents of output buffer to the file
fwrite($fp, ob_get_contents());
// close the file
fclose($fp);
// Send the output to the browser
ob_end_flush();
}
// cache home page
elseif ((osc_is_home_page())&&(osc_get_preference('main_cache', 'cacheplugin')== 'active')&&(!osc_show_flash_message())) {
$cachefile = osc_get_preference('upload_path', 'cacheplugin')."main/".cacheplugin_subdomain()."_cache.html";
$cachetime = osc_get_preference('main_time', 'cacheplugin')*3600;
@mkdir(osc_get_preference('upload_path', 'cacheplugin')."main/", 0777, true);
// open the cache file for writing
$fp = fopen($cachefile, 'w');
// save the contents of output buffer to the file
fwrite($fp, ob_get_contents());
// close the file
fclose($fp);
// Send the output to the browser
ob_end_flush();
}
}
}
}
}
function cacheplugin_add_comment($item) {
$ItemStorageFolder = osc_get_preference('item_storage_folder', 'cacheplugin') ;
$PubbDate = osc_item_pub_date($item);
$DatePubb = date_create($PubbDate);
$cachePubbDate = date_format($DatePubb, $ItemStorageFolder);
$IdItem = osc_item_id($item);
$files = rglob(osc_get_preference('upload_path', 'cacheplugin')."item/".$cachePubbDate."/".$IdItem.".html");
foreach($files as $f) {
@unlink($f);
}
}
function cacheplugin_item_edit_post($item) {
$ItemStorageFolder = osc_get_preference('item_storage_folder', 'cacheplugin') ;
$PubbDate = osc_item_pub_date($item);
$DatePubb = date_create($PubbDate);
$cachePubbDate = date_format($DatePubb, $ItemStorageFolder);
$IdItem = osc_item_id($item);
$files = rglob(osc_get_preference('upload_path', 'cacheplugin')."item/".$cachePubbDate."/".$IdItem.".html");
foreach($files as $f) {
@unlink($f);
}
}
function cacheplugin_delete_item($id) {
$ItemStorageFolder = osc_get_preference('item_storage_folder', 'cacheplugin') ;
$PubbDate = osc_item_pub_date($id);
$DatePubb = date_create($PubbDate);
$cachePubbDate = date_format($DatePubb, $ItemStorageFolder);
$files = rglob(osc_get_preference('upload_path', 'cacheplugin')."item/".$cachePubbDate."/".$id.".html");
foreach($files as $f) {
@unlink($f);
}
$files = rglob(osc_get_preference('upload_path', 'cacheplugin')."main/*");
foreach($files as $f) {
@unlink($f);
}
}
function cacheplugin_posted_item(){
$files = rglob(osc_get_preference('upload_path', 'cacheplugin')."main/*");
foreach($files as $f) {
@unlink($f);
}
}
function cacheplugin_clear_item() {
$files = rglob(osc_get_preference('upload_path', 'cacheplugin')."item/");
foreach($files as $f) {
cacheplugin_recursiveRemove($f);
}
}
function cacheplugin_clear_static() {
$files = rglob(osc_get_preference('upload_path', 'cacheplugin')."static/*");
foreach($files as $f) {
@unlink($f);
}
}
function cacheplugin_clear_main() {
$files = rglob(osc_get_preference('upload_path', 'cacheplugin')."main/*");
foreach($files as $f) {
@unlink($f);
}
}
function cacheplugin_clear_all() {
cacheplugin_clear_item();
cacheplugin_clear_static();
cacheplugin_clear_main();
}
function cacheplugin_edit_comment($id) {
$conn = getConnection();
$ItemIds = $conn->osc_dbFetchResult("SELECT fk_i_item_id FROM %st_item_comment WHERE pk_i_id = %d", DB_TABLE_PREFIX, $id);
$ItemId = $ItemIds['fk_i_item_id'];
$PubbDate = osc_item_pub_date($ItemId['fk_i_item_id']);
$DatePubb = date_create($PubbDate);
$cachePubbDate = date_format($DatePubb, 'Y-m-d');
$files = rglob(osc_get_preference('upload_path', 'cacheplugin')."item/".$cachePubbDate."/".$ItemId.".html");
foreach($files as $f) {
@unlink($f);
}
}
// This can be removed once we no longer support Osclass 310 and older.
function cacheplugin_admin_menu_old() {
echo '<h3><a href="#">Cache Plugin</a></h3>
<ul>
<li><a href="' . osc_admin_render_plugin_url(osc_plugin_folder(__FILE__) . 'admin/conf.php') . '">» ' . __('Settings', 'cacheplugin') . '</a></li>
<li><a href="' . osc_admin_render_plugin_url(osc_plugin_folder(__FILE__) . 'admin/help.php') . '">» ' . __('Help', 'cacheplugin') . '</a></li>
</ul>';
}
function cacheplugin_admin_menu() {
osc_add_admin_submenu_divider( 'plugins', 'Cache Plugin', 'cacheplugin', $capability = null);
osc_admin_menu_plugins( __('Settings', 'cacheplugin'), osc_admin_render_plugin_url(osc_plugin_folder(__FILE__) . 'admin/conf.php'), 'cacheplugin-setings', $capability = null, $icon_url = null );
osc_admin_menu_plugins( __('Help', 'cacheplugin'), osc_admin_render_plugin_url(osc_plugin_folder(__FILE__) . 'admin/help.php'), 'cacheplugin-help', $capability = null, $icon_url = null );
}
function cacheplugin_admin_configure() {
osc_admin_render_plugin(osc_plugin_path(osc_plugin_folder(__FILE__)) . 'admin/conf.php') ;
}
/**
* ADD HOOKS
*/
osc_register_plugin(osc_plugin_path(__FILE__), 'cacheplugin_install');
osc_add_hook(osc_plugin_path(__FILE__)."_configure", 'cacheplugin_admin_configure');
osc_add_hook(osc_plugin_path(__FILE__)."_uninstall", 'cacheplugin_uninstall');
osc_add_hook(osc_plugin_path(__FILE__) . "_disable", 'cacheplugin_clear_all');
// hooks for create cache
osc_add_hook('before_html', 'cacheplugin_cache_start',8);
osc_add_hook('after_html', 'cacheplugin_cache_end',2);
// clear cahe after item actions
if(osc_version()<320) {
osc_add_hook('item_edit_post', 'cacheplugin_item_edit_post');
} else {
osc_add_hook('edited_item', 'cacheplugin_item_edit_post');
}
if (osc_get_preference('posted_item_clean_cache', 'cacheplugin')== 'active') {
if(osc_version()<320) {
osc_add_hook('item_form_post', 'cacheplugin_posted_item');
} else {
osc_add_hook('posted_item', 'cacheplugin_posted_item');
}
}
osc_add_hook('theme_activate', 'cacheplugin_clear_all'); // clear all cache when theme change
osc_add_hook('activate_item', 'cacheplugin_delete_item');
osc_add_hook('deactivate_item', 'cacheplugin_delete_item');
osc_add_hook('enable_item', 'cacheplugin_delete_item');
osc_add_hook('disable_item', 'cacheplugin_delete_item');
osc_add_hook('delete_item', 'cacheplugin_delete_item');
osc_add_hook('item_spam_on', 'cacheplugin_delete_item');
osc_add_hook('item_spam_off', 'cacheplugin_delete_item');
// clear cache after comment
osc_add_hook('add_comment', 'cacheplugin_add_comment');
osc_add_hook('activate_comment', 'cacheplugin_edit_comment');
osc_add_hook('deactivate_comment', 'cacheplugin_edit_comment');
osc_add_hook('enable_comment', 'cacheplugin_edit_comment');
osc_add_hook('disable_comment', 'cacheplugin_edit_comment');
osc_add_hook('delete_comment', 'cacheplugin_edit_comment');
// FANCY MENU
if(osc_version()<320) {
osc_add_hook('admin_menu', 'cacheplugin_admin_menu_old');
} else {
osc_add_hook('admin_menu_init', 'cacheplugin_admin_menu');
}
?>