Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- define('PATH_T3X', 't3x');
- error_reporting(E_ALL & ~E_NOTICE);
- class T3xProcessor {
- private $t3xFile = '';
- private $content = array();
- private $viewableFiles = array(
- // Images
- 'gif','png','jpg',
- // PHP source
- 'php',
- // HTML
- 'html','htm','tmpl',
- // Plain Text
- 'txt','ts','sql', 'js','css','xml'
- );
- private $type = 't3x';
- private $compression = array();
- private $t3dChunks = array();
- public function __construct($t3xFile) {
- $this->t3xFile = $t3xFile;
- $fileParts = explode('.', $t3xFile);
- $this->type = strtolower(array_pop($fileParts));
- switch($this->type) {
- case 't3x':
- list($md5, $compress, $this->content) = explode(':', file_get_contents($t3xFile), 3);
- break;
- case 't3d':
- $content = file_get_contents($t3xFile);
- while ($chunk = $this->parseT3dChunk($content)) {
- $this->t3dChunks[] = $chunk;
- }
- break;
- default:
- throw new Exception('Unknown type: '.$this->type);
- }
- switch($compress) {
- case 'gzcompress':
- $this->compression[] = 'gzip';
- $this->content = gzuncompress($this->content);
- $contentMd5 = md5($this->content);
- break;
- default:
- $contentMd5 = NULL;
- }
- if ($contentMd5 && $md5 != $contentMd5) {
- throw new Exception('Checksum failed, expected '.$md5.', have '.$contentMd5);
- }
- if ($this->type=='t3x') {
- $this->content = unserialize($this->content);
- }
- }
- private function parseT3dChunk(&$content) {
- list($md5, $compress, $length, $content) = explode(':', $content, 4);
- if (!$content) {
- return FALSE;
- }
- $chunkLength = intval($length);
- $chunk = substr($content, 0, $chunkLength);
- $contentMd5 = md5($chunk);
- if ($md5 != $contentMd5) {
- throw new Exception('Checksum failed, expected '.$md5.', have '.$contentMd5);
- }
- if (intval($compress)) {
- $chunk = gzuncompress($chunk);
- $this->compression[] = 'gzip';
- } else {
- $this->compression[] = 'none';
- }
- $content = substr($content, $chunkLength+1);
- return unserialize($chunk);
- }
- private function setDownloadHeaders() {
- header('Last-Modified: '.gmdate('D, d M Y H:i:s', $file['mtime']) . ' GMT');
- header('Expires: Mon, 31 Jan 1996 00:13:37 GMT');
- header('Pragma: no-cache');
- header('Cache-Control: no-cache, no-store, max-age=0, must-revalidate');
- header('Content-Type: application/octet-stream');
- header('Content-Transfer-Encoding: binary');
- header('Connection: close');
- }
- private function getExtension($filename) {
- $filename = explode('.', $filename);
- return strtolower(array_pop($filename));
- }
- public function getFilename($path) {
- $path = explode('/', $path);
- return strtolower(array_pop($path));
- }
- private function isViewable($filename) {
- $filename = $this->getFilename($filename);
- $ext = $this->getExtension($filename);
- $isDot = substr($filename, 0, 1)==='.';
- return (!$isDot && in_array($ext, $this->viewableFiles));
- }
- public function showHeader() {
- echo '<h2>Displaying '.substr($this->t3xFile,4)."</h2>\n";
- }
- public function showProperties() {
- echo '<div id="tab5"><ul>';
- $properties = array(
- 'Type: '.$this->type,
- 'Filesize: '.filesize($this->t3xFile).'B',
- 'Compression: '.implode(', ', array_unique($this->compression)),
- );
- foreach ($properties as $property) {
- echo '<li>'.$property."</li>\n";
- }
- echo '<li><a href="http://typo3.org/extensions/repository/view/' . $this->content['EM_CONF']['title'] . '" target="_blank">TER</a></li>';
- echo '</ul></div>';
- }
- public function showEmConf() {
- echo '<div id="tab2">';
- echo '<table cellspacing=0>';
- echo '<tr><th>Key</th><th> Value</th></tr>';
- if (is_array($this->content['EM_CONF'])) {
- foreach($this->content['EM_CONF'] as $k=>$v) {
- echo '<tr class="data"><td>'.$k;
- echo '</td><td>: '.(is_array($v) ? '<pre>'.var_export($v).'</pre>' : $v);
- echo "</td></tr>\n";
- }
- }
- echo "</table></div>\n\n";
- }
- public function showFileIndex() {
- echo '<div id="tab3">';
- echo '<table cellspacing=0>';
- echo "<tr><th>Name</th><th>Size</th><th>MTime</th><th>IsExec?</th><th>Valid?</th><th>Download</th></tr>\n";
- foreach ($this->content['FILES'] as $filename=>$info) {
- if (substr(strtolower($info['name']),-4)=='.t3d') {
- $name = '<a href="t3x_reader.php?current_t3x='.$info['name'].'&extractfrom='.$this->t3xFile.'">'.$info['name'].'</a>';
- } else {
- $name = $info['name'];
- }
- echo '<tr class="data"><td>'.$name;
- echo "</td><td>".$info['size'];
- echo "</td><td>".date('Y-m-d H:i:s', $info['mtime']);
- echo "</td><td>".($info['is_executable'] ? 'Yes' : 'No');
- echo "</td><td>".($info['content_md5'] == md5($info['content']) ? 'Yes' : 'No');
- echo '</td><td><a href="t3x_reader.php?t3x='.$this->t3xFile.'&a=dl&f='.$info['name'].'"><img src="../../famfamfam_silk_icons_v013/icons/bullet_disk.png" /></a>';
- if ($this->isViewable($info['name'])) {
- echo ' <a href="t3x_reader.php?t3x='.$this->t3xFile.'&a=vw&f='.$info['name'].'" target="_blank"><img src="../../famfamfam_silk_icons_v013/icons/magnifier.png" /></a>';
- }
- echo "</td></tr>\n";
- }
- echo '</table></div>';
- }
- public function showConvert() {
- echo '<div id="tab4"><ul>';
- echo '<li><a href="t3x_reader.php?t3x='.$this->t3xFile.'&a=ar&t=zip">Download as ZIP</a>'."</li>\n";
- echo '</ul></div>';
- }
- public function getFileContent($filename) {
- return $this->content['FILES'][$filename]['content'];
- }
- public function download($filename) {
- $file = $this->content['FILES'][$filename];
- $dlname = $this->getFilename($filename);
- $this->setDownloadHeaders();
- header('Content-Length: '.$file['size']);
- header('Content-Disposition: attachment; filename="'.$dlname.'"');
- echo $this->getFileContent($filename);
- flush();
- exit();
- }
- public function view($filename) {
- $file = $this->content['FILES'][$filename];
- $ext = explode('.', $filename);
- $ext = strtolower($ext[count($ext)-1]);
- $mimetype = $content = null;
- switch ($ext) {
- case 'gif':
- $mimetype = 'image/gif';
- $content = $file['content'];
- break;
- case 'png':
- $mimetype = 'image/png';
- $content = $file['content'];
- break;
- case 'jpg':
- $mimetype = 'image/jpg';
- $content = $file['content'];
- break;
- case 'php':
- $mimetype = 'text/html';
- $content = highlight_string($file['content'], TRUE);
- break;
- case 'html':
- case 'htm':
- case 'tmpl':
- $mimetype = 'text/html';
- $content = $file['content'];
- break;
- case 'txt':
- case 'sql':
- case 'ts':
- $mimetype = 'text/plain';
- $content = $file['content'];
- break;
- default:
- }
- if ($mimetype) {
- header('Content-Type: '.$mimetype);
- echo $content;
- exit();
- }
- }
- public function archive($type) {
- switch ($type) {
- case 'zip':
- $this->archiveZip();
- break;
- }
- }
- public function archiveZip() {
- $zip = new ZipArchive();
- $zipFile = tempnam('.', 'tmp').'.zip';
- if (TRUE !== ($r=$zip->open($zipFile, ZIPARCHIVE::CREATE))) {
- throw new Exception('can not create zip archive '.$zipFile.' err='.$r);
- }
- foreach($this->content['FILES'] as $file) {
- $zip->addFromString($file['name'], $file['content']);
- }
- $zip->close();
- $this->setDownloadHeaders();
- header('Content-Length: '.filesize($zipFile));
- header('Content-Disposition: attachment; filename="'.$this->getFilename($this->t3xFile).'.zip"');
- @readfile($zipFile);
- flush();
- unlink($zipFile);
- exit();
- }
- }
- if (count($_GET)) {
- switch ($_GET['a']) {
- case 'dl':
- $tp = new T3xProcessor($_GET['t3x']);
- $tp->download($_GET['f']);
- break;
- case 'vw':
- $tp = new T3xProcessor($_GET['t3x']);
- $tp->view($_GET['f']);
- break;
- case 'ar':
- $tp = new T3xProcessor($_GET['t3x']);
- $tp->archive($_GET['t']);
- break;
- }
- exit('invalid headers');
- }
- @mkdir(PATH_T3X);
- $t3x = NULL;
- if ($_SERVER['REQUEST_METHOD']=="POST") {
- $up = $_FILES['upload_t3x'];
- if (filter_var($_POST['download_t3x'], FILTER_VALIDATE_URL, FILTER_FLAG_SCHEME_REQUIRED)) {
- $path = parse_url($_POST['download_t3x'], PHP_URL_PATH);
- $path = explode('/', $path);
- array_pop($path);
- $filename = implode('.', array_slice($path, 4));
- $content = file_get_contents($_POST['download_t3x']);
- file_put_contents(PATH_T3X.DIRECTORY_SEPARATOR.$filename, $content);
- $t3x = PATH_T3X.DIRECTORY_SEPARATOR.$filename;
- $_POST['current_t3x'] = $filename;
- } elseif (!$up['error'] && in_array(strtolower(substr($up['name'], -4)), array('.t3x','.t3d'))) { // file upload
- // upload_t3x
- $t3x = PATH_T3X.DIRECTORY_SEPARATOR.$up['name'];
- copy($up['tmp_name'], $t3x);
- $_POST['current_t3x'] = $up['name'];
- } elseif ($_POST['current_t3x']) {
- $t3x = PATH_T3X.DIRECTORY_SEPARATOR.$_POST['current_t3x'];
- }
- } elseif ($_GET['current_t3x']) {
- $t3x = PATH_T3X.DIRECTORY_SEPARATOR.$_GET['current_t3x'];
- if ($_GET['extractfrom']) {
- $tp = new T3xProcessor($_GET['extractfrom']);
- $t3x = PATH_T3X.DIRECTORY_SEPARATOR.$tp->getFilename($_GET['current_t3x']);
- file_put_content($t3x, $tp->getFileContent($_GET['current_t3x']));
- unset($tp);
- }
- }
- $options = '<option value="">-- Select T3X/T3D --</option>';
- $di = new DirectoryIterator(PATH_T3X);
- $files = array();
- foreach ($di as $fileInfo) {
- if (!$fileInfo->isFile()) {
- continue;
- }
- $ext = strtolower(substr($fileInfo->getFilename(), -4));
- if (!in_array($ext, array('.t3x','.t3d'))) {
- continue;
- }
- $files[] = $fileInfo->getFilename();
- }
- sort($files);
- foreach ($files as $filename) {
- $selected = ($filename==$_POST['current_t3x'] ? 'selected="selected"' : '');
- $options .= '<option value="'.$filename.'" '.$selected.'>'.$filename."</option>\n";
- }
- if ($t3x) {
- $tp = new T3xProcessor($t3x);
- }
- ?>
- <html>
- <head>
- <title>T3X Reader</title>
- <link href="https://code.jquery.com/ui/1.11.1/themes/blitzer/jquery-ui.css" rel="stylesheet" type="text/css" />
- <script type="text/javascript" src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
- <script type="text/javascript" src="https://code.jquery.com/ui/1.10.4/jquery-ui.min.js"></script>
- <style type="text/css">
- table {
- padding: 0px;
- }
- th {
- text-align: left;
- background-color: silver;
- padding-right: 10px;
- }
- td {
- padding-right: 25px;
- }
- tr.data > td {
- font-size: 11px;
- }
- a > img {
- border: 0;
- }
- div#tabs > div {
- max-height: 380px;
- overflow-y: scroll;
- }
- tr.even {
- background-color: white;
- }
- </style>
- <script type="text/javascript">
- jQuery.noConflict();
- jQuery(document).ready(function(){
- jQuery('#tabs').tabs();
- jQuery('#tab3 table tr.data:even').addClass('even');
- });
- </script>
- </head>
- <body>
- <h1>T3X reader</h1>
- <?php
- if ($tp instanceof T3xProcessor) {
- $tp->showHeader();
- }
- ?>
- <div id="tabs" style="display:block">
- <ul>
- <li><a href="#tab1">Select T3X</a></li>
- <?php if ($tp instanceof T3xProcessor) { ?>
- <li><a href="#tab5">Properties</a></li>
- <li><a href="#tab2">EM-Conf</a></li>
- <li><a href="#tab3">File list</a></li>
- <li><a href="#tab4">Convert</a></li>
- <?php } ?>
- </ul>
- <div id="tab1">
- <p>Pick a T3X file to process or upload:</p>
- <form method="POST" action="t3x_reader.php" enctype="multipart/form-data">
- <table>
- <tr><td>Process already uploaded file</td><td>: <select name="current_t3x"><?php echo $options; ?></select></td></tr>
- <tr><td>URL to download T3X</td><td>: <input type="text" name="download_t3x" value="" /></td></tr>
- <tr><td>Upload T3X file</td><td>: <input type="file" name="upload_t3x" value="" /></td></tr>
- <tr><td> </td><td><input type="submit" value=" GO " /></td></tr>
- </table>
- </form>
- </div>
- <?php
- if ($tp instanceof T3xProcessor) {
- $tp->showEmConf();
- $tp->showProperties();
- $tp->showFileIndex();
- $tp->showConvert();
- }
- ?>
- </div>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement