ILIAS  Release_4_2_x_branch Revision 61807
 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  protected $log;
41 
42  protected $result_string = '';
43  protected $result_header = '';
44  protected $http_code = '';
45  protected $result;
46  protected $result_type;
47  protected $header_parsing = false;
48 
49  protected $headers = array();
50 
60  public function __construct($a_res,$with_headers = false,$a_type = self::RESULT_TYPE_JSON)
61  {
62  global $ilLog;
63 
64  $this->log = $ilLog;
65 
66  $this->result_string = $a_res;
67  $this->result_type = $a_type;
68 
69  if($with_headers)
70  {
71  $this->header_parsing = true;
72  }
73 
74  $this->init();
75  }
76 
84  public function setHTTPCode($a_code)
85  {
86  $this->http_code = $a_code;
87  }
88 
94  public function getHTTPCode()
95  {
96  return $this->http_code;
97  }
98 
105  public function getPlainResultString()
106  {
107  return $this->result_string;
108  }
109 
117  public function getResult()
118  {
119  return $this->result;
120  }
121 
126  public function setHeaders($a_headers)
127  {
128  $this->headers = $a_headers;
129  }
130 
136  public function getHeaders()
137  {
138  return $this->headers ? $this->headers : array();
139  }
140 
146  private function init()
147  {
148  if(!$this->result_string)
149  {
150  $this->result = array();
151  return true;
152  }
153 
154  if($this->header_parsing)
155  {
156  $this->splitHeader();
157  $this->parseHeader();
158  }
159 
160  switch($this->result_type)
161  {
162  case self::RESULT_TYPE_JSON:
163  $this->result = json_decode($this->result_string);
164  break;
165 
166  case self::RESULT_TYPE_URL_LIST:
167  $this->result = $this->parseUriList($this->result_string);
168  break;
169  }
170  return true;
171  }
172 
180  private function splitHeader()
181  {
182  $pos = strpos($this->result_string,"\r\n\r\n");
183  if($pos !== false)
184  {
185  $this->result_header = substr($this->result_string,0,$pos + 2);
186  $this->result_string = substr($this->result_string,$pos + 2,-1);
187  return true;
188  }
189  else
190  {
191  $this->log->write(__METHOD__.': Cannot find header entry');
192  throw new ilECSConnectorException('Cannot find header part.');
193  }
194  }
195 
202  private function parseHeader()
203  {
204  // In the moment only look for "Location:" value
205  $location_start = strpos($this->result_header,"Location:");
206  if($location_start !== false)
207  {
208  $location_start += 10;
209  $location_end = strpos($this->result_header,"\r\n",$location_start);
210 
211  $location = substr($this->result_header,$location_start,$location_end - $location_start);
212  $this->headers['Location'] = $location;
213  }
214  return true;
215  }
216 
222  private function parseUriList($a_content)
223  {
224  include_once 'Services/WebServices/ECS/classes/class.ilECSUriList.php';
225  $list = new ilECSUriList();
226  $lines = explode("\n", $this->getPlainResultString());
227  foreach($lines as $line)
228  {
229  $line = trim($line);
230  if(!strlen($line))
231  {
232  continue;
233  }
234  $uri_parts = explode("/", $line);
235  $list->add($line, array_pop($uri_parts));
236  }
237 
238  return $list;
239  }
240 }
241 
242 ?>