ktutil.inc.svn-base
来自「PHP 知识管理系统(基于树结构的知识管理系统), 英文原版的PHP源码。」· SVN-BASE 代码 · 共 1,334 行 · 第 1/3 页
SVN-BASE
1,334 行
<?php/** * $Id$ * * Small non-domain-specific utility functions * * KnowledgeTree Community Edition * Document Management Made Simple * Copyright (C) 2008 KnowledgeTree Inc. * Portions copyright The Jam Warehouse Software (Pty) Limited * * This program is free software; you can redistribute it and/or modify it under * the terms of the GNU General Public License version 3 as published by the * Free Software Foundation. * * 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, see <http://www.gnu.org/licenses/>. * * You can contact KnowledgeTree Inc., PO Box 7775 #87847, San Francisco, * California 94120-7775, or email info@knowledgetree.com. * * The interactive user interfaces in modified source and object code versions * of this program must display Appropriate Legal Notices, as required under * Section 5 of the GNU General Public License version 3. * * In accordance with Section 7(b) of the GNU General Public License version 3, * these Appropriate Legal Notices must retain the display of the "Powered by * KnowledgeTree" logo and retain the original copyright notice. If the display of the * logo is not reasonably feasible for technical reasons, the Appropriate Legal Notices * must display the words "Powered by KnowledgeTree" and retain the original * copyright notice. * Contributor( s): ______________________________________ */require_once(KT_LIB_DIR . '/util/KTStopwords.php');class KTUtil { const MIN_IN_SECS = 60; const HOUR_IN_SECS = 3600; const DAY_IN_SECS = 86400; const KB = 1024; const MB = 1048576; const GB = 1000000000; const TB = 1099511627776; const PB = 1125899906842624; /** * Used to resolve the server name * */ static function save_base_kt_url() { global $default; $serverName = KTUtil::getSystemSetting('server_name', false); if($serverName !== false && !empty($serverName)){ return $serverName; } $rootUrl = $default->rootUrl; $protocol = $default->sslEnabled ? 'https' : 'http'; $port = $_SERVER['SERVER_PORT']+0; $serverName = $_SERVER['SERVER_NAME']; // We don't want to set the servername to localhost, it should be set to the url that will be used normally if($serverName == 'localhost' || $serverName == '127.0.0.1') { return false; } $base_url = $protocol . '://' . $serverName; if (($protocol == 'http' && $port == 80) || ($protocol == 'https' && $port == 443)) { // don't need to do anything } else { $base_url .= ':' . $port; } // Add the root url $base_url .= $rootUrl; // Save as system setting KTUtil::setSystemSetting('server_name', $base_url); } static function kt_url() { global $default; static $base_url = null; if (!is_null($base_url)) { return $base_url; } $config = KTConfig::getSingleton(); $serverName = $config->get('knowledgeTree/serverName', $_SERVER['HTTP_HOST']); // $serverName gets set in dmsDefaults, if $_SERVER['HTTP_HOST'] is empty, it gets set to localhost // we want to avoid this if possible if(empty($serverName) || $serverName == 'localhost') { // The host has not been set - check if the serverName setting exists $base_url = KTUtil::getSystemSetting('server_name', false); if (false !== $base_url && !empty($base_url)) { /* We are checking if the object exists because we could have an error pre or during initialization */ if(is_object($default->log)) { $default->log->debug("kt_url: base url - $base_url"); } $base_url = str_replace(array("\n","\r"), array('',''), $base_url); return $base_url; } // TODO: Check if the $_SERVER['SERVER_NAME'] variable is set. $serverName = 'localhost'; } // build up the url $base_url = ($default->sslEnabled ? 'https' : 'http') .'://'.$serverName . $default->rootUrl; if(is_object($default->log)) { $default->log->debug("kt_url: base url - $base_url"); } return $base_url; } static function call_page($path) { global $default; $base_url = KTUtil::getSystemSetting('server_name', false); if (false === $base_url || empty($base_url)) { $default->log->info("call_page: $path - cannot call script until user logs in from a url other than localhost or 127.0.0.1!"); return; } $kt_url = KTUtil::kt_url(); $full_url = $kt_url . '/' . $path; $default->log->debug("call_page: calling $full_url"); $ch = curl_init($full_url); curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch,CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch,CURLOPT_FOLLOWLOCATION, true); curl_exec($ch); curl_close($ch); } static function computePeriod($diff, $suffix = null, $returnArray=false) { if (is_null($suffix)) { $suffix = _kt('ago'); } $days = floor($diff / KTUtil::DAY_IN_SECS); $hours = floor(($diff - $days * KTUtil::DAY_IN_SECS) / KTUtil::HOUR_IN_SECS); $mins = floor(($diff - $days * KTUtil::DAY_IN_SECS - $hours * KTUtil::HOUR_IN_SECS) / KTUtil::MIN_IN_SECS); $secs = $diff % KTUtil::MIN_IN_SECS; $str = ''; if ($days > 0) $str .= sprintf(_kt(' %d day(s)'), $days); if ($hours > 0) $str .= sprintf(_kt(' %d hour(s)'), $hours); if ($mins > 0) $str .= sprintf(_kt(' %d minute(s)'), $mins); // lets loose some granularity. the string does get quite long... if ($days == 0 && $hours == 0 && $mins == 0 && $secs > 0) { $str .= sprintf(_kt(' %d second(s)'), $secs); } if (empty($str)) { return _kt('never'); } $str .= " $suffix"; if ($returnArray) { return array( 'str'=>$str, 'days'=>$days, 'mins'=>$mins, 'secs'=>$secs ); } return $str; } static function computePeriodToDate($start, $suffix = null, $returnArray=false) { return KTUtil::computePeriod(time() - $start, $suffix, $returnArray); } static function filesizeToString($filesize) { $filesize = (double) $filesize; if ($filesize >= KTUtil::PB) { return number_format($filesize / KTUtil::PB, 2, '.',',') . _kt('PB'); } elseif ($filesize >= KTUtil::TB) { return number_format($filesize / KTUtil::TB, 2, '.',',') . _kt('TB'); } elseif ($filesize >= KTUtil::GB) { return number_format($filesize / KTUtil::GB, 2, '.',',') . _kt('GB'); } elseif ($filesize >= KTUtil::MB) { return number_format($filesize / KTUtil::MB, 2, '.',',') . _kt('MB'); } elseif ($filesize >= KTUtil::KB) { return number_format($filesize / KTUtil::KB, 2, '.',',') . _kt('KB'); } else { return $filesize . _kt('B'); } } static $invalidFilenameCharacters = array('\\','/',':','*','?','"','<','>','|','%','+','\'','`'); /** * Checks if a filename is valid * * @param string $filename * @return boolean */ static function isValidFilename($filename) { foreach(KTUtil::$invalidFilenameCharacters as $char) { if (strpos($filename, $char) !== false) { return false; } } return true; } static function replaceInvalidCharacters($filename) { foreach(KTUtil::$invalidFilenameCharacters as $char) { $filename = str_replace($char, '-', $filename); } return $filename; } function extractGPC () { foreach (func_get_args() as $var) { if (array_key_exists($var, $_REQUEST)) { $GLOBALS["$var"] = $_REQUEST["$var"]; } } } function strToBool ($sString, $null = false, $empty = false) { $sString = strtoupper($sString); if (in_array($sString, array('Y','YES','ON','TRUE'))) { return true; } elseif (in_array($sString, array('N', 'NO','OFF','FALSE'))) { return false; } elseif ($sString == '') { return $empty; } else { return $null; } } function intToBool ($sString) { $iInt = (int)$sString; return $iInt !== 0; } function anyToBool ($sString, $null = false) { if (is_bool($sString)) { return $sString; } if (is_numeric($sString)) { return KTUtil::intToBool($sString); } if (is_string($sString)) { if (KTUtil::strToBool($sString) === true) { return true; } } if (is_null($sString)) { return $null; } return false; } function randomString($length=16, $sRandom="ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"){ $sString = ""; $iCurLen = 0; $iRandomLen = strlen($sRandom); while ($length > $iCurLen) { $sString .= substr($sRandom, mt_rand(0, $iRandomLen -1), 1); $iCurLen++; } return $sString; } //this function fudges the strlen. It returns a ? when the character is a multi-byte character. //str len is therefore measured correctly by counting the ?'s. //http://www.phpwact.org/php/i18n/charsets function utf8_strlen($string){ return strlen(utf8_decode($string)); } static function &arrayGet($aArray, $sKey, $mDefault = null, $bDefaultIfEmpty = true) { if (!is_array($aArray)) { $aArray = (array) $aArray; } if ($aArray !== 0 && $aArray !== '0' && empty($aArray)) { return $mDefault; } if (array_key_exists($sKey, $aArray)) { $mVal =& $aArray[$sKey]; if (empty($mVal) && $bDefaultIfEmpty) { return $mDefault; } return $mVal; } return $mDefault; } function requestValue($sKey, $mDefault = null) { return KTUtil::arrayGet($_REQUEST, $sKey, $mDefault); } // {{{ whereToString /** * Convert an array of parameterised strings to a single * parameterised string. * * Return null in case of an empty array. */ static function whereToString($aWhere) { $aStrings = array(); $aParams = array(); foreach ($aWhere as $oSomething) { if (is_string($oSomething)) { $aStrings[] = $oSomething; } else if (is_array($oSomething)) { $aStrings[] = $oSomething[0]; $aNewParams = array(); foreach ($oSomething[1] as $oParam) { if (is_array($oParam)) { $aNewParams = array_merge($aNewParams, $oParam); } else { $aNewParams[] = $oParam; } } $aParams = array_merge($aParams, $aNewParams); } else { return PEAR::raiseError(_kt("Weird WhereClause passed")); } } if (count($aStrings) === 0) { return null; } return array(join(" AND ", $aStrings), $aParams); } // }}} // {{{ safeShellString function safeShellString () { $aArgs = func_get_args(); $aSafeArgs = array(); if (is_array($aArgs[0])) { $aArgs = $aArgs[0]; } $aSafeArgs[] = escapeshellarg(array_shift($aArgs)); if (is_array($aArgs[0])) { $aArgs = $aArgs; } foreach ($aArgs as $sArg) { if (empty($sArg)) { $aSafeArgs[] = "''"; } else { $aSafeArgs[] = escapeshellarg($sArg); } } return join(" ", $aSafeArgs); } // }}} // {{{ pexec /** * Portably execute a command on any of the supported platforms. */ function pexec($aCmd, $aOptions = null) { if (is_array($aCmd)) { $sCmd = KTUtil::safeShellString($aCmd); } else { $sCmd = $aCmd; } $sAppend = KTUtil::arrayGet($aOptions, 'append'); if ($sAppend) { $sCmd .= " >> " . escapeshellarg($sAppend); } $sPopen = KTUtil::arrayGet($aOptions, 'popen');
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?