-
Notifications
You must be signed in to change notification settings - Fork 101
Expand file tree
/
Copy pathhttp.php
More file actions
187 lines (160 loc) · 4.36 KB
/
http.php
File metadata and controls
187 lines (160 loc) · 4.36 KB
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
<?php
/**
* @package Joomla.Site
* @subpackage Com_api
*
* @copyright Copyright (C) 2009-2014 Techjoomla, Tekdi Technologies Pvt. Ltd. All rights reserved.
* @license GNU GPLv2 <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>
* @link http://techjoomla.com
* Work derived from the original RESTful API by Techjoomla (https://github.com/techjoomla/Joomla-REST-API)
* and the com_api extension by Brian Edgerton (http://www.edgewebworks.com)
*/
defined('_JEXEC') or die('Restricted access');
use Joomla\CMS\Filter\InputFilter;
use Joomla\CMS\Factory;
use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Uri\Uri;
/**
* ApiControllerHttp class
*
* @since 1.0
*/
class ApiControllerHttp extends ApiController
{
public $callbackname = 'callback';
/**
* Typical view method for MVC based architecture
*
* @param boolean $cachable If true, the view output will be cached
* @param array $urlparams An array of safe url parameters and their variable types, for valid values see {@link InputFilter::clean()}.
*
* @return Mixed
*
* @since 12.2
*/
public function display($cachable = false, $urlparams = array())
{
$this->resetDocumentType();
$app = Factory::getApplication();
$name = $app->input->get('app', '', 'CMD');
$params = ComponentHelper::getParams('com_api');
$callMethod = $app->input->getMethod();
$httpOrigin = $app->input->server->getString('HTTP_REFERER', '');
$UriObj = Uri::getInstance($httpOrigin);
$referer = $UriObj->toString(array('scheme', 'host', 'port'));
// Special method for OPTIONS method
if ((! empty($params->get("allow_cors"))))
{
$corsUrls = $params->get('cors', "*");
if ($corsUrls === "*")
{
header("Access-Control-Allow-Origin: " . '*');
}
else
{
$corsUrlsArray = array_map('trim', explode(',', $corsUrls));
if (in_array($referer, $corsUrlsArray))
{
header("Access-Control-Allow-Origin: " . $referer);
}
}
header("Access-Control-Allow-Methods: " . strtoupper($params->get("allow_cors")));
}
if ($callMethod === "OPTIONS")
{
header("Content-type: application/json");
header("Access-Control-Allow-Headers: " . $params->get("allow_headers"));
jexit();
}
try
{
header("status: 200");
//JResponse::setHeader('status', 200);
$resource_response = ApiPlugin::getInstance($name)->fetchResource();
echo $this->respond($resource_response);
}
catch (Exception $e)
{
header("status: " . $e->http_code);
//JResponse::setHeader('status', $e->http_code);
echo $this->respond($e);
}
}
/**
* Send the response in the correct format
*
* @param OBJECT $response exception
*
* @return json
*
* @since 2.0
*/
private function respond($response)
{
$app = Factory::getApplication();
$accept = $app->input->server->get('HTTP_ACCEPT', 'application/json', 'STRING');
$compatibility = $app->input->server->get('HTTP_X_COMPATIBILITY_MODE', 0, 'INT');
// Enforce JSON in compatibility mode
if ($compatibility)
{
$output = new \stdClass;
header("Content-type: application/json");
if ($response instanceof Exception)
{
$output->message = $response->getMessage();
$output->code = $response->getCode();
}
else
{
$output = $response->get('response');
}
echo json_encode($output);
die();
}
switch ($accept)
{
case 'application/json':
default:
header("Content-type: application/json");
$format = 'json';
break;
case 'application/xml':
header("Content-type: application/xml");
$format = 'xml';
break;
}
$output_overrride = JPATH_ROOT . '/templates/' . $app->getTemplate() . '/' . $format . '/api.php';
if (file_exists($output_overrride))
{
require_once $output_overrride;
}
else
{
require_once JPATH_COMPONENT . '/libraries/response/' . $format . 'response.php';
}
$classname = 'API' . ucfirst($format) . 'Response';
$output = new $classname($response);
echo $output->__toString();
jexit();
}
/**
* Resets the document type to format=raw
*
* @todo Figure out if there is a better way to do this
*
* @return void
*
* @since 0.1
*/
private function resetDocumentType()
{
if (!headers_sent())
{
foreach (headers_list() as $header)
{
header_remove(explode(":", $header, 2)[0]);
}
}
//JResponse::clearHeaders();
}
}