-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use controller style for register managed server
- Loading branch information
Showing
10 changed files
with
458 additions
and
117 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
<?php | ||
|
||
namespace OCA\OJSXC\Controller; | ||
|
||
use OCP\AppFramework\Controller; | ||
use OCP\IConfig; | ||
use OCP\IRequest; | ||
use OCP\IUserSession; | ||
use OCP\IURLGenerator; | ||
use OCP\ILogger; | ||
use OCP\AppFramework\Http\JSONResponse; | ||
use OCP\AppFramework\Http; | ||
use OCA\OJSXC\Exceptions\Exception; | ||
use OCA\OJSXC\IDataRetriever; | ||
|
||
class ManagedServerController extends Controller | ||
{ | ||
private $urlGenerator; | ||
private $config; | ||
private $userSession; | ||
private $logger; | ||
private $dataRetriever; | ||
private $registrationUrl; | ||
|
||
public function __construct($appName, | ||
IRequest $request, | ||
IURLGenerator $urlGenerator, | ||
IConfig $config, | ||
IUserSession $userSession, | ||
ILogger $logger, | ||
IDataRetriever $dataRetriever, | ||
$registrationUrl) { | ||
parent::__construct($appName, $request); | ||
|
||
$this->urlGenerator = $urlGenerator; | ||
$this->config = $config; | ||
$this->userSession = $userSession; | ||
$this->logger = $logger; | ||
$this->dataRetriever = $dataRetriever; | ||
$this->registrationUrl = $registrationUrl; | ||
} | ||
|
||
public function register($promotionCode = null) { | ||
$promotionCode = (preg_match('/^[0-9a-z]+$/i', $promotionCode)) ? $promotionCode : null; | ||
$registrationResult = false; | ||
|
||
try { | ||
$registrationResult = $this->doRegistration($promotionCode); | ||
} catch (\Exception $exception) { | ||
$this->logger->warning('RMS: Abort with message: '.$exception->getMessage()); | ||
|
||
return new JSONResponse(array( | ||
'result' => 'error', | ||
'data' => array( | ||
'msg' => $exception->getMessage() | ||
) | ||
), Http::STATUS_INTERNAL_SERVER_ERROR); | ||
} | ||
|
||
if ($registrationResult) { | ||
return array( | ||
'result' => 'success', | ||
'data' => array() | ||
); | ||
} | ||
} | ||
|
||
private function doRegistration($promotionCode) { | ||
$apiUrl = $this->urlGenerator->linkToRouteAbsolute('ojsxc.externalApi.index'); | ||
$apiSecret = $this->config->getAppValue('ojsxc', 'apiSecret'); | ||
$userId = $this->userSession->getUser()->getUID(); | ||
|
||
$data = array( | ||
'apiUrl' => $apiUrl, | ||
'apiSecret' => $apiSecret, | ||
'apiVersion' => 1, | ||
'userId' => $userId, | ||
'promotionCode' => $promotionCode | ||
); | ||
|
||
$response = $this->dataRetriever->fetchUrl($this->registrationUrl, $data); | ||
|
||
if ($response['body'] === false) { | ||
throw new Exception('Couldn\'t reach the registration server'); | ||
} | ||
|
||
$responseJSON = json_decode($response['body']); | ||
|
||
if ($responseJSON === null) { | ||
throw new Exception('Couldn\'t parse the response. Response code: '.$response['headers']['response_code']); | ||
} | ||
|
||
if ($response['headers']['response_code'] !== 200) { | ||
$this->logger->info('RMS: Response code: '.$response['headers']['response_code']); | ||
|
||
throw new Exception(htmlspecialchars($responseJSON->message)); | ||
} | ||
|
||
if (!preg_match('#^https://#', $responseJSON->boshUrl) || | ||
!preg_match('#/http-bind/?$#', $responseJSON->boshUrl) || | ||
preg_match('/\?|#/', $responseJSON->boshUrl)) { | ||
throw new Exception('Got a bad bosh URL'); | ||
} | ||
|
||
if (!preg_match('/^([a-z\d](-*[a-z\d])*)(\.([a-z\d](-*[a-z\d])*))*$/i', $responseJSON->domain) || | ||
!preg_match('/^.{1,253}$/', $responseJSON->domain) || | ||
!preg_match('/^[^\.]{1,63}(\.[^\.]{1,63})*$/', $responseJSON->domain)) { | ||
throw new Exception('Got a bad domain'); | ||
} | ||
|
||
$this->config->setAppValue('ojsxc', 'serverType', 'managed'); | ||
$this->config->setAppValue('ojsxc', 'boshUrl', $responseJSON->boshUrl); | ||
$this->config->setAppValue('ojsxc', 'xmppDomain', $responseJSON->domain); | ||
$this->config->setAppValue('ojsxc', 'timeLimitedToken', 'true'); | ||
$this->config->setAppValue('ojsxc', 'managedServer', 'registered'); | ||
$this->config->setAppValue('ojsxc', 'externalServices', implode('|', $responseJSON->externalServices)); | ||
|
||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
namespace OCA\OJSXC; | ||
|
||
class DataRetriever implements IDataRetriever { | ||
public function fetchUrl($url, $data = array()) { | ||
$context = stream_context_create(array('http' => | ||
array( | ||
'method' => 'POST', | ||
'ignore_errors' => '1', | ||
'header' => 'Content-type: application/x-www-form-urlencoded', | ||
'content' => http_build_query($data) | ||
) | ||
)); | ||
|
||
$body = file_get_contents($url, false, $context); | ||
$headers = []; | ||
|
||
if ($body !== false) { | ||
$headers = $this->parseHeaders($http_response_header); | ||
} | ||
|
||
return [ | ||
'body' => $body, | ||
'headers' => $headers | ||
]; | ||
} | ||
|
||
private function parseHeaders($headers) | ||
{ | ||
$head = array(); | ||
|
||
foreach ($headers as $k=>$v) { | ||
$t = explode(':', $v, 2); | ||
if (isset($t[1])) { | ||
$head[ trim($t[0]) ] = trim($t[1]); | ||
} else { | ||
$head[] = $v; | ||
if (preg_match('#HTTP/[0-9\.]+\s+([0-9]+)#', $v, $out)) { | ||
$head['reponse_code'] = intval($out[1]); | ||
} | ||
} | ||
} | ||
|
||
return $head; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
namespace OCA\OJSXC; | ||
|
||
interface IDataRetriever { | ||
public function fetchUrl($url, $data); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.