Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — master ( 086918...38b64c )
by
unknown
03:43 queued 13s
created

ToolboxController::getEncryptedCoreName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 4
b 0
f 0
nc 2
nop 0
dl 0
loc 9
rs 10
1
<?php
2
/**
3
 * (c) Kitodo. Key to digital objects e.V. <[email protected]>
4
 *
5
 * This file is part of the Kitodo and TYPO3 projects.
6
 *
7
 * @license GNU General Public License version 3 or later.
8
 * For the full copyright and license information, please read the
9
 * LICENSE.txt file that was distributed with this source code.
10
 */
11
12
namespace Kitodo\Dlf\Controller;
13
14
use Kitodo\Dlf\Common\Helper;
15
use TYPO3\CMS\Core\Utility\GeneralUtility;
16
use TYPO3\CMS\Core\Utility\MathUtility;
17
18
/**
19
 * Controller class for plugin 'Toolbox'.
20
 *
21
 * @author Sebastian Meyer <[email protected]>
22
 * @package TYPO3
23
 * @subpackage dlf
24
 * @access public
25
 */
26
class ToolboxController extends AbstractController
27
{
28
29
    /**
30
     * This holds the current document
31
     *
32
     * @var \Kitodo\Dlf\Common\Doc
33
     * @access private
34
     */
35
    private $doc;
36
37
    /**
38
     * The main method of the plugin
39
     *
40
     * @return void
41
     */
42
    public function mainAction()
43
    {
44
        // Load current document.
45
        $this->loadDocument();
46
47
        $this->view->assign('double', $this->requestData['double']);
48
49
        if (!$this->isDocMissingOrEmpty()) {
50
            $this->doc = $this->document->getDoc();
51
        }
52
53
        $this->renderTool();
54
    }
55
56
    /**
57
     * Renders tool in the toolbox.
58
     *
59
     * @access private
60
     *
61
     * @return void
62
     */
63
    private function renderTool() {
64
        if (!empty($this->settings['tool'])) {
65
            switch ($this->settings['tool']) {
66
                case 'tx_dlf_annotationtool':
67
                case 'annotationtool':
68
                    $this->renderToolByName('renderAnnotationTool');
69
                    break;
70
                case 'tx_dlf_fulltextdownloadtool':
71
                case 'fulltextdownloadtool':
72
                    $this->renderToolByName('renderFulltextDownloadTool');
73
                    break;
74
                case 'tx_dlf_fulltexttool':
75
                case 'fulltexttool':
76
                    $this->renderToolByName('renderFulltextTool');
77
                    break;
78
                case 'tx_dlf_imagedownloadtool':
79
                case 'imagedownloadtool':
80
                    $this->renderToolByName('renderImageDownloadTool');
81
                    break;
82
                case 'tx_dlf_imagemanipulationtool':
83
                case 'imagemanipulationtool':
84
                    $this->renderToolByName('renderImageManipulationTool');
85
                    break;
86
                case 'tx_dlf_pdfdownloadtool':
87
                case 'pdfdownloadtool':
88
                    $this->renderToolByName('renderPdfDownloadTool');
89
                    break;
90
                case 'tx_dlf_searchindocumenttool':
91
                case 'searchindocumenttool':
92
                    $this->renderToolByName('renderSearchInDocumentTool');
93
                    break;
94
                default:
95
                    $this->logger->warning('Incorrect tool configuration: "' . $this->settings['tool'] . '". This tool does not exist.');
96
            }
97
        }
98
    }
99
100
    /**
101
     * Renders tool by the name in the toolbox.
102
     *
103
     * @access private
104
     *
105
     * @return void
106
     */
107
    private function renderToolByName(string $tool) {
108
        $this->$tool();
109
        $this->view->assign($tool, true);
110
    }
111
112
    /**
113
     * Renders the annotation tool
114
     *
115
     * @access private
116
     *
117
     * @return void
118
     */
119
    private function renderAnnotationTool()
120
    {
121
        if ($this->isDocMissingOrEmpty()) {
122
            // Quit without doing anything if required variables are not set.
123
            return '';
0 ignored issues
show
Bug Best Practice introduced by
The expression return '' returns the type string which is incompatible with the documented return type void.
Loading history...
124
        }
125
126
        $this->setPage();
127
128
        $annotationContainers = $this->doc->physicalStructureInfo[$this->doc->physicalStructure[$this->requestData['page']]]['annotationContainers'];
129
        if (
130
            $annotationContainers != null
131
            && sizeof($annotationContainers) > 0
132
        ) {
133
            $this->view->assign('annotationTool', true);
134
        } else {
135
            $this->view->assign('annotationTool', false);
136
        }
137
    }
138
139
    /**
140
     * Renders the fulltext download tool
141
     *
142
     * @access private
143
     *
144
     * @return void
145
     */
146
    private function renderFulltextDownloadTool()
147
    {
148
        if (
149
            $this->isDocMissingOrEmpty()
150
            || empty($this->extConf['fileGrpFulltext'])
151
        ) {
152
            // Quit without doing anything if required variables are not set.
153
            return '';
0 ignored issues
show
Bug Best Practice introduced by
The expression return '' returns the type string which is incompatible with the documented return type void.
Loading history...
154
        }
155
156
        $this->setPage();
157
158
        // Get text download.
159
        $this->view->assign('fulltextDownload', !$this->isFullTextEmpty());
160
    }
161
162
    /**
163
     * Renders the fulltext tool
164
     *
165
     * @access private
166
     *
167
     * @return void
168
     */
169
    private function renderFulltextTool()
170
    {
171
        if (
172
            $this->isDocMissingOrEmpty()
173
            || empty($this->extConf['fileGrpFulltext'])
174
        ) {
175
            // Quit without doing anything if required variables are not set.
176
            return '';
0 ignored issues
show
Bug Best Practice introduced by
The expression return '' returns the type string which is incompatible with the documented return type void.
Loading history...
177
        }
178
179
        $this->setPage();
180
181
        if (!$this->isFullTextEmpty()) {
182
            $this->view->assign('fulltext', true);
183
            $this->view->assign('activateFullTextInitially', MathUtility::forceIntegerInRange($this->settings['activateFullTextInitially'], 0, 1, 0));
184
        } else {
185
            $this->view->assign('fulltext', false);
186
        }
187
    }
188
189
    /**
190
     * Renders the image download tool
191
     *
192
     * @access private
193
     *
194
     * @return void
195
     */
196
    private function renderImageDownloadTool()
197
    {
198
        if (
199
            $this->isDocMissingOrEmpty()
200
            || empty($this->settings['fileGrpsImageDownload'])
201
        ) {
202
            // Quit without doing anything if required variables are not set.
203
            return '';
0 ignored issues
show
Bug Best Practice introduced by
The expression return '' returns the type string which is incompatible with the documented return type void.
Loading history...
204
        }
205
206
        $this->setPage();
207
208
        $imageArray = [];
209
        // Get left or single page download.
210
        $imageArray[0] = $this->getImage($this->requestData['page']);
211
        if ($this->requestData['double'] == 1) {
212
            $imageArray[1] = $this->getImage($this->requestData['page'] + 1);
213
        }
214
        $this->view->assign('imageDownload', $imageArray);
215
    }
216
217
    /**
218
     * Get image's URL and MIME type
219
     *
220
     * @access private
221
     *
222
     * @param int $page: Page number
223
     *
224
     * @return array Array of image links and image format information
225
     */
226
    private function getImage($page)
227
    {
228
        $image = [];
229
        // Get @USE value of METS fileGrp.
230
        $fileGrps = GeneralUtility::trimExplode(',', $this->settings['fileGrpsImageDownload']);
231
        while ($fileGrp = @array_pop($fileGrps)) {
232
            // Get image link.
233
            $fileGroup = $this->doc->physicalStructureInfo[$this->doc->physicalStructure[$page]]['files'][$fileGrp];
234
            if (!empty($fileGroup)) {
235
                $image['url'] = $this->doc->getDownloadLocation($fileGroup);
236
                $image['mimetype'] = $this->doc->getFileMimeType($fileGroup);
237
                switch ($image['mimetype']) {
238
                    case 'image/jpeg':
239
                        $image['mimetypeLabel']  = ' (JPG)';
240
                        break;
241
                    case 'image/tiff':
242
                        $image['mimetypeLabel']  = ' (TIFF)';
243
                        break;
244
                    default:
245
                        $image['mimetypeLabel']  = '';
246
                }
247
                break;
248
            } else {
249
                $this->logger->warning('File not found in fileGrp "' . $fileGrp . '"');
250
            }
251
        }
252
        return $image;
253
    }
254
255
    /**
256
     * Renders the image manipulation tool
257
     *
258
     * @access private
259
     *
260
     * @return void
261
     */
262
    private function renderImageManipulationTool()
263
    {
264
        // Set parent element for initialization.
265
        $parentContainer = !empty($this->settings['parentContainer']) ? $this->settings['parentContainer'] : '.tx-dlf-imagemanipulationtool';
266
267
        $this->view->assign('imageManipulation', true);
268
        $this->view->assign('parentContainer', $parentContainer);
269
    }
270
271
    /**
272
     * Renders the PDF download tool
273
     *
274
     * @access private
275
     *
276
     * @return void
277
     */
278
    private function renderPdfDownloadTool()
279
    {
280
        if (
281
            $this->isDocMissingOrEmpty()
282
            || empty($this->extConf['fileGrpDownload'])
283
        ) {
284
            // Quit without doing anything if required variables are not set.
285
            return '';
0 ignored issues
show
Bug Best Practice introduced by
The expression return '' returns the type string which is incompatible with the documented return type void.
Loading history...
286
        }
287
288
        $this->setPage();
289
290
        // Get single page downloads.
291
        $this->view->assign('pageLinks', $this->getPageLink());
292
        // Get work download.
293
        $this->view->assign('workLink', $this->getWorkLink());
294
    }
295
296
    /**
297
     * Get page's download link
298
     *
299
     * @access private
300
     *
301
     * @return array Link to downloadable page
302
     */
303
    private function getPageLink()
304
    {
305
        $firstPageLink = '';
306
        $secondPageLink = '';
307
        $pageLinkArray = [];
308
        $pageNumber = $this->requestData['page'];
309
        $fileGrpsDownload = GeneralUtility::trimExplode(',', $this->extConf['fileGrpDownload']);
310
        // Get image link.
311
        while ($fileGrpDownload = array_shift($fileGrpsDownload)) {
312
            $firstFileGroupDownload = $this->doc->physicalStructureInfo[$this->doc->physicalStructure[$pageNumber]]['files'][$fileGrpDownload];
313
            if (!empty($firstFileGroupDownload)) {
314
                $firstPageLink = $this->doc->getFileLocation($firstFileGroupDownload);
315
                // Get second page, too, if double page view is activated.
316
                $secondFileGroupDownload = $this->doc->physicalStructureInfo[$this->doc->physicalStructure[$pageNumber + 1]]['files'][$fileGrpDownload];
317
                if (
318
                    $this->requestData['double']
319
                    && $pageNumber < $this->doc->numPages
320
                    && !empty($secondFileGroupDownload)
321
                ) {
322
                    $secondPageLink = $this->doc->getFileLocation($secondFileGroupDownload);
323
                }
324
                break;
325
            }
326
        }
327
        if (
328
            empty($firstPageLink)
329
            && empty($secondPageLink)
330
        ) {
331
            $this->logger->warning('File not found in fileGrps "' . $this->extConf['fileGrpDownload'] . '"');
332
        }
333
334
        if (!empty($firstPageLink)) {
335
            $pageLinkArray[0] = $firstPageLink;
336
        }
337
        if (!empty($secondPageLink)) {
338
            $pageLinkArray[1] = $secondPageLink;
339
        }
340
        return $pageLinkArray;
341
    }
342
343
    /**
344
     * Get work's download link
345
     *
346
     * @access private
347
     *
348
     * @return string Link to downloadable work
349
     */
350
    private function getWorkLink()
351
    {
352
        $workLink = '';
353
        $fileGrpsDownload = GeneralUtility::trimExplode(',', $this->extConf['fileGrpDownload']);
354
        // Get work link.
355
        while ($fileGrpDownload = array_shift($fileGrpsDownload)) {
356
            $fileGroupDownload = $this->doc->physicalStructureInfo[$this->doc->physicalStructure[0]]['files'][$fileGrpDownload];
357
            if (!empty($fileGroupDownload)) {
358
                $workLink = $this->doc->getFileLocation($fileGroupDownload);
359
                break;
360
            } else {
361
                $details = $this->doc->getLogicalStructure($this->doc->toplevelId);
362
                if (!empty($details['files'][$fileGrpDownload])) {
363
                    $workLink = $this->doc->getFileLocation($details['files'][$fileGrpDownload]);
364
                    break;
365
                }
366
            }
367
        }
368
        if (empty($workLink)) {
369
            $this->logger->warning('File not found in fileGrps "' . $this->extConf['fileGrpDownload'] . '"');
370
        }
371
        return $workLink;
372
    }
373
374
    /**
375
     * Renders the searchInDocument tool
376
     *
377
     * @access private
378
     *
379
     * @return void
380
     */
381
    private function renderSearchInDocumentTool()
382
    {
383
        if (
384
            $this->isDocMissingOrEmpty()
385
            || empty($this->extConf['fileGrpFulltext'])
386
            || empty($this->settings['solrcore'])
387
        ) {
388
            // Quit without doing anything if required variables are not set.
389
            return '';
0 ignored issues
show
Bug Best Practice introduced by
The expression return '' returns the type string which is incompatible with the documented return type void.
Loading history...
390
        }
391
392
        $this->setPage();
393
394
        // Quit if no fulltext file is present
395
        if ($this->isFullTextEmpty()) {
396
            return;
397
        }
398
399
        // Fill markers.
400
        $viewArray = [
401
            'LABEL_QUERY_URL' => $this->settings['queryInputName'],
402
            'LABEL_START' => $this->settings['startInputName'],
403
            'LABEL_ID' => $this->settings['idInputName'],
404
            'LABEL_PAGE_URL' => $this->settings['pageInputName'],
405
            'LABEL_HIGHLIGHT_WORD' => $this->settings['highlightWordInputName'],
406
            'LABEL_ENCRYPTED' => $this->settings['encryptedInputName'],
407
            'CURRENT_DOCUMENT' => $this->getCurrentDocumentId(),
408
            'SOLR_ENCRYPTED' => $this->getEncryptedCoreName() ? : ''
409
        ];
410
411
        $this->view->assign('searchInDocument', $viewArray);
412
    }
413
414
    /**
415
     * Get current document id. As default the uid will be used.
416
     * In case there is defined documentIdUrlSchema then the id will
417
     * extracted from this URL.
418
     *
419
     * @access private
420
     *
421
     * @return string with current document id
422
     */
423
    private function getCurrentDocumentId()
424
    {
425
        $id = $this->document->getUid();
426
427
        if ($id !== null && $id > 0) {
428
            // we found the document uid
429
            return (string) $id;
430
        } else {
431
            $id = $this->requestData['id'];
432
            if (!GeneralUtility::isValidUrl($id)) {
433
                // we found no valid URI --> something unexpected we cannot search within.
434
                return '';
435
            }
436
        }
437
438
        // example: https://host.de/items/*id*/record
439
        if (!empty($this->settings['documentIdUrlSchema'])) {
440
            $arr = explode('*', $this->settings['documentIdUrlSchema']);
441
442
            if (count($arr) == 2) {
443
                $id = explode($arr[0], $id)[0];
444
            } else if (count($arr) == 3) {
445
                $sub = substr($id, strpos($id, $arr[0]) + strlen($arr[0]), strlen($id));
446
                $id = substr($sub, 0, strpos($sub, $arr[2]));
447
            }
448
        }
449
        return $id;
450
    }
451
452
    /**
453
     * Get the encrypted Solr core name
454
     *
455
     * @access private
456
     *
457
     * @return string with encrypted core name
458
     */
459
    private function getEncryptedCoreName()
460
    {
461
        // Get core name.
462
        $name = Helper::getIndexNameFromUid($this->settings['solrcore'], 'tx_dlf_solrcores');
463
        // Encrypt core name.
464
        if (!empty($name)) {
465
            $name = Helper::encrypt($name);
466
        }
467
        return $name;
468
    }
469
470
    /**
471
     * Check if the full text is empty.
472
     *
473
     * @access private
474
     *
475
     * @return bool true if empty, false otherwise
476
     */
477
    private function isFullTextEmpty() {
478
        $fileGrpsFulltext = GeneralUtility::trimExplode(',', $this->extConf['fileGrpFulltext']);
479
        while ($fileGrpFulltext = array_shift($fileGrpsFulltext)) {
480
            $fullTextFile = $this->doc->physicalStructureInfo[$this->doc->physicalStructure[$this->requestData['page']]]['files'][$fileGrpFulltext];
481
            if (!empty($fullTextFile)) {
482
                break;
483
            }
484
        }
485
        return empty($fullTextFile);
486
    }
487
}
488