/** * Tests the event propagation stopping property * * @return void * @triggers fake.event */ public function testPropagation() { $event = new CakeEvent('fake.event'); $this->assertFalse($event->isStopped()); $event->stopPropagation(); $this->assertTrue($event->isStopped()); }
public function beforeDispatch(CakeEvent $event) { $MaintenanceMode = Configure::read('MaintenanceMode'); /* Not in maintenance mode*/ if (!$MaintenanceMode['enabled']) { return; } /* Allow access from following IPS*/ if (!empty($MaintenanceMode['ip_filters'])) { if (!is_array($MaintenanceMode['ip_filters'])) { $ips = array($MaintenanceMode['ip_filters']); } else { $ips = $MaintenanceMode['ip_filters']; } $userIP = $this->_getUserIpAddr(); foreach ($ips as $ip) { if ($this->_compareIp($userIP, $ip)) { return; } } } $statusCode = 503; $body = 'Currently undergoing maintenance'; if (!empty($MaintenanceMode['code'])) { $statusCode = $MaintenanceMode['code']; } if (!empty($MaintenanceMode['view']['template'])) { $View = $this->_getView(); $body = $View->render($MaintenanceMode['view']['template'], $MaintenanceMode['view']['layout']); } $event->data['response']->statusCode($statusCode); $event->data['response']->body($body); $event->stopPropagation(); return $event->data['response']; }
/** * Checks whether the response was cached and set the body accordingly. * * @param CakeEvent $event containing the request and response object * @return CakeResponse with cached content if found, null otherwise */ public function beforeDispatch(CakeEvent $event) { if (Configure::read('Cache.check') !== true) { return; } $path = $event->data['request']->here(); if ($path === '/') { $path = 'home'; } $prefix = Configure::read('Cache.viewPrefix'); if ($prefix) { $path = $prefix . '_' . $path; } $path = strtolower(Inflector::slug($path)); $filename = CACHE . 'views' . DS . $path . '.php'; if (!file_exists($filename)) { $filename = CACHE . 'views' . DS . $path . '_index.php'; } if (file_exists($filename)) { $controller = null; $view = new View($controller); $result = $view->renderCache($filename, microtime(true)); if ($result !== false) { $event->stopPropagation(); $event->data['response']->body($result); return $event->data['response']; } } }
/** * Checks if a requested asset exists and sends it to the browser * * @param CakeEvent $event containing the request and response object * @return mixed The resulting response. * @throws NotFoundException When asset not found */ public function beforeDispatch(CakeEvent $event) { $url = urldecode($event->data['request']->url); if (strpos($url, '..') !== false || strpos($url, '.') === false) { return; } // CUSTOMIZE DELETE 2014/07/02 ryuring // >>> /*if ($result = $this->_filterAsset($event)) { $event->stopPropagation(); return $result; }*/ // <<< $assetFile = $this->_getAssetFile($url); if ($assetFile === null || !file_exists($assetFile)) { return null; } $response = $event->data['response']; $event->stopPropagation(); $response->modified(filemtime($assetFile)); if ($response->checkNotModified($event->data['request'])) { return $response; } $pathSegments = explode('.', $url); $ext = array_pop($pathSegments); $this->_deliverAsset($response, $assetFile, $ext); return $response; }
/** * Checks if request is for a compiled asset, otherwise skip any operation * * @param CakeEvent $event containing the request and response object * @throws NotFoundException * @return CakeResponse if the client is requesting a recognized asset, null otherwise */ public function beforeDispatch(CakeEvent $event) { $url = $event->data['request']->url; $Config = $this->_getConfig(); $production = !Configure::read('debug'); if ($production && !$Config->general('alwaysEnableController')) { return; } $build = $this->_getBuild($url); if ($build === false) { return; } if (isset($event->data['request']->query['theme'])) { $Config->theme($event->data['request']->query['theme']); } // Dynamically defined build file. Disabled in production for // hopefully obvious reasons. if ($Config->files($build) === array()) { $files = array(); if (isset($event->data['request']->query['file'])) { $files = $event->data['request']->query['file']; } $Config->files($build, $files); } try { $Compiler = new AssetCompiler($Config); $mtime = $Compiler->getLastModified($build); $event->data['response']->modified($mtime); if ($event->data['response']->checkNotModified($event->data['request'])) { $event->stopPropagation(); return $event->data['response']; } $contents = $Compiler->generate($build); } catch (Exception $e) { throw new NotFoundException($e->getMessage()); } $event->data['response']->type($Config->getExt($build)); $event->data['response']->body($contents); $event->stopPropagation(); return $event->data['response']; }
public function beforeDispatch(CakeEvent $event) { if (!preg_match("/media\\/router\\/index\\//i", $event->data['request']->url)) { return; } $event->stopPropagation(); $requestData = $event->data['request']->params; list($type, $id, $size, $filename) = $requestData['pass']; App::uses('MediaPath', 'Media.Vendor'); $this->PHMedia = new MediaPath(); $fname = $this->PHMedia->getFileName($type, $id, $size, $filename); $aFName = $this->PHMedia->getFileInfo($filename); $response = $event->data['response']; if (!file_exists($fname)) { $this->_processMedia($requestData['pass'], $fname, $aFName); } $this->_deliverMedia($response, $fname, $aFName); return $response; }
/** * Handle OPTIONS requests * * If it's an options request, loop on the configured http verbs and add * an Access-Control-Allow-Methods header with the verbs the application * is configured to respond to. * * @param CakeEvent $event * @return CakeResponse|null */ public function beforeDispatch(CakeEvent $event) { $request = $event->data['request']; if (!$request->is('options')) { return; } $event->stopPropagation(); $url = $request->url; $verbs = Configure::read('Crud.HttpMethodFilter.verbs') ?: $this->defaultVerbs; $allowedMethods = array(); foreach ($verbs as $verb) { $_SERVER['REQUEST_METHOD'] = $verb; if (Router::parse('/' . $url)) { $allowedMethods[] = $verb; } } $_SERVER['REQUEST_METHOD'] = 'OPTIONS'; $response = $event->data['response']; $response->header('Access-Control-Allow-Methods', implode(', ', $allowedMethods)); return $response; }
/** * Before Dispatch - Set CORS access control headers * * @author Anthony Putignano <*****@*****.**> * @since 1.0 * @param CakeEvent $event * @return CakeResponse */ public function beforeDispatch(CakeEvent $event) { $request = $event->data['request']; $response = $event->data['response']; $origin = !empty($_SERVER['HTTP_ORIGIN']) ? $_SERVER['HTTP_ORIGIN'] : '*'; $response->header('Access-Control-Allow-Origin: ' . $origin); $response->header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS'); if (!empty($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS'])) { $response->header('Access-Control-Allow-Headers: ' . $_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']); } // Currently exit on all OPTIONS requests // Fix for swagger-ui CORS preflight requests if ($request->is('OPTIONS')) { if (defined('ENVIRONMENT') && ENVIRONMENT === 'development') { $response->header('Cache-Control: no-cache'); } else { $response->header('Cache-Control: max-age=3600'); } $event->stopPropagation(); return $response; } }
/** * Inspects the requests and if the url is the one set for processing the Sendgrid * webhook, it will convert the payload to one or several SendgridEvent objects and * trigger the configured callback for each one of them. * * @param CakeEvent $event * @return CakeResponse */ public function beforeDispatch(CakeEvent $event) { $config = Configure::read('Dispatcher.filters.GridHook'); $endpoint = '/webhook/sendgrid'; if (!empty($config['endpoint'])) { $endpoint = $config['endpoint']; } $request = $event->data['request']; $response = $event->data['response']; if ('/' . $request->url !== $endpoint || !$request->is('post')) { return; } $callable = $config['handler']; if (!is_callable($callable)) { throw new InvalidArgumentException('Not a valid handler for sendgrid webhooks'); } $data = ['events' => json_decode($request->input(), true), 'meta' => ['relay' => $request->query('relay')]]; $this->_trigger($callable, $data); $response->statusCode(200); $event->stopPropagation(); return $response; }
/** * Checks whether the response was cached and set the body accordingly. * * @param CakeEvent $event containing the request and response object * @return CakeResponse with cached content if found, null otherwise */ public function beforeDispatch(CakeEvent $event) { if (Configure::read('Cache.check') !== true) { return null; } // CUSTOMIZE 2014/08/11 ryuring // $this->request->here で、URLを取得する際、URL末尾の 「index」の有無に関わらず // 同一ファイルを参照すべきだが、別々のURLを出力してしまう為、 // 正規化された URLを取得するメソッドに変更 // >>> //$path = $event->data['request']->here(); // --- $path = $event->data['request']->normalizedHere(); // <<< if ($path === '/') { $path = 'index'; } $prefix = Configure::read('Cache.viewPrefix'); if ($prefix) { $path = $prefix . '_' . $path; } $path = strtolower(Inflector::slug($path)); $filename = CACHE . 'views' . DS . $path . '.php'; if (!file_exists($filename)) { $filename = CACHE . 'views' . DS . $path . '_index.php'; } if (file_exists($filename)) { $controller = null; $view = new View($controller); $view->response = $event->data['response']; $result = $view->renderCache($filename, microtime(true)); if ($result !== false) { $event->stopPropagation(); $event->data['response']->body($result); return $event->data['response']; } } }
/** * Auxiliary function to help in stopPropagation testing * * @param CakeEvent $event * @return void */ public function stopListener($event) { $event->stopPropagation(); }
public function beforeDispatch(CakeEvent $event) { $event->data['response']->statusCode(500); $event->stopPropagation(); return $event->data['response']; }
public function testStopPropagation() { $this->event->stopPropagation(false); $this->assertFalse($this->event->isStopped()); }
/** * Generates the path the image depending on the storage adapter * * @param CakeEvent $Event * @return void */ public function imagePath($Event) { if ($Event->data['image']['adapter'] == 'Local') { $Helper = $Event->subject(); extract($Event->data); $path = $this->_buildPath($image, true, $hash); $Event->data['path'] = $path; $Event->stopPropagation(); } }
/** * setFlash * * An API request doesn't need flash messages - so stop them being processed * * @param CakeEvent $event */ public function setFlash(CakeEvent $event) { $event->stopPropagation(); }
public function afterDelete(CakeEvent $event) { $event->subject->controller->set('success', $event->subject->success); $event->stopPropagation(); return $event->subject->controller->render(); }
/** * TestFilterDispatcher::beforeDispatch() * * @param mixed $event * @return CakeResponse|bool */ public function beforeDispatch(CakeEvent $event) { $event->stopPropagation(); $response = $event->data['request']; $response->addParams(array('settings' => $this->settings)); return null; }
/** * Builds an url to the given image for the amazon s3 adapter * * http(s)://<bucket>.s3.amazonaws.com/<object> * http(s)://s3.amazonaws.com/<bucket>/<object> * * @param CakeEvent $Event * @return void */ protected function _buildAmazonS3Path(CakeEvent $Event) { extract($Event->data); $path = $this->_buildPath($image, true, $hash); $image['path'] = '/' . $path; $config = StorageManager::config($Event->data['image']['adapter']); $bucket = $config['adapterOptions'][1]; if (!empty($config['cloudFrontUrl'])) { $cfDist = $config['cloudFrontUrl']; } else { $cfDist = null; } $http = 'http'; if (!empty($Event->data['options']['ssl']) && $Event->data['options']['ssl'] === true) { $http = 'https'; } $image['path'] = str_replace('\\', '/', $image['path']); $bucketPrefix = !empty($Event->data['options']['bucketPrefix']) && $Event->data['options']['bucketPrefix'] === true; $Event->data['path'] = $this->_buildCloudFrontDistributionUrl($http, $image['path'], $bucket, $bucketPrefix, $cfDist); $Event->stopPropagation(); }