1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
<?php
namespace oroboros\core\abstracts\controllers;
abstract class AbstractAjaxController
extends AbstractHttpController
{
const CLASS_SCOPE = \oroboros\core\interfaces\api\ClassScopeApi::CLASS_SCOPE_AJAX_CONTROLLER;
const DEFAULT_RESPONSE_TYPE = 'text';
private $_request_types = array(
'get' => self::FLAG_TYPE_GET,
'post' => self::FLAG_TYPE_POST,
'put' => self::FLAG_TYPE_PUT,
'delete' => self::FLAG_TYPE_DELETE,
'options' => self::FLAG_TYPE_OPTIONS,
'head' => self::FLAG_TYPE_HEAD,
);
private $_request_type;
private $_request;
private $_response_types = array(
'text',
'json',
'html',
'scripts',
'flash',
'css'
);
private $_response_data = array();
private $_response_headers = array();
private $_response_type = self::DEFAULT_RESPONSE_TYPE;
private $_encoding = array();
private $_config;
private $_preferences;
private $_errors;
public function initialize(array $params = array(), array $flags = array()) {
parent::initialize($params, $flags);
$this->_initializeAjaxEnvironment();
}
public function error(array $params = array(), array $flags = array()) {
$trace = debug_backtrace();
foreach($trace as $key => $value) {
echo $value['function'] . PHP_EOL;
}
$code = ((isset($params['code'])) ? $params['code'] : self::DEFAULT_ERROR_CODE);
http_response_code($code);
exit();
}
protected function _queue($type, $key, $value = NULL) {
switch ($type) {
case 'header':
return $this->_queueHeader($key, $value);
break;
case 'data':
return $this->_queueData($key, $value);
break;
case 'default':
throw new \oroboros\core\utilities\exception\Exception("Invalid queue type: [" . $type . "]", self::ERROR_LOGIC_BAD_PARAMETERS);
break;
}
}
protected function _request() {
return ((isset($this->_request['data'])) ? json_decode($this->_request['data'], JSON_OBJECT_AS_ARRAY): FALSE);
}
protected function _requestType() {
return $this->_request_type;
}
protected function _encoding() {
return $this->_encoding;
}
protected function _responseType() {
return $this->_response_type;
}
protected function _packageRenderData(array $data = array()) {
$result = array(
'headers' => $this->_headers(),
'data' => $this->_data(),
'type' => $this->_responseType(),
'schema' => $this->_config(),
'errors' => $this->_errors(),
'conditions' => $this->_conditions(),
'router' => $this->_router()
);
if ($this->_checkParam('render')) {
foreach ($this->_getParam('render') as $key => $value) {
$result[$key] = $value;
}
}
foreach ($data as $key => $value) {
$result[$key] = $value;
}
return $result;
}
protected function _setRouter($router) {
$this->_router = $router;
}
protected function _router() {
return $this->_router;
}
protected function _setConditions($conditions) {
$this->_conditions = $conditions;
}
protected function _conditions() {
return $this->_conditions;
}
protected function _setErrors($errors) {
$this->_errors = $errors;
}
protected function _errors() {
return $this->_errors;
}
protected function _setConfig($config) {
$this->_config = $config;
}
protected function _config($key = NULL) {
if (isset($key)) {
return ((isset($this->_config[$key]) ? $this->_config[$key] : FALSE));
} else {
return $this->_config;
}
}
protected function _setPreferences($preferences) {
$this->_preferences = $preferences;
}
protected function _preferences($key = NULL) {
if (isset($key)) {
return ((isset($this->_preferences[$key]) ? $this->_preferences[$key] : FALSE));
} else {
return $this->_preferences;
}
}
protected function _setHeader($key, $value) {
$this->_response_headers[$key] = $value;
}
protected function _headers() {
return $this->_response_headers;
}
protected function _setData($key, $value) {
$this->_response_data[$key] = $value;
}
protected function _data() {
return $this->_response_data;
}
private function _initializeAjaxEnvironment() {
$headers = getallheaders();
$this->_initializeContentType($headers['Accept']);
$this->_initializeCompression($headers['Accept-Encoding']);
$this->_initializeRequestType();
}
private function _initializeContentType($content_type) {
$type = FALSE;
switch (explode(';', $content_type)[0]) {
case 'text/html':
case 'application/xhtml+xml':
$type = 'html';
break;
case 'application/xml':
$type = 'xml';
break;
case 'application/javascript':
$type = 'scripts';
break;
case 'application/json':
$type = 'json';
break;
case 'text/css':
$type = 'css';
break;
case 'application/x-shockwave-flash':
$type = 'flash';
break;
case 'text/plain':
case '*/*':
default:
$type = self::DEFAULT_RESPONSE_TYPE;
break;
}
$this->_response_type = $type;
}
private function _initializeCompression($encoding) {
$encoding = explode(', ', $encoding);
$this->_encoding = \oroboros\Common::map($encoding, function($item) {
return trim($item);
});
}
private function _initializeRequestType() {
$method = strtolower($_SERVER['REQUEST_METHOD']);
$this->_request_type = $method;
switch ($method) {
case 'get':
$this->_request = $_GET;
break;
case 'post':
$this->_request = $_POST;
break;
case 'put':
case 'delete':
$putfp = fopen('php://input', 'r');
$this->_request = '';
while ($data = fread($putfp, 1024))
$this->_request .= $data;
fclose($putfp);
break;
}
}
}