base.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. <?php
  2. /*
  3. [UCenter] (C)2001-2099 Comsenz Inc.
  4. This is NOT a freeware, use is subject to license terms
  5. $Id: base.php 1167 2014-11-03 03:06:21Z hypowang $
  6. */
  7. !defined('IN_UC') && exit('Access Denied');
  8. class base {
  9. var $sid;
  10. var $time;
  11. var $onlineip;
  12. var $db;
  13. var $view;
  14. var $user = array();
  15. var $settings = array();
  16. var $cache = array();
  17. var $app = array();
  18. var $lang = array();
  19. var $input = array();
  20. function __construct() {
  21. $this->base();
  22. }
  23. function base() {
  24. $this->init_var();
  25. $this->init_db();
  26. $this->init_cache();
  27. $this->init_app();
  28. $this->init_user();
  29. $this->init_template();
  30. $this->init_note();
  31. $this->init_mail();
  32. }
  33. function init_var() {
  34. $this->time = time();
  35. $cip = getenv('HTTP_CLIENT_IP');
  36. $xip = getenv('HTTP_X_FORWARDED_FOR');
  37. $rip = getenv('REMOTE_ADDR');
  38. $srip = $_SERVER['REMOTE_ADDR'];
  39. if($cip && strcasecmp($cip, 'unknown')) {
  40. $this->onlineip = $cip;
  41. } elseif($xip && strcasecmp($xip, 'unknown')) {
  42. $this->onlineip = $xip;
  43. } elseif($rip && strcasecmp($rip, 'unknown')) {
  44. $this->onlineip = $rip;
  45. } elseif($srip && strcasecmp($srip, 'unknown')) {
  46. $this->onlineip = $srip;
  47. }
  48. preg_match("/[\d\.]{7,15}/", $this->onlineip, $match);
  49. $this->onlineip = $match[0] ? $match[0] : 'unknown';
  50. define('FORMHASH', $this->formhash());
  51. $_GET['page'] = max(1, intval(getgpc('page')));
  52. include_once UC_ROOT.'./view/default/main.lang.php';
  53. $this->lang = &$lang;
  54. }
  55. function init_cache() {
  56. $this->settings = $this->cache('settings');
  57. $this->cache['apps'] = $this->cache('apps');
  58. if(PHP_VERSION > '5.1') {
  59. $timeoffset = intval($this->settings['timeoffset'] / 3600);
  60. @date_default_timezone_set('Etc/GMT'.($timeoffset > 0 ? '-' : '+').(abs($timeoffset)));
  61. }
  62. }
  63. function init_input($getagent = '') {
  64. $input = getgpc('input', 'R');
  65. if($input) {
  66. $input = $this->authcode($input, 'DECODE', $this->app['authkey']);
  67. parse_str($input, $this->input);
  68. $this->input = daddslashes($this->input, 1, TRUE);
  69. $agent = $getagent ? $getagent : $this->input['agent'];
  70. if(($getagent && $getagent != $this->input['agent']) || (!$getagent && md5($_SERVER['HTTP_USER_AGENT']) != $agent)) {
  71. exit('Access denied for agent changed');
  72. } elseif($this->time - $this->input('time') > 3600) {
  73. exit('Authorization has expired');
  74. }
  75. }
  76. if(empty($this->input)) {
  77. exit('Invalid input');
  78. }
  79. }
  80. function init_db() {
  81. if(function_exists("mysql_connect")) {
  82. require_once UC_ROOT.'lib/db.class.php';
  83. } else {
  84. require_once UC_ROOT.'lib/dbi.class.php';
  85. }
  86. $this->db = new ucserver_db();
  87. $this->db->connect(UC_DBHOST, UC_DBUSER, UC_DBPW, UC_DBNAME, UC_DBCHARSET, UC_DBCONNECT, UC_DBTABLEPRE);
  88. }
  89. function init_app() {
  90. $appid = intval(getgpc('appid'));
  91. $appid && $this->app = $this->cache['apps'][$appid];
  92. }
  93. function init_user() {
  94. if(isset($_COOKIE['uc_auth'])) {
  95. @list($uid, $username, $agent) = explode('|', $this->authcode($_COOKIE['uc_auth'], 'DECODE', ($this->input ? $this->app['appauthkey'] : UC_KEY)));
  96. if($agent != md5($_SERVER['HTTP_USER_AGENT'])) {
  97. $this->setcookie('uc_auth', '');
  98. } else {
  99. @$this->user['uid'] = $uid;
  100. @$this->user['username'] = $username;
  101. }
  102. }
  103. }
  104. function init_template() {
  105. $charset = UC_CHARSET;
  106. require_once UC_ROOT.'lib/template.class.php';
  107. $this->view = new template();
  108. $this->view->assign('dbhistories', $this->db->histories);
  109. $this->view->assign('charset', $charset);
  110. $this->view->assign('dbquerynum', $this->db->querynum);
  111. $this->view->assign('user', $this->user);
  112. }
  113. function init_note() {
  114. if($this->note_exists() && !getgpc('inajax')) {
  115. $this->load('note');
  116. $_ENV['note']->send();
  117. }
  118. }
  119. function init_mail() {
  120. if($this->mail_exists() && !getgpc('inajax')) {
  121. $this->load('mail');
  122. $_ENV['mail']->send();
  123. }
  124. }
  125. function authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) {
  126. $ckey_length = 4;
  127. $key = md5($key ? $key : UC_KEY);
  128. $keya = md5(substr($key, 0, 16));
  129. $keyb = md5(substr($key, 16, 16));
  130. $keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length): substr(md5(microtime()), -$ckey_length)) : '';
  131. $cryptkey = $keya.md5($keya.$keyc);
  132. $key_length = strlen($cryptkey);
  133. $string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', $expiry ? $expiry + time() : 0).substr(md5($string.$keyb), 0, 16).$string;
  134. $string_length = strlen($string);
  135. $result = '';
  136. $box = range(0, 255);
  137. $rndkey = array();
  138. for($i = 0; $i <= 255; $i++) {
  139. $rndkey[$i] = ord($cryptkey[$i % $key_length]);
  140. }
  141. for($j = $i = 0; $i < 256; $i++) {
  142. $j = ($j + $box[$i] + $rndkey[$i]) % 256;
  143. $tmp = $box[$i];
  144. $box[$i] = $box[$j];
  145. $box[$j] = $tmp;
  146. }
  147. for($a = $j = $i = 0; $i < $string_length; $i++) {
  148. $a = ($a + 1) % 256;
  149. $j = ($j + $box[$a]) % 256;
  150. $tmp = $box[$a];
  151. $box[$a] = $box[$j];
  152. $box[$j] = $tmp;
  153. $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
  154. }
  155. if($operation == 'DECODE') {
  156. if((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16)) {
  157. return substr($result, 26);
  158. } else {
  159. return '';
  160. }
  161. } else {
  162. return $keyc.str_replace('=', '', base64_encode($result));
  163. }
  164. }
  165. function page($num, $perpage, $curpage, $mpurl) {
  166. $multipage = '';
  167. $mpurl .= strpos($mpurl, '?') ? '&' : '?';
  168. if($num > $perpage) {
  169. $page = 10;
  170. $offset = 2;
  171. $pages = @ceil($num / $perpage);
  172. if($page > $pages) {
  173. $from = 1;
  174. $to = $pages;
  175. } else {
  176. $from = $curpage - $offset;
  177. $to = $from + $page - 1;
  178. if($from < 1) {
  179. $to = $curpage + 1 - $from;
  180. $from = 1;
  181. if($to - $from < $page) {
  182. $to = $page;
  183. }
  184. } elseif($to > $pages) {
  185. $from = $pages - $page + 1;
  186. $to = $pages;
  187. }
  188. }
  189. $multipage = ($curpage - $offset > 1 && $pages > $page ? '<a href="'.$mpurl.'page=1" class="first"'.$ajaxtarget.'>1 ...</a>' : '').
  190. ($curpage > 1 && !$simple ? '<a href="'.$mpurl.'page='.($curpage - 1).'" class="prev"'.$ajaxtarget.'>&lsaquo;&lsaquo;</a>' : '');
  191. for($i = $from; $i <= $to; $i++) {
  192. $multipage .= $i == $curpage ? '<strong>'.$i.'</strong>' :
  193. '<a href="'.$mpurl.'page='.$i.($ajaxtarget && $i == $pages && $autogoto ? '#' : '').'"'.$ajaxtarget.'>'.$i.'</a>';
  194. }
  195. $multipage .= ($curpage < $pages && !$simple ? '<a href="'.$mpurl.'page='.($curpage + 1).'" class="next"'.$ajaxtarget.'>&rsaquo;&rsaquo;</a>' : '').
  196. ($to < $pages ? '<a href="'.$mpurl.'page='.$pages.'" class="last"'.$ajaxtarget.'>... '.$realpages.'</a>' : '').
  197. (!$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>' : '');
  198. $multipage = $multipage ? '<div class="pages">'.(!$simple ? '<em>&nbsp;'.$num.'&nbsp;</em>' : '').$multipage.'</div>' : '';
  199. }
  200. return $multipage;
  201. }
  202. function page_get_start($page, $ppp, $totalnum) {
  203. $totalpage = ceil($totalnum / $ppp);
  204. $page = max(1, min($totalpage, intval($page)));
  205. return ($page - 1) * $ppp;
  206. }
  207. function load($model, $base = NULL, $release = '') {
  208. $base = $base ? $base : $this;
  209. if(empty($_ENV[$model])) {
  210. $release = !$release ? RELEASE_ROOT : $release;
  211. if(file_exists(UC_ROOT.$release."model/$model.php")) {
  212. require_once UC_ROOT.$release."model/$model.php";
  213. } else {
  214. require_once UC_ROOT."model/$model.php";
  215. }
  216. eval('$_ENV[$model] = new '.$model.'model($base);');
  217. }
  218. return $_ENV[$model];
  219. }
  220. function get_setting($k = array(), $decode = FALSE) {
  221. $return = array();
  222. $sqladd = $k ? "WHERE k IN (".$this->implode($k).")" : '';
  223. $settings = $this->db->fetch_all("SELECT * FROM ".UC_DBTABLEPRE."settings $sqladd");
  224. if(is_array($settings)) {
  225. foreach($settings as $arr) {
  226. $return[$arr['k']] = $decode ? unserialize($arr['v']) : $arr['v'];
  227. }
  228. }
  229. return $return;
  230. }
  231. function set_setting($k, $v, $encode = FALSE) {
  232. $v = is_array($v) || $encode ? addslashes(serialize($v)) : $v;
  233. $this->db->query("REPLACE INTO ".UC_DBTABLEPRE."settings SET k='$k', v='$v'");
  234. }
  235. function message($message, $redirect = '', $type = 0, $vars = array()) {
  236. include_once UC_ROOT.'view/default/messages.lang.php';
  237. if(isset($lang[$message])) {
  238. $message = $lang[$message] ? str_replace(array_keys($vars), array_values($vars), $lang[$message]) : $message;
  239. }
  240. $this->view->assign('message', $message);
  241. if(!strpos($redirect, 'sid=') && (!strpos($redirect, 'ttp://'))) {
  242. if(!strpos($redirect, '?')) {
  243. $redirect .= '?sid='.$this->sid;
  244. } else {
  245. $redirect .= '&sid='.$this->sid;
  246. }
  247. }
  248. $this->view->assign('redirect', $redirect);
  249. if($type == 0) {
  250. $this->view->display('message');
  251. } elseif($type == 1) {
  252. $this->view->display('message_client');
  253. }
  254. exit;
  255. }
  256. function formhash() {
  257. return substr(md5(substr($this->time, 0, -4).UC_KEY), 16);
  258. }
  259. function submitcheck() {
  260. return @getgpc('formhash', 'P') == FORMHASH ? true : false;
  261. }
  262. function date($time, $type = 3) {
  263. $format[] = $type & 2 ? (!empty($this->settings['dateformat']) ? $this->settings['dateformat'] : 'Y-n-j') : '';
  264. $format[] = $type & 1 ? (!empty($this->settings['timeformat']) ? $this->settings['timeformat'] : 'H:i') : '';
  265. return gmdate(implode(' ', $format), $time + $this->settings['timeoffset']);
  266. }
  267. function implode($arr) {
  268. return "'".implode("','", (array)$arr)."'";
  269. }
  270. function set_home($uid, $dir = '.') {
  271. $uid = sprintf("%09d", $uid);
  272. $dir1 = substr($uid, 0, 3);
  273. $dir2 = substr($uid, 3, 2);
  274. $dir3 = substr($uid, 5, 2);
  275. !is_dir($dir.'/'.$dir1) && mkdir($dir.'/'.$dir1, 0777);
  276. !is_dir($dir.'/'.$dir1.'/'.$dir2) && mkdir($dir.'/'.$dir1.'/'.$dir2, 0777);
  277. !is_dir($dir.'/'.$dir1.'/'.$dir2.'/'.$dir3) && mkdir($dir.'/'.$dir1.'/'.$dir2.'/'.$dir3, 0777);
  278. }
  279. function get_home($uid) {
  280. $uid = sprintf("%09d", $uid);
  281. $dir1 = substr($uid, 0, 3);
  282. $dir2 = substr($uid, 3, 2);
  283. $dir3 = substr($uid, 5, 2);
  284. return $dir1.'/'.$dir2.'/'.$dir3;
  285. }
  286. function get_avatar($uid, $size = 'big', $type = '') {
  287. $size = in_array($size, array('big', 'middle', 'small')) ? $size : 'big';
  288. $uid = abs(intval($uid));
  289. $uid = sprintf("%09d", $uid);
  290. $dir1 = substr($uid, 0, 3);
  291. $dir2 = substr($uid, 3, 2);
  292. $dir3 = substr($uid, 5, 2);
  293. $typeadd = $type == 'real' ? '_real' : '';
  294. return $dir1.'/'.$dir2.'/'.$dir3.'/'.substr($uid, -2).$typeadd."_avatar_$size.jpg";
  295. }
  296. function &cache($cachefile) {
  297. static $_CACHE = array();
  298. if(!isset($_CACHE[$cachefile])) {
  299. $cachepath = UC_DATADIR.'./cache/'.$cachefile.'.php';
  300. if(!file_exists($cachepath)) {
  301. $this->load('cache');
  302. $_ENV['cache']->updatedata($cachefile);
  303. } else {
  304. include_once $cachepath;
  305. }
  306. }
  307. return $_CACHE[$cachefile];
  308. }
  309. function input($k) {
  310. return isset($this->input[$k]) ? (is_array($this->input[$k]) ? $this->input[$k] : trim($this->input[$k])) : NULL;
  311. }
  312. function serialize($s, $htmlon = 0) {
  313. if(file_exists(UC_ROOT.RELEASE_ROOT.'./lib/xml.class.php')) {
  314. include_once UC_ROOT.RELEASE_ROOT.'./lib/xml.class.php';
  315. } else {
  316. include_once UC_ROOT.'./lib/xml.class.php';
  317. }
  318. return xml_serialize($s, $htmlon);
  319. }
  320. function unserialize($s) {
  321. if(file_exists(UC_ROOT.RELEASE_ROOT.'./lib/xml.class.php')) {
  322. include_once UC_ROOT.RELEASE_ROOT.'./lib/xml.class.php';
  323. } else {
  324. include_once UC_ROOT.'./lib/xml.class.php';
  325. }
  326. return xml_unserialize($s);
  327. }
  328. function cutstr($string, $length, $dot = ' ...') {
  329. if(strlen($string) <= $length) {
  330. return $string;
  331. }
  332. $string = str_replace(array('&amp;', '&quot;', '&lt;', '&gt;'), array('&', '"', '<', '>'), $string);
  333. $strcut = '';
  334. if(strtolower(UC_CHARSET) == 'utf-8') {
  335. $n = $tn = $noc = 0;
  336. while($n < strlen($string)) {
  337. $t = ord($string[$n]);
  338. if($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) {
  339. $tn = 1; $n++; $noc++;
  340. } elseif(194 <= $t && $t <= 223) {
  341. $tn = 2; $n += 2; $noc += 2;
  342. } elseif(224 <= $t && $t < 239) {
  343. $tn = 3; $n += 3; $noc += 2;
  344. } elseif(240 <= $t && $t <= 247) {
  345. $tn = 4; $n += 4; $noc += 2;
  346. } elseif(248 <= $t && $t <= 251) {
  347. $tn = 5; $n += 5; $noc += 2;
  348. } elseif($t == 252 || $t == 253) {
  349. $tn = 6; $n += 6; $noc += 2;
  350. } else {
  351. $n++;
  352. }
  353. if($noc >= $length) {
  354. break;
  355. }
  356. }
  357. if($noc > $length) {
  358. $n -= $tn;
  359. }
  360. $strcut = substr($string, 0, $n);
  361. } else {
  362. for($i = 0; $i < $length; $i++) {
  363. $strcut .= ord($string[$i]) > 127 ? $string[$i].$string[++$i] : $string[$i];
  364. }
  365. }
  366. $strcut = str_replace(array('&', '"', '<', '>'), array('&amp;', '&quot;', '&lt;', '&gt;'), $strcut);
  367. return $strcut.$dot;
  368. }
  369. function setcookie($key, $value, $life = 0, $httponly = false) {
  370. (!defined('UC_COOKIEPATH')) && define('UC_COOKIEPATH', '/');
  371. (!defined('UC_COOKIEDOMAIN')) && define('UC_COOKIEDOMAIN', '');
  372. if($value == '' || $life < 0) {
  373. $value = '';
  374. $life = -1;
  375. }
  376. $life = $life > 0 ? $this->time + $life : ($life < 0 ? $this->time - 31536000 : 0);
  377. $path = $httponly && PHP_VERSION < '5.2.0' ? UC_COOKIEPATH."; HttpOnly" : UC_COOKIEPATH;
  378. $secure = $_SERVER['SERVER_PORT'] == 443 ? 1 : 0;
  379. if(PHP_VERSION < '5.2.0') {
  380. setcookie($key, $value, $life, $path, UC_COOKIEDOMAIN, $secure);
  381. } else {
  382. setcookie($key, $value, $life, $path, UC_COOKIEDOMAIN, $secure, $httponly);
  383. }
  384. }
  385. function note_exists() {
  386. $noteexists = $this->db->result_first("SELECT value FROM ".UC_DBTABLEPRE."vars WHERE name='noteexists'");
  387. if(empty($noteexists)) {
  388. return FALSE;
  389. } else {
  390. return TRUE;
  391. }
  392. }
  393. function mail_exists() {
  394. $mailexists = $this->db->result_first("SELECT value FROM ".UC_DBTABLEPRE."vars WHERE name='mailexists'");
  395. if(empty($mailexists)) {
  396. return FALSE;
  397. } else {
  398. return TRUE;
  399. }
  400. }
  401. function dstripslashes($string) {
  402. if(is_array($string)) {
  403. foreach($string as $key => $val) {
  404. $string[$key] = $this->dstripslashes($val);
  405. }
  406. } else {
  407. $string = stripslashes($string);
  408. }
  409. return $string;
  410. }
  411. }
  412. ?>