Completed
Branch — master (f6660f)
by Stefan
02:22
created

FileHandler.class.php (3 issues)

1
<?php
2
3
// Copyright (C) 2014 Universitätsbibliothek Mannheim
4
// See file LICENSE for license details.
5
6
// Authors: Alexander Wagner, Stefan Weil
7
8
// Test whether the script was called directly (used for unit test).
9
if (!isset($unittest)) {
10
    $unittest = array();
11
}
12
$unittest[__FILE__] = (sizeof(get_included_files()) == 1);
13
14
15
16
class FehHandler extends FileHandler
17
{
18
    public function getControls()
19
    {
20
        return FileHandler::CURSOR | FileHandler::ZOOM;
21
    }
22
    public function show($path)
23
    {
24
    }
25
}
26
27
class LibreOfficeHandler extends FileHandler
28
{
29
    public function getControls()
30
    {
31
        return FileHandler::CURSOR | FileHandler::ZOOM;
32
    }
33
    public function show($path)
34
    {
35
    }
36
}
37
38
class MidoriHandler extends FileHandler
39
{
40
    public function getControls()
41
    {
42
        return FileHandler::CURSOR | FileHandler::ZOOM;
43
    }
44
    public function show($path)
45
    {
46
    }
47
}
48
49
class VlcHandler extends FileHandler
50
{
51
    public function getControls()
52
    {
53
        return FileHandler::CURSOR | FileHandler::ZOOM;
54
    }
55
    public function show($path)
56
    {
57
    }
58
}
59
60
class ZathuraHandler extends FileHandler
61
{
62
    public function getControls()
63
    {
64
        return FileHandler::CURSOR | FileHandler::ZOOM |
65
               FileHandler::HOME | FileHandler::END |
66
               FileHandler::PRIOR | FileHandler::NEXT |
67
               FileHandler::DOWNLOAD;
68
    }
69
    public function show($path)
70
    {
71
    }
72
}
73
74
abstract class FileHandler
75
{
76
77
    // Constants for allowed controls.
78
    const UP = 1;
79
    const DOWN = 2;
80
    const LEFT = 4;
81
    const RIGHT = 8;
82
    const ZOOMIN = 16;
83
    const ZOOMOUT = 32;
84
    const HOME = 64;
85
    const END = 128;
86
    const PRIOR = 256;
87
    const NEXT = 512;
88
    const DOWNLOAD = 1024;
89
    const COUNTERCLOCKWISE = 2048;
90
    const CLOCKWISE = 4096;
91
92
    // Shortcuts for combinations of controls.
93
    const CURSOR = 15; // UP | DOWN | LEFT | RIGHT
94
    const ZOOM = 48;   // ZOOMIN | ZOOMOUT
95
    const ALL = 2047;
96
97
    // up down left right zoomin zoomout home end prior next download
98
99
    // protected $FILES = array();
0 ignored issues
show
Unused Code Comprehensibility introduced by
55% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
100
    // protected $UPLOAD_PATH;
101
102
    abstract protected function getControls();
103
    abstract protected function show($path);
104
105
    public static function getFileHandler($file)
106
    {
107
108
        // Get get directory, name and file extension
109
        $pathParts = pathinfo($file);
110
        $ftype = strtolower($pathParts['extension']);
111
        $fdir = $pathParts['dirname'];
112
        $fname = $pathParts['filename'];
113
        $fhandler = "";
114
115
        // Define filehandlers
116
        $pdfHandler = '/usr/bin/zathura';
117
        $imageHandler = '/usr/bin/feh --scale-down';
118
        $webHandler = '/usr/bin/midori -p';
119
        $avHandler = '/usr/bin/cvlc --no-audio';
120
        $officeApp = "";
121
122
        // $params;
123
        // echo $ftype;
124
        if ($ftype === 'pdf') {
125
            $fhandler=$pdfHandler;
126
127
        } elseif ($ftype === 'gif' || $ftype === 'jpg' || $ftype === 'png') {
128
            $fhandler=$imageHandler;
129
130
        } elseif ($ftype === 'html' || $ftype === 'url') {
131
            $fhandler=$webHandler;
132
133
        } elseif ($ftype === 'mpg' || $ftype === 'mpeg' || $ftype === 'avi' ||
134
                  $ftype === 'mp3' || $ftype === 'mp4') {
135
            $fhandler=$avHandler;
136
137
        } else {
138
            if ($ftype === 'doc' || $ftype === 'docx' || $ftype === 'odt' || $ftype === 'txt') {
139
                $officeApp = "writer";
140
141
            } elseif ($ftype === 'ppt' || $ftype === 'pptx' || $ftype === 'pps' || $ftype === 'ppsx' || $ftype === 'odp') {
142
                $officeApp = "impress";
143
144
            } elseif ($ftype === 'xls' || $ftype === 'xlsx' || $ftype === 'ods') {
145
                $officeApp = "calc";
146
            }
147
            $convertedFile = convertOffice($file, $officeApp, $fdir, $fname);
148
            if ($convertedFile) {
149
                $file = $convertedFile;
150
                $fhandler = $pdfHandler;
151
            } else {
152
                $fhandler = "/usr/bin/libreoffice --'$officeApp' --nologo --norestore -o";
153
            }
154
        }
155
156
        /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
48% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
157
        alternatively with mime-types
158
159
            // $ftype = mime_content_type($this->UPLOAD_PATH.$file);
160
            // if($ftype=='application/pdf')
161
            // if($ftype=='image/gif' || $ftype=='image/jpg' || $ftype=='image/png' )
162
            // if($ftype=='html' || $ftype=='url' || $ftype="text/plain")
163
            // (...)
164
165
        */
166
167
        return array($fhandler, $file);
168
    }
169
}
170
171
function convertOffice($inputFile, $office, $outputDir, $fileName)
172
{
173
    shell_exec("/usr/bin/libreoffice --headless --convert-to pdf:'$office'_pdf_Export --outdir '$outputDir' '$inputFile' >/dev/null 2>&1");
174
    $newFile=$outputDir . '/' . $fileName . '.pdf';
175
    if (file_exists($newFile)) {
176
        return $newFile;
177
    } else {
178
        return false;
179
    }
180
}
181
182
if ($unittest[__FILE__]) {
183
    // Run unit test.
184
    $midoriHandler = new MidoriHandler;
185
    $zathuraHandler = new ZathuraHandler;
186
    echo("DOWNLOAD   =" . FileHandler::DOWNLOAD . "\n");
187
    echo("filehandler=" . FileHandler::getFileHandler("test.txt") . "\n");
0 ignored issues
show
Are you sure FileHandler::getFileHandler('test.txt') of type array<integer,string> can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

187
    echo("filehandler=" . /** @scrutinizer ignore-type */ FileHandler::getFileHandler("test.txt") . "\n");
Loading history...
188
    $handler = ${'midori' . 'Handler'};
189
    echo("controls   =" . $handler->getControls() . "\n");
190
}
191