ILIAS  Release_4_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilECSResult.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2006 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
23 
24 include_once('./Services/WebServices/ECS/classes/class.ilECSConnectorException.php');
25 
36 {
37  const RESULT_TYPE_JSON = 1;
38 
39  protected $log;
40 
41  protected $result_string = '';
42  protected $result_header = '';
43  protected $http_code = '';
44  protected $result;
45  protected $result_type;
46  protected $header_parsing = false;
47 
48  protected $headers = array();
49 
59  public function __construct($a_res,$with_headers = false,$a_type = self::RESULT_TYPE_JSON)
60  {
61  global $ilLog;
62 
63  $this->log = $ilLog;
64 
65  $this->result_string = $a_res;
66  $this->result_type = $a_type;
67 
68  if($with_headers)
69  {
70  $this->header_parsing = true;
71  }
72 
73  $this->init();
74  }
75 
83  public function setHTTPCode($a_code)
84  {
85  $this->http_code = $a_code;
86  }
87 
93  public function getHTTPCode()
94  {
95  return $this->http_code;
96  }
97 
104  public function getPlainResultString()
105  {
106  return $this->result_string;
107  }
108 
116  public function getResult()
117  {
118  return $this->result;
119  }
120 
126  public function getHeaders()
127  {
128  return $this->headers ? $this->headers : array();
129  }
130 
136  private function init()
137  {
138  if(!$this->result_string)
139  {
140  $this->result = array();
141  return true;
142  }
143 
144  if($this->header_parsing)
145  {
146  $this->splitHeader();
147  $this->parseHeader();
148  }
149 
150  switch($this->result_type)
151  {
152  case self::RESULT_TYPE_JSON:
153  $this->result = json_decode($this->result_string);
154  break;
155  }
156  return true;
157  }
158 
166  private function splitHeader()
167  {
168  $pos = strpos($this->result_string,"\r\n\r\n");
169  if($pos !== false)
170  {
171  $this->result_header = substr($this->result_string,0,$pos + 2);
172  $this->result_string = substr($this->result_string,$pos + 2,-1);
173  return true;
174  }
175  else
176  {
177  $this->log->write(__METHOD__.': Cannot find header entry');
178  throw new ilECSConnectorException('Cannot find header part.');
179  }
180  }
181 
188  private function parseHeader()
189  {
190  // In the moment only look for "Location:" value
191  $location_start = strpos($this->result_header,"Location:");
192  if($location_start !== false)
193  {
194  $location_start += 10;
195  $location_end = strpos($this->result_header,"\r\n",$location_start);
196 
197  $location = substr($this->result_header,$location_start,$location_end - $location_start);
198  $this->headers['Location'] = $location;
199  }
200  return true;
201  }
202 }
203 
204 ?>