本文整理汇总了PHP中Imbo\EventManager\EventInterface::getRequest方法的典型用法代码示例。如果您正苦于以下问题:PHP EventInterface::getRequest方法的具体用法?PHP EventInterface::getRequest怎么用?PHP EventInterface::getRequest使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Imbo\EventManager\EventInterface
的用法示例。
在下文中一共展示了EventInterface::getRequest方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: prepareImage
/**
* Prepare an image
*
* This method should prepare an image object from php://input. The method must also figure out
* the width, height, mime type and extension of the image.
*
* @param EventInterface $event The current event
* @throws ImageException
*/
public function prepareImage(EventInterface $event)
{
$request = $event->getRequest();
// Fetch image data from input
$imageBlob = $request->getContent();
if (empty($imageBlob)) {
$e = new ImageException('No image attached', 400);
$e->setImboErrorCode(Exception::IMAGE_NO_IMAGE_ATTACHED);
throw $e;
}
// Open the image with imagick to fetch the mime type
$imagick = new Imagick();
try {
$imagick->readImageBlob($imageBlob);
$mime = $imagick->getImageMimeType();
$size = $imagick->getImageGeometry();
} catch (ImagickException $e) {
$e = new ImageException('Invalid image', 415);
$e->setImboErrorCode(Exception::IMAGE_INVALID_IMAGE);
throw $e;
}
if (!Image::supportedMimeType($mime)) {
$e = new ImageException('Unsupported image type: ' . $mime, 415);
$e->setImboErrorCode(Exception::IMAGE_UNSUPPORTED_MIMETYPE);
throw $e;
}
// Store relevant information in the image instance and attach it to the request
$image = new Image();
$image->setMimeType($mime)->setExtension(Image::getFileExtension($mime))->setBlob($imageBlob)->setWidth($size['width'])->setHeight($size['height'])->setOriginalChecksum(md5($imageBlob));
$request->setImage($image);
}
示例2: setHeaders
/**
* Right before the response is sent to the client, check if any HTTP cache control headers
* have explicity been set for this response. If not, apply the configured defaults.
*
* @param EventInterface $event The event instance
*/
public function setHeaders(EventInterface $event)
{
$method = $event->getRequest()->getMethod();
// Obviously we shouldn't bother doing any HTTP caching logic for non-GET/HEAD requests
if ($method !== 'GET' && $method !== 'HEAD') {
return;
}
$response = $event->getResponse();
$headers = $event->getResponse()->headers;
// Imbo defaults to 'public' as cache-control value - if it has changed from this value,
// assume the resource requested has explicitly defined its own caching rules and fall back
if ($headers->get('Cache-Control') !== 'public') {
return;
}
// Get configured HTTP cache defaults from configuration, then apply them
$config = $event->getConfig()['httpCacheHeaders'];
if (isset($config['maxAge'])) {
$response->setMaxAge((int) $config['maxAge']);
}
if (isset($config['sharedMaxAge'])) {
$response->setSharedMaxAge($config['sharedMaxAge']);
}
if (isset($config['public']) && $config['public']) {
$response->setPublic();
} else {
if (isset($config['public'])) {
$response->setPrivate();
}
}
if (isset($config['mustRevalidate']) && $config['mustRevalidate']) {
$headers->addCacheControlDirective('must-revalidate');
}
}
示例3: generateImageIdentifier
/**
* Using the configured image identifier generator, attempt to generate a unique image
* identifier for the given image until we either have found a unique ID or we hit the maximum
* allowed attempts.
*
* @param EventInterface $event The current event
* @param Image $image The event to generate the image identifier for
* @return string
* @throws ImageException
*/
private function generateImageIdentifier(EventInterface $event, Image $image)
{
$database = $event->getDatabase();
$config = $event->getConfig();
$user = $event->getRequest()->getUser();
$imageIdentifierGenerator = $config['imageIdentifierGenerator'];
if (is_callable($imageIdentifierGenerator) && !$imageIdentifierGenerator instanceof GeneratorInterface) {
$imageIdentifierGenerator = $imageIdentifierGenerator();
}
if ($imageIdentifierGenerator->isDeterministic()) {
return $imageIdentifierGenerator->generate($image);
}
// Continue generating image identifiers until we get one that does not already exist
$maxAttempts = 100;
$attempts = 0;
do {
$imageIdentifier = $imageIdentifierGenerator->generate($image);
$attempts++;
} while ($attempts < $maxAttempts && $database->imageExists($user, $imageIdentifier));
// Did we reach our max attempts limit?
if ($attempts === $maxAttempts) {
$e = new ImageException('Failed to generate unique image identifier', 503);
$e->setImboErrorCode(Exception::IMAGE_IDENTIFIER_GENERATION_FAILED);
// Tell the client it's OK to retry later
$event->getResponse()->headers->set('Retry-After', 1);
throw $e;
}
return $imageIdentifier;
}
示例4: addRules
/**
* Add access rules for the specified public key
*
* @param EventInterface $event The current event
*/
public function addRules(EventInterface $event)
{
$accessControl = $event->getAccessControl();
if (!$accessControl instanceof MutableAdapterInterface) {
throw new ResourceException('Access control adapter is immutable', 405);
}
$request = $event->getRequest();
$publicKey = $request->getRoute()->get('publickey');
$data = json_decode($request->getContent(), true);
if (!is_array($data)) {
throw new InvalidArgumentException('No access rule data provided', 400);
}
// If a single rule was provided, wrap it in an array
if (!count($data) || !isset($data[0])) {
$data = [$data];
}
$accessControl = $event->getAccessControl();
// Perform rule validation
foreach ($data as $rule) {
$this->validateRule($event, $rule);
}
// Insert the rules
foreach ($data as $rule) {
$accessControl->addAccessRule($publicKey, $rule);
}
}
示例5: transform
/**
* Transform images
*
* @param EventInterface $event The current event
*/
public function transform(EventInterface $event)
{
$request = $event->getRequest();
$image = $event->getResponse()->getModel();
$eventManager = $event->getManager();
$presets = $event->getConfig()['transformationPresets'];
// Fetch transformations specifed in the query and transform the image
foreach ($request->getTransformations() as $transformation) {
if (isset($presets[$transformation['name']])) {
// Preset
foreach ($presets[$transformation['name']] as $name => $params) {
if (is_int($name)) {
// No hardcoded params, use the ones from the request
$name = $params;
$params = $transformation['params'];
} else {
// Some hardcoded params. Merge with the ones from the request, making the
// hardcoded params overwrite the ones from the request
$params = array_replace($transformation['params'], $params);
}
$eventManager->trigger('image.transformation.' . strtolower($name), array('image' => $image, 'params' => $params));
}
} else {
// Regular transformation
$eventManager->trigger('image.transformation.' . strtolower($transformation['name']), array('image' => $image, 'params' => $transformation['params']));
}
}
}
示例6: checkAccessToken
/**
* {@inheritdoc}
*/
public function checkAccessToken(EventInterface $event)
{
$request = $event->getRequest();
$response = $event->getResponse();
$query = $request->query;
$eventName = $event->getName();
if (($eventName === 'image.get' || $eventName === 'image.head') && $this->isWhitelisted($request)) {
// All transformations in the request are whitelisted. Skip the access token check
return;
}
// If the response has a short URL header, we can skip the access token check
if ($response->headers->has('X-Imbo-ShortUrl')) {
return;
}
if (!$query->has('accessToken')) {
throw new RuntimeException('Missing access token', 400);
}
$token = $query->get('accessToken');
// First the the raw un-encoded URI, then the URI as is
$uris = array($request->getRawUri(), $request->getUriAsIs());
$privateKeys = $event->getUserLookup()->getPrivateKeys($request->getPublicKey()) ?: [];
foreach ($uris as $uri) {
// Remove the access token from the query string as it's not used to generate the HMAC
$uri = rtrim(preg_replace('/(?<=(\\?|&))accessToken=[^&]+&?/', '', $uri), '&?');
foreach ($privateKeys as $privateKey) {
$correctToken = hash_hmac('sha256', $uri, $privateKey);
if ($correctToken === $token) {
return;
}
}
}
throw new RuntimeException('Incorrect access token', 400);
}
示例7: send
/**
* Send the response
*
* @param EventInterface $event The current event
*/
public function send(EventInterface $event)
{
$request = $event->getRequest();
$response = $event->getResponse();
// Vary on public key header. Public key specified in query and URL path doesn't have to be
// taken into consideration, since they will have varying URLs
$response->setVary('X-Imbo-PublicKey', false);
// Optionally mark this response as not modified
$response->isNotModified($request);
// Inject a possible image identifier into the response headers
$imageIdentifier = null;
if ($image = $request->getImage()) {
// The request has an image. This means that an image was just added.
// Get the image identifier from the image model
$imageIdentifier = $image->getImageIdentifier();
} else {
if ($identifier = $request->getImageIdentifier()) {
// An image identifier exists in the request URI, use that
$imageIdentifier = $identifier;
}
}
if ($imageIdentifier) {
$response->headers->set('X-Imbo-ImageIdentifier', $imageIdentifier);
}
$response->send();
}
示例8: updateModelBeforeStoring
/**
* Update the image model blob before storing it in case an event listener has changed the
* image
*
* @param EventInterface $event The event instance
*/
public function updateModelBeforeStoring(EventInterface $event)
{
$image = $event->getRequest()->getImage();
if ($image->hasBeenTransformed()) {
$image->setBlob($this->imagick->getImageBlob());
}
}
示例9: getImages
/**
* Handle GET and HEAD requests
*
* @param EventInterface $event The current event
*/
public function getImages(EventInterface $event)
{
$acl = $event->getAccessControl();
$missingAccess = [];
$users = $event->getRequest()->getUsers();
foreach ($users as $user) {
$hasAccess = $acl->hasAccess($event->getRequest()->getPublicKey(), 'images.get', $user);
if (!$hasAccess) {
$missingAccess[] = $user;
}
}
if (!empty($missingAccess)) {
throw new RuntimeException('Public key does not have access to the users: [' . implode(', ', $missingAccess) . ']', 400);
}
$event->getManager()->trigger('db.images.load', ['users' => $users]);
}
示例10: addHeader
/**
* Add the HashTwo header to the response
*
* @param EventInterface $event The current event
*/
public function addHeader(EventInterface $event)
{
$request = $event->getRequest();
$response = $event->getResponse();
$user = $request->getUser();
$imageIdentifier = $response->getModel()->getImageIdentifier();
$response->headers->set($this->header, ['imbo;image;' . $user . ';' . $imageIdentifier, 'imbo;user;' . $user]);
}
示例11: addHeader
/**
* Add the HashTwo header to the response
*
* @param EventInterface $event The current event
*/
public function addHeader(EventInterface $event)
{
$request = $event->getRequest();
$response = $event->getResponse();
$publicKey = $request->getPublicKey();
$imageIdentifier = $response->getModel()->getImageIdentifier();
$response->headers->set($this->header, array('imbo;image;' . $publicKey . ';' . $imageIdentifier, 'imbo;user;' . $publicKey));
}
示例12: enforceMaxSize
/**
* {@inheritdoc}
*/
public function enforceMaxSize(EventInterface $event)
{
$image = $event->getRequest()->getImage();
$width = $image->getWidth();
$height = $image->getHeight();
if ($this->width && $width > $this->width || $this->height && $height > $this->height) {
$event->getManager()->trigger('image.transformation.maxsize', array('image' => $image, 'params' => array('width' => $this->width, 'height' => $this->height)));
}
}
示例13: queueRequest
/**
* Handle other requests
*
* @param EventInterface $event The event instance
*/
public function queueRequest(EventInterface $event)
{
$request = $event->getRequest();
$eventName = $event->getName();
$urls = (array) $this->params[$eventName];
$data = ['event' => $eventName, 'url' => $request->getRawUri(), 'imageIdentifier' => $request->getImageIdentifier(), 'publicKey' => $request->getPublicKey()];
foreach ($urls as $url) {
$this->requestQueue[] = $this->getHttpClient()->post($url, null, $data);
}
}
示例14: addImage
/**
* Handle POST requests
*
* @param EventInterface
*/
public function addImage(EventInterface $event)
{
$event->getManager()->trigger('db.image.insert');
$event->getManager()->trigger('storage.image.insert');
$request = $event->getRequest();
$response = $event->getResponse();
$image = $request->getImage();
$model = new Model\ArrayModel();
$model->setData(['imageIdentifier' => $image->getImageIdentifier(), 'width' => $image->getWidth(), 'height' => $image->getHeight(), 'extension' => $image->getExtension()]);
$response->setModel($model);
}
示例15: checkAccess
/**
* {@inheritdoc}
*/
public function checkAccess(EventInterface $event)
{
$request = $event->getRequest();
$ip = $request->getClientIp();
if ($this->isIPv6($ip)) {
$ip = $this->expandIPv6($ip);
}
$access = $this->isAllowed($ip);
if (!$access) {
throw new RuntimeException('Access denied', 403);
}
}