chengkun
2025-05-26 8f3df543230cd4403368b39b9bbe5726d11a0284
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
!defined('P_W') && exit('Forbidden');
/*uc_client 客户端类型
* 1:独立系统;
* 2:用户中心服务端;
* 3:用户中心子系统
*/
 
define('UC_SERVER',UC_API);
//相对于用户中心的应用id
define('UC_APPID', (UC_API && UC_APPID > 0) ? UC_APPID : 0);
define('UC_CONNECT_METHOD','mysql');
define('UC_DBPRE',UC_DBTABLEPRE);
define('UC_DBPCONNECT', '0');
 
class UC {
    
    var $db;
    var $time;
    var $appid;
    var $onlineip;
    var $user = array();
    var $controls = array();
    var $models = array();
    var $cache = null;
 
    function __construct() {
        $this->UC();
    }
 
    function UC() {
 
        $this->time = time();
        $this->appid = UC_APPID;
        $this->onlineip = 'unknown';
 
        if ($_SERVER['HTTP_X_FORWARDED_FOR'] && preg_match('/^([0-9]{1,3}\.){3}[0-9]{1,3}$/', $_SERVER['HTTP_X_FORWARDED_FOR'])) {
            $this->onlineip = $_SERVER['HTTP_X_FORWARDED_FOR'];
        } elseif ($_SERVER['HTTP_CLIENT_IP'] && preg_match('/^([0-9]{1,3}\.){3}[0-9]{1,3}$/', $_SERVER['HTTP_CLIENT_IP'])) {
            $this->onlineip = $_SERVER['HTTP_CLIENT_IP'];
        } elseif (preg_match('/^([0-9]{1,3}\.){3}[0-9]{1,3}$/',$_SERVER['REMOTE_ADDR'])) {
            $this->onlineip = $_SERVER['REMOTE_ADDR'];
        }
        
        $this->init_db();
        $this->init_notify();
    }
 
    function init_db() {
        require_once S_DIR .'/pw_client/class_db.php';
        $this->db = new UcDB(UC_DBHOST, UC_DBUSER, UC_DBPW, UC_DBNAME, UC_DBPCONNECT, UC_DBCHARSET);
    }
 
    function init_notify() {
        if (UC_SERVER == 2 && $this->config('uc_syncreditexists' . $this->appid)) {
            $credit = $this->load('credit');
            $credit->synupdate();
        }
        if (UC_SERVER > 0 && $this->config('uc_notifyexists' . $this->appid)) {
            $notify = $this->load('notify');
            $notify->send();
        }
    }
 
    function config($var) {
        if ($this->cache == null) {
            $this->cache = array();
            $query = $this->db->query("SELECT * FROM pw_config");
            while ($rt = $this->db->fetch_array($query)) {
                if ($rt['vtype'] == 'array' && !is_array($rt['db_value'] = unserialize($rt['db_value']))) {
                    $rt['db_value'] = array();
                }
                $this->cache[$rt['db_name']] = $rt['db_value'];
            }
        }
        return $this->cache[$var];
    }
 
    function control($model) {
        if (empty($this->controls[$model])) {
            require_once Pcv(UC_CLIENT_ROOT . "control/{$model}.php");
            eval('$this->controls[$model] = new '.$model.'control($this);');
        }
        return $this->controls[$model];
    }
 
    function load($model) {
        if (empty($this->models[$model])) {
            require_once Pcv(UC_CLIENT_ROOT . "model/{$model}.php");
            eval('$this->models[$model] = new '.$model.'model($this);');
        }
        return $this->models[$model];
    }
 
    //static function
    function escape($var) {
        if (is_array($var)) {
            foreach ($var as $key => $value) {
                $var[$key] = trim(UC::escape($value));
            }
            return $var;
        } elseif (is_numeric($var)) {
            return " '".$var."' ";
        } else {
            return " '".addslashes($var)."' ";
        }
    }
    
    //static function
    function sqlSingle($array) {
        //Copyright (c) 2003-09 PHPWind
        $array = UC::escape($array);
        $str = '';
        foreach ($array as $key => $val) {
            $str .= ($str ? ', ' : ' ').$key.'='.$val;
        }
        return $str;
    }
 
    //static function
    function implode($array) {
        return implode(',', UC::escape($array));
    }
 
    //static function
    function sqlMulti($array) {
        $str = '';
        foreach ($array as $val) {
            if (!empty($val)) {
                $str .= ($str ? ', ' : ' ') . '(' . UC::implode($val) .') ';
            }
        }
        return $str;
    }
 
    //static function
    function strcode($string, $hash_key, $encode = true) {
        !$encode && $string = base64_decode($string);
        $code = '';
        $key  = substr(md5($_SERVER['HTTP_USER_AGENT'] . $hash_key),8,18);
        $keylen = strlen($key);
        $strlen = strlen($string);
        for ($i = 0; $i < $strlen; $i++) {
            $k        = $i % $keylen;
            $code  .= $string[$i] ^ $key[$k];
        }
        return ($encode ? base64_encode($code) : $code);
    }
}
?>