summaryrefslogtreecommitdiff
path: root/lib/viewers
diff options
context:
space:
mode:
authorAleksander Machniak <alec@alec.pl>2013-05-19 10:43:14 (GMT)
committerAleksander Machniak <alec@alec.pl>2013-05-19 10:43:14 (GMT)
commiteb3de35228faeeaf66517988c687030d8abba924 (patch)
tree31cf379e44a27c7ea2b8d5b41ea77a61ed9fd339 /lib/viewers
parent8f1bf539d0816b3e24f5aa3cd5e14ee4ace6a3d4 (diff)
downloadkolab-chwala-eb3de35228faeeaf66517988c687030d8abba924.tar.gz
Move viewers to the API
Diffstat (limited to 'lib/viewers')
-rw-r--r--lib/viewers/media.php17
-rw-r--r--lib/viewers/odf.php27
-rw-r--r--lib/viewers/pdf.php23
-rw-r--r--lib/viewers/text.php75
4 files changed, 103 insertions, 39 deletions
diff --git a/lib/viewers/media.php b/lib/viewers/media.php
index 82d9024..1bf7d49 100644
--- a/lib/viewers/media.php
+++ b/lib/viewers/media.php
@@ -25,7 +25,7 @@
/**
* Class integrating HTML5 audio/video player from http://mediaelementjs.com
*/
-class file_ui_viewer_media extends file_ui_viewer
+class file_viewer_media extends file_viewer
{
protected $mimetypes = array(
'video/mp4',
@@ -51,12 +51,12 @@ class file_ui_viewer_media extends file_ui_viewer
/**
* Class constructor
*
- * @param file_ui File UI object
+ * @param file_api File API object
*/
- public function __construct($ui)
+ public function __construct($api)
{
// @TODO: disable types not supported by some browsers
- $this->ui = $ui;
+ $this->api = $api;
}
/**
@@ -67,7 +67,8 @@ class file_ui_viewer_media extends file_ui_viewer
*/
public function frame($file, $mimetype = null)
{
- $file_uri = htmlentities($this->ui->file_url($file));
+ $path = $_SERVER['SCRIPT_URI'];
+ $file_uri = htmlentities($this->api->file_url($file));
$mimetype = htmlentities($mimetype);
$source = "<source src=\"$file_uri\" type=\"$mimetype\"></source>";
@@ -79,9 +80,9 @@ class file_ui_viewer_media extends file_ui_viewer
}
return <<<EOT
- <link rel="stylesheet" type="text/css" href="viewers/media/mediaelementplayer.css" />
- <script type="text/javascript" src="js/jquery.min.js"></script>
- <script type="text/javascript" src="viewers/media/mediaelement-and-player.js"></script>
+ <link rel="stylesheet" type="text/css" href="{$path}viewers/media/mediaelementplayer.css" />
+ <script type="text/javascript" src="{$path}js/jquery.min.js"></script>
+ <script type="text/javascript" src="{$path}viewers/media/mediaelement-and-player.js"></script>
<$tag id="media-player" controls preload="auto">$source</$tag>
<style>
.mejs-container { text-align: center; }
diff --git a/lib/viewers/odf.php b/lib/viewers/odf.php
index 099f720..5a5112b 100644
--- a/lib/viewers/odf.php
+++ b/lib/viewers/odf.php
@@ -22,7 +22,10 @@
+--------------------------------------------------------------------------+
*/
-class file_ui_viewer_odf extends file_ui_viewer
+/**
+ * Class integrating ODF documents viewer from http://webodf.org
+ */
+class file_viewer_odf extends file_viewer
{
protected $mimetypes = array(
'application/vnd.oasis.opendocument.text',
@@ -49,11 +52,11 @@ class file_ui_viewer_odf extends file_ui_viewer
/**
* Class constructor
*
- * @param file_ui File UI object
+ * @param file_api File API object
*/
- public function __construct($ui)
+ public function __construct($api)
{
- $this->ui = $ui;
+ $this->api = $api;
}
/**
@@ -91,6 +94,20 @@ class file_ui_viewer_odf extends file_ui_viewer
}
/**
+ * Return file viewer URL
+ *
+ * @param string $file File name
+ * @param string $mimetype File type
+ */
+ public function href($file, $mimetype = null)
+ {
+ return $_SERVER['SCRIPT_URI'] . '?method=file_get'
+ . '&viewer=odf'
+ . '&file=' . urlencode($file)
+ . '&token=' . urlencode(session_id());
+ }
+
+ /**
* Print output and exit
*
* @param string $file File name
@@ -98,7 +115,7 @@ class file_ui_viewer_odf extends file_ui_viewer
*/
public function output($file, $mimetype = null)
{
- $file_uri = $this->ui->file_url($file);
+ $file_uri = $this->api->file_url($file);
echo <<<EOT
<html>
diff --git a/lib/viewers/pdf.php b/lib/viewers/pdf.php
index 51aef6a..442714b 100644
--- a/lib/viewers/pdf.php
+++ b/lib/viewers/pdf.php
@@ -22,7 +22,10 @@
+--------------------------------------------------------------------------+
*/
-class file_ui_viewer_pdf extends file_ui_viewer
+/**
+ * Class integrating PDF viewer from https://github.com/mozilla/pdf.js
+ */
+class file_viewer_pdf extends file_viewer
{
protected $mimetypes = array(
'application/pdf',
@@ -33,16 +36,26 @@ class file_ui_viewer_pdf extends file_ui_viewer
'text/x-pdf',
);
+ /**
+ * Class constructor
+ *
+ * @param file_api File API object
+ */
+ public function __construct($api)
+ {
+ // @TODO: disable types not supported by some browsers
+ $this->api = $api;
+ }
/**
- * Print output and exit
+ * Return file viewer URL
*
* @param string $file File name
* @param string $mimetype File type
*/
- public function output($file, $mimetype = null)
+ public function href($file, $mimetype = null)
{
- header('Location: viewers/pdf/viewer.html?file=' . urlencode($this->ui->file_url($file)));
- exit;
+ return $_SERVER['SCRIPT_URI'] . 'viewers/pdf/viewer.html'
+ . '?file=' . urlencode($this->api->file_url($file));
}
}
diff --git a/lib/viewers/text.php b/lib/viewers/text.php
index 1f9c019..236396d 100644
--- a/lib/viewers/text.php
+++ b/lib/viewers/text.php
@@ -25,7 +25,7 @@
/**
* Class integrating text editor http://ajaxorg.github.io/ace
*/
-class file_ui_viewer_text extends file_ui_viewer
+class file_viewer_text extends file_viewer
{
/**
* Mimetype to tokenizer map
@@ -56,7 +56,16 @@ class file_ui_viewer_text extends file_ui_viewer
*/
public function supported_mimetypes()
{
- return array_keys($this->mimetypes);
+ // we return only mimetypes not starting with text/
+ $mimetypes = array();
+
+ foreach (array_keys($this->mimetypes) as $type) {
+ if (strpos($type, 'text/') !== 0) {
+ $mimetypes[] = $type;
+ }
+ }
+
+ return $mimetypes;
}
/**
@@ -68,7 +77,7 @@ class file_ui_viewer_text extends file_ui_viewer
*/
public function supports($mimetype)
{
- return $this->mimetypes[$mimetype] || preg_match('/^text\//', $mimetype);
+ return $this->mimetypes[$mimetype] || preg_match('/^text\/(?!(pdf|x-pdf))/', $mimetype);
}
/**
@@ -76,12 +85,26 @@ class file_ui_viewer_text extends file_ui_viewer
*/
protected function print_file($file)
{
- $observer = new file_viewer_request_observer;
- $request = $this->ui->api->request();
+ $stdout = fopen('php://output', 'w');
- $request->attach($observer);
- $this->ui->api->get('file_get', array('file' => $file, 'force-type' => 'text/plain'));
- $request->detach($observer);
+ stream_filter_register('file_viewer_text', 'file_viewer_content_filter');
+ stream_filter_append($stdout, 'file_viewer_text');
+
+ $this->api->api->file_get($file, array(), $stdout);
+ }
+
+ /**
+ * Return file viewer URL
+ *
+ * @param string $file File name
+ * @param string $mimetype File type
+ */
+ public function href($file, $mimetype = null)
+ {
+ return $_SERVER['SCRIPT_URI'] . '?method=file_get'
+ . '&viewer=text'
+ . '&file=' . urlencode($file)
+ . '&token=' . urlencode(session_id());
}
/**
@@ -127,24 +150,34 @@ class file_ui_viewer_text extends file_ui_viewer
/**
- * Observer for HTTP_Request2 implementing file body printing
- * with HTML special characters "escaping" for use in HTML code
+ * PHP stream filter to detect escape html special chars in a file
*/
-class file_viewer_request_observer implements SplObserver
+class file_viewer_content_filter extends php_user_filter
{
- public function update(SplSubject $subject)
+ private $buffer = '';
+ private $cutoff = 2048;
+
+ function onCreate()
{
- $event = $subject->getLastEvent();
+ $this->cutoff = rand(2048, 3027);
+ return true;
+ }
+
+ function filter($in, $out, &$consumed, $closing)
+ {
+ while ($bucket = stream_bucket_make_writeable($in)) {
+ $bucket->data = htmlspecialchars($bucket->data, ENT_COMPAT | ENT_HTML401 | ENT_IGNORE);
+ $this->buffer .= $bucket->data;
- switch ($event['name']) {
- case 'receivedHeaders':
- case 'receivedBody':
- break;
+ // keep buffer small enough
+ if (strlen($this->buffer) > 4096) {
+ $this->buffer = substr($this->buffer, $this->cutoff);
+ }
- case 'receivedBodyPart':
- case 'receivedEncodedBodyPart':
- echo htmlspecialchars($event['data'], ENT_COMPAT | ENT_HTML401 | ENT_IGNORE);
- break;
+ $consumed += $bucket->datalen; // or strlen($bucket->data)?
+ stream_bucket_append($out, $bucket);
}
+
+ return PSFS_PASS_ON;
}
}