chengkun
2025-05-23 a6f7b382623096b6a00924f418447cf5204e825e
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
<?php
 
define('IN_DISCUZ', TRUE);
 
define('UC_CLIENT_VERSION', '1.5.0');    //note UCenter 版本标识
define('UC_CLIENT_RELEASE', '20081031');
 
define('API_DELETEUSER', 1);        //note 用户删除 API 接口开关
define('API_RENAMEUSER', 1);        //note 用户改名 API 接口开关
define('API_GETTAG', 1);        //note 获取标签 API 接口开关
define('API_SYNLOGIN', 1);        //note 同步登录 API 接口开关
define('API_SYNLOGOUT', 1);        //note 同步登出 API 接口开关
define('API_UPDATEPW', 1);        //note 更改用户密码 开关
define('API_UPDATEBADWORDS', 1);    //note 更新关键字列表 开关
define('API_UPDATEHOSTS', 1);        //note 更新域名解析缓存 开关
define('API_UPDATEAPPS', 1);        //note 更新应用列表 开关
define('API_UPDATECLIENT', 1);        //note 更新客户端缓存 开关
define('API_UPDATECREDIT', 1);        //note 更新用户积分 开关
define('API_GETCREDITSETTINGS', 1);    //note 向 UCenter 提供积分设置 开关
define('API_GETCREDIT', 1);        //note 获取用户的某项积分 开关
define('API_UPDATECREDITSETTINGS', 1);    //note 更新应用积分设置 开关
 
define('API_RETURN_SUCCEED', '1');
define('API_RETURN_FAILED', '-1');
define('API_RETURN_FORBIDDEN', '-2');
 
define('DISCUZ_ROOT', '../');
 
//note 普通的 http 通知方式
if(!defined('IN_UC')) {
 
    error_reporting(0);
    set_magic_quotes_runtime(0);
    
    defined('MAGIC_QUOTES_GPC') || define('MAGIC_QUOTES_GPC', get_magic_quotes_gpc());
    require_once DISCUZ_ROOT.'./config.inc.php';
 
    $_DCACHE = $get = $post = array();
 
    $code = @$_GET['code'];
    parse_str(_authcode($code, 'DECODE', UC_KEY), $get);
    if(MAGIC_QUOTES_GPC) {
        $get = _stripslashes($get);
    }
 
    $timestamp = time();
    if($timestamp - $get['time'] > 3600) {
        exit('Authracation has expiried');
    }
    if(empty($get)) {
        exit('Invalid Request');
    }
    $action = $get['action'];
 
    require_once DISCUZ_ROOT.'./uc_client/lib/xml.class.php';
    $post = xml_unserialize(file_get_contents('php://input'));
 
    if(in_array($get['action'], array('test', 'deleteuser', 'renameuser', 'gettag', 'synlogin', 'synlogout', 'updatepw', 'updatebadwords', 'updatehosts', 'updateapps', 'updateclient', 'updatecredit', 'getcreditsettings', 'updatecreditsettings'))) {
        require_once DISCUZ_ROOT.'./include/db_mysql.class.php';
        $GLOBALS['db'] = new dbstuff;
        $GLOBALS['db']->connect($dbhost, $dbuser, $dbpw, $dbname, $pconnect, true, $dbcharset);
        $GLOBALS['tablepre'] = $tablepre;
        unset($dbhost, $dbuser, $dbpw, $dbname, $pconnect);
        $uc_note = new uc_note();
        exit($uc_note->$get['action']($get, $post));
    } else {
        exit(API_RETURN_FAILED);
    }
 
//note include 通知方式
} else {
 
    require_once DISCUZ_ROOT.'./config.inc.php';
    require_once DISCUZ_ROOT.'./include/db_mysql.class.php';
    $GLOBALS['db'] = new dbstuff;
    $GLOBALS['db']->connect($dbhost, $dbuser, $dbpw, $dbname, $pconnect, true, $dbcharset);
    $GLOBALS['tablepre'] = $tablepre;
    unset($dbhost, $dbuser, $dbpw, $dbname, $pconnect);
}
 
class uc_note {
 
    var $dbconfig = '';
    var $db = '';
    var $tablepre = '';
    var $appdir = '';
 
    function _serialize($arr, $htmlon = 0) {
        if(!function_exists('xml_serialize')) {
            include_once DISCUZ_ROOT.'./uc_client/lib/xml.class.php';
        }
        return xml_serialize($arr, $htmlon);
    }
 
    function uc_note() {
        $this->appdir = substr(dirname(__FILE__), 0, -3);
        $this->dbconfig = $this->appdir.'./config.inc.php';
        $this->db = $GLOBALS['db'];
        $this->tablepre = $GLOBALS['tablepre'];
    }
 
    function test($get, $post) {
        return API_RETURN_SUCCEED;
    }
 
    function deleteuser($get, $post) {
        $uids = $get['ids'];
        !API_DELETEUSER && exit(API_RETURN_FORBIDDEN);
 
        //note 用户删除 API 接口
        $threads = array();
 
        $query = $this->db->query("SELECT f.fid, t.tid FROM ".$this->tablepre."threads t LEFT JOIN ".$this->tablepre."forums f ON t.fid=f.fid WHERE t.authorid IN ($uids) ORDER BY f.fid");
        while($thread = $this->db->fetch_array($query)) {
            $threads[$thread['fid']] .= ($threads[$thread['fid']] ? ',' : '').$thread['tid'];
        }
 
        if($threads) {
            require_once $this->appdir.'./forumdata/cache/cache_settings.php';
            foreach($threads as $fid => $tids) {
                $query = $this->db->query("SELECT attachment, thumb, remote FROM ".$this->tablepre."attachments WHERE tid IN ($tids)");
                while($attach = $this->db->fetch_array($query)) {
                    @unlink($_DCACHE['settings']['attachdir'].'/'.$attach['attachment']);
                    $attach['thumb'] && @unlink($_DCACHE['settings']['attachdir'].'/'.$attach['attachment'].'.thumb.jpg');
                }
 
                foreach(array('threads', 'threadsmod', 'relatedthreads', 'posts', 'polls', 'polloptions', 'trades', 'activities', 'activityapplies', 'debates', 'debateposts', 'attachments', 'favorites', 'mythreads', 'myposts', 'subscriptions', 'typeoptionvars', 'forumrecommend') as $value) {
                    $this->db->query("DELETE FROM ".$this->tablepre."$value WHERE tid IN ($tids)", 'UNBUFFERED');
                }
 
                require_once $this->appdir.'./include/post.func.php';
                updateforumcount($fid);
            }
            if($globalstick && $stickmodify) {
                require_once $this->appdir.'./include/cache.func.php';
                updatecache('globalstick');
            }
        }
 
        $query = $this->db->query("DELETE FROM ".$this->tablepre."members WHERE uid IN ($uids)");
        $this->db->query("DELETE FROM ".$this->tablepre."access WHERE uid IN ($uids)", 'UNBUFFERED');
        $this->db->query("DELETE FROM ".$this->tablepre."memberfields WHERE uid IN ($uids)", 'UNBUFFERED');
        $this->db->query("DELETE FROM ".$this->tablepre."favorites WHERE uid IN ($uids)", 'UNBUFFERED');
        $this->db->query("DELETE FROM ".$this->tablepre."moderators WHERE uid IN ($uids)", 'UNBUFFERED');
        $this->db->query("DELETE FROM ".$this->tablepre."subscriptions WHERE uid IN ($uids)", 'UNBUFFERED');
 
        $query = $this->db->query("SELECT uid, attachment, thumb, remote FROM ".$this->tablepre."attachments WHERE uid IN ($uids)");
        while($attach = $this->db->fetch_array($query)) {
            @unlink($_DCACHE['settings']['attachdir'].'/'.$attach['attachment']);
            $attach['thumb'] && @unlink($_DCACHE['settings']['attachdir'].'/'.$attach['attachment'].'.thumb.jpg');
        }
        $this->db->query("DELETE FROM ".$this->tablepre."attachments WHERE uid IN ($uids)");
 
        $this->db->query("DELETE FROM ".$this->tablepre."posts WHERE authorid IN ($uids)");
        $this->db->query("DELETE FROM ".$this->tablepre."trades WHERE sellerid IN ($uids)");
 
        return API_RETURN_SUCCEED;
    }
 
    function renameuser($get, $post) {
        $uid = $get['uid'];
        $usernameold = $get['oldusername'];
        $usernamenew = $get['newusername'];
        if(!API_RENAMEUSER) {
            return API_RETURN_FORBIDDEN;
        }
 
        //note 获取标签 API 接口
        require $this->dbconfig;
        require_once $this->appdir.'./include/db_'.$database.'.class.php';
 
        $db = new dbstuff;
        $this->db->connect($dbhost, $dbuser, $dbpw, $dbname, $pconnect, true, $dbcharset ? $dbcharset : $charset);
        unset($dbhost, $dbuser, $dbpw, $dbname, $pconnect);
 
        $this->db->query("UPDATE ".$this->tablepre."announcements SET author='$usernamenew' WHERE author='$usernameold'");
        $this->db->query("UPDATE ".$this->tablepre."banned SET admin='$usernamenew' WHERE admin='$usernameold'");
        $this->db->query("UPDATE ".$this->tablepre."forums SET lastpost=REPLACE(lastpost, '\t$usernameold', '\t$usernamenew')");
        $this->db->query("UPDATE ".$this->tablepre."members SET username='$usernamenew' WHERE uid='$uid'");
        $this->db->query("UPDATE ".$this->tablepre."pms SET msgfrom='$usernamenew' WHERE msgfromid='$uid'");
        $this->db->query("UPDATE ".$this->tablepre."posts SET author='$usernamenew' WHERE authorid='$uid'");
        $this->db->query("UPDATE ".$this->tablepre."threads SET author='$usernamenew' WHERE authorid='$uid'");
        $this->db->query("UPDATE ".$this->tablepre."threads SET lastposter='$usernamenew' WHERE lastposter='$usernameold'");
        $this->db->query("UPDATE ".$this->tablepre."threadsmod SET username='$usernamenew' WHERE uid='$uid'");
        return API_RETURN_SUCCEED;
    }
 
    function gettag($get, $post) {
        $name = $get['id'];
        if(!API_GETTAG) {
            return API_RETURN_FORBIDDEN;
        }
 
        //note 获取标签 API 接口
        require $this->dbconfig;
        require_once $this->appdir.'./include/db_'.$database.'.class.php';
 
        $db = new dbstuff;
        $this->db->connect($dbhost, $dbuser, $dbpw, $dbname, $pconnect, true, $dbcharset ? $dbcharset : $charset);
        unset($dbhost, $dbuser, $dbpw, $dbname, $pconnect);
 
        $name = trim($name);
        if(empty($name) || !preg_match('/^([\x7f-\xff_-]|\w|\s)+$/', $name) || strlen($name) > 20) {
            return API_RETURN_FAILED;
        }
 
        require_once $this->appdir.'./include/misc.func.php';
 
        $tag = $this->db->fetch_first("SELECT * FROM ".$this->tablepre."tags WHERE tagname='$name'");
        if($tag['closed']) {
            return API_RETURN_FAILED;
        }
 
        $tpp = 10;
        $PHP_SELF = $_SERVER['PHP_SELF'] ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_NAME'];
        $boardurl = 'http://'.$_SERVER['HTTP_HOST'].preg_replace("/\/+(api)?\/*$/i", '', substr($PHP_SELF, 0, strrpos($PHP_SELF, '/'))).'/';
        $query = $this->db->query("SELECT t.* FROM ".$this->tablepre."threadtags tt LEFT JOIN ".$this->tablepre."threads t ON t.tid=tt.tid AND t.displayorder>='0' WHERE tt.tagname='$name' ORDER BY tt.tid DESC LIMIT $tpp");
        $threadlist = array();
        while($tagthread = $this->db->fetch_array($query)) {
            if($tagthread['tid']) {
                $threadlist[] = array(
                    'subject' => $tagthread['subject'],
                    'uid' => $tagthread['authorid'],
                    'username' => $tagthread['author'],
                    'dateline' => $tagthread['dateline'],
                    'url' => $boardurl.'viewthread.php?tid='.$tagthread['tid'],
                );
            }
        }
 
        $return = array($name, $threadlist);
        return $this->_serialize($return, 1);
    }
 
    function synlogin($get, $post) {
        $uid = $get['uid'];
        $username = $get['username'];
        if(!API_SYNLOGIN) {
            return API_RETURN_FORBIDDEN;
        }
 
        //note 同步登录 API 接口
        require $this->dbconfig;
        require_once $this->appdir.'./include/db_'.$database.'.class.php';
        require_once $this->appdir.'./forumdata/cache/cache_settings.php';
 
        $db = new dbstuff;
        $this->db->connect($dbhost, $dbuser, $dbpw, $dbname, $pconnect, true, $dbcharset ? $dbcharset : $charset);
        unset($dbhost, $dbuser, $dbpw, $dbname, $pconnect);
        $cookietime = 2592000;
        $discuz_auth_key = md5($_DCACHE['settings']['authkey'].$_SERVER['HTTP_USER_AGENT']);
        header('P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"');
        $uid = intval($uid);
        $query = $this->db->query("SELECT username, uid, password, secques FROM ".$this->tablepre."members WHERE uid='$uid'");
        if($member = $this->db->fetch_array($query)) {
            _setcookie('sid', '', -86400 * 365);
            _setcookie('cookietime', $cookietime, 31536000);
            _setcookie('auth', _authcode("$member[password]\t$member[secques]\t$member[uid]", 'ENCODE', $discuz_auth_key), $cookietime);
        } else {
            _setcookie('cookietime', $cookietime, 31536000);
            _setcookie('loginuser', $username, $cookietime);
            _setcookie('activationauth', _authcode($username, 'ENCODE', $discuz_auth_key), $cookietime);
        }
    }
 
    function synlogout($get, $post) {
        if(!API_SYNLOGOUT) {
            return API_RETURN_FORBIDDEN;
        }
 
        //note 同步登出 API 接口
        header('P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"');
        _setcookie('auth', '', -86400 * 365);
        _setcookie('sid', '', -86400 * 365);
        _setcookie('loginuser', '', -86400 * 365);
        _setcookie('activationauth', '', -86400 * 365);
    }
 
    function updatepw($get, $post) {
        if(!API_UPDATEPW) {
            return API_RETURN_FORBIDDEN;
        }
        $username = $get['username'];
        $password = $get['password'];
        require $this->dbconfig;
        require_once $this->appdir.'./include/db_'.$database.'.class.php';
        $db = new dbstuff;
        $this->db->connect($dbhost, $dbuser, $dbpw, $dbname, $pconnect, true, $dbcharset ? $dbcharset : $charset);
        unset($dbhost, $dbuser, $dbpw, $dbname, $pconnect);
 
        $newpw = md5(time().rand(100000, 999999));
        $this->db->query("UPDATE ".$this->tablepre."members SET password='$newpw' WHERE username='$username'");
        return API_RETURN_SUCCEED;
    }
 
    function updatebadwords($get, $post) {
        if(!API_UPDATEBADWORDS) {
            return API_RETURN_FORBIDDEN;
        }
        $cachefile = $this->appdir.'./uc_client/data/cache/badwords.php';
        $fp = fopen($cachefile, 'w');
        $data = array();
        if(is_array($post)) {
            foreach($post as $k => $v) {
                $data['findpattern'][$k] = $v['findpattern'];
                $data['replace'][$k] = $v['replacement'];
            }
        }
        $s = "<?php\r\n";
        $s .= '$_CACHE[\'badwords\'] = '.var_export($data, TRUE).";\r\n";
        fwrite($fp, $s);
        fclose($fp);
        return API_RETURN_SUCCEED;
    }
 
    function updatehosts($get, $post) {
        if(!API_UPDATEHOSTS) {
            return API_RETURN_FORBIDDEN;
        }
        $cachefile = $this->appdir.'./uc_client/data/cache/hosts.php';
        $fp = fopen($cachefile, 'w');
        $s = "<?php\r\n";
        $s .= '$_CACHE[\'hosts\'] = '.var_export($post, TRUE).";\r\n";
        fwrite($fp, $s);
        fclose($fp);
        return API_RETURN_SUCCEED;
    }
 
    function updateapps($get, $post) {
        if(!API_UPDATEAPPS) {
            return API_RETURN_FORBIDDEN;
        }
        $UC_API = $post['UC_API'];
 
        //note 写 app 缓存文件
        $cachefile = $this->appdir.'./uc_client/data/cache/apps.php';
        $fp = fopen($cachefile, 'w');
        $s = "<?php\r\n";
        $s .= '$_CACHE[\'apps\'] = '.var_export($post, TRUE).";\r\n";
        fwrite($fp, $s);
        fclose($fp);
 
        //note 写配置文件
        if(is_writeable($this->appdir.'./config.inc.php')) {
            $configfile = trim(file_get_contents($this->appdir.'./config.inc.php'));
            $configfile = substr($configfile, -2) == '?>' ? substr($configfile, 0, -2) : $configfile;
            $configfile = preg_replace("/define\('UC_API',\s*'.*?'\);/i", "define('UC_API', '$UC_API');", $configfile);
            if($fp = @fopen($this->appdir.'./config.inc.php', 'w')) {
                @fwrite($fp, trim($configfile));
                @fclose($fp);
            }
        }
        
        global $_DCACHE;
        require_once $this->appdir.'./forumdata/cache/cache_settings.php';
        require_once $this->appdir.'./include/cache.func.php';
        foreach($post as $appid => $app) {
            $_DCACHE['settings']['ucapp'][$appid]['viewprourl'] = $app['url'].$app['viewprourl'];
        }
        updatesettings();
    
        return API_RETURN_SUCCEED;
    }
 
    function updateclient($get, $post) {
        if(!API_UPDATECLIENT) {
            return API_RETURN_FORBIDDEN;
        }
        $cachefile = $this->appdir.'./uc_client/data/cache/settings.php';
        $fp = fopen($cachefile, 'w');
        $s = "<?php\r\n";
        $s .= '$_CACHE[\'settings\'] = '.var_export($post, TRUE).";\r\n";
        fwrite($fp, $s);
        fclose($fp);
        return API_RETURN_SUCCEED;
    }
 
    function updatecredit($get, $post) {
        if(!API_UPDATECREDIT) {
            return API_RETURN_FORBIDDEN;
        }
        $credit = $get['credit'];
        $amount = $get['amount'];
        $uid = $get['uid'];
        require $this->dbconfig;
        require_once $this->appdir.'./include/db_'.$database.'.class.php';
        require_once $this->appdir.'./forumdata/cache/cache_settings.php';
 
        $db = new dbstuff;
        $this->db->connect($dbhost, $dbuser, $dbpw, $dbname, $pconnect, true, $dbcharset ? $dbcharset : $charset);
        unset($dbhost, $dbuser, $dbpw, $dbname, $pconnect);
 
        $this->db->query("UPDATE ".$this->tablepre."members SET extcredits$credit=extcredits$credit+'$amount' WHERE uid='$uid'");
 
        $discuz_user = $this->db->result_first("SELECT username FROM ".$this->tablepre."members WHERE uid='$uid'");
 
        $this->db->query("INSERT INTO ".$this->tablepre."creditslog (uid, fromto, sendcredits, receivecredits, send, receive, dateline, operation)
                VALUES ('$uid', '$discuz_user', '0', '$credit', '0', '$amount', '$timestamp', 'EXC')");
        return API_RETURN_SUCCEED;
    }
 
    function getcredit($get, $post) {
        if(!API_GETCREDIT) {
            return API_RETURN_FORBIDDEN;
        }
        require $this->dbconfig;
        require_once $this->appdir.'./include/db_'.$database.'.class.php';
 
        $db = new dbstuff;
        $this->db->connect($dbhost, $dbuser, $dbpw, $dbname, $pconnect, true, $dbcharset ? $dbcharset : $charset);
        unset($dbhost, $dbuser, $dbpw, $dbname, $pconnect);
 
        $uid = intval($get['uid']);
        $credit = intval($get['credit']);
        echo $credit >= 1 && $credit <= 8 ? $this->db->result_first("SELECT extcredits$credit FROM ".$this->tablepre."members WHERE uid='$uid'") : 0;
    }
 
    function getcreditsettings($get, $post) {
        if(!API_GETCREDITSETTINGS) {
            return API_RETURN_FORBIDDEN;
        }
        require_once $this->appdir.'./forumdata/cache/cache_settings.php';
        $credits = array();
        foreach($_DCACHE['settings']['extcredits'] as $id => $extcredits) {
            $credits[$id] = array(strip_tags($extcredits['title']), $extcredits['unit']);
        }
        return $this->_serialize($credits);
    }
 
    function updatecreditsettings($get, $post) {
        if(!API_UPDATECREDITSETTINGS) {
            return API_RETURN_FORBIDDEN;
        }
        $credit = $get['credit'];
        require $this->dbconfig;
        $outextcredits = array();
        if($credit) {
            foreach($credit as $appid => $credititems) {
                if($appid == UC_APPID) {
                    foreach($credititems as $value) {
                        $outextcredits[] = array(
                            'appiddesc' => $value['appiddesc'],
                            'creditdesc' => $value['creditdesc'],
                            'creditsrc' => $value['creditsrc'],
                            'title' => $value['title'],
                            'unit' => $value['unit'],
                            'ratiosrc' => $value['ratiosrc'],
                            'ratiodesc' => $value['ratiodesc'],
                            'ratio' => $value['ratio']
                        );
                    }
                }
            }
        }
 
        global $_DCACHE;
        require_once $this->appdir.'./include/db_'.$database.'.class.php';
        require_once $this->appdir.'./forumdata/cache/cache_settings.php';
        require_once $this->appdir.'./include/cache.func.php';
 
        $db = new dbstuff;
        $this->db->connect($dbhost, $dbuser, $dbpw, $dbname, $pconnect, true, $dbcharset ? $dbcharset : $charset);
        unset($dbhost, $dbuser, $dbpw, $dbname, $pconnect);
 
        $this->db->query("REPLACE INTO ".$this->tablepre."settings (variable, value) VALUES ('outextcredits', '".addslashes(serialize($outextcredits))."');", 'UNBUFFERED');
 
        $tmp = array();
        foreach($outextcredits as $value) {
            $key = $value['appiddesc'].'|'.$value['creditdesc'];
            if(!isset($tmp[$key])) {
                $tmp[$key] = array('title' => $value['title'], 'unit' => $value['unit']);
            }
            $tmp[$key]['ratiosrc'][$value['creditsrc']] = $value['ratiosrc'];
            $tmp[$key]['ratiodesc'][$value['creditsrc']] = $value['ratiodesc'];
            $tmp[$key]['creditsrc'][$value['creditsrc']] = $value['ratio'];
        }
        $_DCACHE['settings']['outextcredits'] = $tmp;
 
        updatesettings();
 
        return API_RETURN_SUCCEED;
 
    }
}
 
//note 使用该函数前需要 require_once $this->appdir.'./config.inc.php';
function _setcookie($var, $value, $life = 0, $prefix = 1) {
    global $cookiepre, $cookiedomain, $cookiepath, $timestamp, $_SERVER;
    setcookie(($prefix ? $cookiepre : '').$var, $value,
        $life ? $timestamp + $life : 0, $cookiepath,
        $cookiedomain, $_SERVER['SERVER_PORT'] == 443 ? 1 : 0);
}
 
function _authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) {
    $ckey_length = 4;
 
    $key = md5($key ? $key : UC_KEY);
    $keya = md5(substr($key, 0, 16));
    $keyb = md5(substr($key, 16, 16));
    $keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length): substr(md5(microtime()), -$ckey_length)) : '';
 
    $cryptkey = $keya.md5($keya.$keyc);
    $key_length = strlen($cryptkey);
 
    $string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', $expiry ? $expiry + time() : 0).substr(md5($string.$keyb), 0, 16).$string;
    $string_length = strlen($string);
 
    $result = '';
    $box = range(0, 255);
 
    $rndkey = array();
    for($i = 0; $i <= 255; $i++) {
        $rndkey[$i] = ord($cryptkey[$i % $key_length]);
    }
 
    for($j = $i = 0; $i < 256; $i++) {
        $j = ($j + $box[$i] + $rndkey[$i]) % 256;
        $tmp = $box[$i];
        $box[$i] = $box[$j];
        $box[$j] = $tmp;
    }
 
    for($a = $j = $i = 0; $i < $string_length; $i++) {
        $a = ($a + 1) % 256;
        $j = ($j + $box[$a]) % 256;
        $tmp = $box[$a];
        $box[$a] = $box[$j];
        $box[$j] = $tmp;
        $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
    }
 
    if($operation == 'DECODE') {
        if((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16)) {
            return substr($result, 26);
        } else {
                return '';
            }
    } else {
        return $keyc.str_replace('=', '', base64_encode($result));
    }
 
}
 
function _stripslashes($string) {
    if(is_array($string)) {
        foreach($string as $key => $val) {
            $string[$key] = _stripslashes($val);
        }
    } else {
        $string = stripslashes($string);
    }
    return $string;
}