public function xRequestResultAction() { $this->request->defineParams(array('requests' => array('type' => 'json'), 'decision')); if (!in_array($this->getParam('decision'), array(FarmLease::STATUS_APPROVE, FarmLease::STATUS_DECLINE))) { throw new Scalr_Exception_Core('Wrong status'); } foreach ($this->getParam('requests') as $id) { $req = $this->db->GetRow('SELECT * FROM farm_lease_requests WHERE id = ? LIMIT 1', array($id)); if ($req) { $dbFarm = DBFarm::LoadByID($req['farm_id']); $this->user->getPermissions()->validate($dbFarm); $this->db->Execute('UPDATE farm_lease_requests SET status = ?, answer_comment = ?, answer_user_id = ? WHERE id = ?', array($this->getParam('decision'), $this->getParam('comment'), $this->user->getId(), $id)); try { $mailer = Scalr::getContainer()->mailer; $user = new Scalr_Account_User(); $user->loadById($dbFarm->createdByUserId); if ($this->getContainer()->config('scalr.auth_mode') == 'ldap') { if ($user->getSetting(Scalr_Account_User::SETTING_LDAP_EMAIL)) { $mailer->addTo($user->getSetting(Scalr_Account_User::SETTING_LDAP_EMAIL)); } else { $mailer->addTo($user->getEmail()); } } else { $mailer->addTo($user->getEmail()); } } catch (Exception $e) { $mailer = null; } if ($this->getParam('decision') == FarmLease::STATUS_APPROVE) { if ($req['request_days'] > 0) { $dt = $dbFarm->GetSetting(DBFarm::SETTING_LEASE_TERMINATE_DATE); $dt = new DateTime($dt); $dt->add(new DateInterval('P' . $req['request_days'] . 'D')); $dbFarm->SetSetting(DBFarm::SETTING_LEASE_TERMINATE_DATE, $dt->format('Y-m-d H:i:s')); $dbFarm->SetSetting(DBFarm::SETTING_LEASE_NOTIFICATION_SEND, null); if ($mailer) { $mailer->sendTemplate(SCALR_TEMPLATES_PATH . '/emails/farm_lease_non_standard_approve.eml', array('{{farm_name}}' => $dbFarm->Name, '{{user_name}}' => $this->user->getEmail(), '{{comment}}' => $this->getParam('comment'), '{{date}}' => $dt->format('M j, Y'), '{{envName}}' => $dbFarm->GetEnvironmentObject()->name, '{{envId}}' => $dbFarm->GetEnvironmentObject()->id)); } } else { $dbFarm->SetSetting(DBFarm::SETTING_LEASE_STATUS, ''); $dbFarm->SetSetting(DBFarm::SETTING_LEASE_TERMINATE_DATE, ''); $dbFarm->SetSetting(DBFarm::SETTING_LEASE_NOTIFICATION_SEND, ''); if ($mailer) { $mailer->sendTemplate(SCALR_TEMPLATES_PATH . '/emails/farm_lease_non_standard_forever.eml', array('{{farm_name}}' => $dbFarm->Name, '{{user_name}}' => $this->user->getEmail(), '{{comment}}' => $this->getParam('comment'), '{{envName}}' => $dbFarm->GetEnvironmentObject()->name, '{{envId}}' => $dbFarm->GetEnvironmentObject()->id)); } } } else { $dt = new DateTime($dbFarm->GetSetting(DBFarm::SETTING_LEASE_TERMINATE_DATE)); SettingEntity::increase(SettingEntity::LEASE_DECLINED_REQUEST); if ($mailer) { $mailer->sendTemplate(SCALR_TEMPLATES_PATH . '/emails/farm_lease_non_standard_decline.eml', array('{{farm_name}}' => $dbFarm->Name, '{{user_name}}' => $this->user->getEmail(), '{{date}}' => $dt->format('M j, Y'), '{{comment}}' => $this->getParam('comment'), '{{envName}}' => $dbFarm->GetEnvironmentObject()->name, '{{envId}}' => $dbFarm->GetEnvironmentObject()->id)); } } } } $this->response->success(); }
function handleWork($farmId) { try { $dbFarm = DBFarm::LoadByID($farmId); $governance = new Scalr_Governance($dbFarm->EnvID); $settings = $governance->getValue(Scalr_Governance::CATEGORY_GENERAL, Scalr_Governance::GENERAL_LEASE, 'notifications'); $curDate = new DateTime(); $td = new DateTime($dbFarm->GetSetting(DBFarm::SETTING_LEASE_TERMINATE_DATE)); if ($td > $curDate) { // only inform user $days = $td->diff($curDate)->days; $notifications = json_decode($dbFarm->GetSetting(DBFarm::SETTING_LEASE_NOTIFICATION_SEND), true); if (is_array($settings)) { foreach ($settings as $n) { if (!$notifications[$n['key']] && $n['period'] >= $days) { $mailer = Scalr::getContainer()->mailer; $tdHuman = Scalr_Util_DateTime::convertDateTime($td, $dbFarm->GetSetting(DBFarm::SETTING_TIMEZONE), 'M j, Y'); if ($n['to'] == 'owner') { $user = new Scalr_Account_User(); $user->loadById($dbFarm->createdByUserId); if (Scalr::config('scalr.auth_mode') == 'ldap') { $email = $user->getSetting(Scalr_Account_User::SETTING_LDAP_EMAIL); if (!$email) { $email = $user->getEmail(); } } else { $email = $user->getEmail(); } $mailer->addTo($email); } else { foreach (explode(',', $n['emails']) as $email) { $mailer->addTo(trim($email)); } } $mailer->sendTemplate(SCALR_TEMPLATES_PATH . '/emails/farm_lease_terminate.eml', array('{{terminate_date}}' => $tdHuman, '{{farm}}' => $dbFarm->Name, '{{envName}}' => $dbFarm->GetEnvironmentObject()->name, '{{envId}}' => $dbFarm->GetEnvironmentObject()->id)); $notifications[$n['key']] = 1; $dbFarm->SetSetting(DBFarm::SETTING_LEASE_NOTIFICATION_SEND, json_encode($notifications)); $this->logger->info("Notification was sent by key: " . $n['key'] . " about farm: " . $dbFarm->Name . " by lease manager"); } } } } else { // terminate farm $event = new FarmTerminatedEvent(0, 1, false, 1); Scalr::FireEvent($farmId, $event); $this->logger->info("Farm: " . $dbFarm->Name . " was terminated by lease manager"); } } catch (Exception $e) { var_dump($e->getMessage()); } }
public function BuildRestServer($request) { try { $Reflect = new ReflectionObject($this); if ($Reflect->hasMethod($request['Action'])) { //Authenticate if ($request['AuthType'] == 'ldap') { $this->AuthenticateLdap($request); } else { if ($request['AuthVersion'] == 2) { $this->AuthenticateRESTv2($request); } elseif ($request['AuthVersion'] == 3) { $this->AuthenticateRESTv3($request); } else { $this->AuthenticateREST($request); } if ($this->user->getSetting(Scalr_Account_User::SETTING_API_ENABLED) != 1) { throw new Exception(_("Your API keys are currently disabled. You can enable access at Settings > API access.")); } //Check IP Addresses if ($this->user->getSetting(Scalr_Account_User::SETTING_API_IP_WHITELIST)) { $ips = explode(",", $this->user->getSetting(Scalr_Account_User::SETTING_API_IP_WHITELIST)); if (!$this->IPAccessCheck($ips)) { throw new Exception(sprintf(_("Access to the API is not allowed from your IP '%s'"), $_SERVER['REMOTE_ADDR'])); } } } //Check limit if ($this->Environment->getPlatformConfigValue(Scalr_Environment::SETTING_API_LIMIT_ENABLED, false) == 1) { $hour = $this->Environment->getPlatformConfigValue(Scalr_Environment::SETTING_API_LIMIT_HOUR, false); $limit = $this->Environment->getPlatformConfigValue(Scalr_Environment::SETTING_API_LIMIT_REQPERHOUR, false); $usage = $this->Environment->getPlatformConfigValue(Scalr_Environment::SETTING_API_LIMIT_USAGE, false); if ($usage >= $limit && $hour == date("YmdH")) { $reset = 60 - (int) date("i"); header("HTTP/1.0 429 Too Many Requests"); exit; //throw new Exception(sprintf("Hourly API requests limit (%s) exceeded. Limit will be reset within %s minutes", $limit, $reset)); } if (date("YmdH") > $hour) { $hour = date("YmdH"); $usage = 0; } $this->Environment->setPlatformConfig(array(Scalr_Environment::SETTING_API_LIMIT_USAGE => $usage + 1, Scalr_Environment::SETTING_API_LIMIT_HOUR => $hour), false); } //Execute API call $ReflectMethod = $Reflect->getMethod($request['Action']); $args = array(); foreach ($ReflectMethod->getParameters() as $param) { if (!$param->isOptional() && !isset($request[$param->getName()])) { throw new Exception(sprintf("Missing required parameter '%s'", $param->getName())); } else { if ($param->isArray()) { $args[$param->getName()] = (array) $request[$param->getName()]; } else { $args[$param->getName()] = $request[$param->getName()]; } } } $result = $ReflectMethod->invokeArgs($this, $args); $this->LastTransactionID = $result->TransactionID; // Create response $DOMDocument = new DOMDocument('1.0', 'UTF-8'); $DOMDocument->loadXML("<{$request['Action']}Response></{$request['Action']}Response>"); $this->ObjectToXML($result, $DOMDocument->documentElement, $DOMDocument); $retval = $DOMDocument->saveXML(); } else { throw new Exception(sprintf("Action '%s' is not defined", $request['Action'])); } } catch (Exception $e) { if (!$this->LastTransactionID) { $this->LastTransactionID = Scalr::GenerateUID(); } $retval = "<?xml version=\"1.0\"?>\n" . "<Error>\n" . "\t<TransactionID>{$this->LastTransactionID}</TransactionID>\n" . "\t<Message>{$e->getMessage()}</Message>\n" . "</Error>\n"; } if (isset($this->user)) { $this->LogRequest($this->LastTransactionID, $request['Action'], $_SERVER['REMOTE_ADDR'], $request, $retval); } header("Content-type: text/xml"); header("Content-length: " . strlen($retval)); header("Access-Control-Allow-Origin: *"); print $retval; }
public function BuildRestServer($request) { try { $Reflect = new ReflectionObject($this); if ($Reflect->hasMethod($request['Action'])) { //Authenticate if ($request['AuthVersion'] == 2) { $this->AuthenticateRESTv2($request); } elseif ($request['AuthVersion'] == 3) { $this->AuthenticateRESTv3($request); } else { $this->AuthenticateREST($request); } if ($this->user->getSetting(Scalr_Account_User::SETTING_API_ENABLED) != 1) { throw new Exception(_("API disabled for you. You can enable it at 'Settings -> Environments'")); } //Check IP Addresses if ($this->user->getSetting(Scalr_Account_User::SETTING_API_IP_WHITELIST)) { $ips = explode(",", $this->user->getSetting(Scalr_Account_User::SETTING_API_IP_WHITELIST)); if (!$this->IPAccessCheck($ips) && $_SERVER['REMOTE_ADDR'] != API_SERVER_IP) { throw new Exception(sprintf(_("Access to the API is not allowed from your IP '%s'"), $_SERVER['REMOTE_ADDR'])); } } //Execute API call $ReflectMethod = $Reflect->getMethod($request['Action']); $args = array(); foreach ($ReflectMethod->getParameters() as $param) { if (!$param->isOptional() && !isset($request[$param->getName()])) { throw new Exception(sprintf("Missing required parameter '%s'", $param->getName())); } else { $args[$param->getName()] = $request[$param->getName()]; } } $result = $ReflectMethod->invokeArgs($this, $args); $this->LastTransactionID = $result->TransactionID; // Create response $DOMDocument = new DOMDocument('1.0', 'UTF-8'); $DOMDocument->loadXML("<{$request['Action']}Response></{$request['Action']}Response>"); $this->ObjectToXML($result, $DOMDocument->documentElement, $DOMDocument); $retval = $DOMDocument->saveXML(); } else { throw new Exception(sprintf("Action '%s' is not defined", $request['Action'])); } } catch (Exception $e) { if (!$this->LastTransactionID) { $this->LastTransactionID = Scalr::GenerateUID(); } if ($request['SysDebug']) { $debugInfo = "<StringToSign>{$this->debug['stringToSign']}</StringToSign>"; $debugInfo .= "<reqSignature>{$this->debug['reqSignature']}</reqSignature>"; $debugInfo .= "<validSignature>{$this->debug['validSignature']}</validSignature>"; $debugInfo .= "<md5AccessKey>{$this->debug['md5AccessKey']}</md5AccessKey>"; $debugInfo .= "<usedAuthVersion>{$this->debug['usedAuthVersion']}</usedAuthVersion>"; } $retval = "<?xml version=\"1.0\"?>\n" . "<Error>\n" . "\t<TransactionID>{$this->LastTransactionID}</TransactionID>\n" . "\t<Message>{$e->getMessage()}</Message>\n" . $debugInfo . "</Error>\n"; } $this->LogRequest($this->LastTransactionID, $request['Action'], $_SERVER['REMOTE_ADDR'] == API_SERVER_IP ? 'Mobile app' : $_SERVER['REMOTE_ADDR'], $request, $retval); header("Content-type: text/xml"); header("Content-length: " . strlen($retval)); print $retval; }