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