ktutil.inc
来自「PHP 知识管理系统(基于树结构的知识管理系统), 英文原版的PHP源码。」· INC 代码 · 共 1,334 行 · 第 1/3 页
INC
1,334 行
{
$script = $stackPath . '/java/jre/bin/java.exe';
}
else
{
$script = $stackPath . '/java/jre/bin/java';
}
break;
case 'externalBinary/df':
if (OS_WINDOWS)
{
$script = $stackPath . '/bin/gnuwin32/df.exe';
}
else
{
$script = $stackPath . '/common/bin/df';
}
break;
case 'externalBinary/php':
if (OS_WINDOWS)
{
$script = $stackPath . '/php/php.exe';
}
else
{
$script = $stackPath . '/php/bin/php';
}
break;
default:
return null;
}
if (is_file($script))
{
return $script;
}
return false;
}
// now accepts strings OR arrays!
// {{{ addQueryString
static function addQueryString($url, $qs) {
require_once(KT_DIR . '/thirdparty/pear/Net/URL.php');
$oUrl = new Net_URL($url);
$oKTConfig =& KTConfig::getSingleton();
if ($oKTConfig->get("KnowledgeTree/sslEnabled")) {
$oUrl->protocol = 'https';
if ($oUrl->port == 80) {
$oUrl->port = 443;
}
}
$host = $oKTConfig->get("KnowledgeTree/serverName");
$host = explode(':', $host);
$oUrl->host = $host[0];
if(!is_array($qs)) {
$aQs = $oUrl->_parseRawQuerystring($qs);
} else {
$aQs =& $qs;
}
foreach ($aQs as $k => $v) {
$oUrl->addQueryString($k, $v, true);
}
return $oUrl->getUrl();
}
// }}}
// {{{ ktLink
function ktLink($base, $subpath='', $qs='') {
$KTConfig =& KTConfig::getSingleton();
$root = $KTConfig->get("KnowledgeTree/rootUrl");
$url = generateLink($base);
if (!empty($subpath)) {
$hasPathInfo = $KTConfig->get("KnowledgeTree/pathInfoSupport");
if ($hasPathInfo) {
$url .= $subpath;
} else {
$url = KTUtil::addQueryString($url, "kt_path_info=" . $subpath);
}
}
return KTUtil::addQueryString($url, $qs);
}
// }}}
// {{{ addQueryStringSelf
static function addQueryStringSelf($qs) {
return KTUtil::addQueryString($_SERVER['PHP_SELF'], $qs);
}
// }}}
// {{{ isAbsolutePath
static function isAbsolutePath($sPath) {
$sPath = str_replace('\\', '/', $sPath);
$sReal = str_replace('\\', '/', realpath($sPath));
if(substr($sPath, -1, 1) == '/' && substr($sReal, -1, 1) != '/'){
$sReal .= '/';
}
return (strtolower($sReal) == strtolower($sPath));
if (substr($sPath, 0, 1) == '/') {
return true;
}
if (OS_WINDOWS && (substr($sPath, 1, 2) == ':/')) {
return true;
}
if (OS_WINDOWS && (substr($sPath, 1, 2) == ':\\')) {
return true;
}
return false;
}
// }}}
// {{{ formatPlainText
/* formats input text for discussion body. replaces newlines
with <br/> tags to allow for paragraphing. should also strip
html elements.
*/
function formatPlainText($sText) {
return str_replace("\n", '<br/>', str_replace("\r\n", '<br/>', trim($sText)));
}
// }}}
// getBenchmarkTime
function getBenchmarkTime () {
$microtime_simple = explode(' ', microtime());
return ((float) $microtime_simple[1] + (float) $microtime_simple[0]);
}
function phraseSplit($sSearchString) {
// this should probably be moved to a DBUtil method
$sMinWord = DBUtil::getOneResultKey("SHOW VARIABLES LIKE 'ft_min_word_len'", "Value");
if(is_numeric($sMinWord)) {
$iMinWord = (int)$sMinWord;
} else {
$iMinWord = 4;
}
$a = preg_split('#"#', $sSearchString);
$i = 0;
$phrases = array();
$word_parts = array();
foreach ($a as $part) {
if ($i%2 == 0) {
$word_parts[] = $part;
} else {
$phrases[] = $part;
}
$i += 1;
}
$oStopwords =& KTStopwords::getSingleton();
$words = array();
foreach ($word_parts as $part) {
$w = (array) explode(' ', $part);
foreach ($w as $potential) {
if (strlen($potential) >= $iMinWord && !$oStopwords->isStopword($potential)) {
$words[] = $potential;
}
}
}
return array(
'words' => $words,
'phrases' => $phrases,
);
}
function phraseQuote($sQuery) {
foreach(KTUtil::phraseSplit($sQuery) as $k => $v) {
$t = array();
foreach ($v as $part) {
$t[] = sprintf('+"%s"', $part);
}
$q_set[$k] = join(' ', $t);
}
return implode(' ',$q_set);
}
static function running_user() {
if (substr(PHP_OS, 0, 3) == "WIN") {
return null;
}
if (extension_loaded("posix")) {
$uid = posix_getuid();
$userdetails = posix_getpwuid($uid);
return $userdetails['name'];
}
if (file_exists('/usr/bin/whoami')) {
return exec('/usr/bin/whoami');
}
if (file_exists('/usr/bin/id')) {
return exec('/usr/bin/id -nu');
}
return null;
}
static function getSystemSetting($name, $default = null) {
// XXX make this use a cache layer?
$sTable = KTUtil::getTableName('system_settings');
$aQuery = array(
sprintf('SELECT value FROM %s WHERE name = ?', $sTable),
array($name),
);
$res = DBUtil::getOneResultKey($aQuery, 'value');
if (PEAR::isError($res)) {
if(!is_null($default)){
return $default;
}
return PEAR::raiseError(sprintf(_kt('Unable to retrieve system setting %s: %s'), $name, $res->getMessage()));
}
if (is_null($res)) { return $default; }
return $res;
}
function setSystemSetting($name, $value) {
// we either need to insert or update:
$sTable = KTUtil::getTableName('system_settings');
$current_value = KTUtil::getSystemSetting($name);
if (is_null($current_value)) {
// insert
$res = DBUtil::autoInsert(
$sTable,
array(
'name' => $name,
'value' => $value,
),
null // opts
);
if (PEAR::isError($res)) { return $res; }
else { return true; }
} else {
// update
$aQuery = array(
sprintf('UPDATE %s SET value = ? WHERE name = ?', $sTable),
array($value, $name),
);
$res = DBUtil::runQuery($aQuery);
if (PEAR::isError($res)) { return $res; }
return true;
}
}
function getSystemIdentifier() {
$sIdentifier = KTUtil::getSystemSetting('kt_system_identifier');
if (empty($sIdentifier)) {
$sIdentifier = md5(uniqid(mt_rand(), true));
KTUtil::setSystemSetting('kt_system_identifier', $sIdentifier);
}
return $sIdentifier;
}
function getKTVersions() {
$aVersions = array();
$sProfessionalFile = KT_DIR . '/docs/VERSION-PRO.txt';
$sOssFile = KT_DIR . '/docs/VERSION-OSS.txt';
$sDevProfessionalFile = KT_DIR . '/docs/VERSION-PRO-DEV.txt';
$sDevOssFile = KT_DIR . '/docs/VERSION-OSS-DEV.txt';
if (file_exists($sDevProfessionalFile)) {
$sVersion = trim(file_get_contents($sDevProfessionalFile));
$aVersions['Development Commercial'] = $sVersion;
} elseif (file_exists($sDevOssFile)) {
$sVersion = trim(file_get_contents($sDevOssFile));
$aVersions['Development OSS'] = $sVersion;
} elseif (file_exists($sProfessionalFile)) {
$sVersion = trim(file_get_contents($sProfessionalFile));
$aVersions['Commercial Edition'] = $sVersion;
} elseif (file_exists($sOssFile)) {
$sVersion = trim(file_get_contents($sOssFile));
$aVersions['OSS'] = $sVersion;
} else {
$aVersions['ERR'] = "Unknown version";
}
return $aVersions;
}
// this will have to move somewhere else
function buildSelectOptions($aVocab, $cur = null) {
$sRet = '';
foreach($aVocab as $k=>$v) {
$sRet .= '<option value="' . $k . '"';
if($k == $cur) $sRet .= ' selected="selected"';
$sRet .= '>' . $v . '</option>';
}
return $sRet;
}
function keyArray($aEntities, $sIdFunc = 'getId') {
$aRet = array();
foreach ($aEntities as $oEnt) {
$meth = array(&$oEnt, $sIdFunc);
$id = call_user_func($meth);
$aRet[$id] = $oEnt;
}
return $aRet;
}
/**
* Generates breadcrumbs for a browsable collection
*
* @param object $oFolder The folder being browsed to
* @param integer $iFolderId The id of the folder
* @param array $aURLParams The url parameters for each folder/breadcrumb link
* @return unknown
*/
function generate_breadcrumbs($oFolder, $iFolderId, $aURLParams) {
static $aFolders = array();
static $aBreadcrumbs = array();
// Check if selected folder is a parent of the current folder
if(in_array($iFolderId, $aFolders)){
$temp = array_flip($aFolders);
$key = $temp[$iFolderId];
array_splice($aFolders, $key);
array_splice($aBreadcrumbs, $key);
return $aBreadcrumbs;
}
// Check for the parent of the selected folder unless its the root folder
$iParentId = $oFolder->getParentID();
if(is_null($iParentId)){
// folder is root
return '';
}
if($iFolderId != 1 && in_array($iParentId, $aFolders)){
$temp = array_flip($aFolders);
$key = $temp[$iParentId];
array_splice($aFolders, $key);
array_splice($aBreadcrumbs, $key);
array_push($aFolders, $iFolderId);
$aParams = $aURLParams;
$aParams['fFolderId'] = $iFolderId;
$url = KTUtil::addQueryString($_SERVER['PHP_SELF'], $aParams);
$aBreadcrumbs[] = array('url' => $url, 'name' => $oFolder->getName());
return $aBreadcrumbs;
}
// Get the root folder name
$oRootFolder = Folder::get(1);
$sRootFolder =$oRootFolder->getName();
// Create the breadcrumbs
$folder_path_names = $oFolder->getPathArray();
array_unshift($folder_path_names, $sRootFolder);
$folder_path_ids = explode(',', $oFolder->getParentFolderIds());
$folder_path_ids[] = $oFolder->getId();
if ($folder_path_ids[0] == 0) {
array_shift($folder_path_ids);
array_shift($folder_path_names);
}
$iCount = count($folder_path_ids);
$range = range(0, $iCount - 1);
foreach ($range as $index) {
$id = $folder_path_ids[$index];
$name = $folder_path_names[$index];
$aParams = $aURLParams;
$aParams['fFolderId'] = $id;
$url = KTUtil::addQueryString($_SERVER['PHP_SELF'], $aParams);
$aBreadcrumbs[] = array('url' => $url, 'name' => $name);
}
$aFolders = $folder_path_ids;
return $aBreadcrumbs;
}
/**
* Generates the correct headers for downloading a document
*/
function download($path, $mimeType, $fileSize, $fileName, $displayType = 'attachment')
{
if (file_exists($path)) {
// IE7 adds underscores to filenames: fix
// IE6 displays characters incorrectly
$browser = $_SERVER['HTTP_USER_AGENT'];
if ( strpos( strtoupper( $browser), 'MSIE') !== false) {
$fileName = rawurlencode($fileName);
}
// Set the correct headers
header("Content-Type: {$mimeType}");
header("Content-Length: {$fileSize}");
header("Content-Disposition: {$displayType}; filename=\"{$fileName}\"");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
// Document must be cached in order to open it
header("Cache-Control: must-revalidate");
// Allows the file to be downloaded, otherwise it attempts the download as "action.php?..."
header("Pragma: public");
readfile($path);
}else {
return false;
}
}
}
/**
*
* Merges two arrays using array_merge
*
* array_merge in PHP5 got more strict about its parameter handling,
* forcing arrays.
*
*/
if (version_compare(phpversion(), '5.0') === -1) {
function kt_array_merge() {
$args = func_get_args();
return call_user_func_array("array_merge",$args);
}
} else {
eval('
function kt_array_merge() {
$args = func_get_args();
foreach ($args as &$arg) {
$arg = (array)$arg;
}
return call_user_func_array("array_merge",$args);
}
');
}
?>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?