source: sandbox/webservice/api/adapters/ExpressoAdapter.php @ 6165

Revision 6165, 2.6 KB checked in by niltonneto, 12 years ago (diff)

Ticket #2507 - Refatorado e centralizado tratamento de Erros.

  • Property svn:executable set to *
Line 
1<?php
2class ExpressoAdapter extends Resource {
3
4        private $expressoVersion;
5        private $request;
6        private $params;
7        private $result;
8        private $error;
9        private $id;
10       
11        function __construct($id){
12                $GLOBALS['phpgw_info'] = array(
13                                'flags' => array(
14                                                'currentapp'            => 'login',
15                                                'noheader'              => True,
16                                                'disable_Template_class' => True
17                                )
18                );
19               
20                include_once('../../header.inc.php');
21                $this->expressoVersion = substr($GLOBALS['phpgw_info']['server']['versions']['phpgwapi'],0,3);                 
22        }
23       
24        protected function setRequest($request){
25                $this->request = $request;
26        }
27       
28        public function getRequest(){
29                return $this->request;
30        }
31       
32        protected function getExpressoVersion(){
33                return $this->expressoVersion;
34        }
35       
36        protected function setResult($result){
37                $this->result = $result;
38        }
39       
40        public function getResult(){
41                return $this->result;
42        }
43       
44        protected function setId($id){
45                $this->id = $id;
46        }
47       
48        public function getId(){
49                return $this->id;
50        }
51       
52        protected function setParams($params){         
53                $this->params = $params;
54        }
55       
56        public function getParams(){   
57                return $this->params;
58        }
59       
60        public function getParam($param){
61                return $this->params->$param;
62        }
63       
64        public function setError($error) {
65                $this-> error = $error;
66        }
67       
68        protected function getError() {
69                return $this-> error;
70        }
71       
72        public function post($request){
73                $this->setRequest($request);           
74                parse_str($request->data, &$array);             
75                $data = (object)$array;         
76                if($data){
77                        if($data->params){                                                             
78                                $this->setParams(json_decode($data->params));
79                        }
80                        if($data->id)
81                                $this->setId($data->id);
82                }
83        }       
84       
85        public function get($request){
86                $response = new Response($request);
87                $response->code = Response::OK;
88                $response->addHeader('content-type', 'text/html');             
89                $response->body = "<H4>Metodo GET nao permitido para este recurso.</H4>";               
90                return $response;
91        }
92       
93        public function getResponse(){
94                $response = new Response($this->getRequest());
95                $response->code = Response::OK;
96                $response->addHeader('content-type', 'application/json');
97
98                if($this->getId())
99                        $body['id']     = $this->getId();
100                if($this->getResult())
101                        $body['result'] = $this->getResult();
102                else {
103                        Errors::runException("E_UNKNOWN_ERROR");                       
104                }
105               
106               
107                $response->body = json_encode($body);
108               
109                return $response;
110        }
111       
112        protected function isLoggedIn(){
113                if($this->getParam('auth') != null) {
114                        list($sessionid, $kp3) = explode(":", $this->getParam('auth'));
115                        if($GLOBALS['phpgw']->session->verify($sessionid, $kp3)){                                                                       
116                                return $sessionid;
117                        }
118                        else{
119                                Errors::runException("LOGIN_AUTH_INVALID");                                                     
120                        }
121                }
122                else{
123                        Errors::runException("LOGIN_NOT_LOGGED_IN");                   
124                }               
125        }       
126                       
127}
Note: See TracBrowser for help on using the repository browser.