chengkun
2025-04-18 1bb985f32f2efe0f9dd69f3cf29a1c809b1cf96d
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
<?php
/*
* $Author :PHPYUN开发团队
*
* 官网: http://www.phpyun.com
*
* 版权所有 2009-2021 宿迁鑫潮信息技术有限公司,并保留所有权利。
*
* 软件声明:未经授权前提下,不得用于商业运营、二次开发以及任何形式的再次发布。
 */
!defined('P_W') && exit('Forbidden');
 
define('API_CLOSED', 1);
define('API_SIGN_ERROR', 2);
define('API_MODE_NOT_EXISTS', 3);
define('API_METHOD_NOT_EXISTS', 4);
 
class ApiResponse {
 
    var $result;
    var $mode;
 
    function ApiResponse($res, $mode = null) {
        $this->result = $res;
        $this->mode = $mode;
    }
 
    function getResult() {
        return $this->result;
    }
 
    function getMode() {
        return $this->mode;
    }
}
 
class ErrorMsg {
 
    var $errCode = 0;
    var $errMessage = '';
 
    function ErrorMsg($errCode, $errMessage) {
        $this->errCode = $errCode;
        $this->errMessage = $errMessage;
    }
 
    function getErrCode() {
        return $this->errCode;
    }
 
    function getErrMessage() {
        return $this->errMessage;
    }
 
    function getResult() {
        return null;
    }
}
 
class api_client {
 
    var $type;
    var $apikey;
    var $charset;
    var $db;
    var $classdb;
    var $siteappkey;
 
    function api_client() {
        global $mysqli;
        $this->apikey    = '';
        $this->type        = '';
        $this->siteappkey ='';
        $this->db        =& $mysqli;
        $this->classdb    = array();
        $this->charset    = UC_CHARSET;
    }
 
    function run($request) {
        global $mysqli,$config;
        $request = $this->strips($request);
        if (isset($request['type']) && $request['type'] == 'uc') {
            $this->type        = 'uc';
            $this->apikey    = UC_KEY;
        } else {
            $this->type        = 'app';
            $this->apikey    = UC_APPID;
            $this->siteappkey = UC_KEY;
        }
        /***
        if ($this->type == 'app' && !$GLOBALS['o_appifopen']) {
            return new ErrorMsg(API_CLOSED, 'App Closed');
        }
        ***/
        ksort($request);
        reset($request);
        $arg = '';
        foreach ($request as $key => $value) {
            if ($value && $key!='sig') {
                $arg.="$key=$value&";
            }
        }
        if(empty($this->apikey) || md5($arg.$this->apikey)!=$request['sig']) {
            return new ErrorMsg(API_SIGN_ERROR, 'Error Sign');
        }
        $mode    = $request['mode'];
        $method    = $request['method'];
        //echo $request['params'];
        $params = isset($request['params'])?unserialize($request['params']):array();
        if (isset($params['appthreads'])) {
            require_once(R_P.'class_json.php');
            $json = new Services_JSON(true);
            $params['appthreads'] = $json->decode(@gzuncompress($params['appthreads']));
        }
 
        if ($params && isset($request['charset'])) {
            $params = pwConvert($params,$this->charset,$request['charset']);
        }
        //print_r($this->callback($mode, $method, $params));
        return $this->callback($mode, $method, $params);
    }
 
    function callback($mode, $method, $params) {
 
        if (!isset($this->classdb[$mode])) {
            if (!file_exists(R_P.'class_' . $mode . '.php')) {
                return new ErrorMsg(API_MODE_NOT_EXISTS, "Class($mode) Not Exists");
            }
            require_once Pcv(R_P.'class_' . $mode . '.php');
            $this->classdb[$mode] = new $mode($this);
        }
 
        if (!method_exists($this->classdb[$mode], $method)) {
            return new ErrorMsg(API_METHOD_NOT_EXISTS, "Method($method of $mode) Not Exists");
        }
        !is_array($params) && $params = array();
        //print_r($params);
        return @call_user_func_array(array(&$this->classdb[$mode],$method), $params);
    }
 
    function dataFormat($data) {
        $res = array(
            'charset' => $this->charset
        );
        if (strtolower(get_class($data)) == 'apiresponse') {
            $res['result'] = $data->getResult();
        } else {
            $res['errCode'] = $data->getErrCode();
            $res['errMessage'] = $data->getErrMessage();
        }
        return serialize($res);
    }
    function strips($param) {
        if (is_array($param)) {
            foreach ($param as $key => $value) {
                $param[$key] = $this->strips($value);
            }
        } else {
            $param = stripslashes($param);
        }
        return $param;
    }
    function strcode($string, $encode = true) {
        !$encode && $string = base64_decode($string);
        $code = '';
        $key  = substr(md5($_SERVER['HTTP_USER_AGENT'] . $this->apikey),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;
    }
 
 
 
}
?>