public function __invoke($redirect, $redirectToUrl = false) { $controller = $this->getController(); $request = $controller->getRequest(); $container = new Container('prg_post1'); if ($request->isPost()) { $container->setExpirationHops(1, 'post'); $container->post = $request->getPost()->toArray(); if (method_exists($controller, 'getPluginManager')) { // get the redirect plugin from the plugin manager $redirector = $controller->getPluginManager()->get('Redirect'); } else { /* * if the user wants to redirect to a route, the redirector has to come * from the plugin manager -- otherwise no router will be injected */ if ($redirectToUrl === false) { throw new RuntimeException('Could not redirect to a route without a router'); } $redirector = new Redirect(); } if ($redirectToUrl === false) { return $redirector->toRoute($redirect); } return $redirector->toUrl($redirect); } else { if ($container->post !== null) { $post = $container->post; unset($container->post); return $post; } return false; } }
public function testIterationHonorsExpirationHops() { $this->container->foo = 'bar'; $this->container->bar = 'baz'; $this->container->baz = 'bat'; $this->container->setExpirationHops(1, array('foo', 'baz')); $storage = $this->manager->getStorage(); $ts = $storage->getRequestAccessTime(); // First hop $storage->setMetadata('_REQUEST_ACCESS_TIME', $ts + 60); $expected = array('foo' => 'bar', 'bar' => 'baz', 'baz' => 'bat'); $test = array(); foreach ($this->container as $key => $value) { $test[$key] = $value; } $this->assertSame($expected, $test); // Second hop $storage->setMetadata('_REQUEST_ACCESS_TIME', $ts + 120); $expected = array('bar' => 'baz'); $test = array(); foreach ($this->container as $key => $value) { $test[$key] = $value; } $this->assertSame($expected, $test); }
/** * Example of AJAX File Upload with Session Progress and partial validation. * * @return array|ViewModel */ public function sessionPartialAction() { $form = new Form\SingleUpload('file-form'); $inputFilter = $form->getInputFilter(); $container = new Container('partialExample'); $tempFile = $container->partialTempFile; if ($this->getRequest()->isPost()) { // POST Request: Process form $postData = array_merge_recursive($this->getRequest()->getPost()->toArray(), $this->getRequest()->getFiles()->toArray()); // Disable required file input if we already have an upload if (isset($tempFile)) { $inputFilter->get('file')->setRequired(false); } $form->setData($postData); if ($form->isValid()) { // If we did not get a new file upload this time around, use the temp file $data = $form->getData(); if (empty($data['file']) || isset($data['file']['error']) && $data['file']['error'] !== UPLOAD_ERR_OK) { $data['file'] = $tempFile; } // // ...Save the form... // if (!empty($postData['isAjax'])) { // Send back success information via JSON $this->sessionContainer->formData = $data; return new JsonModel(array('status' => true, 'redirect' => $this->url()->fromRoute('fileupload/success'), 'formData' => $data)); } else { // Non-JS form submit, redirect to success page return $this->redirectToSuccessPage($data); } } else { // Extend the session $container->setExpirationHops(1, 'partialTempFile'); // Form was not valid, but the file input might be... // Save file to a temporary file if valid. $data = $form->getData(); $fileErrors = $form->get('file')->getMessages(); if (empty($fileErrors) && isset($data['file']['error']) && $data['file']['error'] === UPLOAD_ERR_OK) { // NOTE: $data['file'] contains the filtered file path. // 'FileRenameUpload' Filter has been run, and moved the file. $container->partialTempFile = $tempFile = $data['file']; } if (!empty($postData['isAjax'])) { // Send back failure information via JSON return new JsonModel(array('status' => false, 'formErrors' => $form->getMessages(), 'formData' => $data, 'tempFile' => $tempFile)); } } } else { // GET Request: Clear previous temp file from session unset($container->partialTempFile); $tempFile = null; } return array('title' => 'Session Partial Progress Upload', 'form' => $form, 'tempFiles' => isset($tempFile) ? array($tempFile) : null); }
/** * Get session object * * @throws Exception\InvalidArgumentException * @return Container */ public function getSession() { if (!isset($this->session) || null === $this->session) { $id = $this->getId(); if (!class_exists($this->sessionClass)) { throw new Exception\InvalidArgumentException("Session class {$this->sessionClass} not found"); } $this->session = new $this->sessionClass('Zend_Form_Captcha_' . $id); $this->session->setExpirationHops(1, null); $this->session->setExpirationSeconds($this->getTimeout()); } return $this->session; }
/** * Get session object * * @return \Zend\Session\Container */ public function getSession() { if (!isset($this->_session) || null === $this->_session) { $id = $this->getId(); if (!class_exists($this->_sessionClass)) { \Zend\Loader::loadClass($this->_sessionClass); } $this->_session = new $this->_sessionClass('Zend_Form_Captcha_' . $id); $this->_session->setExpirationHops(1, null, true); $this->_session->setExpirationSeconds($this->getTimeout()); } return $this->_session; }
/** * This method will iterate over an array and show its contents as separated strings. * The method will accept an array with unlimited depth. * * <code> * $myArray = [ * 0 => 'A', * 1 => ['subA','subB', * [0 => 'subsubA', 1 => 'subsubB', * 2 => [0 => 'subsubsubA', 1 => 'subsubsubB'] * ] * ], * 2 => 'B', * 3 => ['subA','subB','subC'], * 4 => 'C' * ]; * * $myArray = "Another way is to pass only a string"; * * $this->setLayoutMessages($myArray, "default"); * </code> * * @param array|string|RecursiveArrayIterator $message * @param string $namespace determinate the message layout and color. * * @return Container */ public function __invoke($message = [], $namespace = 'default') { if (!in_array($namespace, ['success', 'error', 'warning', 'info', 'default'])) { $namespace = 'default'; } $iterator = new RecursiveArrayIterator((array) $message); $trans = new Container('translations'); $arr = []; while ($iterator->valid()) { if ($iterator->hasChildren()) { $this->__invoke($iterator->getChildren(), $namespace); } else { if (!isset($trans->flashMessages) || !$trans->flashMessages instanceof SplQueue) { $trans->flashMessages = new SplQueue(); } $arr[$namespace] = $iterator->current(); $trans->flashMessages->push($arr); } $iterator->next(); } $trans->setExpirationHops(1, ['flashMessages']); return $trans; }
/** * Example of a single file upload when form is partially valid. * * @return array|ViewModel */ public function partialAction() { $form = new Form\SingleUpload('file-form'); $inputFilter = $form->getInputFilter(); $container = new Container('partialExample'); $tempFile = $container->partialTempFile; if ($this->getRequest()->isPost()) { // POST Request: Process form $data = array_merge($this->getRequest()->getPost()->toArray(), $this->getRequest()->getFiles()->toArray()); // Disable required file input if we already have an upload if (isset($tempFile)) { $inputFilter->get('file')->setRequired(false); } $form->setData($data); if ($form->isValid()) { // If we did not get a new file upload this time around, use the temp file $data = $form->getData(); if (empty($data['file'])) { $data['file'] = $tempFile['tmp_name']; } // // ...Save the form... // return $this->redirectToSuccessPage($data); } else { // Extend the session $container->setExpirationHops(1, 'partialTempFile'); // Form was not valid, but the file input might be... // Save file to a temporary file if valid. $data = $form->getData(); if (!empty($data['file'])) { // NOTE: $data['file'] contains the filtered file path $tempFile = $form->get('file')->getValue(); // Get the raw file upload array value $tempFilePath = './data/tmpuploads/partial' . uniqid('_'); move_uploaded_file($data['file'], $tempFilePath); $tempFile['tmp_name'] = $tempFilePath; $container->partialTempFile = $tempFile; } } } else { // GET Request: Clear previous temp file from session unset($container->partialTempFile); $tempFile = null; } $view = new ViewModel(array('title' => 'Partial Validation Examples', 'form' => $form, 'tempFiles' => isset($tempFile) ? array($tempFile) : null)); $view->setTemplate('zf2-file-upload-examples/examples/single'); return $view; }
/** * Example of a single file upload when form is partially valid. * * @return array|ViewModel */ public function partialAction() { $form = new Form\SingleUpload('file-form'); $inputFilter = $form->getInputFilter(); $container = new Container('partialExample'); $tempFile = $container->partialTempFile; if ($this->getRequest()->isPost()) { // POST Request: Process form $data = array_merge_recursive($this->getRequest()->getPost()->toArray(), $this->getRequest()->getFiles()->toArray()); // Disable required file input if we already have an upload if (isset($tempFile)) { $inputFilter->get('file')->setRequired(false); } $form->setData($data); if ($form->isValid()) { // If we did not get a new file upload this time around, use the temp file $data = $form->getData(); if (isset($data['file']['error']) && $data['file']['error'] !== UPLOAD_ERR_OK) { $data['file'] = $tempFile; } // // ...Save the form... // return $this->redirectToSuccessPage($data); } else { // Extend the session $container->setExpirationHops(1, 'partialTempFile'); // Form was not valid, but the file input might be... // Save file to a temporary file if valid. $data = $form->getData(); $fileErrors = $form->get('file')->getMessages(); if (empty($fileErrors) && isset($data['file']['error']) && $data['file']['error'] === UPLOAD_ERR_OK) { // NOTE: $data['file'] contains the filtered file path. // 'FileRenameUpload' Filter has been run, and moved the file. $container->partialTempFile = $tempFile = $data['file']; } } } else { // GET Request: Clear previous temp file from session unset($container->partialTempFile); $tempFile = null; } $view = new ViewModel(array('title' => 'Partial Validation Examples', 'form' => $form, 'tempFiles' => isset($tempFile) ? array($tempFile) : null)); $view->setTemplate('zf2-file-upload-examples/examples/single'); return $view; }
/** * @runInSeparateProcess */ public function testExpirationHops() { // since we cannot explicitly test reinitalizing the session // we will act in how session manager would in this case. $storage = new SessionArrayStorage(); $manager = new SessionManager(null, $storage); $manager->start(); $container = new Container('test'); $container->foo = 'bar'; $container->setExpirationHops(1); $copy = $_SESSION; $_SESSION = null; $storage->init($copy); $this->assertEquals('bar', $container->foo); $copy = $_SESSION; $_SESSION = null; $storage->init($copy); $this->assertNull($container->foo); }