forked from mvdkleijn/funky_cache
/
index.php
145 lines (109 loc) · 4.66 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
<?php
/*
* Funky Cache plugin for Wolf CMS. <http://www.wolfcms.org>
*
* Copyright (C) 2012 Martijn van der Kleijn <martijn.niji@gmail.com>
* Copyright (c) 2008-2009 Mika Tuupola
*
* This file is part of the Funky Cache plugin for Wolf CMS. It is licensed
* under the MIT license.
*
* For details, see: http://www.opensource.org/licenses/mit-license.php
*/
//require_once 'models/FunkyCachePage.php';
Plugin::setInfos(array(
'id' => 'funky_cache',
'title' => 'Funky Cache',
'description' => 'Enables funky caching, making your site ultra fast.',
'version' => '0.4.1',
'license' => 'MIT',
'author' => 'Mika Tuupola & Martijn van der Kleijn',
'require_wolf_version' => '0.7.5+',
//'update_url' => 'http://www.appelsiini.net/download/frog-plugins.xml',
'website' => 'http://vanderkleijn.net/'
));
AutoLoader::addFolder(PLUGINS_ROOT.'/funky_cache/models');
/* Stuff for backend. */
if (defined('CMS_BACKEND')) {
AutoLoader::addFolder(dirname(__FILE__).'/lib');
Plugin::addController('funky_cache', __('Cache'), "admin_view", true);
#Observer::observe('page_edit_after_save', 'funky_cache_delete_one');
Observer::observe('page_edit_after_save', 'funky_cache_delete_all');
Observer::observe('page_add_after_save', 'funky_cache_delete_all');
Observer::observe('page_delete', 'funky_cache_delete_all');
Observer::observe('view_page_edit_plugins', 'funky_cache_show_select');
Observer::observe('comment_after_add', 'funky_cache_delete_all');
Observer::observe('comment_after_edit', 'funky_cache_delete_all');
Observer::observe('comment_after_delete', 'funky_cache_delete_all');
Observer::observe('comment_after_approve', 'funky_cache_delete_all');
Observer::observe('comment_after_unapprove', 'funky_cache_delete_all');
Observer::observe('layout_after_edit', 'funky_cache_delete_all');
Observer::observe('snippet_after_edit', 'funky_cache_delete_all');
/* TODO Fix this to work with configurable cache folder. */
function funky_cache_delete_one($page) {
$data['url'] = '/'.$page->getUri().URL_SUFFIX;
if (($cache = FunkyCachePage::findOneFrom('FunkyCachePage', 'url=?', array($data['url'])))) {
$cache->delete();
}
}
function funky_cache_delete_all() {
$cache = FunkyCachePage::findAllFrom('FunkyCachePage');
foreach ($cache as $page) {
$page->delete();
}
$message = sprintf('Cache was automatically cleared.');
Observer::notify('log_event', $message, 'funky_cache', 7);
}
function funky_cache_show_select($page) {
$enabled = isset($page->funky_cache_enabled) ?
$page->funky_cache_enabled : funky_cache_by_default();
print '
<p><label for="page_funky_cache_enabled">'.__('Should cache').'</label>
<select id="page_funky_cache_enabled" name="page[funky_cache_enabled]">
<option value="0"'.($enabled == 0 ? ' selected="selected"' : '').'>'.__('No').'</option>
<option value="1"'.($enabled == 1 ? ' selected="selected"' : '').'>'.__('Yes').'</option>
</select>
</p>';
}
}
else {
/* Stuff for frontend. */
Observer::observe('page_found', 'funky_cache_create');
//Observer::observe('page_requested', 'funky_cache_debug');
function funky_cache_debug($page) {
if (DEBUG) {
print "Cache miss... ";
}
}
function funky_cache_create($page) {
if ($page->funky_cache_enabled) {
$data['url'] = URI_PUBLIC.CURRENT_URI.URL_SUFFIX;
// Correct URL for frontpage - should become index.html
if ($data['url'] == URI_PUBLIC.URL_SUFFIX) {
$data['url'] = URI_PUBLIC.'index'.funky_cache_suffix();
}
$data['url'] = funky_cache_folder().$data['url'];
$data['url'] = preg_replace('#//#', '/', $data['url']);
$data['page'] = $page;
if (!($cache = FunkyCachePage::findOneFrom('FunkyCachePage', 'url=?', array($data['url'])))) {
$cache = new FunkyCachePage($data);
}
$cache->page = $page;
$cache->save();
}
}
}
function funky_cache_suffix() {
return Plugin::getSetting('funky_cache_suffix', 'funky_cache');
}
function funky_cache_by_default() {
return Plugin::getSetting('funky_cache_by_default', 'funky_cache');
}
function funky_cache_folder() {
$folder = '/'.Plugin::getSetting('funky_cache_folder', 'funky_cache').'/';
$folder = preg_replace('#//*#', '/', $folder);
return $folder;
}
function funky_cache_folder_is_root() {
return '/' == funky_cache_folder();
}