UB-Mannheim /
PalMA
| 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
|
|||
| 19 | { |
||
| 20 | return FileHandler::CURSOR | FileHandler::ZOOM; |
||
| 21 | } |
||
| 22 | function show($path) |
||
|
0 ignored issues
–
show
|
|||
| 23 | { |
||
| 24 | } |
||
| 25 | } |
||
| 26 | |||
| 27 | class LibreOfficeHandler extends FileHandler |
||
| 28 | { |
||
| 29 | function getControls() |
||
|
0 ignored issues
–
show
|
|||
| 30 | { |
||
| 31 | return FileHandler::CURSOR | FileHandler::ZOOM; |
||
| 32 | } |
||
| 33 | function show($path) |
||
|
0 ignored issues
–
show
|
|||
| 34 | { |
||
| 35 | } |
||
| 36 | } |
||
| 37 | |||
| 38 | class MidoriHandler extends FileHandler |
||
| 39 | { |
||
| 40 | function getControls() |
||
|
0 ignored issues
–
show
|
|||
| 41 | { |
||
| 42 | return FileHandler::CURSOR | FileHandler::ZOOM; |
||
| 43 | } |
||
| 44 | function show($path) |
||
|
0 ignored issues
–
show
|
|||
| 45 | { |
||
| 46 | } |
||
| 47 | } |
||
| 48 | |||
| 49 | class VlcHandler extends FileHandler |
||
| 50 | { |
||
| 51 | function getControls() |
||
|
0 ignored issues
–
show
|
|||
| 52 | { |
||
| 53 | return FileHandler::CURSOR | FileHandler::ZOOM; |
||
| 54 | } |
||
| 55 | function show($path) |
||
|
0 ignored issues
–
show
|
|||
| 56 | { |
||
| 57 | } |
||
| 58 | } |
||
| 59 | |||
| 60 | class ZathuraHandler extends FileHandler |
||
| 61 | { |
||
| 62 | function getControls() |
||
|
0 ignored issues
–
show
|
|||
| 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
|
|||
| 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(); |
||
| 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 | /* |
||
| 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 |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.