ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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 $DIC;
66 
67  $ilLog = $DIC['ilLog'];
68 
69  $this->log = $ilLog;
70 
71  $this->result_string = $a_res;
72  $this->result_type = $a_type;
73 
74  if ($with_headers) {
75  $this->header_parsing = true;
76  }
77 
78  $this->init();
79  }
80 
88  public function setHTTPCode($a_code)
89  {
90  $this->http_code = $a_code;
91  }
92 
98  public function getHTTPCode()
99  {
100  return $this->http_code;
101  }
102 
109  public function getPlainResultString()
110  {
111  return $this->result_string;
112  }
113 
121  public function getResult()
122  {
123  return $this->result;
124  }
125 
130  public function setHeaders($a_headers)
131  {
132  $this->headers = $a_headers;
133  }
134 
140  public function getHeaders()
141  {
142  return $this->headers ? $this->headers : array();
143  }
144 
150  private function init()
151  {
152  if ($this->header_parsing and $this->result_string) {
153  $this->splitHeader();
154  $this->parseHeader();
155  }
156 
157 
158  switch ($this->result_type) {
159  case self::RESULT_TYPE_JSON:
160  if ($this->result_string) {
161  $this->result = json_decode($this->result_string);
162  } else {
163  $this->result = array();
164  }
165  break;
166 
167  case self::RESULT_TYPE_URL_LIST:
168  $this->result = $this->parseUriList($this->result_string);
169  break;
170  }
171  return true;
172  }
173 
181  private function splitHeader()
182  {
183  $pos = strpos($this->result_string, "\r\n\r\n");
184  if ($pos !== false) {
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  } else {
189  $this->log->write(__METHOD__ . ': Cannot find header entry');
190  throw new ilECSConnectorException('Cannot find header part.');
191  }
192  }
193 
200  private function parseHeader()
201  {
202  // In the moment only look for "Location:" value
203  $location_start = strpos($this->result_header, "Location:");
204  if ($location_start !== false) {
205  $location_start += 10;
206  $location_end = strpos($this->result_header, "\r\n", $location_start);
207 
208  $location = substr($this->result_header, $location_start, $location_end - $location_start);
209  $this->headers['Location'] = $location;
210  }
211 
212  $ecs_sender = strpos($this->result_header, self::HEADER_ECS_SENDER);
213  if ($ecs_sender !== false) {
214  $sender_start = +13;
215  $sender_end = strpos($this->result_header, "\r\n", $sender_start);
216  $sender = substr($this->result_header, $sender_start, $sender_end - $sender_start);
217 
218  $senders_arr = explode(',', $sender);
219  $this->header_map[self::HEADER_ECS_SENDER] = $senders_arr;
220  }
221  return true;
222  }
223 
229  private function parseUriList($a_content)
230  {
231  include_once 'Services/WebServices/ECS/classes/class.ilECSUriList.php';
232  $list = new ilECSUriList();
233  $lines = explode("\n", $this->getPlainResultString());
234  foreach ($lines as $line) {
235  $line = trim($line);
236  if (!strlen($line)) {
237  continue;
238  }
239  $uri_parts = explode("/", $line);
240  $list->add($line, array_pop($uri_parts));
241  }
242 
243  return $list;
244  }
245 }
const RESULT_TYPE_URL_LIST
if(isset($_REQUEST['delete'])) $list
Definition: registry.php:41
parseHeader()
Parse header.
global $DIC
Definition: saml.php:7
$location
Definition: buildRTE.php:44
Presentation of ecs uril (http://...campusconnect/courselinks)
getResult()
get result
setHeaders($a_headers)
Set header.
getPlainResultString()
get unformated result string
$a_type
Definition: workflow.php:92
$a_content
Definition: workflow.php:93
__construct($a_res, $with_headers=false, $a_type=self::RESULT_TYPE_JSON)
Constructor.
setHTTPCode($a_code)
set HTTP return code
parseUriList($a_content)
getHTTPCode()
get HTTP code
splitHeader()
Split header and content.
init()
init result (json_decode) private
getHeaders()
get headers