source: sandbox/webservice/api/webservice.php @ 5681

Revision 5681, 2.1 KB checked in by niltonneto, 12 years ago (diff)

Ticket #2507 - Implementada primeira parte do método Catalog.getUserContacts().

Line 
1<?php
2include_once("http_request.class.php");
3
4$public_functions = array(
5        "Expresso.login",
6        "Expresso.logout",
7        "Expresso.getExpressoVersion",
8        "Mail.getUserFolders",
9        "Mail.getUserMessages",
10        "Catalog.getUserContacts",
11);
12
13$GLOBALS['phpgw_info'] = array(
14        'flags' => array(
15                        'currentapp'            => 'login',
16                        'noheader'              => True,
17                        'disable_Template_class' => True,
18                        'public_functions'       => $public_functions
19        )
20);
21
22include_once('../header.inc.php');
23
24// NO COOKIES!!!!
25unset($_COOKIE);
26switch ($_SERVER['REQUEST_METHOD']) {
27        case 'GET':
28        case 'POST':           
29                execute($_REQUEST);
30        break;
31       
32        default:
33        exit();
34}
35
36function execute($request){     
37        if(empty($request)) {
38                $request = array();
39                $http_request = new http_request();
40                $content_type = $http_request->header('Content-Type');
41                $accept = $http_request->header('Accept');
42                $content = $http_request->body();       
43       
44                if (($content_type == 'application/x-www-form-urlencoded')
45                        && ($accept == 'application/json') && $content != null) {
46               
47                        $req_obj = json_decode($content);
48                        $request['params']      = (array)$req_obj->params;
49                        $request['method']      = $req_obj->method;
50                        $request ['format']     = "json-rpc";
51                        $request['id']          = $req_obj->id;
52                }
53        }
54               
55        list($params, $method, $format, $id) = array_values($request);
56
57        if(verifyMethod($method)){
58                $method = explode(".", $method);       
59                include_once("./".$format."/".$method[0].".php");
60                $obj = new $method[0]($id);     
61                $response = $obj->$method[1]($params); 
62        }
63        else{
64                $response = array(
65                                'result'        => null,
66                                'error'         => "Available Resources: ". implode(", ", $GLOBALS['phpgw_info']['flags']['public_functions']),
67                                'id'            => $id
68                );             
69        }
70        dispatch($response, $format);
71}
72
73function verifyMethod($method) {
74        if(array_search( $method, $GLOBALS['phpgw_info']['flags']['public_functions']) !== FALSE){
75                return true;
76        }
77        else {
78                return false;
79        }
80}
81
82function dispatch($response, $format){
83        $e_response = false;
84        switch($format){               
85                case 'json-rpc':
86                        $e_response = @json_encode($response);
87                        break;
88                case 'xml-rpc':
89                        $e_response = @xmlrpc_encode($response);
90                        break;
91                default:
92                        $e_response = false;   
93                        break;
94        }       
95        echo $e_response;       
96}
Note: See TracBrowser for help on using the repository browser.