📄 base.php
字号:
<?php
/*
[UCenter] (C)2001-2008 Comsenz Inc.
This is NOT a freeware, use is subject to license terms
$Id: base.php 12189 2008-01-17 07:29:56Z heyond $
*/
!defined('IN_UC') && exit('Access Denied');
/**
* 该基类有三个用途:
* 1. 纯接口(任何人, 此时需要检查 $input 的值)
* 2. 带交互界面的(限会员, 检查 $input 中 uid)
* 3. 管理(限管理员, 检查 $_COOKIE['uc_auth'] 是否为founder)
*/
class base {
var $time;
var $onlineip;
var $db;
var $view;
var $user = array();
var $settings = array();
var $cache = array();
var $app = array();
var $lang = array();
var $input = array();//note 如果为空,并且存在appid,则从外部传递参数非法。
/**
* 初始化基类
*
*/
function base() {
$this->init_var();
$this->init_db();
$this->init_cache();
$this->init_app();
$this->init_user();
$this->init_template();
$this->init_note();
// $this->cron();
}
/**
* 初始化常用变量,如果有 code 则解开后,放入 $_GET 超级全局变量
*
*/
function init_var() {
$this->time = time();
$cip = getenv('HTTP_CLIENT_IP');
$xip = getenv('HTTP_X_FORWARDED_FOR');
$rip = getenv('REMOTE_ADDR');
$srip = $_SERVER['REMOTE_ADDR'];
if($cip && strcasecmp($cip, 'unknown')) {
$this->onlineip = $cip;
} elseif($xip && strcasecmp($xip, 'unknown')) {
$this->onlineip = $xip;
} elseif($rip && strcasecmp($rip, 'unknown')) {
$this->onlineip = $rip;
} elseif($srip && strcasecmp($srip, 'unknown')) {
$this->onlineip = $srip;
}
preg_match("/[\d\.]{7,15}/", $this->onlineip, $match);
$this->onlineip = $match[0] ? $match[0] : 'unknown';
define('FORMHASH', $this->formhash());
$_GET['page'] = max(1, intval(getgpc('page')));
include_once UC_ROOT.'./view/default/main.lang.php';
$this->lang = &$lang;
}
function init_cache() {
//note 全局设置
$_CACHE = $this->cache('settings');
$this->settings = &$_CACHE['settings'];
$this->cache = &$_CACHE;
if(PHP_VERSION > '5.1') {
$timeoffset = intval($this->settings['timeoffset'] / 3600);
@date_default_timezone_set('Etc/GMT'.($timeoffset > 0 ? '-' : '+').(abs($timeoffset)));
}
}
//note 此参数仅为FLASH,或者第三方应用请求用户中心时使用。
function init_input($getagent = '') {
//note 解密应用提交的数据
$input = getgpc('input', 'R');
if($input) {
$input = $this->authcode($input, 'DECODE', $this->app['authkey']);
parse_str($input, $this->input);
$this->input = daddslashes($this->input, 1, TRUE);
$agent = $getagent ? $getagent : $this->input['agent'];
if(($getagent && $getagent != $this->input['agent']) || (!$getagent && md5($_SERVER['HTTP_USER_AGENT']) != $agent)) {
exit('Access denied for agent changed');
} elseif($this->time - $this->input('time') > 3600) {
exit('Authorization has expired');
}
}
if(empty($this->input)) {
exit('Invalid input');
}
}
/**
* 实例化数据库类
*
*/
function init_db() {
require_once UC_ROOT.'lib/db.class.php';
$this->db = new db();
$this->db->connect(UC_DBHOST, UC_DBUSER, UC_DBPW, UC_DBNAME, UC_DBCHARSET, UC_DBCONNECT, UC_DBTABLEPRE);
}
function init_app() {
$appid = intval(getgpc('appid'));
$appid && $this->app = $this->cache['apps'][$appid];
}
/**
* 初始化用户数据
*
*/
function init_user() {
//note 解密 cookie
if(isset($_COOKIE['uc_auth'])) {
@list($uid, $username, $agent) = explode('|', $this->authcode($_COOKIE['uc_auth'], 'DECODE', ($this->input ? $this->app['appauthkey'] : UC_KEY)));
if($agent != md5($_SERVER['HTTP_USER_AGENT'])) {
$this->setcookie('uc_auth', '');
} else {
@$this->user['uid'] = $uid;
@$this->user['username'] = $username;
}
}
}
/**
* 实例化模板类
*
*/
function init_template() {
$charset = UC_CHARSET;
require_once UC_ROOT.'lib/template.class.php';
$this->view = new template();
$this->view->assign('dbhistories', $this->db->histories);
$this->view->assign('charset', $charset);
$this->view->assign('dbquerynum', $this->db->querynum);
$this->view->assign('user', $this->user);
}
function init_note() {
if($this->note_exists()) {
$this->load('note');
$_ENV['note']->send();
}
}
/**
* 字符串加密以及解密函数
*
* @param string $string 原文或者密文
* @param string $operation 操作(ENCODE | DECODE), 默认为 DECODE
* @param string $key 密钥
* @param int $expiry 密文有效期, 加密时候有效, 单位 秒,0 为永久有效
* @return string 处理后的 原文或者 经过 base64_encode 处理后的密文
*
* @example
*
* $a = authcode('abc', 'ENCODE', 'key');
* $b = authcode($a, 'DECODE', 'key'); // $b(abc)
*
* $a = authcode('abc', 'ENCODE', 'key', 3600);
* $b = authcode('abc', 'DECODE', 'key'); // 在一个小时内,$b(abc),否则 $b 为空
*/
function authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) {
$ckey_length = 4; // 随机密钥长度 取值 0-32;
// 加入随机密钥,可以令密文无任何规律,即便是原文和密钥完全相同,加密结果也会每次不同,增大破解难度。
// 取值越大,密文变动规律越大,密文变化 = 16 的 $ckey_length 次方
// 当此值为 0 时,则不产生随机密钥
$key = md5($key ? $key : UC_KEY);
$keya = md5(substr($key, 0, 16));
$keyb = md5(substr($key, 16, 16));
$keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length): substr(md5(microtime()), -$ckey_length)) : '';
$cryptkey = $keya.md5($keya.$keyc);
$key_length = strlen($cryptkey);
$string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', $expiry ? $expiry + time() : 0).substr(md5($string.$keyb), 0, 16).$string;
$string_length = strlen($string);
$result = '';
$box = range(0, 255);
$rndkey = array();
for($i = 0; $i <= 255; $i++) {
$rndkey[$i] = ord($cryptkey[$i % $key_length]);
}
for($j = $i = 0; $i < 256; $i++) {
$j = ($j + $box[$i] + $rndkey[$i]) % 256;
$tmp = $box[$i];
$box[$i] = $box[$j];
$box[$j] = $tmp;
}
for($a = $j = $i = 0; $i < $string_length; $i++) {
$a = ($a + 1) % 256;
$j = ($j + $box[$a]) % 256;
$tmp = $box[$a];
$box[$a] = $box[$j];
$box[$j] = $tmp;
$result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
}
if($operation == 'DECODE') {
if((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16)) {
return substr($result, 26);
} else {
return '';
}
} else {
return $keyc.str_replace('=', '', base64_encode($result));
}
}
/**
* 翻页函数
*
* @param int $num 总纪录数
* @param int $perpage 每页大小
* @param int $curpage 当前页面
* @param string $mpurl url
* @return string 类似于: <div class="page">***</div>
*/
function page($num, $perpage, $curpage, $mpurl) {
$multipage = '';
$mpurl .= strpos($mpurl, '?') ? '&' : '?';
if($num > $perpage) {
$page = 10;
$offset = 2;
$pages = @ceil($num / $perpage);
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="first"'.$ajaxtarget.'>1 ...</a>' : '').
($curpage > 1 && !$simple ? '<a href="'.$mpurl.'page='.($curpage - 1).'" class="prev"'.$ajaxtarget.'>‹‹</a>' : '');
for($i = $from; $i <= $to; $i++) {
$multipage .= $i == $curpage ? '<strong>'.$i.'</strong>' :
'<a href="'.$mpurl.'page='.$i.($ajaxtarget && $i == $pages && $autogoto ? '#' : '').'"'.$ajaxtarget.'>'.$i.'</a>';
}
$multipage .= ($curpage < $pages && !$simple ? '<a href="'.$mpurl.'page='.($curpage + 1).'" class="next"'.$ajaxtarget.'>››</a>' : '').
($to < $pages ? '<a href="'.$mpurl.'page='.$pages.'" class="last"'.$ajaxtarget.'>... '.$realpages.'</a>' : '').
(!$simple && $pages > $page && !$ajaxtarget ? '<kbd><input type="text" name="custompage" size="3" onkeydown="if(event.keyCode==13) {window.location=\''.$mpurl.'page=\'+this.value; return false;}" /></kbd>' : '');
$multipage = $multipage ? '<div class="pages">'.(!$simple ? '<em> '.$num.' </em>' : '').$multipage.'</div>' : '';
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -