public function track() { if (empty($_COOKIE[self::HTTP_REFERRER_COOKIE]) && ($referrer = getenv('HTTP_REFERER'))) { $uri = parse_url($referrer); $domain = preg_quote($this->config->getPublicVars('domain')); $value = !preg_match("/{$domain}/", $uri['host']) ? sprintf('%s||%s', $this->request->getPath(), $referrer) : '/'; $this->response->setCookie(self::HTTP_REFERRER_COOKIE, $value, '+1 year'); } if (empty($_COOKIE[self::HTTP_CAMPAIGN_COOKIE]) && ($cmp = $this->request->getParameter('_cmp'))) { $this->response->setCookie(self::HTTP_CAMPAIGN_COOKIE, $cmp, '+1 year'); } }
public function index($minified) { /** @var MMinifiedDatum $minify */ if ($minify = $minified[0] ?? null) { $this->response->setStatusCode(200); $this->response->setHeader('Content-Type', preg_match('/\\.js$/', $minify->name) ? 'application/javascript' : 'text/css'); $this->response->setContent($minify->content); } else { $this->response->setStatusCode(404); } return $this->response; }
public function index(int $ar_list_id) { $user_ids = $this->listManager->getTargetUserIds($ar_list_id); $out = fopen('php://output', 'w'); $this->response->asFile(sprintf('ar_download_list_%d.csv', $ar_list_id), 'text/csv'); /** @var User $user */ foreach (User::find($user_ids ?? [0]) as $user) { $attrs = $user->attributesToArray(); unset($attrs['password']); unset($attrs['ident']); if (empty($header)) { $header = true; fputcsv($out, array_keys($attrs)); } fputcsv($out, $attrs); } fclose($out); }
public function send(ResponseEvent $event) { /** @var HttpResponseEx $response */ $response = $event->getResponse(); if ($response->getStatusCode() === 401) { $user_id = $this->session->getLoggedInUserId(); $reason = $user_id > 0 ? $this->lang->getText('Your account does not have the required authorization to view this page') : $this->lang->getText('You must be logged in to view this page'); if ($event->isAjaxRequest()) { $response->setContent($reason); } else { $url = $this->response->getLoginRedirect($reason, true); $response->redirect($url, 302); } } if (!headers_sent()) { foreach ($response->getHeaders() as $header) { header($header, false); } } echo $response->getContent() ?? sprintf("Error code: %d", $response->getStatusCode()); }
public function run() { try { $this->boot(); $event = new RequestEvent($this->request); $this->dispatcher->fire(RequestEvent::REQUEST_HANDLE, $event); $this->response = $event->getResponse(); } catch (AuthError $e) { $this->response->setStatusCode(401); } catch (ResourceNotFoundException $e) { $this->response->setStatusCode(404); } catch (\Throwable $e) { http_response_code(500); $this->response->setStatusCode(500); if ($e instanceof PrintableError) { $this->response->setContent($e->getMessage()); } else { if (preg_match("/Duplicate entry '(.*?)' for key '(.*?)'/", $e->getMessage(), $matches)) { $errorStr = sprintf("DUPLICATE: %s ('%s' is already in use).", ucfirst($matches[2]), $matches[1]); } else { if ($this->debug->enabled()) { throw $e; } else { $event = new ExceptionEvent($e); $this->dispatcher->fire(ExceptionEvent::EXCEPTION_UNHANDLED, $event); } if (preg_match("/Unknown column 'user_id'.*from (.*?) /", $e->getMessage(), $matches)) { $errorStr = sprintf("Permission::SAME_USER requires a `user_id` column (which is missing in table {$matches['1']})"); } else { $errorStr = 'Internal server error'; } } $this->response->setContent($errorStr); } } if ($this->response->getStatusCode() === 200) { $this->dispatcher->fire(ResponseEvent::RESPONSE_RENDER, new ResponseEvent($this->response)); } else { $this->dispatcher->fire(ResponseEvent::RESPONSE_ERROR, new ResponseEvent($this->response)); } }
public function destroySession() { $this->userInfo->clearCache($this->getLoggedInUserId()); $this->data = new stdClass(); $this->response->setCookie(self::COOKIE_NAME, null); }
public function execute(ControllerEvent $event) { try { $params = $event->getParams(true) ?? []; $ajaxReq = $params['_contentType'] === 'ajax'; if ($params['_method'] === 'GET' && ($parents = $params['_parents'] ?? null)) { $alias = $ajaxReq ? $params['alias'] : null; if ($models = $this->modelLoader->loadModels($parents, $alias)) { $event->setParam('_models', $models); foreach ($models as $key => $value) { $event->setParam($key, $value); } } foreach ($parents as $key => $value) { $event->setParam("_{$key}", $models[$key] ?? []); } } $args = array_merge([':event' => $event], $event->getParams() ?? []); $action = $this->getController($event->getController()); $final = true; $response = $output = $this->injector->execute($action, $args); if ($response instanceof Redirection) { $this->dispatcher->fire(RedirectEvent::REDIRECT, new RedirectEvent($response)); //chance to do something with redirection $response->redirect(); //exists } else { if ($response instanceof HttpResponseEx) { $event->setResponse($this->response); } else { if ($response instanceof View) { if ($ajaxReq && !empty($alias) && !empty($models)) { $output = '{}'; /** @var CollectionEx $model */ foreach ($models as $tlp => $model) { if ($array = $model->toArray()) { if ($child = $tlp === $alias ? $array : $this->findChildByAlias($array, $alias)) { $output = json_encode($child, JSON_PRETTY_PRINT); } } } } else { /** @var View $view */ $view = $response; $final = $response->isFinal(); $this->viewParser->setHelpers($view->getHelpers()); $this->viewParser->setLayout($view->getLayout()); $this->viewParser->setAdditionalLayoutFiles($view->getAdditionalLayoutFiles()); $this->viewParser->setVars(array_merge($event->getParams(true) ?? [], $view->getVars() ?? [])); if ($content = $view->getContent()) { if (!empty($view->getViewFile())) { trigger_error('You should only specify either content or view file'); } $this->viewParser->setContent($content); } elseif ($templatePath = $view->getViewFile()) { $this->viewParser->loadViewFile($templatePath, $view->isPathLayouts()); } elseif (empty($view->getViewFile())) { if (is_string($event->getController())) { @(list($class) = explode('@', $event->getController(), 2)); $this->viewParser->loadViewFile($class, $view->isPathLayouts()); } else { throw new ControllerError("View must be explicitly set (when Controller is not a string)"); } } $output = $this->viewParser->render(); } } $this->response->setStatusCode(200); $this->response->setContent(is_string($output) ? $output : ''); $this->response->setFinal($final); $event->setResponse($this->response); } } } catch (InjectionException $e) { throw new ControllerError(sprintf("Unable to run controller: %s [%s]", $e->getMessage(), var_export($event->getController(), true))); } }