summaryrefslogtreecommitdiff
path: root/lib/viewers/text.php
blob: 718692c96bb038b3b871b09166792461c0aad645 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<?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_viewer_text extends file_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-php' => 'php',
        'text/x-sh' => 'sh',
        'text/xml' => 'xml',
        'application/xml' => 'xml',
        'application/x-vbscript' => 'vbscript',
        'message/rfc822' => 'text',
        'application/x-empty' => 'text',
    );

    /**
     * File extension to highligter mode mapping
     *
     * @var array
     */
    protected $extensions = array(
        'php'  => '/^(php|phpt|inc)$/',
        'html' => '/^html?$/',
        'css'  => '/^css$/',
        'xml'  => '/^xml$/',
        'javascript' => '/^js$/',
        'sh'   => '/^sh$/',
    );

    /**
     * Returns list of supported mimetype
     *
     * @return array List of mimetypes
     */
    public function supported_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;
    }

    /**
     * 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\/(?!(pdf|x-pdf))/', $mimetype);
    }

    /**
     * Print file content
     */
    protected function print_file($file)
    {
        $stdout = fopen('php://output', 'w');

        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 $this->api->file_url($file) . '&viewer=text';
    }

    /**
     * Print output and exit
     *
     * @param string $file     File name
     * @param string $mimetype File type
     */
    public function output($file, $mimetype = null)
    {
        $mode = $this->get_mode($mimetype, $file);
        $href = addcslashes($this->api->file_url($file), "'");

        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>
  <script src="viewers/text/file_editor.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">';

        $this->print_file($file);

        echo "</pre>
  <script>
    var file_editor = new file_editor;
    file_editor.init('editor', '$mode', '$href');
  </script>
</body>
</html>";
    }

    protected function get_mode($mimetype, $filename)
    {
        $mimetype = strtolower($mimetype);

        if ($this->mimetypes[$mimetype]) {
            return $this->mimetypes[$mimetype];
        }

        $filename = explode('.', $filename);
        $extension = count($filename) > 1 ? array_pop($filename) : null;

        if ($extension) {
            foreach ($this->extensions as $mode => $regexp) {
                if (preg_match($regexp, $extension)) {
                    return $mode;
                }
            }
        }

        return 'text';
    }
}


/**
 * PHP stream filter to detect escape html special chars in a file
 */
class file_viewer_content_filter extends php_user_filter
{
    private $buffer = '';
    private $cutoff = 2048;

    function onCreate()
    {
        $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;

            // keep buffer small enough
            if (strlen($this->buffer) > 4096) {
                $this->buffer = substr($this->buffer, $this->cutoff);
            }

            $consumed += $bucket->datalen; // or strlen($bucket->data)?
            stream_bucket_append($out, $bucket);
        }

        return PSFS_PASS_ON;
    }
}