Discuz!源代码分析系列:./include/global.func.php(2)

转自www.discuz.net 作者:郭鑫

/**
* 用来得到上一个页面的地址,也就是来路。
* @para string $default 这个参数是直接设置一个refer,不用判断得到
*
* @return string
*/

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用的,我觉得这个和clearcookies放到一起比较好,不过好像这个是按字母排的…
* @para string $var cookie名
* @para string $value cookie值
* @para int $life 生存时间
* @para int $prefix cookie前缀
*
*/

function dsetcookie($var, $value, $life = 0, $prefix = 1) {
        global $cookiepre, $cookiedomain, $cookiepath, $timestamp, $_SERVER;
        //echo $prefix."--".$var."--".$value."--".$life."--".$cookiepath;
        
        setcookie(($prefix ? $cookiepre : '').$var, $value,
                $life ? $timestamp + $life : 0, $cookiepath,
                $cookiedomain, $_SERVER['SERVER_PORT'] == 443 ? 1 : 0);
}
复制内容到剪贴板代码:
/**
* 删除论坛的附件用的
* @para string $filename 附件名
* @para int $havethumb 是否有缩略图
* @para int $remote 是否为远程附件
*
*/

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连接用的,比如把 [email protected]换成:<a href="mailto:[email protected]">[email protected]</a>这样的形式
* @para string $email
* @para int $tolink
*/

function emailconv($email, $tolink = 1) {
        $email = str_replace(array('@', '.'), array('&#64;', '&#46;'), $email);
        return $tolink ? '<a href="mailto: '.$email.'">'.$email.'</a>': $email;
}
复制内容到剪贴板代码:
/**
* 记录错误日志用的
* @para string $type 错误类型
* @para string $message 错误内容
* @para int $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) {
                dexit();
        }
}
复制内容到剪贴板代码:
/**
* 判断访问者是不是robot
*
* @return boolean
*/

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;
}
复制内容到剪贴板代码:
/**
* 得到一个文件的扩展名
* @para string $filename
*
* @return string
*/

function fileext($filename) {
        return trim(substr(strrchr($filename, '.'), 1, 10));
}

/**
* 用当前时间,会员名,uid,密码,authkey生成一个form hash(哈希)
*
* @return string
*/

function formhash() {
        global $discuz_user, $discuz_uid, $discuz_pw, $timestamp, $discuz_auth_key;
        return substr(md5(substr($timestamp, 0, -7).$discuz_user.$discuz_uid.$discuz_pw.$discuz_auth_key), 8, 8);
}
复制内容到剪贴板代码:
/**
* 生成论坛访问权限的字串,以|隔开
* @para string $permstr 访问权限字串
*
* @return string
*/

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);
}
复制内容到剪贴板代码:
/**
* 得到用户组,同步groupid和member['groupid'],当会员积分和当前积分不一致更新members表。
* @para int $uid 会员的uid
* @para array $group 会员所属的用户组
* @para array $member
*
* @return string
*/

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'";
                }
                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'];
}
复制内容到剪贴板代码:
/**
* 这个的作用主要是把序列化后存在于数据库中的会员组到期信息取出来
* @para string $terms
* @para int $expiry
*/

function groupexpiry($terms) {
        $terms = is_array($terms) ? $terms : unserialize($terms);
        $groupexpiry = isset($terms['main']['time']) ? intval($terms['main']['time']) : 0;
        if(is_array($terms['ext'])) {
                foreach($terms['ext'] as $expiry) {
                        if((!$groupexpiry && $expiry) || $expiry < $groupexpiry) {
                                $groupexpiry = $expiry;
                        }
                }
        }
        return $groupexpiry;
}
复制内容到剪贴板代码:
/**
* 看看一个ip是不是在允许访问的范围内
* @para string $ip
* @para array $accesslist
*
* @return boolean
*/

function ipaccess($ip, $accesslist) {
        return preg_match("/^(".str_replace(array("\r\n", ' '), array('|', ''), preg_quote($accesslist, '/')).")/", $ip);
}
复制内容到剪贴板代码:
/**
* 判断ip是不是被ban了
* @para string $onlineip
*
* @return boolean
*/

function ipbanned($onlineip) {
        global $ipaccess, $timestamp, $cachelost;

        if($ipaccess && !ipaccess($onlineip, $ipaccess)) {
                return TRUE;
        }

        $cachelost .= (@include DISCUZ_ROOT.'./forumdata/cache/cache_ipbanned.php') ? '' : ' ipbanned';
        if(empty($_DCACHE['ipbanned'])) {
                return FALSE;
        } else {
                if($_DCACHE['ipbanned']['expiration'] < $timestamp) {
                        @unlink(DISCUZ_ROOT.'./forumdata/cache/cache_ipbanned.php');
                }
                return preg_match("/^(".$_DCACHE['ipbanned']['regexp'].")$/", $onlineip);
        }
}
复制内容到剪贴板代码:
/**
* 检查一个email的合法性
* @para string $email
*
* @return boolean
*/

function isemail($email) {
        return strlen($email) > 6 && preg_match("/^[\w\-\.]+@[\w\-\.]+(\.\w+)+$/", $email);
}
复制内容到剪贴板代码:
/**
* Discuz语言解析用到的东西,即按照需要装入语言包这个数组
* @para string $file 语言文件(如:templates, email, actions等)
* @para int $templateid 用的是哪套模板中的,若没有的话用TEMPLATEID这个常数取代
* @para string $tpldir 模板所在的目录
*
* @return array or false
*/

function language($file, $templateid = 0, $tpldir = '') {
        $tpldir = $tpldir ? $tpldir : TPLDIR;
        $templateid = $templateid ? $templateid : TEMPLATEID;

        $languagepack = DISCUZ_ROOT.'./'.$tpldir.'/'.$file.'.lang.php';
        if(file_exists($languagepack)) {
                return $languagepack;
        } elseif($templateid != 1 && $tpldir != './templates/default') {
                return language($file, 1, './templates/default');
        } else {
                return FALSE;
        }
}
复制内容到剪贴板代码:
/**
* 超经典的分页函数来了
* @para int $num 记录总数
* @para int $perpage 每页的记录数
* @para int $curpage 当前页
* @para string $mpurl 这个是用来保留query string中的参数的,打个比方:forumdisplay.php?fid=2,这个就是mpurl了,处理后会变成forumdisplay.php?fid=2&page=xx
* @para int $maxpages 最大的页数
* @para int $page 总页数
* @para int $simple 好像simple如果比一大的话就只有第一页,上一页,下一页,最后页
* @para string $onclick 点击触发的事件,AJAX用的。
*
* @return string
*/

function multi($num, $perpage, $curpage, $mpurl, $maxpages = 0, $page = 10, $simple = 0, $onclick = '') {
        $multipage = '';
        $mpurl .= strpos($mpurl, '?') ? '&' : '?';
        $onclick = $onclick ? ' onclick="'.$onclick.'(event)"' : '';
        if($num > $perpage) {
                $offset = 2;

                $realpages = @ceil($num / $perpage);
                $pages = $maxpages && $maxpages < $realpages ? $maxpages : $realpages;

                if($page > $pages) {
                        $from = 1;
                        $to = $pages;
                } else {
                        $from = $curpage - $offset;
                        $to = $from + $page - 1;
                        if($from < 1) {
                                $to = $curpage + 1 - $from;
                                $from = 1;
                                if($to - $from < $page) {
                                        $to = $page;
                                }
                        } elseif($to > $pages) {
                                $from = $pages - $page + 1;
                                $to = $pages;
                        }
                }

                $multipage = ($curpage - $offset > 1 && $pages > $page ? '<a href="'.$mpurl.'page=1" class="p_redirect"'.$onclick.'>|&#8249;</a>' : '').
                        ($curpage > 1 && !$simple ? '<a href="'.$mpurl.'page='.($curpage - 1).'" class="p_redirect">&#8249;&#8249;</a>' : '');
                for($i = $from; $i <= $to; $i++) {
                        $multipage .= $i == $curpage ? '<a class="p_curpage">'.$i.'</a>' :
                                '<a href="'.$mpurl.'page='.$i.'" class="p_num"'.$onclick.'>'.$i.'</a>';
                }

                $multipage .= ($curpage < $pages && !$simple ? '<a href="'.$mpurl.'page='.($curpage + 1).'" class="p_redirect"'.$onclick.'>&#8250;&#8250;</a>' : '').
                        ($to < $pages ? '<a href="'.$mpurl.'page='.$pages.'" class="p_redirect"'.$onclick.'>&#8250;|</a>' : '').
                        ($curpage == $maxpages ? '<a class="p_redirect" href="misc.php?action=maxpages&pages='.$maxpages.'">&#8250;?</a>' : '').
                        (!$simple && $pages > $page ? '<a class="p_pages" style="padding: 0px"><input class="p_input" type="text" name="custompage" onKeyDown="if(event.keyCode==13) {window.location=\''.$mpurl.'page=\'+this.value; return false;}"></a>' : '');

                $multipage = $multipage ? '<div class="p_bar">'.(!$simple ? '<a class="p_total">&nbsp;'.$num.'&nbsp;</a><a class="p_pages">&nbsp;'.$curpage.'/'.$realpages.'&nbsp;</a>' : '').$multipage.'</div>' : '';
        }
        return $multipage;
}
 

你可能感兴趣的:(return,function,源代码,include,default)