base.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. {
  10. function getgpc($k, $var = 'G')
  11. {
  12. switch ($var)
  13. {
  14. case 'G': $var = &$_GET;
  15. break;
  16. case 'P': $var = &$_POST;
  17. break;
  18. case 'C': $var = &$_COOKIE;
  19. break;
  20. case 'R': $var = &$_REQUEST;
  21. break;
  22. }
  23. return isset($var[$k]) ? $var[$k] : NULL;
  24. }
  25. }
  26. class base
  27. {
  28. var $sid;
  29. var $time;
  30. var $onlineip;
  31. var $db;
  32. var $key;
  33. var $settings = array();
  34. var $cache = array();
  35. var $app = array();
  36. var $user = array();
  37. var $input = array();
  38. function __construct()
  39. {
  40. $this->base();
  41. }
  42. function base()
  43. {
  44. $this->init_var();
  45. $this->init_db();
  46. $this->init_cache();
  47. $this->init_note();
  48. $this->init_mail();
  49. }
  50. function init_var()
  51. {
  52. $this->time = time();
  53. $cip = getenv('HTTP_CLIENT_IP');
  54. $xip = getenv('HTTP_X_FORWARDED_FOR');
  55. $rip = getenv('REMOTE_ADDR');
  56. $srip = $_SERVER['REMOTE_ADDR'];
  57. if ($cip && strcasecmp($cip, 'unknown'))
  58. {
  59. $this->onlineip = $cip;
  60. }
  61. elseif ($xip && strcasecmp($xip, 'unknown'))
  62. {
  63. $this->onlineip = $xip;
  64. }
  65. elseif ($rip && strcasecmp($rip, 'unknown'))
  66. {
  67. $this->onlineip = $rip;
  68. }
  69. elseif ($srip && strcasecmp($srip, 'unknown'))
  70. {
  71. $this->onlineip = $srip;
  72. }
  73. preg_match("/[\d\.]{7,15}/", $this->onlineip, $match);
  74. $this->onlineip = $match[0] ? $match[0] : 'unknown';
  75. $this->app['appid'] = UC_APPID;
  76. }
  77. function init_input()
  78. {
  79. }
  80. function init_db()
  81. {
  82. if (function_exists("mysqli_connect"))
  83. {
  84. require_once UC_ROOT . 'lib/dbi.class.php';
  85. }
  86. else
  87. {
  88. require_once UC_ROOT . 'lib/db.class.php';
  89. }
  90. $this->db = new ucclient_db();
  91. $this->db->connect(UC_DBHOST, UC_DBUSER, UC_DBPW, '', UC_DBCHARSET, UC_DBCONNECT, UC_DBTABLEPRE);
  92. }
  93. function load($model, $base = NULL)
  94. {
  95. $base = $base ? $base : $this;
  96. if (empty($_ENV[$model]))
  97. {
  98. require_once UC_ROOT . "./model/{$model}.php";
  99. $classname = $model . 'model';
  100. $_ENV[$model] = new $classname($base);
  101. }
  102. return $_ENV[$model];
  103. }
  104. function date($time, $type = 3)
  105. {
  106. if (!$this->settings)
  107. {
  108. $this->settings = $this->cache('settings');
  109. }
  110. $format[] = $type & 2 ? (!empty($this->settings['dateformat']) ? $this->settings['dateformat'] : 'Y-n-j') : '';
  111. $format[] = $type & 1 ? (!empty($this->settings['timeformat']) ? $this->settings['timeformat'] : 'H:i') : '';
  112. return gmdate(implode(' ', $format), $time + $this->settings['timeoffset']);
  113. }
  114. function page_get_start($page, $ppp, $totalnum)
  115. {
  116. $totalpage = ceil($totalnum / $ppp);
  117. $page = max(1, min($totalpage, intval($page)));
  118. return ($page - 1) * $ppp;
  119. }
  120. function implode($arr)
  121. {
  122. return "'" . implode("','", (array) $arr) . "'";
  123. }
  124. function &cache($cachefile)
  125. {
  126. static $_CACHE = array();
  127. if (!isset($_CACHE[$cachefile]))
  128. {
  129. $cachepath = UC_DATADIR . './cache/' . $cachefile . '.php';
  130. if (!file_exists($cachepath))
  131. {
  132. $this->load('cache');
  133. $_ENV['cache']->updatedata($cachefile);
  134. }
  135. else
  136. {
  137. include_once $cachepath;
  138. }
  139. }
  140. return $_CACHE[$cachefile];
  141. }
  142. function get_setting($k = array(), $decode = FALSE)
  143. {
  144. $return = array();
  145. $sqladd = $k ? "WHERE k IN (" . $this->implode($k) . ")" : '';
  146. $settings = $this->db->fetch_all("SELECT * FROM " . UC_DBTABLEPRE . "settings $sqladd");
  147. if (is_array($settings))
  148. {
  149. foreach ($settings as $arr)
  150. {
  151. $return[$arr['k']] = $decode ? unserialize($arr['v']) : $arr['v'];
  152. }
  153. }
  154. return $return;
  155. }
  156. function init_cache()
  157. {
  158. $this->settings = $this->cache('settings');
  159. $this->cache['apps'] = $this->cache('apps');
  160. if (PHP_VERSION > '5.1')
  161. {
  162. $timeoffset = intval($this->settings['timeoffset'] / 3600);
  163. @date_default_timezone_set('Etc/GMT' . ($timeoffset > 0 ? '-' : '+') . (abs($timeoffset)));
  164. }
  165. }
  166. function cutstr($string, $length, $dot = ' ...')
  167. {
  168. if (strlen($string) <= $length)
  169. {
  170. return $string;
  171. }
  172. $string = str_replace(array('&amp;', '&quot;', '&lt;', '&gt;'), array('&', '"', '<', '>'), $string);
  173. $strcut = '';
  174. if (strtolower(UC_CHARSET) == 'utf-8')
  175. {
  176. $n = $tn = $noc = 0;
  177. while ($n < strlen($string))
  178. {
  179. $t = ord($string[$n]);
  180. if ($t == 9 || $t == 10 || (32 <= $t && $t <= 126))
  181. {
  182. $tn = 1;
  183. $n++;
  184. $noc++;
  185. }
  186. elseif (194 <= $t && $t <= 223)
  187. {
  188. $tn = 2;
  189. $n += 2;
  190. $noc += 2;
  191. }
  192. elseif (224 <= $t && $t < 239)
  193. {
  194. $tn = 3;
  195. $n += 3;
  196. $noc += 2;
  197. }
  198. elseif (240 <= $t && $t <= 247)
  199. {
  200. $tn = 4;
  201. $n += 4;
  202. $noc += 2;
  203. }
  204. elseif (248 <= $t && $t <= 251)
  205. {
  206. $tn = 5;
  207. $n += 5;
  208. $noc += 2;
  209. }
  210. elseif ($t == 252 || $t == 253)
  211. {
  212. $tn = 6;
  213. $n += 6;
  214. $noc += 2;
  215. }
  216. else
  217. {
  218. $n++;
  219. }
  220. if ($noc >= $length)
  221. {
  222. break;
  223. }
  224. }
  225. if ($noc > $length)
  226. {
  227. $n -= $tn;
  228. }
  229. $strcut = substr($string, 0, $n);
  230. }
  231. else
  232. {
  233. for ($i = 0; $i < $length; $i++)
  234. {
  235. $strcut .= ord($string[$i]) > 127 ? $string[$i] . $string[++$i] : $string[$i];
  236. }
  237. }
  238. $strcut = str_replace(array('&', '"', '<', '>'), array('&amp;', '&quot;', '&lt;', '&gt;'), $strcut);
  239. return $strcut . $dot;
  240. }
  241. function init_note()
  242. {
  243. if ($this->note_exists())
  244. {
  245. $this->load('note');
  246. $_ENV['note']->send();
  247. }
  248. }
  249. function note_exists()
  250. {
  251. $noteexists = $this->db->result_first("SELECT value FROM " . UC_DBTABLEPRE . "vars WHERE name='noteexists" . UC_APPID . "'");
  252. if (empty($noteexists))
  253. {
  254. return FALSE;
  255. }
  256. else
  257. {
  258. return TRUE;
  259. }
  260. }
  261. function init_mail()
  262. {
  263. if ($this->mail_exists() && !getgpc('inajax'))
  264. {
  265. $this->load('mail');
  266. $_ENV['mail']->send();
  267. }
  268. }
  269. function authcode($string, $operation = 'DECODE', $key = '', $expiry = 0)
  270. {
  271. return uc_authcode($string, $operation, $key, $expiry);
  272. }
  273. function unserialize($s)
  274. {
  275. return uc_unserialize($s);
  276. }
  277. function input($k)
  278. {
  279. return isset($this->input[$k]) ? (is_array($this->input[$k]) ? $this->input[$k] : trim($this->input[$k])) : NULL;
  280. }
  281. function mail_exists()
  282. {
  283. $mailexists = $this->db->result_first("SELECT value FROM " . UC_DBTABLEPRE . "vars WHERE name='mailexists'");
  284. if (empty($mailexists))
  285. {
  286. return FALSE;
  287. }
  288. else
  289. {
  290. return TRUE;
  291. }
  292. }
  293. function dstripslashes($string)
  294. {
  295. if (is_array($string))
  296. {
  297. foreach ($string as $key => $val)
  298. {
  299. $string[$key] = $this->dstripslashes($val);
  300. }
  301. }
  302. else
  303. {
  304. $string = stripslashes($string);
  305. }
  306. return $string;
  307. }
  308. }