/**
* Render the data array.
*
* @param null $Data
* @return bool
* @throws Exception
*/
public function renderData($Data = null)
{
if ($Data === null) {
$Data = array();
// Remove standard and "protected" data from the top level.
foreach ($this->Data as $Key => $Value) {
if ($Key && in_array($Key, array('Title', 'Breadcrumbs'))) {
continue;
}
if (isset($Key[0]) && $Key[0] === '_') {
continue;
// protected
}
$Data[$Key] = $Value;
}
unset($this->Data);
}
// Massage the data for better rendering.
foreach ($Data as $Key => $Value) {
if (is_a($Value, 'Gdn_DataSet')) {
$Data[$Key] = $Value->resultArray();
}
}
$CleanOutut = c('Api.Clean', true);
if ($CleanOutut) {
// Remove values that should not be transmitted via api
$Remove = array('Password', 'HashMethod', 'TransientKey', 'Permissions', 'Attributes', 'AccessToken');
// Remove PersonalInfo values for unprivileged requests.
if (!Gdn::session()->checkPermission('Garden.Moderation.Manage')) {
$Remove[] = 'InsertIPAddress';
$Remove[] = 'UpdateIPAddress';
$Remove[] = 'LastIPAddress';
$Remove[] = 'AllIPAddresses';
$Remove[] = 'Fingerprint';
if (C('Api.Clean.Email', true)) {
$Remove[] = 'Email';
}
$Remove[] = 'DateOfBirth';
$Remove[] = 'Preferences';
$Remove[] = 'Banned';
$Remove[] = 'Admin';
$Remove[] = 'Confirmed';
$Remove[] = 'Verified';
$Remove[] = 'DiscoveryText';
$Remove[] = 'InviteUserID';
$Remove[] = 'DateSetInvitations';
$Remove[] = 'CountInvitations';
$Remove[] = 'CountNotifications';
$Remove[] = 'CountBookmarks';
$Remove[] = 'CountDrafts';
$Remove[] = 'HourOffset';
$Remove[] = 'Gender';
$Remove[] = 'Punished';
$Remove[] = 'Troll';
}
$Data = removeKeysFromNestedArray($Data, $Remove);
}
if (debug() && ($Trace = trace())) {
// Clear passwords from the trace.
array_walk_recursive($Trace, function (&$Value, $Key) {
if (in_array(strtolower($Key), array('password'))) {
$Value = '***';
}
});
$Data['Trace'] = $Trace;
}
// Make sure the database connection is closed before exiting.
$this->EventArguments['Data'] =& $Data;
$this->finalize();
// Add error information from the form.
if (isset($this->Form) && sizeof($this->Form->validationResults())) {
$this->statusCode(400);
$Data['Code'] = 400;
$Data['Exception'] = Gdn_Validation::resultsAsText($this->Form->validationResults());
}
$this->sendHeaders();
// Check for a special view.
$ViewLocation = $this->fetchViewLocation(($this->View ? $this->View : $this->RequestMethod) . '_' . strtolower($this->deliveryMethod()), false, false, false);
if (file_exists($ViewLocation)) {
include $ViewLocation;
return;
}
// Add schemes to to urls.
if (!c('Garden.AllowSSL') || c('Garden.ForceSSL')) {
$r = array_walk_recursive($Data, array('Gdn_Controller', '_FixUrlScheme'), Gdn::request()->scheme());
}
if (ob_get_level()) {
ob_clean();
}
switch ($this->deliveryMethod()) {
case DELIVERY_METHOD_XML:
safeHeader('Content-Type: text/xml', true);
echo '<?xml version="1.0" encoding="utf-8"?>' . "\n";
$this->_renderXml($Data);
return true;
break;
case DELIVERY_METHOD_PLAIN:
return true;
break;
case DELIVERY_METHOD_JSON:
default:
if (($Callback = $this->Request->get('callback', false)) && $this->allowJSONP()) {
safeHeader('Content-Type: application/javascript; charset=' . c('Garden.Charset', 'utf-8'), true);
// This is a jsonp request.
echo $Callback . '(' . json_encode($Data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . ');';
return true;
} else {
safeHeader('Content-Type: application/json; charset=' . c('Garden.Charset', 'utf-8'), true);
// This is a regular json request.
echo json_encode($Data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
return true;
}
break;
}
return false;
}