base.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 1059 2011-03-01 07:25:09Z monkey $
  6. */
  7. !defined('IN_UC') && exit('Access Denied');
  8. if(!function_exists('getgpc')) {
  9. function getgpc($k, $var='G') {
  10. switch($var) {
  11. case 'G': $var = &$_GET; break;
  12. case 'P': $var = &$_POST; break;
  13. case 'C': $var = &$_COOKIE; break;
  14. case 'R': $var = &$_REQUEST; break;
  15. }
  16. return isset($var[$k]) ? $var[$k] : NULL;
  17. }
  18. }
  19. class base {
  20. var $time;
  21. var $onlineip;
  22. var $db;
  23. var $key;
  24. var $settings = array();
  25. var $cache = array();
  26. var $app = array();
  27. var $user = array();
  28. var $input = array();
  29. function __construct() {
  30. $this->base();
  31. }
  32. function base() {
  33. $this->init_var();
  34. $this->init_db();
  35. $this->init_cache();
  36. $this->init_note();
  37. $this->init_mail();
  38. }
  39. function init_var() {
  40. $this->time = time();
  41. $cip = getenv('HTTP_CLIENT_IP');
  42. $xip = getenv('HTTP_X_FORWARDED_FOR');
  43. $rip = getenv('REMOTE_ADDR');
  44. $srip = $_SERVER['REMOTE_ADDR'];
  45. if($cip && strcasecmp($cip, 'unknown')) {
  46. $this->onlineip = $cip;
  47. } elseif($xip && strcasecmp($xip, 'unknown')) {
  48. $this->onlineip = $xip;
  49. } elseif($rip && strcasecmp($rip, 'unknown')) {
  50. $this->onlineip = $rip;
  51. } elseif($srip && strcasecmp($srip, 'unknown')) {
  52. $this->onlineip = $srip;
  53. }
  54. preg_match("/[\d\.]{7,15}/", $this->onlineip, $match);
  55. $this->onlineip = $match[0] ? $match[0] : 'unknown';
  56. $this->app['appid'] = UC_APPID;
  57. }
  58. function init_input() {
  59. }
  60. function init_db() {
  61. require_once UC_ROOT.'lib/db.class.php';
  62. $this->db = new ucclient_db();
  63. $this->db->connect(UC_DBHOST, UC_DBUSER, UC_DBPW, '', UC_DBCHARSET, UC_DBCONNECT, UC_DBTABLEPRE);
  64. }
  65. function load($model, $base = NULL) {
  66. $base = $base ? $base : $this;
  67. if(empty($_ENV[$model])) {
  68. require_once UC_ROOT."./model/$model.php";
  69. eval('$_ENV[$model] = new '.$model.'model($base);');
  70. }
  71. return $_ENV[$model];
  72. }
  73. function date($time, $type = 3) {
  74. if(!$this->settings) {
  75. $this->settings = $this->cache('settings');
  76. }
  77. $format[] = $type & 2 ? (!empty($this->settings['dateformat']) ? $this->settings['dateformat'] : 'Y-n-j') : '';
  78. $format[] = $type & 1 ? (!empty($this->settings['timeformat']) ? $this->settings['timeformat'] : 'H:i') : '';
  79. return gmdate(implode(' ', $format), $time + $this->settings['timeoffset']);
  80. }
  81. function page_get_start($page, $ppp, $totalnum) {
  82. $totalpage = ceil($totalnum / $ppp);
  83. $page = max(1, min($totalpage,intval($page)));
  84. return ($page - 1) * $ppp;
  85. }
  86. function implode($arr) {
  87. return "'".implode("','", (array)$arr)."'";
  88. }
  89. function &cache($cachefile) {
  90. static $_CACHE = array();
  91. if(!isset($_CACHE[$cachefile])) {
  92. $cachepath = UC_DATADIR.'./cache/'.$cachefile.'.php';
  93. if(!file_exists($cachepath)) {
  94. $this->load('cache');
  95. $_ENV['cache']->updatedata($cachefile);
  96. } else {
  97. include_once $cachepath;
  98. }
  99. }
  100. return $_CACHE[$cachefile];
  101. }
  102. function get_setting($k = array(), $decode = FALSE) {
  103. $return = array();
  104. $sqladd = $k ? "WHERE k IN (".$this->implode($k).")" : '';
  105. $settings = $this->db->fetch_all("SELECT * FROM ".UC_DBTABLEPRE."settings $sqladd");
  106. if(is_array($settings)) {
  107. foreach($settings as $arr) {
  108. $return[$arr['k']] = $decode ? unserialize($arr['v']) : $arr['v'];
  109. }
  110. }
  111. return $return;
  112. }
  113. function init_cache() {
  114. $this->settings = $this->cache('settings');
  115. $this->cache['apps'] = $this->cache('apps');
  116. if(PHP_VERSION > '5.1') {
  117. $timeoffset = intval($this->settings['timeoffset'] / 3600);
  118. @date_default_timezone_set('Etc/GMT'.($timeoffset > 0 ? '-' : '+').(abs($timeoffset)));
  119. }
  120. }
  121. function cutstr($string, $length, $dot = ' ...') {
  122. if(strlen($string) <= $length) {
  123. return $string;
  124. }
  125. $string = str_replace(array('&amp;', '&quot;', '&lt;', '&gt;'), array('&', '"', '<', '>'), $string);
  126. $strcut = '';
  127. if(strtolower(UC_CHARSET) == 'utf-8') {
  128. $n = $tn = $noc = 0;
  129. while($n < strlen($string)) {
  130. $t = ord($string[$n]);
  131. if($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) {
  132. $tn = 1; $n++; $noc++;
  133. } elseif(194 <= $t && $t <= 223) {
  134. $tn = 2; $n += 2; $noc += 2;
  135. } elseif(224 <= $t && $t < 239) {
  136. $tn = 3; $n += 3; $noc += 2;
  137. } elseif(240 <= $t && $t <= 247) {
  138. $tn = 4; $n += 4; $noc += 2;
  139. } elseif(248 <= $t && $t <= 251) {
  140. $tn = 5; $n += 5; $noc += 2;
  141. } elseif($t == 252 || $t == 253) {
  142. $tn = 6; $n += 6; $noc += 2;
  143. } else {
  144. $n++;
  145. }
  146. if($noc >= $length) {
  147. break;
  148. }
  149. }
  150. if($noc > $length) {
  151. $n -= $tn;
  152. }
  153. $strcut = substr($string, 0, $n);
  154. } else {
  155. for($i = 0; $i < $length; $i++) {
  156. $strcut .= ord($string[$i]) > 127 ? $string[$i].$string[++$i] : $string[$i];
  157. }
  158. }
  159. $strcut = str_replace(array('&', '"', '<', '>'), array('&amp;', '&quot;', '&lt;', '&gt;'), $strcut);
  160. return $strcut.$dot;
  161. }
  162. function init_note() {
  163. if($this->note_exists()) {
  164. $this->load('note');
  165. $_ENV['note']->send();
  166. }
  167. }
  168. function note_exists() {
  169. $noteexists = $this->db->fetch_first("SELECT value FROM ".UC_DBTABLEPRE."vars WHERE name='noteexists".UC_APPID."'");
  170. if(empty($noteexists)) {
  171. return FALSE;
  172. } else {
  173. return TRUE;
  174. }
  175. }
  176. function init_mail() {
  177. if($this->mail_exists() && !getgpc('inajax')) {
  178. $this->load('mail');
  179. $_ENV['mail']->send();
  180. }
  181. }
  182. function authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) {
  183. return uc_authcode($string, $operation, $key, $expiry);
  184. }
  185. /*
  186. function serialize() {
  187. }
  188. */
  189. function unserialize($s) {
  190. return uc_unserialize($s);
  191. }
  192. function input($k) {
  193. return isset($this->input[$k]) ? (is_array($this->input[$k]) ? $this->input[$k] : trim($this->input[$k])) : NULL;
  194. }
  195. function mail_exists() {
  196. $mailexists = $this->db->fetch_first("SELECT value FROM ".UC_DBTABLEPRE."vars WHERE name='mailexists'");
  197. if(empty($mailexists)) {
  198. return FALSE;
  199. } else {
  200. return TRUE;
  201. }
  202. }
  203. function dstripslashes($string) {
  204. if(is_array($string)) {
  205. foreach($string as $key => $val) {
  206. $string[$key] = $this->dstripslashes($val);
  207. }
  208. } else {
  209. $string = stripslashes($string);
  210. }
  211. return $string;
  212. }
  213. }
  214. ?>