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