/**
* @param int $id
* @param string $name
* @param string $description
* @param int $isSync
* @param bool $allowScriptParameters
* @param int $envId optional
* @param int $timeout optional
* @param int $version
* @param RawData $content
* @param string $tags
* @param string $uploadType optional
* @param string $uploadUrl optional
* @param FileUploadData $uploadFile optional
* @param bool $checkScriptParameters optional
* @throws Scalr_UI_Exception_NotFound
* @throws Scalr_Exception_InsufficientPermissions
* @throws Exception
*/
public function xSaveAction($id, $name, $description, $isSync = 0, $allowScriptParameters = false, $envId = NULL, $timeout = NULL, $version, RawData $content, $tags, $uploadType = NULL, $uploadUrl = NULL, FileUploadData $uploadFile = NULL, $checkScriptParameters = false)
{
$this->request->restrictAccess('SCRIPTS', 'MANAGE');
$validator = new Validator();
$validator->validate($name, 'name', Validator::NOEMPTY);
if ($uploadType && $uploadType == 'URL') {
$validator->validate($uploadUrl, 'uploadUrl', Validator::URL);
if (!$validator->isValid($this->response)) {
return;
}
}
if ($uploadType) {
$content = false;
if ($uploadType == 'URL') {
$content = @file_get_contents($uploadUrl);
$validator->validate($content, 'uploadUrl', Validator::NOEMPTY, [], 'Invalid source');
} else {
if ($uploadType == 'File') {
$content = $uploadFile;
$validator->validate($content, 'uploadFile', Validator::NOEMPTY, [], 'Invalid source');
} else {
$validator->addError('uploadType', 'Invalid source for script');
}
}
}
$envId = $this->getEnvironmentId(true);
$content = str_replace("\r\n", "\n", $content);
$tagsResult = [];
foreach (explode(',', $tags) as $t) {
$t = trim($t);
if ($t) {
if (!preg_match('/^[a-zA-Z0-9-]{3,10}$/', $t)) {
$validator->addError('tags', sprintf('Invalid name for tag: %s', $t));
}
$tagsResult[] = $t;
}
}
$tags = $tagsResult;
$criteria = [];
$criteria[] = ['name' => $name];
if ($id) {
$criteria[] = ['id' => ['$ne' => $id]];
}
switch ($this->request->getScope()) {
case Script::SCOPE_ENVIRONMENT:
$criteria[] = ['envId' => $envId];
$criteria[] = ['accountId' => $this->user->getAccountId()];
break;
case Script::SCOPE_ACCOUNT:
$criteria[] = ['envId' => null];
$criteria[] = ['accountId' => $this->user->getAccountId()];
break;
case Script::SCOPE_SCALR:
$criteria[] = ['envId' => null];
$criteria[] = ['accountId' => null];
break;
}
if (Script::findOne($criteria)) {
$validator->addError('name', 'Script name must be unique within current scope');
}
if (!$validator->isValid($this->response)) {
return;
}
/* @var $script Script */
if ($id) {
$script = Script::findPk($id);
if (!$script) {
throw new Scalr_UI_Exception_NotFound();
}
$script->checkPermission($this->user, $envId);
if (!$script->accountId && $this->user->getType() != Scalr_Account_User::TYPE_SCALR_ADMIN) {
throw new Scalr_Exception_InsufficientPermissions();
}
if (!$script->envId && $this->request->getScope() == ScopeInterface::SCOPE_ENVIRONMENT) {
throw new Scalr_Exception_InsufficientPermissions();
}
} else {
$script = new Script();
$script->accountId = $this->user->getAccountId() ?: NULL;
$script->createdById = $this->user->getId();
$script->createdByEmail = $this->user->getEmail();
$script->envId = $envId;
$version = 1;
}
//check variables in script content
if (!$id && $checkScriptParameters && !$allowScriptParameters) {
$scriptHasParameters = Script::hasVariables($content);
if (!$scriptHasParameters) {
/* @var $scriptVersion ScriptVersion */
foreach ($script->getVersions() as $scriptVersion) {
if ($scriptVersion->version != $version) {
$scriptHasParameters = Script::hasVariables($scriptVersion->content);
if ($scriptHasParameters) {
break;
}
}
}
}
if ($scriptHasParameters) {
$this->response->data(['showScriptParametersConfirmation' => true]);
$this->response->failure();
return;
}
}
$script->name = $name;
$script->description = $description;
$script->timeout = $timeout ? $timeout : NULL;
$script->isSync = $isSync == 1 ? 1 : 0;
$script->allowScriptParameters = $allowScriptParameters ? 1 : 0;
$script->os = !strncmp($content, '#!cmd', strlen('#!cmd')) || !strncmp($content, '#!powershell', strlen('#!powershell')) ? Script::OS_WINDOWS : Script::OS_LINUX;
$script->save();
$scriptVersion = NULL;
if ($version) {
$scriptVersion = $script->getVersion($version);
}
if (!$scriptVersion && $script->getLatestVersion()->content !== $content) {
$scriptVersion = new ScriptVersion();
$scriptVersion->scriptId = $script->id;
$scriptVersion->version = $script->getLatestVersion()->version + 1;
}
if ($scriptVersion) {
$scriptVersion->changedById = $this->user->getId();
$scriptVersion->changedByEmail = $this->user->getEmail();
$scriptVersion->content = $content;
$scriptVersion->save();
}
if ($this->user->getAccountId()) {
Tag::setTags($tags, $this->user->getAccountId(), Tag::RESOURCE_SCRIPT, $script->id);
}
$this->response->success('Script successfully saved');
$this->response->data(['script' => array_merge($this->getScript($script), $this->getScriptInfo($script))]);
}