help.inc.php
7.91 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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<?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 {
// 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;
}
// $sFSPath : filesystem path for the resource.
// return true or false
function isImageFile($sFSPath) {
$pi = pathinfo($sFSPath);
$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;
}
// output the help image referred to by the subpath
function outputHelpImage($sSubPath) {
// FIXME there are error cases here ...
$aPathInfo = KTHelp::_getLocationInfo($sSubPath);
if (PEAR::isError($aPathInfo)) { return $aPathInfo; } // gets caught further up the stack
$pi = pathinfo($aPathInfo['external']);
$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");
}
header("Content-Type: $mime_type");
header("Content-Length: " . filesize($fspath));
readfile($fspath); // does this output it?!
exit(0);
}
/*
input:
sSubPath : the path in the form "plugin/lower-level-file"
sLangCode : the language code in the form "en_US"
returns a dictionary
{
'is_image': string
'title': string
'body': string
}
*/
function getHelpInfo($sSubPath, $sLangCode = null) {
$aInfo = array(
'is_image' => false,
'title' => null,
'body' => null,
'help_id' => null,
'name' => null,
);
$aPathInfo = KTHelp::_getLocationInfo($sSubPath, $sLangCode);
if (PEAR::isError($aPathInfo)) {
return $aPathInfo;
}
// first, check the extension to see if its an image.
// failing that, check the DB for an entry (+ return if found)
// failing that, check the FS for the entry (+ return if found)
// failing that, through an exception.
if (KTHelp::isImageFile($aPathInfo['external'])) {
$aInfo['is_image'] = true;
return $aInfo;
}
// check DB
$oReplacement =& KTHelpReplacement::getByName($aPathInfo['internal']);
if (!PEAR::isError($oReplacement)) {
$aInfo['title'] = $oReplacement->getTitle();
$aInfo['body'] = $oReplacement->getDescription();
$aInfo['help_id'] = $oReplacement->getID();
$aInfo['name'] = $oReplacement->getName();
return $aInfo;
}
// check FS
if (!file_exists($aPathInfo['external'])) {
return PEAR::raiseError(_kt("Unable to locate the requested help file for this language."));
}
// so it might exist in some form.
$contents = file_get_contents($aPathInfo['external']);
$aData = KTHelp::_parseHTML($contents);
$aInfo['body'] = KTUtil::arrayGet($aData,'body');
if (empty($aInfo['body'])) {
return PEAR::raiseError(_kt("The requested help language has no contents."));
}
$aInfo['title'] = KTUtil::arrayGet($aData, 'title', _kt("Untitled Help File"));
$aInfo['name'] = $aPathInfo['internal'];
return $aInfo;
}
function _getLocationInfo($sSubPath, $sLangCode = null) {
// FIXME use a cheap cache here? is it even worth it?
$aInfo = array(
'subpath' => null,
'internal' => null,
'external' => null,
);
$oHelpReg =& KTHelpRegistry::getSingleton();
if (is_null($sLangCode)) {
global $default;
$sLangCode = $default->defaultLanguage;
}
$aParts = explode('/', $sSubPath);
if (count($aParts) < 2) {
return PEAR::raiseError(_kt("Too few parts to the requested help location."));
}
$sPluginName = $aParts[0];
$sSubLocation = implode('/', array_slice($aParts, 1));
$sInternalName = sprintf("%s/%s/%s", $sPluginName, $sLangCode, $sSubLocation);
// this is a pseudo-name. essentially, this maps to the canonical
// name of the help file in the database, NOT to the filesystem
//$sBaseDir = sprintf("%s/kthelp/%s/%s", KT_DIR, $sPluginName, $sLangCode);
$sBaseDir = $oHelpReg->getBaseDir($sPluginName, $sLangCode);
if (PEAR::isError($sBaseDir)) { return $sBaseDir; }
$sExternalName = sprintf("%s/%s", $sBaseDir, $sSubLocation);
$aInfo['subpath'] = $sSubPath;
$aInfo['internal'] = $sInternalName;
$aInfo['external'] = $sExternalName;
return $aInfo;
}
}
class KTHelpRegistry {
var $plugin_lang_map;
function KTHelpRegistry() {
$this->plugin_lang_map = array();
}
function &getSingleton () {
if (!KTUtil::arrayGet($GLOBALS['_KT_PLUGIN'], 'oKTHelpRegistry')) {
$GLOBALS['_KT_PLUGIN']['oKTHelpRegistry'] = new KTHelpRegistry;
}
return $GLOBALS['_KT_PLUGIN']['oKTHelpRegistry'];
}
function registerHelp($sPluginName, $sLang, $sBaseDir) {
$lang_map = KTUtil::arrayGet($this->plugin_lang_map, $sPluginName, array());
$lang_map[$sLang] = $sBaseDir;
$this->plugin_lang_map[$sPluginName] = $lang_map;
}
function getBaseDir($sPluginName, $sLangCode) {
$lang_map = KTUtil::arrayGet($this->plugin_lang_map, $sPluginName);
if (is_null($lang_map)) {
return PEAR::raiseError(_kt("There is no help available in your language for this plugin"));
}
$sBaseDir = KTUtil::arrayGet($lang_map, $sLangCode);
if (is_null($sBaseDir)) {
return PEAR::raiseError(_kt("There is no help available in your language for this plugin"));
}
return $sBaseDir;
}
}
?>