/** * SetUp method * * @return void */ public function setUp() { parent::setUp(); Request::addDetector('mobile', function ($request) { $detector = new MobileDetect(); return $detector->isMobile(); }); Request::addDetector('tablet', function ($request) { $detector = new MobileDetect(); return $detector->isTablet(); }); $this->event = new Event('Controller.beforeFilter'); $this->Controller = new MobileComponentTestController(new Request()); $this->Controller->request->session()->delete('User'); Configure::delete('User'); }
<?php use Cake\Event\EventManager; use Cake\Event\Event; use Cake\Network\Request; EventManager::instance()->on('Controller.initialize', function (Event $event) { $controller = $event->subject(); if ($controller->components()->has('RequestHandler')) { $controller->RequestHandler->config('viewClassMap.xlsx', 'CakeExcel.Excel'); } }); Request::addDetector('xlsx', ['accept' => ['application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'], 'param' => '_ext', 'value' => 'xlsx']);
Security::salt(Configure::consume('Security.salt')); /** * The default crypto extension in 3.0 is OpenSSL. * If you are migrating from 2.x uncomment this code to * use a more compatible Mcrypt based implementation */ // Security::engine(new \Cake\Utility\Crypto\Mcrypt()); /** * Setup detectors for mobile and tablet. */ Request::addDetector('mobile', function ($request) { $detector = new \Detection\MobileDetect(); return $detector->isMobile(); }); Request::addDetector('tablet', function ($request) { $detector = new \Detection\MobileDetect(); return $detector->isTablet(); }); /** * Custom Inflector rules, can be set to correctly pluralize or singularize * table, model, controller names or whatever other string is passed to the * inflection functions. * * Inflector::rules('plural', ['/^(inflect)or$/i' => '\1ables']); * Inflector::rules('irregular', ['red' => 'redlings']); * Inflector::rules('uninflected', ['dontinflectme']); * Inflector::rules('transliteration', ['/å/' => 'aa']); */ /** * Plugins need to be loaded manually, you can either load them one by one or all of them in a single call * Uncomment one of the lines below, as you need. make sure you read the documentation on Plugin to use more * advanced ways of loading plugins
/** * Test adding detectors and having them work. * * @return void */ public function testAddDetector() { $request = new Request(); Request::addDetector('closure', function ($request) { return true; }); $this->assertTrue($request->is('closure')); Request::addDetector('get', function ($request) { return $request->env('REQUEST_METHOD') === 'GET'; }); $request->env('REQUEST_METHOD', 'GET'); $this->assertTrue($request->is('get')); Request::addDetector('compare', ['env' => 'TEST_VAR', 'value' => 'something']); $request->env('TEST_VAR', 'something'); $this->assertTrue($request->is('compare'), 'Value match failed.'); $request->env('TEST_VAR', 'wrong'); $this->assertFalse($request->is('compare'), 'Value mis-match failed.'); Request::addDetector('compareCamelCase', ['env' => 'TEST_VAR', 'value' => 'foo']); $request->env('TEST_VAR', 'foo'); $this->assertTrue($request->is('compareCamelCase'), 'Value match failed.'); $this->assertTrue($request->is('comparecamelcase'), 'detectors should be case insensitive'); $this->assertTrue($request->is('COMPARECAMELCASE'), 'detectors should be case insensitive'); $request->env('TEST_VAR', 'not foo'); $this->assertFalse($request->is('compareCamelCase'), 'Value match failed.'); $this->assertFalse($request->is('comparecamelcase'), 'detectors should be case insensitive'); $this->assertFalse($request->is('COMPARECAMELCASE'), 'detectors should be case insensitive'); Request::addDetector('banana', ['env' => 'TEST_VAR', 'pattern' => '/^ban.*$/']); $request->env('TEST_VAR', 'banana'); $this->assertTrue($request->isBanana()); $request->env('TEST_VAR', 'wrong value'); $this->assertFalse($request->isBanana()); Request::addDetector('mobile', ['env' => 'HTTP_USER_AGENT', 'options' => ['Imagination']]); $request->env('HTTP_USER_AGENT', 'Imagination land'); $this->assertTrue($request->isMobile()); Request::addDetector('index', ['param' => 'action', 'value' => 'index']); $request->params['action'] = 'index'; $request->clearDetectorCache(); $this->assertTrue($request->isIndex()); $request->params['action'] = 'add'; $request->clearDetectorCache(); $this->assertFalse($request->isIndex()); Request::addDetector('callme', [$this, 'detectCallback']); $request->return = true; $request->clearDetectorCache(); $this->assertTrue($request->isCallMe()); Request::addDetector('extension', ['param' => '_ext', 'options' => ['pdf', 'png', 'txt']]); $request->params['_ext'] = 'pdf'; $request->clearDetectorCache(); $this->assertTrue($request->is('extension')); $request->params['_ext'] = 'exe'; $request->clearDetectorCache(); $this->assertFalse($request->isExtension()); }
*/ Request::addDetector('controller', function ($request, $controller) { return in_array($request->param('controller'), (array) $controller); }); /** * Adds `is('prefix')` detector. * * It checks if the specified prefix is the current prefix. * The prefix name can be passed as string or array. * * Example: * <code> * $this->request->isPrefix(['admin', 'manager']); * </code> */ Request::addDetector('prefix', function ($request, $prefix) { return in_array($request->param('prefix'), (array) $prefix); }); /** * Adds `is('url')` detector. * * It checks if the specified url is the current url. * * Example: * <code> * $this->request->isUrl(['_name' => 'posts']); * </code> */ Request::addDetector('url', function ($request, $url) { return rtrim(Router::url($url), '/') === rtrim($request->here, '/'); });
if (preg_match(sprintf('/^(%s)$/', implode('|', $banned)), $request->clientIp())) { return true; } //In any other case, saves the result in the session $request->session()->write('allowed_ip', true); return false; }); /** * Adds `is('offline')` detector. * * It checks if the site is offline. * * Example: * <code> * $this->request->isOffline(); * </code> */ Request::addDetector('offline', function ($request) { if (!config('default.offline')) { return false; } //Always online for admin requests if ($request->is('admin')) { return false; } //Always online for some actions if ($request->is('action', ['offline', 'login', 'logout'])) { return false; } return true; });
<?php use Cake\Core\Plugin; use Cake\I18n\I18n; use Spider\Lib\SpiderNav; use Cake\Network\Request; use Spider\Lib\Hook; Hook::component('*', 'Spider.Spider'); Hook::helper('*', 'Spider.Spider'); Hook::configFile('*', 'events'); Request::addDetector('chrome', ['env' => 'HTTP_USER_AGENT', 'pattern' => '/Chrome/i']); SpiderNav::add('sidebar', 'Dashboard', ['title' => __d('spider', 'Dashboard'), 'url' => SpiderNav::getDashboardUrl(), 'icon' => 'fa fa-home', 'weight' => -10]); I18n::locale('fa_IR'); Plugin::load('BootstrapUI'); require_once 'spider_bootstrap.php';
return !empty($request->params['plugin']) && strtolower($request->params['plugin']) === 'system' && !empty($request->params['controller']) && strtolower($request->params['controller']) === 'dashboard' && !empty($request->params['action']) && strtolower($request->params['action']) === 'index'; }); /** * Checks if current URL is language prefixed. * * $request->isLocalized(); */ Request::addDetector('localized', function ($request) { $locales = array_keys(quickapps('languages')); $localesPattern = '(' . implode('|', array_map('preg_quote', $locales)) . ')'; $url = str_starts_with($request->url, '/') ? str_replace_once('/', '', $request->url) : $request->url; return preg_match("/^{$localesPattern}\\//", $url); }); /** * Checks if visitor user is logged in. * * $request->isUserLoggedIn(); */ Request::addDetector('userLoggedIn', function ($request) { $sessionExists = $request->session()->check('Auth.User.id'); $sessionContent = $request->session()->read('Auth.User.id'); return $sessionExists && !empty($sessionContent); }); /** * Checks if visitor user is logged in and has administrator privileges. * * $request->isUserAdmin(); */ Request::addDetector('userAdmin', function ($request) { return in_array(ROLE_ID_ADMINISTRATOR, array_values(user()->role_ids)); });
* Date: 5/12/2015 * Time: 12:59 PM */ namespace WebService\Routing\Filter; use Cake\Event\Event; use Cake\Network\Request; use Cake\Routing\DispatcherFilter; // Add a callback detector. For the service of ours. Request::addDetector('service', function ($request) { if (!empty($_GET['service']) && $_GET['service'] == true) { return true; } if ($request->is('ajax') || $request->is('json')) { return true; } if (!empty($request->params['_ext'])) { if (in_array($request->params['_ext'], ['json', 'api', 'service', 'rest'])) { return true; } } return false; }); class ServiceFilter extends DispatcherFilter { public function beforeDispatch(Event $event) { $request = $event->data['request']; $response = $event->data['response']; if ($request->is('service')) { if ($request->is('options')) { header('Content-Type: application/json');
<?php use Cake\Event\EventManager; use Cake\Event\Event; use Cake\Network\Request; EventManager::instance()->on('Controller.initialize', function (Event $event) { $controller = $event->subject(); if ($controller->components()->has('RequestHandler')) { $controller->RequestHandler->config('viewClassMap.csv', 'CsvView.Csv'); } }); Request::addDetector('csv', ['accept' => ['text/csv'], 'param' => '_ext', 'value' => 'csv']);
<?php use Cake\Cache\Cache; use Cake\Core\Configure; use Cake\Core\Configure\Engine\PhpConfig; use Cake\Event\EventManager; use Cake\Network\Request; use Cake\Routing\Router; use Lil\Event\LilEventListener; Router::plugin('Lil', function ($routes) { $routes->connect('/:controller/:action/*'); }); Configure::load('Lil.config', 'default', true); Cache::config('Lil', ['className' => 'Cake\\Cache\\Engine\\FileEngine', 'duration' => '+1 week', 'probability' => 100, 'path' => CACHE . 'lil_']); Request::addDetector('lilPopup', function ($request) { return $request->query('lil_submit') == 'dialog'; }); $LilEventListener = new LilEventListener(); EventManager::instance()->on($LilEventListener);