FileHandler   A
last analyzed

Complexity

Total Complexity 29

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 58
dl 0
loc 99
rs 10
c 2
b 0
f 0
wmc 29

1 Method

Rating   Name   Duplication   Size   Complexity  
D getFileHandler() 0 68 29
1
<?php
2
3
namespace palma;
4
5
// Copyright (C) 2014-2023 Universitätsbibliothek Mannheim
6
// See file LICENSE for license details.
7
8
abstract class FileHandler
9
{
10
  // Constants for allowed controls.
11
  public const UP = 1;
12
  public const DOWN = 2;
13
  public const LEFT = 4;
14
  public const RIGHT = 8;
15
  public const ZOOMIN = 16;
16
  public const ZOOMOUT = 32;
17
  public const HOME = 64;
18
  public const END = 128;
19
  public const PRIOR = 256;
20
  public const NEXT = 512;
21
  public const DOWNLOAD = 1024;
22
  public const COUNTERCLOCKWISE = 2048;
23
  public const CLOCKWISE = 4096;
24
25
  // Shortcuts for combinations of controls.
26
  public const CURSOR = 15; // UP | DOWN | LEFT | RIGHT
27
  public const ZOOM = 48;   // ZOOMIN | ZOOMOUT
28
  public const ALL = 2047;
29
30
  // up down left right zoomin zoomout home end prior next download
31
32
  // protected $FILES = array();
33
  // protected $UPLOAD_PATH;
34
35
  abstract protected function getControls(): void;
36
  abstract protected function show(string $path): void;
37
38
  /** @return array<int,string> */
39
  public static function getFileHandler(string $file): array
40
  {
41
42
    // Get get directory, name and file extension
43
    $pathParts = pathinfo($file);
44
    $ftype = strtolower($pathParts['extension']);
45
    $fdir = $pathParts['dirname'];
46
    $fname = $pathParts['filename'];
47
    $fhandler = "";
48
49
    // Define filehandlers
50
    $pdfHandler = '/usr/bin/zathura';
51
    $imageHandler = '/usr/bin/feh --scale-down';
52
    $webHandler = '/usr/bin/x-www-browser';
53
    foreach (["/usr/lib/palma", "./scripts"] as $dir) {
54
      $palmaBrowser = $dir . "/palma-browser";
55
      if (file_exists($palmaBrowser)) {
56
        $webHandler = $palmaBrowser;
57
        break;
58
      }
59
    }
60
    $avHandler = '/usr/bin/cvlc --no-audio';
61
    $officeApp = "writer";
62
63
    // $params;
64
    // echo $ftype;
65
    if ($ftype === 'pdf') {
66
      $fhandler = $pdfHandler;
67
    } elseif ($ftype === 'gif' || $ftype === 'jpg' || $ftype === 'png') {
68
      $fhandler = $imageHandler;
69
    } elseif ($ftype === 'html' || $ftype === 'url') {
70
      $fhandler = $webHandler;
71
    } elseif (
72
        $ftype === 'mpg' || $ftype === 'mpeg' || $ftype === 'avi' ||
73
        $ftype === 'mp3' || $ftype === 'mp4' || $ftype === 'wmv'
74
    ) {
75
      $fhandler = $avHandler;
76
    } else {
77
      if ($ftype === 'doc' || $ftype === 'docx' || $ftype === 'odt' || $ftype === 'txt') {
78
        $officeApp = "writer";
79
      } elseif ($ftype === 'ppt' || $ftype === 'pptx' || $ftype === 'pps' || $ftype === 'ppsx' || $ftype === 'odp') {
80
        $officeApp = "impress";
81
      } elseif ($ftype === 'xls' || $ftype === 'xlsx' || $ftype === 'ods') {
82
        $officeApp = "calc";
83
      } elseif (shell_exec("/usr/bin/file -b '$file'") === "ASCII text") {
84
        $officeApp = "writer";
85
      }
86
      $convertedFile = convertOffice($file, $officeApp, $fdir, $fname);
87
      if ($convertedFile) {
88
        $file = $convertedFile;
89
        $fhandler = $pdfHandler;
90
      } else {
91
        $fhandler = "/usr/bin/libreoffice --'$officeApp' --nologo --norestore -o";
92
      }
93
    }
94
95
    /*
96
       alternatively with mime-types
97
98
       // $ftype = mime_content_type($this->UPLOAD_PATH.$file);
99
       // if($ftype=='application/pdf')
100
       // if($ftype=='image/gif' || $ftype=='image/jpg' || $ftype=='image/png' )
101
       // if($ftype=='html' || $ftype=='url' || $ftype="text/plain")
102
       // (...)
103
104
     */
105
106
    return array($fhandler, $file);
107
  }
108
}
109
110
function convertOffice(string $inputFile, string $office, string $outputDir, string $fileName): mixed
111
{
112
  $cmd = "/usr/bin/libreoffice --headless" .
113
         " --convert-to pdf:'$office'_pdf_Export" .
114
         " --outdir '$outputDir'" .
115
         " '$inputFile' >/dev/null 2>&1";
116
  shell_exec($cmd);
117
  $newFile = $outputDir . '/' . $fileName . '.pdf';
118
  if (file_exists($newFile)) {
119
    return $newFile;
120
  } else {
121
    return false;
122
  }
123
}
124