Differences Between: [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403]
1 <?php 2 // This file is part of Moodle - http://moodle.org/ 3 // 4 // Moodle is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // Moodle is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 16 17 /** 18 * This file contains an abstract definition of an LTI service 19 * 20 * @package mod_lti 21 * @copyright 2014 Vital Source Technologies http://vitalsource.com 22 * @author Stephen Vickers 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 24 */ 25 26 27 namespace mod_lti\local\ltiservice; 28 29 defined('MOODLE_INTERNAL') || die; 30 31 /** 32 * The mod_lti\local\ltiservice\response class. 33 * 34 * @package mod_lti 35 * @since Moodle 2.8 36 * @copyright 2014 Vital Source Technologies http://vitalsource.com 37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 38 */ 39 class response { 40 41 /** @var int HTTP response code. */ 42 private $code; 43 /** @var string HTTP response reason. */ 44 private $reason; 45 /** @var string HTTP request method. */ 46 private $requestmethod; 47 /** @var string HTTP request accept header. */ 48 private $accept; 49 /** @var string HTTP response content type. */ 50 private $contenttype; 51 /** @var string HTTP request body. */ 52 private $data; 53 /** @var string HTTP response body. */ 54 private $body; 55 /** @var array HTTP response codes. */ 56 private $responsecodes; 57 /** @var array HTTP additional headers. */ 58 private $additionalheaders; 59 60 /** 61 * Class constructor. 62 */ 63 public function __construct() { 64 65 $this->code = 200; 66 $this->reason = ''; 67 $this->requestmethod = $_SERVER['REQUEST_METHOD']; 68 $this->accept = ''; 69 $this->contenttype = ''; 70 $this->data = ''; 71 $this->body = ''; 72 $this->responsecodes = array( 73 200 => 'OK', 74 201 => 'Created', 75 202 => 'Accepted', 76 300 => 'Multiple Choices', 77 400 => 'Bad Request', 78 401 => 'Unauthorized', 79 402 => 'Payment Required', 80 403 => 'Forbidden', 81 404 => 'Not Found', 82 405 => 'Method Not Allowed', 83 406 => 'Not Acceptable', 84 415 => 'Unsupported Media Type', 85 500 => 'Internal Server Error', 86 501 => 'Not Implemented' 87 ); 88 $this->additionalheaders = array(); 89 90 } 91 92 /** 93 * Get the response code. 94 * 95 * @return int 96 */ 97 public function get_code() { 98 return $this->code; 99 } 100 101 /** 102 * Set the response code. 103 * 104 * @param int $code Response code 105 */ 106 public function set_code($code) { 107 $this->code = $code; 108 $this->reason = ''; 109 } 110 111 /** 112 * Get the response reason. 113 * 114 * @return string 115 */ 116 public function get_reason() { 117 $code = $this->code; 118 if (($code < 200) || ($code >= 600)) { 119 $code = 500; // Status code must be between 200 and 599. 120 } 121 if (empty($this->reason) && array_key_exists($code, $this->responsecodes)) { 122 $this->reason = $this->responsecodes[$code]; 123 } 124 // Use generic reason for this category (based on first digit) if a specific reason is not defined. 125 if (empty($this->reason)) { 126 $this->reason = $this->responsecodes[intval($code / 100) * 100]; 127 } 128 return $this->reason; 129 } 130 131 /** 132 * Set the response reason. 133 * 134 * @param string $reason Reason 135 */ 136 public function set_reason($reason) { 137 $this->reason = $reason; 138 } 139 140 /** 141 * Get the request method. 142 * 143 * @return string 144 */ 145 public function get_request_method() { 146 return $this->requestmethod; 147 } 148 149 /** 150 * Get the request accept header. 151 * 152 * @return string 153 */ 154 public function get_accept() { 155 return $this->accept; 156 } 157 158 /** 159 * Set the request accept header. 160 * 161 * @param string $accept Accept header value 162 */ 163 public function set_accept($accept) { 164 $this->accept = $accept; 165 } 166 167 /** 168 * Get the response content type. 169 * 170 * @return string 171 */ 172 public function get_content_type() { 173 return $this->contenttype; 174 } 175 176 /** 177 * Set the response content type. 178 * 179 * @param string $contenttype Content type 180 */ 181 public function set_content_type($contenttype) { 182 $this->contenttype = $contenttype; 183 } 184 185 /** 186 * Get the request body. 187 * 188 * @return string 189 */ 190 public function get_request_data() { 191 return $this->data; 192 } 193 194 /** 195 * Set the response body. 196 * 197 * @param string $data Body data 198 */ 199 public function set_request_data($data) { 200 $this->data = $data; 201 } 202 203 /** 204 * Set the response body. 205 * 206 * @param string $body Body data 207 */ 208 public function set_body($body) { 209 $this->body = $body; 210 } 211 212 /** 213 * Add an additional header. 214 * 215 * @param string $header The new header 216 */ 217 public function add_additional_header($header) { 218 array_push($this->additionalheaders, $header); 219 } 220 221 /** 222 * Send the response. 223 */ 224 public function send() { 225 header("HTTP/1.0 {$this->code} {$this->get_reason()}"); 226 foreach ($this->additionalheaders as $header) { 227 header($header); 228 } 229 if ((($this->code >= 200) && ($this->code < 300)) || !empty($this->body)) { 230 if (!empty($this->contenttype)) { 231 header("Content-Type: {$this->contenttype}; charset=utf-8"); 232 } 233 if (!empty($this->body)) { 234 echo $this->body; 235 } 236 } else if ($this->code >= 400) { 237 header("Content-Type: application/json; charset=utf-8"); 238 $body = new \stdClass(); 239 $body->status = $this->code; 240 $body->reason = $this->get_reason(); 241 $body->request = new \stdClass(); 242 $body->request->method = $_SERVER['REQUEST_METHOD']; 243 $body->request->url = $_SERVER['REQUEST_URI']; 244 if (isset($_SERVER['HTTP_ACCEPT'])) { 245 $body->request->accept = $_SERVER['HTTP_ACCEPT']; 246 } 247 if (isset($_SERVER['CONTENT_TYPE'])) { 248 $body->request->contentType = explode(';', $_SERVER['CONTENT_TYPE'], 2)[0]; 249 } 250 echo json_encode($body, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); 251 } 252 } 253 254 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body