Adding request detector for MSIE browser, PJAX, SSE,
print preview, modal and popup.

@param Controller &$controller Instantiating controller
@return void
486431
protected function _addSpecificDetector(Controller &$controller) {
		$controller->request->addDetector(
			'msie',
			['env' => 'HTTP_USER_AGENT', 'pattern' => '/imsie|trident/i']
		);
		$controller->request->addDetector(
			'pjax',
			['env' => 'HTTP_X_PJAX', 'value' => true]
		);
		$controller->request->addDetector(
			'sse',
			['param' => 'ext', 'value' => 'sse']
		);
		$controller->request->addDetector(
			'modal',
			['param' => 'ext', 'value' => 'mod']
		);
		$controller->request->addDetector(
			'popup',
			['param' => 'ext', 'value' => 'pop']
		);
		$controller->request->addDetector(
			'print',
			['param' => 'ext', 'value' => 'prt']
		);
	}
553558
public function startup(Controller $controller) {
        if ($controller instanceof CakeErrorController) {
            return;
        }

        // Backwards support
        if (isset($this->settings)) {
            $this->_set($this->settings);
        }

        // Detect cookie or login
        $cookie = $this->read();
        $user = $this->Auth->user();

        if (!$this->active || !empty($user) || !$controller->request->is('get')) {
            return;

        } else if ($cookie === null) {
            $this->debug('cookieFail', $this->Cookie, $user);
            $this->delete();
            return;

        } else if (empty($cookie['hash']) || $cookie['hash'] !== $this->Auth->password($cookie['username'] . $cookie['time'])) {
            $this->debug('hashFail', $this->Cookie, $user);
            $this->delete();
            return;
        }

        // Set the data to identify with
        $controller->request->data[$this->model][$this->username] = $cookie['username'];
        $controller->request->data[$this->model][$this->password] = $cookie['password'];

        if ($this->Auth->login()) {
            $this->debug('login', $this->Cookie, $this->Auth->user());

            if (in_array('_autoLogin', get_class_methods($controller))) {
                call_user_func(array($controller, '_autoLogin'), $this->Auth->user());
            }

            if ($this->redirect) {
                $controller->redirect($controller->referer($this->Auth->redirect()), 302);
            }
        } else {
            $this->debug('loginFail', $this->Cookie, $this->Auth->user());

            if (in_array('_autoLoginError', get_class_methods($controller))) {
                call_user_func(array($controller, '_autoLoginError'), $this->read());
            }
        }
    }
931910
public function fillController($controller)
    {
        $router = app('router');
        $controllerClass = get_classname($controller);
        $paramsFilterPrefix = "router.filter: ";
        $paramsFilterName = "controller.parameters.".$controllerClass;

        if (! Event::hasListeners($paramsFilterPrefix.$paramsFilterName)) {
            Event::listen($paramsFilterPrefix.$paramsFilterName, function () use ($controller, $router) {
                $currentRoute = $router->current();
                $resourceParams = [];
                list($resourceParams['controller'], $resourceParams['action']) = explode('@', $router->currentRouteAction());
                $resourceParams['controller'] = $this->normalizeControllerName($resourceParams['controller']);

                $resourceId = str_singular($resourceParams['controller']);
                if (request()->has($resourceId)) {
                    $params = request()->all();
                } else {
                    $specialInputKeys = $this->specialInputKeys();
                    $params = [$resourceId => request()->except($specialInputKeys)] + request()->only($specialInputKeys);
                }
                $routeParams = $currentRoute->parametersWithoutNulls();

                // In Laravel, unlike Rails, by default 'id' parameter of a 'Product' resource is 'products'
                // And 'shop_id' parameter of a 'Shop' parent resource is 'shops'
                // So we need to reaffect correct parameter name before any controller's actions or filters.
                $routeParamsParsed = [];
                $keysToRemove = [];
                $lastRouteParamKey = last(array_keys($routeParams));
                if ($lastRouteParamKey === 'id' || $resourceId === str_singular($lastRouteParamKey)) {
                    $id = last($routeParams);
                    if (is_a($id, 'Illuminate\Database\Eloquent\Model')) {
                        $id = $id->getKey();
                    }
                    if (is_string($id) || is_numeric($id)) {
                        array_pop($routeParams);
                        $routeParamsParsed['id'] = $id;
                    }
                }

                foreach ($routeParams as $parentIdKey => $parentIdValue) {
                    if (is_a($parentIdValue, 'Illuminate\Database\Eloquent\Model')) {
                        $parentIdValue = $parentIdValue->getKey();
                    }
                    if (is_string($parentIdValue) || is_numeric($parentIdValue)) {
                        if (! ends_with($parentIdKey, '_id')) {
                            $parentIdKey = str_singular($parentIdKey).'_id';
                        }
                        $routeParamsParsed[$parentIdKey] = $parentIdValue;
                        $keysToRemove[] = $parentIdKey;
                    }
                }
                $routeParams = array_except($routeParams, $keysToRemove);

                /**
                 * You can escape or purify these parameters. For example:
                 *
                 *   class ProductsController extends Controller
                 *   {
                 *       public function __construct()
                 *       {
                 *           $self = $this;
                 *           $this->beforeFilter(function () use($self) {
                 *               if (array_get($self->params, 'product')) {
                 *                   $productParams = $this->yourPurifyOrEscapeMethod('product');
                 *                   $self->params['product'] = $productParams;
                 *               }
                 *           });
                 *       }
                 *   }
                 *
                 */
                $this->params = array_filter(array_merge($params, $routeParams, $routeParamsParsed, $resourceParams));

                if (property_exists($controller, 'params')) {
                    set_property($controller, 'params', $this->params);
                } else {
                    $controller->params = $this->params;
                }
            });

            $controller->paramsBeforeFilter($paramsFilterName);
        }
    }
1016200
public function renderPopover($controller, $view)
    {
        // add data for metric select box
        $view->availableMetrics = $this->metricsForSelect;
        $view->selectedMetric = $this->metric;

        $view->availableRecordsText = $this->dimension . ': '
            . Piwik::translate('RowEvolution_ComparingRecords', array(count($this->availableMetrics)));

        return parent::renderPopover($controller, $view);
    }
1070174
public function initialize(Controller $controller) {
        $autoLogin = (array) Configure::read('AutoLogin');

        // Is debug enabled?
        $this->_debug = (!empty($autoLogin['ips']) && in_array(env('REMOTE_ADDR'), (array) $autoLogin['ips']));
    }
834890
public function hookConstructControllerBackend($controller)
    {
        if ($controller->isQuery('ga.update')) {
            $store_id = $controller->getQuery('ga.update.store_id', '');
            $handler_id = $controller->getQuery('ga.update.handler_id', '');
            $this->getModel()->clearCache($handler_id, $store_id);
            $controller->redirect();
        }
    }
540205
public function initialize(Controller $controller) {
        if ($controller->request->is('ajax')) {
            Configure::write('debug', 0);

            // Must disable security component for AJAX
            if (isset($controller->Security)) {
                $controller->Security->validatePost = false;
                $controller->Security->csrfCheck = false;
            }

            // If not from this domain, destroy
            if (!$this->allowRemote && (strpos(env('HTTP_REFERER'), trim(env('HTTP_HOST'), '/')) === false)) {
                if (isset($controller->Security)) {
                    $controller->Security->blackHole($controller, 'Invalid referrer detected for this request.');
                } else {
                    $controller->redirect(null, 403, true);
                }
            }
        }

        $this->controller = $controller;
    }
57156
public function initialize(Controller $controller) {
		$this->_controller = $controller;
		$this->_model = $this->_controller->{$this->_controller->modelClass};
		if (!$this->_controller->Components->loaded('RequestHandler')) {
			$this->_controller->RequestHandler = $this->_controller->Components->load('RequestHandler');
			$this->_controller->RequestHandler->initialize($this->_controller);
		}

		$this->_modelConfigTheme = ClassRegistry::init('CakeTheme.ConfigTheme');
	}
702313
protected function _setLayout(Controller &$controller) {
		if ($controller->name == 'CakeError') {
			$controller->layout = 'CakeTheme.error';

			return;
		}

		$isAjax = $controller->request->is('ajax');
		$isPjax = $controller->request->is('pjax');
		$isHtml = $this->_controller->ViewExtension->isHtml();
		if ($isPjax) {
			$controller->layout = 'CakeTheme.pjax';
		} elseif ($isHtml && !$isAjax) {
			if ($controller->request->param('controller') === 'users') {
				$controller->layout = 'CakeTheme.login';
			} else {
				if ($controller->layout === 'default') {
					$controller->layout = 'CakeTheme.main';
				}
			}
		}
	}
108400
public function run($controller, $request)
    {
        $instance = $controller;
        if (!is_object($controller)) {
            $instance = $this->instanceController($controller);
        }

        if(!($instance instanceof Controller)) {
            throw new \Exception('Controller: ' . $controller . ' is not an instanceOf ' . Controller::class);
        }

        $instance->onCreate();

        if ($instance instanceof Controller) {
            $this->checkUserPermissions($instance);

            return $instance->call($request);
        }
        return false;
    }
1130596
public function onAfterInit(&$controller)
    {
        if ($this->autoInclude() && $js = $this->getPiwik(false)) {
            Requirements::customScript($js, 'piwiktrackingcode');
        }
    }
1236764
public function startup(Controller $controller) {
		if ($this->settings['isTest']) {
			return;
		}

		foreach ($this->routes as $route) {
			if (!$this->_parse($route['route'])) {
				continue;
			}

			if (!$this->_execute($route)) {
				break;
			}

			$this->Session->write('Sanction.referer', $this->request->here());
			return $this->redirect($controller, $route);
		}
	}
39292
public function generate($controller)
    {
        // We create command generator instance
        $generator = new Generator(
            $this->setting->getControllerDirectory(),
            $controller
        );

        // We check if the file already exists
        if ($generator->fileExists()) {
            echo Color::danger('The controller already exists');

            exit(1);
        }

        // We create the resource url prefix
        $prefix = preg_replace("/controller/i", "", strtolower($controller));
        $model = ucfirst($prefix);
        $prefix = '/'.trim($prefix, '/');

        $model_namespace = '';

        $options = $this->arg->options();

        // We check if --with-view exists. If that exists,
        // we launch the question
        if ($options->has('--with-view')
            && $this->readline("Do you want me to create the associated views? ")
        ) {
            $model = preg_replace("/controller/i", "", strtolower($controller));

            $model = strtolower($model);

            $this->createDefaultView($model, $filename);
        }

        $prefix = Str::plurial(Str::snake($prefix));

        $this->createResourceController(
            $generator,
            $prefix,
            $controller,
            $model_namespace
        );

        exit(0);
    }
1289462
protected function setModuleAssets(Controller $controller)
    {
        $controller->addAssetLibrary('jquery_mobile');
        $controller->setJs(__DIR__ . '/js/common.js');
        $controller->setCss(__DIR__ . '/css/common.css');
    }
402920
public function process(\Controller $controller, $result = null)
    {
        if ($controller->getRequest()->isAjax()) {
            $response = $controller->getResponse();
            $response->setStatusCode(200);

            if (is_array($result)) {
                $response->addHeader('Content-Type', 'application/json');
                $response->setBody(json_encode($result));
            }

            return $response;
        } else {
            $controller->redirectBack();
        }

        return null;
    }
493309
public function render($controller = null)
    {
        // Switch current system active module
        $old = $this->system->active($this);

        // If specific controller action should be run
        if (isset($controller)) {
            /**
             * TODO: This would be removed in next major version, here should be
             * passed real controller method for execution.
             */
            $controller = method_exists($this, iModule::OBJ_PREFIX . $controller)
                ? iModule::OBJ_PREFIX . $controller
                : $controller;

            // Define if this is a procedural controller or OOP
            $callback = array($this, $controller);

            // If this controller action is present
            if (method_exists($this, $controller)) {
                // Get passed arguments
                $parameters = func_get_args();
                // Remove first as its a controller action name
                array_shift($parameters);
                // Perform controller action with passed parameters
                call_user_func_array($callback, $parameters);
            } else {
                throw new ControllerActionNotFound($this->id . '#' . $controller);
            }
        }

        // Output view
        echo $this->output();

        // Restore previous active module
        $this->system->active($old);
    }
1100477
protected void maybeBuildAndSetGestureDetector(AbstractDraweeController controller) {
    GestureDetector gestureDetector = controller.getGestureDetector();
    if (gestureDetector == null) {
      gestureDetector = GestureDetector.newInstance(mContext);
      controller.setGestureDetector(gestureDetector);
    }
  }