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
|
<?php
namespace Bitweaver\Liberty;
use Bitweaver\KernelTools;
/**
* @version $Header$
* @package liberty
* @subpackage plugins_filter
*/
/*
* IMPORTANT - READ
* This filter is not ready for usage yet. it has to be re-written
* This is a collection of code from throughout bitweaver that was once part of this feature in the good old tikiwiki days
*
* other files related to this feature are:
* - kernel/view_cache.php
* - kernel/admin/list_cache.php
* - and their related tpl files
*
*
* Feel free to fix this stuff.
*
*
* Further down there are some functions that were pulled from BitSystem and
* need to go somewhere else.
* I'm not sure where the best place for this stuff is - perhaps it would be
* best to move all this stuff to a separate package - xing
*/
/**
* definitions ( guid character limit is 16 chars )
*/
define( 'PLUGIN_GUID_FILTERURLCACHE', 'filterurlcache' );
global $gLibertySystem;
$pluginParams = [
'title' => 'External Links Cache',
'description' => 'If you insert a link to an external page, this filter will proceede to cache that page to ensure that you can view the page, even if it moves or gets removed from the original location.',
'auto_activate' => false,
'plugin_type' => FILTER_PLUGIN,
// filter functions
'presplit_function' => '\Bitweaver\Liberty\urlcache_postparsefilter',
'postparse_function' => '\Bitweaver\Liberty\urlcache_postparsefilter',
];
$gLibertySystem->registerPlugin( PLUGIN_GUID_FILTERURLCACHE, $pluginParams );
function urlcache_links( $links, &$pCommonObject ) {
global $gBitSystem;
if( $gBitSystem->isFeatureActive( 'liberty_cache_pages' ) && $pCommonObject ) {
foreach( $links as $link ) {
if( !$pCommonObject->urlcache_is_cached( $link ) ) {
$pCommonObject->urlcache_store($link);
}
}
}
}
/**
* Check if given url is currently cached locally
*
* @param string URL to check
* @return integer Id of the cached item
* @todo LEGACY FUNCTIONS that need to be cleaned / moved / or deprecated & deleted
*/
function urlcache_is_cached($url) {
// return false until this is fixed
return false;
$query = "select `cache_id` from `".BIT_DB_PREFIX."liberty_link_cache` where `url`=?";
// sometimes we can have a cache_id of 0(?!) - seen it with my own eyes, spiderr
$ret = $this->mDb->getOne($query, [ $url ] );
return $ret;
}
/**
* Cache given url
* If \c $data present (passed) it is just associated \c $url and \c $data.
* Else it will request data for given URL and store it in DB.
* Actualy (currently) data may be proviced by TIkiIntegrator only.
* @param string URL to cache
* @param string Data to be cached
* @return bool True if item was successfully cached
* @todo LEGACY FUNCTIONS that need to be cleaned / moved / or deprecated & deleted
*/
function urlcache_store($url, $data = '') {
// Avoid caching internal references... (only if $data not present)
// (cdx) And avoid other protocols than http...
// 03-Nov-2003, by zaufi
// preg_match("_^(mailto:|ftp:|gopher:|file:|smb:|news:|telnet:|javascript:|nntp:|nfs:)_",$url)
// was removed (replaced to explicit http[s]:// detection) bcouse
// I now (and actualy use in my production Tiki) another bunch of protocols
// available in my konqueror... (like ldap://, ldaps://, nfs://, fish://...)
// ... seems like it is better to enum that allowed explicitly than all
// noncacheable protocols.
if (((strstr($url, 'tiki-') || strstr($url, 'messu-')) && $data == '')
|| (substr($url, 0, 7) != 'http://' && substr($url, 0, 8) != 'https://'))
return false;
// Request data for URL if nothing given in parameters
// (reuse $data var)
if( $data == '' ) {
$data = KernelTools::bit_http_request( $url );
}
// If stuff inside [] is *really* malformatted, $data
// will be empty. -rlpowell
if( !$this->isCached( $url ) && is_string($data)) {
global $gBitSystem;
$refresh = $gBitSystem->getUTCTime();
$query = "insert into `".BIT_DB_PREFIX."liberty_link_cache`(`url`,`data`,`refresh`) values(?,?,?)";
$result = $this->mDb->query($query, [ $url,BitDb::db_byte_encode($data),$refresh ] );
return !isset( $error );
}
return false;
}
function urlcache_postparsefilter( $pData, $pFilterHash ) {
$notcachedlinks = $this->get_links_nocache($data);
$cachedlinks = array_diff($links, $notcachedlinks);
$this->cache_links($cachedlinks,$pCommonObject);
// prepare link for pattern usage
$link2 = str_replace("/", "\/", preg_quote($link));
//use of urlencode for using cached versions of dynamic sites
$cosa = "<a class=\"bitcache\" href=\"".KERNEL_PKG_URL."view_cache.php?url=".urlencode($link)."\">(cache)</a>";
$pattern = "/(?<!\[)\[$link2\|([^\]\|]+)\|([^\]]+)\]/";
$data = preg_replace($pattern, "<a $class href='$link'>$1</a>", $data);
$pattern = "/(?<!\[)\[$link2\|([^\]\|]+)\]/";
$data = preg_replace($pattern, "<a $class href='$link'>$1</a> $cosa", $data);
$pattern = "/(?<!\[)\[$link2\]/";
$data = preg_replace($pattern, "<a $class href='$link'>$link</a> $cosa", $data);
}
// functions pulled from BitSystem
function list_cache($offset, $max_records, $sort_mode, $find) {
if ($find) {
$findesc = '%' . $find . '%';
$mid = " where (`url` like ?) ";
$bindvars=[$findesc];
} else {
$mid = "";
$bindvars=[];
}
$query = "select `cache_id` ,`url`,`refresh` from `".BIT_DB_PREFIX."liberty_link_cache` $mid order by ".$this->mDb->convertSortmode($sort_mode);
$query_cant = "select count(*) from `".BIT_DB_PREFIX."liberty_link_cache` $mid";
$result = $this->mDb->query($query,$bindvars,$max_records,$offset);
$cant = $this->mDb->getOne($query_cant,$bindvars);
$ret = [];
while ($res = $result->fetchRow()) {
$ret[] = $res;
}
$retval = [];
$retval["data"] = $ret;
$retval["cant"] = $cant;
return $retval;
}
function refresh_cache($cache_id) {
global $gBitSystem;
$query = "select `url` from `".BIT_DB_PREFIX."liberty_link_cache`
where `cache_id`=?";
$url = $this->mDb->getOne($query, [ $cache_id ] );
$data = KernelTools::bit_http_request($url);
$refresh = $gBitSystem->getUTCTime();
$query = "update `".BIT_DB_PREFIX."liberty_link_cache`
set `data`=?, `refresh`=?
where `cache_id`=? ";
$result = $this->mDb->query($query, [ $data, $refresh, $cache_id ] );
return true;
}
function remove_cache($cache_id) {
$query = "delete from `".BIT_DB_PREFIX."liberty_link_cache` where `cache_id`=?";
$result = $this->mDb->query($query, [ $cache_id ] );
return true;
}
function get_cache($cache_id) {
$query = "select * from `".BIT_DB_PREFIX."liberty_link_cache`
where `cache_id`=?";
$result = $this->mDb->query($query, [ $cache_id ] );
$res = $result->fetchRow();
return $res;
}
|