<?PHP
/**
* Global Function
*
* @author Avenger <
avenger@PHP.net>
* @version 1.14 $Id 2003-05-30 10:10:08 $
*/
/**
* 彈出提示框
*
* @Access public
* @param string $txt 彈出一個(gè)提示框,$txt為要彈出的內(nèi)容
* @return void
*/
function popbox($txt) {
echo "<script language=‘JavaScript‘>alert(‘".$txt."‘)</script>";
}
/**
* 非法操作警告
*
* @Access public
* @param string $C_alert 提示的錯(cuò)誤信息
* @param string $I_goback 返回后返回到哪一頁(yè),不指定則不返回
* @return void
*/
function alert($C_alert,$I_goback=‘main.PHP‘) {
if(!empty($I_goback)) {
echo "<script>alert(‘$C_alert‘);window.location.href=‘$I_goback‘;</script>";
} else {
echo "<script>alert(‘$C_alert‘);</script>";
}
}
/**
* 產(chǎn)生隨機(jī)字符串
*
* 產(chǎn)生一個(gè)指定長(zhǎng)度的隨機(jī)字符串,并返回給用戶
*
* @Access public
* @param int $len 產(chǎn)生字符串的位數(shù)
* @return string
*/
function randstr($len=6) {
$chars=‘ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-@#~‘; // characters to build the passWord from
mt_srand((double)microtime()*1000000*getmypid()); // seed the random number generater (must be done)
$passWord=‘‘;
while(strlen($passWord)<$len)
$passWord.=substr($chars,(mt_rand()%strlen($chars)),1);
return $passWord;
}
/**
* 判斷下拉菜音的選取項(xiàng)
*
* 可以判斷字符串一和字符串二是否相等.從而使相等的項(xiàng)目在下拉菜單中被選擇
*
* @Access public
* @param string $str1 要比較的字符串一
* @param string $str2 要比較的字符串二
* @return string 相等返回字符串"selected",否則返回空字符串
*/
function ckselect($str1,$str2) {
if($str1==$str2) {
return ‘ selected‘;
}
return ‘‘;
}
/**
* 一個(gè)自定義的Ftp函數(shù)
*
* @Access private
* @return void
*/
function myftp($ftp_server,$ftp_port,$ftp_username,$ftp_passWord,$ftp_path=‘/‘) {
$ftpid=@ftp_connect($ftp_server,$ftp_port) or die(‘Connect To Ftp Server Error!‘);
@ftp_login($ftpid,$ftp_username,$ftp_passWord) or die(‘Login Ftp Error!‘);
@ftp_chdir($ftpid,‘/wwwroot/‘.$ftp_path) or die(‘Chdir Error!‘);
return $ftpid;
}
/**
* 截取中文部分字符串
*
* 截取指定字符串指定長(zhǎng)度的函數(shù),該函數(shù)可自動(dòng)判定中英文,不會(huì)出現(xiàn)亂碼
*
* @Access public
* @param string $str 要處理的字符串
* @param int $strlen 要截取的長(zhǎng)度默認(rèn)為10
* @param string $other 是否要加上省略號(hào),默認(rèn)會(huì)加上
* @return string
*/
function showtitle($str,$strlen=10,$other=true) {
$j = 0;
for($i=0;$i<$strlen;$i++)
if(ord(substr($str,$i,1))>0xa0) $j++;
if($j%2!=0) $strlen++;
$rstr=substr($str,0,$strlen);
if (strlen($str)>$strlen && $other) {$rstr.=‘...‘;}
return $rstr;
}
/**
* 制作鏈接
*
* @Access public
* @param string url 要鏈接到的網(wǎng)址
* @param string linktext 顯示的鏈接文字
* @param string target 目標(biāo)框架
* @param string extras 擴(kuò)展參數(shù)
* @return string
*/
function make_link ($url, $linktext=false, $target=false, $extras=false) {
return sprintf("<a href=\"%s\"%s%s>%s</a>",
$url,
($target ? ‘ target="‘.$target.‘"‘ : ‘‘),
($extras ? ‘ ‘.$extras : ‘‘),
($linktext ? $linktext : $url)
);
}
/**
* 格式化用戶評(píng)論
*
* @Access public
* @param string
* @return void
*/
function clean_note($text) {
$text = htmlspecialchars(trim($text));
/* turn urls into links */
$text = preg_replace("/((mailtohttpftpnntpnews):.+?)(>\s\)\"\.\s$)/","<a href=\"\1\">\1</a>\3",$text);
/* this ‘fixing‘ code will go away eventually. */
$fixes = array(‘<br>‘, ‘<p>‘, ‘</p>‘);
reset($fixes);
while (list(,$f) = each($fixes)) {
$text = str_replace(htmlspecialchars($f), $f, $text);
$text = str_replace(htmlspecialchars(strtoupper($f)), $f, $text);
}
/* <p> tags make things look awfully weird (breaks things out of the <code>
tag). Just convert them to <br>‘s
*/
$text = str_replace (array (‘<P>‘, ‘<p>‘), ‘<br>‘, $text);
/* Remove </p> tags to prevent it from showing up in the note */
$text = str_replace (array (‘</P>‘, ‘</p>‘), ‘‘, $text);
/* preserve linebreaks */
$text = str_replace("\n", "<br>", $text);
/* this will only break long lines */
if (function_exists("Wordwrap")) {
$text = Wordwrap($text);
}
// Preserve spacing of user notes
$text = str_replace(" ", " ", $text);
return $text;
}
/**
* 獲取圖象信息的函數(shù)
*
* 一個(gè)全面獲取圖象信息的函數(shù)
*
* @Access public
* @param string $img 圖片路徑
* @return array
*/
function getimageinfo($img) {
$img_info = getimagesize($img);
switch ($img_info[2]) {
case 1:
$imgtype = "GIF";
break;
case 2:
$imgtype = "JPG";
break;
case 3:
$imgtype = "PNG";
break;
}
$img_size = ceil(filesize($img)/1000)."k";
$new_img_info = array (
"width"=>$img_info[0],
"height"=>$img_info[1],
"type"=>$imgtype,
"size"=>$img_size
);
return $new_img_info;
}
/**
* 計(jì)算當(dāng)前時(shí)間
*
* 以微秒為單位返回當(dāng)前系統(tǒng)的時(shí)間
*
* @Access public
* @return real
*/
function getmicrotime() {
$tmp = eXPlode(‘ ‘, microtime());
return (real)$tmp[1]. substr($tmp[0], 1);
}
/**
* 寫文件操作
*
* @Access public
* @param bool
* @return void
*/
function wfile($file,$content,$mode=‘w‘) {
$oldmask = umask(0);
$fp = fopen($file, $mode);
if (!$fp) return false;
fwrite($fp,$content);
fclose($fp);
umask($oldmask);
return true;
}
/**
* 加載模板文件
*
* @Access public
* @return void
*/
function tpl_load($tplfile,$path=‘./templates/‘,$empty=‘remove‘) {
global $tpl;
$path ? ‘‘ : $path=‘./templates/‘;
require_once ‘HTML/Template/PHPLIB.PHP‘;
$tpl = new Template_PHPLIB($path,$empty);
$tpl->setFile(‘main‘,$tplfile);
}
/**
* 模板解析輸出
*
* @Access public
* @return void
*/
function tpl_output() {
global $tpl;
$tpl->parse(‘output‘,‘main‘);
$tpl->p(‘output‘);
}
/**
* 郵件發(fā)送函數(shù)
*
* @Access public private
* @param bool
* @return void
*/
function mailSender($from, $to, $title, $content) {
$from ? $from =
‘sender@PHPe.net‘ : ‘‘;
$title ? $title = ‘From Exceed PHP...‘ : ‘‘;
$sig = "
感謝您使用我們的服務(wù).\n\n
Exceed PHP(超越PHP)\n
$maildate\n\n
}
/**
* UBB解析
*
* @param none
* @Access public
* @return void
*/
function ubbParse($txt, $coverhtml=0) {
if ($coverhtml == 0) $txt = nl2br(htmlspecialchars($txt)); //BR和HTML轉(zhuǎn)換
//只轉(zhuǎn)換BR,不轉(zhuǎn)換HTML
if ($coverhtml == 1) {
if (!preg_match(‘/^<.+>$/s‘, $txt) && !preg_match(‘/<table.+<\/table>/is‘, $txt)) {
$txt = strip_tags($txt);
$txt = nl2br($txt);
}
}
// pre and quote
$txt = preg_replace( "#\[quote\](.+?)\[/quote\]#is", "<blockquote>\1</blockquote>", $txt );
$txt = preg_replace( "#\[code\](.+?)\[/code\]#is", "<p class=‘PHP‘>\1</p>", $txt );
// Colors 支持篏套
while( preg_match( "#\[color=([^\]]+)\](.+?)\[/color\]#is", $txt ) ) {
$txt = preg_replace( "#\[color=([^\]]+)\](.+?)\[/color\]#is", "<span style=‘color:\1‘>\2</span>", $txt );
}
// Align
$txt = preg_replace( "#\[center\](.+?)\[/center\]#is", "<center>\1</center>", $txt );
$txt = preg_replace( "#\[left\](.+?)\[/left\]#is", "<div align=left>\1</div>", $txt );
$txt = preg_replace( "#\[right\](.+?)\[/right\]#is", "<div align=right>\1</div>", $txt );
// Sub & sup
$txt = preg_replace( "#\[sup\](.+?)\[/sup\]#is", "<sup>\1</sup>", $txt );
$txt = preg_replace( "#\[sub\](.+?)\[/sub\]#is", "<sub>\1</sub>", $txt );
// email tags
// [email]avenger@PHP.net[/email] [email=avenger@PHP.net]Email me[/email]
$txt = preg_replace( "#\[email\](\S+?)\[/email\]#i" , "<a href=‘mailto:\1‘>\1</a>", $txt );
$txt = preg_replace( "#\[email\s*=\s*\"\;([\.\w\-]+\@[\.\w\-]+\.[\.\w\-]+)\s*\"\;\s*\](.*?)\[\/email\]#i" , "<a href=‘mailto:\1‘>\2</a>", $txt );
$txt = preg_replace( "#\[email\s*=\s*([\.\w\-]+\@[\.\w\-]+\.[\w\-]+)\s*\](.*?)\[\/email\]#i" , "<a href=‘mailto:\1‘>\2</a>", $txt );
// url tags
// [url]http://www.PHPe.net[/url] [url=http://www.PHPe.net]Exceed PHP![/url]
$txt = preg_replace( "#\[url\](\S+?)\[/url\]#i" , "<a href=‘\1‘ target=‘_blank‘>\1</a>", $txt );
$txt = preg_replace( "#\[url\s*=\s*\"\;\s*(\S+?)\s*\"\;\s*\](.*?)\[\/url\]#i" , "<a href=‘\1‘ target=‘_blank‘>\2</a>", $txt );
$txt = preg_replace( "#\[url\s*=\s*(\S+?)\s*\](.*?)\[\/url\]#i" , "<a href=‘\1‘ target=‘_blank‘>\2</a>", $txt );
// Start off with the easy stuff
$txt = preg_replace( "#\[b\](.+?)\[/b\]#is", "<b>\1</b>", $txt );
$txt = preg_replace( "#\[i\](.+?)\[/i\]#is", "<i>\1</i>", $txt );
$txt = preg_replace( "#\[u\](.+?)\[/u\]#is", "<u>\1</u>", $txt );
$txt = preg_replace( "#\[s\](.+?)\[/s\]#is", "<s>\1</s>", $txt );
// Images
$txt = preg_replace( "#\[img\](.+?)\[/img\]#i", "<a href=‘\1‘ target=‘_blank‘><img alt=‘Click to fullsize‘ src=‘\1‘ border=‘0‘ onload=‘Javascript:if(this.width>500) this.width=500‘ align=‘center‘ hspace=‘10‘ vspace=‘10‘></a><br />", $txt );
// Attach
$txt = preg_replace( "#\[attach\s*=\s*\"\;\s*(\S+?)\s*\"\;\s*\](.*?)\[\/attach\]#i" , "<a href=‘\2‘ target=‘_blank‘><b>相關(guān)附件:</b>\1</a>", $txt );
$txt = preg_replace( "#\[attach\s*=\s*(\S+?)\s*\](.*?)\[\/attach\]#i" , "<a href=‘\2‘ target=‘_blank‘><b>相關(guān)附件:</b>\1</a>", $txt );
// Iframe
$txt = preg_replace( "#\[iframe\](.+?)\[/iframe\]#i", "<div align=‘center‘><iframe src=‘\1‘ style=‘width:96%;height:400px‘></iframe><br clear=‘a(chǎn)ll‘><a href=‘\1‘ target=‘_blank‘>在新窗口打開鏈接</a></div>", $txt );
// (c) (r) and (tm)
$txt = preg_replace( "#\(c\)#i" , "©" , $txt );
$txt = preg_replace( "#\(tm\)#i" , "" , $txt );
$txt = preg_replace( "#\(r\)#i" , "®" , $txt );
return $txt;
}
?>