ILIAS  Release_5_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;
39 
40  const HEADER_ECS_SENDER = 'X-EcsSender';
41 
42  protected $log;
43 
44  protected $result_string = '';
45  protected $result_header = '';
46  protected $http_code = '';
47  protected $result;
48  protected $result_type;
49  protected $header_parsing = false;
50 
51  protected $headers = array();
52  protected $header_map = array();
53 
63  public function __construct($a_res,$with_headers = false,$a_type = self::RESULT_TYPE_JSON)
64  {
65  global $ilLog;
66 
67  $this->log = $ilLog;
68 
69  $this->result_string = $a_res;
70  $this->result_type = $a_type;
71 
72  if($with_headers)
73  {
74  $this->header_parsing = true;
75  }
76 
77  $this->init();
78  }
79 
87  public function setHTTPCode($a_code)
88  {
89  $this->http_code = $a_code;
90  }
91 
97  public function getHTTPCode()
98  {
99  return $this->http_code;
100  }
101 
108  public function getPlainResultString()
109  {
110  return $this->result_string;
111  }
112 
120  public function getResult()
121  {
122  return $this->result;
123  }
124 
129  public function setHeaders($a_headers)
130  {
131  $this->headers = $a_headers;
132  }
133 
139  public function getHeaders()
140  {
141  return $this->headers ? $this->headers : array();
142  }
143 
149  private function init()
150  {
151  if($this->header_parsing and $this->result_string)
152  {
153  $this->splitHeader();
154  $this->parseHeader();
155  }
156 
157 
158  switch($this->result_type)
159  {
160  case self::RESULT_TYPE_JSON:
161  if($this->result_string)
162  {
163  $this->result = json_decode($this->result_string);
164  }
165  else
166  {
167  $this->result = array();
168  }
169  break;
170 
171  case self::RESULT_TYPE_URL_LIST:
172  $this->result = $this->parseUriList($this->result_string);
173  break;
174  }
175  return true;
176  }
177 
185  private function splitHeader()
186  {
187  $pos = strpos($this->result_string,"\r\n\r\n");
188  if($pos !== false)
189  {
190  $this->result_header = substr($this->result_string,0,$pos + 2);
191  $this->result_string = substr($this->result_string,$pos + 2,-1);
192  return true;
193  }
194  else
195  {
196  $this->log->write(__METHOD__.': Cannot find header entry');
197  throw new ilECSConnectorException('Cannot find header part.');
198  }
199  }
200 
207  private function parseHeader()
208  {
209  // In the moment only look for "Location:" value
210  $location_start = strpos($this->result_header,"Location:");
211  if($location_start !== false)
212  {
213  $location_start += 10;
214  $location_end = strpos($this->result_header,"\r\n",$location_start);
215 
216  $location = substr($this->result_header,$location_start,$location_end - $location_start);
217  $this->headers['Location'] = $location;
218  }
219 
220  $ecs_sender = strpos($this->result_header,self::HEADER_ECS_SENDER);
221  if($ecs_sender !== false)
222  {
223  $sender_start =+ 13;
224  $sender_end = strpos($this->result_header,"\r\n",$sender_start);
225  $sender = substr($this->result_header,$sender_start,$sender_end - $sender_start);
226 
227  $senders_arr = explode(',',$sender);
228  $this->header_map[self::HEADER_ECS_SENDER] = $senders_arr;
229  }
230  return true;
231  }
232 
238  private function parseUriList($a_content)
239  {
240  include_once 'Services/WebServices/ECS/classes/class.ilECSUriList.php';
241  $list = new ilECSUriList();
242  $lines = explode("\n", $this->getPlainResultString());
243  foreach($lines as $line)
244  {
245  $line = trim($line);
246  if(!strlen($line))
247  {
248  continue;
249  }
250  $uri_parts = explode("/", $line);
251  $list->add($line, array_pop($uri_parts));
252  }
253 
254  return $list;
255  }
256 }
257 
258 ?>