userAllowed()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
c 0
b 0
f 0
nc 3
nop 2
dl 0
loc 10
rs 10
1
<?php declare(strict_types=1);
2
3
/*
4
 * You may not change or alter any portion of this comment or credits
5
 * of supporting developers from this source code or any supporting source code
6
 * which is considered copyrighted (c) material of the original comment or credit authors.
7
 *
8
 * This program is distributed in the hope that it will be useful,
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 */
12
13
/**
14
 * @copyright    {@link https://xoops.org/ XOOPS Project}
15
 * @license      {@link https://www.gnu.org/licenses/gpl-2.0.html GNU GPL 2 or later}
16
 * @author       Brian Wahoff <[email protected]>
17
 * @author       Eric Juden <[email protected]>
18
 * @author       XOOPS Development Team
19
 */
20
21
use Xmf\Request;
22
use XoopsModules\Xhelp;
23
24
require __DIR__ . '/header.php';
25
require_once \dirname(__DIR__, 2) . '/mainfile.php';
26
27
if (!defined('XHELP_CONSTANTS_INCLUDED')) {
28
    require_once XOOPS_ROOT_PATH . '/modules/xhelp/include/constants.php';
29
}
30
31
//require_once XHELP_BASE_PATH . '/functions.php';
32
33
global $xoopsUser, $xoopsDB, $xoopsConfig, $xoopsModuleConfig, $xoopsModule, $xoopsTpl, $xoopsRequestUri;
34
$helper = Xhelp\Helper::getInstance();
35
36
if (!$xoopsUser) {
37
    redirect_header(XOOPS_URL . '/user.php?xoops_redirect=' . htmlspecialchars($xoopsRequestUri, ENT_QUOTES | ENT_HTML5), 3);
38
}
39
$xhelp_id = 0;
40
41
if (Request::hasVar('id', 'GET')) {
42
    $xhelp_id = Request::getInt('id', 0, 'GET');
43
}
44
45
$viewFile = false;
46
47
/** @var \XoopsModules\Xhelp\FileHandler $fileHandler */
48
$fileHandler = $helper->getHandler('File');
49
/** @var \XoopsModules\Xhelp\TicketHandler $ticketHandler */
50
$ticketHandler = $helper->getHandler('Ticket');
51
/** @var \XoopsModules\Xhelp\StaffHandler $staffHandler */
52
$staffHandler = $helper->getHandler('Staff');
53
$file         = $fileHandler->get($xhelp_id);
54
$mimeType     = $file->getVar('mimetype');
55
$ticket       = $ticketHandler->get($file->getVar('ticketid'));
56
57
$filename_full = $file->getVar('filename');
58
if ($file->getVar('responseid') > 0) {
59
    $removeText = $file->getVar('ticketid') . '_' . $file->getVar('responseid') . '_';
60
} else {
61
    $removeText = $file->getVar('ticketid') . '_';
62
}
63
$filename = str_replace($removeText, '', $filename_full);
64
65
//Security:
66
// Only Staff Members, Admins, or ticket Submitter should be able to see file
67
if (userAllowed($ticket, $xoopsUser)) {
0 ignored issues
show
Bug introduced by
It seems like $ticket can also be of type false; however, parameter $ticket of userAllowed() does only seem to accept XoopsModules\Xhelp\Ticket, maybe add an additional type check? ( Ignorable by Annotation )

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

67
if (userAllowed(/** @scrutinizer ignore-type */ $ticket, $xoopsUser)) {
Loading history...
68
    $viewFile = true;
69
} elseif ($staffHandler->isStaff($xoopsUser->getVar('uid'))) {
70
    $viewFile = true;
71
} elseif ($xoopsUser->isAdmin($xoopsModule->getVar('mid'))) {
72
    $viewFile = true;
73
}
74
75
if (!$viewFile) {
76
    $helper->redirect('index.php', 3, _NOPERM);
77
}
78
79
//Check if the file exists
80
$fileAbsPath = XHELP_UPLOAD_PATH . '/' . $filename_full;
0 ignored issues
show
Bug introduced by
The constant XHELP_UPLOAD_PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
81
if (!file_exists($fileAbsPath)) {
82
    $helper->redirect('index.php', 3, _XHELP_NO_FILES_ERROR);
83
}
84
85
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
86
header('Cache-Control: private', false);
87
header('Content-Transfer-Encoding: binary');
88
header('Content-Length: ' . filesize($fileAbsPath));
89
90
if (isset($mimeType)) {
91
    header('Content-Type: ' . $mimeType);
92
} else {
93
    header('Content-Type: application/octet-stream');
94
}
95
96
// Add Header to set filename
97
header('Content-Disposition: attachment; filename=' . $filename);
98
99
// Open the file
100
if (isset($mimeType) && false !== mb_strpos($mimeType, 'text/')) {
101
    $fp = fopen($fileAbsPath, 'rb');
102
} else {
103
    $fp = fopen($fileAbsPath, 'rb');
104
}
105
106
// Write file to browser
107
fpassthru($fp);
108
109
/**
110
 * @param Xhelp\Ticket $ticket
111
 * @param XoopsUser    $user
112
 * @return bool
113
 */
114
function userAllowed(Xhelp\Ticket $ticket, XoopsUser $user): bool
115
{
116
    $emails = $ticket->getEmails(true);
117
    foreach ($emails as $email) {
118
        if ($email->getVar('email') == $user->getVar('email')) {
119
            return true;
120
        }
121
    }
122
123
    return false;
124
}
125