forked from joepie91/cphp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
include.lib.php
99 lines (82 loc) · 2.71 KB
/
include.lib.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
<?php
/*
* CPHP is more free software. It is licensed under the WTFPL, which
* allows you to do pretty much anything with it, without having to
* ask permission. Commercial use is allowed, and no attribution is
* required. We do politely request that you share your modifications
* to benefit other developers, but you are under no enforced
* obligation to do so :)
*
* Please read the accompanying LICENSE document for the full WTFPL
* licensing text.
*/
if($_CPHP !== true) { die(); }
if(!isset($cphp_config->libraries))
{
/* No library configuration has been specified. We don't need to
* execute the rest of this file. */
return;
}
/* Some libraries want to have a cache directory. Instead of storing all
* this stuff in the document root, we'll put it into /tmp (or any other
* config-specified directory). We should probably make sure the needed
* directories exist, though... */
$tmp_dir = (isset($cphp_config->tmp_dir)) ? $cphp_config->tmp_dir : "/tmp";
if(!file_exists("{$tmp_dir}/cphp"))
{
mkdir("{$tmp_dir}/cphp", 0700);
}
if(!file_exists("{$tmp_dir}/cphp/cache"))
{
mkdir("{$tmp_dir}/cphp/cache", 0700);
}
/* We'll set up HTMLPurifier here if so desired. */
if(isset($cphp_config->libraries->htmlpurifier))
{
if(!is_dir("{$tmp_dir}/cphp/cache/htmlpurifier"))
{
mkdir("{$tmp_dir}/cphp/cache/htmlpurifier", 0700);
}
$library_config = $cphp_config->libraries->htmlpurifier;
$library_path = (isset($library_config->path)) ? $library_config->path : "lib/htmlpurifier/HTMLPurifier.auto.php";
require_once($library_path);
$purifier_config = HTMLPurifier_Config::createDefault();
if(isset($library_config->doctype))
{
$purifier_config->set("HTML.Doctype", $library_config->doctype);
}
if(isset($library_config->encoding))
{
$purifier_config->set("Core.Encoding", $library_config->encoding);
}
$purifier_config->set("Cache.SerializerPath", "{$tmp_dir}/cphp/cache/htmlpurifier");
$cphp_purifier = new HTMLPurifier($purifier_config);
$cphp_hash_purifier_config = md5(serialize($purifier_config));
function purify_html($input, $cache_duration = 3600, $config = null)
{
if(isset($config))
{
$config->set("Cache.SerializerPath", "{$tmp_dir}/cphp/cache/htmlpurifier");
$cphp_purifier = new HTMLPurifier($config);
$hash_config = md5(serialize($config));
}
else
{
global $cphp_purifier;
global $cphp_hash_purifier_config;
$hash_config = $cphp_hash_purifier_config;
}
$hash_input = md5($input) . md5($input . "x");
$memcache_key = "purify_{$hash_config}_{$hash_input}";
if($result = mc_get($memcache_key))
{
return $result;
}
else
{
$result = $cphp_purifier->purify($input);
mc_set($memcache_key, $result, $cache_duration);
return $result;
}
}
}