Discuz!常用函数解析(3)

<?php
/**
* 刷新重定向
*/
function dreferer($default = '') {
        global $referer, $indexname;

        $default = empty($default) ? $indexname : '';
        if(empty($referer) && isset($GLOBALS['_SERVER']['HTTP_REFERER'])) {
                $referer = preg_replace("/([\?&])((sid\=[a-z0-9]{6})(&|$))/i", '\\1', $GLOBALS['_SERVER']['HTTP_REFERER']);
                $referer = substr($referer, -1) == '?' ? substr($referer, 0, -1) : $referer;
        } else {
                $referer = dhtmlspecialchars($referer);
        }

        if(!preg_match("/(\.php|[a-z]+(\-\d+)+\.html)/", $referer) || strpos($referer, 'logging.php')) {
                $referer = $default;
        }
        return $referer;
}

/**
* 设置cookie
* @param $var - 变量名
* @param $value - 变量值
* @param $life - 生命期
* @param $prefix - 前缀
*/
function dsetcookie($var, $value, $life = 0, $prefix = 1) {
        global $cookiepre, $cookiedomain, $cookiepath, $timestamp, $_SERVER;
        setcookie(($prefix ? $cookiepre : '').$var, $value,
                $life ? $timestamp + $life : 0, $cookiepath,
                $cookiedomain, $_SERVER['SERVER_PORT'] == 443 ? 1 : 0);
}

function dunlink($filename, $havethumb = 0, $remote = 0) {
        global $authkey, $ftp, $attachdir;
        if($remote) {
                require_once DISCUZ_ROOT.'./include/ftp.func.php';
                if(!$ftp['connid']) {
                        if(!($ftp['connid'] = dftp_connect($ftp['host'], $ftp['username'], authcode($ftp['password'], 'DECODE', md5($authkey)), $ftp['attachdir'], $ftp['port'], $ftp['ssl']))) {
                                return;
                        }
                }
                dftp_delete($ftp['connid'], $filename);
                $havethumb && dftp_delete($ftp['connid'], $filename.'.thumb.jpg');
        } else {
                @unlink($attachdir.'/'.$filename);
                $havethumb && @unlink($attachdir.'/'.$filename.'.thumb.jpg');
        }
}

/**
* 格式化email
* @param $email - 邮箱地址
* @param $tolink - 是否增加链接
* @return 返回代码
*/
function emailconv($email, $tolink = 1) {
        $email = str_replace(array('@', '.'), array('@', '.'), $email);
        return $tolink ? '<a href="mailto: '.$email.'">'.$email.'</a>': $email;
}

/**
* 系统错误日志
* @param $type - 信息类型
* @param $message - 信息
* @param $halt - 是否退出
*/
function errorlog($type, $message, $halt = 1) {
        global $timestamp, $discuz_userss, $onlineip, $_SERVER;
        $user = empty($discuz_userss) ? '' : $discuz_userss.'<br />';
        $user .= $onlineip.'|'.$_SERVER['REMOTE_ADDR'];
        writelog('errorlog', dhtmlspecialchars("$timestamp\t$type\t$user\t".str_replace(array("\r", "\n"), array(' ', ' '), trim($message))));
        if($halt) {
                exit();
        }
}

/**
* 去掉文件扩展名
* @param $finename - 文件名称
* @return 文件名
*/
function fileext($filename) {
        return trim(substr(strrchr($filename, '.'), 1, 10));
}

/**
* 产生form防伪码
*/
function formhash($specialadd = '') {
        global $discuz_user, $discuz_uid, $discuz_pw, $timestamp, $discuz_auth_key;
        $hashadd = defined('IN_ADMINCP') ? 'Only For Discuz! Admin Control Panel' : '';
        return substr(md5(substr($timestamp, 0, -7).$discuz_user.$discuz_uid.$discuz_pw.$discuz_auth_key.$hashadd.$specialadd), 8, 8);
}

/**
* 论坛权限
* @param $permstr - 权限信息
* @return 0 无权限 > 0 有权限
*/
function forumperm($permstr) {
        global $groupid, $extgroupids;

        $groupidarray = array($groupid);
        foreach(explode("\t", $extgroupids) as $extgroupid) {
                if($extgroupid = intval(trim($extgroupid))) {
                        $groupidarray[] = $extgroupid;
                }
        }
        return preg_match("/(^|\t)(".implode('|', $groupidarray).")(\t|$)/", $permstr);
}

/**
权限表达式
* @param $formula - 权限表达式
* @param $type - 0 论坛权限验证 1 勋章权限验证 2 返回勋章权限字串
*/
function formulaperm($formula, $type = 0) {
        global $_DSESSION, $extcredits, $formulamessage, $usermsg, $forum, $language;

        if((!$formula || $_DSESSION['adminid'] == 1 || $forum['ismoderator']) && !$type) {
                return;
        }
        $formula = unserialize($formula);$formula = $formula[1];
        if(!$formula) {
                return;
        }
        @eval("\$formulaperm = ($formula) ? TRUE : FALSE;");
        if(!$formulaperm || $type == 2) {
                include_once language('misc');
                $search = array('$_DSESSION[\'digestposts\']', '$_DSESSION[\'posts\']', '$_DSESSION[\'oltime\']', '$_DSESSION[\'pageviews\']');
                $replace = array($language['formulaperm_digestposts'], $language['formulaperm_posts'], $language['formulaperm_oltime'], $language['formulaperm_pageviews']);
                for($i = 1; $i <= 8; $i++) {
                        $search[] = '$_DSESSION[\'extcredits'.$i.'\']';
                        $replace[] = $extcredits[$i]['title'] ? $extcredits[$i]['title'] : $language['formulaperm_extcredits'].$i;
                }
                $i = 0;$usermsg = '';
                foreach($search as $s) {
                        $usermsg .= strexists($formula, $s) ? $replace[$i].' = '.(@eval('return intval('.$s.');')).'   ' : '';
                        $i++;
                }
                $search = array_merge($search, array('and', 'or', '>=', '<='));
                $replace = array_merge($replace, array('  '.$language['formulaperm_and'].'  ', '  '.$language['formulaperm_or'].'  ', '≥', '≤'));
                $formulamessage = str_replace($search, $replace, $formula);

                if($type == 1) {
                        showmessage('medal_permforum_nopermission', NULL, 'NOPERM');
                } elseif($type == 2) {
                        return $formulamessage;
                } else {
                        showmessage('forum_permforum_nopermission', NULL, 'NOPERM');
                }
        }
        return TRUE;
}

/**
* 获取用户所在组
* @param $uid - 用户组
* @param $group - 用户组
* @param $member - 用户组
*/
function getgroupid($uid, $group, &$member) {
        global $creditsformula, $db, $tablepre;

        if(!empty($creditsformula)) {
                $updatearray = array();
                eval("\$credits = round($creditsformula);");

                if($credits != $member['credits']) {
                        $updatearray[] = "credits='$credits'";
                        $member['credits'] = $credits;
                }
                if($group['type'] == 'member' && !($member['credits'] >= $group['creditshigher'] && $member['credits'] < $group['creditslower'])) {
                        $query = $db->query("SELECT groupid FROM {$tablepre}usergroups WHERE type='member' AND $member[credits]>=creditshigher AND $member[credits]<creditslower LIMIT 1");
                        if($db->num_rows($query)) {
                                $member['groupid'] = $db->result($query, 0);
                                $updatearray[] = "groupid='$member[groupid]'";
                        }
                }

                if($updatearray) {
                        $db->query("UPDATE {$tablepre}members SET ".implode(', ', $updatearray)." WHERE uid='$uid'");
                }
        }

        return $member['groupid'];
}

function getrobot() {
        if(!defined('IS_ROBOT')) {
                $kw_spiders = 'Bot|Crawl|Spider|slurp|sohu-search|lycos|robozilla';
                $kw_browsers = 'MSIE|Netscape|Opera|Konqueror|Mozilla';
                if(preg_match("/($kw_browsers)/", $_SERVER['HTTP_USER_AGENT'])) {
                        define('IS_ROBOT', FALSE);
                } elseif(preg_match("/($kw_spiders)/", $_SERVER['HTTP_USER_AGENT'])) {
                        define('IS_ROBOT', TRUE);
                } else {
                        define('IS_ROBOT', FALSE);
                }
        }
        return IS_ROBOT;
}

/**
* 根据用户的 uid 得到 avatar/home 目录
*
* @param int $uid
* @return string
*/
function get_home($uid) {
        $uid = sprintf("%05d", $uid);
        $dir1 = substr($uid, 0, -4);
        $dir2 = substr($uid, -4, 2);
        $dir3 = substr($uid, -2, 2);
        return $dir1.'/'.$dir2.'/'.$dir3;
}
?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值