Discuz!常用函数解析(5)

<?php
/**
* 问题答案加密
* @param $questionid - 问题
* @param $answer - 答案
* @return 返回加密的字串
*/
function quescrypt($questionid, $answer) {
        return $questionid > 0 && $answer != '' ? substr(md5($answer.md5($questionid)), 16, 8) : '';
}

/**
* 产生伪静态地址
* @param $tid 主题id
* @param $page 页标
* @param $prevpage 上一页
* @param extra 扩展
* @return 返回链接
*/
function rewrite_thread($tid, $page = 0, $prevpage = 0, $extra = '') {
        return '<a href="thread-'.$tid.'-'.($page ? $page : 1).'-'.($prevpage && !IS_ROBOT ? $prevpage : 1).'.html"'.stripslashes($extra).'>';
}

/**
* 产生伪静态地址
* @param $fid 论坛id
* @param $page 页标
* @param extra 扩展
* @return 返回链接
*/
function rewrite_forum($fid, $page = 0, $extra = '') {
        return '<a href="forum-'.$fid.'-'.($page ? $page : 1).'.html"'.stripslashes($extra).'>';
}

function rewrite_space($uid, $username, $extra = '') {
        $GLOBALS['rewritecompatible'] && $username = rawurlencode($username);
        return '<a href="space-'.($uid ? 'uid-'.$uid : 'username-'.$username).'.html"'.stripslashes($extra).'>';
}

function rewrite_tag($name, $extra = '') {
        $GLOBALS['rewritecompatible'] && $name = rawurlencode($name);
        return '<a href="tag-'.$name.'.html"'.stripslashes($extra).'>';
}

/**
* 产生随机码
* @param $length - 要多长
* @param $numberic - 数字还是字符串
* @return 返回字符串
*/
function random($length, $numeric = 0) {
        PHP_VERSION < '4.2.0' && mt_srand((double)microtime() * 1000000);
        if($numeric) {
                $hash = sprintf('%0'.$length.'d', mt_rand(0, pow(10, $length) - 1));
        } else {
                $hash = '';
                $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
                $max = strlen($chars) - 1;
                for($i = 0; $i < $length; $i++) {
                        $hash .= $chars[mt_rand(0, $max)];
                }
        }
        return $hash;
}

/**
* 删除非空目录
* @param $path 目录
*/
function removedir($dirname, $keepdir = FALSE) {

        $dirname = wipespecial($dirname);

        if(!is_dir($dirname)) {
                return FALSE;
        }
        $handle = opendir($dirname);
        while(($file = readdir($handle)) !== FALSE) {
                if($file != '.' && $file != '..') {
                        $dir = $dirname . DIRECTORY_SEPARATOR . $file;
                        is_dir($dir) ? removedir($dir) : unlink($dir);
                }
        }
        closedir($handle);
        return !$keepdir ? (@rmdir($dirname) ? TRUE : FALSE) : TRUE;
}

function request($cachekey, $fid = 0, $type = 0, $return = 0) {
        global $timestamp, $_DCACHE;
        $datalist = '';
        if($fid && in_array(CURSCRIPT, array('forumdisplay', 'viewthread'))) {
                $specialfid = $GLOBALS['forum']['fid'];
                $cachekey = !isset($GLOBALS['infosidestatus']['f'.$specialfid][$type]) ? $GLOBALS['infosidestatus'][$type] : $GLOBALS['infosidestatus']['f'.$specialfid][$type];
                $key = $cachekey;
                $cachekey .= '_fid'.$specialfid;
        } else {
                $specialfid = 0;
                $key = $cachekey;
        }
        $cachefile = DISCUZ_ROOT.'./forumdata/cache/request_'.$cachekey.'.php';
        if((@!include($cachefile)) || $expiration < $timestamp) {
                require_once DISCUZ_ROOT.'./forumdata/cache/cache_request.php';
                require_once DISCUZ_ROOT.'./include/request.func.php';
                parse_str($_DCACHE['request'][$key]['url'], $requestdata);
                $datalist = parse_request($requestdata, $cachefile, 0, $specialfid, $key);
        }
        if(!$return) {
                echo $datalist;
        } else {
                return $datalist;
        }
}

/**
* 发送邮件
* @param $email_to - 接受者
* @param $email_subject - 标题
* @param $email_messge - 内容
* @param $email_from - 发送者
*/
function sendmail($email_to, $email_subject, $email_message, $email_from = '') {
        extract($GLOBALS, EXTR_SKIP);
        require DISCUZ_ROOT.'./include/sendmail.inc.php';
}

/**
* 发送短消息
* @param $toid - 接收方id
* @param $subject - 标题
* @param $message - 内容
* @param $fromid - 发送方id
* @param $from - 发送方名字
*/
function sendpm($toid, $subject, $message, $fromid = '') {
        extract($GLOBALS, EXTR_SKIP);
        include language('pms');

        require_once DISCUZ_ROOT.'./uc_client/client.php';

        if(isset($language[$subject])) {
                eval("\$subject = addslashes(\"".$language[$subject]."\");");
        }
        if(isset($language[$message])) {
                eval("\$message = addslashes(\"".$language[$message]."\");");
        }

        if($fromid === '') {
                $fromid = $discuz_uid;
        }
        uc_pm_send($fromid, $toid, $subject, $message);
}

/**
* 显示标准提示信息
* @param $toid - 信息
* @param $subject - 跳转到的url
* @param $message - 扩展
*/
function showmessage($message, $url_forward = '', $extra = '') {
        extract($GLOBALS, EXTR_SKIP);
        global $extrahead, $discuz_action, $debuginfo, $seccode, $fid, $tid, $charset, $show_message, $inajax, $_DCACHE, $advlist;
        define('CACHE_FORBIDDEN', TRUE);
        $show_message = $message;
        $msgforward = unserialize($_DCACHE['settings']['msgforward']);
        $msgforward['refreshtime'] = intval($msgforward['refreshtime']) * 1000;
        $url_forward = empty($url_forward) ? '' : (empty($_DCOOKIE['sid']) && $transsidstatus ? transsid($url_forward) : $url_forward);

        if($url_forward && empty($inajax) && $msgforward['quick'] && $msgforward['messages'] && @in_array($message, $msgforward['messages'])) {
                updatesession();
                dheader("location: ".str_replace('&', '&', $url_forward));
        }

        if(in_array($extra, array('HALTED', 'NOPERM'))) {
                $fid = $tid = 0;
                $discuz_action = 254;
        } else {
                $discuz_action = 255;
        }

        include language('messages');

        if(isset($language[$message])) {
                $pre = $inajax ? 'ajax_' : '';
                eval("\$show_message = \"".(isset($language[$pre.$message]) ? $language[$pre.$message] : $language[$message])."\";");
                unset($pre);
        }

        $show_message .= $url_forward && empty($inajax) ? '<script>setTimeout("window.location.href =\''.$url_forward.'\';", '.$msgforward['refreshtime'].');</script>' : '';

        if($advlist = array_merge($globaladvs ? $globaladvs['type'] : array(), $redirectadvs ? $redirectadvs['type'] : array())) {
                $advitems = ($globaladvs ? $globaladvs['items'] : array()) + ($redirectadvs ? $redirectadvs['items'] : array());
                foreach($advlist AS $type => $redirectadvs) {
                        $advlist[$type] = $advitems[$redirectadvs[array_rand($redirectadvs)]];
                }
        }

        if($extra == 'NOPERM') {
                //get secure code checking status (pos. -2)
                if($seccodecheck = substr(sprintf('%05b', $seccodestatus), -2, 1)) {
                        $seccode = random(6, 1) + $seccode{0} * 1000000;
                }
                include template('nopermission');
        } else {
                include template('showmessage');
        }
        dexit();
}

/**
* 显示等级
* @param $num - 等级
*/
function showstars($num) {
        global $starthreshold;

        $alt = 'alt="Rank: '.$num.'"';
        if(empty($starthreshold)) {
                for($i = 0; $i < $num; $i++) {
                        echo '<img src="'.IMGDIR.'/star_level1.gif" '.$alt.' />';
                }
        } else {
                for($i = 3; $i > 0; $i--) {
                        $numlevel = intval($num / pow($starthreshold, ($i - 1)));
                        $num = ($num % pow($starthreshold, ($i - 1)));
                        for($j = 0; $j < $numlevel; $j++) {
                                echo '<img src="'.IMGDIR.'/star_level'.$i.'.gif" '.$alt.' />';
                        }
                }
        }
}

/**
* 返回当前链接的域名
* @return 返回域名
*/
function site() {
        return $_SERVER['HTTP_HOST'];
}

/**
* 判断一个字符串是否在另一个字符串中存在
* @param haystack 待查找的字符串
* @param $needls 被查找的字符串
* @return 是否存在
*/
function strexists($haystack, $needle) {
        return !(strpos($haystack, $needle) === FALSE);
}

/**
* 验证码数字转字母
* @param seccode 带转换的验证码数字变量
*/
function seccodeconvert(&$seccode) {
        global $seccodedata, $charset;
        $seccode = substr($seccode, -6);
        if($seccodedata['type'] == 1) {
                include_once language('seccode');
                $len = strtoupper($charset) == 'GBK' ? 2 : 3;
                $code = array(substr($seccode, 0, 3), substr($seccode, 3, 3));
                $seccode = '';
                for($i = 0; $i < 2; $i++) {
                        $seccode .= substr($lang['chn'], $code[$i] * $len, $len);
                }
                return;
        } elseif($seccodedata['type'] == 3) {
                $s = sprintf('%04s', base_convert($seccode, 10, 20));
                $seccodeunits = 'CEFHKLMNOPQRSTUVWXYZ';
        } else {
                $s = sprintf('%04s', base_convert($seccode, 10, 24));
                $seccodeunits = 'BCEFGHJKMPQRTVWXY2346789';
        }
        $seccode = '';
        for($i = 0; $i < 4; $i++) {
                $unit = ord($s{$i});
                $seccode .= ($unit >= 0x30 && $unit <= 0x39) ? $seccodeunits[$unit - 0x30] : $seccodeunits[$unit - 0x57];
        }
}

?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值