help.inc.php
3.44 KB
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
<?php
/* help has changed significantly. see /help.php */
require_once(KT_LIB_DIR . "/database/dbutil.inc");
class KTHelp {
function getHelpSubPath($sHelpFile) {
if (empty($sHelpFile)) { return false; }
$path_segments = explode("/", $sHelpFile);
// cannot be empty, must contain at _least_ 1 item.
if (empty($path_segments[0])) {
$path_segments = array_slice($path_segments,1);
}
if (empty($path_segments) or (count($path_segments) < 2)) {
return false; // FIXME use PEAR::Error
}
// we now assume that path_segments[0] is the module
// path_segments[1..] is the subpath. we need to insert the LANG
$lang_code = 'EN'; // FIXME extract the lang from the environ (?)
$final_path = array(null,'kthelp', $path_segments[0]);
$final_path[] = $lang_code;
$final_path = array_merge($final_path, array_slice($path_segments, 1));
$help_path = implode('/',$final_path);
return $help_path;
}
function getHelpFromFile($sHelpFile) {
if (empty($sHelpFile)) { return false; }
$help_path = KTHelp::getHelpSubPath($sHelpFile);
$fspath = KT_DIR . $help_path; // FIXME use OS.path_sep equivalent?
if (!file_exists($fspath)) {
return false;
}
if (KTHelp::isImageFile($help_path)) {
return false; // can't - not what users expect.
}
// now we ASSUME its html: we'll fail anyway if we aren't.
$handle = fopen($fspath, "r");
$contents = fread($handle, filesize($fspath));
fclose($handle);
$info = KTHelp::_parseHTML($contents);
$body = KTUtil::arrayGet($info,'body');
if (empty($body)) {
return false;
}
$info['name'] = $help_path; // set so we can save into db if needed.
return $info;
}
// world's simplest (and possibly worst) regex-split.
function _parseHTML($sHTML) {
$title_array = preg_split('#</?title>#',$sHTML,-1,PREG_SPLIT_NO_EMPTY);
$body_array = preg_split('#</?body>#',$sHTML,-1,PREG_SPLIT_NO_EMPTY);
$res = array();
if (count($title_array) > 2) {
$res['title'] = $title_array[1];
}
if (count($body_array) > 2) {
$res['body'] = $body_array[1];
}
//var_dump($body_array);
return $res;
}
function isImageFile($sHelpPath) {
// from pluginutil.inc.php
$fspath = KT_DIR . $sHelpPath;
$pi = pathinfo($fspath);
$mime_type = "";
$sExtension = KTUtil::arrayGet($pi, 'extension');
if (!empty($sExtension)) {
$mime_type = DBUtil::getOneResultKey(array("SELECT mimetypes FROM " . KTUtil::getTableName('mimetypes') . " WHERE LOWER(filetypes) = ?", $sExtension), "mimetypes");
}
if (($mime_type == 'image/png') || ($mime_type == 'image/gif') || ($mime_type == 'image/jpeg')) {
}
return false;
}
function outputHelpImage($sHelpPath) {
$fspath = KT_DIR . $sHelpPath;
header("Content-Type: $mime_type");
header("Content-Length: " . filesize($fspath));
readfile($fspath); // does this output it?!
exit(0);
}
}
?>