/**
  * Action for importing an instance
  */
 public function importInstanceAction()
 {
     $session = new Zend_Session_Namespace('login');
     $instanceID = $session->importResponsesInstanceID;
     $instanceName = $this->_getParam('instanceName');
     $importResponses = $this->_getParam('importResponsesRadioButton');
     $decryptID = $this->_hasParam('decryptID') ? $this->_getParam('decryptID') : null;
     if (is_numeric($instanceID)) {
         $session->importResponsesInstanceID = intVal($instanceID);
     } elseif (isset($importResponses) && $importResponses === 'importInstanceResponses') {
         $this->_redirector->gotoRouteAndExit(array('action' => 'index'));
     }
     $uploadErrors = array(UPLOAD_ERR_INI_SIZE => 'The uploaded file exceeds the upload_max_filesize directive in php.ini.', UPLOAD_ERR_FORM_SIZE => 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.', UPLOAD_ERR_PARTIAL => 'The uploaded file was only partially uploaded.', UPLOAD_ERR_NO_FILE => 'No file was uploaded.', UPLOAD_ERR_NO_TMP_DIR => 'Missing a temporary folder.', UPLOAD_ERR_CANT_WRITE => 'Failed to write file to disk.', UPLOAD_ERR_EXTENSION => 'File upload stopped by extension.');
     $errorCode = $_FILES['instanceFile']['error'];
     if ($errorCode !== UPLOAD_ERR_OK) {
         if (isset($uploadErrors[$errorCode])) {
             throw new Exception($uploadErrors[$errorCode]);
         } else {
             throw new Exception("Unknown error uploading file.");
         }
     }
     $file = $_FILES['instanceFile']['tmp_name'];
     $filename = $_FILES['instanceFile']['name'];
     if (preg_match('/\\.enc$/i', $filename)) {
         if (!isset($decryptID) || $decryptID == 0) {
             throw new Exception('Key not specified for encrypted file');
         }
         $crypto = new CryptoModel(array('cryptoID' => $decryptID));
         if (preg_match('/\\.zip\\.enc$/i', $filename)) {
             $decrypted = $crypto->decrypt(file_get_contents($file), false);
             $tempfile = tempnam(PROJECT_PATH . DIRECTORY_SEPARATOR . 'tmp', 'zip');
             unlink($tempfile);
             file_put_contents($tempfile, $decrypted);
             $import = new ZipArchiveModel(null, array('filename' => $tempfile));
         } elseif (preg_match('/\\.xml.enc$/i', $filename)) {
             $decrypted = $crypto->decrypt(file_get_contents($file));
             $import = $decrypted;
         } else {
             throw new Exception('Unrecognized file extension [' . $filename . ']');
         }
     } elseif (preg_match('/\\.zip$/i', $filename)) {
         $import = new ZipArchiveModel(null, array('filename' => $file));
     } elseif (preg_match('/\\.xml$/i', $filename)) {
         $import = file_get_contents($file);
     } else {
         throw new Exception('Unrecognized file extension [' . $filename . ']');
     }
     // Import the questionnaire definition if it doesn't already exist
     QuestionnaireModel::importXML($import);
     if ($importResponses === 'importInstanceResponses') {
         $importResponsesInstanceID = $this->_getParam('importResponsesInstanceSelect');
         InstanceModel::importXML($import, $instanceName, array('instanceID' => $importResponsesInstanceID));
     } elseif ($importResponses === 'importXMLResponses') {
         InstanceModel::importXML($import, $instanceName, array('pageResponses' => array('all' => 1)));
     } else {
         InstanceModel::importXML($import, $instanceName);
     }
     $this->flash('notice', 'Import Complete');
     $this->_redirector->gotoRoute(array('action' => 'index'));
 }
 /**
  * Action for importing an questionnaire
  */
 public function importQuestionnaireAction()
 {
     $uploadErrors = array(UPLOAD_ERR_INI_SIZE => 'The uploaded file exceeds the upload_max_filesize directive in php.ini.', UPLOAD_ERR_FORM_SIZE => 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.', UPLOAD_ERR_PARTIAL => 'The uploaded file was only partially uploaded.', UPLOAD_ERR_NO_FILE => 'No file was uploaded.', UPLOAD_ERR_NO_TMP_DIR => 'Missing a temporary folder.', UPLOAD_ERR_CANT_WRITE => 'Failed to write file to disk.', UPLOAD_ERR_EXTENSION => 'File upload stopped by extension.');
     $decryptID = $this->_hasParam('decryptID') ? $this->_getParam('decryptID') : null;
     $errorCode = $_FILES['questionnaireFile']['error'];
     if ($errorCode !== UPLOAD_ERR_OK) {
         if (isset($uploadErrors[$errorCode])) {
             throw new Exception($uploadErrors[$errorCode]);
         } else {
             throw new Exception("Unknown error uploading file.");
         }
     }
     $file = $_FILES['questionnaireFile']['tmp_name'];
     $filename = $_FILES['questionnaireFile']['name'];
     if (preg_match('/\\.enc$/i', $filename)) {
         if (!isset($decryptID) || $decryptID == 0) {
             throw new Exception('Key not specified for encrypted file');
         }
         $crypto = new CryptoModel(array('cryptoID' => $decryptID));
         if (preg_match('/\\.zip\\.enc$/i', $filename)) {
             $decrypted = $crypto->decrypt(file_get_contents($file));
             $tempfile = tempnam(PROJECT_PATH . DIRECTORY_SEPARATOR . 'tmp', 'zip');
             unlink($tempfile);
             file_put_contents($tempfile, $decrypted);
             $import = new ZipArchiveModel(null, array('filename' => $tempfile));
         } elseif (preg_match('/\\.xml\\.enc$/i', $filename)) {
             $decrypted = $crypto->decrypt(file_get_contents($file));
             $import = $decrypted;
         } else {
             throw new Exception('Unrecognized file extension [' . $filename . ']');
         }
     } elseif (preg_match('/\\.zip$/i', $filename)) {
         $import = new ZipArchiveModel(null, array('filename' => $file));
     } elseif (preg_match('/\\.xml$/i', $filename)) {
         $import = file_get_contents($file);
     } else {
         throw new Exception('Unrecognized file extension [' . $filename . ']');
     }
     QuestionnaireModel::importXML($import);
     $this->flash('notice', 'Import Complete');
     $this->_redirector->gotoRoute(array('action' => 'index'));
 }