public static function getEventScriptList(AbstractServerEvent $event, DBServer $eventServer, DBServer $targetServer)
{
$db = \Scalr::getDb();
$accountScripts = $db->GetAll("\n SELECT * FROM account_scripts\n WHERE account_id = ?\n AND (event_name = ? OR event_name = '*')\n ", [$eventServer->clientId, $event->GetName()]);
$roleScripts = $db->GetAll("\n SELECT * FROM role_scripts\n WHERE (event_name = ? OR event_name = '*') AND role_id = ?\n ", [$event->GetName(), $eventServer->GetFarmRoleObject()->RoleID]);
$scripts = $db->GetAll("\n SELECT *, `script_type` as `type`\n FROM farm_role_scripts\n WHERE (event_name = ? OR event_name = '*') AND farmid = ?\n ", [$event->GetName(), $eventServer->farmId]);
foreach ($accountScripts as $script) {
$scripts[] = array("id" => "a{$script['id']}", "type" => $script['script_type'], "scriptid" => $script['script_id'], "params" => $script['params'], "event_name" => $event->GetName(), "target" => $script['target'], "version" => $script['version'], "timeout" => $script['timeout'], "issync" => $script['issync'], "order_index" => $script['order_index'], "scope" => "account", 'script_path' => $script['script_path'], 'run_as' => $script['run_as'], 'script_type' => $script['script_type']);
}
foreach ($roleScripts as $script) {
$params = $db->GetOne("\n SELECT params\n FROM farm_role_scripting_params\n WHERE farm_role_id = ?\n AND `hash` = ?\n AND farm_role_script_id = '0'\n LIMIT 1\n ", array($eventServer->farmRoleId, $script['hash']));
if ($params) {
$script['params'] = $params;
}
$scripts[] = array("id" => "r{$script['id']}", "scriptid" => $script['script_id'], "type" => $script['script_type'], "params" => $script['params'], "event_name" => $event->GetName(), "target" => $script['target'], "version" => $script['version'], "timeout" => $script['timeout'], "issync" => $script['issync'], "order_index" => $script['order_index'], "scope" => "role", 'script_path' => $script['script_path'], 'run_as' => $script['run_as'], 'script_type' => $script['script_type']);
}
$retval = [];
foreach ($scripts as $scriptSettings) {
$scriptSettings['order_index'] = (double) $scriptSettings['order_index'];
// If target set to that instance only
if ($scriptSettings['target'] == Script::TARGET_INSTANCE && $eventServer->serverId != $targetServer->serverId) {
continue;
}
// If target set to all instances in specific role
if ($scriptSettings['target'] == Script::TARGET_ROLE && $eventServer->farmRoleId != $targetServer->farmRoleId) {
continue;
}
if (!$scriptSettings['scope']) {
// Validate that event was triggered on the same farmRoleId as script
if ($eventServer->farmRoleId != $scriptSettings['farm_roleid']) {
continue;
}
// Validate that target server has the same farmRoleId as event server with target ROLE
if ($scriptSettings['target'] == Script::TARGET_ROLE && $targetServer->farmRoleId != $scriptSettings['farm_roleid']) {
continue;
}
}
if ($scriptSettings['target'] == Script::TARGET_ROLES || $scriptSettings['target'] == Script::TARGET_BEHAVIORS || $scriptSettings['target'] == Script::TARGET_FARMROLES) {
if ($scriptSettings['scope'] != 'role') {
$targets = $db->GetAll("SELECT * FROM farm_role_scripting_targets WHERE farm_role_script_id = ?", array($scriptSettings['id']));
} else {
$targets = [];
}
$execute = false;
foreach ($targets as $target) {
switch ($target['target_type']) {
case "farmrole":
if ($scriptSettings['target'] == Script::TARGET_ROLES && $targetServer->farmRoleId == $target['target'] || $scriptSettings['target'] == Script::TARGET_FARMROLES && $targetServer->GetFarmRoleObject()->Alias == $target['target']) {
$execute = true;
}
break;
case "behavior":
if ($targetServer->GetFarmRoleObject()->GetRoleObject()->hasBehavior($target['target'])) {
$execute = true;
}
break;
}
}
if (!$execute) {
continue;
}
}
if ($scriptSettings['target'] == "" || $scriptSettings['id'] == "") {
continue;
}
$script = self::prepareScript($scriptSettings, $targetServer, $event);
if ($script) {
while (true) {
$index = (string) $scriptSettings['order_index'];
if (empty($retval[$index])) {
$retval[$index] = $script;
break;
} else {
$scriptSettings['order_index'] += 0.01;
}
}
}
}
if (!empty($retval) && is_array($retval)) {
ksort($retval);
}
return $retval;
}