Page MenuHomePhorge

No OneTemporary

Size
11 KB
Referenced Files
None
Subscribers
None
diff --git a/lib/client/file_ui_client_file.php b/lib/client/file_ui_client_file.php
index dea9c83..7b8581d 100644
--- a/lib/client/file_ui_client_file.php
+++ b/lib/client/file_ui_client_file.php
@@ -1,164 +1,153 @@
<?php
/*
+--------------------------------------------------------------------------+
| This file is part of the Kolab File API |
| |
| Copyright (C) 2011-2013, Kolab Systems AG |
| |
| This program is free software: you can redistribute it and/or modify |
| it under the terms of the GNU Affero General Public License as published |
| by the Free Software Foundation, either version 3 of the License, or |
| (at your option) any later version. |
| |
| This program is distributed in the hope that it will be useful, |
| but WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public License |
| along with this program. If not, see <http://www.gnu.org/licenses/> |
+--------------------------------------------------------------------------+
| Author: Aleksander Machniak <machniak@kolabsys.com> |
+--------------------------------------------------------------------------+
*/
class file_ui_client_file extends file_ui
{
private $file;
private $filedata;
private $viewer;
public function action_open()
{
$this->ui_init();
// assign default set of translations
$this->output->add_translation('saving', 'deleting');
$this->file = $this->get_input('file', 'GET'); // @TODO: error handling
// Set filename (without path?) as a page title
$this->page_title = $this->file;
$this->task_template = 'file_open';
// fetch file metadata
$this->file_data();
$this->output->set_env('file', $this->file);
$this->output->set_env('filedata', $this->filedata);
// read browser capabilities
if (isset($_GET['caps'])) {
$caps = explode(',', $_GET['caps']);
$capabilities = array('pdf' => 0, 'flash' => 0, 'tif' => 0);
foreach ($caps as $c) {
$capabilities[$c] = 1;
}
$this->output->set_env('browser_capabilities', $capabilities);
}
if (!empty($_GET['viewer'])) {
$this->viewer = $this->find_viewer($this->filedata['mimetype']);
}
}
/**
* File content template object
*/
public function file_open_frame()
{
// check if viewer provides frame content
if ($this->viewer) {
if ($frame = $this->viewer->frame($this->file, $this->filedata['mimetype'])) {
return $frame;
}
}
// src attribute will be set on page load
return html::iframe(array('id' => 'file-content'));
}
/**
* Fetch and parse file metadata
*/
protected function file_data()
{
$response = $this->api_get('file_info', array('file' => $this->file));
$this->filedata = $response->get(); // @TODO: error handling
$mimetype = $this->real_mimetype($this->filedata['type']);
// create href string for file load frame
if (!empty($_GET['viewer'])) {
$href = '?task=viewer&mimetype=' . urlencode($mimetype);
}
else {
$href = 'api/?method=file_get';
if ($mimetype != $this->filedata['type']) {
$href .= '&force-type=' . urlencode($mimetype);
}
}
$href .= '&file=' . urlencode($this->file)
. '&token=' . urlencode($_SESSION['user']['token']);
$this->filedata['mimetype'] = $mimetype;
$this->filedata['href'] = $href;
}
/**
* Apply some fixes on file mimetype string
*/
protected function real_mimetype($mimetype)
{
if (preg_match('/^text\/(.+)/i', $mimetype, $m)) {
// fix pdf mimetype
if (preg_match('/^(pdf|x-pdf)$/i', $m[1])) {
$mimetype = 'application/pdf';
}
-
- // display all text/* (with few exceptions) as text/plain
- // to force display mode, in other case browser would invoke save-as window
- // @TODO: display HTML as text? implement editor for both modes?
- if (!preg_match('/^(plain|html)$/i', $m[1])) {
- $mimetype = 'text/plain';
- }
- }
- // handle message/rfc822 as text/plain
- if (preg_match('/^message\/rfc822/i', $mimetype)) {
- $mimetype = 'text/plain';
}
return $mimetype;
}
public function file_open_data()
{
$table = new html_table(array('cols' => 2));
// file name
$table->add('label', $this->translate('file.name').':');
$table->add('data filename', html::quote($this->filedata['name']));
// file type
// @TODO: human-readable type name
$table->add('label', $this->translate('file.type').':');
$table->add('data filetype', html::quote($this->filedata['type']));
// file size
$table->add('label', $this->translate('file.size').':');
$table->add('data filesize', html::quote($this->show_bytes($this->filedata['size'])));
// file modification time
$table->add('label', $this->translate('file.mtime').':');
$table->add('data filemtime', html::quote($this->filedata['mtime']));
// @TODO: for images: width, height, color depth, etc.
// @TODO: for text files: count of characters, lines, words
return $table->show();
}
}
diff --git a/lib/viewers/text.php b/lib/viewers/text.php
index 637adf1..1d2acdf 100644
--- a/lib/viewers/text.php
+++ b/lib/viewers/text.php
@@ -1,145 +1,149 @@
<?php
/*
+--------------------------------------------------------------------------+
| This file is part of the Kolab File API |
| |
| Copyright (C) 2011-2013, Kolab Systems AG |
| |
| This program is free software: you can redistribute it and/or modify |
| it under the terms of the GNU Affero General Public License as published |
| by the Free Software Foundation, either version 3 of the License, or |
| (at your option) any later version. |
| |
| This program is distributed in the hope that it will be useful, |
| but WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public License |
| along with this program. If not, see <http://www.gnu.org/licenses/> |
+--------------------------------------------------------------------------+
| Author: Aleksander Machniak <machniak@kolabsys.com> |
+--------------------------------------------------------------------------+
*/
/**
* Class integrating text editor http://ajaxorg.github.io/ace
*/
class file_ui_viewer_text extends file_ui_viewer
{
/**
* Mimetype to tokenizer map
*
* @var array
*/
protected $mimetypes = array(
'text/plain' => 'text',
'text/html' => 'html',
'text/javascript' => 'javascript',
'text/ecmascript' => 'javascript',
'text/x-c' => 'c_cpp',
'text/css' => 'css',
'text/x-java-source' => 'java',
'text/x-sh' => 'sh',
'text/xml' => 'xml',
'application/xml' => 'xml',
'application/x-vbscript' => 'vbscript',
'message/rfc822' => 'text',
);
/**
* Returns list of supported mimetype
*
* @return array List of mimetypes
*/
public function supported_mimetypes()
{
return array_keys($this->mimetypes);
}
/**
* Check if mimetype is supported by the viewer
*
* @param string $mimetype File type
*
* @return bool
*/
public function supports($mimetype)
{
return $this->mimetypes[$mimetype] || preg_match('/^text\//', $mimetype);
}
+ /**
+ * Print file content
+ */
+ protected function print_file($file)
+ {
+ $observer = new file_viewer_request_observer;
+ $request = $this->ui->api->request();
+
+ $request->attach($observer);
+ $this->ui->api->get('file_get', array('file' => $file, 'force-type' => 'text/plain'));
+ $request->detach($observer);
+ }
+
/**
* Print output and exit
*
* @param string $file File name
* @param string $mimetype File type
*/
public function output($file, $mimetype = null)
{
$mode = $this->mimetypes[$mimetype] ?: 'text';
- echo <<<EOT
-<!DOCTYPE html>
+ echo '<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Editor</title>
<script src="viewers/text/ace.js" type="text/javascript" charset="utf-8"></script>
<style>
#editor { top: 0; right: 0; bottom: 0; left: 0; position: absolute; font-size: 14px; padding: 0; margin: 0; }
+ .ace_search_options { float: right; }
</style>
</head>
<body>
- <pre id="editor">
-EOT;
- // print file body
- $observer = new file_viewer_request_observer;
- $request = $this->ui->api->request();
- $request->attach($observer);
- $this->ui->api->get('file_get', array('file' => $file));
- $request->detach($observer);
+ <pre id="editor">';
+
+ $this->print_file($file);
- echo <<<EOT
- </pre>
+ echo "</pre>
<script>
- var editor = ace.edit("editor"),
+ var editor = ace.edit('editor'),
session = editor.getSession();
+ editor.focus();
editor.setReadOnly(true);
-// editor.setTheme("ace/theme/twilight");
- session.setMode("ace/mode/$mode");
+ session.setMode('ace/mode/$mode');
</script>
</body>
-</html>
-EOT;
+</html>";
}
}
/**
* Observer for HTTP_Request2 implementing file body printing
* with HTML special characters "escaping" for use in HTML code
*/
class file_viewer_request_observer implements SplObserver
{
public function update(SplSubject $subject)
{
$event = $subject->getLastEvent();
switch ($event['name']) {
case 'receivedHeaders':
+ case 'receivedBody':
break;
case 'receivedBodyPart':
case 'receivedEncodedBodyPart':
echo htmlspecialchars($event['data'], ENT_COMPAT | ENT_HTML401 | ENT_IGNORE);
break;
-
- case 'receivedBody':
- break;
}
}
}

File Metadata

Mime Type
text/x-diff
Expires
Tue, Feb 3, 9:04 PM (12 h, 52 s)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
427442
Default Alt Text
(11 KB)

Event Timeline