Ucenter 分析三/*base.php*/

<?php
/*
*文件名称:/model/base.php
*摘 要:UC基础类
*功 能:变量的初始化 常用类的实例化
*
*copyright(c) 2009 蜗牛工作室[ www.px234.cn]
*分析深度:第一轮分析
*作 者:蜗牛
*联 系 QQ:49364332
*分析日期:2009-03-04
*
*/
!defined('IN_UC') && exit('Access Denied');//用来判断本文件是否由 /admin.php调用的,不是则报错
//该常量在 /admin.php中已经定义了 如果其他文件 或者URL直接访问 则报错。有效的防止该文件被非法访问
class base {
var $time;//时间变量
var $onlineip;//在线IP变量
var $db;//数据库变量
var $view;//模板变量
var $user = array();//用户变量
var $settings = array();//常用设置变量
var $cache = array();//缓存变量
var $app = array();//应用程序变量
var $lang = array();//语言变量
var $input = array();//输入变量
function __construct() {
   $this->base();//执行构造函数
}
function base() {
   $this->init_var(); //变量初始化
   $this->init_db(); //数据库链接
   $this->init_cache();//常用设置的缓存文件的初始化
   $this->init_app();//
   $this->init_user();//
   $this->init_template();
   $this->init_note();
   $this->init_mail();
   //   $this->cron();
}
//以上两个方法 __construct()   base()有效的解决了 php4 与PHP5在结构函数上的兼容问题
function init_var() {//一些常用变量的初始化
   $this->time = time(); //将当前时间 存入成员变量time中
   //getenv() 获取系统的环境变量
   $cip = getenv('HTTP_CLIENT_IP');//获取客户端IP
   $xip = getenv('HTTP_X_FORWARDED_FOR');//获取客户端IP
   $rip = getenv('REMOTE_ADDR');//获取客户端IP
   $srip = $_SERVER['REMOTE_ADDR'];//获取客户端IP
  
   if($cip && strcasecmp($cip, 'unknown')) {//strcasecmp()函数的作用是:对两个字符串进行比较。0为相等
$this->onlineip = $cip;//若CIP存在 则用户IP为CIP值
   } elseif($xip && strcasecmp($xip, 'unknown')) {
$this->onlineip = $xip;//若XIP存在 则用户IP为XIP值
   } elseif($rip && strcasecmp($rip, 'unknown')) {
$this->onlineip = $rip;//若RIP存在 则用户IP为RIP值
   } elseif($srip && strcasecmp($srip, 'unknown')) {
$this->onlineip = $srip;//若SRIP存在 则用户IP为SRIP值
   }//通过四种方法来获取用户IP
   preg_match("/[/d/.]{7,15}/", $this->onlineip, $match);
   $this->onlineip = $match[0] ? $match[0] : 'unknown';   //简单过滤掉 不合格式的IP
          //获取onlineIP 并存入成员变量onlineip中
         
  
       define('FORMHASH', $this->formhash()); //定义哈希加密常量
  
       //如果页面小于1 则取页码为1   用处何在?
       $_GET['page'] =   max(1, intval(getgpc('page')));
  
      
   //加载主要 语言文件<主要是一些 提示信息>   内容为:$lang数组
   include_once UC_ROOT.'./view/default/main.lang.php';
  
   //建立 lang数组的引用
   $this->lang = &$lang;   //为什么要用引用 而不是直接赋值呢?
}

//初始化 settings以及apps缓存
function init_cache() {
   $this->settings = $this->cache('settings');
   //cache():自定义缓存函数   作用:返回缓存文件里的数组
   $this->cache['apps'] = $this->cache('apps');
  
   //时间设置   这个还不知道用处 及原理
   if(PHP_VERSION > '5.1') {
$timeoffset = intval($this->settings['timeoffset'] / 3600);
//intval. 变量转成整数类型
@date_default_timezone_set('Etc/GMT'.($timeoffset > 0 ? '-' : '+').(abs($timeoffset)));
   }
}
//初始化 表单以及URL传递来的数据   暂未分析
function init_input($getagent = '') {
   $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');
   }
}
//加载数据库操作类 DB类   链接数据库
function init_db() {
   require_once UC_ROOT.'lib/db.class.php';//加载数据库类
   $this->db = new db();//实例化数据库类 以后可以用$this->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'));//获取应用程序ID
   $appid && $this->app = $this->cache['apps'][$appid];
  
}
//用户初始化
function init_user() {
   if(isset($_COOKIE['uc_auth'])) {
//list 用数组中的值 为LIST中的变量赋值

@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;
}
   }
  
}
//模板初始化 加载模板类 并实例化 得到 $this->view对象
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() && !getgpc('inajax')) {
$this->load('note');
$_ENV['note']->send();
   }
}
function init_mail() {
   if($this->mail_exists() && !getgpc('inajax')) {
$this->load('mail');
$_ENV['mail']->send();
   }
}
//
function authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) {
   $ckey_length = 4;
   $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));
   }
}
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.'>&lsaquo;&lsaquo;</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.'>&rsaquo;&rsaquo;</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" /></kbd>' : '');
$multipage = $multipage ? '<div class="pages">'.(!$simple ? '<em>&nbsp;'.$num.'&nbsp;</em>' : '').$multipage.'</div>' : '';
   }
   return $multipage;
}
function page_get_start($page, $ppp, $totalnum) {
   $totalpage = ceil($totalnum / $ppp);
   $page =   max(1, min($totalpage, intval($page)));
   return ($page - 1) * $ppp;
}

//模型加载函数
function load($model, $base = NULL, $release = '') {
   $base = $base ? $base : $this;
   if(empty($_ENV[$model])) {
$release = !$release ? RELEASE_ROOT : $release;
if(file_exists(UC_ROOT.$release."model/$model.php")) {
require_once UC_ROOT.$release."model/$model.php";
} else {
require_once UC_ROOT."model/$model.php";
}
eval('$_ENV[$model] = new '.$model.'model($base);');
   }
   return $_ENV[$model];
}
function get_setting($k = array(), $decode = FALSE) {
   $return = array();
   $sqladd = $k ? "WHERE k IN (".$this->implode($k).")" : '';
   $settings = $this->db->fetch_all("SELECT * FROM ".UC_DBTABLEPRE."settings $sqladd");
   if(is_array($settings)) {
foreach($settings as $arr) {
$return[$arr['k']] = $decode ? unserialize($arr['v']) : $arr['v'];
}
   }
   return $return;
}
function set_setting($k, $v, $encode = FALSE) {
   $v = is_array($v) || $encode ? addslashes(serialize($v)) : $v;
   $this->db->query("REPLACE INTO ".UC_DBTABLEPRE."settings SET k='$k', v='$v'");
}
function message($message, $redirect = '', $type = 0, $vars = array()) {
   include_once UC_ROOT.'view/default/messages.lang.php';
   if(isset($lang[$message])) {
$message = $lang[$message] ? str_replace(array_keys($vars), array_values($vars), $lang[$message]) : $message;
   }
   $this->view->assign('message', $message);
   $this->view->assign('redirect', $redirect);
   if($type == 0) {
$this->view->display('message');
   } elseif($type == 1) {
$this->view->display('message_client');
   }
   exit;
}
function formhash() {//用当前时间与 UC_KEY进行MD5加密以得到formhash 哈希密钥
   return substr(md5(substr($this->time, 0, -4).UC_KEY), 16);
}
function submitcheck() {
   return @getgpc('formhash', 'P') == FORMHASH ? true : false;
}
function date($time, $type = 3) {
   $format[] = $type & 2 ? (!empty($this->settings['dateformat']) ? $this->settings['dateformat'] : 'Y-n-j') : '';
   $format[] = $type & 1 ? (!empty($this->settings['timeformat']) ? $this->settings['timeformat'] : 'H:i') : '';
   return gmdate(implode(' ', $format), $time + $this->settings['timeoffset']);
}
function implode($arr) {
   return "'".implode("','", (array)$arr)."'";
}
function set_home($uid, $dir = '.') {
   $uid = sprintf("%09d", $uid);
   $dir1 = substr($uid, 0, 3);
   $dir2 = substr($uid, 3, 2);
   $dir3 = substr($uid, 5, 2);
   !is_dir($dir.'/'.$dir1) && mkdir($dir.'/'.$dir1, 0777);
   !is_dir($dir.'/'.$dir1.'/'.$dir2) && mkdir($dir.'/'.$dir1.'/'.$dir2, 0777);
   !is_dir($dir.'/'.$dir1.'/'.$dir2.'/'.$dir3) && mkdir($dir.'/'.$dir1.'/'.$dir2.'/'.$dir3, 0777);
}
function get_home($uid) {
   $uid = sprintf("%09d", $uid);
   $dir1 = substr($uid, 0, 3);
   $dir2 = substr($uid, 3, 2);
   $dir3 = substr($uid, 5, 2);
   return $dir1.'/'.$dir2.'/'.$dir3;
}
function get_avatar($uid, $size = 'big', $type = '') {
   $size = in_array($size, array('big', 'middle', 'small')) ? $size : 'big';
   $uid = abs(intval($uid));
   $uid = sprintf("%09d", $uid);
   $dir1 = substr($uid, 0, 3);
   $dir2 = substr($uid, 3, 2);
   $dir3 = substr($uid, 5, 2);
   $typeadd = $type == 'real' ? '_real' : '';
   return   $dir1.'/'.$dir2.'/'.$dir3.'/'.substr($uid, -2).$typeadd."_avatar_$size.jpg";
}
// 缓存函数   返回缓存文件里的数组
function &cache($cachefile) { //引用函数  
   static $_CACHE = array(); //定义静态变量$_CACHE
   if(!isset($_CACHE[$cachefile])) {//如果没有设置$_CACHE[]数组 则
$cachepath = UC_DATADIR.'./cache/'.$cachefile.'.php';

    if(!file_exists($cachepath)) {
    $this->load('cache'); //加载CACHE模型 并返回 实例化CAHEMODEL类后的对象$_ENV[$model]
   
    $_ENV['cache']->updatedata($cachefile);
   }
    else {
include_once $cachepath;
   }
   }
  
   //返回缓存文件里的数组
   return $_CACHE[$cachefile];
}

function input($k) {
   return isset($this->input[$k]) ? (is_array($this->input[$k]) ? $this->input[$k] : trim($this->input[$k])) : NULL;
}
function serialize($s, $htmlon = 0) {
   if(file_exists(UC_ROOT.RELEASE_ROOT.'./lib/xml.class.php')) {
include_once UC_ROOT.RELEASE_ROOT.'./lib/xml.class.php';
   } else {
include_once UC_ROOT.'./lib/xml.class.php';
   }
   return xml_serialize($s, $htmlon);
}
function unserialize($s) {
   if(file_exists(UC_ROOT.RELEASE_ROOT.'./lib/xml.class.php')) {
include_once UC_ROOT.RELEASE_ROOT.'./lib/xml.class.php';
   } else {
include_once UC_ROOT.'./lib/xml.class.php';
   }
   return xml_unserialize($s);
}
function cutstr($string, $length, $dot = ' ...') {
   if(strlen($string) <= $length) {
return $string;
   }
   $string = str_replace(array('&amp;', '&quot;', '&lt;', '&gt;'), array('&', '"', '<', '>'), $string);
   $strcut = '';
   if(strtolower(UC_CHARSET) == 'utf-8') {
$n = $tn = $noc = 0;
while($n < strlen($string)) {
$t = ord($string[$n]);
if($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) {
    $tn = 1; $n++; $noc++;
} elseif(194 <= $t && $t <= 223) {
    $tn = 2; $n += 2; $noc += 2;
} elseif(224 <= $t && $t < 239) {
    $tn = 3; $n += 3; $noc += 2;
} elseif(240 <= $t && $t <= 247) {
    $tn = 4; $n += 4; $noc += 2;
} elseif(248 <= $t && $t <= 251) {
    $tn = 5; $n += 5; $noc += 2;
} elseif($t == 252 || $t == 253) {
    $tn = 6; $n += 6; $noc += 2;
} else {
    $n++;
}
if($noc >= $length) {
    break;
}
}
if($noc > $length) {
$n -= $tn;
}
$strcut = substr($string, 0, $n);
   } else {
for($i = 0; $i < $length; $i++) {
$strcut .= ord($string[$i]) > 127 ? $string[$i].$string[++$i] : $string[$i];
}
   }
   $strcut = str_replace(array('&', '"', '<', '>'), array('&amp;', '&quot;', '&lt;', '&gt;'), $strcut);
   return $strcut.$dot;
}
function setcookie($key, $value, $life = 0, $httponly = false) {
   (!defined('UC_COOKIEPATH')) && define('UC_COOKIEPATH', '/');
   (!defined('UC_COOKIEDOMAIN')) && define('UC_COOKIEDOMAIN', '');
   if($value == '' || $life < 0) {
$value = '';
$life = -1;
   }
  
   $life = $life > 0 ? $this->time + $life : ($life < 0 ? $this->time - 31536000 : 0);
   $path = $httponly && PHP_VERSION < '5.2.0' ? UC_COOKIEPATH."; HttpOnly" : UC_COOKIEPATH;
   $secure = $_SERVER['SERVER_PORT'] == 443 ? 1 : 0;
   if(PHP_VERSION < '5.2.0') {
setcookie($key, $value, $life, $path, UC_COOKIEDOMAIN, $secure);
   } else {
setcookie($key, $value, $life, $path, UC_COOKIEDOMAIN, $secure, $httponly);
   }
}
function note_exists() {
   $noteexists = $this->db->fetch_first("SELECT value FROM ".UC_DBTABLEPRE."vars WHERE name='noteexists'");
   if(empty($noteexists)) {
return FALSE;
   } else {
return TRUE;
   }
}
function mail_exists() {
   $mailexists = $this->db->fetch_first("SELECT value FROM ".UC_DBTABLEPRE."vars WHERE name='mailexists'");
   if(empty($mailexists)) {
return FALSE;
   } else {
return TRUE;
   }
}
function dstripslashes($string) {
   if(is_array($string)) {
foreach($string as $key => $val) {
$string[$key] = $this->dstripslashes($val);
}
   } else {
$string = stripslashes($string);
   }
   return $string;
}
}
?>[/php]
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值