Example #1
0
 /**
  * Export a layout.
  * @param [type] $layoutId [description]
  */
 function Export($layoutId)
 {
     if ($layoutId == 0 || $layoutId == '') {
         return $this->SetError(__('Must provide layoutId'));
     }
     $config = new Config();
     if (!$config->CheckZip()) {
         return $this->SetError(__('Zip is not enabled on this server'));
     }
     $libraryPath = Config::GetSetting('LIBRARY_LOCATION');
     try {
         $dbh = PDOConnect::init();
         $sth = $dbh->prepare('
             SELECT layout, description, backgroundImageId, xml
               FROM layout
              WHERE layoutid = :layoutid');
         $sth->execute(array('layoutid' => $layoutId));
         if (!($row = $sth->fetch())) {
             $this->ThrowError(__('Layout not found.'));
         }
         // Open a ZIP file with the same name as the layout
         File::EnsureLibraryExists();
         $zip = new ZipArchive();
         $fileName = $libraryPath . 'temp/export_' . Kit::ValidateParam($row['layout'], _FILENAME) . '.zip';
         $result = $zip->open($fileName, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE);
         if ($result !== true) {
             $this->ThrowError(__('Can\'t create ZIP. Error Code: ' . $result));
         }
         // Add layout information to the ZIP
         $layout = array('layout' => Kit::ValidateParam($row['layout'], _STRING), 'description' => Kit::ValidateParam($row['description'], _STRING));
         $zip->addFromString('layout.json', json_encode($layout));
         // Add the layout XLF
         $xml = $row['xml'];
         $zip->addFromString('layout.xml', $xml);
         $params = array('layoutid' => $layoutId, 'excludeType' => 'module');
         $SQL = ' 
             SELECT media.mediaid, media.name, media.storedAs, originalFileName, type, duration
               FROM `media` 
                 INNER JOIN `lklayoutmedia`
                 ON lklayoutmedia.mediaid = media.mediaid
              WHERE lklayoutmedia.layoutid = :layoutid
                AND media.type <> :excludeType
             ';
         // Add the media to the ZIP
         $mediaSth = $dbh->prepare($SQL);
         $mediaSth->execute($params);
         $mappings = array();
         foreach ($mediaSth->fetchAll() as $media) {
             $mediaFilePath = $libraryPath . $media['storedAs'];
             $zip->addFile($mediaFilePath, 'library/' . $media['originalFileName']);
             $mappings[] = array('file' => $media['originalFileName'], 'mediaid' => $media['mediaid'], 'name' => $media['name'], 'type' => $media['type'], 'duration' => $media['duration'], 'background' => $media['mediaid'] == $row['backgroundImageId'] ? 1 : 0);
         }
         // Add the mappings file to the ZIP
         $zip->addFromString('mapping.json', json_encode($mappings));
         $zip->close();
         // Uncomment only if you are having permission issues
         // chmod($fileName, 0777);
         // Push file back to browser
         if (ini_get('zlib.output_compression')) {
             ini_set('zlib.output_compression', 'Off');
         }
         $size = filesize($fileName);
         header('Content-Type: application/octet-stream');
         header("Content-Transfer-Encoding: Binary");
         header("Content-disposition: attachment; filename=\"" . basename($fileName) . "\"");
         //Output a header
         header('Pragma: public');
         header('Cache-Control: max-age=86400');
         header('Expires: ' . gmdate('D, d M Y H:i:s \\G\\M\\T', time() + 86400));
         header('Content-Length: ' . $size);
         // Send via Apache X-Sendfile header?
         if (Config::GetSetting('SENDFILE_MODE') == 'Apache') {
             header("X-Sendfile: {$fileName}");
             exit;
         }
         // Send via Nginx X-Accel-Redirect?
         if (Config::GetSetting('SENDFILE_MODE') == 'Nginx') {
             header("X-Accel-Redirect: /download/temp/" . basename($fileName));
             exit;
         }
         // Return the file with PHP
         // Disable any buffering to prevent OOM errors.
         @ob_end_clean();
         @ob_end_flush();
         readfile($fileName);
         exit;
     } catch (Exception $e) {
         Debug::LogEntry('error', $e->getMessage());
         if (!$this->IsError()) {
             $this->SetError(1, __('Unknown Error'));
         }
         return false;
     }
 }