1: <?php
2: 3: 4: 5: 6: 7: 8: 9:
10:
11:
12: class WxRobot_Extends{
13:
14: 15: 16:
17: public static $_instance = null;
18:
19: 20: 21: 22: 23:
24: public function __construct(){
25:
26: $this->options = get_option(WEIXIN_ROBOT_OPTIONS);
27: $this->obj = WxRobot_SDK::instance();
28: }
29:
30: 31: 32: 33: 34:
35: public static function instance(){
36: if( is_null (self::$_instance)){
37: self::$_instance = new self();
38: }
39: return self::$_instance;
40: }
41:
42: 43: 44: 45: 46: 47: 48:
49: public function dealwith($func, $args){
50: $res = '';
51: switch($func){
52: case 'all' : $res = $this->p_all($args);break;
53: case 'subscribe': $res = $this->p_subscribe('');break;
54: case 'scan' : $res = $this->p_scan($args);break;
55: case 'text' : $res = $this->p_text($args);break;
56: case 'image' : $res = $this->p_image($args);break;
57: case 'voice' : $res = $this->p_voice($args);break;
58: case 'video' : $res = $this->p_video($args);break;
59: case 'location' : $res = $this->p_location($args);break;
60: case 'link' : $res = $this->p_link($args);break;
61: case 'menu' : $res = $this->p_menu($args);break;
62: default : $res = $this->p_text('');break;
63: }
64: if(empty($res)){
65: return false;
66: }
67: return $res;
68: }
69:
70:
71: 72: 73: 74: 75: 76:
77: private function p_all($args){
78: if(empty($args)){return false;}
79: if($data = $this->extends_start('all', $args)){
80: return $data;
81: }
82: return false;
83: }
84:
85: 86: 87: 88: 89:
90: private function p_subscribe($args){
91: if($data = $this->extends_start('subscribe', $args)){
92: return $data;
93: }
94: return false;
95: }
96:
97: 98: 99: 100: 101:
102: private function p_scan($args){
103: if($data = $this->extends_start('scan', $args)){
104: return $data;
105: }
106: return false;
107: }
108:
109: 110: 111: 112: 113: 114:
115: private function p_text($kw){
116: if(empty($kw)){return false;}
117: if($data = $this->extends_start('text', $kw)){
118: return $data;
119: }
120: return false;
121: }
122:
123: 124: 125: 126: 127: 128:
129: private function p_image($info){
130: if(empty($info)){return false;}
131: if($data = $this->extends_start('image', $info)){
132: return $data;
133: }
134: return false;
135: }
136:
137: 138: 139: 140: 141: 142:
143: private function p_voice($info){
144: if(empty($info)){return false;}
145: if($data = $this->extends_start('voice', $info)){
146: return $data;
147: }
148: return false;
149: }
150:
151: 152: 153: 154: 155: 156:
157: private function p_video($info){
158: if(empty($info)){return false;}
159: if($data = $this->extends_start('video', $info)){
160: return $data;
161: }
162: return false;
163: }
164:
165: 166: 167: 168: 169: 170:
171: private function p_location($info){
172: if(empty($info)){return false;}
173: if($data = $this->extends_start('location', $info)){
174: return $data;
175: }
176: return false;
177: }
178:
179: 180: 181: 182: 183: 184:
185: private function p_link(){
186: if(empty($info)){return false;}
187: if($data = $this->extends_start('link', $info)){
188: return $data;
189: }
190: return false;
191: }
192:
193: 194: 195: 196: 197: 198:
199: private function p_menu($menu_name){
200: if(empty($menu_name)){return false;}
201: if($data = $this->extends_start('menu', $menu_name)){
202: return $data;
203: }
204: return false;
205: }
206:
207: 208: 209: 210: 211: 212: 213:
214: private function extends_start($name, $args){
215: $flist = WxRobot_Table_Extends::instance()->select_extends_type($name);
216: if(!$flist) return false;
217: foreach($flist as $k=>$v){
218: if($name == $v['ext_type']){
219: $abspath = WEIXIN_PLUGINS.$v['ext_name'];
220: if(!file_exists($abspath)){
221: WxRobot_Table_Extends::instance()->delete_extends_name($v['ext_name']);
222: }else{
223: include_once($abspath);
224: $tt = explode('.', $v['ext_name']);
225: $cn = $tt[0];
226: $obj = new $cn($this);
227: if(method_exists($obj, 'start')){
228: $data = $obj->start($args);
229: if( $data ) return $data;
230: }
231: }
232: }
233: }
234: return false;
235: }
236:
237:
238: 239: 240: 241: 242: 243:
244: private function _c($f){
245: if(!file_exists($f)){
246: $fn = basename($f);
247: WxRobot_Table_Extends::instance()->delete_extends_name($fn);
248: return false;
249: }else{
250: include_once($f);
251: return true;
252: }
253: }
254:
255: 256: 257: 258: 259: 260:
261: public function admin($fn){
262: $abspath = WEIXIN_PLUGINS.$fn.'.php';
263: if($this->_c($abspath)){
264: $obj = new $fn($this);
265: if(method_exists($obj, 'admin')){
266: $obj->admin();
267: }
268: }
269: }
270:
271: 272: 273: 274: 275: 276:
277: public function frontend($fn){
278: $abspath = WEIXIN_PLUGINS.$fn.'.php';
279: if($this->_c($abspath)){
280: $obj = new $fn($this);
281: if(method_exists($obj, 'frontend')){
282: $obj->frontend();
283: }
284:
285: }
286: }
287:
288:
289: 290: 291: 292: 293: 294: 295: 296: 297:
298: public function admin_menu($object, $method, $titleName, $linkName){
299: add_submenu_page('weixin_robot',
300: 'weixin_robot',
301: $titleName,
302: 'manage_options',
303: $linkName,
304: array($object, $method));
305: }
306:
307:
308: 309: 310: 311: 312: 313: 314:
315: public function __call($method, $args){
316: if(!empty($args)){
317: return call_user_func_array(array($this->obj, $method), $args);
318: }else{
319: return call_user_func(array($this->obj, $method));
320: }
321: }
322:
323: 324: 325: 326: 327:
328: public function getAcceptInfo(){
329: return $this->info;
330: }
331:
332: 333: 334: 335: 336:
337: public function getConfigInfo(){
338: return $this->options;
339: }
340:
341: 342: 343: 344: 345:
346: public function getUserOpenID(){
347: return $this->info['FromUserName'];
348: }
349:
350: 351: 352: 353: 354:
355: public function getAppID(){
356: return $this->options['ai'];
357: }
358:
359: 360: 361: 362: 363:
364: public function getAppSelect(){
365: return $this->options['as'];
366: }
367: }
368: ?>
369: