public static GenerateRandomKey ( integer $length = 128 ) : string | ||
$length | integer | optional The length of the key |
Résultat | string | Returns the random string of specified length |
public static function GenerateRandomKey($length = 128)
{
//Is there windows os?
if (DIRECTORY_SEPARATOR === '\\') {
//windows os
$rnd = '';
$t = ceil($length / 40);
for ($i = 0; $i < $t; ++$i) {
$rnd .= sha1(uniqid());
}
} else {
//unix os
$rnd = file_get_contents('/dev/urandom', null, null, 0, $length);
}
$key = substr(base64_encode($rnd), 0, $length);
return $key;
}
public function xSaveAction($url, $endpointId = null) { if (!$endpointId) { $endpoint = new WebhookEndpoint(); $endpoint->level = WebhookEndpoint::LEVEL_ENVIRONMENT; $endpoint->accountId = $this->getEnvironment()->clientId; $endpoint->envId = $this->getEnvironmentId(); $endpoint->securityKey = Scalr::GenerateRandomKey(64); } else { $endpoint = WebhookEndpoint::findPk($endpointId); if ($endpoint->envId != $this->getEnvironmentId() || $endpoint->accountId != $this->getEnvironment()->clientId) { throw new Scalr_Exception_Core('Insufficient permissions to edit endpoint'); } } foreach (WebhookEndpoint::findByUrl($url) as $ep) { if ($ep->endpointId != $endpoint->endpointId && $ep->envId == $this->getEnvironmentId()) { $this->request->addValidationErrors('url', 'Endpoint url must be unique within environment'); break; } } if ($this->request->isValid()) { if ($endpoint->url != $url) { $endpoint->isValid = false; $endpoint->url = $url; } ////temporarily disable url validation per Igor`s request(see also webhooks/endpoints/view.js) $endpoint->isValid = true; $endpoint->save(); $this->response->data(array('endpoint' => array('endpointId' => $endpoint->endpointId, 'url' => $endpoint->url, 'isValid' => $endpoint->isValid, 'validationToken' => $endpoint->validationToken, 'securityKey' => $endpoint->securityKey))); } else { $this->response->data($this->request->getValidationErrors()); $this->response->failure(); } }