Completed
Pull Request — master (#137)
by Philipp
04:15
created

FileHandler::getControls()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
nc 1
dl 0
loc 1
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
    function getControls()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
19
    {
20
        return FileHandler::CURSOR | FileHandler::ZOOM;
21
    }
22
    function show($path)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
23
    {
24
    }
25
}
26
27
class LibreOfficeHandler extends FileHandler
28
{
29
    function getControls()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
30
    {
31
        return FileHandler::CURSOR | FileHandler::ZOOM;
32
    }
33
    function show($path)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
34
    {
35
    }
36
}
37
38
class MidoriHandler extends FileHandler
39
{
40
    function getControls()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
41
    {
42
        return FileHandler::CURSOR | FileHandler::ZOOM;
43
    }
44
    function show($path)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
45
    {
46
    }
47
}
48
49
class VlcHandler extends FileHandler
50
{
51
    function getControls()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
52
    {
53
        return FileHandler::CURSOR | FileHandler::ZOOM;
54
    }
55
    function show($path)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
56
    {
57
    }
58
}
59
60
class ZathuraHandler extends FileHandler
61
{
62
    function getControls()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
63
    {
64
        return FileHandler::CURSOR | FileHandler::ZOOM |
65
               FileHandler::HOME | FileHandler::END |
66
               FileHandler::PRIOR | FileHandler::NEXT |
67
               FileHandler::DOWNLOAD;
68
    }
69
    function show($path)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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 = "";
0 ignored issues
show
Unused Code introduced by
$fhandler is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
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) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $convertedFile of type string|false is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
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");
188
    $handler = ${'midori' . 'Handler'};
189
    echo("controls   =" . $handler->getControls() . "\n");
190
}
191