Mini Shell
<?php
/**
* Copyright © Cloud Linux GmbH & Cloud Linux Software, Inc 2010-2019 All Rights Reserved
*
* Licensed under CLOUD LINUX LICENSE AGREEMENT
* http://cloudlinux.com/docs/LICENSE.TXT
*/
namespace App\Base;
class Response
{
/**
* Prepares response with JSON header.
*
* @param $response
* @param int $statusCode
* @param string $description
* @param string $errorMessage
*
* @return string
*/
public static function asJson(
$response, $statusCode = 200, $description = 'OK', $errorMessage = ''
) {
$response = (object) $response;
if ($errorMessage) {
$response = self::extendResultByBilling($response, $errorMessage);
}
$header = self::prepareHeader(
'application/json', $statusCode, $description,
array('Cache-Control: no-cache, must-revalidate')
);
$header .= json_encode($response);
return $header;
}
/**
* Prepares response with binary file.
*
* @param $filename
*/
public static function serveFile($fileName, $fileSize, $requestData, $cloudlinuxCli) {
echo self::prepareHeader('application/x-download', 200, 'OK',
array('Content-Length: '.$fileSize));
$requestData['method'] = 'log_data';
$fullCommandStr = sprintf(
"%s --data=%s 2>&1",
$cloudlinuxCli, base64_encode(json_encode($requestData))
);
$descriptorspec = array(
0 => array("pipe", "r"), // stdin is a pipe that the child will read from
1 => array("pipe", "w"), // stdout is a pipe that the child will write to
2 => array("pipe", "w") // stderr is a pipe that the child will write to
);
$pipes = array();
$process = proc_open($fullCommandStr, $descriptorspec, $pipes);
if (is_resource($process)) {
while ($s = fgets($pipes[1])) {
print $s;
flush();
}
}
exit();
}
/**
* Prepares response with JAVASCRIPT header.
*
* @param $response
* @return string
*/
public static function asJS($response)
{
return self::prepareHeader('application/javascript').$response;
}
/**
* Prepares response with CSS header.
*
* @param $response
* @return string
*/
public static function asCSS($response)
{
return self::prepareHeader('text/css').$response;
}
/**
* Prepares response with HTML header.
*
* @param $response
* @return string
*/
public static function asHTML($response)
{
return self::prepareHeader('text/html').$response;
}
/**
* Prepares response with Fonts header.
*
* @param $response
* @return string
*/
public static function asFont($response)
{
return self::prepareHeader('application/font-otf').$response;
}
/**
* Prepares correct response header.
*
* @param $contentType
* @param int $statusCode
* @param string $description
* @param array $additionalHeaders
* @return string
*/
public static function prepareHeader(
$contentType, $statusCode = 200,
$description = 'OK', $additionalHeaders = array()
) {
$header = "HTTP/1.1 $statusCode $description\n";
if (!empty($additionalHeaders)) {
foreach ($additionalHeaders as $_header) {
$header .= $_header."\n";
}
}
$header .= "Content-type: $contentType\n\n";
return $header;
}
/**
* Extends response with error message.
*
* @param $response
* @param $errorMessage
*
* @return mixed
*/
public static function extendResultByBilling($response, $errorMessage)
{
switch (Base::load()->getBilling()) {
case 'Spa':
$response->result = $errorMessage;
break;
}
return $response;
}
}
Zerion Mini Shell 1.0