public static function generatePreview($id, $versionId = false) { $rez = array(); $file = array(); $coreName = Config::get('core_name'); $coreUrl = Config::get('core_url'); $filesDir = Config::get('files_dir'); $filesPreviewDir = Config::get('files_preview_dir'); if (!empty($versionId)) { $file = DM\FilesVersions::read($versionId); $file['version_id'] = $versionId; $file['id'] = $id; } else { $file = DM\Files::read($id); } if (empty($file)) { \CB\debug('Error accessing file preview (' . $id . '). Record not found in DB.'); return array('html' => ''); } $content = DM\FilesContent::read($file['content_id']); $preview = DM\FilePreviews::read($content['id']); if (!empty($preview)) { switch ($preview['status']) { case 1: case 2: return array('processing' => true); case 3: return array('html' => L\get('ErrorCreatingPreview')); } } $ext = explode('.', $file['name']); $ext = array_pop($ext); $ext = strtolower($ext); $rez['ext'] = $ext; $rez['filename'] = $content['id'] . '_.html'; $previewFilename = $filesPreviewDir . $rez['filename']; $fn = $filesDir . $content['path'] . DIRECTORY_SEPARATOR . $content['id']; // $nfn = $filesPreviewDir . $content['id'] . '_.' . $ext; if (!file_exists($fn)) { \CB\debug('Error accessing file preview (' . $id . '). Its content (id: ' . @$content['id'] . ') doesnt exist on the disk.'); return false; } switch ($ext) { case 'rtf': case 'doc': case 'xls': case 'csv': case 'ppt': case 'pps': case 'docx': case 'docm': case 'xlsx': case 'pptx': case 'odt': if (empty($preview)) { DM\FilePreviews::create(array('id' => $content['id'], 'group' => 'office', 'status' => 1, 'filename' => null, 'size' => 0, 'cdate' => 'CURRENT_TIMESTAMP')); } if (file_exists($previewFilename)) { Files::deletePreview($content['id']); } $cmd = 'php -f ' . LIB_DIR . 'PreviewExtractorOffice.php -- -c ' . $coreName . ' > ' . Config::get('debug_log') . '_office &'; if (IS_WINDOWS) { $cmd = 'start /D "' . LIB_DIR . '" php -f PreviewExtractorOffice.php -- -c ' . $coreName . ' > ' . Config::get('debug_log') . '_office'; } pclose(popen($cmd, "r")); return array('processing' => true); break; case 'xml': case 'htm': case 'html': case 'dhtml': case 'xhtml': require_once LIB_DIR . 'PreviewExtractor.php'; $content = file_get_contents($fn); $pe = new PreviewExtractor(); $content = $pe->purify($content, array('URI.Base' => '/' . $coreName . '/', 'URI.MakeAbsolute' => true)); file_put_contents($previewFilename, $content); //copy($fn, $previewFilename); break; case 'txt': case 'log': case 'css': case 'js': case 'json': case 'php': case 'bat': case 'ini': case 'sys': case 'sql': file_put_contents($previewFilename, '<pre>' . Util\adjustTextForDisplay(file_get_contents($fn)) . '<pre>'); break; case 'pdf': $html = 'PDF'; //Ext panel - PreviewPanel view if (empty($_SERVER['HTTP_X_REQUESTED_WITH'])) { //full browser window view $url = $coreUrl . 'download/' . $file['id'] . '/'; $html = ' <object data="' . $url . '" type="application/pdf" width="100%" height="100%"> It appears you don\'t have Adobe Reader or PDF support in this web browser. <a href="' . $url . '">Click here to download the file</a> <embed src="' . $url . '" type="application/pdf" /> </object>'; } return array('html' => $html); break; case 'tif': case 'tiff': case 'svg': $pfn = $filesPreviewDir . $content['id']; $convertedImages = array($pfn . '_.png'); if (!file_exists($convertedImages[0])) { $convertedImages = array(); try { $images = new \Imagick($fn); $i = 1; foreach ($images as $image) { $image->setImageFormat('png'); $image->writeImage($pfn . $i . '_.png'); $convertedImages[] = $content['id'] . $i . '_.png'; $i++; } } catch (\Exception $e) { return $rez; } } file_put_contents($previewFilename, '<img src="/' . $coreName . '/view/' . implode('" class="fit-img" style="margin: auto" />' . "<br /><hr />\n" . '<img src="/' . $coreName . '/view/', $convertedImages) . '" class="fit-img" style="margin: auto" />'); break; default: if (substr($content['type'], 0, 5) == 'image' && substr($content['type'], 0, 9) !== 'image/svg') { file_put_contents($previewFilename, '<div style="padding: 5px 10px"><img src="/' . $coreName . '/download/' . $file['id'] . (empty($version_id) ? '' : '/' . $version_id) . '/" class="fit-img" style="margin: auto"></div>'); } } if (!empty($preview)) { DM\FilePreviews::update(array('id' => $content['id'], 'filename' => $rez['filename'])); } else { DM\FilePreviews::create(array('id' => $content['id'], 'filename' => $rez['filename'])); } return $rez; }
public static function generatePreview($id, $version_id = false) { $rez = array(); $file = array(); $coreName = Config::get('core_name'); $coreUrl = Config::get('core_url'); $filesDir = Config::get('files_dir'); $filesPreviewDir = Config::get('files_preview_dir'); $sql = 'SELECT f.id ,f.content_id ,f.name ,c.path ,c.`type` ,p.status FROM files f LEFT JOIN files_content c ON f.content_id = c.id LEFT JOIN file_previews p ON c.id = p.id WHERE f.id = $1 AND c.size > 0'; if (!empty($version_id)) { $sql = 'SELECT $1 `id` ,f.id `version_id` ,f.content_id ,f.name ,c.path ,c.`type` ,p.status FROM files_versions f LEFT JOIN files_content c ON f.content_id = c.id LEFT JOIN file_previews p ON c.id = p.id WHERE f.file_id = $1 AND f.id = $2 AND c.size > 0'; } $res = DB\dbQuery($sql, array($id, $version_id)) or die(DB\dbQueryError()); if ($r = $res->fetch_assoc()) { $file = $r; } $res->close(); if (empty($file)) { \CB\debug('Error accessing file preview (' . $id . '). Record not found in DB.'); return array('html' => ''); } switch ($file['status']) { case 1: case 2: return array('processing' => true); case 3: return array('html' => L\get('ErrorCreatingPreview')); } $ext = explode('.', $file['name']); $ext = array_pop($ext); $ext = strtolower($ext); $rez['ext'] = $ext; $rez['filename'] = $file['content_id'] . '_.html'; $preview_filename = $filesPreviewDir . $rez['filename']; $fn = $filesDir . $file['path'] . DIRECTORY_SEPARATOR . $file['content_id']; $nfn = $filesPreviewDir . $file['content_id'] . '_.' . $ext; if (!file_exists($fn)) { \CB\debug('Error accessing file preview (' . $id . '). Its content (id: ' . @$file['content_id'] . ') doesnt exist on the disk.'); return false; } switch ($ext) { case 'rtf': case 'doc': case 'xls': case 'csv': case 'ppt': case 'pps': case 'docx': case 'docm': case 'xlsx': case 'pptx': case 'odt': DB\dbQuery('INSERT INTO file_previews (id, `group`, status, filename, SIZE) VALUES($1 ,\'office\' ,1 ,NULL ,0) ON DUPLICATE KEY UPDATE `group` = \'office\' ,status =1 ,filename = NULL ,SIZE = 0 ,cdate = CURRENT_TIMESTAMP', $file['content_id']) or die(DB\dbQueryError()); if (file_exists($preview_filename)) { Files::deletePreview($file['content_id']); } $cmd = 'php -f ' . LIB_DIR . 'PreviewExtractorOffice.php -- -c ' . $coreName . ' > ' . Config::get('debug_log') . '_office &'; if (IS_WINDOWS) { $cmd = 'start /D "' . LIB_DIR . '" php -f PreviewExtractorOffice.php -- -c ' . $coreName; } pclose(popen($cmd, "r")); return array('processing' => true); break; case 'xml': case 'htm': case 'html': case 'dhtml': case 'xhtml': //file_put_contents( $preview_filename, Files::purify(file_get_contents($fn)) ); require_once LIB_DIR . 'PreviewExtractor.php'; $content = file_get_contents($fn); $pe = new PreviewExtractor(); $content = $pe->purify($content, array('URI.Base' => '/' . $coreName . '/', 'URI.MakeAbsolute' => true)); file_put_contents($preview_filename, $content); //copy($fn, $preview_filename); break; case 'txt': case 'css': case 'js': case 'json': case 'php': case 'bat': case 'ini': case 'sys': case 'sql': file_put_contents($preview_filename, '<pre>' . Util\adjustTextForDisplay(file_get_contents($fn)) . '<pre>'); break; case 'pdf': $html = 'PDF'; //Ext panel - PreviewPanel view if (empty($_SERVER['HTTP_X_REQUESTED_WITH'])) { //full browser window view $url = $coreUrl . 'download/' . $file['id'] . '/'; $html = ' <object data="' . $url . '" type="application/pdf" width="100%" height="100%"> It appears you don\'t have Adobe Reader or PDF support in this web browser. <a href="' . $url . '">Click here to download the file</a> <embed src="' . $url . '" type="application/pdf" /> </object>'; } return array('html' => $html); break; case 'tif': case 'tiff': case 'svg': $convertedImage = $filesPreviewDir . $file['content_id'] . '_.png'; if (!file_exists($convertedImage)) { try { $image = new \Imagick($fn); $image->setImageFormat('png'); $image->writeImage($convertedImage); } catch (\Exception $e) { return $rez; } } file_put_contents($preview_filename, '<img src="/' . $coreName . '/view/' . $file['content_id'] . '_.png" class="fit-img" style="margin: auto" />'); break; default: if (substr($file['type'], 0, 5) == 'image' && substr($file['type'], 0, 9) !== 'image/svg') { file_put_contents($preview_filename, '<div style="padding: 5px 10px"><img src="/' . $coreName . '/download/' . $file['id'] . (empty($version_id) ? '' : '/' . $version_id) . '/" class="fit-img" style="margin: auto"></div>'); } } return $rez; }