help.inc.php
4.45 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
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
<?php
/**
* $Id$
*
* Copyright (c) 2006 Jam Warehouse http://www.jamwarehouse.com
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; using version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* -------------------------------------------------------------------------
*
* You can contact the copyright owner regarding licensing via the contact
* details that can be found on the KnowledgeTree web site:
*
* http://www.ktdms.com/
*/
/* 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 true;
}
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);
}
}
?>