/** * Get file stream response * * * @access public * @param string $file * @return Stream */ public function getFileResponse($file) { $response = new Response(); if (!empty($file) && file_exists($file)) { $response = new Stream(); $response->setStream(fopen($file, 'r')); $response->setStatusCode(200); $response->setStreamName(basename($file)); $headers = new Headers(); $headers->addHeaders(array('Content-Disposition' => 'attachment; filename="' . basename($file) . '"', 'Content-Type' => 'application/octet-stream', 'Content-Length' => filesize($file), 'Expires' => '@0', 'Cache-Control' => 'must-revalidate', 'Pragma' => 'public')); $response->setHeaders($headers); } return $response; }
public function tfaRenderQrCodeAction() { $filePath = tempnam('data/tmp/', '2fa-qr-'); $renderer = new \BaconQrCode\Renderer\Image\Png(); $renderer->setForegroundColor(new \BaconQrCode\Renderer\Color\Rgb(170, 45, 76)); $renderer->setHeight(256); $renderer->setWidth(256); $writer = new \BaconQrCode\Writer($renderer); $writer->writeFile('123456', $filePath); $response = new Stream(); $response->setCleanup(true); $response->setStream(fopen($filePath, 'rb')); $response->setStreamName($filePath); $headers = $response->getHeaders(); $headers->addHeaderLine('Content-Length', filesize($filePath)); $headers->addHeaderLine('Content-Type', 'image/png'); return $response; }
public function stream($file, $type) { switch ($type) { case "css": $mimeType = 'text/css'; break; case 'js': $mimeType = 'text/javascript'; break; default: $mimeType = mime_content_type($file); } $response = new Stream(); $response->setStream(fopen($file, 'r')); $response->setStatusCode(200); $response->setStreamName(basename($file)); $headers = new Headers(); $headers->addHeaders(array('Content-Type' => $mimeType, 'Content-Length' => filesize($file))); $response->setHeaders($headers); return $response; }
public function renderQrCodeAction() { /** @var AccountInterface $account */ $account = $this->zourceAccount(); $oneTimePassword = new TOTP($account->getContact()->getFullName(), $this->tfaService->getSecret()); $oneTimePassword->setIssuer('Zource'); $oneTimePassword->setIssuerIncludedAsParameter(true); $oneTimePassword->setParameter('image', $this->url()->fromRoute('settings/security/tfa-image', [], ['force_canonical' => true])); $filePath = tempnam('data/tmp/', '2fa-qr-'); $renderer = new Png(); $renderer->setHeight(256); $renderer->setWidth(256); $writer = new Writer($renderer); $writer->writeFile($oneTimePassword->getProvisioningUri(true), $filePath); $response = new Stream(); $response->setCleanup(true); $response->setStream(fopen($filePath, 'rb')); $response->setStreamName($filePath); $headers = $response->getHeaders(); $headers->addHeaderLine('Content-Length', filesize($filePath)); $headers->addHeaderLine('Content-Type', 'image/png'); return $response; }
public function downloadFile($filePath, $filename = FALSE) { $name = $filename ?: basename($filePath); $response = new Stream(); $response->setStream(fopen($filePath, 'r')); $response->setStatusCode(200); $response->setStreamName(basename($filePath)); $headers = new Headers(); $headers->addHeaders(array('Content-Disposition' => "attachment; filename='{$name}'", 'Content-Type' => 'application/octet-stream', 'Content-Length' => filesize($filePath), 'Expires' => '@0', 'Cache-Control' => 'must-revalidate', 'Pragma' => 'public')); $response->setHeaders($headers); return $response; }
/** * @param $file * @return Stream */ function forceDownloadAction($file) { // $file = '/path/to/my/file.txt'; $file = $_SERVER['DOCUMENT_ROOT'] . '/' . $file; $response = new Stream(); $response->setStream(fopen($file, 'r')); $response->setStatusCode(200); $response->setStreamName(basename($file)); $headers = new Headers(); $headers->addHeaders(array('Content-Disposition' => 'attachment; filename="' . basename($file) . '"', 'Content-Type' => 'application/octet-stream', 'Content-Length' => filesize($file))); $response->setHeaders($headers); return $response; }
public function pdfAction() { $number = (int) $this->params()->fromRoute('id', 0); $file = 'data/invoice/invoice-' . sprintf('%06d', $number) . '.pdf'; $response = new Stream(); $response->setStream(fopen($file, 'r')); $response->setStreamName(basename($file)); $headers = new Headers(); $headers->addHeaders(array('Content-Disposition' => 'attachment; filename="' . basename($file) . '"', 'Content-Type' => 'application/pdf', 'Content-Length' => filesize($file))); $response->setHeaders($headers); return $response; }
public function viewFileAttachmentAction() { $file_no = (int) $this->params()->fromRoute('id', 0); if (!$file_no) { return $this->redirect()->toRoute('app', array('controller' => 'failed', 'action' => 'forbidden')); } $currentUser = $this->auth()->get('user_no'); $dbAdapter = $this->getDb(); $sql = "SELECT \n t_decision_file.file_no, \n t_decision_file.decision_no, \n t_decision_file.file_name, \n t_decision_file.content_type, \n OCTET_LENGTH( t_decision_file.binary_data) as file_size, \n t_decision_file.binary_data,\n t_decision.create_user\n FROM t_decision_file \n LEFT JOIN t_decision \n ON t_decision_file.decision_no = t_decision.decision_no\n WHERE t_decision_file.file_no = {$file_no}"; $result = $dbAdapter->query($sql)->execute(); $file = $result->current(); if (!$file) { return $this->redirect()->toRoute('app', array('controller' => 'failed', 'action' => 'not-found')); } $continue = true; // you must be the create user of the request if ($file['create_user'] != $currentUser) { $continue = false; } // if you are not the create_user of the request you must be 1 of approver if (!$continue) { // check file restriction $result2 = $dbAdapter->query("SELECT\n t_decision_approval.user_no\n FROM t_decision_approval\n WHERE t_decision_approval.decision_no = {$file['decision_no']}")->execute(); $resultSet = new ResultSet(); $resultSet->initialize($result2); $resultAr = $resultSet->toArray(); if (is_array($resultAr)) { foreach ($resultAr as $rec) { if ((int) $rec['user_no'] === (int) $currentUser) { $continue = true; break; } } } } // Otherwise you will not be able to access this attachment if (!$continue) { $this->flashMessenger()->addMessage("You don't have enough access level to view this document"); return $this->redirect()->toRoute('app', array('controller' => 'failed', 'action' => 'forbidden')); } $file_name = $file["file_name"]; $file_size = $file["file_size"]; $file_mime_type = $file["content_type"]; $file_content = $file["binary_data"]; $new_filename = "temp_" . $file_name; $filePath = APP_UPLOAD_DIR . $new_filename; \file_put_contents($filePath, $file_content); $response = new Stream(); $response->setStream(fopen($filePath, 'r')); $response->setStatusCode(200); $response->setStreamName(basename($filePath)); $headers = new Headers(); $headers->addHeaders(array('Content-Disposition' => 'inline; filename="' . $new_filename . '"', 'Content-Type' => $file_mime_type . ';charset=UTF-8', 'Content-Length' => $file_size)); $response->setHeaders($headers); /**$view = new ViewModel(array( 'response' => $response, 'headers' => $headers )); $view->setTemplate('/common/view-attachment.phtml'); $view->setTerminal(true);**/ // return $view; return $response; }
/** * @param MvcEvent $event */ public function onDispatchError(MvcEvent $event) { if (Application::ERROR_ROUTER_NO_MATCH != $event->getError()) { // ignore other than 'no route' errors return; } // get URI stripped of a base URL $request = $event->getRequest(); $uri = str_replace($request->getBaseUrl(), '', $request->getRequestUri()); // try get image ID from URI $id = $this->manager->matchUri($uri); if (!$id) { // abort if URI does not match return; } // try get image from repository $image = $this->repository->find($id); if (!$image) { // abort if image does not exist return; } // store image $this->manager->store($image); // return image in response as a stream $headers = new Headers(); $headers->addHeaders(['Content-Type' => $image->getType(), 'Content-Length' => $image->getLength()]); $response = new Stream(); $response->setStatusCode(Response::STATUS_CODE_200); $response->setStream($image->getResource()); $response->setStreamName($image->getName()); $response->setHeaders($headers); $event->setResponse($response); }