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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<?php
 
class creditmodel {
 
    var $base;
    var $db;
    var $relate;
 
    function __construct(&$base) {
        $this->creditmodel($base);
    }
 
    function creditmodel(&$base) {
        $this->base = $base;
        $this->db = $base->db;
    }
 
    function isAllow($cid) {
        return in_array($cid, array('money', 'rvrc', 'credit', 'currency'));
    }
 
    function getRelate($appid) {
        $relate = array();
        $syncredit = $this->base->config('uc_syncredit');
        $myApp = $this->base->load('app');
 
        if ($myApp->isUc($appid)) {
            foreach ($syncredit as $ctype => $value) {
                $relate[$ctype] = $ctype;
            }
        } else {
            foreach ($syncredit as $ctype => $value) {
                if (isset($value[$appid])) {
                    $relate[$value[$appid]] = $ctype;
                }
            }
        }
        return $relate;
    }
 
    function syncredit($uids, $cids) {
        $apps  = $notify = array();
        $myApp = $this->base->load('app');
        $applist = $myApp->applist();
        $fields = array('uid');
        $sp = 0;
        foreach ($applist as $key => $app) {
            if (!$app['uc']) {
                $fields[] = 'app'.$key;
                if ($key == $this->base->appid) {
                    $apps[$key] = 1;
                } elseif (array_intersect($cids, $this->getRelate($key))) {
                    $apps[$key] = 0;
                    $notify[] = array('uc_syncreditexists' . $key, 1);
                } else {
                    $sp = 1;
                }
            }
        }
        if (empty($apps)) {
            return;
        }
        $stats = array();
        if ($sp) {
            $query = $this->db->query("SELECT * FROM pw_ucsyncredit WHERE uid IN(" . UC::implode($uids) . ')');
            while ($rt = $this->db->fetch_array($query)) {
                $stats[$rt['uid']] = $rt;
            }
        }
        $pwSQL = array();
        foreach ($uids as $uid) {
            $sql = array($uid);
            foreach ($applist as $key => $app) {
                if (!$app['uc']) {
                    $sql[] = isset($apps[$key]) ? $apps[$key] : (isset($stats[$uid]) ? $stats[$uid]['app'.$key] : 1);
                }
            }
            $pwSQL[] = $sql;
        }
        if ($pwSQL) {
            $this->db->update("REPLACE INTO pw_ucsyncredit (" . implode(',', $fields) . ") VALUES " . UC::sqlMulti($pwSQL));
        }
        if ($notify) {
            $this->db->update("REPLACE INTO pw_config (db_name, db_value) VALUES " . UC::sqlMulti($notify));
        }
    }
 
    function synupdate($appid = null, $uids = array()) {
        !$appid && $appid = $this->base->appid;
        //$this->_synupdate($appid, $uids);
        register_shutdown_function(array($this, '_synupdate'), $appid, $uids);
    }
 
    function _synupdate($appid, $u_arr) {
        $myApp = $this->base->load('app');
        $applist = $myApp->applist();
        if (!$app = $applist[$appid]) {
            return false;
        }
        $field = 'app' . $appid;
        if ($u_arr) {
            $query = $this->db->query("SELECT * FROM pw_ucsyncredit WHERE uid IN(" . UC::implode($u_arr) . ')');
        } else {
            $query = $this->db->query("SELECT * FROM pw_ucsyncredit WHERE $field < '1' LIMIT 10");
            if ($this->db->num_rows($query) == 0) {
                $this->db->query("UPDATE pw_config SET db_value='0' WHERE db_name=" . UC::escape('uc_syncreditexists'.$appid));
                return false;
            }
        }
        $us = array();
        while ($rt = $this->db->fetch_array($query)) {
            $us[$rt['uid']] = $rt;
        }
        if (empty($us)) {
            return false;
        }
        $uids = array_keys($us);
        $ucds = $this->get($uids);
        $ucds = $this->exchange($ucds, $appid);
        if ($appid == $this->base->appid && file_exists(R_P . 'api/class_base.php')) {
            include(R_P . 'api/class_base.php');
            $api  = new api_client();
            $resp = $api->dataFormat($api->callback('Credit', 'syncredit', array($ucds)));
            $success = isset($resp['result']);
        } else {
            $resp = $myApp->ucfopen($app['siteurl'], $app['interface'], $app['secretkey'], 'Credit', 'syncredit', array($ucds));
            $success = isset($resp['result']);
        }
        if ($success) {
            $cls = array();
            foreach ($us as $key => $data) {
                $data[$field] = 1;
                if ($this->isComplete($data, $applist)) {
                    $cls[] = $key;
                }
            }
            $cls && $this->db->update("DELETE FROM pw_ucsyncredit WHERE uid IN(" . UC::implode($cls) . ')');
            $this->db->update("UPDATE pw_ucsyncredit SET $field='1' WHERE uid IN(" . UC::implode($uids) . ')');
        }
    }
 
    function isComplete($notify, $apps) {
        foreach ($apps as $key => $app) {
            if (!$app['uc'] && $notify['app'.$key] < 1) {
                return false;
            }
        }
        return true;
    }
 
    function add($uid, $cid, $value, $isAdd = true) {
        $cid == 'rvrc' && $value *= 10;
        $value = intval($value);
        if (is_numeric($cid)) {
            $rt = $this->db->get_one("SELECT uid,value FROM pw_membercredit WHERE uid=" . UC::escape($uid) . ' AND cid=' . UC::escape($cid));
            if ($rt) {
                if ($isAdd) {
                    $this->db->update("UPDATE pw_membercredit SET value=value+" . UC::escape($value) .  ' WHERE uid=' . UC::escape($uid) . ' AND cid=' . UC::escape($cid));
                    return $rt['value'] + $value;
                } else {
                    $this->db->update("UPDATE pw_membercredit SET value=" . UC::escape($value) .  ' WHERE uid=' . UC::escape($uid) . ' AND cid=' . UC::escape($cid));
                    return $value;
                }
            } else {
                $this->db->update("INSERT INTO pw_membercredit SET " . UC::sqlSingle(array('uid' => $uid, 'cid' => $cid, 'value' => $value)));
                return $value;
            }
        } elseif ($this->isAllow($cid)) {
            if ($isAdd) {
                $this->db->update("UPDATE pw_memberdata SET $cid=$cid+" . UC::escape($value) . ' WHERE uid=' . UC::escape($uid));
                $cid == 'rvrc' && $cid = "FLOOR(rvrc/10)";
                return $this->db->get_value("SELECT $cid FROM pw_memberdata WHERE uid=" . UC::escape($uid));
            } else {
                $this->db->update("UPDATE pw_memberdata SET $cid=" . UC::escape($value) . ' WHERE uid=' . UC::escape($uid));
                $cid == 'rvrc' && $value /= 10;
                return $value;
            }
        }
        return null;
    }
 
    function exchange($ucds, $appid) {
        $ucredit = array();
        if ($appid > 0) {
            $syncredit = $this->base->config('uc_syncredit');
            foreach ($ucds as $uid => $setv) {
                foreach ($setv as $ctype => $value) {
                    if (isset($syncredit[$ctype][$appid])) {
                        $ucredit[$uid][$syncredit[$ctype][$appid]] = $value;
                    }
                }
            }
        }
        return $ucredit;
    }
 
    function get($uids) {
        $ucredit = $ucd = $mcd = array();
        $syncredit = $this->base->config('uc_syncredit');
        foreach ($syncredit as $key => $value) {
            if (is_numeric($key)) {
                $ucd[] = $key;
            } elseif ($this->isAllow($key)) {
                $key == 'rvrc' && $key = "FLOOR(rvrc/10) AS rvrc";
                $mcd[] = $key;
            }
        }
        if ($mcd) {
            $query = $this->db->query("SELECT uid," . implode(',', $mcd) . " FROM pw_memberdata WHERE uid IN(" . UC::implode($uids) . ')');
            while ($rt = $this->db->fetch_array($query)) {
                $ucredit[$rt['uid']] = $rt;
            }
        }
        if ($ucd) {
            $query = $this->db->query("SELECT * FROM pw_membercredit WHERE uid IN(" . UC::implode($uids) . ') AND cid IN(' . UC::implode($ucd) . ')');
            while ($rt = $this->db->fetch_array($query)) {
                $ucredit[$rt['uid']][$rt['cid']] = $rt['value'];
            }
        }
        return $ucredit;
    }
 
    function get22($uid) {
        $retv = array();
        $query = $this->db->query("SELECT cid,value FROM uc_usercredit WHERE uid=" . UC::escape($uid));
        while ($rt = $this->db->fetch_array($query)) {
            $retv[$rt['cid']] = $rt['value'];
        }
        return $retv;
    }
}
?>